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 QString fileName = KFileDialog::getOpenFileName( QDir::homeDirPath() + QString::fromLatin1( "/.opera/contacts.adr" ) );
00068 if ( fileName.isEmpty() )
00069 return addrList;
00070
00071 QFile file( fileName );
00072 if ( !file.open( IO_ReadOnly ) ) {
00073 QString msg = i18n( "<qt>Unable to open <b>%1</b> for reading.</qt>" );
00074 KMessageBox::error( parentWidget(), msg.arg( fileName ) );
00075 return addrList;
00076 }
00077
00078 QTextStream stream( &file );
00079 stream.setEncoding( QTextStream::UnicodeUTF8 );
00080 QString line, key, value;
00081 bool parseContact = false;
00082 KABC::Addressee addr;
00083
00084 QRegExp separator( "\x02\x02" );
00085
00086 while ( !stream.atEnd() ) {
00087 line = stream.readLine();
00088 line = line.stripWhiteSpace();
00089 if ( line == QString::fromLatin1( "#CONTACT" ) ) {
00090 parseContact = true;
00091 addr = KABC::Addressee();
00092 continue;
00093 } else if ( line.isEmpty() ) {
00094 parseContact = false;
00095 if ( !addr.isEmpty() ) {
00096 addrList.append( addr );
00097 addr = KABC::Addressee();
00098 }
00099 continue;
00100 }
00101
00102 if ( parseContact == true ) {
00103 int sep = line.find( '=' );
00104 key = line.left( sep ).lower();
00105 value = line.mid( sep + 1 );
00106 if ( key == QString::fromLatin1( "name" ) )
00107 addr.setNameFromString( value );
00108 else if ( key == QString::fromLatin1( "mail" ) ) {
00109 QStringList emails = QStringList::split( separator, value );
00110
00111 QStringList::Iterator it = emails.begin();
00112 bool preferred = true;
00113 for ( ; it != emails.end(); ++it ) {
00114 addr.insertEmail( *it, preferred );
00115 preferred = false;
00116 }
00117 } else if ( key == QString::fromLatin1( "phone" ) )
00118 addr.insertPhoneNumber( KABC::PhoneNumber( value ) );
00119 else if ( key == QString::fromLatin1( "fax" ) )
00120 addr.insertPhoneNumber( KABC::PhoneNumber( value,
00121 KABC::PhoneNumber::Fax | KABC::PhoneNumber::Home ) );
00122 else if ( key == QString::fromLatin1( "postaladdress" ) ) {
00123 KABC::Address address( KABC::Address::Home );
00124 address.setLabel( value.replace( separator, "\n" ) );
00125 addr.insertAddress( address );
00126 } else if ( key == QString::fromLatin1( "description" ) )
00127 addr.setNote( value.replace( separator, "\n" ) );
00128 else if ( key == QString::fromLatin1( "url" ) )
00129 addr.setUrl( KURL( value ) );
00130 else if ( key == QString::fromLatin1( "pictureurl" ) ) {
00131 KABC::Picture pic( value );
00132 addr.setPhoto( pic );
00133 }
00134 }
00135 }
00136
00137 file.close();
00138
00139 return addrList;
00140 }
00141
00142 #include "opera_xxport.moc"
This file is part of the documentation for kaddressbook Library Version 3.3.2.