kaddressbook

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 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"
KDE Home | KDE Accessibility Home | Description of Access Keys