kaddressbook
opera_xxport.cpp00001
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 K_EXPORT_KADDRESSBOOK_XXFILTER( libkaddrbk_opera_xxport, OperaXXPort )
00040
00041 OperaXXPort::OperaXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name )
00042 : KAB::XXPort( ab, parent, name )
00043 {
00044 createImportAction( i18n( "Import Opera Addressbook..." ) );
00045 }
00046
00047 KABC::AddresseeList OperaXXPort::importContacts( const QString& ) const
00048 {
00049 KABC::AddresseeList addrList;
00050
00051 QString fileName = KFileDialog::getOpenFileName( QDir::homeDirPath() + QString::fromLatin1( "/.opera/contacts.adr" ) );
00052 if ( fileName.isEmpty() )
00053 return addrList;
00054
00055 QFile file( fileName );
00056 if ( !file.open( IO_ReadOnly ) ) {
00057 QString msg = i18n( "<qt>Unable to open <b>%1</b> for reading.</qt>" );
00058 KMessageBox::error( parentWidget(), msg.arg( fileName ) );
00059 return addrList;
00060 }
00061
00062 QTextStream stream( &file );
00063 stream.setEncoding( QTextStream::UnicodeUTF8 );
00064 QString line, key, value;
00065 bool parseContact = false;
00066 KABC::Addressee addr;
00067
00068 QRegExp separator( "\x02\x02" );
00069
00070 while ( !stream.atEnd() ) {
00071 line = stream.readLine();
00072 line = line.stripWhiteSpace();
00073 if ( line == QString::fromLatin1( "#CONTACT" ) ) {
00074 parseContact = true;
00075 addr = KABC::Addressee();
00076 continue;
00077 } else if ( line.isEmpty() ) {
00078 parseContact = false;
00079 if ( !addr.isEmpty() ) {
00080 addrList.append( addr );
00081 addr = KABC::Addressee();
00082 }
00083 continue;
00084 }
00085
00086 if ( parseContact == true ) {
00087 int sep = line.find( '=' );
00088 key = line.left( sep ).lower();
00089 value = line.mid( sep + 1 );
00090 if ( key == QString::fromLatin1( "name" ) )
00091 addr.setNameFromString( value );
00092 else if ( key == QString::fromLatin1( "mail" ) ) {
00093 QStringList emails = QStringList::split( separator, value );
00094
00095 QStringList::Iterator it = emails.begin();
00096 bool preferred = true;
00097 for ( ; it != emails.end(); ++it ) {
00098 addr.insertEmail( *it, preferred );
00099 preferred = false;
00100 }
00101 } else if ( key == QString::fromLatin1( "phone" ) )
00102 addr.insertPhoneNumber( KABC::PhoneNumber( value ) );
00103 else if ( key == QString::fromLatin1( "fax" ) )
00104 addr.insertPhoneNumber( KABC::PhoneNumber( value,
00105 KABC::PhoneNumber::Fax | KABC::PhoneNumber::Home ) );
00106 else if ( key == QString::fromLatin1( "postaladdress" ) ) {
00107 KABC::Address address( KABC::Address::Home );
00108 address.setLabel( value.replace( separator, "\n" ) );
00109 addr.insertAddress( address );
00110 } else if ( key == QString::fromLatin1( "description" ) )
00111 addr.setNote( value.replace( separator, "\n" ) );
00112 else if ( key == QString::fromLatin1( "url" ) )
00113 addr.setUrl( KURL( value ) );
00114 else if ( key == QString::fromLatin1( "pictureurl" ) ) {
00115 KABC::Picture pic( value );
00116 addr.setPhoto( pic );
00117 }
00118 }
00119 }
00120
00121 file.close();
00122
00123 return addrList;
00124 }
00125
00126 #include "opera_xxport.moc"
|