kabc Library API Documentation

field.src.cpp

00001 /*
00002     This file is part of libkabc.
00003     Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
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       return a.phoneNumber( PhoneNumber::Home ).number();
00130     case FieldImpl::BusinessPhone:
00131       return a.phoneNumber( PhoneNumber::Work ).number();
00132     case FieldImpl::MobilePhone:
00133       return a.phoneNumber( PhoneNumber::Cell ).number();
00134     case FieldImpl::HomeFax:
00135       return a.phoneNumber( PhoneNumber::Home | PhoneNumber::Fax ).number();
00136     case FieldImpl::BusinessFax:
00137       return a.phoneNumber( PhoneNumber::Work | PhoneNumber::Fax ).number();
00138     case FieldImpl::CarPhone:
00139       return a.phoneNumber( PhoneNumber::Car ).number();
00140     case FieldImpl::Isdn:
00141       return a.phoneNumber( PhoneNumber::Isdn ).number();
00142     case FieldImpl::Pager:
00143       return a.phoneNumber( PhoneNumber::Pager ).number();
00144     case FieldImpl::HomeAddressStreet:
00145       return a.address( Address::Home ).street();
00146     case FieldImpl::HomeAddressLocality:
00147       return a.address( Address::Home ).locality();
00148     case FieldImpl::HomeAddressRegion:
00149       return a.address( Address::Home ).region();
00150     case FieldImpl::HomeAddressPostalCode:
00151       return a.address( Address::Home ).postalCode();
00152     case FieldImpl::HomeAddressCountry:
00153       return a.address( Address::Home ).country();
00154     case FieldImpl::BusinessAddressStreet:
00155       return a.address( Address::Work ).street();
00156     case FieldImpl::BusinessAddressLocality:
00157       return a.address( Address::Work ).locality();
00158     case FieldImpl::BusinessAddressRegion:
00159       return a.address( Address::Work ).region();
00160     case FieldImpl::BusinessAddressPostalCode:
00161       return a.address( Address::Work ).postalCode();
00162     case FieldImpl::BusinessAddressCountry:
00163       return a.address( Address::Work ).country();
00164     case FieldImpl::CustomField:
00165       return a.custom( mImpl->app(), mImpl->key() );
00166     default:
00167       return QString::null;
00168   }
00169 }
00170 
00171 bool Field::setValue( KABC::Addressee &a, const QString &value )
00172 {
00173   switch ( mImpl->fieldId() ) {
00174     --CASESETVALUE--
00175     case FieldImpl::Birthday:
00176       a.setBirthday( QDate::fromString( value, Qt::ISODate ) );
00177     case FieldImpl::CustomField:
00178       a.insertCustom( mImpl->app(), mImpl->key(), value );
00179     default:
00180       return false;
00181   }
00182 }
00183 
00184 bool Field::isCustom()
00185 {
00186   return mImpl->fieldId() == FieldImpl::CustomField;
00187 }
00188 
00189 Field::List Field::allFields()
00190 {
00191   if ( mAllFields.isEmpty() ) {
00192     --CREATEFIELDS--
00193   }
00194 
00195   return mAllFields;
00196 }
00197 
00198 Field::List Field::defaultFields()
00199 {
00200   if ( mDefaultFields.isEmpty() ) {
00201     createDefaultField( FieldImpl::GivenName );
00202     createDefaultField( FieldImpl::FamilyName );
00203     createDefaultField( FieldImpl::Email );
00204   }
00205 
00206   return mDefaultFields;
00207 }
00208 
00209 void Field::createField( int id, int category )
00210 {
00211   mAllFields.append( new Field( new FieldImpl( id, category ) ) );
00212 }
00213 
00214 void Field::createDefaultField( int id, int category )
00215 {
00216   mDefaultFields.append( new Field( new FieldImpl( id, category ) ) );
00217 }
00218 
00219 void Field::deleteFields()
00220 {
00221   Field::List::ConstIterator it;
00222 
00223   for( it = mAllFields.begin(); it != mAllFields.end(); ++it ) {
00224     delete (*it);
00225   }
00226   mAllFields.clear();
00227 
00228   for( it = mDefaultFields.begin(); it != mDefaultFields.end(); ++it ) {
00229     delete (*it);
00230   }
00231   mDefaultFields.clear();
00232 
00233   for( it = mCustomFields.begin(); it != mCustomFields.end(); ++it ) {
00234     delete (*it);
00235   }
00236   mCustomFields.clear();
00237 }
00238 
00239 void Field::saveFields( const QString &identifier,
00240                         const Field::List &fields )
00241 {
00242   KConfig *cfg = KGlobal::config();
00243   KConfigGroupSaver( cfg, "KABCFields" );
00244 
00245   saveFields( cfg, identifier, fields );
00246 }
00247 
00248 void Field::saveFields( KConfig *cfg, const QString &identifier,
00249                         const Field::List &fields )
00250 {
00251   QValueList<int> fieldIds;
00252   
00253   int custom = 0;
00254   Field::List::ConstIterator it;
00255   for( it = fields.begin(); it != fields.end(); ++it ) {
00256     fieldIds.append( (*it)->mImpl->fieldId() );
00257     if( (*it)->isCustom() ) {
00258       QStringList customEntry;
00259       customEntry << (*it)->mImpl->label();
00260       customEntry << (*it)->mImpl->key();
00261       customEntry << (*it)->mImpl->app();
00262       cfg->writeEntry( "KABC_CustomEntry_" + identifier + "_" +
00263                        QString::number( custom++ ), customEntry );
00264     }
00265   }
00266   
00267   cfg->writeEntry( identifier, fieldIds );
00268 }
00269 
00270 Field::List Field::restoreFields( const QString &identifier )
00271 {
00272   KConfig *cfg = KGlobal::config();
00273   KConfigGroupSaver( cfg, "KABCFields" );
00274  
00275   return restoreFields( cfg, identifier );
00276 }
00277 
00278 Field::List Field::restoreFields( KConfig *cfg, const QString &identifier )
00279 {
00280   QValueList<int> fieldIds = cfg->readIntListEntry( identifier );
00281 
00282   Field::List fields;
00283 
00284   int custom = 0;
00285   QValueList<int>::ConstIterator it;
00286   for( it = fieldIds.begin(); it != fieldIds.end(); ++it ) {
00287     FieldImpl *f = 0;
00288     if ( (*it) == FieldImpl::CustomField ) {
00289       QStringList customEntry = cfg->readListEntry( "KABC_CustomEntry_" +
00290                                                  identifier + "_" +
00291                                                  QString::number( custom++ ) );
00292       f = new FieldImpl( *it, CustomCategory, customEntry[ 0 ],
00293                          customEntry[ 1 ], customEntry[ 2 ] );
00294     } else {
00295       f = new FieldImpl( *it );
00296     }
00297     fields.append( new Field( f ) );
00298   }
00299   
00300   return fields;
00301 }
00302 
00303 bool Field::equals( Field *field )
00304 {
00305   bool sameId = ( mImpl->fieldId() == field->mImpl->fieldId() );
00306 
00307   if ( !sameId ) return false;
00308 
00309   if ( mImpl->fieldId() != FieldImpl::CustomField ) return true;
00310   
00311   return mImpl->key() == field->mImpl->key();
00312 }
00313 
00314 Field *Field::createCustomField( const QString &label, int category,
00315                                  const QString &key, const QString &app )
00316 {
00317   Field *field = new Field( new FieldImpl( FieldImpl::CustomField,
00318                                            category | CustomCategory,
00319                                            label, key, app ) );
00320   mCustomFields.append( field );
00321 
00322   return field;
00323 }
KDE Logo
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:20 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001