kab2kabc.cpp
/*
This file is part of libkabc.
Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include <qfile.h>
#include <qtextstream.h>
#include <kabapi.h>
#include <kaboutdata.h>
#include <kapplication.h>
#include <kcmdlineargs.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kglobal.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kstandarddirs.h>
#include "addressbook.h"
#include "stdaddressbook.h"
using namespace KABC;
static const KCmdLineOptions options[] =
{
{ "disable-autostart", I18N_NOOP( "Disable automatic startup on login" ), 0 },
{ "quiet", "", 0 },
{ "o", 0, 0 },
{ "override", I18N_NOOP( "Override existing entries" ), "1" },
KCmdLineLastOption
};
void readKMailEntry( const QString &kmailEntry, KABC::AddressBook *ab )
{
kdDebug() << "KMAILENTRY: " << kmailEntry << endl;
QString entry = kmailEntry.simplifyWhiteSpace();
if ( entry.isEmpty() ) return;
QString email;
QString name;
QString comment;
if ( entry.at( entry.length() -1 ) == ')' ) {
int br = entry.findRev( '(' );
if ( br >= 0 ) {
comment = entry.mid( br + 1, entry.length() - br - 2 );
entry.truncate( br );
if ( entry.at( entry.length() - 1 ).isSpace() ) {
entry.truncate( br - 1 );
}
}
}
int posSpace = entry.findRev( ' ' );
if ( posSpace < 0 ) {
email = entry;
if ( !comment.isEmpty() ) {
name = comment;
comment = "";
}
} else {
email = entry.mid( posSpace + 1 );
name = entry.left( posSpace );
}
if ( email.at( 0 ) == '<' && email.at( email.length() - 1) == '>' ) {
email = email.mid( 1, email.length() - 2 );
}
if ( name.at( 0 ) == '"' && name.at( name.length() - 1) == '"' ) {
name = name.mid( 1, name.length() - 2 );
}
if ( name.at( 0 ) == '\'' && name.at( name.length() - 1) == '\'' ) {
name = name.mid( 1, name.length() - 2 );
}
if ( name.at( name.length() -1 ) == ')' ) {
int br = name.findRev( '(' );
if ( br >= 0 ) {
comment = name.mid( br + 1, name.length() - br - 2 ) + " " + comment;
name.truncate( br );
if ( name.at( name.length() - 1 ).isSpace() ) {
name.truncate( br - 1 );
}
}
}
kdDebug() << " EMAIL : " << email << endl;
kdDebug() << " NAME : " << name << endl;
kdDebug() << " COMMENT : " << comment << endl;
KABC::Addressee::List al = ab->findByEmail( email );
if ( al.isEmpty() ) {
KABC::Addressee a;
a.setNameFromString( name );
a.insertEmail( email );
a.setNote( comment );
ab->insertAddressee( a );
kdDebug() << "--INSERTED: " << a.realName() << endl;
}
}
void importKMailAddressBook( KABC::AddressBook *ab )
{
QString fileName = locateLocal( "data", "kmail/addressbook" );
QString kmailConfigName = locate( "config", "kmailrc" );
if ( !kmailConfigName.isEmpty() ) {
KConfig cfg( kmailConfigName );
cfg.setGroup( "Addressbook" );
fileName = cfg.readPathEntry( "default", fileName );
}
if ( !KStandardDirs::exists( fileName ) ) {
kdDebug(5700) << "Couldn't find KMail addressbook." << endl;
return;
}
QFile f( fileName );
if ( !f.open(IO_ReadOnly) ) {
kdDebug(5700) << "Couldn't open file '" << fileName << "'" << endl;
return;
}
QStringList kmailEntries;
QTextStream t( &f );
while ( !t.eof() ) {
kmailEntries.append( t.readLine() );
}
f.close();
QStringList::ConstIterator it;
for ( it = kmailEntries.begin(); it != kmailEntries.end(); ++it ) {
if ( (*it).at( 0 ) == '#' ) continue;
bool insideQuote = false;
int end = (*it).length() - 1;
for ( int i = end; i; i-- ) {
if ( (*it).at( i ) == '"' ) {
if ( insideQuote )
insideQuote = false;
else
insideQuote = true;
} else if ( (*it).at( i ) == ',' && !insideQuote ) {
readKMailEntry( (*it).mid( i + 1, end - i ), ab );
end = i - 1;
}
}
readKMailEntry( (*it).mid( 0, end + 1 ), ab );
}
}
void readKAddressBookEntries( const QString &dataString, Addressee &a )
{
// Strip "KMail:1.0" prefix and "[EOS]" suffix.
QString str = dataString.mid( 11, dataString.length() - 24 );
QStringList entries = QStringList::split( "\n[EOR]\n ", str );
Address homeAddress( Address::Home );
Address businessAddress( Address::Work );
Address otherAddress;
QStringList::ConstIterator it;
for ( it = entries.begin(); it != entries.end(); ++it ) {
int pos = (*it).find( "\n" );
QString fieldName = (*it).left( pos );
QString fieldValue = (*it).mid( pos + 2 );
if ( fieldName == "X-HomeFax" ) {
a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Home |
PhoneNumber::Fax ) );
} else if ( fieldName == "X-OtherPhone" ) {
a.insertPhoneNumber( PhoneNumber( fieldValue, 0 ) );
} else if ( fieldName == "X-PrimaryPhone" ) {
a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Pref ) );
} else if ( fieldName == "X-BusinessFax" ) {
a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Work |
PhoneNumber::Fax ) );
} else if ( fieldName == "X-CarPhone" ) {
a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Car ) );
} else if ( fieldName == "X-MobilePhone" ) {
a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Cell ) );
} else if ( fieldName == "X-ISDN" ) {
a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Isdn ) );
} else if ( fieldName == "X-OtherFax" ) {
a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Fax ) );
} else if ( fieldName == "X-Pager" ) {
a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Pager ) );
} else if ( fieldName == "X-BusinessPhone" ) {
a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Work ) );
} else if ( fieldName == "X-HomePhone" ) {
a.insertPhoneNumber( PhoneNumber( fieldValue, PhoneNumber::Home ) );
} else if ( fieldName == "X-HomeAddress" ) {
homeAddress.setLabel( fieldValue );
} else if ( fieldName == "X-HomeAddressStreet" ) {
homeAddress.setStreet( fieldValue );
} else if ( fieldName == "X-HomeAddressCity" ) {
homeAddress.setLocality( fieldValue );
} else if ( fieldName == "X-HomeAddressPostalCode" ) {
homeAddress.setPostalCode( fieldValue );
} else if ( fieldName == "X-HomeAddressState" ) {
homeAddress.setRegion( fieldValue );
} else if ( fieldName == "X-HomeAddressCountry" ) {
homeAddress.setCountry( fieldValue );
} else if ( fieldName == "X-BusinessAddress" ) {
businessAddress.setLabel( fieldValue );
} else if ( fieldName == "X-BusinessAddressStreet" ) {
businessAddress.setStreet( fieldValue );
} else if ( fieldName == "X-BusinessAddressCity" ) {
businessAddress.setLocality( fieldValue );
} else if ( fieldName == "X-BusinessAddressPostalCode" ) {
businessAddress.setPostalCode( fieldValue );
} else if ( fieldName == "X-BusinessAddressState" ) {
businessAddress.setRegion( fieldValue );
} else if ( fieldName == "X-BusinessAddressCountry" ) {
businessAddress.setCountry( fieldValue );
} else if ( fieldName == "X-OtherAddress" ) {
otherAddress.setLabel( fieldValue );
} else if ( fieldName == "X-OtherAddressStreet" ) {
otherAddress.setStreet( fieldValue );
} else if ( fieldName == "X-OtherAddressCity" ) {
otherAddress.setLocality( fieldValue );
} else if ( fieldName == "X-OtherAddressPostalCode" ) {
otherAddress.setPostalCode( fieldValue );
} else if ( fieldName == "X-OtherAddressState" ) {
otherAddress.setRegion( fieldValue );
} else if ( fieldName == "X-OtherAddressCountry" ) {
otherAddress.setCountry( fieldValue );
} else if ( fieldName == "NICKNAME" ) {
a.setNickName( fieldValue );
} else if ( fieldName == "ORG" ) {
a.setOrganization( fieldValue );
} else if ( fieldName == "ROLE" ) {
a.setRole( fieldValue );
} else if ( fieldName == "BDAY" ) {
a.setBirthday( KGlobal::locale()->readDate( fieldValue ) );
} else if ( fieldName == "WEBPAGE" ) {
a.setUrl( KURL( fieldValue ) );
} else if ( fieldName == "N" ) {
} else if ( fieldName == "X-FirstName" ) {
} else if ( fieldName == "X-MiddleName" ) {
} else if ( fieldName == "X-LastName" ) {
} else if ( fieldName == "X-Title" ) {
} else if ( fieldName == "X-Suffix" ) {
} else if ( fieldName == "X-FileAs" ) {
} else if ( fieldName == "EMAIL" ) {
a.insertEmail( fieldValue, true );
} else if ( fieldName == "X-E-mail2" ) {
a.insertEmail( fieldValue );
} else if ( fieldName == "X-E-mail3" ) {
a.insertEmail( fieldValue );
} else if ( fieldName == "X-Notes" ) {
} else {
a.insertCustom( "KADDRESSBOOK", fieldName, fieldValue );
}
}
if ( !homeAddress.isEmpty() ) a.insertAddress( homeAddress );
if ( !businessAddress.isEmpty() ) a.insertAddress( businessAddress );
if ( !otherAddress.isEmpty() ) a.insertAddress( otherAddress );
}
void importKab( KABC::AddressBook *ab, bool override, bool quiet )
{
QString fileName = KGlobal::dirs()->saveLocation( "data", "kab/" );
fileName += "addressbook.kab";
if ( !QFile::exists( fileName ) ) {
if ( !quiet ) {
KMessageBox::error( 0, "<qt>" + i18n( "Address book file <b>%1</b> not found! Make sure the old address book is located there and you have read permission for this file." )
.arg( fileName ) + "</qt>" );
}
kdDebug(5700) << "No KDE 2 addressbook found." << endl;
return;
}
kdDebug(5700) << "Converting old-style kab addressbook to "
"new-style kabc addressbook." << endl;
KabAPI kab( 0 );
if ( kab.init() != ::AddressBook::NoError ) {
kdDebug(5700) << "Error initing kab" << endl;
exit( 1 );
}
KabKey key;
::AddressBook::Entry entry;
int num = kab.addressbook()->noOfEntries();
kdDebug(5700) << "kab Addressbook has " << num << " entries." << endl;
for ( int i = 0; i < num; ++i ) {
if ( ::AddressBook::NoError != kab.addressbook()->getKey( i, key ) ) {
kdDebug(5700) << "Error getting key for index " << i << " from kab." << endl;
continue;
}
if ( ::AddressBook::NoError != kab.addressbook()->getEntry( key, entry ) ) {
kdDebug(5700) << "Error getting entry for index " << i << " from kab." << endl;
continue;
}
Addressee a;
// Convert custom entries
int count = 0;
bool idFound = false;
QStringList::ConstIterator customIt;
for ( customIt = entry.custom.begin(); customIt != entry.custom.end(); ++customIt ) {
if ( (*customIt).startsWith( "X-KABC-UID:" ) ) {
a.setUid( (*customIt).mid( (*customIt).find( ":" ) + 1 ) );
idFound = true;
} else if ( (*customIt).startsWith( "KMail:1.0\n" ) ) {
readKAddressBookEntries( *customIt, a );
} else {
a.insertCustom( "kab2kabc", QString::number( count++ ), *customIt );
}
}
if ( idFound ) {
if ( !override ) continue;
} else {
entry.custom << "X-KABC-UID:" + a.uid();
::AddressBook::ErrorCode error = kab.addressbook()->change( key, entry );
if ( error != ::AddressBook::NoError ) {
kdDebug(5700) << "kab.change returned with error " << error << endl;
} else {
kdDebug(5700) << "Wrote back to kab uid " << a.uid() << endl;
}
}
a.setTitle( entry.title );
a.setFormattedName( entry.fn );
a.setPrefix( entry.nameprefix );
a.setGivenName( entry.firstname );
a.setAdditionalName( entry.middlename );
a.setFamilyName( entry.lastname );
a.setBirthday( entry.birthday );
QStringList::ConstIterator emailIt;
for ( emailIt = entry.emails.begin(); emailIt != entry.emails.end(); ++emailIt )
a.insertEmail( *emailIt );
QStringList::ConstIterator phoneIt;
for ( phoneIt = entry.telephone.begin(); phoneIt != entry.telephone.end(); ++phoneIt ) {
int kabType = (*phoneIt++).toInt();
if ( phoneIt == entry.telephone.end() ) break;
QString number = *phoneIt;
int type = 0;
if ( kabType == ::AddressBook::Fixed ) type = PhoneNumber::Voice;
else if ( kabType == ::AddressBook::Mobile ) type = PhoneNumber::Cell | PhoneNumber::Voice;
else if ( kabType == ::AddressBook::Fax ) type = PhoneNumber::Fax;
else if ( kabType == ::AddressBook::Modem ) type = PhoneNumber::Modem;
a.insertPhoneNumber( PhoneNumber( number, type ) );
}
if ( entry.URLs.count() > 0 ) {
a.setUrl( KURL( entry.URLs.first() ) );
if ( entry.URLs.count() > 1 ) {
kdWarning() << "More than one URL. Ignoring all but the first." << endl;
}
}
int noAdr = entry.noOfAddresses();
for ( int j = 0; j < noAdr; ++j ) {
::AddressBook::Entry::Address kabAddress;
entry.getAddress( j, kabAddress );
Address adr;
adr.setStreet( kabAddress.address );
adr.setPostalCode( kabAddress.zip );
adr.setLocality( kabAddress.town );
adr.setCountry( kabAddress.country );
adr.setRegion( kabAddress.state );
QString label;
if ( !kabAddress.headline.isEmpty() ) label += kabAddress.headline + "\n";
if ( !kabAddress.position.isEmpty() ) label += kabAddress.position + "\n";
if ( !kabAddress.org.isEmpty() ) label += kabAddress.org + "\n";
if ( !kabAddress.orgUnit.isEmpty() ) label += kabAddress.orgUnit + "\n";
if ( !kabAddress.orgSubUnit.isEmpty() ) label += kabAddress.orgSubUnit + "\n";
if ( !kabAddress.deliveryLabel.isEmpty() ) label += kabAddress.deliveryLabel + "\n";
adr.setLabel( label );
a.insertAddress( adr );
}
QString note = entry.comment;
if ( !entry.user1.isEmpty() ) note += "\nUser1: " + entry.user1;
if ( !entry.user2.isEmpty() ) note += "\nUser2: " + entry.user2;
if ( !entry.user3.isEmpty() ) note += "\nUser3: " + entry.user3;
if ( !entry.user4.isEmpty() ) note += "\nUser4: " + entry.user4;
if ( !entry.keywords.count() == 0 ) note += "\nKeywords: " + entry.keywords.join( ", " );
QStringList::ConstIterator talkIt;
for ( talkIt = entry.talk.begin(); talkIt != entry.talk.end(); ++talkIt ) {
note += "\nTalk: " + (*talkIt);
}
a.setNote( note );
a.setPrefix( entry.rank + a.prefix() ); // Add rank to prefix
a.setCategories( entry.categories );
kdDebug(5700) << "Addressee: " << a.familyName() << endl;
ab->insertAddressee( a );
}
kab.save( true );
}
int main( int argc, char **argv )
{
KAboutData aboutData( "kab2kabc", I18N_NOOP( "Kab to Kabc Converter" ), "0.1" );
aboutData.addAuthor( "Cornelius Schumacher", 0, "schumacher@kde.org" );
KCmdLineArgs::init( argc, argv, &aboutData );
KCmdLineArgs::addCmdLineOptions( options );
KApplication app;
KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
bool override = false;
if ( args->isSet( "override" ) ) {
kdDebug() << "Override existing entries." << endl;
override = true;
}
bool quiet = false;
if ( args->isSet( "quiet" ) )
quiet = true;
if ( args->isSet( "disable-autostart" ) ) {
kdDebug() << "Disable autostart." << endl;
KConfig *config = app.config();
config->setGroup( "Startup" );
config->writeEntry( "EnableAutostart", false );
}
KABC::AddressBook *kabcBook = StdAddressBook::self();
importKMailAddressBook( kabcBook );
importKab( kabcBook, override, quiet );
StdAddressBook::save();
kdDebug(5700) << "Saved kabc addressbook to '" << kabcBook->identifier() << "'" << endl;
}
KURL
KApplication
KABC::Address
Postal address information.
Definition: address.h:55
locate
QString locate(const char *type, const QString &filename, const KInstance *instance=KGlobal::instance())
KABC::PhoneNumber
Phonenumber information.
Definition: phonenumber.h:38
KGlobal::locale
static KLocale * locale()
KABC::Addressee::insertEmail
void insertEmail(const QString &email, bool preferred=false)
Insert an email address.
Definition: addressee.src.cpp:393
KABC::Addressee::insertPhoneNumber
void insertPhoneNumber(const PhoneNumber &phoneNumber)
Insert a phone number.
Definition: addressee.src.cpp:441
locateLocal
QString locateLocal(const char *type, const QString &filename, const KInstance *instance=KGlobal::instance())
KCmdLineArgs
KABC::Addressee::setCategories
void setCategories(const QStringList &)
Set categories to given value.
Definition: addressee.src.cpp:759
KApplication::config
KConfig * config() const
KABC::StdAddressBook::save
static bool save() KDE_DEPRECATED
Saves the standard address book to disk.
Definition: stdaddressbook.cpp:163
KCmdLineArgs::parsedArgs
static KCmdLineArgs * parsedArgs(const char *id=0)
kdDebug
kdbgstream kdDebug(int area=0)
klocale.h
KStandardDirs::exists
static bool exists(const QString &fullPath)
KABC::Addressee::realName
QString realName() const
Return the name of the addressee.
Definition: addressee.src.cpp:342
KABC::AddressBook::insertAddressee
void insertAddressee(const Addressee &addr)
Insert an addressee into the address book.
Definition: addressbook.cpp:518
KCmdLineOptions
KGlobal::dirs
static KStandardDirs * dirs()
KStandardDirs::saveLocation
QString saveLocation(const char *type, const QString &suffix=QString::null, bool create=true) const
kdWarning
kdbgstream kdWarning(int area=0)
KStdAccel::label
QString label(StdAccel id)
KCmdLineArgs::isSet
bool isSet(const char *option) const
I18N_NOOP
#define I18N_NOOP(x)
KABC::Addressee::setUid
void setUid(const QString &uid)
Set unique identifier.
Definition: addressee.src.cpp:165
KAboutData
KMessageBox::error
static void error(QWidget *parent, const QString &text, const QString &caption=QString::null, int options=Notify)
KStdAccel::key
int key(StdAccel) KDE_DEPRECATED
KABC::Address::setCountry
void setCountry(const QString &)
Sets the country.
Definition: address.cpp:239
KABC::AddressBook::identifier
virtual QString identifier()
Returns a string identifying this addressbook.
Definition: addressbook.cpp:637
KABC::Address::setPostalCode
void setPostalCode(const QString &)
Sets the postal code.
Definition: address.cpp:221
KABC::Addressee
address book entry
Definition: addressee.src.h:74
KABC::Addressee::insertAddress
void insertAddress(const Address &address)
Insert an address.
Definition: addressee.src.cpp:656
KConfig
KABC::Addressee::insertCustom
void insertCustom(const QString &app, const QString &name, const QString &value)
Insert custom entry.
Definition: addressee.src.cpp:772
KCmdLineArgs::init
static void init(int _argc, char **_argv, const char *_appname, const char *programName, const char *_description, const char *_version, bool noKApp=false)
KCmdLineArgs::addCmdLineOptions
static void addCmdLineOptions(const KCmdLineOptions *options, const char *name=0, const char *id=0, const char *afterId=0)
KABC::Address::setLocality
void setLocality(const QString &)
Sets the locality, e.g.
Definition: address.cpp:185
KABC::Address::setLabel
void setLabel(const QString &)
Sets the delivery label.
Definition: address.cpp:257
KABC::Addressee::uid
QString uid() const
Return unique identifier.
Definition: addressee.src.cpp:173
KABC::StdAddressBook::self
static StdAddressBook * self()
Returns the standard addressbook object.
Definition: stdaddressbook.cpp:57
endl
kndbgstream & endl(kndbgstream &s)
KConfig::setGroup
void setGroup(const QString &group)
KABC::Address::isEmpty
bool isEmpty() const
Returns true, if the address is empty.
Definition: address.cpp:68
KConfig::writeEntry
void writeEntry(const QString &pKey, const QString &pValue, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
KABC::Address::setRegion
void setRegion(const QString &)
Sets the region, e.g.
Definition: address.cpp:203
KABC::AddressBook
Address Book.
Definition: addressbook.h:43
KStdAccel::end
const KShortcut & end()
KABC::Address::setStreet
void setStreet(const QString &)
Sets the street (including number).
Definition: address.cpp:167
KStdAccel::name
QString name(StdAccel id)
KABC::AddressBook::findByEmail
Addressee::List findByEmail(const QString &email)
Searches all addressees which match the specified email address.
Definition: addressbook.cpp:603
KABC::Addressee::setNameFromString
DECLARATIONS void setNameFromString(const QString &)
Set name fields by parsing the given string and trying to associate the parts of the string with acco...
Definition: addressee.src.cpp:185

KDE Home | KDE Accessibility Home | Description of Access Keys