kaddressbook

opera_xxport.cpp
1 /*
2  This file is part of KAddressbook.
3  Copyright (c) 2003 - 2003 Daniel Molkentin <molkentin@kde.org>
4  Tobias Koenig <tokoe@kde.org>
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 
20  As a special exception, permission is given to link this program
21  with any edition of TQt, and distribute the resulting executable,
22  without including the source code for TQt in the source distribution.
23 */
24 
25 #include <tqfile.h>
26 #include <tqregexp.h>
27 
28 #include <tdefiledialog.h>
29 #include <tdeio/netaccess.h>
30 #include <tdelocale.h>
31 #include <tdemessagebox.h>
32 #include <tdetempfile.h>
33 #include <kurl.h>
34 
35 #include <kdebug.h>
36 
37 #include "opera_xxport.h"
38 
39 K_EXPORT_KADDRESSBOOK_XXFILTER( libkaddrbk_opera_xxport, OperaXXPort )
40 
41 OperaXXPort::OperaXXPort( TDEABC::AddressBook *ab, TQWidget *parent, const char *name )
42  : KAB::XXPort( ab, parent, name )
43 {
44  createImportAction( i18n( "Import Opera Addressbook..." ) );
45 }
46 
47 TDEABC::AddresseeList OperaXXPort::importContacts( const TQString& ) const
48 {
49  TDEABC::AddresseeList addrList;
50 
51  TQString fileName = KFileDialog::getOpenFileName( TQDir::homeDirPath() + TQString::fromLatin1( "/.opera/contacts.adr" ) );
52  if ( fileName.isEmpty() )
53  return addrList;
54 
55  TQFile file( fileName );
56  if ( !file.open( IO_ReadOnly ) ) {
57  TQString msg = i18n( "<qt>Unable to open <b>%1</b> for reading.</qt>" );
58  KMessageBox::error( parentWidget(), msg.arg( fileName ) );
59  return addrList;
60  }
61 
62  TQTextStream stream( &file );
63  stream.setEncoding( TQTextStream::UnicodeUTF8 );
64  TQString line, key, value;
65  bool parseContact = false;
66  TDEABC::Addressee addr;
67 
68  TQRegExp separator( "\x02\x02" );
69 
70  while ( !stream.atEnd() ) {
71  line = stream.readLine();
72  line = line.stripWhiteSpace();
73  if ( line == TQString::fromLatin1( "#CONTACT" ) ) {
74  parseContact = true;
75  addr = TDEABC::Addressee();
76  continue;
77  } else if ( line.isEmpty() ) {
78  parseContact = false;
79  if ( !addr.isEmpty() ) {
80  addrList.append( addr );
81  addr = TDEABC::Addressee();
82  }
83  continue;
84  }
85 
86  if ( parseContact == true ) {
87  int sep = line.find( '=' );
88  key = line.left( sep ).lower();
89  value = line.mid( sep + 1 );
90  if ( key == TQString::fromLatin1( "name" ) )
91  addr.setNameFromString( value );
92  else if ( key == TQString::fromLatin1( "mail" ) ) {
93  TQStringList emails = TQStringList::split( separator, value );
94 
95  TQStringList::Iterator it = emails.begin();
96  bool preferred = true;
97  for ( ; it != emails.end(); ++it ) {
98  addr.insertEmail( *it, preferred );
99  preferred = false;
100  }
101  } else if ( key == TQString::fromLatin1( "phone" ) )
102  addr.insertPhoneNumber( TDEABC::PhoneNumber( value ) );
103  else if ( key == TQString::fromLatin1( "fax" ) )
104  addr.insertPhoneNumber( TDEABC::PhoneNumber( value,
105  TDEABC::PhoneNumber::Fax | TDEABC::PhoneNumber::Home ) );
106  else if ( key == TQString::fromLatin1( "postaladdress" ) ) {
107  TDEABC::Address address( TDEABC::Address::Home );
108  address.setLabel( value.replace( separator, "\n" ) );
109  addr.insertAddress( address );
110  } else if ( key == TQString::fromLatin1( "description" ) )
111  addr.setNote( value.replace( separator, "\n" ) );
112  else if ( key == TQString::fromLatin1( "url" ) )
113  addr.setUrl( KURL( value ) );
114  else if ( key == TQString::fromLatin1( "pictureurl" ) ) {
115  TDEABC::Picture pic( value );
116  addr.setPhoto( pic );
117  }
118  }
119  }
120 
121  file.close();
122 
123  return addrList;
124 }
125 
126 #include "opera_xxport.moc"