22 #include <tqtextstream.h>
25 #include <tdeaboutdata.h>
26 #include <tdeapplication.h>
27 #include <tdecmdlineargs.h>
28 #include <tdeconfig.h>
30 #include <tdeglobal.h>
32 #include <tdemessagebox.h>
33 #include <kstandarddirs.h>
35 #include "addressbook.h"
36 #include "stdaddressbook.h"
42 {
"disable-autostart",
I18N_NOOP(
"Disable automatic startup on login" ), 0 },
45 {
"override",
I18N_NOOP(
"Override existing entries" ),
"1" },
53 TQString entry = kmailEntry.simplifyWhiteSpace();
54 if ( entry.isEmpty() )
return;
60 if ( entry.at( entry.length() -1 ) ==
')' ) {
61 int br = entry.findRev(
'(' );
63 comment = entry.mid( br + 1, entry.length() - br - 2 );
65 if ( entry.at( entry.length() - 1 ).isSpace() ) {
66 entry.truncate( br - 1 );
71 int posSpace = entry.findRev(
' ' );
74 if ( !comment.isEmpty() ) {
79 email = entry.mid( posSpace + 1 );
80 name = entry.left( posSpace );
83 if ( email.at( 0 ) ==
'<' && email.at( email.length() - 1) ==
'>' ) {
84 email = email.mid( 1, email.length() - 2 );
86 if (
name.at( 0 ) ==
'"' &&
name.at(
name.length() - 1) ==
'"' ) {
89 if (
name.at( 0 ) ==
'\'' &&
name.at(
name.length() - 1) ==
'\'' ) {
93 if (
name.at(
name.length() -1 ) ==
')' ) {
94 int br =
name.findRev(
'(' );
96 comment =
name.mid( br + 1,
name.length() - br - 2 ) +
" " + comment;
98 if (
name.at(
name.length() - 1 ).isSpace() ) {
99 name.truncate( br - 1 );
108 TDEABC::Addressee::List al = ab->
findByEmail( email );
109 if ( al.isEmpty() ) {
113 a.setNote( comment );
123 TQString fileName =
locateLocal(
"data",
"kmail/addressbook" );
124 TQString kmailConfigName =
locate(
"config",
"kmailrc" );
125 if ( !kmailConfigName.isEmpty() ) {
127 cfg.setGroup(
"Addressbook" );
128 fileName = cfg.readPathEntry(
"default", fileName );
131 kdDebug(5700) <<
"Couldn't find KMail addressbook." <<
endl;
135 TQFile f( fileName );
136 if ( !f.open(IO_ReadOnly) ) {
137 kdDebug(5700) <<
"Couldn't open file '" << fileName <<
"'" <<
endl;
141 TQStringList kmailEntries;
143 TQTextStream t( &f );
145 kmailEntries.append( t.readLine() );
149 TQStringList::ConstIterator it;
150 for ( it = kmailEntries.begin(); it != kmailEntries.end(); ++it ) {
151 if ( (*it).at( 0 ) ==
'#' )
continue;
152 bool insideQuote =
false;
153 int end = (*it).length() - 1;
154 for (
int i = end; i; i-- ) {
155 if ( (*it).at( i ) ==
'"' ) {
160 }
else if ( (*it).at( i ) ==
',' && !insideQuote ) {
161 readKMailEntry( (*it).mid( i + 1, end - i ), ab );
166 readKMailEntry( (*it).mid( 0, end + 1 ), ab );
170 void readKAddressBookEntries(
const TQString &dataString,
Addressee &a )
173 TQString str = dataString.mid( 11, dataString.length() - 24 );
175 TQStringList entries = TQStringList::split(
"\n[EOR]\n ", str );
177 Address homeAddress( Address::Home );
178 Address businessAddress( Address::Work );
181 TQStringList::ConstIterator it;
182 for ( it = entries.begin(); it != entries.end(); ++it ) {
183 int pos = (*it).find(
"\n" );
184 TQString fieldName = (*it).left( pos );
185 TQString fieldValue = (*it).mid( pos + 2 );
187 if ( fieldName ==
"X-HomeFax" ) {
189 PhoneNumber::Fax ) );
190 }
else if ( fieldName ==
"X-OtherPhone" ) {
192 }
else if ( fieldName ==
"X-PrimaryPhone" ) {
194 }
else if ( fieldName ==
"X-BusinessFax" ) {
196 PhoneNumber::Fax ) );
197 }
else if ( fieldName ==
"X-CarPhone" ) {
199 }
else if ( fieldName ==
"X-MobilePhone" ) {
201 }
else if ( fieldName ==
"X-ISDN" ) {
203 }
else if ( fieldName ==
"X-OtherFax" ) {
205 }
else if ( fieldName ==
"X-Pager" ) {
207 }
else if ( fieldName ==
"X-BusinessPhone" ) {
209 }
else if ( fieldName ==
"X-HomePhone" ) {
211 }
else if ( fieldName ==
"X-HomeAddress" ) {
212 homeAddress.setLabel( fieldValue );
213 }
else if ( fieldName ==
"X-HomeAddressStreet" ) {
214 homeAddress.setStreet( fieldValue );
215 }
else if ( fieldName ==
"X-HomeAddressCity" ) {
216 homeAddress.setLocality( fieldValue );
217 }
else if ( fieldName ==
"X-HomeAddressPostalCode" ) {
218 homeAddress.setPostalCode( fieldValue );
219 }
else if ( fieldName ==
"X-HomeAddressState" ) {
220 homeAddress.setRegion( fieldValue );
221 }
else if ( fieldName ==
"X-HomeAddressCountry" ) {
222 homeAddress.setCountry( fieldValue );
223 }
else if ( fieldName ==
"X-BusinessAddress" ) {
224 businessAddress.setLabel( fieldValue );
225 }
else if ( fieldName ==
"X-BusinessAddressStreet" ) {
226 businessAddress.setStreet( fieldValue );
227 }
else if ( fieldName ==
"X-BusinessAddressCity" ) {
228 businessAddress.setLocality( fieldValue );
229 }
else if ( fieldName ==
"X-BusinessAddressPostalCode" ) {
230 businessAddress.setPostalCode( fieldValue );
231 }
else if ( fieldName ==
"X-BusinessAddressState" ) {
232 businessAddress.setRegion( fieldValue );
233 }
else if ( fieldName ==
"X-BusinessAddressCountry" ) {
234 businessAddress.setCountry( fieldValue );
235 }
else if ( fieldName ==
"X-OtherAddress" ) {
236 otherAddress.
setLabel( fieldValue );
237 }
else if ( fieldName ==
"X-OtherAddressStreet" ) {
239 }
else if ( fieldName ==
"X-OtherAddressCity" ) {
241 }
else if ( fieldName ==
"X-OtherAddressPostalCode" ) {
243 }
else if ( fieldName ==
"X-OtherAddressState" ) {
245 }
else if ( fieldName ==
"X-OtherAddressCountry" ) {
247 }
else if ( fieldName ==
"NICKNAME" ) {
248 a.setNickName( fieldValue );
249 }
else if ( fieldName ==
"ORG" ) {
250 a.setOrganization( fieldValue );
251 }
else if ( fieldName ==
"ROLE" ) {
252 a.setRole( fieldValue );
253 }
else if ( fieldName ==
"BDAY" ) {
255 }
else if ( fieldName ==
"WEBPAGE" ) {
256 a.setUrl(
KURL( fieldValue ) );
257 }
else if ( fieldName ==
"N" ) {
258 }
else if ( fieldName ==
"X-FirstName" ) {
259 }
else if ( fieldName ==
"X-MiddleName" ) {
260 }
else if ( fieldName ==
"X-LastName" ) {
261 }
else if ( fieldName ==
"X-Title" ) {
262 }
else if ( fieldName ==
"X-Suffix" ) {
263 }
else if ( fieldName ==
"X-FileAs" ) {
264 }
else if ( fieldName ==
"EMAIL" ) {
266 }
else if ( fieldName ==
"X-E-mail2" ) {
268 }
else if ( fieldName ==
"X-E-mail3" ) {
270 }
else if ( fieldName ==
"X-Notes" ) {
272 a.
insertCustom(
"KADDRESSBOOK", fieldName, fieldValue );
276 if ( !homeAddress.isEmpty() ) a.
insertAddress( homeAddress );
277 if ( !businessAddress.isEmpty() ) a.
insertAddress( businessAddress );
284 fileName +=
"addressbook.kab";
285 if ( !TQFile::exists( fileName ) ) {
287 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." )
288 .arg( fileName ) +
"</qt>" );
290 kdDebug(5700) <<
"No KDE 2 addressbook found." <<
endl;
294 kdDebug(5700) <<
"Converting old-style kab addressbook to "
295 "new-style tdeabc addressbook." <<
endl;
298 if ( kab.init() != ::AddressBook::NoError ) {
304 ::AddressBook::Entry entry;
306 int num = kab.addressbook()->noOfEntries();
308 kdDebug(5700) <<
"kab Addressbook has " << num <<
" entries." <<
endl;
310 for (
int i = 0; i < num; ++i ) {
311 if ( ::AddressBook::NoError != kab.addressbook()->getKey( i, key ) ) {
312 kdDebug(5700) <<
"Error getting key for index " << i <<
" from kab." <<
endl;
315 if ( ::AddressBook::NoError != kab.addressbook()->getEntry( key, entry ) ) {
316 kdDebug(5700) <<
"Error getting entry for index " << i <<
" from kab." <<
endl;
324 bool idFound =
false;
325 TQStringList::ConstIterator customIt;
326 for ( customIt = entry.custom.begin(); customIt != entry.custom.end(); ++customIt ) {
327 if ( (*customIt).startsWith(
"X-KABC-UID:" ) ) {
328 a.
setUid( (*customIt).mid( (*customIt).find(
":" ) + 1 ) );
330 }
else if ( (*customIt).startsWith(
"KMail:1.0\n" ) ) {
331 readKAddressBookEntries( *customIt, a );
333 a.
insertCustom(
"tdeab2tdeabc", TQString::number( count++ ), *customIt );
337 if ( !
override )
continue;
339 entry.custom <<
"X-KABC-UID:" + a.
uid();
340 ::AddressBook::ErrorCode error = kab.addressbook()->change( key, entry );
341 if ( error != ::AddressBook::NoError ) {
342 kdDebug(5700) <<
"kab.change returned with error " << error <<
endl;
348 a.setTitle( entry.title );
349 a.setFormattedName( entry.fn );
350 a.setPrefix( entry.nameprefix );
351 a.setGivenName( entry.firstname );
352 a.setAdditionalName( entry.middlename );
353 a.setFamilyName( entry.lastname );
354 a.setBirthday( entry.birthday );
356 TQStringList::ConstIterator emailIt;
357 for ( emailIt = entry.emails.begin(); emailIt != entry.emails.end(); ++emailIt )
360 TQStringList::ConstIterator phoneIt;
361 for ( phoneIt = entry.telephone.begin(); phoneIt != entry.telephone.end(); ++phoneIt ) {
362 int kabType = (*phoneIt++).toInt();
363 if ( phoneIt == entry.telephone.end() )
break;
364 TQString number = *phoneIt;
366 if ( kabType == ::AddressBook::Fixed ) type = PhoneNumber::Voice;
367 else if ( kabType == ::AddressBook::Mobile ) type = PhoneNumber::Cell | PhoneNumber::Voice;
368 else if ( kabType == ::AddressBook::Fax ) type = PhoneNumber::Fax;
369 else if ( kabType == ::AddressBook::Modem ) type = PhoneNumber::Modem;
373 if ( entry.URLs.count() > 0 ) {
374 a.setUrl(
KURL( entry.URLs.first() ) );
375 if ( entry.URLs.count() > 1 ) {
376 kdWarning() <<
"More than one URL. Ignoring all but the first." <<
endl;
380 int noAdr = entry.noOfAddresses();
381 for (
int j = 0; j < noAdr; ++j ) {
382 ::AddressBook::Entry::Address kabAddress;
383 entry.getAddress( j, kabAddress );
394 if ( !kabAddress.headline.isEmpty() )
label += kabAddress.headline +
"\n";
395 if ( !kabAddress.position.isEmpty() )
label += kabAddress.position +
"\n";
396 if ( !kabAddress.org.isEmpty() )
label += kabAddress.org +
"\n";
397 if ( !kabAddress.orgUnit.isEmpty() )
label += kabAddress.orgUnit +
"\n";
398 if ( !kabAddress.orgSubUnit.isEmpty() )
label += kabAddress.orgSubUnit +
"\n";
399 if ( !kabAddress.deliveryLabel.isEmpty() )
label += kabAddress.deliveryLabel +
"\n";
405 TQString note = entry.comment;
407 if ( !entry.user1.isEmpty() ) note +=
"\nUser1: " + entry.user1;
408 if ( !entry.user2.isEmpty() ) note +=
"\nUser2: " + entry.user2;
409 if ( !entry.user3.isEmpty() ) note +=
"\nUser3: " + entry.user3;
410 if ( !entry.user4.isEmpty() ) note +=
"\nUser4: " + entry.user4;
412 if ( !entry.keywords.count() == 0 ) note +=
"\nKeywords: " + entry.keywords.join(
", " );
414 TQStringList::ConstIterator talkIt;
415 for ( talkIt = entry.talk.begin(); talkIt != entry.talk.end(); ++talkIt ) {
416 note +=
"\nTalk: " + (*talkIt);
421 a.setPrefix( entry.rank + a.prefix() );
425 kdDebug(5700) <<
"Addressee: " << a.familyName() <<
endl;
433 int main(
int argc,
char **argv )
436 aboutData.addAuthor(
"Cornelius Schumacher", 0,
"schumacher@kde.org" );
445 bool override =
false;
447 if ( args->
isSet(
"override" ) ) {
455 if ( args->
isSet(
"quiet" ) )
458 if ( args->
isSet(
"disable-autostart" ) ) {
463 config->
writeEntry(
"EnableAutostart",
false );
468 importKMailAddressBook( tdeabcBook );
470 importKab( tdeabcBook,
override, quiet );
static void error(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, int options=Notify)
void insertAddressee(const Addressee &addr)
Insert an addressee into the address book.
Addressee::List findByEmail(const TQString &email)
Searches all addressees which match the specified email address.
virtual TQString identifier()
Returns a string identifying this addressbook.
Postal address information.
void setPostalCode(const TQString &)
Sets the postal code.
bool isEmpty() const
Returns true, if the address is empty.
void setLocality(const TQString &)
Sets the locality, e.g.
void setRegion(const TQString &)
Sets the region, e.g.
void setLabel(const TQString &)
Sets the delivery label.
void setStreet(const TQString &)
Sets the street (including number).
void setCountry(const TQString &)
Sets the country.
void setUid(const TQString &uid)
Set unique identifier.
void setCategories(const TQStringList &)
Set categories to given value.
void insertPhoneNumber(const PhoneNumber &phoneNumber)
Insert a phone number.
void insertCustom(const TQString &app, const TQString &name, const TQString &value)
Insert custom entry.
TQString realName() const
Return the name of the addressee.
void insertEmail(const TQString &email, bool preferred=false)
Insert an email address.
void insertAddress(const Address &address)
Insert an address.
DECLARATIONS void setNameFromString(const TQString &)
Set name fields by parsing the given string and trying to associate the parts of the string with acco...
TQString uid() const
Return unique identifier.
static bool save() TDE_DEPRECATED
Saves the standard address book to disk.
static StdAddressBook * self()
Returns the standard addressbook object.
bool isSet(const char *option) const
static TDECmdLineArgs * parsedArgs(const char *id=0)
static void addCmdLineOptions(const TDECmdLineOptions *options, const char *name=0, const char *id=0, const char *afterId=0)
static void init(int _argc, char **_argv, const char *_appname, const char *programName, const char *_description, const char *_version, bool noKApp=false)
void writeEntry(const TQString &pKey, const TQString &pValue, bool bPersistent=true, bool bGlobal=false, bool bNLS=false)
void setGroup(const TQString &group)
static TDEStandardDirs * dirs()
static TDELocale * locale()
TDEConfig * config() const
TQString saveLocation(const char *type, const TQString &suffix=TQString::null, bool create=true) const
static bool exists(const TQString &fullPath)
kndbgstream & endl(kndbgstream &s)
kdbgstream kdWarning(int area=0)
kdbgstream kdDebug(int area=0)
TQString locate(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())
TQString locateLocal(const char *type, const TQString &filename, const TDEInstance *instance=TDEGlobal::instance())
static data, shared by ALL addressee objects
TQString name(StdAccel id)
const TDEShortcut & end()
TQString label(StdAccel id)