summaryrefslogtreecommitdiffabout
path: root/kabc/plugins/olaccess/olaccessconverter.cpp
Unidiff
Diffstat (limited to 'kabc/plugins/olaccess/olaccessconverter.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/plugins/olaccess/olaccessconverter.cpp444
1 files changed, 444 insertions, 0 deletions
diff --git a/kabc/plugins/olaccess/olaccessconverter.cpp b/kabc/plugins/olaccess/olaccessconverter.cpp
new file mode 100644
index 0000000..4adcae4
--- a/dev/null
+++ b/kabc/plugins/olaccess/olaccessconverter.cpp
@@ -0,0 +1,444 @@
1/*
2 This file is part of libkabc.
3 Copyright (c) 2002 Tobias Koenig <tokoe@kde.org>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21/*
22Enhanced Version of the file for platform independent KDE tools.
23Copyright (c) 2004 Ulf Schenk
24
25$Id$
26*/
27
28#include "kglobal.h"
29#include <qregexp.h>
30
31
32#include "sharpdtmconverter.h"
33
34#include <sl/slcategories.h>
35#include <libkdepim/ksyncprofile.h>
36//US #include <qpe/categoryselect.h>
37
38
39using namespace KABC;
40using namespace SlCategory;
41
42SharpDTMConverter::SharpDTMConverter() : catDB(0)
43{
44}
45
46SharpDTMConverter::~SharpDTMConverter()
47{
48 deinit();
49}
50
51bool SharpDTMConverter::init()
52{
53 catDB = new SlCategory::SlCategories();
54
55 if (!catDB)
56 return false;
57
58// catDB->load( categoryFileName() );
59 return true;
60}
61
62void SharpDTMConverter::deinit()
63{
64 if (catDB)
65 {
66 delete catDB;
67 catDB = 0;
68 }
69}
70
71bool SharpDTMConverter::sharpToAddressee( const CardId &contact, const SlZDataBase* database, Addressee &addr )
72{
73 SlZDataBase* db = (SlZDataBase*)database;
74
75 // for syncing: we need setting of the two fields
76 addr.setExternalUID( QString::number( contact ) );
77 addr.setOriginalExternalUID( QString::number( contact ) );
78 addr.setTempSyncStat( SYNC_TEMPSTATE_NEW_EXTERNAL );
79
80
81 // name
82 //qDebug("SharpDTMConverter::sharpToAddressee check if the fileAs transformation works!!");
83 addr.setFormattedName(db->readField(ZdbAdrs::FileAs));
84 //addr.setName(db->readField(ZdbAdrs::FullName));
85
86 addr.setFamilyName( db->readField(ZdbAdrs::LastName) );
87 addr.setGivenName( db->readField(ZdbAdrs::FirstName) );
88 addr.setAdditionalName( db->readField(ZdbAdrs::MiddleName) );
89 addr.setPrefix( db->readField(ZdbAdrs::Title) );
90 addr.setSuffix( db->readField(ZdbAdrs::Suffix) );
91
92
93 QString emailstr = db->readField(ZdbAdrs::Emails);
94 emailstr.replace( QRegExp(","), " " );
95 emailstr.replace( QRegExp(";"), " " );
96 emailstr.replace( QRegExp(":"), " " );
97 //qDebug("SharpDTMConverter::sharpToAddressee whats the character to seperate the emailadresses? %s ", emailstr.latin1());
98 QStringList emails = QStringList::split(" ", emailstr.simplifyWhiteSpace());
99 bool defE = false;
100 bool found = false;
101 for ( QStringList::Iterator it = emails.begin(); it != emails.end(); ++it ) {
102 if (found )
103 defE = false;
104 else
105 found = defE = ((*it).lower() == db->readField(ZdbAdrs::DefaultEmail).lower());
106 addr.insertEmail( *it, defE );
107 }
108 if ( ! found )
109 if (!db->readField(ZdbAdrs::DefaultEmail).isEmpty())
110 addr.insertEmail(db->readField(ZdbAdrs::DefaultEmail), true);
111
112 // home
113 if ((!db->readField(ZdbAdrs::HomeStreet).isEmpty()) ||
114 (!db->readField(ZdbAdrs::HomeCity).isEmpty()) ||
115 (!db->readField(ZdbAdrs::HomeState).isEmpty()) ||
116 (!db->readField(ZdbAdrs::HomeZip).isEmpty()) ||
117 (!db->readField(ZdbAdrs::HomeCountry).isEmpty()))
118 {
119 Address homeaddress;
120 homeaddress.setType(Address::Home);
121//US homeaddress.setPostOfficeBox( "" );
122//US homeaddress.setExtended( "" );
123 homeaddress.setStreet( db->readField(ZdbAdrs::HomeStreet).replace( QRegExp("\\r"), ""));
124 homeaddress.setLocality( db->readField(ZdbAdrs::HomeCity) );
125 homeaddress.setRegion( db->readField(ZdbAdrs::HomeState) );
126 homeaddress.setPostalCode( db->readField(ZdbAdrs::HomeZip) );
127 homeaddress.setCountry( db->readField(ZdbAdrs::HomeCountry) );
128
129 addr.insertAddress( homeaddress );
130 }
131
132 if (!db->readField(ZdbAdrs::HomePhone).isEmpty())
133 {
134 PhoneNumber homephone;
135 homephone.setType( PhoneNumber::Home );
136 homephone.setNumber( db->readField(ZdbAdrs::HomePhone) );
137 addr.insertPhoneNumber( homephone );
138 }
139
140 if (!db->readField(ZdbAdrs::HomeFax).isEmpty())
141 {
142 PhoneNumber homefax;
143 homefax.setType( PhoneNumber::Home | PhoneNumber::Fax );
144 homefax.setNumber( db->readField(ZdbAdrs::HomeFax) );
145 addr.insertPhoneNumber( homefax );
146 }
147
148 if (!db->readField(ZdbAdrs::HomeMobile).isEmpty())
149 {
150 PhoneNumber homemobile;
151 homemobile.setType( PhoneNumber::Home | PhoneNumber::Cell );
152 homemobile.setNumber( db->readField(ZdbAdrs::HomeMobile) );
153 addr.insertPhoneNumber( homemobile );
154 }
155
156 addr.setUrl( db->readField(ZdbAdrs::HomeWebPage) );
157
158
159 // business
160 if ((!db->readField(ZdbAdrs::BusinessStreet).isEmpty()) ||
161 (!db->readField(ZdbAdrs::BusinessCity).isEmpty()) ||
162 (!db->readField(ZdbAdrs::BusinessState).isEmpty()) ||
163 (!db->readField(ZdbAdrs::BusinessZip).isEmpty()) ||
164 (!db->readField(ZdbAdrs::BusinessCountry).isEmpty()))
165 {
166 Address businessaddress;
167 businessaddress.setType(Address::Work);
168//US businessaddress.setPostOfficeBox( "" );
169//US businessaddress.setExtended( "" );
170 businessaddress.setStreet( db->readField(ZdbAdrs::BusinessStreet).replace( QRegExp("\\r"), "") );
171 businessaddress.setLocality( db->readField(ZdbAdrs::BusinessCity) );
172 businessaddress.setRegion( db->readField(ZdbAdrs::BusinessState) );
173 businessaddress.setPostalCode( db->readField(ZdbAdrs::BusinessZip) );
174 businessaddress.setCountry( db->readField(ZdbAdrs::BusinessCountry) );
175
176 addr.insertAddress( businessaddress );
177 }
178
179
180 if (!db->readField(ZdbAdrs::BusinessPhone).isEmpty())
181 {
182 PhoneNumber businessphone;
183 businessphone.setType( PhoneNumber::Work );
184 businessphone.setNumber( db->readField(ZdbAdrs::BusinessPhone) );
185 addr.insertPhoneNumber( businessphone );
186 }
187
188 if (!db->readField(ZdbAdrs::BusinessFax).isEmpty())
189 {
190 PhoneNumber businessfax;
191 businessfax.setType( PhoneNumber::Work | PhoneNumber::Fax );
192 businessfax.setNumber( db->readField(ZdbAdrs::BusinessFax) );
193 addr.insertPhoneNumber( businessfax );
194 }
195
196 if (!db->readField(ZdbAdrs::BusinessMobile).isEmpty())
197 {
198 PhoneNumber businessmobile;
199 businessmobile.setType( PhoneNumber::Work | PhoneNumber::Cell );
200 businessmobile.setNumber( db->readField(ZdbAdrs::BusinessMobile) );
201 addr.insertPhoneNumber( businessmobile );
202 }
203
204 if (!db->readField(ZdbAdrs::BusinessPager).isEmpty())
205 {
206 PhoneNumber businesspager;
207 businesspager.setType( PhoneNumber::Work | PhoneNumber::Pager );
208 businesspager.setNumber( db->readField(ZdbAdrs::BusinessPager) );
209 addr.insertPhoneNumber( businesspager );
210 }
211
212 addr.setRole( db->readField(ZdbAdrs::JobTitle) );
213 addr.setOrganization( db->readField(ZdbAdrs::Company) );
214 addr.insertCustom( "KADDRESSBOOK", "X-Profession", db->readField(ZdbAdrs::Profession) );
215 addr.insertCustom( "KADDRESSBOOK", "X-AssistantsName", db->readField(ZdbAdrs::Assistant) );
216 addr.insertCustom( "KADDRESSBOOK", "X-Department", db->readField(ZdbAdrs::Department) );
217 addr.insertCustom( "KADDRESSBOOK", "X-ManagersName", db->readField(ZdbAdrs::Manager) );
218 addr.insertCustom( "KADDRESSBOOK", "X-Office", db->readField(ZdbAdrs::Office) );
219
220 //personal
221 addr.insertCustom( "KADDRESSBOOK", "X-SpousesName", db->readField(ZdbAdrs::Spouse) );
222
223 QString gen = db->readField(ZdbAdrs::Gender);
224 //qDebug("SharpDTMConverter::sharpToAddressee pleas check that gender works!! : Gender: %s", gen.latin1());
225 //qDebug("SharpDTMConverter::sharpToAddressee: may be int db->readUshortField(\"ZdbAdrs::Gender\") is here better suited");
226 if (gen == "1")
227 addr.insertCustom( "KADDRESSBOOK", "X-Gender", "male");
228 else if (gen == "2")
229 addr.insertCustom( "KADDRESSBOOK", "X-Gender", "female");
230 else
231 addr.insertCustom( "KADDRESSBOOK", "X-Gender", "undef");
232
233
234 QDate ann = KGlobal::locale()->readDate( db->readField(ZdbAdrs::Anniversary) );
235 if (ann.isValid()) {
236 QString dt = KGlobal::locale()->formatDate(ann, true, KLocale::ISODate);
237 //qDebug("qtopiaToAddressee annyversary found:%s", dt.latin1());
238 addr.insertCustom( "KADDRESSBOOK", "X-Anniversary", dt);
239 } else
240 addr.insertCustom( "KADDRESSBOOK", "X-Anniversary", " ");
241
242
243
244 addr.insertCustom( "KADDRESSBOOK", "X-Children", db->readField(ZdbAdrs::Children) );
245
246
247 QDate birthd = KGlobal::locale()->readDate( db->readField(ZdbAdrs::Birthday) );
248 //qDebug("birtd %s ", birthd.toString().latin1());
249 if (birthd.isValid())
250 addr.setBirthday( birthd );
251
252 addr.setNickName( db->readField(ZdbAdrs::Nickname) );
253
254 // others
255 //US I put opies BusinessWebPage into Ka/Pi's notes block, because no other native field is available.
256 //QString notes = db->readField(ZdbAdrs::Notes);
257 //notes += "\nBusinessWebPage: " + db->readField(ZdbAdrs::BusinessWebPage) + "\n";
258 if ( addr.url().isEmpty() )
259 addr.setUrl( db->readField(ZdbAdrs::BusinessWebPage) );
260 addr.setNote( db->readField(ZdbAdrs::Notes).replace( QRegExp("\\r"), ""));
261
262
263
264//US QString groups() const { return find( Qtopia::Groups ); }
265//US QStringList groupList() const;
266
267 //qDebug("SharpDTMConverter::sharpToAddressee please check that the categories will be converted" );
268 QArray<int> catArray = db->readCategories();
269 QString cat;
270
271 for ( unsigned int i=0; i < catArray.size(); i++ ) {
272 cat = catDB->label(catArray[i]);
273 if ( cat.isEmpty() )
274 addr.insertCategory(QString::number(catArray[i]));
275 else
276 addr.insertCategory( cat );
277 }
278
279 return true;
280}
281
282bool SharpDTMConverter::addresseeToSharp( const Addressee &addr, SlZDataBase* database , const CardId &contact )
283{
284 bool cellHome = false, cellWork = false;
285 // name
286 database->writeField(ZdbAdrs::LastName, addr.familyName());
287 database->writeField(ZdbAdrs::FirstName, addr.givenName());
288 database->writeField(ZdbAdrs::MiddleName, addr.additionalName());
289 database->writeField(ZdbAdrs::Title, addr.prefix());
290 database->writeField(ZdbAdrs::Suffix, addr.suffix());
291
292 //qDebug("SharpDTMConverter::addresseeToSharp check if the fileAs transformation works!!\n%s",addr.formattedName().latin1() );
293 QString formattedName = addr.formattedName();
294 if ( formattedName.isEmpty() ) {
295 if ( !addr.familyName().isEmpty() ) {
296 formattedName = addr.familyName();
297 if ( !addr.givenName().isEmpty() ) {
298 formattedName += ", ";
299 formattedName += addr.givenName();
300 }
301 } else
302 formattedName = addr.givenName();
303 }
304 database->writeField(ZdbAdrs::FileAs, formattedName);
305 database->writeField(ZdbAdrs::FullName, formattedName);
306
307 // email
308 //qDebug("SharpDTMConverter::addresseeToSharp check which seperator we need here for the emails!!");
309 //qDebug("SharpDTMConverter::addresseeToSharp its probably the same from sharpToAddressee");
310 QString emails = addr.emails().join(" ");
311 database->writeField(ZdbAdrs::Emails, emails );
312
313 database->writeField(ZdbAdrs::DefaultEmail, addr.preferredEmail() );
314 // home
315 const Address homeaddress = addr.address(Address::Home);
316 database->writeField(ZdbAdrs::HomeStreet, homeaddress.street());
317 database->writeField(ZdbAdrs::HomeCity, homeaddress.locality());
318 database->writeField(ZdbAdrs::HomeState, homeaddress.region());
319 database->writeField(ZdbAdrs::HomeZip, homeaddress.postalCode());
320 database->writeField(ZdbAdrs::HomeCountry, homeaddress.country());
321
322 PhoneNumber homephone = addr.phoneNumber( PhoneNumber::Home );
323 database->writeField(ZdbAdrs::HomePhone, homephone.number());
324 PhoneNumber homefax = addr.phoneNumber( PhoneNumber::Home | PhoneNumber::Fax );
325 database->writeField(ZdbAdrs::HomeFax, homefax.number());
326 PhoneNumber homemobile = addr.phoneNumber( PhoneNumber::Home | PhoneNumber::Cell );
327 database->writeField(ZdbAdrs::HomeMobile, homemobile.number());
328 if (!homemobile.number().isEmpty()) {
329 cellHome = true;
330 }
331 database->writeField(ZdbAdrs::HomeWebPage, addr.url().url());
332 // business
333 const Address businessaddress = addr.address(Address::Work);
334 //qDebug("write business address ");
335 database->writeField(ZdbAdrs::BusinessStreet, businessaddress.street());
336 database->writeField(ZdbAdrs::BusinessCity, businessaddress.locality());
337 database->writeField(ZdbAdrs::BusinessState, businessaddress.region());
338 database->writeField(ZdbAdrs::BusinessZip, businessaddress.postalCode());
339 database->writeField(ZdbAdrs::BusinessCountry, businessaddress.country());
340
341 PhoneNumber businessphone = addr.phoneNumber( PhoneNumber::Work );
342 database->writeField(ZdbAdrs::BusinessPhone, businessphone.number());
343
344 PhoneNumber businessfax = addr.phoneNumber( PhoneNumber::Work | PhoneNumber::Fax );
345 database->writeField(ZdbAdrs::BusinessFax, businessfax.number());
346
347 PhoneNumber businessmobile = addr.phoneNumber( PhoneNumber::Work | PhoneNumber::Cell );
348 database->writeField(ZdbAdrs::BusinessMobile, businessmobile.number());
349 if (!businessmobile.number().isEmpty()) {
350 cellWork = true;
351 }
352 PhoneNumber mobile = addr.phoneNumber( PhoneNumber::Cell );
353 if (! mobile.number().isEmpty()) {
354 if ( ! cellHome )
355 database->writeField(ZdbAdrs::HomeMobile, mobile.number());
356 else if (! cellWork )
357 database->writeField(ZdbAdrs::BusinessMobile, mobile.number());
358 }
359
360 PhoneNumber businesspager = addr.phoneNumber( PhoneNumber::Work | PhoneNumber::Pager );
361 database->writeField(ZdbAdrs::BusinessPager, businesspager.number());
362
363 database->writeField(ZdbAdrs::JobTitle, addr.role());
364 database->writeField(ZdbAdrs::Company, addr.organization());
365
366 database->writeField(ZdbAdrs::Profession, addr.custom( "KADDRESSBOOK", "X-Profession" ));
367 database->writeField(ZdbAdrs::Assistant, addr.custom( "KADDRESSBOOK", "X-AssistantsName" ));
368 database->writeField(ZdbAdrs::Department, addr.custom( "KADDRESSBOOK", "X-Department" ));
369 database->writeField(ZdbAdrs::Manager, addr.custom( "KADDRESSBOOK", "X-ManagersName" ));
370 database->writeField(ZdbAdrs::Office, addr.custom( "KADDRESSBOOK", "X-Office" ));
371
372 //personal
373 database->writeField(ZdbAdrs::Spouse, addr.custom( "KADDRESSBOOK", "X-Spouse" ));
374
375 QString gt = addr.custom( "KADDRESSBOOK", "X-Gender" );
376 //qDebug("SharpDTMConverter::addresseeToSharp please check that gender works!! : Gender: %s", gt.latin1());
377 //qDebug("SharpDTMConverter::addresseeToSharp: may be writeField(\"ZdbAdrs::Gender\", ushort) is here better suited?");
378 //qDebug("SharpDTMConverter::addresseeToSharp: check also the reverse functionality in sharpToAddressee");
379 if (gt == "male")
380 database->writeField(ZdbAdrs::Gender, "1");
381 else if (gt == "female")
382 database->writeField(ZdbAdrs::Gender, "2");
383 else
384 database->writeField(ZdbAdrs::Gender, "");
385
386 QString dateS ;
387 QDate dt = KGlobal::locale()->readDate(addr.custom("KADDRESSBOOK", "X-Anniversary" ), "%Y-%m-%d"); // = Qt::ISODate
388 if ( dt.isValid() ) {
389 dateS = KGlobal::locale()->formatDate(dt, true );
390 }
391 database->writeField(ZdbAdrs::Anniversary, dateS );
392 database->writeField(ZdbAdrs::Children, addr.custom( "KADDRESSBOOK", "X-Children" ));
393 dt = addr.birthday().date();
394 dateS = "";
395 if ( dt.isValid() ) {
396 dateS = KGlobal::locale()->formatDate(dt, true );
397 }
398 database->writeField(ZdbAdrs::Birthday, dateS);
399 database->writeField(ZdbAdrs::Nickname, addr.nickName());
400
401 // other
402 database->writeField(ZdbAdrs::Notes, addr.note());
403
404//US QString groups() const { return find( Qtopia::Groups ); }
405//US QStringList groupList() const;
406
407
408 //qDebug("SharpDTMConverter::addresseeToSharp please check if category transformation works");
409
410 return true;
411}
412
413
414bool SharpDTMConverter::setCategories( const Addressee &addr, SlZDataBase* database , const CardId &contact )
415{
416 QStringList list = addr.categories();
417
418 QArray<int> ids(list.count());
419 uint index = 0;
420 for(uint i=0; i<ids.size(); i++){
421 if(catDB->exists(list[i])){
422 ids[index] = catDB->id(list[i]);
423 //qDebug("set exist cat %d %s ",ids[index] , list[i].latin1());
424 index++;
425 } else {
426 ids[index] = catDB->addCategory(list[i]);
427 //qDebug("add new cat %d %s ",ids[index] , list[i].latin1());
428 index++;
429 }
430 }
431 bool res ;
432
433 if ( !(res = database->updateCategories(contact, ids) ))
434 qDebug("SharpDTMConverter::Error updating categories");
435
436 return res;
437}
438
439QDate SharpDTMConverter::convertDate( QString s)
440{
441 QDate dt = KGlobal::locale()->readDate( s );
442 return dt;
443 return QDate ();
444}