From kde-commits Sun Mar 03 19:05:23 2013 From: Tobias Koenig Date: Sun, 03 Mar 2013 19:05:23 +0000 To: kde-commits Subject: [kdepimlibs] kabc: Use QMap to store custom fields of an Addressee object Message-Id: <20130303190523.2DB13A604F () git ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=136233753217812 Git commit 4a1fb1330f144588dc596b883dbc54745432c858 by Tobias Koenig. Committed on 03/03/2013 at 20:03. Pushed by tokoe into branch 'master'. Use QMap to store custom fields of an Addressee object Extended unit test for custom fields. M +36 -39 kabc/addressee.cpp M +3 -0 kabc/addressee.h M +78 -1 kabc/tests/addresseetest.cpp M +1 -0 kabc/tests/addresseetest.h http://commits.kde.org/kdepimlibs/4a1fb1330f144588dc596b883dbc54745432c858 diff --git a/kabc/addressee.cpp b/kabc/addressee.cpp index 8b79325..c6c6515 100644 --- a/kabc/addressee.cpp +++ b/kabc/addressee.cpp @@ -98,7 +98,7 @@ class Addressee::Private : public QSharedData mKeys =3D other.mKeys; mEmails =3D other.mEmails; mCategories =3D other.mCategories; - mCustom =3D other.mCustom; + mCustomFields =3D other.mCustomFields; = #ifndef KDEPIM_NO_KRESOURCES mResource =3D other.mResource; @@ -143,7 +143,7 @@ class Addressee::Private : public QSharedData Key::List mKeys; QStringList mEmails; QStringList mCategories; - QStringList mCustom; + QMap mCustomFields; = #ifndef KDEPIM_NO_KRESOURCES Resource *mResource; @@ -348,7 +348,7 @@ bool Addressee::operator=3D=3D( const Addressee &addres= see ) const return false; } = - if ( !listEquals( d->mCustom, addressee.d->mCustom ) ) { + if ( d->mCustomFields !=3D addressee.d->mCustomFields ) { kDebug() << "custom differs"; return false; } @@ -1632,59 +1632,54 @@ void Addressee::insertCustom( const QString &app, c= onst QString &name, = d->mEmpty =3D false; = - QString qualifiedName =3D app + QLatin1Char( '-' ) + name + QLatin1Char(= ':' ); + const QString qualifiedName =3D app + QLatin1Char( '-' ) + name; = - QStringList::Iterator it; - QStringList::Iterator end( d->mCustom.end() ); - for ( it =3D d->mCustom.begin(); it !=3D end; ++it ) { - if ( ( *it ).startsWith( qualifiedName ) ) { - ( *it ) =3D qualifiedName + value; - return; - } - } - - d->mCustom.append( qualifiedName + value ); + d->mCustomFields.insert( qualifiedName, value ); } = void Addressee::removeCustom( const QString &app, const QString &name ) { - const QString qualifiedName =3D app + QLatin1Char( '-' ) + name + QLatin= 1Char( ':' ); + const QString qualifiedName =3D app + QLatin1Char( '-' ) + name; = - QStringList::Iterator it; - for ( it =3D d->mCustom.begin(); it !=3D d->mCustom.end(); ++it ) { - if ( ( *it ).startsWith( qualifiedName ) ) { - d->mCustom.erase( it ); - return; - } - } + d->mCustomFields.remove( qualifiedName ); } = QString Addressee::custom( const QString &app, const QString &name ) const { - QString qualifiedName =3D app + QLatin1Char( '-' ) + name + QLatin1Char(= ':' ); - QString value; - - QStringList::ConstIterator it; - QStringList::ConstIterator end( d->mCustom.constEnd() ); - for ( it =3D d->mCustom.constBegin(); it !=3D end; ++it ) { - if ( ( *it ).startsWith( qualifiedName ) ) { - value =3D ( *it ).mid( ( *it ).indexOf( QLatin1Char( ':' ) ) + 1 ); - break; - } - } + const QString qualifiedName =3D app + QLatin1Char( '-' ) + name; = - return value; + return d->mCustomFields.value( qualifiedName ); } = -void Addressee::setCustoms( const QStringList &l ) +void Addressee::setCustoms( const QStringList &customs ) { d->mEmpty =3D false; - d->mCustom =3D l; + + d->mCustomFields.clear(); + + foreach ( const QString &custom, customs ) { + const int index =3D custom.indexOf( QLatin1Char( ':' ) ); + if ( index =3D=3D -1 ) + continue; + + const QString qualifiedName =3D custom.left( index ); + const QString value =3D custom.mid( index + 1 ); + + d->mCustomFields.insert( qualifiedName, value ); + } } = QStringList Addressee::customs() const { - return d->mCustom; + QStringList result; + + QMapIterator it( d->mCustomFields ); + while ( it.hasNext() ) { + it.next(); + result << it.key() + QLatin1Char( ':' ) + it.value(); + } + + return result; } = void Addressee::parseEmailAddress( const QString &rawEmail, QString &fullN= ame, @@ -1919,7 +1914,7 @@ QDataStream &KABC::operator<<( QDataStream &s, const = Addressee &a ) s << a.d->mAddresses; s << a.d->mEmails; s << a.d->mCategories; - s << a.d->mCustom; + s << a.customs(); s << a.d->mKeys; return s; } @@ -1957,7 +1952,9 @@ QDataStream &KABC::operator>>( QDataStream &s, Addres= see &a ) s >> a.d->mAddresses; s >> a.d->mEmails; s >> a.d->mCategories; - s >> a.d->mCustom; + QStringList customFields; + s >> customFields; + a.setCustoms( customFields ); s >> a.d->mKeys; = a.d->mEmpty =3D false; diff --git a/kabc/addressee.h b/kabc/addressee.h index b184bc1..b81deba 100644 --- a/kabc/addressee.h +++ b/kabc/addressee.h @@ -905,6 +905,9 @@ class KABC_EXPORT Addressee = /** Return list of all custom entries. + + The format of the custom entries is 'app-key:value' and the list is = sorted + alphabetically by 'app-key'. */ QStringList customs() const; = diff --git a/kabc/tests/addresseetest.cpp b/kabc/tests/addresseetest.cpp index dc0a392..b40f508 100644 --- a/kabc/tests/addresseetest.cpp +++ b/kabc/tests/addresseetest.cpp @@ -302,7 +302,9 @@ void AddresseeTest::serializeTest() categories << QLatin1String( "Helper" ) << QLatin1String( "Friend" ); = QStringList customs; - customs << QLatin1String( "X-Danger: high" ); + customs << QLatin1String( "FirstApp-FirstKey:FirstValue" ) + << QLatin1String( "SecondApp-SecondKey:SecondValue" ) + << QLatin1String( "ThirdApp-ThirdKey:ThirdValue" ); = addressee1.setUid( QLatin1String( "My uid" ) ); addressee1.setName( QLatin1String( "John Sinclair" ) ); @@ -368,3 +370,78 @@ void AddresseeTest::nameFromStringTest() QCOMPARE( a.formattedName(), QLatin1String( "Firstname Lastname" ) ); } = +void AddresseeTest::customFieldsTest() +{ + KABC::Addressee a; + + // test for empty + QVERIFY( a.customs().isEmpty() ); + + // test insert + a.insertCustom( QLatin1String( "MyApp" ), QLatin1String( "MyKey" ), QLat= in1String( "MyValue" ) ); + QCOMPARE( a.customs().count(), 1 ); + QCOMPARE( a.custom( QLatin1String( "MyApp" ), QLatin1String( "MyKey" ) )= , QLatin1String( "MyValue" ) ); + + a.insertCustom( QLatin1String( "MyApp" ), QLatin1String( "MyKey" ), QLat= in1String( "YourValue" ) ); + QCOMPARE( a.customs().count(), 1 ); // still one, we overwrite... + QCOMPARE( a.custom( QLatin1String( "MyApp" ), QLatin1String( "MyKey" ) )= , QLatin1String( "YourValue" ) ); + + // test query non-existing app/key + QCOMPARE( a.custom( QLatin1String( "MyApp" ), QLatin1String( "UnknownKey= " ) ), QString() ); + QCOMPARE( a.custom( QLatin1String( "UnknownApp" ), QLatin1String( "MyKey= " ) ), QString() ); + + // test insert with different key + a.insertCustom( QLatin1String( "MyApp" ), QLatin1String( "AnotherKey" ),= QLatin1String( "OtherValue" ) ); + QCOMPARE( a.customs().count(), 2 ); + QCOMPARE( a.custom( QLatin1String( "MyApp" ), QLatin1String( "AnotherKey= " ) ), QLatin1String( "OtherValue" ) ); + QCOMPARE( a.custom( QLatin1String( "MyApp" ), QLatin1String( "MyKey" ) )= , QLatin1String( "YourValue" ) ); + + // test insert with different app + a.insertCustom( QLatin1String( "OtherApp" ), QLatin1String( "OtherKey" )= , QLatin1String( "OurValue" ) ); + QCOMPARE( a.customs().count(), 3 ); + QCOMPARE( a.custom( QLatin1String( "OtherApp" ), QLatin1String( "OtherKe= y" ) ), QLatin1String( "OurValue" ) ); + QCOMPARE( a.custom( QLatin1String( "MyApp" ), QLatin1String( "AnotherKey= " ) ), QLatin1String( "OtherValue" ) ); + QCOMPARE( a.custom( QLatin1String( "MyApp" ), QLatin1String( "MyKey" ) )= , QLatin1String( "YourValue" ) ); + + // test customs + QCOMPARE( a.customs().at( 0 ), QLatin1String( "MyApp-AnotherKey:OtherVal= ue" ) ); + QCOMPARE( a.customs().at( 1 ), QLatin1String( "MyApp-MyKey:YourValue" ) = ); + QCOMPARE( a.customs().at( 2 ), QLatin1String( "OtherApp-OtherKey:OurValu= e" ) ); + + // test equal operator + KABC::Addressee b; + b.setUid( a.uid() ); + b.insertCustom( QLatin1String( "OtherApp" ), QLatin1String( "OtherKey" )= , QLatin1String( "OurValue" ) ); + b.insertCustom( QLatin1String( "MyApp" ), QLatin1String( "MyKey" ), QLat= in1String( "YourValue" ) ); + b.insertCustom( QLatin1String( "MyApp" ), QLatin1String( "AnotherKey" ),= QLatin1String( "OtherValue" ) ); + + QCOMPARE( a, b ); + + b.insertCustom( QLatin1String( "MyApp" ), QLatin1String( "AnotherKey" ),= QLatin1String( "WrongValue" ) ); + QVERIFY( a !=3D b ); + + // test setCustoms + KABC::Addressee c; + c.insertCustom( QLatin1String( "ThisApp" ), QLatin1String( "ShouldNotBe"= ), QLatin1String( "There" ) ); + QCOMPARE( c.customs().count(), 1 ); + + const QStringList testData =3D QStringList() << QLatin1String( "FirstApp= -FirstKey:FirstValue" ) + << QLatin1String( "SecondApp-= SecondKey:SecondValue" ) + << QLatin1String( "ThirdApp-T= hirdKey:ThirdValue" ); + + c.setCustoms( testData ); + QCOMPARE( c.customs().count(), 3 ); + + QCOMPARE( c.custom( QLatin1String( "FirstApp" ), QLatin1String( "FirstKe= y" ) ), QLatin1String( "FirstValue" ) ); + QCOMPARE( c.custom( QLatin1String( "SecondApp" ), QLatin1String( "Second= Key" ) ), QLatin1String( "SecondValue" ) ); + QCOMPARE( c.custom( QLatin1String( "ThirdApp" ), QLatin1String( "ThirdKe= y" ) ), QLatin1String( "ThirdValue" ) ); + + // test remove + QCOMPARE( c.customs().count(), 3 ); + c.removeCustom( QLatin1String( "UnknownApp" ), QLatin1String( "FirstKey"= ) ); + QCOMPARE( c.customs().count(), 3 ); + c.removeCustom( QLatin1String( "FirstApp" ), QLatin1String( "UnknownKey"= ) ); + QCOMPARE( c.customs().count(), 3 ); + c.removeCustom( QLatin1String( "FirstApp" ), QLatin1String( "FirstKey" )= ); + QCOMPARE( c.customs().count(), 2 ); +} diff --git a/kabc/tests/addresseetest.h b/kabc/tests/addresseetest.h index 6d49276..16ed19c 100644 --- a/kabc/tests/addresseetest.h +++ b/kabc/tests/addresseetest.h @@ -36,6 +36,7 @@ class AddresseeTest : public QObject void serializeTest(); void fullEmailTest(); void nameFromStringTest(); + void customFieldsTest(); }; = #endif