diff --git a/akonadi/contact/editor/phoneeditwidget.cpp b/akonadi/contact/editor/phoneeditwidget.cpp index c57a1a8dd..d1fb00303 100644 --- a/akonadi/contact/editor/phoneeditwidget.cpp +++ b/akonadi/contact/editor/phoneeditwidget.cpp @@ -1,398 +1,398 @@ /* This file is part of Akonadi Contact. Copyright (c) 2009 Tobias Koenig 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. */ #include "phoneeditwidget.h" #include "autoqpointer_p.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include PhoneTypeCombo::PhoneTypeCombo( QWidget *parent ) : KComboBox( parent ), mType( KABC::PhoneNumber::Home ), mLastSelected( 0 ) { for ( int i = 0; i < KABC::PhoneNumber::typeList().count(); ++i ) mTypeList.append( KABC::PhoneNumber::typeList().at( i ) ); mTypeList.append( -1 ); // Others... update(); connect( this, SIGNAL( activated( int ) ), this, SLOT( selected( int ) ) ); } PhoneTypeCombo::~PhoneTypeCombo() { } void PhoneTypeCombo::setType( KABC::PhoneNumber::Type type ) { if ( !mTypeList.contains( type ) ) mTypeList.insert( mTypeList.at( mTypeList.count() - 1 ), type ); mType = type; update(); } KABC::PhoneNumber::Type PhoneTypeCombo::type() const { return mType; } void PhoneTypeCombo::update() { clear(); for ( int i = 0; i < mTypeList.count(); ++i ) { if ( mTypeList.at( i ) == -1 ) // "Other..." entry addItem( i18nc( "@item:inlistbox Category of contact info field", "Other..." ) ); else - addItem( KABC::PhoneNumber::fullTypeLabel( KABC::PhoneNumber::Type( mTypeList.at( i ) ) ) ); + addItem( KABC::PhoneNumber::typeLabel( KABC::PhoneNumber::Type( mTypeList.at( i ) ) ) ); } setCurrentIndex( mLastSelected = mTypeList.indexOf( mType ) ); } void PhoneTypeCombo::selected( int pos ) { if ( mTypeList.at( pos ) == -1 ) otherSelected(); else { mType = KABC::PhoneNumber::Type( mTypeList.at( pos ) ); mLastSelected = pos; } } void PhoneTypeCombo::otherSelected() { AutoQPointer dlg = new PhoneTypeDialog( mType, this ); if ( dlg->exec() ) { mType = dlg->type(); if ( !mTypeList.contains( mType ) ) mTypeList.insert( mTypeList.at( mTypeList.count() - 1 ), mType ); } else { setType( KABC::PhoneNumber::Type( mTypeList.at( mLastSelected ) ) ); } update(); } PhoneNumberWidget::PhoneNumberWidget( QWidget *parent ) : QWidget( parent ) { QHBoxLayout *layout = new QHBoxLayout( this ); layout->setSpacing( 11 ); layout->setMargin( 0 ); mTypeCombo = new PhoneTypeCombo( this ); mNumberEdit = new KLineEdit( this ); layout->addWidget( mTypeCombo ); layout->addWidget( mNumberEdit ); connect( mTypeCombo, SIGNAL( activated( int ) ), SIGNAL( modified() ) ); connect( mNumberEdit, SIGNAL( textChanged( const QString& ) ), SIGNAL( modified() ) ); } void PhoneNumberWidget::setNumber( const KABC::PhoneNumber &number ) { mNumber = number; disconnect( mTypeCombo, SIGNAL( activated( int ) ), this, SIGNAL( modified() ) ); mTypeCombo->setType( number.type() ); connect( mTypeCombo, SIGNAL( activated( int ) ), SIGNAL( modified() ) ); mNumberEdit->setText( number.number() ); } KABC::PhoneNumber PhoneNumberWidget::number() const { KABC::PhoneNumber number( mNumber ); number.setType( mTypeCombo->type() ); number.setNumber( mNumberEdit->text() ); return number; } void PhoneNumberWidget::setReadOnly( bool readOnly ) { mTypeCombo->setEnabled( !readOnly ); mNumberEdit->setReadOnly( readOnly ); } PhoneNumberListWidget::PhoneNumberListWidget( QWidget *parent ) : QWidget( parent ), mReadOnly( false ) { mWidgetLayout = new QVBoxLayout( this ); mMapper = new QSignalMapper( this ); connect( mMapper, SIGNAL( mapped( int ) ), SLOT( changed( int ) ) ); setPhoneNumbers( KABC::PhoneNumber::List() ); } PhoneNumberListWidget::~PhoneNumberListWidget() { } void PhoneNumberListWidget::setReadOnly( bool readOnly ) { mReadOnly = readOnly; foreach ( PhoneNumberWidget *const widget, mWidgets ) widget->setReadOnly( readOnly ); } int PhoneNumberListWidget::phoneNumberCount() const { return mPhoneNumberList.count(); } void PhoneNumberListWidget::setPhoneNumbers( const KABC::PhoneNumber::List &list ) { mPhoneNumberList = list; KABC::PhoneNumber::TypeList types; types << KABC::PhoneNumber::Home; types << KABC::PhoneNumber::Work; types << KABC::PhoneNumber::Cell; // add an empty entry per default if ( mPhoneNumberList.count() < 3 ) for ( int i = mPhoneNumberList.count(); i < 3; ++i ) mPhoneNumberList.append( KABC::PhoneNumber( QString(), types[ i ] ) ); recreateNumberWidgets(); } KABC::PhoneNumber::List PhoneNumberListWidget::phoneNumbers() const { KABC::PhoneNumber::List list; KABC::PhoneNumber::List::ConstIterator it; for ( it = mPhoneNumberList.constBegin(); it != mPhoneNumberList.constEnd(); ++it ) if ( !(*it).number().isEmpty() ) list.append( *it ); return list; } void PhoneNumberListWidget::add() { mPhoneNumberList.append( KABC::PhoneNumber() ); recreateNumberWidgets(); } void PhoneNumberListWidget::remove() { mPhoneNumberList.removeLast(); recreateNumberWidgets(); } void PhoneNumberListWidget::recreateNumberWidgets() { foreach ( QWidget *const widget, mWidgets ) { mWidgetLayout->removeWidget( widget ); delete widget; } mWidgets.clear(); KABC::PhoneNumber::List::ConstIterator it; int counter = 0; for ( it = mPhoneNumberList.constBegin(); it != mPhoneNumberList.constEnd(); ++it ) { PhoneNumberWidget *wdg = new PhoneNumberWidget( this ); wdg->setNumber( *it ); mMapper->setMapping( wdg, counter ); connect( wdg, SIGNAL( modified() ), mMapper, SLOT( map() ) ); mWidgetLayout->addWidget( wdg ); mWidgets.append( wdg ); wdg->show(); ++counter; } setReadOnly( mReadOnly ); } void PhoneNumberListWidget::changed( int pos ) { mPhoneNumberList[ pos ] = mWidgets.at( pos )->number(); } PhoneEditWidget::PhoneEditWidget( QWidget *parent ) : QWidget( parent ), mReadOnly( false ) { QGridLayout *layout = new QGridLayout( this ); layout->setSpacing( KDialog::spacingHint() ); mListScrollArea = new QScrollArea( this ); mPhoneNumberListWidget = new PhoneNumberListWidget; mListScrollArea->setWidget( mPhoneNumberListWidget ); mListScrollArea->setWidgetResizable( true ); // ugly but size policies seem to be messed up dialog (parent) wide const int scrollAreaMinHeight = mPhoneNumberListWidget->sizeHint().height() + mListScrollArea->horizontalScrollBar()->sizeHint().height(); mListScrollArea->setMinimumHeight( scrollAreaMinHeight ); layout->addWidget( mListScrollArea, 0, 0, 1, 2 ); mAddButton = new QPushButton( i18n( "Add" ), this ); mAddButton->setMaximumSize( mAddButton->sizeHint() ); layout->addWidget( mAddButton, 1, 0, Qt::AlignRight ); mRemoveButton = new QPushButton( i18n( "Remove" ), this ); mRemoveButton->setMaximumSize( mRemoveButton->sizeHint() ); layout->addWidget( mRemoveButton, 1, 1 ); connect( mAddButton, SIGNAL( clicked() ), mPhoneNumberListWidget, SLOT( add() ) ); connect( mRemoveButton, SIGNAL( clicked() ), mPhoneNumberListWidget, SLOT( remove() ) ); connect( mAddButton, SIGNAL( clicked() ), SLOT( changed() ) ); connect( mRemoveButton, SIGNAL( clicked() ), SLOT( changed() ) ); } PhoneEditWidget::~PhoneEditWidget() { } void PhoneEditWidget::setReadOnly( bool readOnly ) { mReadOnly = readOnly; mAddButton->setEnabled( !readOnly ); mRemoveButton->setEnabled( !readOnly && mPhoneNumberListWidget->phoneNumberCount() > 3 ); mPhoneNumberListWidget->setReadOnly( readOnly ); } void PhoneEditWidget::changed() { mRemoveButton->setEnabled( !mReadOnly && mPhoneNumberListWidget->phoneNumberCount() > 3 ); } void PhoneEditWidget::loadContact( const KABC::Addressee &contact ) { mPhoneNumberListWidget->setPhoneNumbers( contact.phoneNumbers() ); changed(); } void PhoneEditWidget::storeContact( KABC::Addressee &contact ) const { const KABC::PhoneNumber::List oldNumbers = contact.phoneNumbers(); for ( int i = 0; i < oldNumbers.count(); ++i ) contact.removePhoneNumber( oldNumbers.at( i ) ); const KABC::PhoneNumber::List newNumbers = mPhoneNumberListWidget->phoneNumbers(); for ( int i = 0; i < newNumbers.count(); ++i ) contact.insertPhoneNumber( newNumbers.at( i ) ); } /////////////////////////////////////////// // PhoneTypeDialog PhoneTypeDialog::PhoneTypeDialog( KABC::PhoneNumber::Type type, QWidget *parent ) : KDialog( parent), mType( type ) { setCaption( i18n( "Edit Phone Number" ) ); setButtons( Ok | Cancel ); setDefaultButton( Ok ); showButtonSeparator( true ); QWidget *page = new QWidget( this ); setMainWidget( page ); QVBoxLayout *layout = new QVBoxLayout( page ); layout->setSpacing( spacingHint() ); layout->setMargin( 0 ); mPreferredBox = new QCheckBox( i18n( "This is the preferred phone number" ), page ); layout->addWidget( mPreferredBox ); QGroupBox *box = new QGroupBox( i18n( "Types" ), page ); layout->addWidget( box ); QGridLayout *buttonLayout = new QGridLayout( box ); // fill widgets mTypeList = KABC::PhoneNumber::typeList(); mTypeList.removeAll( KABC::PhoneNumber::Pref ); KABC::PhoneNumber::TypeList::ConstIterator it; mGroup = new QButtonGroup( box ); mGroup->setExclusive( false ); int row, column, counter; row = column = counter = 0; for ( it = mTypeList.constBegin(); it != mTypeList.constEnd(); ++it, ++counter ) { QCheckBox *cb = new QCheckBox( KABC::PhoneNumber::typeLabel( *it ), box ); cb->setChecked( type & mTypeList[ counter ] ); buttonLayout->addWidget( cb, row, column ); mGroup->addButton( cb ); column++; if ( column == 5 ) { column = 0; ++row; } } mPreferredBox->setChecked( mType & KABC::PhoneNumber::Pref ); } KABC::PhoneNumber::Type PhoneTypeDialog::type() const { KABC::PhoneNumber::Type type = 0; for ( int i = 0; i < mGroup->buttons().count(); ++i ) { QCheckBox *box = dynamic_cast( mGroup->buttons().at( i ) ) ; if ( box && box->isChecked() ) type |= mTypeList[ i ]; } if ( mPreferredBox->isChecked() ) type = type | KABC::PhoneNumber::Pref; else type = type & ~KABC::PhoneNumber::Pref; return type; } #include "phoneeditwidget.moc" diff --git a/kabc/phonenumber.cpp b/kabc/phonenumber.cpp index 8778c26ef..fb89b0d40 100644 --- a/kabc/phonenumber.cpp +++ b/kabc/phonenumber.cpp @@ -1,270 +1,283 @@ /* This file is part of libkabc. Copyright (c) 2001 Cornelius Schumacher 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. */ #include "phonenumber.h" #include #include #include #include using namespace KABC; static QString cleanupNumber( const QString &input ) { return input.simplified(); } class PhoneNumber::Private : public QSharedData { public: Private( Type type ) : mId( KRandom::randomString( 8 ) ), mType( type ) { } Private( const Private &other ) : QSharedData( other ) { mId = other.mId; mType = other.mType; mNumber = other.mNumber; } QString mId; Type mType; QString mNumber; }; PhoneNumber::PhoneNumber() : d( new Private( Home ) ) { } PhoneNumber::PhoneNumber( const QString &number, Type type ) : d( new Private( type ) ) { d->mNumber = cleanupNumber( number ); } PhoneNumber::PhoneNumber( const PhoneNumber &other ) : d( other.d ) { } PhoneNumber::~PhoneNumber() { } bool PhoneNumber::operator==( const PhoneNumber &other ) const { if ( d->mId != other.d->mId ) { return false; } if ( d->mNumber != other.d->mNumber ) { return false; } if ( d->mType != other.d->mType ) { return false; } return true; } bool PhoneNumber::operator!=( const PhoneNumber &other ) const { return !( other == *this ); } PhoneNumber &PhoneNumber::operator=( const PhoneNumber &other ) { if ( this != &other ) { d = other.d; } return *this; } bool PhoneNumber::isEmpty() const { return d->mNumber.isEmpty(); } void PhoneNumber::setId( const QString &id ) { d->mId = id; } QString PhoneNumber::id() const { return d->mId; } void PhoneNumber::setNumber( const QString &number ) { d->mNumber = cleanupNumber( number ); } QString PhoneNumber::number() const { return d->mNumber; } void PhoneNumber::setType( Type type ) { d->mType = type; } PhoneNumber::Type PhoneNumber::type() const { return d->mType; } QString PhoneNumber::typeLabel() const { - return fullTypeLabel( type() ); + return typeLabel( type() ); } PhoneNumber::TypeList PhoneNumber::typeList() { static TypeList list; if ( list.isEmpty() ) { list << Home << Work << Msg << Pref << Voice << Fax << Cell << Video << Bbs << Modem << Car << Isdn << Pcs << Pager; } return list; } -QString PhoneNumber::typeLabel( Type type ) +QString PhoneNumber::typeFlagLabel( TypeFlag type ) { - if ( type & Pref ) { - return i18nc( "Preferred phone", "Preferred" ); - } - switch ( type ) { case Home: return i18nc( "Home phone", "Home" ); break; case Work: return i18nc( "Work phone", "Work" ); break; case Msg: return i18n( "Messenger" ); break; case Pref: - return i18n( "Preferred Number" ); + return i18nc( "Preferred phone", "Preferred" ); break; case Voice: return i18n( "Voice" ); break; case Fax: return i18n( "Fax" ); break; case Cell: return i18nc( "Mobile Phone", "Mobile" ); break; case Video: return i18nc( "Video phone", "Video" ); break; case Bbs: return i18n( "Mailbox" ); break; case Modem: return i18n( "Modem" ); break; case Car: return i18nc( "Car Phone", "Car" ); break; case Isdn: return i18n( "ISDN" ); break; case Pcs: return i18n( "PCS" ); break; case Pager: return i18n( "Pager" ); break; - case Home + Fax: - return i18n( "Home Fax" ); - break; - case Work + Fax: - return i18n( "Work Fax" ); - break; default: return i18nc( "another type of phone", "Other" ); } } -QString PhoneNumber::fullTypeLabel( Type type ) +QString PhoneNumber::typeLabel( Type type ) { QString label; bool first = true; + // special cases + // Pref stand alone -> Preferred Number + // Home+Fax or Work+Fax -> combine as initial string + if ( type == Pref ) { + return i18n( "Preferred Number" ); + } + + if ( type & Fax ) { + if ( type & Home ) { + label = i18n( "Home Fax" ); + first = false; + type &= ~Fax; + type &= ~Home; + } else if ( type & Work ) { + label = i18n( "Work Fax" ); + first = false; + type &= ~Fax; + type &= ~Work; + } + } + const TypeList list = typeList(); TypeList::ConstIterator it; for ( it = list.begin(); it != list.end(); ++it ) { - if ( ( type & (*it) ) && ( (*it) != Pref ) ) { + // these are actually flags + const TypeFlag flag = static_cast( static_cast( *it ) ); + if ( type & flag ) { if ( !first ) { label.append( QLatin1Char( '/' ) ); } - label.append( typeLabel( *it ) ); + label.append( typeFlagLabel( flag ) ); if ( first ) { first = false; } } } return label; } QString PhoneNumber::toString() const { QString str; str += QString::fromLatin1( "PhoneNumber {\n" ); str += QString::fromLatin1( " Id: %1\n" ).arg( d->mId ); str += QString::fromLatin1( " Type: %1\n" ).arg( typeLabel( d->mType ) ); str += QString::fromLatin1( " Number: %1\n" ).arg( d->mNumber ); str += QString::fromLatin1( "}\n" ); return str; } QDataStream &KABC::operator<<( QDataStream &s, const PhoneNumber &phone ) { return s << phone.d->mId << (uint)phone.d->mType << phone.d->mNumber; } QDataStream &KABC::operator>>( QDataStream &s, PhoneNumber &phone ) { uint type; s >> phone.d->mId >> type >> phone.d->mNumber; phone.d->mType = PhoneNumber::Type( type ); return s; } diff --git a/kabc/phonenumber.h b/kabc/phonenumber.h index fa0cca31f..f1b414e61 100644 --- a/kabc/phonenumber.h +++ b/kabc/phonenumber.h @@ -1,229 +1,229 @@ /* This file is part of libkabc. Copyright (c) 2001 Cornelius Schumacher 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. */ #ifndef KABC_PHONENUMBER_H #define KABC_PHONENUMBER_H #include "kabc_export.h" #include #include namespace KABC { /** * @short Phonenumber information. * * This class provides phone number information. A phone number is classified by * a type. The following types are available, it's possible to use multiple types * Types for a number by combining them through a logical or. */ class KABC_EXPORT PhoneNumber { friend KABC_EXPORT QDataStream &operator<<( QDataStream &, const PhoneNumber & ); friend KABC_EXPORT QDataStream &operator>>( QDataStream &, PhoneNumber & ); public: /** Phone number types. */ enum TypeFlag { Home = 1, /**< Home number */ Work = 2, /**< Office number */ Msg = 4, /**< Messaging */ Pref = 8, /**< Preferred number */ Voice = 16, /**< Voice */ Fax = 32, /**< Fax machine */ Cell = 64, /**< Cell phone */ Video = 128, /**< Video phone */ Bbs = 256, /**< Mailbox */ Modem = 512, /**< Modem */ Car = 1024, /**< Car phone */ Isdn = 2048, /**< ISDN connection */ Pcs = 4096, /**< Personal Communication Service*/ Pager = 8192 /**< Pager */ }; Q_DECLARE_FLAGS( Type, TypeFlag ) /** * List of phone number types. */ typedef QList TypeList; /** * List of phone numbers. */ typedef QList List; /** * Creates an empty phone number object. */ PhoneNumber(); /** * Creates a phone number object. * * @param number Number * @param type Type as defined in enum. Multiple types can be * specified by combining them by a logical or. */ PhoneNumber( const QString &number, Type type = Home ); //krazy:exclude=explicit /** * Copy constructor. * * Fast operation, PhoneNumber's data is implicitly shared. * * @param other The PhoneNumber object to copy from */ PhoneNumber( const PhoneNumber &other ); /** * Destroys the phone number. */ ~PhoneNumber(); /** * Equality operator. * * @return @c true if number, type and identifier are equal, * otherwise @c false */ bool operator==( const PhoneNumber & ) const; /** * Not-Equal operator. */ bool operator!=( const PhoneNumber & ) const; /** * Assignment operator. * * Fast operation, PhoneNumber's data is implicitly shared. * * @param other The PhoneNumber object to asssign to @c this */ PhoneNumber &operator=( const PhoneNumber &other ); /** * Returns true, if the phone number is empty. */ bool isEmpty() const; /** * Sets the unique @p identifier. */ void setId( const QString &identifier ); /** * Returns the unique identifier. */ QString id() const; /** * Sets the phone @p number. */ void setNumber( const QString &number ); /** * Returns the phone number. */ QString number() const; /** * Sets the @p type. * Multiple types can be specified by combining them by a logical or. * * @param type The #Type of the phone number */ void setType( Type type ); /** * Returns the type. Can be a multiple types combined by a logical or. * * @see #TypeFlag * @see typeLabel() */ Type type() const; /** * Returns a translated string of the address' type. */ QString typeLabel() const; /** * Returns a list of all available types */ static TypeList typeList(); /** * Returns the translated label for phone number @p type. * + * In opposite to typeFlagLabel( TypeFlag type ), it returns all types + * of the phone number concatenated by '/'. + * * @param type An OR'ed combination of #TypeFlag * * @see type() */ static QString typeLabel( Type type ); /** * Returns the translated label for phone number @p type. * - * In opposite to typeLabel( Type type ), it returns all types - * of the phone number concatenated by '/'. - * * @param type An OR'ed combination of #TypeFlag * - * @see type() + * @see typeLabel() * @since 4.5 */ - static QString fullTypeLabel( Type type ); + static QString typeFlagLabel( TypeFlag type ); /** * Returns a string representation of the phone number. */ QString toString() const; private: class Private; QSharedDataPointer d; }; Q_DECLARE_OPERATORS_FOR_FLAGS( PhoneNumber::Type ) /** * Serializes the phone @p number object into the @p stream. * * @param stream The stream to write into * @param number The phone number object to serialize */ KABC_EXPORT QDataStream &operator<<( QDataStream &stream, const PhoneNumber &number ); /** * Initializes the phone @p number object from the @p stream. * * @param stream The stream to read from * @param number The phone number object to deserialize into */ KABC_EXPORT QDataStream &operator>>( QDataStream &stream, const PhoneNumber &number ); } #endif diff --git a/kabc/tests/phonenumbertest.cpp b/kabc/tests/phonenumbertest.cpp index 8d5f8d3ef..b941b99bd 100644 --- a/kabc/tests/phonenumbertest.cpp +++ b/kabc/tests/phonenumbertest.cpp @@ -1,101 +1,174 @@ /* This file is part of libkabc. Copyright (c) 2007 Tobias Koenig 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. */ #include "phonenumbertest.h" #include "kabc/phonenumber.h" #include QTEST_KDEMAIN( PhoneNumberTest, NoGUI ) void PhoneNumberTest::emptyTest() { KABC::PhoneNumber number; QVERIFY( number.isEmpty() ); } void PhoneNumberTest::storeTest() { KABC::PhoneNumber number; number.setId( QLatin1String( "My Id" ) ); number.setType( KABC::PhoneNumber::Work | KABC::PhoneNumber::Cell ); number.setNumber( QLatin1String( "2734826345" ) ); QVERIFY( number.id() == QLatin1String( "My Id" ) ); QVERIFY( number.type() == ( KABC::PhoneNumber::Work | KABC::PhoneNumber::Cell ) ); QVERIFY( number.number() == QLatin1String( "2734826345" ) ); } void PhoneNumberTest::equalsTest() { KABC::PhoneNumber number1, number2; number1.setId( QLatin1String( "My Id" ) ); number1.setType( KABC::PhoneNumber::Work | KABC::PhoneNumber::Cell ); number1.setNumber( QLatin1String( "2734826345" ) ); number2.setId( QLatin1String( "My Id" ) ); number2.setType( KABC::PhoneNumber::Work | KABC::PhoneNumber::Cell ); number2.setNumber( QLatin1String( "2734826345" ) ); QVERIFY( number1 == number2 ); } void PhoneNumberTest::differsTest() { KABC::PhoneNumber number1( QLatin1String( "123" ), KABC::PhoneNumber::Home ); KABC::PhoneNumber number2( QLatin1String( "123" ), KABC::PhoneNumber::Work ); QVERIFY( number1 != number2 ); } void PhoneNumberTest::assignmentTest() { KABC::PhoneNumber number1, number2; number1.setId( QLatin1String( "My Id" ) ); number1.setType( KABC::PhoneNumber::Work | KABC::PhoneNumber::Cell ); number1.setNumber( QLatin1String( "2734826345" ) ); number1 = number2; QVERIFY( number1 == number2 ); } void PhoneNumberTest::serializeTest() { KABC::PhoneNumber number1, number2; number1.setId( QLatin1String( "My Id" ) ); number1.setType( KABC::PhoneNumber::Work | KABC::PhoneNumber::Cell ); number1.setNumber( QLatin1String( "2734826345" ) ); QByteArray data; QDataStream s( &data, QIODevice::WriteOnly ); s << number1; QDataStream t( &data, QIODevice::ReadOnly ); t >> number2; QVERIFY( number1 == number2 ); } +void PhoneNumberTest::labelTest() +{ + QMap labels; + + const KABC::PhoneNumber::TypeList types = KABC::PhoneNumber::typeList(); + + // check all types standalone + Q_FOREACH( KABC::PhoneNumber::Type type, types ) { + const KABC::PhoneNumber phone( QLatin1String( "1" ), type ); + QCOMPARE( phone.type(), type ); + + // Pref is special cased + if ( type != KABC::PhoneNumber::Pref ) { + QCOMPARE( phone.typeLabel(), KABC::PhoneNumber::typeFlagLabel( (KABC::PhoneNumber::TypeFlag)(int)type ) ); + labels.insert( type, phone.typeLabel() ); + } else { + labels.insert( type, KABC::PhoneNumber::typeFlagLabel( (KABC::PhoneNumber::TypeFlag)(int)type ) ); + } + QCOMPARE( KABC::PhoneNumber::typeLabel( type ), phone.typeLabel() ); + } + + // combine all with Pref + Q_FOREACH( KABC::PhoneNumber::Type type, types ) { + KABC::PhoneNumber::Type combinedType = type | KABC::PhoneNumber::Pref; + const KABC::PhoneNumber phone( QLatin1String( "1" ), combinedType ); + QCOMPARE( phone.type(), combinedType ); + QCOMPARE( KABC::PhoneNumber::typeLabel( combinedType ), phone.typeLabel() ); + + if ( type < KABC::PhoneNumber::Pref ) { + const QString expectedCombinedString = QString::fromLatin1( "%1/%2" ).arg( labels[ type ] ).arg( labels[ KABC::PhoneNumber::Pref ] ); + QCOMPARE( phone.typeLabel(), expectedCombinedString ); + } else if ( type > KABC::PhoneNumber::Pref ) { + const QString expectedCombinedString = QString::fromLatin1( "%1/%2" ).arg( labels[ KABC::PhoneNumber::Pref ] ).arg( labels[ type ] ); + QCOMPARE( phone.typeLabel(), expectedCombinedString ); + } + } + + // combine all with Fax + Q_FOREACH( KABC::PhoneNumber::Type type, types ) { + KABC::PhoneNumber::Type combinedType = type | KABC::PhoneNumber::Fax; + const KABC::PhoneNumber phone( QLatin1String( "1" ), combinedType ); + QCOMPARE( phone.type(), combinedType ); + QCOMPARE( KABC::PhoneNumber::typeLabel( combinedType ), phone.typeLabel() ); + + if ( type == KABC::PhoneNumber::Home || type == KABC::PhoneNumber::Work ) { + // special cased + } else if ( type < KABC::PhoneNumber::Fax ) { + const QString expectedCombinedString = QString::fromLatin1( "%1/%2" ).arg( labels[ type ] ).arg( labels[ KABC::PhoneNumber::Fax ] ); + QCOMPARE( phone.typeLabel(), expectedCombinedString ); + } else if ( type > KABC::PhoneNumber::Fax ) { + const QString expectedCombinedString = QString::fromLatin1( "%1/%2" ).arg( labels[ KABC::PhoneNumber::Fax ] ).arg( labels[ type ] ); + QCOMPARE( phone.typeLabel(), expectedCombinedString ); + } + } + + // special cases + QCOMPARE( KABC::PhoneNumber::typeLabel( KABC::PhoneNumber::Pref ), QLatin1String( "Preferred Number" ) ); + QCOMPARE( KABC::PhoneNumber::typeLabel( KABC::PhoneNumber::Home | + KABC::PhoneNumber::Fax ), + QLatin1String( "Home Fax" ) ); + QCOMPARE( KABC::PhoneNumber::typeLabel( KABC::PhoneNumber::Work | + KABC::PhoneNumber::Fax ), + QLatin1String( "Work Fax" ) ); + QCOMPARE( KABC::PhoneNumber::typeLabel( KABC::PhoneNumber::Home | + KABC::PhoneNumber::Fax | + KABC::PhoneNumber::Pref ), + QLatin1String( "Home Fax/Preferred" ) ); + QCOMPARE( KABC::PhoneNumber::typeLabel( KABC::PhoneNumber::Work | + KABC::PhoneNumber::Fax | + KABC::PhoneNumber::Pref ), + QLatin1String( "Work Fax/Preferred" ) ); +} + #include "phonenumbertest.moc" diff --git a/kabc/tests/phonenumbertest.h b/kabc/tests/phonenumbertest.h index 48b774efb..8d641713e 100644 --- a/kabc/tests/phonenumbertest.h +++ b/kabc/tests/phonenumbertest.h @@ -1,39 +1,40 @@ /* This file is part of libkabc. Copyright (c) 2007 Tobias Koenig 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. */ #ifndef PHONENUMBER_TEST_H #define PHONENUMBER_TEST_H #include class PhoneNumberTest : public QObject { Q_OBJECT private Q_SLOTS: void emptyTest(); void storeTest(); void equalsTest(); void differsTest(); void assignmentTest(); void serializeTest(); + void labelTest(); }; #endif