# kabc Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# kab2kabc.cpp

    00001 /*
    00002     This file is part of libkabc.
    00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library 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 GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public License
    00016     along with this library; see the file COPYING.LIB.  If not, write to
    00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018     Boston, MA 02111-1307, USA.
    00019 */
    00020
    00021 #include <qfile.h>
    00022 #include <qtextstream.h>
    00023
    00024 #include <kaboutdata.h>
    00025 #include <kapplication.h>
    00026 #include <kdebug.h>
    00027 #include <klocale.h>
    00028 #include <kcmdlineargs.h>
    00029 #include <kabapi.h>
    00030 #include <kglobal.h>
    00031 #include <kconfig.h>
    00032 #include <kstandarddirs.h>
    00033
    00034 #include "addressbook.h"
    00035 #include "stdaddressbook.h"
    00036
    00037 using namespace KABC;
    00038
    00039 static const KCmdLineOptions options[] =
    00040 {
    00041   {"disable-autostart", I18N_NOOP("Disable automatic startup on login."), 0},
    00042   {"o", 0, 0},
    00043   {"override", I18N_NOOP("Override existing entries."),"1"},
    00044   {0,0,0}
    00045 };
    00046
    00047 void readKMailEntry( const QString &kmailEntry, KABC::AddressBook *ab )
    00048 {
    00049   kdDebug() << "KMAILENTRY: " << kmailEntry << endl;
    00050
    00051   QString entry = kmailEntry.simplifyWhiteSpace();
    00052   if( entry.isEmpty() ) return;
    00053
    00054   QString email;
    00055   QString name;
    00056   QString comment;
    00057
    00058   if( entry.at( entry.length() -1 ) == ')' ) {
    00059     int br = entry.findRev( '(' );
    00060     if( br >= 0 ) {
    00061       comment = entry.mid( br + 1, entry.length() - br - 2 );
    00062       entry.truncate( br );
    00063       if( entry.at( entry.length() - 1 ).isSpace() ) {
    00064         entry.truncate( br - 1 );
    00065       }
    00066     }
    00067   }
    00068
    00069   int posSpace = entry.findRev( ' ' );
    00070   if ( posSpace < 0 ) {
    00071     email = entry;
    00072     if( !comment.isEmpty() ) {
    00073       name = comment;
    00074       comment = "";
    00075     }
    00076   } else {
    00077     email = entry.mid( posSpace + 1 );
    00078     name = entry.left( posSpace );
    00079   }
    00080
    00081   if ( email.at( 0 ) == '<' && email.at( email.length() - 1) == '>' ) {
    00082     email = email.mid( 1, email.length() - 2 );
    00083   }
    00084   if ( name.at( 0 ) == '"' && name.at( name.length() - 1) == '"' ) {
    00085     name = name.mid( 1, name.length() - 2 );
    00086   }
    00087   if ( name.at( 0 ) == '\'' && name.at( name.length() - 1) == '\'' ) {
    00088     name = name.mid( 1, name.length() - 2 );
    00089   }
    00090
    00091   if( name.at( name.length() -1 ) == ')' ) {
    00092     int br = name.findRev( '(' );
    00093     if( br >= 0 ) {
    00094       comment = name.mid( br + 1, name.length() - br - 2 ) + " " + comment;
    00095       name.truncate( br );
    00096       if( name.at( name.length() - 1 ).isSpace() ) {
    00097         name.truncate( br - 1 );
    00098       }
    00099     }
    00100   }
    00101
    00102   kdDebug() << "  EMAIL   : " << email   << endl;
    00103   kdDebug() << "  NAME    : " << name    << endl;
    00104   kdDebug() << "  COMMENT : " << comment << endl;
    00105
    00106   KABC::Addressee::List al = ab->findByEmail( email );
    00107   if ( al.isEmpty() ) {
    00108     KABC::Addressee a;
    00109     a.setNameFromString( name );
    00110     a.insertEmail( email );
    00111     a.setNote( comment );
    00112
    00113     ab->insertAddressee( a );
    00114
    00115     kdDebug() << "--INSERTED: " << a.realName() << endl;
    00116   }
    00117 }
    00118
    00119 void importKMailAddressBook( KABC::AddressBook *ab )
    00120 {
    00121   QString fileName = locateLocal( "data", "kmail/addressbook" );
    00122   QString kmailConfigName = locate( "config", "kmailrc" );
    00123   if ( !kmailConfigName.isEmpty() ) {
    00124     KConfig cfg( kmailConfigName );
    00125     cfg.setGroup( "Addressbook" );
    00126     fileName = cfg.readPathEntry( "default", fileName );
    00127   }
    00128   if ( !KStandardDirs::exists( fileName ) ) {
    00129     kdDebug(5700) << "Couldn't find KMail addressbook." << endl;
    00130     return;
    00131   }
    00132
    00133   QFile f( fileName );
    00134   if ( !f.open(IO_ReadOnly) ) {
    00135     kdDebug(5700) << "Couldn't open file '" << fileName << "'" << endl;
    00136     return;
    00137   }
    00138
    00139   QStringList kmailEntries;
    00140
    00141   QTextStream t( &f );
    00142   while ( !t.eof() ) {
    00143       kmailEntries.append( t.readLine() );
    00144   }
    00145   f.close();
    00146
    00147   QStringList::ConstIterator it;
    00148   for( it = kmailEntries.begin(); it != kmailEntries.end(); ++it ) {
    00149     if ( (*it).at( 0 ) == '#' ) continue;
    00150     bool insideQuote = false;
    00151     int end = (*it).length() - 1;
    00152     for(int i = end; i; i--) {
    00153       if( (*it).at( i ) == '"' ) {
    00154         if(insideQuote)
    00155           insideQuote=false;
    00156         else
    00157           insideQuote=true;
    00158       } else if( (*it).at( i ) == ',' && !insideQuote ) {
    00159         readKMailEntry( (*it).mid( i + 1, end - i ), ab );
    00160         end = i - 1;
    00161       }
    00162     }
    00163     readKMailEntry( (*it).mid( 0, end + 1 ), ab );
    00164
    00165     /*
    00166     QStringList addresses = QStringList::split( ",", *it );
    00167     QStringList::ConstIterator it2;
    00168     for( it2 = addresses.begin(); it2 != addresses.end(); ++it2 ) {
    00169       readKMailEntry( *it2, ab );
    00170     }
    00171     */
    00172   }
    00173 }
    00174
    00175 void readKAddressBookEntries( const QString &dataString, Addressee &a )
    00176 {
    00177   // Strip "KMail:1.0" prefix and "[EOS]" suffix.
    00178   QString str = dataString.mid( 11, dataString.length() - 24 );
    00179
    00180   QStringList entries = QStringList::split("\n[EOR]\n ",str);
    00181
    00182   Address homeAddress( Address::Home );
    00183   Address businessAddress( Address::Work );
    00184   Address otherAddress;
    00185
    00186   QStringList::ConstIterator it;
    00187   for( it = entries.begin(); it != entries.end(); ++it ) {
    00188     int pos = (*it).find("\n");
    00189     QString fieldName = (*it).left( pos );
    00190     QString fieldValue = (*it).mid( pos + 2 );
    00191 //    kdDebug() << "KABENTRY: " << fieldName << endl;
    00192 //    kdDebug() << "KABENTRY: " << fieldName << ":" << fieldValue << endl;
    00193
    00194     if ( fieldName == "X-HomeFax" ) {
    00195       a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Home |
    00196                                                     PhoneNumber::Fax ) );
    00197     } else if ( fieldName == "X-OtherPhone" ) {
    00198       a.insertPhoneNumber( PhoneNumber( fieldValue, 0 ) );
    00199     } else if ( fieldName == "X-PrimaryPhone" ) {
    00200       a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Pref ) );
    00201     } else if ( fieldName == "X-BusinessFax" ) {
    00202       a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Work |
    00203                                                     PhoneNumber::Fax ) );
    00204     } else if ( fieldName == "X-CarPhone" ) {
    00205       a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Car ) );
    00206     } else if ( fieldName == "X-MobilePhone" ) {
    00207       a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Cell ) );
    00208     } else if ( fieldName == "X-ISDN" ) {
    00209       a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Isdn ) );
    00210     } else if ( fieldName == "X-OtherFax" ) {
    00211       a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Fax ) );
    00212     } else if ( fieldName == "X-Pager" ) {
    00213       a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Pager ) );
    00214     } else if ( fieldName == "X-BusinessPhone" ) {
    00215       a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Work ) );
    00216     } else if ( fieldName == "X-HomePhone" ) {
    00217       a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Home ) );
    00218     } else if ( fieldName == "X-HomeAddress" ) {
    00219       homeAddress.setLabel( fieldValue );
    00220     } else if ( fieldName == "X-HomeAddressStreet" ) {
    00221       homeAddress.setStreet( fieldValue );
    00222     } else if ( fieldName == "X-HomeAddressCity" ) {
    00223       homeAddress.setLocality( fieldValue );
    00224     } else if ( fieldName == "X-HomeAddressPostalCode" ) {
    00225       homeAddress.setPostalCode( fieldValue );
    00226     } else if ( fieldName == "X-HomeAddressState" ) {
    00227       homeAddress.setRegion( fieldValue );
    00228     } else if ( fieldName == "X-HomeAddressCountry" ) {
    00229       homeAddress.setCountry( fieldValue );
    00230     } else if ( fieldName == "X-BusinessAddress" ) {
    00231       businessAddress.setLabel( fieldValue );
    00232     } else if ( fieldName == "X-BusinessAddressStreet" ) {
    00233       businessAddress.setStreet( fieldValue );
    00234     } else if ( fieldName == "X-BusinessAddressCity" ) {
    00235       businessAddress.setLocality( fieldValue );
    00236     } else if ( fieldName == "X-BusinessAddressPostalCode" ) {
    00237       businessAddress.setPostalCode( fieldValue );
    00238     } else if ( fieldName == "X-BusinessAddressState" ) {
    00239       businessAddress.setRegion( fieldValue );
    00240     } else if ( fieldName == "X-BusinessAddressCountry" ) {
    00241       businessAddress.setCountry( fieldValue );
    00242     } else if ( fieldName == "X-OtherAddress" ) {
    00243       otherAddress.setLabel( fieldValue );
    00244     } else if ( fieldName == "X-OtherAddressStreet" ) {
    00245       otherAddress.setStreet( fieldValue );
    00246     } else if ( fieldName == "X-OtherAddressCity" ) {
    00247       otherAddress.setLocality( fieldValue );
    00248     } else if ( fieldName == "X-OtherAddressPostalCode" ) {
    00249       otherAddress.setPostalCode( fieldValue );
    00250     } else if ( fieldName == "X-OtherAddressState" ) {
    00251       otherAddress.setRegion( fieldValue );
    00252     } else if ( fieldName == "X-OtherAddressCountry" ) {
    00253       otherAddress.setCountry( fieldValue );
    00254     } else if ( fieldName == "NICKNAME" ) {
    00255       a.setNickName( fieldValue );
    00256     } else if ( fieldName == "ORG" ) {
    00257       a.setOrganization( fieldValue );
    00258     } else if ( fieldName == "ROLE" ) {
    00259       a.setRole( fieldValue );
    00260     } else if ( fieldName == "BDAY" ) {
    00261       a.setBirthday( KGlobal::locale()->readDate( fieldValue ) );
    00262     } else if ( fieldName == "WEBPAGE" ) {
    00263       a.setUrl( KURL( fieldValue ) );
    00264     } else if ( fieldName == "N" ) {
    00265     } else if ( fieldName == "X-FirstName" ) {
    00266     } else if ( fieldName == "X-MiddleName" ) {
    00267     } else if ( fieldName == "X-LastName" ) {
    00268     } else if ( fieldName == "X-Title" ) {
    00269     } else if ( fieldName == "X-Suffix" ) {
    00270     } else if ( fieldName == "X-FileAs" ) {
    00271     } else if ( fieldName == "EMAIL" ) {
    00272       a.insertEmail( fieldValue, true );
    00273     } else if ( fieldName == "X-E-mail2" ) {
    00274       a.insertEmail( fieldValue );
    00275     } else if ( fieldName == "X-E-mail3" ) {
    00276       a.insertEmail( fieldValue );
    00277     } else if ( fieldName == "X-Notes" ) {
    00278     } else {
    00279       a.insertCustom( "KADDRESSBOOK", fieldName, fieldValue );
    00280     }
    00281   }
    00282
    00283   if ( !homeAddress.isEmpty() ) a.insertAddress( homeAddress );
    00284   if ( !businessAddress.isEmpty() ) a.insertAddress( businessAddress );
    00285   if ( !otherAddress.isEmpty() ) a.insertAddress( otherAddress );
    00286 }
    00287
    00288 void importKab( KABC::AddressBook *ab, bool override )
    00289 {
    00290   if (!QFile::exists(locateLocal("data", "kab/addressbook.kab") )) {
    00291     kdDebug() << "No KDE 2 addressbook found." << endl;
    00292     return;
    00293   }
    00294
    00295   kdDebug(5700) << "Converting old-style kab addressbook to "
    00296                "new-style kabc addressbook." << endl;
    00297
    00298   KabAPI kab(0);
    00299   if (kab.init() != ::AddressBook::NoError) {
    00300     kdDebug(5700) << "Error initing kab" << endl;
    00301     exit(1);
    00302   }
    00303
    00304   KabKey key;
    00305   ::AddressBook::Entry entry;
    00306
    00307   int num = kab.addressbook()->noOfEntries();
    00308
    00309   kdDebug(5700) << "kab Addressbook has " << num << " entries." << endl;
    00310
    00311   for (int i = 0; i < num; ++i) {
    00312     if (::AddressBook::NoError != kab.addressbook()->getKey(i,key)) {
    00313       kdDebug(5700) << "Error getting key for index " << i << " from kab." << endl;
    00314       continue;
    00315     }
    00316     if (::AddressBook::NoError != kab.addressbook()->getEntry(key,entry))
    00317     {
    00318       kdDebug(5700) << "Error getting entry for index " << i << " from kab." << endl;
    00319       continue;
    00320     }
    00321
    00322     Addressee a;
    00323
    00324     // Convert custom entries
    00325     int count = 0;
    00326     bool idFound = false;
    00327     QStringList::ConstIterator customIt;
    00328     for( customIt = entry.custom.begin(); customIt != entry.custom.end(); ++customIt ) {
    00329       if ( (*customIt).startsWith( "X-KABC-UID:" ) ) {
    00330         a.setUid( (*customIt).mid( (*customIt).find( ":" ) + 1 ) );
    00331         idFound = true;
    00332       } else if ( (*customIt).startsWith( "KMail:1.0\n" ) ) {
    00333         readKAddressBookEntries( *customIt, a );
    00334       } else {
    00335         a.insertCustom( "kab2kabc", QString::number( count++ ), *customIt );
    00336       }
    00337     }
    00338     if( idFound ) {
    00339       if ( !override ) continue;
    00340     } else {
    00341       entry.custom << "X-KABC-UID:" + a.uid();
    00342       ::AddressBook::ErrorCode error = kab.addressbook()->change( key, entry );
    00343       if (error != ::AddressBook::NoError) {
    00344         kdDebug(5700) << "kab.change returned with error " << error << endl;
    00345       } else {
    00346         kdDebug(5700) << "Wrote back to kab uid " << a.uid() << endl;
    00347       }
    00348     }
    00349
    00350     a.setTitle( entry.title );
    00351     a.setFormattedName( entry.fn );
    00352     a.setPrefix( entry.nameprefix );
    00353     a.setGivenName( entry.firstname );
    00354     a.setAdditionalName( entry.middlename );
    00355     a.setFamilyName( entry.lastname );
    00356     a.setBirthday( entry.birthday );
    00357
    00358     QStringList::ConstIterator emailIt;
    00359     for( emailIt = entry.emails.begin(); emailIt != entry.emails.end(); ++emailIt ) {
    00360       a.insertEmail( *emailIt );
    00361     }
    00362
    00363     QStringList::ConstIterator phoneIt;
    00364     for( phoneIt = entry.telephone.begin(); phoneIt != entry.telephone.end(); ++phoneIt ) {
    00365       int kabType = (*phoneIt++).toInt();
    00366       if ( phoneIt == entry.telephone.end() ) break;
    00367       QString number = *phoneIt;
    00368       int type = 0;
    00369       if ( kabType == ::AddressBook::Fixed ) type = PhoneNumber::Voice;
    00370       else if ( kabType == ::AddressBook::Mobile ) type = PhoneNumber::Cell | PhoneNumber::Voice;
    00371       else if ( kabType == ::AddressBook::Fax ) type = PhoneNumber::Fax;
    00372       else if ( kabType == ::AddressBook::Modem ) type = PhoneNumber::Modem;
    00373       a.insertPhoneNumber( PhoneNumber( number, type ) );
    00374     }
    00375
    00376     if ( entry.URLs.count() > 0 ) {
    00377       a.setUrl( entry.URLs.first() );
    00378       if ( entry.URLs.count() > 1 ) {
    00379         kdWarning() << "More than one URL. Ignoring all but the first." << endl;
    00380       }
    00381     }
    00382
    00383     int noAdr = entry.noOfAddresses();
    00384     for( int j = 0; j < noAdr; ++j ) {
    00385       ::AddressBook::Entry::Address kabAddress;
    00386       entry.getAddress( j, kabAddress );
    00387
    00388       Address adr;
    00389
    00390       adr.setStreet( kabAddress.address );
    00391       adr.setPostalCode( kabAddress.zip );
    00392       adr.setLocality( kabAddress.town );
    00393       adr.setCountry( kabAddress.country );
    00394       adr.setRegion( kabAddress.state );
    00395
    00396       QString label;
    00397       if ( !kabAddress.headline.isEmpty() ) label += kabAddress.headline + "\n";
    00398       if ( !kabAddress.position.isEmpty() ) label += kabAddress.position + "\n";
    00399       if ( !kabAddress.org.isEmpty() ) label += kabAddress.org + "\n";
    00400       if ( !kabAddress.orgUnit.isEmpty() ) label += kabAddress.orgUnit + "\n";
    00401       if ( !kabAddress.orgSubUnit.isEmpty() ) label += kabAddress.orgSubUnit + "\n";
    00402       if ( !kabAddress.deliveryLabel.isEmpty() ) label += kabAddress.deliveryLabel + "\n";
    00403       adr.setLabel( label );
    00404
    00405       a.insertAddress( adr );
    00406     }
    00407
    00408     QString note = entry.comment;
    00409
    00410     if ( !entry.user1.isEmpty() ) note += "\nUser1: " + entry.user1;
    00411     if ( !entry.user2.isEmpty() ) note += "\nUser2: " + entry.user2;
    00412     if ( !entry.user3.isEmpty() ) note += "\nUser3: " + entry.user3;
    00413     if ( !entry.user4.isEmpty() ) note += "\nUser4: " + entry.user4;
    00414
    00415     if ( !entry.keywords.count() == 0 ) note += "\nKeywords: " + entry.keywords.join( ", " );
    00416
    00417     QStringList::ConstIterator talkIt;
    00418     for( talkIt = entry.talk.begin(); talkIt != entry.talk.end(); ++talkIt ) {
    00419       note += "\nTalk: " + (*talkIt);
    00420     }
    00421
    00422     a.setNote( note );
    00423
    00424     a.setPrefix( entry.rank + a.prefix() );  // Add rank to prefix
    00425
    00426     a.setCategories( entry.categories );
    00427
    00428     kdDebug(5700) << "Addressee: " << a.familyName() << endl;
    00429
    00430     ab->insertAddressee( a );
    00431   }
    00432
    00433   kab.save( true );
    00434 }
    00435
    00436 int main(int argc,char **argv)
    00437 {
    00438   KAboutData aboutData("kab2kabc",I18N_NOOP("Kab to Kabc Converter"),"0.1");
    00439   aboutData.addAuthor("Cornelius Schumacher", 0, "schumacher@kde.org");
    00440
    00441   KCmdLineArgs::init(argc,argv,&aboutData);
    00442   KCmdLineArgs::addCmdLineOptions( options );
    00443
    00444   KApplication app;
    00445
    00446   KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
    00447
    00448   bool override = false;
    00449
    00450   if ( args->isSet( "override" ) ) {
    00451     kdDebug() << "Override existing entries." << endl;
    00452
    00453     override = true;
    00454   }
    00455
    00456   if ( args->isSet( "disable-autostart" ) ) {
    00457     kdDebug() << "Disable autostart." << endl;
    00458
    00459     KConfig *config = app.config();
    00460     config->setGroup( "Startup" );
    00461     config->writeEntry( "EnableAutostart", false );
    00462   }
    00463
    00464   KABC::AddressBook *kabcBook = StdAddressBook::self();
    00465
    00466   importKMailAddressBook( kabcBook );
    00467
    00468   importKab( kabcBook, override );
    00469
    00470   StdAddressBook::save();
    00471
    00472   kdDebug(5700) << "Saved kabc addressbook to '" << kabcBook->identifier() << "'" << endl;
    00473 }

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:29:20 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001