[prev in list] [next in list] [prev in thread] [next in thread]
List: kde-commits
Subject: [kdepimlibs] kabc: Use QMap to store custom fields of an Addressee object
From: Tobias Koenig <tokoe () kde ! org>
Date: 2013-03-03 19:05:23
Message-ID: 20130303190523.2DB13A604F () git ! kde ! org
[Download RAW message or body]
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 = other.mKeys;
mEmails = other.mEmails;
mCategories = other.mCategories;
- mCustom = other.mCustom;
+ mCustomFields = other.mCustomFields;
#ifndef KDEPIM_NO_KRESOURCES
mResource = other.mResource;
@@ -143,7 +143,7 @@ class Addressee::Private : public QSharedData
Key::List mKeys;
QStringList mEmails;
QStringList mCategories;
- QStringList mCustom;
+ QMap<QString, QString> mCustomFields;
#ifndef KDEPIM_NO_KRESOURCES
Resource *mResource;
@@ -348,7 +348,7 @@ bool Addressee::operator==( const Addressee &addressee ) const
return false;
}
- if ( !listEquals( d->mCustom, addressee.d->mCustom ) ) {
+ if ( d->mCustomFields != addressee.d->mCustomFields ) {
kDebug() << "custom differs";
return false;
}
@@ -1632,59 +1632,54 @@ void Addressee::insertCustom( const QString &app, const \
QString &name,
d->mEmpty = false;
- QString qualifiedName = app + QLatin1Char( '-' ) + name + QLatin1Char( ':' );
+ const QString qualifiedName = app + QLatin1Char( '-' ) + name;
- QStringList::Iterator it;
- QStringList::Iterator end( d->mCustom.end() );
- for ( it = d->mCustom.begin(); it != end; ++it ) {
- if ( ( *it ).startsWith( qualifiedName ) ) {
- ( *it ) = 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 = app + QLatin1Char( '-' ) + name + QLatin1Char( ':' \
); + const QString qualifiedName = app + QLatin1Char( '-' ) + name;
- QStringList::Iterator it;
- for ( it = d->mCustom.begin(); it != 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 = app + QLatin1Char( '-' ) + name + QLatin1Char( ':' );
- QString value;
-
- QStringList::ConstIterator it;
- QStringList::ConstIterator end( d->mCustom.constEnd() );
- for ( it = d->mCustom.constBegin(); it != end; ++it ) {
- if ( ( *it ).startsWith( qualifiedName ) ) {
- value = ( *it ).mid( ( *it ).indexOf( QLatin1Char( ':' ) ) + 1 );
- break;
- }
- }
+ const QString qualifiedName = app + QLatin1Char( '-' ) + name;
- return value;
+ return d->mCustomFields.value( qualifiedName );
}
-void Addressee::setCustoms( const QStringList &l )
+void Addressee::setCustoms( const QStringList &customs )
{
d->mEmpty = false;
- d->mCustom = l;
+
+ d->mCustomFields.clear();
+
+ foreach ( const QString &custom, customs ) {
+ const int index = custom.indexOf( QLatin1Char( ':' ) );
+ if ( index == -1 )
+ continue;
+
+ const QString qualifiedName = custom.left( index );
+ const QString value = custom.mid( index + 1 );
+
+ d->mCustomFields.insert( qualifiedName, value );
+ }
}
QStringList Addressee::customs() const
{
- return d->mCustom;
+ QStringList result;
+
+ QMapIterator<QString, QString> it( d->mCustomFields );
+ while ( it.hasNext() ) {
+ it.next();
+ result << it.key() + QLatin1Char( ':' ) + it.value();
+ }
+
+ return result;
}
void Addressee::parseEmailAddress( const QString &rawEmail, QString &fullName,
@@ -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, Addressee &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 = 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" ), QLatin1String( \
"MyValue" ) ); + QCOMPARE( a.customs().count(), 1 );
+ QCOMPARE( a.custom( QLatin1String( "MyApp" ), QLatin1String( "MyKey" ) ), \
QLatin1String( "MyValue" ) ); +
+ a.insertCustom( QLatin1String( "MyApp" ), QLatin1String( "MyKey" ), QLatin1String( \
"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( "OtherKey" ) ), \
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:OtherValue" ) );
+ QCOMPARE( a.customs().at( 1 ), QLatin1String( "MyApp-MyKey:YourValue" ) );
+ QCOMPARE( a.customs().at( 2 ), QLatin1String( "OtherApp-OtherKey:OurValue" ) );
+
+ // test equal operator
+ KABC::Addressee b;
+ b.setUid( a.uid() );
+ b.insertCustom( QLatin1String( "OtherApp" ), QLatin1String( "OtherKey" ), \
QLatin1String( "OurValue" ) ); + b.insertCustom( QLatin1String( "MyApp" ), \
QLatin1String( "MyKey" ), QLatin1String( "YourValue" ) ); + b.insertCustom( \
QLatin1String( "MyApp" ), QLatin1String( "AnotherKey" ), QLatin1String( "OtherValue" \
) ); +
+ QCOMPARE( a, b );
+
+ b.insertCustom( QLatin1String( "MyApp" ), QLatin1String( "AnotherKey" ), \
QLatin1String( "WrongValue" ) ); + QVERIFY( a != b );
+
+ // test setCustoms
+ KABC::Addressee c;
+ c.insertCustom( QLatin1String( "ThisApp" ), QLatin1String( "ShouldNotBe" ), \
QLatin1String( "There" ) ); + QCOMPARE( c.customs().count(), 1 );
+
+ const QStringList testData = QStringList() << QLatin1String( \
"FirstApp-FirstKey:FirstValue" ) + << \
QLatin1String( "SecondApp-SecondKey:SecondValue" ) + \
<< QLatin1String( "ThirdApp-ThirdKey:ThirdValue" ); +
+ c.setCustoms( testData );
+ QCOMPARE( c.customs().count(), 3 );
+
+ QCOMPARE( c.custom( QLatin1String( "FirstApp" ), QLatin1String( "FirstKey" ) ), \
QLatin1String( "FirstValue" ) ); + QCOMPARE( c.custom( QLatin1String( "SecondApp" ), \
QLatin1String( "SecondKey" ) ), QLatin1String( "SecondValue" ) ); + QCOMPARE( \
c.custom( QLatin1String( "ThirdApp" ), QLatin1String( "ThirdKey" ) ), 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
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic