summaryrefslogtreecommitdiffabout
path: root/kabc/field.cpp
Unidiff
Diffstat (limited to 'kabc/field.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--kabc/field.cpp550
1 files changed, 550 insertions, 0 deletions
diff --git a/kabc/field.cpp b/kabc/field.cpp
new file mode 100644
index 0000000..41bbfde
--- a/dev/null
+++ b/kabc/field.cpp
@@ -0,0 +1,550 @@
1/*** Warning! This file has been generated by the script makeaddressee ***/
2/*
3 This file is part of libkabc.
4 Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.
20*/
21
22/*
23Enhanced Version of the file for platform independent KDE tools.
24Copyright (c) 2004 Ulf Schenk
25
26$Id$
27*/
28
29#include <klocale.h>
30#include <kconfig.h>
31#include <kconfigbase.h>
32#include <kglobal.h>
33
34#include "field.h"
35
36using namespace KABC;
37
38class Field::FieldImpl
39{
40 public:
41 FieldImpl( int fieldId, int category = 0,
42 const QString &label = QString::null,
43 const QString &key = QString::null,
44 const QString &app = QString::null )
45 : mFieldId( fieldId ), mCategory( category ), mLabel( label ),
46 mKey( key ), mApp( app ) {}
47
48 enum FieldId
49 {
50 CustomField,
51 FormattedName,
52 FamilyName,
53 GivenName,
54 AdditionalName,
55 Prefix,
56 Suffix,
57 NickName,
58 Birthday,
59 HomeAddressStreet,
60 HomeAddressLocality,
61 HomeAddressRegion,
62 HomeAddressPostalCode,
63 HomeAddressCountry,
64 HomeAddressLabel,
65 BusinessAddressStreet,
66 BusinessAddressLocality,
67 BusinessAddressRegion,
68 BusinessAddressPostalCode,
69 BusinessAddressCountry,
70 BusinessAddressLabel,
71 HomePhone,
72 BusinessPhone,
73 MobilePhone,
74 HomeFax,
75 BusinessFax,
76 CarPhone,
77 Isdn,
78 Pager,
79 Email,
80 Mailer,
81 Title,
82 Role,
83 Organization,
84 Note,
85 Url
86 };
87
88 int fieldId() { return mFieldId; }
89 int category() { return mCategory; }
90
91 QString label() { return mLabel; }
92 QString key() { return mKey; }
93 QString app() { return mApp; }
94
95 private:
96 int mFieldId;
97 int mCategory;
98
99 QString mLabel;
100 QString mKey;
101 QString mApp;
102};
103
104
105Field::List Field::mAllFields;
106Field::List Field::mDefaultFields;
107Field::List Field::mCustomFields;
108
109
110Field::Field( FieldImpl *impl )
111{
112 mImpl = impl;
113}
114
115Field::~Field()
116{
117 delete mImpl;
118}
119
120QString Field::label()
121{
122 switch ( mImpl->fieldId() ) {
123 case FieldImpl::FormattedName:
124 return Addressee::formattedNameLabel();
125 case FieldImpl::FamilyName:
126 return Addressee::familyNameLabel();
127 case FieldImpl::GivenName:
128 return Addressee::givenNameLabel();
129 case FieldImpl::AdditionalName:
130 return Addressee::additionalNameLabel();
131 case FieldImpl::Prefix:
132 return Addressee::prefixLabel();
133 case FieldImpl::Suffix:
134 return Addressee::suffixLabel();
135 case FieldImpl::NickName:
136 return Addressee::nickNameLabel();
137 case FieldImpl::Birthday:
138 return Addressee::birthdayLabel();
139 case FieldImpl::HomeAddressStreet:
140 return Addressee::homeAddressStreetLabel();
141 case FieldImpl::HomeAddressLocality:
142 return Addressee::homeAddressLocalityLabel();
143 case FieldImpl::HomeAddressRegion:
144 return Addressee::homeAddressRegionLabel();
145 case FieldImpl::HomeAddressPostalCode:
146 return Addressee::homeAddressPostalCodeLabel();
147 case FieldImpl::HomeAddressCountry:
148 return Addressee::homeAddressCountryLabel();
149 case FieldImpl::HomeAddressLabel:
150 return Addressee::homeAddressLabelLabel();
151 case FieldImpl::BusinessAddressStreet:
152 return Addressee::businessAddressStreetLabel();
153 case FieldImpl::BusinessAddressLocality:
154 return Addressee::businessAddressLocalityLabel();
155 case FieldImpl::BusinessAddressRegion:
156 return Addressee::businessAddressRegionLabel();
157 case FieldImpl::BusinessAddressPostalCode:
158 return Addressee::businessAddressPostalCodeLabel();
159 case FieldImpl::BusinessAddressCountry:
160 return Addressee::businessAddressCountryLabel();
161 case FieldImpl::BusinessAddressLabel:
162 return Addressee::businessAddressLabelLabel();
163 case FieldImpl::HomePhone:
164 return Addressee::homePhoneLabel();
165 case FieldImpl::BusinessPhone:
166 return Addressee::businessPhoneLabel();
167 case FieldImpl::MobilePhone:
168 return Addressee::mobilePhoneLabel();
169 case FieldImpl::HomeFax:
170 return Addressee::homeFaxLabel();
171 case FieldImpl::BusinessFax:
172 return Addressee::businessFaxLabel();
173 case FieldImpl::CarPhone:
174 return Addressee::carPhoneLabel();
175 case FieldImpl::Isdn:
176 return Addressee::isdnLabel();
177 case FieldImpl::Pager:
178 return Addressee::pagerLabel();
179 case FieldImpl::Email:
180 return Addressee::emailLabel();
181 case FieldImpl::Mailer:
182 return Addressee::mailerLabel();
183 case FieldImpl::Title:
184 return Addressee::titleLabel();
185 case FieldImpl::Role:
186 return Addressee::roleLabel();
187 case FieldImpl::Organization:
188 return Addressee::organizationLabel();
189 case FieldImpl::Note:
190 return Addressee::noteLabel();
191 case FieldImpl::Url:
192 return Addressee::urlLabel();
193 case FieldImpl::CustomField:
194 return mImpl->label();
195 default:
196 return i18n("Unknown Field");
197 }
198}
199
200int Field::category()
201{
202 return mImpl->category();
203}
204
205QString Field::categoryLabel( int category )
206{
207 switch ( category ) {
208 case All:
209 return i18n("All");
210 case Frequent:
211 return i18n("Frequent");
212 case Address:
213 return i18n("Address");
214 case Email:
215 return i18n("Email");
216 case Personal:
217 return i18n("Personal");
218 case Organization:
219 return i18n("Organization");
220 case CustomCategory:
221 return i18n("Custom");
222 default:
223 return i18n("Undefined");
224 }
225}
226
227QString Field::value( const KABC::Addressee &a )
228{
229 switch ( mImpl->fieldId() ) {
230 case FieldImpl::FormattedName:
231 return a.formattedName();
232 case FieldImpl::FamilyName:
233 return a.familyName();
234 case FieldImpl::GivenName:
235 return a.givenName();
236 case FieldImpl::AdditionalName:
237 return a.additionalName();
238 case FieldImpl::Prefix:
239 return a.prefix();
240 case FieldImpl::Suffix:
241 return a.suffix();
242 case FieldImpl::NickName:
243 return a.nickName();
244 case FieldImpl::Mailer:
245 return a.mailer();
246 case FieldImpl::Title:
247 return a.title();
248 case FieldImpl::Role:
249 return a.role();
250 case FieldImpl::Organization:
251 return a.organization();
252 case FieldImpl::Note:
253 return a.note();
254 case FieldImpl::Email:
255 return a.preferredEmail();
256 case FieldImpl::Birthday:
257 if ( a.birthday().isValid() ) {
258//the generated code had the following format: return a.birthday().date().toString( Qt::ISODate );
259// But Qt::IsoDate was not specified.
260 QString _oldFormat = KGlobal::locale()->dateFormat();
261 KGlobal::locale()->setDateFormat("%Y-%m-%d"); // = Qt::ISODate
262 QString dt = KGlobal::locale()->formatDate(a.birthday().date(), false);
263 KGlobal::locale()->setDateFormat(_oldFormat);
264 return dt;
265 }
266 else
267 return QString::null;
268 case FieldImpl::Url:
269 return a.url().prettyURL();
270 case FieldImpl::HomePhone:
271 return a.phoneNumber( PhoneNumber::Home ).number();
272 case FieldImpl::BusinessPhone:
273 return a.phoneNumber( PhoneNumber::Work ).number();
274 case FieldImpl::MobilePhone:
275 return a.phoneNumber( PhoneNumber::Cell ).number();
276 case FieldImpl::HomeFax:
277 return a.phoneNumber( PhoneNumber::Home | PhoneNumber::Fax ).number();
278 case FieldImpl::BusinessFax:
279 return a.phoneNumber( PhoneNumber::Work | PhoneNumber::Fax ).number();
280 case FieldImpl::CarPhone:
281 return a.phoneNumber( PhoneNumber::Car ).number();
282 case FieldImpl::Isdn:
283 return a.phoneNumber( PhoneNumber::Isdn ).number();
284 case FieldImpl::Pager:
285 return a.phoneNumber( PhoneNumber::Pager ).number();
286 case FieldImpl::HomeAddressStreet:
287 return a.address( Address::Home ).street();
288 case FieldImpl::HomeAddressLocality:
289 return a.address( Address::Home ).locality();
290 case FieldImpl::HomeAddressRegion:
291 return a.address( Address::Home ).region();
292 case FieldImpl::HomeAddressPostalCode:
293 return a.address( Address::Home ).postalCode();
294 case FieldImpl::HomeAddressCountry:
295 return a.address( Address::Home ).country();
296 case FieldImpl::BusinessAddressStreet:
297 return a.address( Address::Work ).street();
298 case FieldImpl::BusinessAddressLocality:
299 return a.address( Address::Work ).locality();
300 case FieldImpl::BusinessAddressRegion:
301 return a.address( Address::Work ).region();
302 case FieldImpl::BusinessAddressPostalCode:
303 return a.address( Address::Work ).postalCode();
304 case FieldImpl::BusinessAddressCountry:
305 return a.address( Address::Work ).country();
306 case FieldImpl::CustomField:
307 return a.custom( mImpl->app(), mImpl->key() );
308 default:
309 return QString::null;
310 }
311}
312
313bool Field::setValue( KABC::Addressee &a, const QString &value )
314{
315 switch ( mImpl->fieldId() ) {
316 case FieldImpl::FormattedName:
317 a.setFormattedName( value );
318 return true;
319 case FieldImpl::FamilyName:
320 a.setFamilyName( value );
321 return true;
322 case FieldImpl::GivenName:
323 a.setGivenName( value );
324 return true;
325 case FieldImpl::AdditionalName:
326 a.setAdditionalName( value );
327 return true;
328 case FieldImpl::Prefix:
329 a.setPrefix( value );
330 return true;
331 case FieldImpl::Suffix:
332 a.setSuffix( value );
333 return true;
334 case FieldImpl::NickName:
335 a.setNickName( value );
336 return true;
337 case FieldImpl::Mailer:
338 a.setMailer( value );
339 return true;
340 case FieldImpl::Title:
341 a.setTitle( value );
342 return true;
343 case FieldImpl::Role:
344 a.setRole( value );
345 return true;
346 case FieldImpl::Organization:
347 a.setOrganization( value );
348 return true;
349 case FieldImpl::Note:
350 a.setNote( value );
351 return true;
352 case FieldImpl::Birthday:
353//US
354//the generated code had the following format: return a.setBirthday( QDate::fromString( value, Qt::ISODate ) );
355// But Qt::IsoDate and QDate::fromString was not specified. Do I have the wrong QT version ?
356 {
357 QDate dt = KGlobal::locale()->readDate( value, "%Y-%m-%d"); // = Qt::ISODate
358 a.setBirthday(dt);
359 }
360 return true;
361 case FieldImpl::CustomField:
362 a.insertCustom( mImpl->app(), mImpl->key(), value );
363 default:
364 return false;
365 }
366}
367
368bool Field::isCustom()
369{
370 return mImpl->fieldId() == FieldImpl::CustomField;
371}
372
373Field::List Field::allFields()
374{
375 if ( mAllFields.isEmpty() ) {
376 createField( FieldImpl::FormattedName, Frequent );
377 createField( FieldImpl::FamilyName, Frequent );
378 createField( FieldImpl::GivenName, Frequent );
379 createField( FieldImpl::AdditionalName );
380 createField( FieldImpl::Prefix );
381 createField( FieldImpl::Suffix );
382 createField( FieldImpl::NickName, Personal );
383 createField( FieldImpl::Birthday, Personal );
384 createField( FieldImpl::HomeAddressStreet, Address|Personal );
385 createField( FieldImpl::HomeAddressLocality, Address|Personal );
386 createField( FieldImpl::HomeAddressRegion, Address|Personal );
387 createField( FieldImpl::HomeAddressPostalCode, Address|Personal );
388 createField( FieldImpl::HomeAddressCountry, Address|Personal );
389 createField( FieldImpl::HomeAddressLabel, Address|Personal );
390 createField( FieldImpl::BusinessAddressStreet, Address|Organization );
391 createField( FieldImpl::BusinessAddressLocality, Address|Organization );
392 createField( FieldImpl::BusinessAddressRegion, Address|Organization );
393 createField( FieldImpl::BusinessAddressPostalCode, Address|Organization );
394 createField( FieldImpl::BusinessAddressCountry, Address|Organization );
395 createField( FieldImpl::BusinessAddressLabel, Address|Organization );
396 createField( FieldImpl::HomePhone, Personal|Frequent );
397 createField( FieldImpl::BusinessPhone, Organization|Frequent );
398 createField( FieldImpl::MobilePhone, Frequent );
399 createField( FieldImpl::HomeFax );
400 createField( FieldImpl::BusinessFax );
401 createField( FieldImpl::CarPhone );
402 createField( FieldImpl::Isdn );
403 createField( FieldImpl::Pager );
404 createField( FieldImpl::Email, Email|Frequent );
405 createField( FieldImpl::Mailer, Email );
406 createField( FieldImpl::Title, Organization );
407 createField( FieldImpl::Role, Organization );
408 createField( FieldImpl::Organization, Organization );
409 createField( FieldImpl::Note );
410 createField( FieldImpl::Url );
411 }
412
413 return mAllFields;
414}
415
416Field::List Field::defaultFields()
417{
418 if ( mDefaultFields.isEmpty() ) {
419 createDefaultField( FieldImpl::GivenName );
420 createDefaultField( FieldImpl::FamilyName );
421 createDefaultField( FieldImpl::Email );
422 }
423
424 return mDefaultFields;
425}
426
427void Field::createField( int id, int category )
428{
429 mAllFields.append( new Field( new FieldImpl( id, category ) ) );
430}
431
432void Field::createDefaultField( int id, int category )
433{
434 mDefaultFields.append( new Field( new FieldImpl( id, category ) ) );
435}
436
437void Field::deleteFields()
438{
439 Field::List::ConstIterator it;
440
441 for( it = mAllFields.begin(); it != mAllFields.end(); ++it ) {
442 delete (*it);
443 }
444 mAllFields.clear();
445
446 for( it = mDefaultFields.begin(); it != mDefaultFields.end(); ++it ) {
447 delete (*it);
448 }
449 mDefaultFields.clear();
450
451 for( it = mCustomFields.begin(); it != mCustomFields.end(); ++it ) {
452 delete (*it);
453 }
454 mCustomFields.clear();
455}
456
457void Field::saveFields( const QString &identifier,
458 const Field::List &fields )
459{
460 KConfig *cfg = KGlobal::config();
461 KConfigGroupSaver( cfg, "KABCFields" );
462 saveFields( cfg, identifier, fields );
463}
464
465void Field::saveFields( KConfig *cfg, const QString &identifier,
466 const Field::List &fields )
467{
468 QValueList<int> fieldIds;
469
470//US
471//US qDebug("Field::saveFields to %s %s", cfg->getFileName().latin1(), identifier.latin1());
472
473 int custom = 0;
474 Field::List::ConstIterator it;
475 for( it = fields.begin(); it != fields.end(); ++it ) {
476//US
477//US qDebug("Field::saveFields field:%i", (*it)->mImpl->fieldId());
478
479 fieldIds.append( (*it)->mImpl->fieldId() );
480 if( (*it)->isCustom() ) {
481 QStringList customEntry;
482 customEntry << (*it)->mImpl->label();
483 customEntry << (*it)->mImpl->key();
484 customEntry << (*it)->mImpl->app();
485 cfg->writeEntry( "KABC_CustomEntry_" + identifier + "_" +
486 QString::number( custom++ ), customEntry );
487 }
488 }
489 cfg->writeEntry( identifier, fieldIds );
490}
491
492Field::List Field::restoreFields( const QString &identifier )
493{
494 KConfig *cfg = KGlobal::config();
495 KConfigGroupSaver( cfg, "KABCFields" );
496 cfg->setGroup( "KABCFields" );
497
498 Field::List l = restoreFields( cfg, identifier );
499
500 return l;
501}
502
503Field::List Field::restoreFields( KConfig *cfg, const QString &identifier )
504{
505 QValueList<int> fieldIds = cfg->readIntListEntry( identifier);
506//US
507 qDebug("Field::restoreFields from %s", cfg->getFileName().latin1());
508
509 Field::List fields;
510
511 int custom = 0;
512 QValueList<int>::ConstIterator it;
513 for( it = fieldIds.begin(); it != fieldIds.end(); ++it ) {
514 FieldImpl *f = 0;
515 if ( (*it) == FieldImpl::CustomField ) {
516 QStringList customEntry = cfg->readListEntry( "KABC_CustomEntry_" +
517 identifier + "_" +
518 QString::number( custom++ ) );
519 f = new FieldImpl( *it, CustomCategory, customEntry[ 0 ],
520 customEntry[ 1 ], customEntry[ 2 ] );
521 } else {
522 f = new FieldImpl( *it );
523 }
524 fields.append( new Field( f ) );
525 }
526
527 return fields;
528}
529
530bool Field::equals( Field *field )
531{
532 bool sameId = ( mImpl->fieldId() == field->mImpl->fieldId() );
533
534 if ( !sameId ) return false;
535
536 if ( mImpl->fieldId() != FieldImpl::CustomField ) return true;
537
538 return mImpl->key() == field->mImpl->key();
539}
540
541Field *Field::createCustomField( const QString &label, int category,
542 const QString &key, const QString &app )
543{
544 Field *field = new Field( new FieldImpl( FieldImpl::CustomField,
545 category | CustomCategory,
546 label, key, app ) );
547 mCustomFields.append( field );
548
549 return field;
550}