kaddressbook Library API Documentation

eudora_xxport.cpp

00001 /*
00002     This file is part of KAddressbook.
00003     Copyright (c) 2003 Daniel Molkentin <molkentin@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <qfile.h>
00025 
00026 #include <kfiledialog.h>
00027 #include <kio/netaccess.h>
00028 #include <klocale.h>
00029 #include <kmessagebox.h>
00030 #include <ktempfile.h>
00031 #include <kurl.h>
00032 
00033 #include <kdebug.h>
00034 
00035 #include "eudora_xxport.h"
00036 
00037 #define CTRL_C 3
00038 
00039 class EudoraXXPortFactory : public KAB::XXPortFactory
00040 {
00041   public:
00042     KAB::XXPort *xxportObject( KABC::AddressBook *ab, QWidget *parent, const char *name )
00043     {
00044       return new EudoraXXPort( ab, parent, name );
00045     }
00046 };
00047 
00048 extern "C"
00049 {
00050   void *init_libkaddrbk_eudora_xxport()
00051   {
00052     return ( new EudoraXXPortFactory() );
00053   }
00054 }
00055 
00056 
00057 EudoraXXPort::EudoraXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name )
00058   : KAB::XXPort( ab, parent, name )
00059 {
00060   createImportAction( i18n( "Import Eudora Addressbook..." ) );
00061 }
00062 
00063 KABC::AddresseeList EudoraXXPort::importContacts( const QString& ) const
00064 {
00065   QString fileName = KFileDialog::getOpenFileName( QDir::homeDirPath(), 
00066         "*.[tT][xX][tT]|" + i18n("Eudora Light Addressbook (*.txt)"), 0 );
00067   if ( fileName.isEmpty() )
00068     return KABC::AddresseeList();
00069 
00070   QFile file( fileName );
00071   if ( !file.open( IO_ReadOnly ) )
00072     return KABC::AddresseeList();
00073 
00074   QString line;
00075   QTextStream stream( &file );
00076   KABC::Addressee *a = 0;
00077   int bytesRead = 0;
00078 
00079   KABC::AddresseeList list;
00080 
00081   while( !stream.eof() ) {
00082     line = stream.readLine();
00083     bytesRead += line.length();
00084     QString tmp;
00085 
00086     if ( line.startsWith( "alias" ) ) {
00087       if ( a ) { // Write it out
00088         list << *a;
00089         delete a;
00090         a = 0;
00091         a = new KABC::Addressee();
00092       } else
00093         a = new KABC::Addressee();
00094 
00095       tmp = key( line ).stripWhiteSpace();
00096       if ( !tmp.isEmpty() )
00097         a->setFormattedName( tmp );
00098 
00099       tmp = email( line ).stripWhiteSpace();
00100       if ( !tmp.isEmpty() )
00101         a->insertEmail( tmp );
00102     } else if ( line.startsWith( "note" ) ) {
00103       if ( !a ) // Must have an alias before a note
00104         break;
00105 
00106       tmp = comment( line ).stripWhiteSpace();
00107       if ( !tmp.isEmpty() )
00108         a->setNote( tmp );
00109 
00110       tmp = get( line, "name" ).stripWhiteSpace();
00111       if ( !tmp.isEmpty() )
00112         a->setNameFromString( tmp );
00113 
00114       tmp = get( line, "address" ).stripWhiteSpace();
00115       if ( !tmp.isEmpty() ) {
00116         KABC::Address addr;
00117         kdDebug() << tmp << endl; // dump complete address
00118         addr.setLabel( tmp );
00119         a->insertAddress( addr );
00120       }
00121 
00122       tmp = get( line, "phone" ).stripWhiteSpace();
00123       if ( !tmp.isEmpty() )
00124          a->insertPhoneNumber( KABC::PhoneNumber( tmp, KABC::PhoneNumber::Home ) );
00125     }
00126   }
00127 
00128   if ( a ) { // Write out address
00129     list << *a;
00130     delete a;
00131     a = 0;
00132   }
00133 
00134   file.close();
00135 
00136   return list;
00137 }
00138 
00139 QString EudoraXXPort::key( const QString& line) const
00140 {
00141   int e;
00142   QString result;
00143   int b = line.find( '\"', 0 );
00144 
00145   if ( b == -1 ) { 
00146     b = line.find( ' ' );
00147     if ( b == -1 )
00148       return result;
00149 
00150     b++;
00151     e = line.find( ' ', b );
00152     result = line.mid( b, e - b );
00153 
00154     return result;
00155   }
00156 
00157   b++;
00158   e = line.find( '\"', b );
00159   if ( e == -1 )
00160     return result;
00161 
00162   result = line.mid( b, e - b );
00163 
00164   return result;
00165 }
00166 
00167 QString EudoraXXPort::email( const QString& line ) const
00168 {
00169   int b;
00170   QString result;
00171   b = line.findRev( '\"' );
00172   if ( b == -1 ) { 
00173     b = line.findRev( ' ' );
00174     if( b == -1 )
00175       return result;
00176   }
00177   result = line.mid( b + 1 );
00178 
00179   return result;
00180 }
00181 
00182 QString EudoraXXPort::comment( const QString& line ) const
00183 {
00184   int b;
00185   QString result;
00186   uint i;
00187   b = line.findRev( '>' );
00188   if ( b == -1 ) {
00189     b = line.findRev( '\"' );
00190     if ( b == -1 )
00191       return result;
00192   }
00193 
00194   result = line.mid( b + 1 );
00195   for ( i = 0; i < result.length(); i++ ) {
00196     if ( result[ i ] == CTRL_C )
00197       result[ i ] = '\n';
00198   }
00199 
00200   return result;
00201 }
00202 
00203 QString EudoraXXPort::get( const QString& line, const QString& key ) const
00204 {
00205   QString fd = "<" + key + ":";
00206   int b, e;
00207   uint i;
00208 
00209   // Find formatted key, return on error
00210   b = line.find( fd );
00211   if ( b == -1 )
00212     return QString::null;
00213 
00214   b += fd.length();
00215   e = line.find( '>', b );
00216   if ( e == -1 )
00217     return QString::null;
00218 
00219   e--;
00220   QString result = line.mid( b, e - b + 1 );
00221   for ( i = 0; i < result.length(); i++ ) {
00222     if ( result[ i ] == CTRL_C )
00223       result[ i ] = '\n';
00224   }
00225 
00226   return result;
00227 }
00228 
00229 #include "eudora_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:51 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003