kaddressbook
eudora_xxport.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <QtCore/QFile>
00025 #include <QtCore/QTextStream>
00026
00027 #include <kfiledialog.h>
00028 #include <kio/netaccess.h>
00029 #include <klocale.h>
00030 #include <kmessagebox.h>
00031 #include <ktemporaryfile.h>
00032 #include <kurl.h>
00033
00034 #include <kdebug.h>
00035
00036 #include "eudora_xxport.h"
00037
00038 #define CTRL_C 3
00039
00040 K_EXPORT_KADDRESSBOOK_XXFILTER( kaddrbk_eudora_xxport, EudoraXXPort )
00041
00042 EudoraXXPort::EudoraXXPort( KABC::AddressBook *ab, QWidget *parent, const char *name )
00043 : KAB::XXPort( ab, parent, name )
00044 {
00045 createImportAction( i18n( "Import Eudora Addressbook..." ) );
00046 }
00047
00048 KABC::Addressee::List EudoraXXPort::importContacts( const QString& ) const
00049 {
00050 QString fileName = KFileDialog::getOpenFileName( QDir::homePath(),
00051 "*.[tT][xX][tT]|" + i18n("Eudora Light Addressbook (*.txt)"), 0 );
00052 if ( fileName.isEmpty() )
00053 return KABC::AddresseeList();
00054
00055 QFile file( fileName );
00056 if ( !file.open( QIODevice::ReadOnly ) )
00057 return KABC::AddresseeList();
00058
00059 QString line;
00060 QTextStream stream( &file );
00061 KABC::Addressee *a = 0;
00062 int bytesRead = 0;
00063
00064 KABC::AddresseeList list;
00065
00066 while( !stream.atEnd() ) {
00067 line = stream.readLine();
00068 bytesRead += line.length();
00069 QString tmp;
00070
00071 if ( line.startsWith( "alias" ) ) {
00072 if ( a ) {
00073 list << *a;
00074 delete a;
00075 a = 0;
00076 a = new KABC::Addressee();
00077 } else
00078 a = new KABC::Addressee();
00079
00080 tmp = key( line ).trimmed();
00081 if ( !tmp.isEmpty() )
00082 a->setFormattedName( tmp );
00083
00084 tmp = email( line ).trimmed();
00085 if ( !tmp.isEmpty() )
00086 a->insertEmail( tmp );
00087 } else if ( line.startsWith( "note" ) ) {
00088 if ( !a )
00089 break;
00090
00091 tmp = comment( line ).trimmed();
00092 if ( !tmp.isEmpty() )
00093 a->setNote( tmp );
00094
00095 tmp = get( line, "name" ).trimmed();
00096 if ( !tmp.isEmpty() )
00097 a->setNameFromString( tmp );
00098
00099 tmp = get( line, "address" ).trimmed();
00100 if ( !tmp.isEmpty() ) {
00101 KABC::Address addr;
00102 kDebug(5720) << tmp;
00103 addr.setLabel( tmp );
00104 a->insertAddress( addr );
00105 }
00106
00107 tmp = get( line, "phone" ).trimmed();
00108 if ( !tmp.isEmpty() )
00109 a->insertPhoneNumber( KABC::PhoneNumber( tmp, KABC::PhoneNumber::Home ) );
00110 }
00111 }
00112
00113 if ( a ) {
00114 list << *a;
00115 delete a;
00116 a = 0;
00117 }
00118
00119 file.close();
00120
00121 return list;
00122 }
00123
00124 QString EudoraXXPort::key( const QString& line) const
00125 {
00126 int e;
00127 QString result;
00128 int b = line.indexOf( '\"', 0 );
00129
00130 if ( b == -1 ) {
00131 b = line.indexOf( ' ' );
00132 if ( b == -1 )
00133 return result;
00134
00135 b++;
00136 e = line.indexOf( ' ', b );
00137 result = line.mid( b, e - b );
00138
00139 return result;
00140 }
00141
00142 b++;
00143 e = line.indexOf( '\"', b );
00144 if ( e == -1 )
00145 return result;
00146
00147 result = line.mid( b, e - b );
00148
00149 return result;
00150 }
00151
00152 QString EudoraXXPort::email( const QString& line ) const
00153 {
00154 int b;
00155 QString result;
00156 b = line.lastIndexOf( '\"' );
00157 if ( b == -1 ) {
00158 b = line.lastIndexOf( ' ' );
00159 if ( b == -1 )
00160 return result;
00161 }
00162 result = line.mid( b + 1 );
00163
00164 return result;
00165 }
00166
00167 QString EudoraXXPort::comment( const QString& line ) const
00168 {
00169 int b;
00170 QString result;
00171 int i;
00172 b = line.lastIndexOf( '>' );
00173 if ( b == -1 ) {
00174 b = line.lastIndexOf( '\"' );
00175 if ( b == -1 )
00176 return result;
00177 }
00178
00179 result = line.mid( b + 1 );
00180 for ( i = 0; i < result.length(); ++i ) {
00181 if ( result[ i ] == CTRL_C )
00182 result[ i ] = '\n';
00183 }
00184
00185 return result;
00186 }
00187
00188 QString EudoraXXPort::get( const QString& line, const QString& key ) const
00189 {
00190 QString fd = '<' + key + ':';
00191 int b, e;
00192
00193
00194 b = line.indexOf( fd );
00195 if ( b == -1 )
00196 return QString();
00197
00198 b += fd.length();
00199 e = line.indexOf( '>', b );
00200 if ( e == -1 )
00201 return QString();
00202
00203 e--;
00204 QString result = line.mid( b, e - b + 1 );
00205 for ( int 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"