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 QString Field::sortKey( const KABC::Addressee &a )
00185 {
00186   switch ( mImpl->fieldId() ) {
00187     --CASEVALUE--
00188     case FieldImpl::Birthday:
00189       if ( a.birthday().isValid() ) {
00190         QDate date = a.birthday().date();
00191         QString key;
00192         key.sprintf( "%02d-%02d", date.month(), date.day() );
00193         return key;
00194       } else
00195         return QString( "00-00" );
00196     default:
00197       return value( a ).lower();
00198   }
00199 }
00200 
00201 bool Field::isCustom()
00202 {
00203   return mImpl->fieldId() == FieldImpl::CustomField;
00204 }
00205 
00206 Field::List Field::allFields()
00207 {
00208   if ( mAllFields.isEmpty() ) {
00209     --CREATEFIELDS--
00210   }
00211 
00212   return mAllFields;
00213 }
00214 
00215 Field::List Field::defaultFields()
00216 {
00217   if ( mDefaultFields.isEmpty() ) {
00218     createDefaultField( FieldImpl::GivenName );
00219     createDefaultField( FieldImpl::FamilyName );
00220     createDefaultField( FieldImpl::Email );
00221   }
00222 
00223   return mDefaultFields;
00224 }
00225 
00226 void Field::createField( int id, int category )
00227 {
00228   mAllFields.append( new Field( new FieldImpl( id, category ) ) );
00229 }
00230 
00231 void Field::createDefaultField( int id, int category )
00232 {
00233   mDefaultFields.append( new Field( new FieldImpl( id, category ) ) );
00234 }
00235 
00236 void Field::deleteFields()
00237 {
00238   Field::List::ConstIterator it;
00239 
00240   for( it = mAllFields.begin(); it != mAllFields.end(); ++it ) {
00241     delete (*it);
00242   }
00243   mAllFields.clear();
00244 
00245   for( it = mDefaultFields.begin(); it != mDefaultFields.end(); ++it ) {
00246     delete (*it);
00247   }
00248   mDefaultFields.clear();
00249 
00250   for( it = mCustomFields.begin(); it != mCustomFields.end(); ++it ) {
00251     delete (*it);
00252   }
00253   mCustomFields.clear();
00254 }
00255 
00256 void Field::saveFields( const QString &identifier,
00257                         const Field::List &fields )
00258 {
00259   KConfig *cfg = KGlobal::config();
00260   KConfigGroupSaver( cfg, "KABCFields" );
00261 
00262   saveFields( cfg, identifier, fields );
00263 }
00264 
00265 void Field::saveFields( KConfig *cfg, const QString &identifier,
00266                         const Field::List &fields )
00267 {
00268   QValueList<int> fieldIds;
00269   
00270   int custom = 0;
00271   Field::List::ConstIterator it;
00272   for( it = fields.begin(); it != fields.end(); ++it ) {
00273     fieldIds.append( (*it)->mImpl->fieldId() );
00274     if( (*it)->isCustom() ) {
00275       QStringList customEntry;
00276       customEntry << (*it)->mImpl->label();
00277       customEntry << (*it)->mImpl->key();
00278       customEntry << (*it)->mImpl->app();
00279       cfg->writeEntry( "KABC_CustomEntry_" + identifier + "_" +
00280                        QString::number( custom++ ), customEntry );
00281     }
00282   }
00283   
00284   cfg->writeEntry( identifier, fieldIds );
00285 }
00286 
00287 Field::List Field::restoreFields( const QString &identifier )
00288 {
00289   KConfig *cfg = KGlobal::config();
00290   KConfigGroupSaver( cfg, "KABCFields" );
00291  
00292   return restoreFields( cfg, identifier );
00293 }
00294 
00295 Field::List Field::restoreFields( KConfig *cfg, const QString &identifier )
00296 {
00297   QValueList<int> fieldIds = cfg->readIntListEntry( identifier );
00298 
00299   Field::List fields;
00300 
00301   int custom = 0;
00302   QValueList<int>::ConstIterator it;
00303   for( it = fieldIds.begin(); it != fieldIds.end(); ++it ) {
00304     FieldImpl *f = 0;
00305     if ( (*it) == FieldImpl::CustomField ) {
00306       QStringList customEntry = cfg->readListEntry( "KABC_CustomEntry_" +
00307                                                  identifier + "_" +
00308                                                  QString::number( custom++ ) );
00309       f = new FieldImpl( *it, CustomCategory, customEntry[ 0 ],
00310                          customEntry[ 1 ], customEntry[ 2 ] );
00311     } else {
00312       f = new FieldImpl( *it );
00313     }
00314     fields.append( new Field( f ) );
00315   }
00316   
00317   return fields;
00318 }
00319 
00320 bool Field::equals( Field *field )
00321 {
00322   bool sameId = ( mImpl->fieldId() == field->mImpl->fieldId() );
00323 
00324   if ( !sameId ) return false;
00325 
00326   if ( mImpl->fieldId() != FieldImpl::CustomField ) return true;
00327   
00328   return mImpl->key() == field->mImpl->key();
00329 }
00330 
00331 Field *Field::createCustomField( const QString &label, int category,
00332                                  const QString &key, const QString &app )
00333 {
00334   Field *field = new Field( new FieldImpl( FieldImpl::CustomField,
00335                                            category | CustomCategory,
00336                                            label, key, app ) );
00337   mCustomFields.append( field );
00338 
00339   return field;
00340 }
KDE Logo
This file is part of the documentation for kabc Library Version 3.2.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu Mar 4 22:45:21 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003