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