summaryrefslogtreecommitdiffabout
path: root/kabc/plugins/sharpdtm/sharpdtmconverter.cpp
blob: 396be0a83a6a99d5941776f1758ae3ce6ed1a50a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
/*
    This file is part of libkabc.
    Copyright (c) 2002 Tobias Koenig <tokoe@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., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.
*/

/*
Enhanced Version of the file for platform independent KDE tools.
Copyright (c) 2004 Ulf Schenk

$Id$
*/

#include "kglobal.h"
#include <qregexp.h>


#include "sharpdtmconverter.h"

#include <sl/slcategories.h>
#include <libkdepim/ksyncprofile.h>
//US #include <qpe/categoryselect.h>


using namespace KABC;
using namespace SlCategory;

SharpDTMConverter::SharpDTMConverter() : catDB(0)
{
}

SharpDTMConverter::~SharpDTMConverter()
{
  deinit();
}

bool SharpDTMConverter::init()
{
  catDB = new SlCategory::SlCategories();

  if (!catDB)
    return false;

//  catDB->load( categoryFileName() );
  return true;
}

void SharpDTMConverter::deinit()
{
  if (catDB)
  {
    delete catDB;
    catDB = 0;
  }
}

bool SharpDTMConverter::sharpToAddressee( const CardId &contact, const SlZDataBase* database, Addressee &addr )
{
	SlZDataBase* db = (SlZDataBase*)database;

    // for syncing: we need setting of the two fields
    addr.setExternalUID( QString::number( contact ) );
    addr.setOriginalExternalUID( QString::number( contact ) ); 
    addr.setTempSyncStat(  SYNC_TEMPSTATE_NEW_EXTERNAL );


    // name
    //qDebug("SharpDTMConverter::sharpToAddressee check if the fileAs transformation works!!");
    addr.setFormattedName(db->readField(ZdbAdrs::FileAs)); // needs fix

    addr.setFamilyName( db->readField(ZdbAdrs::LastName) );
    addr.setGivenName( db->readField(ZdbAdrs::FirstName) );
    addr.setAdditionalName( db->readField(ZdbAdrs::MiddleName) );
    addr.setPrefix( db->readField(ZdbAdrs::Title) );
    addr.setSuffix( db->readField(ZdbAdrs::Suffix) );


    QString emailstr = db->readField(ZdbAdrs::Emails);
    emailstr.replace( QRegExp(","), " " ); 
    emailstr.replace( QRegExp(";"), " " ); 
    emailstr.replace( QRegExp(":"), " " ); 
    //qDebug("SharpDTMConverter::sharpToAddressee whats the character to seperate the emailadresses? %s ", emailstr.latin1());
    QStringList emails = QStringList::split(" ", emailstr.simplifyWhiteSpace());
    bool defE = false;
    bool found = false;
    for ( QStringList::Iterator it = emails.begin(); it != emails.end(); ++it ) {
        if (found )
            defE = false;
        else
            found = defE = ((*it).lower() == db->readField(ZdbAdrs::DefaultEmail).lower());
      addr.insertEmail( *it, defE );
    }
    if ( ! found )
        if (!db->readField(ZdbAdrs::DefaultEmail).isEmpty())
            addr.insertEmail(db->readField(ZdbAdrs::DefaultEmail), true);

    // home
    if ((!db->readField(ZdbAdrs::HomeStreet).isEmpty()) ||
        (!db->readField(ZdbAdrs::HomeCity).isEmpty()) ||
        (!db->readField(ZdbAdrs::HomeState).isEmpty()) ||
        (!db->readField(ZdbAdrs::HomeZip).isEmpty()) ||
        (!db->readField(ZdbAdrs::HomeCountry).isEmpty()))
    {
      Address homeaddress;
      homeaddress.setType(Address::Home);
//US    homeaddress.setPostOfficeBox( "" );
//US    homeaddress.setExtended( "" );
      homeaddress.setStreet( db->readField(ZdbAdrs::HomeStreet).replace( QRegExp("\\r"), ""));
      homeaddress.setLocality( db->readField(ZdbAdrs::HomeCity) );
      homeaddress.setRegion( db->readField(ZdbAdrs::HomeState) );
      homeaddress.setPostalCode( db->readField(ZdbAdrs::HomeZip) );
      homeaddress.setCountry( db->readField(ZdbAdrs::HomeCountry) );

      addr.insertAddress( homeaddress );
    }

    if (!db->readField(ZdbAdrs::HomePhone).isEmpty())
    {
      PhoneNumber homephone;
      homephone.setType( PhoneNumber::Home );
      homephone.setNumber( db->readField(ZdbAdrs::HomePhone) );
      addr.insertPhoneNumber( homephone );
    }

    if (!db->readField(ZdbAdrs::HomeFax).isEmpty())
    {
      PhoneNumber homefax;
      homefax.setType( PhoneNumber::Home | PhoneNumber::Fax );
      homefax.setNumber( db->readField(ZdbAdrs::HomeFax) );
      addr.insertPhoneNumber( homefax );
    }

    if (!db->readField(ZdbAdrs::HomeMobile).isEmpty())
    {
      PhoneNumber homemobile;
      homemobile.setType( PhoneNumber::Home | PhoneNumber::Cell );
      homemobile.setNumber( db->readField(ZdbAdrs::HomeMobile) );
      addr.insertPhoneNumber( homemobile );
    }

    addr.setUrl( db->readField(ZdbAdrs::HomeWebPage) );


    // business
    if ((!db->readField(ZdbAdrs::BusinessStreet).isEmpty()) ||
        (!db->readField(ZdbAdrs::BusinessCity).isEmpty()) ||
        (!db->readField(ZdbAdrs::BusinessState).isEmpty()) ||
        (!db->readField(ZdbAdrs::BusinessZip).isEmpty()) ||
        (!db->readField(ZdbAdrs::BusinessCountry).isEmpty()))
    {
      Address businessaddress;
      businessaddress.setType(Address::Work);
//US    businessaddress.setPostOfficeBox( "" );
//US    businessaddress.setExtended( "" );
      businessaddress.setStreet( db->readField(ZdbAdrs::BusinessStreet).replace( QRegExp("\\r"), "") );
      businessaddress.setLocality( db->readField(ZdbAdrs::BusinessCity) );
      businessaddress.setRegion( db->readField(ZdbAdrs::BusinessState) );
      businessaddress.setPostalCode( db->readField(ZdbAdrs::BusinessZip) );
      businessaddress.setCountry( db->readField(ZdbAdrs::BusinessCountry) );

      addr.insertAddress( businessaddress );
    }


    if (!db->readField(ZdbAdrs::BusinessPhone).isEmpty())
    {
      PhoneNumber businessphone;
      businessphone.setType( PhoneNumber::Work );
      businessphone.setNumber( db->readField(ZdbAdrs::BusinessPhone) );
      addr.insertPhoneNumber( businessphone );
    }

    if (!db->readField(ZdbAdrs::BusinessFax).isEmpty())
    {
      PhoneNumber businessfax;
      businessfax.setType( PhoneNumber::Work | PhoneNumber::Fax );
      businessfax.setNumber( db->readField(ZdbAdrs::BusinessFax) );
      addr.insertPhoneNumber( businessfax );
    }

    if (!db->readField(ZdbAdrs::BusinessMobile).isEmpty())
    {
      PhoneNumber businessmobile;
      businessmobile.setType( PhoneNumber::Work | PhoneNumber::Cell );
      businessmobile.setNumber( db->readField(ZdbAdrs::BusinessMobile) );
      addr.insertPhoneNumber( businessmobile );
    }

    if (!db->readField(ZdbAdrs::BusinessPager).isEmpty())
    {
      PhoneNumber businesspager;
      businesspager.setType( PhoneNumber::Work | PhoneNumber::Pager );
      businesspager.setNumber( db->readField(ZdbAdrs::BusinessPager) );
      addr.insertPhoneNumber( businesspager );
    }

    addr.setRole( db->readField(ZdbAdrs::JobTitle) );
    addr.setOrganization( db->readField(ZdbAdrs::Company) );
    addr.insertCustom( "KADDRESSBOOK", "X-Profession", db->readField(ZdbAdrs::Profession) );
    addr.insertCustom( "KADDRESSBOOK", "X-AssistantsName", db->readField(ZdbAdrs::Assistant) );
    addr.insertCustom( "KADDRESSBOOK", "X-Department", db->readField(ZdbAdrs::Department) );
    addr.insertCustom( "KADDRESSBOOK", "X-ManagersName", db->readField(ZdbAdrs::Manager) );
    addr.insertCustom( "KADDRESSBOOK", "X-Office", db->readField(ZdbAdrs::Office) );

    //personal
    addr.insertCustom( "KADDRESSBOOK", "X-SpousesName", db->readField(ZdbAdrs::Spouse) );

	QString gen = db->readField(ZdbAdrs::Gender);
	//qDebug("SharpDTMConverter::sharpToAddressee pleas check that gender works!! : Gender: %s", gen.latin1());
	//qDebug("SharpDTMConverter::sharpToAddressee: may be int db->readUshortField(\"ZdbAdrs::Gender\") is here better suited");

    if (gen == "male")
      addr.insertCustom( "KADDRESSBOOK", "X-Gender", "male");
    else if (gen == "female")
      addr.insertCustom( "KADDRESSBOOK", "X-Gender", "female");


    QDate ann = KGlobal::locale()->readDate( db->readField(ZdbAdrs::Anniversary) ); 
    if (ann.isValid()) {
      QString dt = KGlobal::locale()->formatDate(ann, true, KLocale::ISODate);
      //qDebug("qtopiaToAddressee annyversary found:%s", dt.latin1());
      addr.insertCustom( "KADDRESSBOOK", "X-Anniversary", dt);
    }
  

    addr.insertCustom( "KADDRESSBOOK", "X-Children", db->readField(ZdbAdrs::Children) );


    QDate birthd = KGlobal::locale()->readDate( db->readField(ZdbAdrs::Birthday) );
    //qDebug("birtd %s ", birthd.toString().latin1());
    if (birthd.isValid())
      addr.setBirthday( birthd );

    addr.setNickName( db->readField(ZdbAdrs::Nickname) );

    // others
    //US I put opies BusinessWebPage into Ka/Pi's notes block, because no other native field is available.
    //QString notes = db->readField(ZdbAdrs::Notes);
    //notes += "\nBusinessWebPage: " + db->readField(ZdbAdrs::BusinessWebPage) + "\n";
    if ( addr.url().isEmpty() )
        addr.setUrl( db->readField(ZdbAdrs::BusinessWebPage) );
    addr.setNote( db->readField(ZdbAdrs::Notes).replace( QRegExp("\\r"), ""));



//US    QString groups() const { return find( Qtopia::Groups ); }
//US    QStringList groupList() const;

	//qDebug("SharpDTMConverter::sharpToAddressee please check that the categories will be converted" );
    QArray<int> catArray = db->readCategories();
    QString cat;

    for ( unsigned int i=0; i < catArray.size(); i++ ) {
      cat = catDB->label(catArray[i]);
      if ( cat.isEmpty() )
        addr.insertCategory(QString::number(catArray[i]));
      else
        addr.insertCategory( cat );
    }

    return true;
}

