# kabc Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# field.cpp

    00001 /*** Warning! This file has been generated by the script makeaddressee ***/
    00002 /*
    00003     This file is part of libkabc.
    00004     Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
    00005
    00006     This library is free software; you can redistribute it and/or
    00007     modify it under the terms of the GNU Library General Public
    00008     License as published by the Free Software Foundation; either
    00009     version 2 of the License, or (at your option) any later version.
    00010
    00011     This library is distributed in the hope that it will be useful,
    00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014     Library General Public License for more details.
    00015
    00016     You should have received a copy of the GNU Library General Public License
    00017     along with this library; see the file COPYING.LIB.  If not, write to
    00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00019     Boston, MA 02111-1307, USA.
    00020 */
    00021
    00022 #include <klocale.h>
    00023 #include <kconfig.h>
    00024 #include <kglobal.h>
    00025
    00026 #include "field.h"
    00027
    00028 using namespace KABC;
    00029
    00030 class Field::FieldImpl
    00031 {
    00032   public:
    00033     FieldImpl( int fieldId, int category = 0,
    00034                const QString &label = QString::null,
    00035                const QString &key = QString::null,
    00036                const QString &app = QString::null )
    00037       : mFieldId( fieldId ), mCategory( category ), mLabel( label ),
    00038         mKey( key ), mApp( app ) {}
    00039
    00040     enum FieldId
    00041     {
    00042       CustomField,
    00043       FormattedName,
    00044       FamilyName,
    00045       GivenName,
    00046       AdditionalName,
    00047       Prefix,
    00048       Suffix,
    00049       NickName,
    00050       Birthday,
    00051       HomeAddressStreet,
    00052       HomeAddressLocality,
    00053       HomeAddressRegion,
    00054       HomeAddressPostalCode,
    00055       HomeAddressCountry,
    00056       HomeAddressLabel,
    00057       BusinessAddressStreet,
    00058       BusinessAddressLocality,
    00059       BusinessAddressRegion,
    00060       BusinessAddressPostalCode,
    00061       BusinessAddressCountry,
    00062       BusinessAddressLabel,
    00063       HomePhone,
    00064       BusinessPhone,
    00065       MobilePhone,
    00066       HomeFax,
    00067       BusinessFax,
    00068       CarPhone,
    00069       Isdn,
    00070       Pager,
    00071       Email,
    00072       Mailer,
    00073       Title,
    00074       Role,
    00075       Organization,
    00076       Note,
    00077       Url
    00078     };
    00079
    00080     int fieldId() { return mFieldId; }
    00081     int category() { return mCategory; }
    00082
    00083     QString label() { return mLabel; }
    00084     QString key() { return mKey; }
    00085     QString app() { return mApp; }
    00086
    00087   private:
    00088     int mFieldId;
    00089     int mCategory;
    00090
    00091     QString mLabel;
    00092     QString mKey;
    00093     QString mApp;
    00094 };
    00095
    00096
    00097 Field::List Field::mAllFields;
    00098 Field::List Field::mDefaultFields;
    00099 Field::List Field::mCustomFields;
    00100
    00101
    00102 Field::Field( FieldImpl *impl )
    00103 {
    00104   mImpl = impl;
    00105 }
    00106
    00107 Field::~Field()
    00108 {
    00109   delete mImpl;
    00110 }
    00111
    00112 QString Field::label()
    00113 {
    00114   switch ( mImpl->fieldId() ) {
    00115     case FieldImpl::FormattedName:
    00116       return Addressee::formattedNameLabel();
    00117     case FieldImpl::FamilyName:
    00118       return Addressee::familyNameLabel();
    00119     case FieldImpl::GivenName:
    00120       return Addressee::givenNameLabel();
    00121     case FieldImpl::AdditionalName:
    00122       return Addressee::additionalNameLabel();
    00123     case FieldImpl::Prefix:
    00124       return Addressee::prefixLabel();
    00125     case FieldImpl::Suffix:
    00126       return Addressee::suffixLabel();
    00127     case FieldImpl::NickName:
    00128       return Addressee::nickNameLabel();
    00129     case FieldImpl::Birthday:
    00130       return Addressee::birthdayLabel();
    00131     case FieldImpl::HomeAddressStreet:
    00132       return Addressee::homeAddressStreetLabel();
    00133     case FieldImpl::HomeAddressLocality:
    00134       return Addressee::homeAddressLocalityLabel();
    00135     case FieldImpl::HomeAddressRegion:
    00136       return Addressee::homeAddressRegionLabel();
    00137     case FieldImpl::HomeAddressPostalCode:
    00138       return Addressee::homeAddressPostalCodeLabel();
    00139     case FieldImpl::HomeAddressCountry:
    00140       return Addressee::homeAddressCountryLabel();
    00141     case FieldImpl::HomeAddressLabel:
    00142       return Addressee::homeAddressLabelLabel();
    00143     case FieldImpl::BusinessAddressStreet:
    00144       return Addressee::businessAddressStreetLabel();
    00145     case FieldImpl::BusinessAddressLocality:
    00146       return Addressee::businessAddressLocalityLabel();
    00147     case FieldImpl::BusinessAddressRegion:
    00148       return Addressee::businessAddressRegionLabel();
    00149     case FieldImpl::BusinessAddressPostalCode:
    00150       return Addressee::businessAddressPostalCodeLabel();
    00151     case FieldImpl::BusinessAddressCountry:
    00152       return Addressee::businessAddressCountryLabel();
    00153     case FieldImpl::BusinessAddressLabel:
    00154       return Addressee::businessAddressLabelLabel();
    00155     case FieldImpl::HomePhone:
    00156       return Addressee::homePhoneLabel();
    00157     case FieldImpl::BusinessPhone:
    00158       return Addressee::businessPhoneLabel();
    00159     case FieldImpl::MobilePhone:
    00160       return Addressee::mobilePhoneLabel();
    00161     case FieldImpl::HomeFax:
    00162       return Addressee::homeFaxLabel();
    00163     case FieldImpl::BusinessFax:
    00164       return Addressee::businessFaxLabel();
    00165     case FieldImpl::CarPhone:
    00166       return Addressee::carPhoneLabel();
    00167     case FieldImpl::Isdn:
    00168       return Addressee::isdnLabel();
    00169     case FieldImpl::Pager:
    00170       return Addressee::pagerLabel();
    00171     case FieldImpl::Email:
    00172       return Addressee::emailLabel();
    00173     case FieldImpl::Mailer:
    00174       return Addressee::mailerLabel();
    00175     case FieldImpl::Title:
    00176       return Addressee::titleLabel();
    00177     case FieldImpl::Role:
    00178       return Addressee::roleLabel();
    00179     case FieldImpl::Organization:
    00180       return Addressee::organizationLabel();
    00181     case FieldImpl::Note:
    00182       return Addressee::noteLabel();
    00183     case FieldImpl::Url:
    00184       return Addressee::urlLabel();
    00185     case FieldImpl::CustomField:
    00186       return mImpl->label();
    00187     default:
    00188       return i18n("Unknown Field");
    00189   }
    00190 }
    00191
    00192 int Field::category()
    00193 {
    00194   return mImpl->category();
    00195 }
    00196
    00197 QString Field::categoryLabel( int category )
    00198 {
    00199   switch ( category ) {
    00200     case All:
    00201       return i18n("All");
    00202     case Frequent:
    00203       return i18n("Frequent");
    00204     case Address:
    00205       return i18n("Address");
    00206     case Email:
    00207       return i18n("Email");
    00208     case Personal:
    00209       return i18n("Personal");
    00210     case Organization:
    00211       return i18n("Organization");
    00212     case CustomCategory:
    00213       return i18n("Custom");
    00214     default:
    00215       return i18n("Undefined");
    00216   }
    00217 }
    00218
    00219 QString Field::value( const KABC::Addressee &a )
    00220 {
    00221   switch ( mImpl->fieldId() ) {
    00222     case FieldImpl::FormattedName:
    00223       return a.formattedName();
    00224     case FieldImpl::FamilyName:
    00225       return a.familyName();
    00226     case FieldImpl::GivenName:
    00227       return a.givenName();
    00228     case FieldImpl::AdditionalName:
    00229       return a.additionalName();
    00230     case FieldImpl::Prefix:
    00231       return a.prefix();
    00232     case FieldImpl::Suffix:
    00233       return a.suffix();
    00234     case FieldImpl::NickName:
    00235       return a.nickName();
    00236     case FieldImpl::Mailer:
    00237       return a.mailer();
    00238     case FieldImpl::Title:
    00239       return a.title();
    00240     case FieldImpl::Role:
    00241       return a.role();
    00242     case FieldImpl::Organization:
    00243       return a.organization();
    00244     case FieldImpl::Note:
    00245       return a.note();
    00246     case FieldImpl::Email:
    00247       return a.preferredEmail();
    00248     case FieldImpl::Birthday:
    00249       if ( a.birthday().isValid() )
    00250         return a.birthday().date().toString( Qt::ISODate );
    00251       else
    00252         return QString::null;
    00253     case FieldImpl::Url:
    00254       return a.url().prettyURL();
    00255     case FieldImpl::HomePhone:
    00256       return a.phoneNumber( PhoneNumber::Home ).number();
    00257     case FieldImpl::BusinessPhone:
    00258       return a.phoneNumber( PhoneNumber::Work ).number();
    00259     case FieldImpl::MobilePhone:
    00260       return a.phoneNumber( PhoneNumber::Cell ).number();
    00261     case FieldImpl::HomeFax:
    00262       return a.phoneNumber( PhoneNumber::Home | PhoneNumber::Fax ).number();
    00263     case FieldImpl::BusinessFax:
    00264       return a.phoneNumber( PhoneNumber::Work | PhoneNumber::Fax ).number();
    00265     case FieldImpl::CarPhone:
    00266       return a.phoneNumber( PhoneNumber::Car ).number();
    00267     case FieldImpl::Isdn:
    00268       return a.phoneNumber( PhoneNumber::Isdn ).number();
    00269     case FieldImpl::Pager:
    00270       return a.phoneNumber( PhoneNumber::Pager ).number();
    00271     case FieldImpl::HomeAddressStreet:
    00272       return a.address( Address::Home ).street();
    00273     case FieldImpl::HomeAddressLocality:
    00274       return a.address( Address::Home ).locality();
    00275     case FieldImpl::HomeAddressRegion:
    00276       return a.address( Address::Home ).region();
    00277     case FieldImpl::HomeAddressPostalCode:
    00278       return a.address( Address::Home ).postalCode();
    00279     case FieldImpl::HomeAddressCountry:
    00280       return a.address( Address::Home ).country();
    00281     case FieldImpl::BusinessAddressStreet:
    00282       return a.address( Address::Work ).street();
    00283     case FieldImpl::BusinessAddressLocality:
    00284       return a.address( Address::Work ).locality();
    00285     case FieldImpl::BusinessAddressRegion:
    00286       return a.address( Address::Work ).region();
    00287     case FieldImpl::BusinessAddressPostalCode:
    00288       return a.address( Address::Work ).postalCode();
    00289     case FieldImpl::BusinessAddressCountry:
    00290       return a.address( Address::Work ).country();
    00291     case FieldImpl::CustomField:
    00292       return a.custom( mImpl->app(), mImpl->key() );
    00293     default:
    00294       return QString::null;
    00295   }
    00296 }
    00297
    00298 bool Field::setValue( KABC::Addressee &a, const QString &value )
    00299 {
    00300   switch ( mImpl->fieldId() ) {
    00301     case FieldImpl::FormattedName:
    00302       a.setFormattedName( value );
    00303       return true;
    00304     case FieldImpl::FamilyName:
    00305       a.setFamilyName( value );
    00306       return true;
    00307     case FieldImpl::GivenName:
    00308       a.setGivenName( value );
    00309       return true;
    00310     case FieldImpl::AdditionalName:
    00311       a.setAdditionalName( value );
    00312       return true;
    00313     case FieldImpl::Prefix:
    00314       a.setPrefix( value );
    00315       return true;
    00316     case FieldImpl::Suffix:
    00317       a.setSuffix( value );
    00318       return true;
    00319     case FieldImpl::NickName:
    00320       a.setNickName( value );
    00321       return true;
    00322     case FieldImpl::Mailer:
    00323       a.setMailer( value );
    00324       return true;
    00325     case FieldImpl::Title:
    00326       a.setTitle( value );
    00327       return true;
    00328     case FieldImpl::Role:
    00329       a.setRole( value );
    00330       return true;
    00331     case FieldImpl::Organization:
    00332       a.setOrganization( value );
    00333       return true;
    00334     case FieldImpl::Note:
    00335       a.setNote( value );
    00336       return true;
    00337     case FieldImpl::Birthday:
    00338       a.setBirthday( QDate::fromString( value, Qt::ISODate ) );
    00339     case FieldImpl::CustomField:
    00340       a.insertCustom( mImpl->app(), mImpl->key(), value );
    00341     default:
    00342       return false;
    00343   }
    00344 }
    00345
    00346 bool Field::isCustom()
    00347 {
    00348   return mImpl->fieldId() == FieldImpl::CustomField;
    00349 }
    00350
    00351 Field::List Field::allFields()
    00352 {
    00353   if ( mAllFields.isEmpty() ) {
    00354     createField( FieldImpl::FormattedName, Frequent );
    00355     createField( FieldImpl::FamilyName, Frequent );
    00356     createField( FieldImpl::GivenName, Frequent );
    00357     createField( FieldImpl::AdditionalName );
    00358     createField( FieldImpl::Prefix );
    00359     createField( FieldImpl::Suffix );
    00360     createField( FieldImpl::NickName, Personal );
    00361     createField( FieldImpl::Birthday, Personal );
    00362     createField( FieldImpl::HomeAddressStreet, Address|Personal );
    00363     createField( FieldImpl::HomeAddressLocality, Address|Personal );
    00364     createField( FieldImpl::HomeAddressRegion, Address|Personal );
    00365     createField( FieldImpl::HomeAddressPostalCode, Address|Personal );
    00366     createField( FieldImpl::HomeAddressCountry, Address|Personal );
    00367     createField( FieldImpl::HomeAddressLabel, Address|Personal );
    00368     createField( FieldImpl::BusinessAddressStreet, Address|Organization );
    00369     createField( FieldImpl::BusinessAddressLocality, Address|Organization );
    00370     createField( FieldImpl::BusinessAddressRegion, Address|Organization );
    00371     createField( FieldImpl::BusinessAddressPostalCode, Address|Organization );
    00372     createField( FieldImpl::BusinessAddressCountry, Address|Organization );
    00373     createField( FieldImpl::BusinessAddressLabel, Address|Organization );
    00374     createField( FieldImpl::HomePhone, Personal|Frequent );
    00375     createField( FieldImpl::BusinessPhone, Organization|Frequent );
    00376     createField( FieldImpl::MobilePhone, Frequent );
    00377     createField( FieldImpl::HomeFax );
    00378     createField( FieldImpl::BusinessFax );
    00379     createField( FieldImpl::CarPhone );
    00380     createField( FieldImpl::Isdn );
    00381     createField( FieldImpl::Pager );
    00382     createField( FieldImpl::Email, Email|Frequent );
    00383     createField( FieldImpl::Mailer, Email );
    00384     createField( FieldImpl::Title, Organization );
    00385     createField( FieldImpl::Role, Organization );
    00386     createField( FieldImpl::Organization, Organization );
    00387     createField( FieldImpl::Note );
    00388     createField( FieldImpl::Url );
    00389   }
    00390
    00391   return mAllFields;
    00392 }
    00393
    00394 Field::List Field::defaultFields()
    00395 {
    00396   if ( mDefaultFields.isEmpty() ) {
    00397     createDefaultField( FieldImpl::GivenName );
    00398     createDefaultField( FieldImpl::FamilyName );
    00399     createDefaultField( FieldImpl::Email );
    00400   }
    00401
    00402   return mDefaultFields;
    00403 }
    00404
    00405 void Field::createField( int id, int category )
    00406 {
    00407   mAllFields.append( new Field( new FieldImpl( id, category ) ) );
    00408 }
    00409
    00410 void Field::createDefaultField( int id, int category )
    00411 {
    00412   mDefaultFields.append( new Field( new FieldImpl( id, category ) ) );
    00413 }
    00414
    00415 void Field::deleteFields()
    00416 {
    00417   Field::List::ConstIterator it;
    00418
    00419   for( it = mAllFields.begin(); it != mAllFields.end(); ++it ) {
    00420     delete (*it);
    00421   }
    00422   mAllFields.clear();
    00423
    00424   for( it = mDefaultFields.begin(); it != mDefaultFields.end(); ++it ) {
    00425     delete (*it);
    00426   }
    00427   mDefaultFields.clear();
    00428
    00429   for( it = mCustomFields.begin(); it != mCustomFields.end(); ++it ) {
    00430     delete (*it);
    00431   }
    00432   mCustomFields.clear();
    00433 }
    00434
    00435 void Field::saveFields( const QString &identifier,
    00436                         const Field::List &fields )
    00437 {
    00438   KConfig *cfg = KGlobal::config();
    00439   KConfigGroupSaver( cfg, "KABCFields" );
    00440
    00441   saveFields( cfg, identifier, fields );
    00442 }
    00443
    00444 void Field::saveFields( KConfig *cfg, const QString &identifier,
    00445                         const Field::List &fields )
    00446 {
    00447   QValueList<int> fieldIds;
    00448
    00449   int custom = 0;
    00450   Field::List::ConstIterator it;
    00451   for( it = fields.begin(); it != fields.end(); ++it ) {
    00452     fieldIds.append( (*it)->mImpl->fieldId() );
    00453     if( (*it)->isCustom() ) {
    00454       QStringList customEntry;
    00455       customEntry << (*it)->mImpl->label();
    00456       customEntry << (*it)->mImpl->key();
    00457       customEntry << (*it)->mImpl->app();
    00458       cfg->writeEntry( "KABC_CustomEntry_" + identifier + "_" +
    00459                        QString::number( custom++ ), customEntry );
    00460     }
    00461   }
    00462
    00463   cfg->writeEntry( identifier, fieldIds );
    00464 }
    00465
    00466 Field::List Field::restoreFields( const QString &identifier )
    00467 {
    00468   KConfig *cfg = KGlobal::config();
    00469   KConfigGroupSaver( cfg, "KABCFields" );
    00470
    00471   return restoreFields( cfg, identifier );
    00472 }
    00473
    00474 Field::List Field::restoreFields( KConfig *cfg, const QString &identifier )
    00475 {
    00476   QValueList<int> fieldIds = cfg->readIntListEntry( identifier );
    00477
    00478   Field::List fields;
    00479
    00480   int custom = 0;
    00481   QValueList<int>::ConstIterator it;
    00482   for( it = fieldIds.begin(); it != fieldIds.end(); ++it ) {
    00483     FieldImpl *f = 0;
    00484     if ( (*it) == FieldImpl::CustomField ) {
    00485       QStringList customEntry = cfg->readListEntry( "KABC_CustomEntry_" +
    00486                                                  identifier + "_" +
    00487                                                  QString::number( custom++ ) );
    00488       f = new FieldImpl( *it, CustomCategory, customEntry[ 0 ],
    00489                          customEntry[ 1 ], customEntry[ 2 ] );
    00490     } else {
    00491       f = new FieldImpl( *it );
    00492     }
    00493     fields.append( new Field( f ) );
    00494   }
    00495
    00496   return fields;
    00497 }
    00498
    00499 bool Field::equals( Field *field )
    00500 {
    00501   bool sameId = ( mImpl->fieldId() == field->mImpl->fieldId() );
    00502
    00503   if ( !sameId ) return false;
    00504
    00505   if ( mImpl->fieldId() != FieldImpl::CustomField ) return true;
    00506
    00507   return mImpl->key() == field->mImpl->key();
    00508 }
    00509
    00510 Field *Field::createCustomField( const QString &label, int category,
    00511                                  const QString &key, const QString &app )
    00512 {
    00513   Field *field = new Field( new FieldImpl( FieldImpl::CustomField,
    00514                                            category | CustomCategory,
    00515                                            label, key, app ) );
    00516   mCustomFields.append( field );
    00517
    00518   return field;
    00519 }

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:29:19 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001