kaddressbook
opera_xxport.cpp
Go to the documentation of this file.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 <QtCore/QFile>
00026 #include <QtCore/QRegExp>
00027 #include <QtCore/QTextStream>
00028
00029 #include <kfiledialog.h>
00030 #include <kio/netaccess.h>
00031 #include <klocale.h>
00032 #include <kmessagebox.h>
00033 #include <ktemporaryfile.h>
00034 #include <kurl.h>
00035
00036 #include <kdebug.h>
00037
00038 #include "opera_xxport.h"
00039
00040 K_EXPORT_KADDRESSBOOK_XXFILTER( kaddrbk_opera_xxport, OperaXXPort )
00041
00042 OperaXXPort::OperaXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name )
00043 : KAB::XXPort( ab, parent, name )
00044 {
00045 createImportAction( i18n( "Import Opera Addressbook..." ) );
00046 }
00047
00048 KABC::Addressee::List OperaXXPort::importContacts( const QString& ) const
00049 {
00050 KABC::Addressee::List addrList;
00051
00052 QString fileName = KFileDialog::getOpenFileName( QDir::homePath() + QString::fromLatin1( "/.opera/contacts.adr" ) );
00053 if ( fileName.isEmpty() )
00054 return addrList;
00055
00056 QFile file( fileName );
00057 if ( !file.open( QIODevice::ReadOnly ) ) {
00058 QString msg = i18n( "<qt>Unable to open <b>%1</b> for reading.</qt>", fileName );
00059 KMessageBox::error( parentWidget(), msg );
00060 return addrList;
00061 }
00062
00063 QTextStream stream( &file );
00064 stream.setCodec( "UTF-8" );
00065 QString line, key, value;
00066 bool parseContact = false;
00067 KABC::Addressee addr;
00068
00069 QRegExp separator( "\x02\x02" );
00070
00071 while ( !stream.atEnd() ) {
00072 line = stream.readLine();
00073 line = line.trimmed();
00074 if ( line == QString::fromLatin1( "#CONTACT" ) ) {
00075 parseContact = true;
00076 addr = KABC::Addressee();
00077 continue;
00078 } else if ( line.isEmpty() ) {
00079 parseContact = false;
00080 if ( !addr.isEmpty() ) {
00081 addrList.append( addr );
00082 addr = KABC::Addressee();
00083 }
00084 continue;
00085 }
00086
00087 if ( parseContact == true ) {
00088 int sep = line.indexOf( '=' );
00089 key = line.left( sep ).toLower();
00090 value = line.mid( sep + 1 );
00091 if ( key == QString::fromLatin1( "name" ) )
00092 addr.setNameFromString( value );
00093 else if ( key == QString::fromLatin1( "mail" ) ) {
00094 QStringList emails = value.split( separator, QString::SkipEmptyParts );
00095
00096 QStringList::Iterator it = emails.begin();
00097 bool preferred = true;
00098 for ( ; it != emails.end(); ++it ) {
00099 addr.insertEmail( *it, preferred );
00100 preferred = false;
00101 }
00102 } else if ( key == QString::fromLatin1( "phone" ) )
00103 addr.insertPhoneNumber( KABC::PhoneNumber( value ) );
00104 else if ( key == QString::fromLatin1( "fax" ) )
00105 addr.insertPhoneNumber( KABC::PhoneNumber( value,
00106 KABC::PhoneNumber::Fax | KABC::PhoneNumber::Home ) );
00107 else if ( key == QString::fromLatin1( "postaladdress" ) ) {
00108 KABC::Address address( KABC::Address::Home );
00109 address.setLabel( value.replace( separator, "\n" ) );
00110 addr.insertAddress( address );
00111 } else if ( key == QString::fromLatin1( "description" ) )
00112 addr.setNote( value.replace( separator, "\n" ) );
00113 else if ( key == QString::fromLatin1( "url" ) )
00114 addr.setUrl( KUrl( value ) );
00115 else if ( key == QString::fromLatin1( "pictureurl" ) ) {
00116 KABC::Picture pic( value );
00117 addr.setPhoto( pic );
00118 }
00119 }
00120 }
00121
00122 file.close();
00123
00124 return addrList;
00125 }
00126
00127 #include "opera_xxport.moc"