kaddressbook Library API Documentation

opera_xxport.cpp

00001 /*
00002     This file is part of KAddressbook.
00003     Copyright (c) 2003 - 2003 Daniel Molkentin <molkentin@kde.org>
00004                               Tobias Koenig <tokoe@kde.org>
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019 
00020     As a special exception, permission is given to link this program
00021     with any edition of Qt, and distribute the resulting executable,
00022     without including the source code for Qt in the source distribution.
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"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Oct 4 14:41:40 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003