# kabc Library API Documentation

Overview | Class Hierarchy | Classes | Classes (annotated) | Members | Source Files

# addresseelist.cpp

    00001 /*
    00002     This file is part of libkabc.
    00003     Copyright (c) 2002 Jost Schenck <jost@schenck.de>
    00004
    00005     This library is free software; you can redistribute it and/or
    00006     modify it under the terms of the GNU Library General Public
    00007     License as published by the Free Software Foundation; either
    00008     version 2 of the License, or (at your option) any later version.
    00009
    00010     This library is distributed in the hope that it will be useful,
    00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00013     Library General Public License for more details.
    00014
    00015     You should have received a copy of the GNU Library General Public License
    00016     along with this library; see the file COPYING.LIB.  If not, write to
    00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    00018     Boston, MA 02111-1307, USA.
    00019 */
    00020
    00021 #include <kdebug.h>
    00022
    00023 #include "addresseelist.h"
    00024
    00025 using namespace KABC;
    00026
    00027 //
    00028 //
    00029 // Traits
    00030 //
    00031 //
    00032
    00033 bool SortingTraits::Uid::eq( const Addressee &a1, const Addressee &a2 )
    00034 {
    00035   // locale awareness doesn't make sense sorting ids
    00036   return ( QString::compare( a1.uid(), a2.uid() ) == 0 );
    00037 }
    00038
    00039 bool SortingTraits::Uid::lt( const Addressee &a1, const Addressee &a2 )
    00040 {
    00041   // locale awareness doesn't make sense sorting ids
    00042   return ( QString::compare( a1.uid(), a2.uid() ) < 0 );
    00043 }
    00044
    00045 bool SortingTraits::Name::eq( const Addressee &a1, const Addressee &a2 )
    00046 {
    00047   return ( QString::localeAwareCompare( a1.name(), a2.name() ) == 0 );
    00048 }
    00049
    00050 bool SortingTraits::Name::lt( const Addressee &a1, const Addressee &a2 )
    00051 {
    00052   return ( QString::localeAwareCompare( a1.name(), a2.name() ) < 0 );
    00053 }
    00054
    00055 bool SortingTraits::FormattedName::eq( const Addressee &a1, const Addressee &a2 )
    00056 {
    00057   return ( QString::localeAwareCompare( a1.formattedName(), a2.formattedName() ) == 0 );
    00058 }
    00059
    00060 bool SortingTraits::FormattedName::lt( const Addressee &a1, const Addressee &a2 )
    00061 {
    00062   return ( QString::localeAwareCompare( a1.formattedName(), a2.formattedName() ) < 0 );
    00063 }
    00064
    00065 bool SortingTraits::FamilyName::eq( const Addressee &a1, const Addressee &a2 )
    00066 {
    00067   return ( QString::localeAwareCompare( a1.familyName(), a2.familyName() ) == 0
    00068            && QString::localeAwareCompare( a1.givenName(), a2.givenName() ) == 0 );
    00069 }
    00070
    00071 bool SortingTraits::FamilyName::lt( const Addressee &a1, const Addressee &a2 )
    00072 {
    00073   int family = QString::localeAwareCompare( a1.familyName(), a2.familyName() );
    00074   if ( 0 == family ) {
    00075     return ( QString::localeAwareCompare( a1.givenName(), a2.givenName() ) < 0 );
    00076   } else {
    00077     return family < 0;
    00078   }
    00079 }
    00080
    00081 bool SortingTraits::GivenName::eq( const Addressee &a1, const Addressee &a2 )
    00082 {
    00083   return ( QString::localeAwareCompare( a1.givenName(), a2.givenName() ) == 0
    00084            && QString::localeAwareCompare( a1.familyName(), a2.familyName() ) == 0 );
    00085 }
    00086
    00087 bool SortingTraits::GivenName::lt( const Addressee &a1, const Addressee &a2 )
    00088 {
    00089   int given = QString::localeAwareCompare( a1.givenName(), a2.givenName() );
    00090   if ( 0 == given ) {
    00091     return ( QString::localeAwareCompare( a1.familyName(), a2.familyName() ) < 0 );
    00092   } else {
    00093     return given < 0;
    00094   }
    00095 }
    00096
    00097 //
    00098 //
    00099 // AddresseeList
    00100 //
    00101 //
    00102
    00103 AddresseeList::AddresseeList()
    00104   : QValueList<Addressee>()
    00105 {
    00106   mReverseSorting = false;
    00107   mActiveSortingCriterion = FormattedName;
    00108 }
    00109
    00110 AddresseeList::~AddresseeList()
    00111 {
    00112 }
    00113
    00114 AddresseeList::AddresseeList( const AddresseeList &l )
    00115   : QValueList<Addressee>( l )
    00116 {
    00117   mReverseSorting = l.reverseSorting();
    00118   mActiveSortingCriterion = l.sortingCriterion();
    00119 }
    00120
    00121 AddresseeList::AddresseeList( const QValueList<Addressee> &l )
    00122   : QValueList<Addressee>( l )
    00123 {
    00124 }
    00125
    00126 void AddresseeList::dump() const
    00127 {
    00128   kdDebug(5700) << "AddresseeList {" << endl;
    00129   kdDebug(5700) << "reverse order: " << ( mReverseSorting ? "true" : "false" ) << endl;
    00130
    00131   QString crit;
    00132   if ( Uid == mActiveSortingCriterion ) {
    00133     crit = "Uid";
    00134   } else if ( Name == mActiveSortingCriterion ) {
    00135     crit = "Name";
    00136   } else if ( FormattedName == mActiveSortingCriterion ) {
    00137     crit = "FormattedName";
    00138   } else if ( FamilyName == mActiveSortingCriterion ) {
    00139     crit = "FamilyName";
    00140   } else if ( GivenName == mActiveSortingCriterion ) {
    00141     crit = "GivenName";
    00142   } else {
    00143     crit = "unknown -- update dump method";
    00144   }
    00145
    00146   kdDebug(5700) << "sorting criterion: " << crit << endl;
    00147
    00148   for ( const_iterator it = begin(); it != end(); ++it ) {
    00149     (*it).dump();
    00150   }
    00151
    00152   kdDebug(5700) << "}" << endl;
    00153 }
    00154
    00155 void AddresseeList::sortBy( SortingCriterion c )
    00156 {
    00157   mActiveSortingCriterion = c;
    00158   if ( Uid == c ) {
    00159     sortByTrait<SortingTraits::Uid>();
    00160   } else if ( Name == c ) {
    00161     sortByTrait<SortingTraits::Name>();
    00162   } else if ( FormattedName == c ) {
    00163     sortByTrait<SortingTraits::FormattedName>();
    00164   } else if ( FamilyName == c ) {
    00165     sortByTrait<SortingTraits::FamilyName>();
    00166   } else if ( GivenName==c ) {
    00167     sortByTrait<SortingTraits::GivenName>();
    00168   } else {
    00169     kdError(5700) << "AddresseeList sorting criterion passed for which a trait is not known. No sorting done." << endl;
    00170   }
    00171 }
    00172
    00173 void AddresseeList::sort()
    00174 {
    00175   sortBy( mActiveSortingCriterion );
    00176 }
    00177
    00178 template<class Trait>
    00179 void AddresseeList::sortByTrait()
    00180 {
    00181   // FIXME: better sorting algorithm, bubblesort is not acceptable for larger lists.
    00182   //
    00183   // for i := 1 to n - 1
    00184   //   do for j := 1 to n - i
    00185   //     do if A[j] > A[j+1]
    00186   //       then temp :=  A[j]
    00187   //         A[j] := A[j + 1]
    00188   //         A[j + 1 ] := temp
    00189
    00190   iterator i1 = begin();
    00191   iterator endIt = end();
    00192   --endIt;
    00193   if ( i1 == endIt ) // don't need sorting
    00194     return;
    00195
    00196   iterator i2 = endIt;
    00197   while( i1 != endIt ) {
    00198     iterator j1 = begin();
    00199     iterator j2 = j1;
    00200     ++j2;
    00201     while( j1 != i2 ) {
    00202       if ( !mReverseSorting && Trait::lt( *j2, *j1 )
    00203            || mReverseSorting && Trait::lt( *j1, *j2 ) ) {
    00204         qSwap( *j1, *j2 );
    00205       }
    00206       ++j1;
    00207       ++j2;
    00208     }
    00209     ++i1;
    00210     --i2;
    00211   }
    00212 }
    00213
    00214 void AddresseeList::sortByField( Field *field )
    00215 {
    00216   iterator i1 = begin();
    00217   iterator endIt = end();
    00218   --endIt;
    00219   if ( i1 == endIt ) // don't need sorting
    00220     return;
    00221
    00222   iterator i2 = endIt;
    00223   while( i1 != endIt ) {
    00224     iterator j1 = begin();
    00225     iterator j2 = j1;
    00226     ++j2;
    00227     while( j1 != i2 ) {
    00228       if ( !mReverseSorting && ( QString::localeAwareCompare( field->value( *j2 ), field->value( *j1 ) ) < 0 )
    00229            || mReverseSorting && ( QString::localeAwareCompare( field->value( *j1 ), field->value( *j2 ) ) < 0 ) ) {
    00230         qSwap( *j1, *j2 );
    00231       }
    00232       ++j1;
    00233       ++j2;
    00234     }
    00235     ++i1;
    00236     --i2;
    00237   }
    00238 }

This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:29:18 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001