bool SharpDTMConverter::addresseeToSharp( const Addressee &addr, SlZDataBase* database , const CardId &contact )
{
    bool cellHome = false, cellWork = false;
    // name
    database->writeField(ZdbAdrs::LastName, addr.familyName());
    database->writeField(ZdbAdrs::FirstName, addr.givenName());
    database->writeField(ZdbAdrs::MiddleName, addr.additionalName());
    database->writeField(ZdbAdrs::Title, addr.prefix());
    database->writeField(ZdbAdrs::Suffix, addr.suffix());

    //qDebug("SharpDTMConverter::addresseeToSharp check if the fileAs transformation works!!\n%s",addr.formattedName().latin1() );
    QString formattedName = addr.formattedName();
    if ( formattedName.isEmpty() ) {
        if ( !addr.familyName().isEmpty() ) {
            formattedName = addr.familyName();
            if ( !addr.givenName().isEmpty() ) {
                formattedName += ", ";
                formattedName += addr.givenName();
            }
        } else
            formattedName = addr.givenName();
    }
    database->writeField(ZdbAdrs::FileAs, formattedName);
    database->writeField(ZdbAdrs::FullName, formattedName);

    // email
    //qDebug("SharpDTMConverter::addresseeToSharp check which seperator we need here for the emails!!");
    //qDebug("SharpDTMConverter::addresseeToSharp its probably the same from sharpToAddressee");
    QString emails = addr.emails().join(" ");
    database->writeField(ZdbAdrs::Emails, emails );

    database->writeField(ZdbAdrs::DefaultEmail, addr.preferredEmail() );


    // home
    const Address homeaddress = addr.address(Address::Home);
    if (!homeaddress.isEmpty()) {
        //qDebug("write home address ");
      database->writeField(ZdbAdrs::HomeStreet, homeaddress.street());
      database->writeField(ZdbAdrs::HomeCity, homeaddress.locality());
      database->writeField(ZdbAdrs::HomeState, homeaddress.region());
      database->writeField(ZdbAdrs::HomeZip, homeaddress.postalCode());
      database->writeField(ZdbAdrs::HomeCountry, homeaddress.country());
    }

    PhoneNumber homephone = addr.phoneNumber( PhoneNumber::Home );
    if (!homephone.number().isEmpty())
      database->writeField(ZdbAdrs::HomePhone, homephone.number());

    PhoneNumber homefax = addr.phoneNumber( PhoneNumber::Home | PhoneNumber::Fax );
    if (!homefax.number().isEmpty())
      database->writeField(ZdbAdrs::HomeFax, homefax.number());

    PhoneNumber homemobile = addr.phoneNumber( PhoneNumber::Home | PhoneNumber::Cell );
    if (!homemobile.number().isEmpty()) {
      database->writeField(ZdbAdrs::HomeMobile, homemobile.number());
      cellHome = true;
    }

    database->writeField(ZdbAdrs::HomeWebPage, addr.url().url());


    // business
    const Address businessaddress = addr.address(Address::Work);
    if (!businessaddress.isEmpty()) {
        //qDebug("write business address ");
      database->writeField(ZdbAdrs::BusinessStreet, businessaddress.street());
      database->writeField(ZdbAdrs::BusinessCity, businessaddress.locality());
      database->writeField(ZdbAdrs::BusinessState, businessaddress.region());
      database->writeField(ZdbAdrs::BusinessZip, businessaddress.postalCode());
      database->writeField(ZdbAdrs::BusinessCountry, businessaddress.country());
    }

    PhoneNumber businessphone = addr.phoneNumber( PhoneNumber::Work );
    if (!businessphone.number().isEmpty())
      database->writeField(ZdbAdrs::BusinessPhone, businessphone.number());

    PhoneNumber businessfax = addr.phoneNumber( PhoneNumber::Work | PhoneNumber::Fax );
    if (!businessfax.number().isEmpty())
      database->writeField(ZdbAdrs::BusinessFax, businessfax.number());

    PhoneNumber businessmobile = addr.phoneNumber( PhoneNumber::Work | PhoneNumber::Cell );
    if (!businessmobile.number().isEmpty()) {
        cellWork = true;
        database->writeField(ZdbAdrs::BusinessMobile, businessmobile.number());
    }
    PhoneNumber mobile = addr.phoneNumber( PhoneNumber::Cell );
    if (! mobile.number().isEmpty()) {
        if ( ! cellHome ) 
            database->writeField(ZdbAdrs::HomeMobile, mobile.number());
        else if (! cellWork )
            database->writeField(ZdbAdrs::BusinessMobile, mobile.number());
    }

    PhoneNumber businesspager = addr.phoneNumber( PhoneNumber::Work | PhoneNumber::Pager );
    if (!businesspager.number().isEmpty())
      database->writeField(ZdbAdrs::BusinessPager, businesspager.number());

    database->writeField(ZdbAdrs::JobTitle, addr.role());
    database->writeField(ZdbAdrs::Company, addr.organization());

    database->writeField(ZdbAdrs::Profession, addr.custom( "KADDRESSBOOK", "X-Profession" ));
    database->writeField(ZdbAdrs::Assistant, addr.custom( "KADDRESSBOOK", "X-AssistantsName" ));
    database->writeField(ZdbAdrs::Department, addr.custom( "KADDRESSBOOK", "X-Department" ));
    database->writeField(ZdbAdrs::Manager, addr.custom( "KADDRESSBOOK", "X-ManagersName" ));
    database->writeField(ZdbAdrs::Office, addr.custom( "KADDRESSBOOK", "X-Office" ));

    //personal
    database->writeField(ZdbAdrs::Spouse, addr.custom( "KADDRESSBOOK", "X-Spouse" ));

    QString gt = addr.custom( "KADDRESSBOOK", "X-Gender" );
	//qDebug("SharpDTMConverter::addresseeToSharp please check that gender works!! : Gender: %s", gt.latin1());
	//qDebug("SharpDTMConverter::addresseeToSharp: may be writeField(\"ZdbAdrs::Gender\", ushort) is here better suited?");
	//qDebug("SharpDTMConverter::addresseeToSharp: check also the reverse functionality in sharpToAddressee");
    if (gt == "male")
      database->writeField(ZdbAdrs::Gender, "male");
    else if (gt == "female")
      database->writeField(ZdbAdrs::Gender, "female");
    else
      database->writeField(ZdbAdrs::Gender, "");


    QDate dt = KGlobal::locale()->readDate(
            addr.custom("KADDRESSBOOK", "X-Anniversary" ), "%Y-%m-%d"); // = Qt::ISODate
    if ( dt.isValid() ) {
        QString dateS =  KGlobal::locale()->formatDate(dt, true  );
        database->writeField(ZdbAdrs::Anniversary, dateS );
    }
    database->writeField(ZdbAdrs::Children, addr.custom( "KADDRESSBOOK", "X-Children" ));
    dt = addr.birthday().date();
    if ( dt.isValid() ) {
        QString dateS =  KGlobal::locale()->formatDate(dt, true );
        database->writeField(ZdbAdrs::Birthday, dateS);
    }
    database->writeField(ZdbAdrs::Nickname, addr.nickName());

    // other
    database->writeField(ZdbAdrs::Notes, addr.note());

//US    QString groups() const { return find( Qtopia::Groups ); }
//US    QStringList groupList() const;


	//qDebug("SharpDTMConverter::addresseeToSharp please check if category transformation works");

    QStringList cats = addr.categories();
    int ii;
    for ( ii = 0; ii < cats.count() ;++ii ) {
        if ( !catDB->exists( cats[ii]  ))
            catDB->addCategory(cats[ii]);
    }
    QArray<int> iar;
    if ( !cats.isEmpty() ) {
      QArray<int> iar = catDB->ids(cats);
      //qDebug("***cat count is %d ", iar.count());
      database->updateCategories(contact, iar);
    }

    return true;
}

QDate SharpDTMConverter::convertDate( QString  s)
{
    QDate dt = KGlobal::locale()->readDate( s ); 
    return dt;
    return QDate ();
}