kaddressbook

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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 K_EXPORT_KADDRESSBOOK_XXFILTER( libkaddrbk_eudora_xxport, EudoraXXPort )
00040 
00041 EudoraXXPort::EudoraXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name )
00042   : KAB::XXPort( ab, parent, name )
00043 {
00044   createImportAction( i18n( "Import Eudora Addressbook..." ) );
00045 }
00046 
00047 KABC::AddresseeList EudoraXXPort::importContacts( const QString& ) const
00048 {
00049   QString fileName = KFileDialog::getOpenFileName( QDir::homeDirPath(),
00050         "*.[tT][xX][tT]|" + i18n("Eudora Light Addressbook (*.txt)"), 0 );
00051   if ( fileName.isEmpty() )
00052     return KABC::AddresseeList();
00053 
00054   QFile file( fileName );
00055   if ( !file.open( IO_ReadOnly ) )
00056     return KABC::AddresseeList();
00057 
00058   QString line;
00059   QTextStream stream( &file );
00060   KABC::Addressee *a = 0;
00061   int bytesRead = 0;
00062 
00063   KABC::AddresseeList list;
00064 
00065   while( !stream.eof() ) {
00066     line = stream.readLine();
00067     bytesRead += line.length();
00068     QString tmp;
00069 
00070     if ( line.startsWith( "alias" ) ) {
00071       if ( a ) { // Write it out
00072         list << *a;
00073         delete a;
00074         a = 0;
00075         a = new KABC::Addressee();
00076       } else
00077         a = new KABC::Addressee();
00078 
00079       tmp = key( line ).stripWhiteSpace();
00080       if ( !tmp.isEmpty() )
00081         a->setFormattedName( tmp );
00082 
00083       tmp = email( line ).stripWhiteSpace();
00084       if ( !tmp.isEmpty() )
00085         a->insertEmail( tmp );
00086     } else if ( line.startsWith( "note" ) ) {
00087       if ( !a ) // Must have an alias before a note
00088         break;
00089 
00090       tmp = comment( line ).stripWhiteSpace();
00091       if ( !tmp.isEmpty() )
00092         a->setNote( tmp );
00093 
00094       tmp = get( line, "name" ).stripWhiteSpace();
00095       if ( !tmp.isEmpty() )
00096         a->setNameFromString( tmp );
00097 
00098       tmp = get( line, "address" ).stripWhiteSpace();
00099       if ( !tmp.isEmpty() ) {
00100         KABC::Address addr;
00101         kdDebug(5720) << tmp << endl; // dump complete address
00102         addr.setLabel( tmp );
00103         a->insertAddress( addr );
00104       }
00105 
00106       tmp = get( line, "phone" ).stripWhiteSpace();
00107       if ( !tmp.isEmpty() )
00108          a->insertPhoneNumber( KABC::PhoneNumber( tmp, KABC::PhoneNumber::Home ) );
00109     }
00110   }
00111 
00112   if ( a ) { // Write out address
00113     list << *a;
00114     delete a;
00115     a = 0;
00116   }
00117 
00118   file.close();
00119 
00120   return list;
00121 }
00122 
00123 QString EudoraXXPort::key( const QString& line) const
00124 {
00125   int e;
00126   QString result;
00127   int b = line.find( '\"', 0 );
00128 
00129   if ( b == -1 ) {
00130     b = line.find( ' ' );
00131     if ( b == -1 )
00132       return result;
00133 
00134     b++;
00135     e = line.find( ' ', b );
00136     result = line.mid( b, e - b );
00137 
00138     return result;
00139   }
00140 
00141   b++;
00142   e = line.find( '\"', b );
00143   if ( e == -1 )
00144     return result;
00145 
00146   result = line.mid( b, e - b );
00147 
00148   return result;
00149 }
00150 
00151 QString EudoraXXPort::email( const QString& line ) const
00152 {
00153   int b;
00154   QString result;
00155   b = line.findRev( '\"' );
00156   if ( b == -1 ) {
00157     b = line.findRev( ' ' );
00158     if ( b == -1 )
00159       return result;
00160   }
00161   result = line.mid( b + 1 );
00162 
00163   return result;
00164 }
00165 
00166 QString EudoraXXPort::comment( const QString& line ) const
00167 {
00168   int b;
00169   QString result;
00170   uint i;
00171   b = line.findRev( '>' );
00172   if ( b == -1 ) {
00173     b = line.findRev( '\"' );
00174     if ( b == -1 )
00175       return result;
00176   }
00177 
00178   result = line.mid( b + 1 );
00179   for ( i = 0; i < result.length(); i++ ) {
00180     if ( result[ i ] == CTRL_C )
00181       result[ i ] = '\n';
00182   }
00183 
00184   return result;
00185 }
00186 
00187 QString EudoraXXPort::get( const QString& line, const QString& key ) const
00188 {
00189   QString fd = "<" + key + ":";
00190   int b, e;
00191   uint i;
00192 
00193   // Find formatted key, return on error
00194   b = line.find( fd );
00195   if ( b == -1 )
00196     return QString::null;
00197 
00198   b += fd.length();
00199   e = line.find( '>', b );
00200   if ( e == -1 )
00201     return QString::null;
00202 
00203   e--;
00204   QString result = line.mid( b, e - b + 1 );
00205   for ( i = 0; i < result.length(); i++ ) {
00206     if ( result[ i ] == CTRL_C )
00207       result[ i ] = '\n';
00208   }
00209 
00210   return result;
00211 }
00212 
00213 #include "eudora_xxport.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys