kabc
addresseelist.cpp
00001 /* 00002 This file is part of libkabc. 00003 Copyright (c) 2002 Jost Schenck <jost@schenck.de> 00004 2003 Tobias Koenig <tokoe@kde.org> 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Library General Public 00008 License as published by the Free Software Foundation; either 00009 version 2 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Library General Public License for more details. 00015 00016 You should have received a copy of the GNU Library General Public License 00017 along with this library; see the file COPYING.LIB. If not, write to 00018 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00019 Boston, MA 02110-1301, USA. 00020 */ 00021 00022 #include "addresseelist.h" 00023 #include "field.h" 00024 #include "sortmode.h" 00025 00026 #include <kdebug.h> 00027 00028 #include <QtCore/QSharedData> 00029 00030 using namespace KABC; 00031 00032 // 00033 // 00034 // Traits 00035 // 00036 // 00037 00038 SortingTraits::Uid::Uid() 00039 : d( 0 ) 00040 { 00041 } 00042 00043 SortingTraits::Uid::~Uid() 00044 { 00045 } 00046 00047 bool SortingTraits::Uid::eq( const Addressee &a1, const Addressee &a2 ) 00048 { 00049 // locale awareness doesn't make sense sorting ids 00050 return QString::compare( a1.uid(), a2.uid() ) == 0; 00051 } 00052 00053 bool SortingTraits::Uid::lt( const Addressee &a1, const Addressee &a2 ) 00054 { 00055 // locale awareness doesn't make sense sorting ids 00056 return QString::compare( a1.uid(), a2.uid() ) < 0; 00057 } 00058 00059 SortingTraits::Name::Name() 00060 : d( 0 ) 00061 { 00062 } 00063 00064 SortingTraits::Name::~Name() 00065 { 00066 } 00067 00068 bool SortingTraits::Name::eq( const Addressee &a1, const Addressee &a2 ) 00069 { 00070 return QString::localeAwareCompare( a1.name(), a2.name() ) == 0; 00071 } 00072 00073 bool SortingTraits::Name::lt( const Addressee &a1, const Addressee &a2 ) 00074 { 00075 return QString::localeAwareCompare( a1.name(), a2.name() ) < 0; 00076 } 00077 00078 SortingTraits::FormattedName::FormattedName() 00079 : d( 0 ) 00080 { 00081 } 00082 00083 SortingTraits::FormattedName::~FormattedName() 00084 { 00085 } 00086 00087 bool SortingTraits::FormattedName::eq( const Addressee &a1, const Addressee &a2 ) 00088 { 00089 return QString::localeAwareCompare( a1.formattedName(), a2.formattedName() ) == 0; 00090 } 00091 00092 bool SortingTraits::FormattedName::lt( const Addressee &a1, const Addressee &a2 ) 00093 { 00094 return QString::localeAwareCompare( a1.formattedName(), a2.formattedName() ) < 0; 00095 } 00096 00097 SortingTraits::FamilyName::FamilyName() 00098 : d( 0 ) 00099 { 00100 } 00101 00102 SortingTraits::FamilyName::~FamilyName() 00103 { 00104 } 00105 00106 bool SortingTraits::FamilyName::eq( const Addressee &a1, const Addressee &a2 ) 00107 { 00108 return 00109 QString::localeAwareCompare( a1.familyName(), a2.familyName() ) == 0 && 00110 QString::localeAwareCompare( a1.givenName(), a2.givenName() ) == 0; 00111 } 00112 00113 bool SortingTraits::FamilyName::lt( const Addressee &a1, const Addressee &a2 ) 00114 { 00115 int family = QString::localeAwareCompare( a1.familyName(), a2.familyName() ); 00116 if ( 0 == family ) { 00117 return QString::localeAwareCompare( a1.givenName(), a2.givenName() ) < 0; 00118 } else { 00119 return family < 0; 00120 } 00121 } 00122 00123 SortingTraits::GivenName::GivenName() 00124 : d( 0 ) 00125 { 00126 } 00127 00128 SortingTraits::GivenName::~GivenName() 00129 { 00130 } 00131 00132 bool SortingTraits::GivenName::eq( const Addressee &a1, const Addressee &a2 ) 00133 { 00134 return 00135 QString::localeAwareCompare( a1.givenName(), a2.givenName() ) == 0 && 00136 QString::localeAwareCompare( a1.familyName(), a2.familyName() ) == 0; 00137 } 00138 00139 bool SortingTraits::GivenName::lt( const Addressee &a1, const Addressee &a2 ) 00140 { 00141 int given = QString::localeAwareCompare( a1.givenName(), a2.givenName() ); 00142 if ( 0 == given ) { 00143 return QString::localeAwareCompare( a1.familyName(), a2.familyName() ) < 0; 00144 } else { 00145 return given < 0; 00146 } 00147 } 00148 00149 // 00150 // 00151 // AddresseeList 00152 // 00153 // 00154 00155 static Field *sActiveField=0; 00156 00157 class AddresseeList::Private : public QSharedData 00158 { 00159 public: 00160 Private() 00161 : mReverseSorting( false ), mActiveSortingCriterion( FormattedName ) 00162 { 00163 } 00164 00165 Private( const Private &other ) 00166 : QSharedData( other ) 00167 { 00168 mReverseSorting = other.mReverseSorting; 00169 mActiveSortingCriterion = other.mActiveSortingCriterion; 00170 } 00171 00172 bool mReverseSorting; 00173 SortingCriterion mActiveSortingCriterion; 00174 }; 00175 00176 AddresseeList::AddresseeList() 00177 : QList<Addressee>(), d( new Private ) 00178 { 00179 } 00180 00181 AddresseeList::~AddresseeList() 00182 { 00183 } 00184 00185 AddresseeList::AddresseeList( const AddresseeList &other ) 00186 : QList<Addressee>( other ), d( other.d ) 00187 { 00188 } 00189 00190 AddresseeList::AddresseeList( const QList<Addressee> &l ) 00191 : QList<Addressee>( l ), d( new Private ) 00192 { 00193 } 00194 00195 AddresseeList &AddresseeList::operator=( const AddresseeList &other ) 00196 { 00197 if ( this != &other ) { 00198 QList<Addressee>::operator=( other ); 00199 d = other.d; 00200 } 00201 00202 return *this; 00203 } 00204 00205 QString AddresseeList::toString() const 00206 { 00207 QString str; 00208 00209 str += QLatin1String( "AddresseeList {\n" ); 00210 str += QString::fromLatin1( " Reverse Order: %1\n" ).arg( d->mReverseSorting ? 00211 QLatin1String( "true" ) : 00212 QLatin1String( "false" ) ); 00213 00214 QString crit; 00215 if ( Uid == d->mActiveSortingCriterion ) { 00216 crit = QLatin1String( "Uid" ); 00217 } else if ( Name == d->mActiveSortingCriterion ) { 00218 crit = QLatin1String( "Name" ); 00219 } else if ( FormattedName == d->mActiveSortingCriterion ) { 00220 crit = QLatin1String( "FormattedName" ); 00221 } else if ( FamilyName == d->mActiveSortingCriterion ) { 00222 crit = QLatin1String( "FamilyName" ); 00223 } else if ( GivenName == d->mActiveSortingCriterion ) { 00224 crit = QLatin1String( "GivenName" ); 00225 } else { 00226 crit = QLatin1String( "unknown -- update dump method" ); 00227 } 00228 00229 str += QString::fromLatin1( " Sorting criterion: %1\n" ).arg( crit ); 00230 00231 for ( const_iterator it = begin(); it != end(); ++it ) { 00232 // str += (*it).toString(); 00233 } 00234 00235 str += QLatin1String( "}\n" ); 00236 00237 return str; 00238 } 00239 00240 void AddresseeList::setReverseSorting( bool reverseSorting ) 00241 { 00242 d->mReverseSorting = reverseSorting; 00243 } 00244 00245 bool AddresseeList::reverseSorting() const 00246 { 00247 return d->mReverseSorting; 00248 } 00249 00250 void AddresseeList::sortBy( SortingCriterion c ) 00251 { 00252 d->mActiveSortingCriterion = c; 00253 if ( Uid == c ) { 00254 sortByTrait<SortingTraits::Uid>(); 00255 } else if ( Name == c ) { 00256 sortByTrait<SortingTraits::Name>(); 00257 } else if ( FormattedName == c ) { 00258 sortByTrait<SortingTraits::FormattedName>(); 00259 } else if ( FamilyName == c ) { 00260 sortByTrait<SortingTraits::FamilyName>(); 00261 } else if ( GivenName == c ) { 00262 sortByTrait<SortingTraits::GivenName>(); 00263 } else { 00264 kError(5700) << "AddresseeList sorting criterion passed for which a trait is not known." 00265 << "No sorting done."; 00266 } 00267 } 00268 00269 void AddresseeList::sort() 00270 { 00271 sortBy( d->mActiveSortingCriterion ); 00272 } 00273 00274 template<class Trait> 00275 void AddresseeList::sortByTrait() 00276 { 00277 // FIXME: better sorting algorithm, bubblesort is not acceptable for larger lists. 00278 // 00279 // for i := 1 to n - 1 00280 // do for j := 1 to n - i 00281 // do if A[j] > A[j+1] 00282 // then temp := A[j] 00283 // A[j] := A[j + 1] 00284 // A[j + 1 ] := temp 00285 00286 iterator i1 = begin(); 00287 iterator endIt = end(); 00288 --endIt; 00289 if ( i1 == endIt ) { // don't need sorting 00290 return; 00291 } 00292 00293 iterator i2 = endIt; 00294 while ( i1 != endIt ) { 00295 iterator j1 = begin(); 00296 iterator j2 = j1; 00297 ++j2; 00298 while ( j1 != i2 ) { 00299 if ( ( !d->mReverseSorting && Trait::lt( *j2, *j1 ) ) || 00300 ( d->mReverseSorting && Trait::lt( *j1, *j2 ) ) ) { 00301 qSwap( *j1, *j2 ); 00302 } 00303 ++j1; 00304 ++j2; 00305 } 00306 ++i1; 00307 --i2; 00308 } 00309 } 00310 00311 void AddresseeList::sortByField( Field *field ) 00312 { 00313 if ( !field ) { 00314 kWarning(5700) << "sortByField called with no active sort field"; 00315 return; 00316 } 00317 00318 sActiveField = field; 00319 00320 if ( count() == 0 ) { 00321 return; 00322 } 00323 00324 KABC::FieldSortMode *mode = new KABC::FieldSortMode( sActiveField, !d->mReverseSorting ); 00325 00326 KABC::Addressee::setSortMode( mode ); 00327 qSort( *this ); 00328 KABC::Addressee::setSortMode( 0 ); 00329 00330 delete mode; 00331 } 00332 00333 void AddresseeList::sortByMode( SortMode *mode ) 00334 { 00335 if ( count() == 0 ) { 00336 return; 00337 } 00338 00339 KABC::Addressee::setSortMode( mode ); 00340 qSort( *this ); 00341 KABC::Addressee::setSortMode( 0 ); 00342 } 00343 00344 SortingCriterion AddresseeList::sortingCriterion() const 00345 { 00346 return d->mActiveSortingCriterion; 00347 } 00348 00349 Field *AddresseeList::sortingField() const 00350 { 00351 return sActiveField; 00352 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 05:09:41 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 05:09:41 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.