kabc Library API Documentation

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., 59 Temple Place - Suite 330, 00019 Boston, MA 02111-1307, USA. 00020 */ 00021 00022 #include <kdebug.h> 00023 00024 #include "addresseelist.h" 00025 #include "field.h" 00026 00027 using namespace KABC; 00028 00029 // 00030 // 00031 // Traits 00032 // 00033 // 00034 00035 bool SortingTraits::Uid::eq( const Addressee &a1, const Addressee &a2 ) 00036 { 00037 // locale awareness doesn't make sense sorting ids 00038 return ( QString::compare( a1.uid(), a2.uid() ) == 0 ); 00039 } 00040 00041 bool SortingTraits::Uid::lt( const Addressee &a1, const Addressee &a2 ) 00042 { 00043 // locale awareness doesn't make sense sorting ids 00044 return ( QString::compare( a1.uid(), a2.uid() ) < 0 ); 00045 } 00046 00047 bool SortingTraits::Name::eq( const Addressee &a1, const Addressee &a2 ) 00048 { 00049 return ( QString::localeAwareCompare( a1.name(), a2.name() ) == 0 ); 00050 } 00051 00052 bool SortingTraits::Name::lt( const Addressee &a1, const Addressee &a2 ) 00053 { 00054 return ( QString::localeAwareCompare( a1.name(), a2.name() ) < 0 ); 00055 } 00056 00057 bool SortingTraits::FormattedName::eq( const Addressee &a1, const Addressee &a2 ) 00058 { 00059 return ( QString::localeAwareCompare( a1.formattedName(), a2.formattedName() ) == 0 ); 00060 } 00061 00062 bool SortingTraits::FormattedName::lt( const Addressee &a1, const Addressee &a2 ) 00063 { 00064 return ( QString::localeAwareCompare( a1.formattedName(), a2.formattedName() ) < 0 ); 00065 } 00066 00067 bool SortingTraits::FamilyName::eq( const Addressee &a1, const Addressee &a2 ) 00068 { 00069 return ( QString::localeAwareCompare( a1.familyName(), a2.familyName() ) == 0 00070 && QString::localeAwareCompare( a1.givenName(), a2.givenName() ) == 0 ); 00071 } 00072 00073 bool SortingTraits::FamilyName::lt( const Addressee &a1, const Addressee &a2 ) 00074 { 00075 int family = QString::localeAwareCompare( a1.familyName(), a2.familyName() ); 00076 if ( 0 == family ) { 00077 return ( QString::localeAwareCompare( a1.givenName(), a2.givenName() ) < 0 ); 00078 } else { 00079 return family < 0; 00080 } 00081 } 00082 00083 bool SortingTraits::GivenName::eq( const Addressee &a1, const Addressee &a2 ) 00084 { 00085 return ( QString::localeAwareCompare( a1.givenName(), a2.givenName() ) == 0 00086 && QString::localeAwareCompare( a1.familyName(), a2.familyName() ) == 0 ); 00087 } 00088 00089 bool SortingTraits::GivenName::lt( const Addressee &a1, const Addressee &a2 ) 00090 { 00091 int given = QString::localeAwareCompare( a1.givenName(), a2.givenName() ); 00092 if ( 0 == given ) { 00093 return ( QString::localeAwareCompare( a1.familyName(), a2.familyName() ) < 0 ); 00094 } else { 00095 return given < 0; 00096 } 00097 } 00098 00099 // 00100 // 00101 // AddresseeList 00102 // 00103 // 00104 00105 static Field *sActiveField=0; 00106 00107 AddresseeList::AddresseeList() 00108 : QValueList<Addressee>() 00109 { 00110 mReverseSorting = false; 00111 mActiveSortingCriterion = FormattedName; 00112 } 00113 00114 AddresseeList::~AddresseeList() 00115 { 00116 } 00117 00118 AddresseeList::AddresseeList( const AddresseeList &l ) 00119 : QValueList<Addressee>( l ) 00120 { 00121 mReverseSorting = l.reverseSorting(); 00122 mActiveSortingCriterion = l.sortingCriterion(); 00123 } 00124 00125 AddresseeList::AddresseeList( const QValueList<Addressee> &l ) 00126 : QValueList<Addressee>( l ) 00127 { 00128 mReverseSorting = false; 00129 } 00130 00131 void AddresseeList::dump() const 00132 { 00133 kdDebug(5700) << "AddresseeList {" << endl; 00134 kdDebug(5700) << "reverse order: " << ( mReverseSorting ? "true" : "false" ) << endl; 00135 00136 QString crit; 00137 if ( Uid == mActiveSortingCriterion ) { 00138 crit = "Uid"; 00139 } else if ( Name == mActiveSortingCriterion ) { 00140 crit = "Name"; 00141 } else if ( FormattedName == mActiveSortingCriterion ) { 00142 crit = "FormattedName"; 00143 } else if ( FamilyName == mActiveSortingCriterion ) { 00144 crit = "FamilyName"; 00145 } else if ( GivenName == mActiveSortingCriterion ) { 00146 crit = "GivenName"; 00147 } else { 00148 crit = "unknown -- update dump method"; 00149 } 00150 00151 kdDebug(5700) << "sorting criterion: " << crit << endl; 00152 00153 for ( const_iterator it = begin(); it != end(); ++it ) { 00154 (*it).dump(); 00155 } 00156 00157 kdDebug(5700) << "}" << endl; 00158 } 00159 00160 void AddresseeList::sortBy( SortingCriterion c ) 00161 { 00162 mActiveSortingCriterion = c; 00163 if ( Uid == c ) { 00164 sortByTrait<SortingTraits::Uid>(); 00165 } else if ( Name == c ) { 00166 sortByTrait<SortingTraits::Name>(); 00167 } else if ( FormattedName == c ) { 00168 sortByTrait<SortingTraits::FormattedName>(); 00169 } else if ( FamilyName == c ) { 00170 sortByTrait<SortingTraits::FamilyName>(); 00171 } else if ( GivenName==c ) { 00172 sortByTrait<SortingTraits::GivenName>(); 00173 } else { 00174 kdError(5700) << "AddresseeList sorting criterion passed for which a trait is not known. No sorting done." << endl; 00175 } 00176 } 00177 00178 void AddresseeList::sort() 00179 { 00180 sortBy( mActiveSortingCriterion ); 00181 } 00182 00183 template<class Trait> 00184 void AddresseeList::sortByTrait() 00185 { 00186 // FIXME: better sorting algorithm, bubblesort is not acceptable for larger lists. 00187 // 00188 // for i := 1 to n - 1 00189 // do for j := 1 to n - i 00190 // do if A[j] > A[j+1] 00191 // then temp := A[j] 00192 // A[j] := A[j + 1] 00193 // A[j + 1 ] := temp 00194 00195 iterator i1 = begin(); 00196 iterator endIt = end(); 00197 --endIt; 00198 if ( i1 == endIt ) // don't need sorting 00199 return; 00200 00201 iterator i2 = endIt; 00202 while( i1 != endIt ) { 00203 iterator j1 = begin(); 00204 iterator j2 = j1; 00205 ++j2; 00206 while( j1 != i2 ) { 00207 if ( !mReverseSorting && Trait::lt( *j2, *j1 ) 00208 || mReverseSorting && Trait::lt( *j1, *j2 ) ) { 00209 qSwap( *j1, *j2 ); 00210 } 00211 ++j1; 00212 ++j2; 00213 } 00214 ++i1; 00215 --i2; 00216 } 00217 } 00218 00219 void AddresseeList::sortByField( Field *field ) 00220 { 00221 if ( !field ) { 00222 kdWarning(5700) << "sortByField called with no active sort field" << endl; 00223 return; 00224 } 00225 00226 sActiveField = field; 00227 00228 if ( count() == 0 ) 00229 return; 00230 00231 KABC::Addressee::setSortKey( sActiveField ); 00232 qHeapSort( *this ); 00233 KABC::Addressee::setSortKey( 0 ); 00234 } 00235 00236 Field* 00237 AddresseeList::sortingField() const 00238 { 00239 return sActiveField; 00240 }
KDE Logo
This file is part of the documentation for kabc Library Version 3.2.3.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Oct 10 18:56:00 2004 by doxygen 1.3.7 written by Dimitri van Heesch, © 1997-2003