opera_xxport.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <qfile.h>
00026 #include <qregexp.h>
00027
00028 #include <kfiledialog.h>
00029 #include <kio/netaccess.h>
00030 #include <klocale.h>
00031 #include <kmessagebox.h>
00032 #include <ktempfile.h>
00033 #include <kurl.h>
00034
00035 #include <kdebug.h>
00036
00037 #include "opera_xxport.h"
00038
00039 class OperaXXPortFactory : public KAB::XXPortFactory
00040 {
00041 public:
00042 KAB::XXPort *xxportObject( KABC::AddressBook *ab, QWidget *parent, const char *name )
00043 {
00044 return new OperaXXPort( ab, parent, name );
00045 }
00046 };
00047
00048 extern "C"
00049 {
00050 void *init_libkaddrbk_opera_xxport()
00051 {
00052 return ( new OperaXXPortFactory() );
00053 }
00054 }
00055
00056
00057 OperaXXPort::OperaXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name )
00058 : KAB::XXPort( ab, parent, name )
00059 {
00060 createImportAction( i18n( "Import Opera Addressbook..." ) );
00061 }
00062
00063 KABC::AddresseeList OperaXXPort::importContacts( const QString& ) const
00064 {
00065 KABC::AddresseeList addrList;
00066
00067
00068 QFile file( QDir::homeDirPath() + QString::fromLatin1( "/.opera/contacts.adr" ) );
00069 if ( !file.open( IO_ReadOnly ) )
00070 return addrList;
00071
00072 QTextStream stream( &file );
00073 stream.setEncoding( QTextStream::UnicodeUTF8 );
00074 QString line, key, value;
00075 bool parseContact = false;
00076 KABC::Addressee addr;
00077
00078 QRegExp separator( "\x02\x02" );
00079
00080 while ( !stream.atEnd() ) {
00081 line = stream.readLine();
00082 line = line.stripWhiteSpace();
00083 if ( line == QString::fromLatin1( "#CONTACT" ) ) {
00084 parseContact = true;
00085 addr = KABC::Addressee();
00086 continue;
00087 } else if ( line.isEmpty() ) {
00088 parseContact = false;
00089 if ( !addr.isEmpty() )
00090 addrList.append( addr );
00091 continue;
00092 }
00093
00094 if ( parseContact == true ) {
00095 int sep = line.find( '=' );
00096 key = line.left( sep ).lower();
00097 value = line.mid( sep + 1 );
00098 if ( key == QString::fromLatin1( "name" ) )
00099 addr.setNameFromString( value );
00100 else if ( key == QString::fromLatin1( "mail" ) ) {
00101 QStringList emails = QStringList::split( separator, value );
00102
00103 QStringList::Iterator it = emails.begin();
00104 bool preferred = true;
00105 for ( ; it != emails.end(); ++it ) {
00106 addr.insertEmail( *it, preferred );
00107 preferred = false;
00108 }
00109 } else if ( key == QString::fromLatin1( "phone" ) )
00110 addr.insertPhoneNumber( KABC::PhoneNumber( value ) );
00111 else if ( key == QString::fromLatin1( "fax" ) )
00112 addr.insertPhoneNumber( KABC::PhoneNumber( value,
00113 KABC::PhoneNumber::Fax | KABC::PhoneNumber::Home ) );
00114 else if ( key == QString::fromLatin1( "postaladdress" ) ) {
00115 KABC::Address address( KABC::Address::Home );
00116 address.setLabel( value.replace( separator, "\n" ) );
00117 addr.insertAddress( address );
00118 } else if ( key == QString::fromLatin1( "description" ) )
00119 addr.setNote( value.replace( separator, "\n" ) );
00120 else if ( key == QString::fromLatin1( "url" ) )
00121 addr.setUrl( KURL( value ) );
00122 else if ( key == QString::fromLatin1( "pictureurl" ) ) {
00123 KABC::Picture pic( value );
00124 addr.setPhoto( pic );
00125 }
00126 }
00127 }
00128
00129 file.close();
00130
00131 return addrList;
00132 }
00133
00134 #include "opera_xxport.moc"
This file is part of the documentation for kaddressbook Library Version 3.2.2.