• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

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 += QString( "AddresseeList {\n" );
00210   str += QString( "   Reverse Order: %1\n" ).arg( d->mReverseSorting ? "true" : "false" );
00211 
00212   QString crit;
00213   if ( Uid == d->mActiveSortingCriterion ) {
00214     crit = "Uid";
00215   } else if ( Name == d->mActiveSortingCriterion ) {
00216     crit = "Name";
00217   } else if ( FormattedName == d->mActiveSortingCriterion ) {
00218     crit = "FormattedName";
00219   } else if ( FamilyName == d->mActiveSortingCriterion ) {
00220     crit = "FamilyName";
00221   } else if ( GivenName == d->mActiveSortingCriterion ) {
00222     crit = "GivenName";
00223   } else {
00224     crit = "unknown -- update dump method";
00225   }
00226 
00227   str += QString( "   Sorting criterion: %1\n" ).arg( crit );
00228 
00229   for ( const_iterator it = begin(); it != end(); ++it ) {
00230 //    str += (*it).toString();
00231   }
00232 
00233   str += QString( "}\n" );
00234 
00235   return str;
00236 }
00237 
00238 void AddresseeList::setReverseSorting( bool reverseSorting )
00239 {
00240   d->mReverseSorting = reverseSorting;
00241 }
00242 
00243 bool AddresseeList::reverseSorting() const
00244 {
00245   return d->mReverseSorting;
00246 }
00247 
00248 void AddresseeList::sortBy( SortingCriterion c )
00249 {
00250   d->mActiveSortingCriterion = c;
00251   if ( Uid == c ) {
00252     sortByTrait<SortingTraits::Uid>();
00253   } else if ( Name == c ) {
00254     sortByTrait<SortingTraits::Name>();
00255   } else if ( FormattedName == c ) {
00256     sortByTrait<SortingTraits::FormattedName>();
00257   } else if ( FamilyName == c ) {
00258     sortByTrait<SortingTraits::FamilyName>();
00259   } else if ( GivenName == c ) {
00260     sortByTrait<SortingTraits::GivenName>();
00261   } else {
00262     kError(5700) << "AddresseeList sorting criterion passed for which a trait is not known."
00263                  << "No sorting done.";
00264   }
00265 }
00266 
00267 void AddresseeList::sort()
00268 {
00269   sortBy( d->mActiveSortingCriterion );
00270 }
00271 
00272 template<class Trait>
00273 void AddresseeList::sortByTrait()
00274 {
00275   // FIXME: better sorting algorithm, bubblesort is not acceptable for larger lists.
00276   //
00277   // for i := 1 to n - 1
00278   //   do for j := 1 to n - i
00279   //     do if A[j] > A[j+1]
00280   //       then temp :=  A[j]
00281   //         A[j] := A[j + 1]
00282   //         A[j + 1 ] := temp
00283 
00284   iterator i1 = begin();
00285   iterator endIt = end();
00286   --endIt;
00287   if ( i1 == endIt ) { // don't need sorting
00288     return;
00289   }
00290 
00291   iterator i2 = endIt;
00292   while ( i1 != endIt ) {
00293     iterator j1 = begin();
00294     iterator j2 = j1;
00295     ++j2;
00296     while ( j1 != i2 ) {
00297       if ( !d->mReverseSorting && Trait::lt( *j2, *j1 ) ||
00298            d->mReverseSorting && Trait::lt( *j1, *j2 ) ) {
00299         qSwap( *j1, *j2 );
00300       }
00301       ++j1;
00302       ++j2;
00303     }
00304     ++i1;
00305     --i2;
00306   }
00307 }
00308 
00309 void AddresseeList::sortByField( Field *field )
00310 {
00311   if ( !field ) {
00312     kWarning(5700) << "sortByField called with no active sort field";
00313     return;
00314   }
00315 
00316   sActiveField = field;
00317 
00318   if ( count() == 0 ) {
00319     return;
00320   }
00321 
00322   KABC::FieldSortMode *mode = new KABC::FieldSortMode( sActiveField, !d->mReverseSorting );
00323 
00324   KABC::Addressee::setSortMode( mode );
00325   qSort( *this );
00326   KABC::Addressee::setSortMode( 0 );
00327 
00328   delete mode;
00329 }
00330 
00331 void AddresseeList::sortByMode( SortMode *mode )
00332 {
00333   if ( count() == 0 ) {
00334     return;
00335   }
00336 
00337   KABC::Addressee::setSortMode( mode );
00338   qSort( *this );
00339   KABC::Addressee::setSortMode( 0 );
00340 }
00341 
00342 SortingCriterion AddresseeList::sortingCriterion() const
00343 {
00344   return d->mActiveSortingCriterion;
00345 }
00346 
00347 Field *AddresseeList::sortingField() const
00348 {
00349   return sActiveField;
00350 }

kabc

Skip menu "kabc"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.5
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal