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   // sanity checks
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"
KDE Logo
This file is part of the documentation for kaddressbook Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:38:53 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003