You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
513 lines
14 KiB
513 lines
14 KiB
15 years ago
|
/*
|
||
|
This file is part of libkabc.
|
||
|
Copyright (c) 2002 Cornelius Schumacher <schumacher@kde.org>
|
||
|
|
||
|
This library is free software; you can redistribute it and/or
|
||
|
modify it under the terms of the GNU Library General Public
|
||
|
License as published by the Free Software Foundation; either
|
||
|
version 2 of the License, or (at your option) any later version.
|
||
|
|
||
|
This library is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
|
Library General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU Library General Public License
|
||
|
along with this library; see the file COPYING.LIB. If not, write to
|
||
|
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||
|
Boston, MA 02110-1301, USA.
|
||
|
*/
|
||
|
|
||
12 years ago
|
#include <tdelocale.h>
|
||
12 years ago
|
#include <tdeconfig.h>
|
||
12 years ago
|
#include <tdeglobal.h>
|
||
15 years ago
|
|
||
|
#include "field.h"
|
||
|
#include "address.h"
|
||
|
|
||
|
using namespace KABC;
|
||
|
|
||
|
class Field::FieldImpl
|
||
|
{
|
||
|
public:
|
||
|
FieldImpl( int fieldId, int category = 0,
|
||
14 years ago
|
const TQString &label = TQString::null,
|
||
|
const TQString &key = TQString::null,
|
||
|
const TQString &app = TQString::null )
|
||
15 years ago
|
: mFieldId( fieldId ), mCategory( category ), mLabel( label ),
|
||
|
mKey( key ), mApp( app ) {}
|
||
14 years ago
|
|
||
15 years ago
|
enum FieldId
|
||
|
{
|
||
|
CustomField,
|
||
|
--ENUMS--
|
||
|
};
|
||
|
|
||
|
int fieldId() { return mFieldId; }
|
||
|
int category() { return mCategory; }
|
||
14 years ago
|
|
||
14 years ago
|
TQString label() { return mLabel; }
|
||
|
TQString key() { return mKey; }
|
||
|
TQString app() { return mApp; }
|
||
14 years ago
|
|
||
15 years ago
|
private:
|
||
|
int mFieldId;
|
||
|
int mCategory;
|
||
|
|
||
14 years ago
|
TQString mLabel;
|
||
|
TQString mKey;
|
||
|
TQString mApp;
|
||
15 years ago
|
};
|
||
|
|
||
|
|
||
|
Field::List Field::mAllFields;
|
||
|
Field::List Field::mDefaultFields;
|
||
|
Field::List Field::mCustomFields;
|
||
|
|
||
|
|
||
|
Field::Field( FieldImpl *impl )
|
||
|
{
|
||
|
mImpl = impl;
|
||
|
}
|
||
|
|
||
|
Field::~Field()
|
||
|
{
|
||
|
delete mImpl;
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString Field::label()
|
||
15 years ago
|
{
|
||
|
switch ( mImpl->fieldId() ) {
|
||
|
--CASELABEL--
|
||
|
case FieldImpl::CustomField:
|
||
|
return mImpl->label();
|
||
|
default:
|
||
|
return i18n("Unknown Field");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int Field::category()
|
||
|
{
|
||
|
return mImpl->category();
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString Field::categoryLabel( int category )
|
||
15 years ago
|
{
|
||
|
switch ( category ) {
|
||
|
case All:
|
||
|
return i18n("All");
|
||
|
case Frequent:
|
||
|
return i18n("Frequent");
|
||
|
case Address:
|
||
|
return i18n("street/postal","Address");
|
||
|
case Email:
|
||
|
return i18n("Email");
|
||
|
case Personal:
|
||
|
return i18n("Personal");
|
||
|
case Organization:
|
||
|
return i18n("Organization");
|
||
|
case CustomCategory:
|
||
|
return i18n("Custom");
|
||
|
default:
|
||
|
return i18n("Undefined");
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString Field::value( const KABC::Addressee &a )
|
||
15 years ago
|
{
|
||
|
switch ( mImpl->fieldId() ) {
|
||
|
--CASEVALUE--
|
||
|
case FieldImpl::Email:
|
||
|
return a.preferredEmail();
|
||
|
case FieldImpl::Birthday:
|
||
|
if ( a.birthday().isValid() )
|
||
|
return a.birthday().date().toString( Qt::ISODate );
|
||
|
else
|
||
14 years ago
|
return TQString::null;
|
||
15 years ago
|
case FieldImpl::Url:
|
||
|
return a.url().prettyURL();
|
||
|
case FieldImpl::HomePhone:
|
||
|
{
|
||
|
PhoneNumber::List::ConstIterator it;
|
||
|
|
||
|
{
|
||
|
// check for preferred number
|
||
|
const PhoneNumber::List list = a.phoneNumbers( PhoneNumber::Home | PhoneNumber::Pref );
|
||
|
for ( it = list.begin(); it != list.end(); ++it )
|
||
|
if ( ((*it).type() & ~(PhoneNumber::Pref)) == PhoneNumber::Home )
|
||
|
return (*it).number();
|
||
|
}
|
||
|
|
||
|
{
|
||
|
// check for normal home number
|
||
|
const PhoneNumber::List list = a.phoneNumbers( PhoneNumber::Home );
|
||
|
for ( it = list.begin(); it != list.end(); ++it )
|
||
|
if ( ((*it).type() & ~(PhoneNumber::Pref)) == PhoneNumber::Home )
|
||
|
return (*it).number();
|
||
|
}
|
||
|
|
||
14 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
case FieldImpl::BusinessPhone:
|
||
|
{
|
||
|
PhoneNumber::List::ConstIterator it;
|
||
|
|
||
|
{
|
||
|
// check for preferred number
|
||
|
const PhoneNumber::List list = a.phoneNumbers( PhoneNumber::Work | PhoneNumber::Pref );
|
||
|
for ( it = list.begin(); it != list.end(); ++it )
|
||
|
if ( ((*it).type() & ~(PhoneNumber::Pref)) == PhoneNumber::Work )
|
||
|
return (*it).number();
|
||
|
}
|
||
|
|
||
|
{
|
||
|
// check for normal work number
|
||
|
const PhoneNumber::List list = a.phoneNumbers( PhoneNumber::Work );
|
||
|
for ( it = list.begin(); it != list.end(); ++it )
|
||
|
if ( ((*it).type() & ~(PhoneNumber::Pref)) == PhoneNumber::Work )
|
||
|
return (*it).number();
|
||
|
}
|
||
|
|
||
14 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
case FieldImpl::MobilePhone:
|
||
|
return a.phoneNumber( PhoneNumber::Cell ).number();
|
||
|
case FieldImpl::HomeFax:
|
||
|
return a.phoneNumber( PhoneNumber::Home | PhoneNumber::Fax ).number();
|
||
|
case FieldImpl::BusinessFax:
|
||
|
return a.phoneNumber( PhoneNumber::Work | PhoneNumber::Fax ).number();
|
||
|
case FieldImpl::CarPhone:
|
||
|
return a.phoneNumber( PhoneNumber::Car ).number();
|
||
|
case FieldImpl::Isdn:
|
||
|
return a.phoneNumber( PhoneNumber::Isdn ).number();
|
||
|
case FieldImpl::Pager:
|
||
|
return a.phoneNumber( PhoneNumber::Pager ).number();
|
||
|
case FieldImpl::HomeAddressStreet:
|
||
|
return a.address( Address::Home ).street();
|
||
|
case FieldImpl::HomeAddressLocality:
|
||
|
return a.address( Address::Home ).locality();
|
||
|
case FieldImpl::HomeAddressRegion:
|
||
|
return a.address( Address::Home ).region();
|
||
|
case FieldImpl::HomeAddressPostalCode:
|
||
|
return a.address( Address::Home ).postalCode();
|
||
|
case FieldImpl::HomeAddressCountry:
|
||
|
return a.address( Address::Home ).country();
|
||
|
case FieldImpl::HomeAddressLabel:
|
||
|
return a.address( Address::Home ).label();
|
||
|
case FieldImpl::BusinessAddressStreet:
|
||
|
return a.address( Address::Work ).street();
|
||
|
case FieldImpl::BusinessAddressLocality:
|
||
|
return a.address( Address::Work ).locality();
|
||
|
case FieldImpl::BusinessAddressRegion:
|
||
|
return a.address( Address::Work ).region();
|
||
|
case FieldImpl::BusinessAddressPostalCode:
|
||
|
return a.address( Address::Work ).postalCode();
|
||
|
case FieldImpl::BusinessAddressCountry:
|
||
|
return a.address( Address::Work ).country();
|
||
|
case FieldImpl::BusinessAddressLabel:
|
||
|
return a.address( Address::Work ).label();
|
||
|
case FieldImpl::CustomField:
|
||
|
return a.custom( mImpl->app(), mImpl->key() );
|
||
|
default:
|
||
14 years ago
|
return TQString::null;
|
||
15 years ago
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
bool Field::setValue( KABC::Addressee &a, const TQString &value )
|
||
15 years ago
|
{
|
||
|
switch ( mImpl->fieldId() ) {
|
||
|
--CASESETVALUE--
|
||
|
case FieldImpl::MobilePhone:
|
||
|
{
|
||
|
PhoneNumber number = a.phoneNumber( PhoneNumber::Cell );
|
||
|
number.setNumber( value );
|
||
|
a.insertPhoneNumber( number );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::HomeFax:
|
||
|
{
|
||
|
PhoneNumber number = a.phoneNumber( PhoneNumber::Home | PhoneNumber::Fax );
|
||
|
number.setNumber( value );
|
||
|
a.insertPhoneNumber( number );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::BusinessFax:
|
||
|
{
|
||
|
PhoneNumber number = a.phoneNumber( PhoneNumber::Work | PhoneNumber::Fax );
|
||
|
number.setNumber( value );
|
||
|
a.insertPhoneNumber( number );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::CarPhone:
|
||
|
{
|
||
|
PhoneNumber number = a.phoneNumber( PhoneNumber::Car );
|
||
|
number.setNumber( value );
|
||
|
a.insertPhoneNumber( number );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::Isdn:
|
||
|
{
|
||
|
PhoneNumber number = a.phoneNumber( PhoneNumber::Isdn );
|
||
|
number.setNumber( value );
|
||
|
a.insertPhoneNumber( number );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::Pager:
|
||
|
{
|
||
|
PhoneNumber number = a.phoneNumber( PhoneNumber::Pager );
|
||
|
number.setNumber( value );
|
||
|
a.insertPhoneNumber( number );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::HomeAddressStreet:
|
||
|
{
|
||
|
KABC::Address address = a.address( Address::Home );
|
||
|
address.setStreet( value );
|
||
|
a.insertAddress( address );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::HomeAddressLocality:
|
||
|
{
|
||
|
KABC::Address address = a.address( Address::Home );
|
||
|
address.setLocality( value );
|
||
|
a.insertAddress( address );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::HomeAddressRegion:
|
||
|
{
|
||
|
KABC::Address address = a.address( Address::Home );
|
||
|
address.setRegion( value );
|
||
|
a.insertAddress( address );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::HomeAddressPostalCode:
|
||
|
{
|
||
|
KABC::Address address = a.address( Address::Home );
|
||
|
address.setPostalCode( value );
|
||
|
a.insertAddress( address );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::HomeAddressCountry:
|
||
|
{
|
||
|
KABC::Address address = a.address( Address::Home );
|
||
|
address.setCountry( value );
|
||
|
a.insertAddress( address );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::HomeAddressLabel:
|
||
|
{
|
||
|
KABC::Address address = a.address( Address::Home );
|
||
|
address.setLabel( value );
|
||
|
a.insertAddress( address );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::BusinessAddressStreet:
|
||
|
{
|
||
|
KABC::Address address = a.address( Address::Work );
|
||
|
address.setStreet( value );
|
||
|
a.insertAddress( address );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::BusinessAddressLocality:
|
||
|
{
|
||
|
KABC::Address address = a.address( Address::Work );
|
||
|
address.setLocality( value );
|
||
|
a.insertAddress( address );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::BusinessAddressRegion:
|
||
|
{
|
||
|
KABC::Address address = a.address( Address::Work );
|
||
|
address.setRegion( value );
|
||
|
a.insertAddress( address );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::BusinessAddressPostalCode:
|
||
|
{
|
||
|
KABC::Address address = a.address( Address::Work );
|
||
|
address.setPostalCode( value );
|
||
|
a.insertAddress( address );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::BusinessAddressCountry:
|
||
|
{
|
||
|
KABC::Address address = a.address( Address::Work );
|
||
|
address.setCountry( value );
|
||
|
a.insertAddress( address );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::BusinessAddressLabel:
|
||
|
{
|
||
|
KABC::Address address = a.address( Address::Work );
|
||
|
address.setLabel( value );
|
||
|
a.insertAddress( address );
|
||
|
return true;
|
||
|
}
|
||
|
case FieldImpl::Birthday:
|
||
14 years ago
|
a.setBirthday( TQT_TQDATE_OBJECT(TQDate::fromString( value, Qt::ISODate )) );
|
||
15 years ago
|
return true;
|
||
|
case FieldImpl::CustomField:
|
||
|
a.insertCustom( mImpl->app(), mImpl->key(), value );
|
||
|
return true;
|
||
|
default:
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
TQString Field::sortKey( const KABC::Addressee &a )
|
||
15 years ago
|
{
|
||
|
switch ( mImpl->fieldId() ) {
|
||
|
--CASEVALUE--
|
||
|
case FieldImpl::Birthday:
|
||
|
if ( a.birthday().isValid() ) {
|
||
14 years ago
|
TQDate date = TQT_TQDATE_OBJECT(a.birthday().date());
|
||
14 years ago
|
TQString key;
|
||
15 years ago
|
key.sprintf( "%02d-%02d", date.month(), date.day() );
|
||
|
return key;
|
||
|
} else
|
||
14 years ago
|
return TQString( "00-00" );
|
||
15 years ago
|
default:
|
||
|
return value( a ).lower();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
bool Field::isCustom()
|
||
|
{
|
||
|
return mImpl->fieldId() == FieldImpl::CustomField;
|
||
|
}
|
||
|
|
||
|
Field::List Field::allFields()
|
||
|
{
|
||
|
if ( mAllFields.isEmpty() ) {
|
||
|
--CREATEFIELDS--
|
||
|
}
|
||
|
|
||
|
return mAllFields;
|
||
|
}
|
||
|
|
||
|
Field::List Field::defaultFields()
|
||
|
{
|
||
|
if ( mDefaultFields.isEmpty() ) {
|
||
|
createDefaultField( FieldImpl::FormattedName );
|
||
|
createDefaultField( FieldImpl::Email );
|
||
|
}
|
||
|
|
||
|
return mDefaultFields;
|
||
|
}
|
||
|
|
||
|
void Field::createField( int id, int category )
|
||
|
{
|
||
|
mAllFields.append( new Field( new FieldImpl( id, category ) ) );
|
||
|
}
|
||
|
|
||
|
void Field::createDefaultField( int id, int category )
|
||
|
{
|
||
|
mDefaultFields.append( new Field( new FieldImpl( id, category ) ) );
|
||
|
}
|
||
|
|
||
|
void Field::deleteFields()
|
||
|
{
|
||
|
Field::List::ConstIterator it;
|
||
|
|
||
|
for ( it = mAllFields.constBegin(); it != mAllFields.constEnd(); ++it ) {
|
||
|
delete (*it);
|
||
|
}
|
||
|
mAllFields.clear();
|
||
|
|
||
|
for ( it = mDefaultFields.constBegin(); it != mDefaultFields.constEnd(); ++it ) {
|
||
|
delete (*it);
|
||
|
}
|
||
|
mDefaultFields.clear();
|
||
|
|
||
|
for ( it = mCustomFields.constBegin(); it != mCustomFields.constEnd(); ++it ) {
|
||
|
delete (*it);
|
||
|
}
|
||
|
mCustomFields.clear();
|
||
|
}
|
||
|
|
||
14 years ago
|
void Field::saveFields( const TQString &identifier,
|
||
15 years ago
|
const Field::List &fields )
|
||
|
{
|
||
12 years ago
|
TDEConfig *cfg = TDEGlobal::config();
|
||
|
TDEConfigGroupSaver( cfg, "KABCFields" );
|
||
15 years ago
|
|
||
|
saveFields( cfg, identifier, fields );
|
||
|
}
|
||
|
|
||
12 years ago
|
void Field::saveFields( TDEConfig *cfg, const TQString &identifier,
|
||
15 years ago
|
const Field::List &fields )
|
||
|
{
|
||
14 years ago
|
TQValueList<int> fieldIds;
|
||
14 years ago
|
|
||
15 years ago
|
int custom = 0;
|
||
|
Field::List::ConstIterator it;
|
||
|
for( it = fields.begin(); it != fields.end(); ++it ) {
|
||
|
fieldIds.append( (*it)->mImpl->fieldId() );
|
||
|
if( (*it)->isCustom() ) {
|
||
14 years ago
|
TQStringList customEntry;
|
||
15 years ago
|
customEntry << (*it)->mImpl->label();
|
||
|
customEntry << (*it)->mImpl->key();
|
||
|
customEntry << (*it)->mImpl->app();
|
||
|
cfg->writeEntry( "KABC_CustomEntry_" + identifier + "_" +
|
||
14 years ago
|
TQString::number( custom++ ), customEntry );
|
||
15 years ago
|
}
|
||
|
}
|
||
14 years ago
|
|
||
15 years ago
|
cfg->writeEntry( identifier, fieldIds );
|
||
|
}
|
||
|
|
||
14 years ago
|
Field::List Field::restoreFields( const TQString &identifier )
|
||
15 years ago
|
{
|
||
12 years ago
|
TDEConfig *cfg = TDEGlobal::config();
|
||
|
TDEConfigGroupSaver( cfg, "KABCFields" );
|
||
14 years ago
|
|
||
15 years ago
|
return restoreFields( cfg, identifier );
|
||
|
}
|
||
|
|
||
12 years ago
|
Field::List Field::restoreFields( TDEConfig *cfg, const TQString &identifier )
|
||
15 years ago
|
{
|
||
14 years ago
|
const TQValueList<int> fieldIds = cfg->readIntListEntry( identifier );
|
||
15 years ago
|
|
||
|
Field::List fields;
|
||
|
|
||
|
int custom = 0;
|
||
14 years ago
|
TQValueList<int>::ConstIterator it;
|
||
15 years ago
|
for( it = fieldIds.begin(); it != fieldIds.end(); ++it ) {
|
||
|
FieldImpl *f = 0;
|
||
|
if ( (*it) == FieldImpl::CustomField ) {
|
||
14 years ago
|
TQStringList customEntry = cfg->readListEntry( "KABC_CustomEntry_" +
|
||
15 years ago
|
identifier + "_" +
|
||
14 years ago
|
TQString::number( custom++ ) );
|
||
15 years ago
|
f = new FieldImpl( *it, CustomCategory, customEntry[ 0 ],
|
||
|
customEntry[ 1 ], customEntry[ 2 ] );
|
||
|
} else {
|
||
|
f = new FieldImpl( *it );
|
||
|
}
|
||
|
fields.append( new Field( f ) );
|
||
|
}
|
||
14 years ago
|
|
||
15 years ago
|
return fields;
|
||
|
}
|
||
|
|
||
|
bool Field::equals( Field *field )
|
||
|
{
|
||
|
bool sameId = ( mImpl->fieldId() == field->mImpl->fieldId() );
|
||
|
|
||
|
if ( !sameId ) return false;
|
||
|
|
||
|
if ( mImpl->fieldId() != FieldImpl::CustomField ) return true;
|
||
14 years ago
|
|
||
15 years ago
|
return mImpl->key() == field->mImpl->key();
|
||
|
}
|
||
|
|
||
14 years ago
|
Field *Field::createCustomField( const TQString &label, int category,
|
||
|
const TQString &key, const TQString &app )
|
||
15 years ago
|
{
|
||
|
Field *field = new Field( new FieldImpl( FieldImpl::CustomField,
|
||
|
category | CustomCategory,
|
||
|
label, key, app ) );
|
||
|
mCustomFields.append( field );
|
||
|
|
||
|
return field;
|
||
|
}
|