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     {
00130       PhoneNumber::List list = a.phoneNumbers( PhoneNumber::Home );
00131       PhoneNumber::List::Iterator it;
00132       for ( it = list.begin(); it != list.end(); ++it )
00133         if ( ((*it).type() & ~(PhoneNumber::Pref)) == PhoneNumber::Home )
00134           return (*it).number();
00135       return QString::null;
00136     }
00137     case FieldImpl::BusinessPhone:
00138     {
00139       PhoneNumber::List list = a.phoneNumbers( PhoneNumber::Work );
00140       PhoneNumber::List::Iterator it;
00141       for ( it = list.begin(); it != list.end(); ++it )
00142         if ( ((*it).type() & ~(PhoneNumber::Pref)) == PhoneNumber::Work )
00143           return (*it).number();
00144       return QString::null;
00145     }
00146     case FieldImpl::MobilePhone:
00147       return a.phoneNumber( PhoneNumber::Cell ).number();
00148     case FieldImpl::HomeFax:
00149       return a.phoneNumber( PhoneNumber::Home | PhoneNumber::Fax ).number();
00150     case FieldImpl::BusinessFax:
00151       return a.phoneNumber( PhoneNumber::Work | PhoneNumber::Fax ).number();
00152     case FieldImpl::CarPhone:
00153       return a.phoneNumber( PhoneNumber::Car ).number();
00154     case FieldImpl::Isdn:
00155       return a.phoneNumber( PhoneNumber::Isdn ).number();
00156     case FieldImpl::Pager:
00157       return a.phoneNumber( PhoneNumber::Pager ).number();
00158     case FieldImpl::HomeAddressStreet:
00159       return a.address( Address::Home ).street();
00160     case FieldImpl::HomeAddressLocality:
00161       return a.address( Address::Home ).locality();
00162     case FieldImpl::HomeAddressRegion:
00163       return a.address( Address::Home ).region();
00164     case FieldImpl::HomeAddressPostalCode:
00165       return a.address( Address::Home ).postalCode();
00166     case FieldImpl::HomeAddressCountry:
00167       return a.address( Address::Home ).country();
00168     case FieldImpl::BusinessAddressStreet:
00169       return a.address( Address::Work ).street();
00170     case FieldImpl::BusinessAddressLocality:
00171       return a.address( Address::Work ).locality();
00172     case FieldImpl::BusinessAddressRegion:
00173       return a.address( Address::Work ).region();
00174     case FieldImpl::BusinessAddressPostalCode:
00175       return a.address( Address::Work ).postalCode();
00176     case FieldImpl::BusinessAddressCountry:
00177       return a.address( Address::Work ).country();
00178     case FieldImpl::CustomField:
00179       return a.custom( mImpl->app(), mImpl->key() );
00180     default:
00181       return QString::null;
00182   }
00183 }
00184 
00185 bool Field::setValue( KABC::Addressee &a, const QString &value )
00186 {
00187   switch ( mImpl->fieldId() ) {
00188     --CASESETVALUE--
00189     case FieldImpl::Birthday:
00190       a.setBirthday( QDate::fromString( value, Qt::ISODate ) );
00191     case FieldImpl::CustomField:
00192       a.insertCustom( mImpl->app(), mImpl->key(), value );
00193     default:
00194       return false;
00195   }
00196 }
00197 
00198 QString Field::sortKey( const KABC::Addressee &a )
00199 {
00200   switch ( mImpl->fieldId() ) {
00201     --CASEVALUE--
00202     case FieldImpl::Birthday:
00203       if ( a.birthday().isValid() ) {
00204         QDate date = a.birthday().date();
00205         QString key;
00206         key.sprintf( "%02d-%02d", date.month(), date.day() );
00207         return key;
00208       } else
00209         return QString( "00-00" );
00210     default:
00211       return value( a ).lower();
00212   }
00213 }
00214 
00215 bool Field::isCustom()
00216 {
00217   return mImpl->fieldId() == FieldImpl::CustomField;
00218 }
00219 
00220 Field::List Field::allFields()
00221 {
00222   if ( mAllFields.isEmpty() ) {
00223     --CREATEFIELDS--
00224   }
00225 
00226   return mAllFields;
00227 }
00228 
00229 Field::List Field::defaultFields()
00230 {
00231   if ( mDefaultFields.isEmpty() ) {
00232     createDefaultField( FieldImpl::GivenName );
00233     createDefaultField( FieldImpl::FamilyName );
00234     createDefaultField( FieldImpl::Email );
00235   }
00236 
00237   return mDefaultFields;
00238 }
00239 
00240 void Field::createField( int id, int category )
00241 {
00242   mAllFields.append( new Field( new FieldImpl( id, category ) ) );
00243 }
00244 
00245 void Field::createDefaultField( int id, int category )
00246 {
00247   mDefaultFields.append( new Field( new FieldImpl( id, category ) ) );
00248 }
00249 
00250 void Field::deleteFields()
00251 {
00252   Field::List::ConstIterator it;
00253 
00254   for( it = mAllFields.begin(); it != mAllFields.end(); ++it ) {
00255     delete (*it);
00256   }
00257   mAllFields.clear();
00258 
00259   for( it = mDefaultFields.begin(); it != mDefaultFields.end(); ++it ) {
00260     delete (*it);
00261   }
00262   mDefaultFields.clear();
00263 
00264   for( it = mCustomFields.begin(); it != mCustomFields.end(); ++it ) {
00265     delete (*it);
00266   }
00267   mCustomFields.clear();
00268 }
00269 
00270 void Field::saveFields( const QString &identifier,
00271                         const Field::List &fields )
00272 {
00273   KConfig *cfg = KGlobal::config();
00274   KConfigGroupSaver( cfg, "KABCFields" );
00275 
00276   saveFields( cfg, identifier, fields );
00277 }
00278 
00279 void Field::saveFields( KConfig *cfg, const QString &identifier,
00280                         const Field::List &fields )
00281 {
00282   QValueList<int> fieldIds;
00283   
00284   int custom = 0;
00285   Field::List::ConstIterator it;
00286   for( it = fields.begin(); it != fields.end(); ++it ) {
00287     fieldIds.append( (*it)->mImpl->fieldId() );
00288     if( (*it)->isCustom() ) {
00289       QStringList customEntry;
00290       customEntry << (*it)->mImpl->label();
00291       customEntry << (*it)->mImpl->key();
00292       customEntry << (*it)->mImpl->app();
00293       cfg->writeEntry( "KABC_CustomEntry_" + identifier + "_" +
00294                        QString::number( custom++ ), customEntry );
00295     }
00296   }
00297   
00298   cfg->writeEntry( identifier, fieldIds );
00299 }
00300 
00301 Field::List Field::restoreFields( const QString &identifier )
00302 {
00303   KConfig *cfg = KGlobal::config();
00304   KConfigGroupSaver( cfg, "KABCFields" );
00305  
00306   return restoreFields( cfg, identifier );
00307 }
00308 
00309 Field::List Field::restoreFields( KConfig *cfg, const QString &identifier )
00310 {
00311   QValueList<int> fieldIds = cfg->readIntListEntry( identifier );
00312 
00313   Field::List fields;
00314 
00315   int custom = 0;
00316   QValueList<int>::ConstIterator it;
00317   for( it = fieldIds.begin(); it != fieldIds.end(); ++it ) {
00318     FieldImpl *f = 0;
00319     if ( (*it) == FieldImpl::CustomField ) {
00320       QStringList customEntry = cfg->readListEntry( "KABC_CustomEntry_" +
00321                                                  identifier + "_" +
00322                                                  QString::number( custom++ ) );
00323       f = new FieldImpl( *it, CustomCategory, customEntry[ 0 ],
00324                          customEntry[ 1 ], customEntry[ 2 ] );
00325     } else {
00326       f = new FieldImpl( *it );
00327     }
00328     fields.append( new Field( f ) );
00329   }
00330   
00331   return fields;
00332 }
00333 
00334 bool Field::equals( Field *field )
00335 {
00336   bool sameId = ( mImpl->fieldId() == field->mImpl->fieldId() );
00337 
00338   if ( !sameId ) return false;
00339 
00340   if ( mImpl->fieldId() != FieldImpl::CustomField ) return true;
00341   
00342   return mImpl->key() == field->mImpl->key();
00343 }
00344 
00345 Field *Field::createCustomField( const QString &label, int category,
00346                                  const QString &key, const QString &app )
00347 {
00348   Field *field = new Field( new FieldImpl( FieldImpl::CustomField,
00349                                            category | CustomCategory,
00350                                            label, key, app ) );
00351   mCustomFields.append( field );
00352 
00353   return field;
00354 }
KDE Logo
This file is part of the documentation for kabc Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Apr 22 14:25:35 2004 by doxygen 1.2.18 written by Dimitri van Heesch, © 1997-2003