22 #include "addresseelist.h"
28 #include <QtCore/QSharedData>
38 SortingTraits::Uid::Uid()
43 SortingTraits::Uid::~Uid()
47 bool SortingTraits::Uid::eq(
const Addressee &a1,
const Addressee &a2 )
50 return QString::compare( a1.uid(), a2.uid() ) == 0;
53 bool SortingTraits::Uid::lt(
const Addressee &a1,
const Addressee &a2 )
56 return QString::compare( a1.uid(), a2.uid() ) < 0;
59 SortingTraits::Name::Name()
64 SortingTraits::Name::~Name()
68 bool SortingTraits::Name::eq(
const Addressee &a1,
const Addressee &a2 )
70 return QString::localeAwareCompare( a1.name(), a2.name() ) == 0;
73 bool SortingTraits::Name::lt(
const Addressee &a1,
const Addressee &a2 )
75 return QString::localeAwareCompare( a1.name(), a2.name() ) < 0;
78 SortingTraits::FormattedName::FormattedName()
83 SortingTraits::FormattedName::~FormattedName()
87 bool SortingTraits::FormattedName::eq(
const Addressee &a1,
const Addressee &a2 )
89 return QString::localeAwareCompare( a1.formattedName(), a2.formattedName() ) == 0;
92 bool SortingTraits::FormattedName::lt(
const Addressee &a1,
const Addressee &a2 )
94 return QString::localeAwareCompare( a1.formattedName(), a2.formattedName() ) < 0;
97 SortingTraits::FamilyName::FamilyName()
102 SortingTraits::FamilyName::~FamilyName()
106 bool SortingTraits::FamilyName::eq(
const Addressee &a1,
const Addressee &a2 )
109 QString::localeAwareCompare( a1.familyName(), a2.familyName() ) == 0 &&
110 QString::localeAwareCompare( a1.givenName(), a2.givenName() ) == 0;
113 bool SortingTraits::FamilyName::lt(
const Addressee &a1,
const Addressee &a2 )
115 int family = QString::localeAwareCompare( a1.familyName(), a2.familyName() );
117 return QString::localeAwareCompare( a1.givenName(), a2.givenName() ) < 0;
123 SortingTraits::GivenName::GivenName()
128 SortingTraits::GivenName::~GivenName()
132 bool SortingTraits::GivenName::eq(
const Addressee &a1,
const Addressee &a2 )
135 QString::localeAwareCompare( a1.givenName(), a2.givenName() ) == 0 &&
136 QString::localeAwareCompare( a1.familyName(), a2.familyName() ) == 0;
139 bool SortingTraits::GivenName::lt(
const Addressee &a1,
const Addressee &a2 )
141 int given = QString::localeAwareCompare( a1.givenName(), a2.givenName() );
143 return QString::localeAwareCompare( a1.familyName(), a2.familyName() ) < 0;
155 static Field *sActiveField=0;
157 class AddresseeList::Private :
public QSharedData
161 : mReverseSorting( false ), mActiveSortingCriterion( FormattedName )
165 Private(
const Private &other )
166 : QSharedData( other )
168 mReverseSorting = other.mReverseSorting;
169 mActiveSortingCriterion = other.mActiveSortingCriterion;
172 bool mReverseSorting;
173 SortingCriterion mActiveSortingCriterion;
177 : QList<Addressee>(), d( new Private )
186 : QList<Addressee>( other ), d( other.d )
191 : QList<Addressee>( l ), d( new Private )
197 if (
this != &other ) {
198 QList<Addressee>::operator=( other );
209 str += QLatin1String(
"AddresseeList {\n" );
210 str += QString::fromLatin1(
" Reverse Order: %1\n" ).arg( d->mReverseSorting ?
211 QLatin1String(
"true" ) :
212 QLatin1String(
"false" ) );
215 if ( Uid == d->mActiveSortingCriterion ) {
216 crit = QLatin1String(
"Uid" );
217 }
else if ( Name == d->mActiveSortingCriterion ) {
218 crit = QLatin1String(
"Name" );
219 }
else if ( FormattedName == d->mActiveSortingCriterion ) {
220 crit = QLatin1String(
"FormattedName" );
221 }
else if ( FamilyName == d->mActiveSortingCriterion ) {
222 crit = QLatin1String(
"FamilyName" );
223 }
else if ( GivenName == d->mActiveSortingCriterion ) {
224 crit = QLatin1String(
"GivenName" );
226 crit = QLatin1String(
"unknown -- update dump method" );
229 str += QString::fromLatin1(
" Sorting criterion: %1\n" ).arg( crit );
230 #if 0 //code commented in loop => comment it too
231 for ( const_iterator it = begin(); it != end(); ++it ) {
236 str += QLatin1String(
"}\n" );
248 return d->mReverseSorting;
253 d->mActiveSortingCriterion = c;
255 sortByTrait<SortingTraits::Uid>();
256 }
else if ( Name == c ) {
257 sortByTrait<SortingTraits::Name>();
258 }
else if ( FormattedName == c ) {
259 sortByTrait<SortingTraits::FormattedName>();
260 }
else if ( FamilyName == c ) {
261 sortByTrait<SortingTraits::FamilyName>();
262 }
else if ( GivenName == c ) {
263 sortByTrait<SortingTraits::GivenName>();
265 kError( 5700 ) <<
"AddresseeList sorting criterion passed for which a trait is not known."
266 <<
"No sorting done.";
272 sortBy( d->mActiveSortingCriterion );
275 template<
class Trait>
287 iterator i1 = begin();
288 iterator endIt = end();
295 while ( i1 != endIt ) {
296 iterator j1 = begin();
300 if ( ( !d->mReverseSorting && Trait::lt( *j2, *j1 ) ) ||
301 ( d->mReverseSorting && Trait::lt( *j1, *j2 ) ) ) {
315 kWarning( 5700 ) <<
"sortByField called with no active sort field";
319 sActiveField = field;
321 if ( count() == 0 ) {
327 KABC::Addressee::setSortMode( mode );
329 KABC::Addressee::setSortMode( 0 );
336 if ( count() == 0 ) {
340 KABC::Addressee::setSortMode( mode );
342 KABC::Addressee::setSortMode( 0 );
347 return d->mActiveSortingCriterion;