00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
#include <klocale.h>
00022
#include <kconfig.h>
00023
#include <kglobal.h>
00024
00025
#include "field.h"
00026
00027
using namespace KABC;
00028
00029
class Field::FieldImpl
00030 {
00031
public:
00032 FieldImpl(
int fieldId,
int category = 0,
00033
const QString &label = QString::null,
00034
const QString &key = QString::null,
00035
const QString &app = QString::null )
00036 : mFieldId( fieldId ), mCategory( category ), mLabel(
label ),
00037 mKey(
key ), mApp( app ) {}
00038
00039
enum FieldId
00040 {
00041 CustomField,
00042 --ENUMS--
00043 };
00044
00045
int fieldId() {
return mFieldId; }
00046
int category() {
return mCategory; }
00047
00048
QString label() {
return mLabel; }
00049
QString key() {
return mKey; }
00050
QString app() {
return mApp; }
00051
00052
private:
00053
int mFieldId;
00054
int mCategory;
00055
00056
QString mLabel;
00057
QString mKey;
00058
QString mApp;
00059 };
00060
00061
00062 Field::List Field::mAllFields;
00063 Field::List Field::mDefaultFields;
00064 Field::List Field::mCustomFields;
00065
00066
00067 Field::Field( FieldImpl *impl )
00068 {
00069 mImpl = impl;
00070 }
00071
00072 Field::~Field()
00073 {
00074
delete mImpl;
00075 }
00076
00077
QString Field::label()
00078 {
00079
switch ( mImpl->fieldId() ) {
00080 --CASELABEL--
00081
case FieldImpl::CustomField:
00082
return mImpl->label();
00083
default:
00084
return i18n(
"Unknown Field");
00085 }
00086 }
00087
00088
int Field::category()
00089 {
00090
return mImpl->category();
00091 }
00092
00093
QString Field::categoryLabel(
int category )
00094 {
00095
switch ( category ) {
00096
case All:
00097
return i18n(
"All");
00098
case Frequent:
00099
return i18n(
"Frequent");
00100
case Address:
00101
return i18n(
"Address");
00102
case Email:
00103
return i18n(
"Email");
00104
case Personal:
00105
return i18n(
"Personal");
00106
case Organization:
00107
return i18n(
"Organization");
00108
case CustomCategory:
00109
return i18n(
"Custom");
00110
default:
00111
return i18n(
"Undefined");
00112 }
00113 }
00114
00115
QString Field::value(
const KABC::Addressee &a )
00116 {
00117
switch ( mImpl->fieldId() ) {
00118 --CASEVALUE--
00119
case FieldImpl::Email:
00120
return a.
preferredEmail();
00121
case FieldImpl::Birthday:
00122
if ( a.
birthday().isValid() )
00123
return a.
birthday().date().toString( Qt::ISODate );
00124
else
00125
return QString::null;
00126
case FieldImpl::Url:
00127
return a.
url().
prettyURL();
00128
case FieldImpl::HomePhone:
00129 {
00130
00131 PhoneNumber::List list = a.
phoneNumbers( PhoneNumber::Home | PhoneNumber::Pref );
00132 PhoneNumber::List::Iterator it;
00133
for ( it = list.begin(); it != list.end(); ++it )
00134
if ( ((*it).type() & ~(PhoneNumber::Pref)) == PhoneNumber::Home )
00135
return (*it).number();
00136
00137
00138 list = a.
phoneNumbers( PhoneNumber::Home );
00139
for ( it = list.begin(); it != list.end(); ++it )
00140
if ( ((*it).type() & ~(PhoneNumber::Pref)) == PhoneNumber::Home )
00141
return (*it).number();
00142
00143
return QString::null;
00144 }
00145
case FieldImpl::BusinessPhone:
00146 {
00147
00148 PhoneNumber::List list = a.
phoneNumbers( PhoneNumber::Work | PhoneNumber::Pref );
00149 PhoneNumber::List::Iterator it;
00150
for ( it = list.begin(); it != list.end(); ++it )
00151
if ( ((*it).type() & ~(PhoneNumber::Pref)) == PhoneNumber::Work )
00152
return (*it).number();
00153
00154
00155 list = a.
phoneNumbers( PhoneNumber::Work );
00156
for ( it = list.begin(); it != list.end(); ++it )
00157
if ( ((*it).type() & ~(PhoneNumber::Pref)) == PhoneNumber::Work )
00158
return (*it).number();
00159
return QString::null;
00160 }
00161
case FieldImpl::MobilePhone:
00162
return a.
phoneNumber( PhoneNumber::Cell ).
number();
00163
case FieldImpl::HomeFax:
00164
return a.
phoneNumber( PhoneNumber::Home | PhoneNumber::Fax ).
number();
00165
case FieldImpl::BusinessFax:
00166
return a.
phoneNumber( PhoneNumber::Work | PhoneNumber::Fax ).
number();
00167
case FieldImpl::CarPhone:
00168
return a.
phoneNumber( PhoneNumber::Car ).
number();
00169
case FieldImpl::Isdn:
00170
return a.
phoneNumber( PhoneNumber::Isdn ).
number();
00171
case FieldImpl::Pager:
00172
return a.
phoneNumber( PhoneNumber::Pager ).
number();
00173
case FieldImpl::HomeAddressStreet:
00174
return a.
address( Address::Home ).
street();
00175
case FieldImpl::HomeAddressLocality:
00176
return a.
address( Address::Home ).
locality();
00177
case FieldImpl::HomeAddressRegion:
00178
return a.
address( Address::Home ).
region();
00179
case FieldImpl::HomeAddressPostalCode:
00180
return a.
address( Address::Home ).
postalCode();
00181
case FieldImpl::HomeAddressCountry:
00182
return a.
address( Address::Home ).
country();
00183
case FieldImpl::BusinessAddressStreet:
00184
return a.
address( Address::Work ).
street();
00185
case FieldImpl::BusinessAddressLocality:
00186
return a.
address( Address::Work ).
locality();
00187
case FieldImpl::BusinessAddressRegion:
00188
return a.
address( Address::Work ).
region();
00189
case FieldImpl::BusinessAddressPostalCode:
00190
return a.
address( Address::Work ).
postalCode();
00191
case FieldImpl::BusinessAddressCountry:
00192
return a.
address( Address::Work ).
country();
00193
case FieldImpl::CustomField:
00194
return a.
custom( mImpl->app(), mImpl->key() );
00195
default:
00196
return QString::null;
00197 }
00198 }
00199
00200
bool Field::setValue(
KABC::Addressee &a,
const QString &value )
00201 {
00202
switch ( mImpl->fieldId() ) {
00203 --CASESETVALUE--
00204
case FieldImpl::Birthday:
00205 a.
setBirthday( QDate::fromString( value, Qt::ISODate ) );
00206
case FieldImpl::CustomField:
00207 a.
insertCustom( mImpl->app(), mImpl->key(), value );
00208
default:
00209
return false;
00210 }
00211 }
00212
00213
QString Field::sortKey(
const KABC::Addressee &a )
00214 {
00215
switch ( mImpl->fieldId() ) {
00216 --CASEVALUE--
00217
case FieldImpl::Birthday:
00218
if ( a.
birthday().isValid() ) {
00219
QDate date = a.
birthday().date();
00220
QString key;
00221
key.sprintf(
"%02d-%02d", date.month(), date.day() );
00222
return key;
00223 }
else
00224
return QString(
"00-00" );
00225
default:
00226
return value( a ).lower();
00227 }
00228 }
00229
00230
bool Field::isCustom()
00231 {
00232
return mImpl->fieldId() == FieldImpl::CustomField;
00233 }
00234
00235 Field::List Field::allFields()
00236 {
00237
if ( mAllFields.isEmpty() ) {
00238 --CREATEFIELDS--
00239 }
00240
00241
return mAllFields;
00242 }
00243
00244 Field::List Field::defaultFields()
00245 {
00246
if ( mDefaultFields.isEmpty() ) {
00247 createDefaultField( FieldImpl::GivenName );
00248 createDefaultField( FieldImpl::FamilyName );
00249 createDefaultField( FieldImpl::Email );
00250 }
00251
00252
return mDefaultFields;
00253 }
00254
00255
void Field::createField(
int id,
int category )
00256 {
00257 mAllFields.append(
new Field(
new FieldImpl(
id, category ) ) );
00258 }
00259
00260
void Field::createDefaultField(
int id,
int category )
00261 {
00262 mDefaultFields.append(
new Field(
new FieldImpl(
id, category ) ) );
00263 }
00264
00265
void Field::deleteFields()
00266 {
00267 Field::List::ConstIterator it;
00268
00269
for( it = mAllFields.begin(); it != mAllFields.end(); ++it ) {
00270
delete (*it);
00271 }
00272 mAllFields.clear();
00273
00274
for( it = mDefaultFields.begin(); it != mDefaultFields.end(); ++it ) {
00275
delete (*it);
00276 }
00277 mDefaultFields.clear();
00278
00279
for( it = mCustomFields.begin(); it != mCustomFields.end(); ++it ) {
00280
delete (*it);
00281 }
00282 mCustomFields.clear();
00283 }
00284
00285
void Field::saveFields(
const QString &identifier,
00286
const Field::List &fields )
00287 {
00288
KConfig *cfg =
KGlobal::config();
00289
KConfigGroupSaver( cfg,
"KABCFields" );
00290
00291 saveFields( cfg, identifier, fields );
00292 }
00293
00294
void Field::saveFields(
KConfig *cfg,
const QString &identifier,
00295
const Field::List &fields )
00296 {
00297
QValueList<int> fieldIds;
00298
00299
int custom = 0;
00300 Field::List::ConstIterator it;
00301
for( it = fields.begin(); it != fields.end(); ++it ) {
00302 fieldIds.append( (*it)->mImpl->fieldId() );
00303
if( (*it)->isCustom() ) {
00304
QStringList customEntry;
00305 customEntry << (*it)->mImpl->label();
00306 customEntry << (*it)->mImpl->key();
00307 customEntry << (*it)->mImpl->app();
00308 cfg->
writeEntry(
"KABC_CustomEntry_" + identifier +
"_" +
00309 QString::number( custom++ ), customEntry );
00310 }
00311 }
00312
00313 cfg->
writeEntry( identifier, fieldIds );
00314 }
00315
00316 Field::List Field::restoreFields(
const QString &identifier )
00317 {
00318
KConfig *cfg =
KGlobal::config();
00319
KConfigGroupSaver( cfg,
"KABCFields" );
00320
00321
return restoreFields( cfg, identifier );
00322 }
00323
00324 Field::List Field::restoreFields(
KConfig *cfg,
const QString &identifier )
00325 {
00326
QValueList<int> fieldIds = cfg->
readIntListEntry( identifier );
00327
00328 Field::List fields;
00329
00330
int custom = 0;
00331
QValueList<int>::ConstIterator it;
00332
for( it = fieldIds.begin(); it != fieldIds.end(); ++it ) {
00333 FieldImpl *f = 0;
00334
if ( (*it) == FieldImpl::CustomField ) {
00335
QStringList customEntry = cfg->
readListEntry(
"KABC_CustomEntry_" +
00336 identifier +
"_" +
00337 QString::number( custom++ ) );
00338 f =
new FieldImpl( *it, CustomCategory, customEntry[ 0 ],
00339 customEntry[ 1 ], customEntry[ 2 ] );
00340 }
else {
00341 f =
new FieldImpl( *it );
00342 }
00343 fields.append(
new Field( f ) );
00344 }
00345
00346
return fields;
00347 }
00348
00349
bool Field::equals( Field *field )
00350 {
00351
bool sameId = ( mImpl->fieldId() == field->mImpl->fieldId() );
00352
00353
if ( !sameId )
return false;
00354
00355
if ( mImpl->fieldId() != FieldImpl::CustomField )
return true;
00356
00357
return mImpl->key() == field->mImpl->key();
00358 }
00359
00360 Field *Field::createCustomField(
const QString &label,
int category,
00361
const QString &key,
const QString &app )
00362 {
00363 Field *field =
new Field(
new FieldImpl( FieldImpl::CustomField,
00364 category | CustomCategory,
00365 label, key, app ) );
00366 mCustomFields.append( field );
00367
00368
return field;
00369 }