summaryrefslogtreecommitdiffstats
path: root/kabc/scripts/field.src.cpp
blob: 91a27779d223c4f93ff5d06341022bfe7a61ab77 (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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
    This file is part of libkabc.
    Copyright (c) 2002 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 <klocale.h>
#include <kconfig.h>
#include <kglobal.h>

#include "field.h"
#include "address.h"

using namespace KABC;

class Field::FieldImpl
{
  public:
    FieldImpl( int fieldId, int category = 0,
               const TQString &label = TQString::null,
               const TQString &key = TQString::null,
               const TQString &app = TQString::null )
      : mFieldId( fieldId ), mCategory( category ), mLabel( label ),
        mKey( key ), mApp( app ) {}

    enum FieldId
    {
      CustomField,
      --ENUMS--
    };

    int fieldId() { return mFieldId; }
    int category() { return mCategory; }

    TQString label() { return mLabel; }
    TQString key() { return mKey; }
    TQString app() { return mApp; }

  private:
    int mFieldId;
    int mCategory;

    TQString mLabel;
    TQString mKey;
    TQString mApp;
};


Field::List Field::mAllFields;
Field::List Field::mDefaultFields;
Field::List Field::mCustomFields;


Field::Field( FieldImpl *impl )
{
  mImpl = impl;
}

Field::~Field()
{
  delete mImpl;
}

TQString Field::label()
{
  switch ( mImpl->fieldId() ) {
    --CASELABEL--
    case FieldImpl::CustomField:
      return mImpl->label();
    default:
      return i18n("Unknown Field");
  }
}

int Field::category()
{
  return mImpl->category();
}

TQString Field::categoryLabel( int category )
{
  switch ( category ) {
    case All:
      return i18n("All");
    case Frequent:
      return i18n("Frequent");
    case Address:
      return i18n("street/postal","Address");
    case Email:
      return i18n("Email");
    case Personal:
      return i18n("Personal");
    case Organization:
      return i18n("Organization");
    case CustomCategory:
      return i18n("Custom");
    default:
      return i18n("Undefined");
  }
}

TQString Field::value( const KABC::Addressee &a )
{
  switch ( mImpl->fieldId() ) {
    --CASEVALUE--
    case FieldImpl::Email:
      return a.preferredEmail();
    case FieldImpl::Birthday:
      if ( a.birthday().isValid() )
        return a.birthday().date().toString( Qt::ISODate );
      else
        return TQString::null;
    case FieldImpl::Url:
      return a.url().prettyURL();
    case FieldImpl::HomePhone:
    {
      PhoneNumber::List::ConstIterator it;

      {
        // check for preferred number
        const PhoneNumber::List list = a.phoneNumbers( PhoneNumber::Home | PhoneNumber::Pref );
        for ( it = list.begin(); it != list.end(); ++it )
          if ( ((*it).type() & ~(PhoneNumber::Pref)) == PhoneNumber::Home )
            return (*it).number();
      }

      {
        // check for normal home number
        const PhoneNumber::List list = a.phoneNumbers( PhoneNumber::Home );
        for ( it = list.begin(); it != list.end(); ++it )
          if ( ((*it).type() & ~(PhoneNumber::Pref)) == PhoneNumber::Home )
            return (*it).number();
      }

      return TQString::null;
    }
    case FieldImpl::BusinessPhone:
    {
      PhoneNumber::List::ConstIterator it;

      {
        // check for preferred number
        const PhoneNumber::List list = a.phoneNumbers( PhoneNumber::Work | PhoneNumber::Pref );
        for ( it = list.begin(); it != list.end(); ++it )
          if ( ((*it).type() & ~(PhoneNumber::Pref)) == PhoneNumber::Work )
            return (*it).number();
      }

      {
        // check for normal work number
        const PhoneNumber::List list = a.phoneNumbers( PhoneNumber::Work );
        for ( it = list.begin(); it != list.end(); ++it )
          if ( ((*it).type() & ~(PhoneNumber::Pref)) == PhoneNumber::Work )
            return (*it).number();
      }

      return TQString::null;
    }
    case FieldImpl::MobilePhone:
      return a.phoneNumber( PhoneNumber::Cell ).number();
    case FieldImpl::HomeFax:
      return a.phoneNumber( PhoneNumber::Home | PhoneNumber::Fax ).number();
    case FieldImpl::BusinessFax:
      return a.phoneNumber( PhoneNumber::Work | PhoneNumber::Fax ).number();
    case FieldImpl::CarPhone:
      return a.phoneNumber( PhoneNumber::Car ).number();
    case FieldImpl::Isdn:
      return a.phoneNumber( PhoneNumber::Isdn ).number();
    case FieldImpl::Pager:
      return a.phoneNumber( PhoneNumber::Pager ).number();
    case FieldImpl::HomeAddressStreet:
      return a.address( Address::Home ).street();
    case FieldImpl::HomeAddressPostOfficeBox:
      return a.address( Address::Home ).postOfficeBox();
    case FieldImpl::HomeAddressLocality:
      return a.address( Address::Home ).locality();
    case FieldImpl::HomeAddressRegion:
      return a.address( Address::Home ).region();
    case FieldImpl::HomeAddressPostalCode:
      return a.address( Address::Home ).postalCode();
    case FieldImpl::HomeAddressCountry:
      return a.address( Address::Home ).country();
    case FieldImpl::HomeAddressLabel:
      return a.address( Address::Home ).label();
    case FieldImpl::BusinessAddressStreet:
      return a.address( Address::Work ).street();
    case FieldImpl::BusinessAddressPostOfficeBox:
      return a.address( Address::Work ).postOfficeBox();
    case FieldImpl::BusinessAddressLocality:
      return a.address( Address::Work ).locality();
    case FieldImpl::BusinessAddressRegion:
      return a.address( Address::Work ).region();
    case FieldImpl::BusinessAddressPostalCode:
      return a.address( Address::Work ).postalCode();
    case FieldImpl::BusinessAddressCountry:
      return a.address( Address::Work ).country();
    case FieldImpl::BusinessAddressLabel:
      return a.address( Address::Work ).label();
    case FieldImpl::CustomField:
      return a.custom( mImpl->app(), mImpl->key() );
    default:
      return TQString::null;
  }
}

bool Field::setValue( KABC::Addressee &a, const TQString &value )
{
  switch ( mImpl->fieldId() ) {
    --CASESETVALUE--
    case FieldImpl::MobilePhone:
      {
        PhoneNumber number = a.phoneNumber( PhoneNumber::Cell );
        number.setNumber( value );
        a.insertPhoneNumber( number );
        return true;
      }
    case FieldImpl::HomeFax:
      {
        PhoneNumber number = a.phoneNumber( PhoneNumber::Home | PhoneNumber::Fax );
        number.setNumber( value );
        a.insertPhoneNumber( number );
        return true;
      }
    case FieldImpl::BusinessFax:
      {
        PhoneNumber number = a.phoneNumber( PhoneNumber::Work | PhoneNumber::Fax );
        number.setNumber( value );
        a.insertPhoneNumber( number );
        return true;
      }
    case FieldImpl::CarPhone:
      {
        PhoneNumber number = a.phoneNumber( PhoneNumber::Car );
        number.setNumber( value );
        a.insertPhoneNumber( number );
        return true;
      }
    case FieldImpl::Isdn:
      {
        PhoneNumber number = a.phoneNumber( PhoneNumber::Isdn );
        number.setNumber( value );
        a.insertPhoneNumber( number );
        return true;
      }
    case FieldImpl::Pager:
      {
        PhoneNumber number = a.phoneNumber( PhoneNumber::Pager );
        number.setNumber( value );
        a.insertPhoneNumber( number );
        return true;
      }
    case FieldImpl::HomeAddressStreet:
      {
        KABC::Address address = a.address( Address::Home );
        address.setStreet( value );
        a.insertAddress( address );
        return true;
      }
    case FieldImpl::HomeAddressPostOfficeBox:
      {
        KABC::Address address = a.address( Address::Home );
        address.setPostOfficeBox( value );
        a.insertAddress( address );
        return true;
      }
    case FieldImpl::HomeAddressLocality:
      {
        KABC::Address address = a.address( Address::Home );
        address.setLocality( value );
        a.insertAddress( address );
        return true;
      }
    case FieldImpl::HomeAddressRegion:
      {
        KABC::Address address = a.address( Address::Home );
        address.setRegion( value );
        a.insertAddress( address );
        return true;
      }
    case FieldImpl::HomeAddressPostalCode:
      {
        KABC::Address address = a.address( Address::Home );
        address.setPostalCode( value );
        a.insertAddress( address );
        return true;
      }
    case FieldImpl::HomeAddressCountry:
      {
        KABC::Address address = a.address( Address::Home );
        address.setCountry( value );
        a.insertAddress( address );
        return true;
      }
    case FieldImpl::HomeAddressLabel:
      {
        KABC::Address address = a.address( Address::Home );
        address.setLabel( value );
        a.insertAddress( address );
        return true;
      }
    case FieldImpl::BusinessAddressStreet:
      {
        KABC::Address address = a.address( Address::Work );
        address.setStreet( value );
        a.insertAddress( address );
        return true;
      }
    case FieldImpl::BusinessAddressPostOfficeBox:
      {
        KABC::Address address = a.address( Address::Work );
        address.setPostOfficeBox( value );
        a.insertAddress( address );
        return true;
      }
    case FieldImpl::BusinessAddressLocality:
      {
        KABC::Address address = a.address( Address::Work );
        address.setLocality( value );
        a.insertAddress( address );
        return true;
      }
    case FieldImpl::BusinessAddressRegion:
      {
        KABC::Address address = a.address( Address::Work );
        address.setRegion( value );
        a.insertAddress( address );
        return true;
      }
    case FieldImpl::BusinessAddressPostalCode:
      {
        KABC::Address address = a.address( Address::Work );
        address.setPostalCode( value );
        a.insertAddress( address );
        return true;
      }
    case FieldImpl::BusinessAddressCountry:
      {
        KABC::Address address = a.address( Address::Work );
        address.setCountry( value );
        a.insertAddress( address );
        return true;
      }
    case FieldImpl::BusinessAddressLabel:
      {
        KABC::Address address = a.address( Address::Work );
        address.setLabel( value );
        a.insertAddress( address );
        return true;
      }
    case FieldImpl::Birthday:
      a.setBirthday( TQDate::fromString( value, Qt::ISODate ) );
      return true;
    case FieldImpl::CustomField:
      a.insertCustom( mImpl->app(), mImpl->key(), value );
      return true;
    default:
      return false;
  }
}

TQString Field::sortKey( const KABC::Addressee &a )
{
  switch ( mImpl->fieldId() ) {
    --CASEVALUE--
    case FieldImpl::Birthday:
      if ( a.birthday().isValid() ) {
        TQDate date = a.birthday().date();
        TQString key;
        key.sprintf( "%02d-%02d", date.month(), date.day() );
        return key;
      } else
        return TQString( "00-00" );
    default:
      return value( a ).lower();
  }
}

bool Field::isCustom()
{
  return mImpl->fieldId() == FieldImpl::CustomField;
}

Field::List Field::allFields()
{
  if ( mAllFields.isEmpty() ) {
    --CREATEFIELDS--
  }

  return mAllFields;
}

Field::List Field::defaultFields()
{
  if ( mDefaultFields.isEmpty() ) {
    createDefaultField( FieldImpl::FormattedName );
    createDefaultField( FieldImpl::Email );
  }

  return mDefaultFields;
}

void Field::createField( int id, int category )
{
  mAllFields.append( new Field( new FieldImpl( id, category ) ) );
}

void Field::createDefaultField( int id, int category )
{
  mDefaultFields.append( new Field( new FieldImpl( id, category ) ) );
}

void Field::deleteFields()
{
  Field::List::ConstIterator it;

  for ( it = mAllFields.constBegin(); it != mAllFields.constEnd(); ++it ) {
    delete (*it);
  }
  mAllFields.clear();

  for ( it = mDefaultFields.constBegin(); it != mDefaultFields.constEnd(); ++it ) {
    delete (*it);
  }
  mDefaultFields.clear();

  for ( it = mCustomFields.constBegin(); it != mCustomFields.constEnd(); ++it ) {
    delete (*it);
  }
  mCustomFields.clear();
}

void Field::saveFields( const TQString &identifier,
                        const Field::List &fields )
{
  KConfig *cfg = KGlobal::config();
  KConfigGroupSaver( cfg, "KABCFields" );

  saveFields( cfg, identifier, fields );
}

void Field::saveFields( KConfig *cfg, const TQString &identifier,
                        const Field::List &fields )
{
  TQValueList<int> fieldIds;

  int custom = 0;
  Field::List::ConstIterator it;
  for( it = fields.begin(); it != fields.end(); ++it ) {
    fieldIds.append( (*it)->mImpl->fieldId() );
    if( (*it)->isCustom() ) {
      TQStringList customEntry;
      customEntry << (*it)->mImpl->label();
      customEntry << (*it)->mImpl->key();
      customEntry << (*it)->mImpl->app();
      cfg->writeEntry( "KABC_CustomEntry_" + identifier + "_" +
                       TQString::number( custom++ ), customEntry );
    }
  }

  cfg->writeEntry( identifier, fieldIds );
}

Field::List Field::restoreFields( const TQString &identifier )
{
  KConfig *cfg = KGlobal::config();
  KConfigGroupSaver( cfg, "KABCFields" );

  return restoreFields( cfg, identifier );
}

Field::List Field::restoreFields( KConfig *cfg, const TQString &identifier )
{
  const TQValueList<int> fieldIds = cfg->readIntListEntry( identifier );

  Field::List fields;

  int custom = 0;
  TQValueList<int>::ConstIterator it;
  for( it = fieldIds.begin(); it != fieldIds.end(); ++it ) {
    FieldImpl *f = 0;
    if ( (*it) == FieldImpl::CustomField ) {
      TQStringList customEntry = cfg->readListEntry( "KABC_CustomEntry_" +
                                                 identifier + "_" +
                                                 TQString::number( custom++ ) );
      f = new FieldImpl( *it, CustomCategory, customEntry[ 0 ],
                         customEntry[ 1 ], customEntry[ 2 ] );
    } else {
      f = new FieldImpl( *it );
    }
    fields.append( new Field( f ) );
  }

  return fields;
}

bool Field::equals( Field *field )
{
  bool sameId = ( mImpl->fieldId() == field->mImpl->fieldId() );

  if ( !sameId ) return false;

  if ( mImpl->fieldId() != FieldImpl::CustomField ) return true;

  return mImpl->key() == field->mImpl->key();
}

Field *Field::createCustomField( const TQString &label, int category,
                                 const TQString &key, const TQString &app )
{
  Field *field = new Field( new FieldImpl( FieldImpl::CustomField,
                                           category | CustomCategory,
                                           label, key, app ) );
  mCustomFields.append( field );

  return field;
}