• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • tdeabc
 

tdeabc

  • tdeabc
addresseelist.cpp
1/*
2 This file is part of libtdeabc.
3 Copyright (c) 2002 Jost Schenck <jost@schenck.de>
4 2003 Tobias Koenig <tokoe@kde.org>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 Boston, MA 02110-1301, USA.
20*/
21
22#include <kdebug.h>
23
24#include "addresseelist.h"
25
26#include "field.h"
27#include "sortmode.h"
28
29using namespace TDEABC;
30
31//
32//
33// Traits
34//
35//
36
37bool SortingTraits::Uid::eq( const Addressee &a1, const Addressee &a2 )
38{
39 // locale awareness doesn't make sense sorting ids
40 return ( TQString::compare( a1.uid(), a2.uid() ) == 0 );
41}
42
43bool SortingTraits::Uid::lt( const Addressee &a1, const Addressee &a2 )
44{
45 // locale awareness doesn't make sense sorting ids
46 return ( TQString::compare( a1.uid(), a2.uid() ) < 0 );
47}
48
49bool SortingTraits::Name::eq( const Addressee &a1, const Addressee &a2 )
50{
51 return ( TQString::localeAwareCompare( a1.name(), a2.name() ) == 0 );
52}
53
54bool SortingTraits::Name::lt( const Addressee &a1, const Addressee &a2 )
55{
56 return ( TQString::localeAwareCompare( a1.name(), a2.name() ) < 0 );
57}
58
59bool SortingTraits::FormattedName::eq( const Addressee &a1, const Addressee &a2 )
60{
61 return ( TQString::localeAwareCompare( a1.formattedName(), a2.formattedName() ) == 0 );
62}
63
64bool SortingTraits::FormattedName::lt( const Addressee &a1, const Addressee &a2 )
65{
66 return ( TQString::localeAwareCompare( a1.formattedName(), a2.formattedName() ) < 0 );
67}
68
69bool SortingTraits::FamilyName::eq( const Addressee &a1, const Addressee &a2 )
70{
71 return ( TQString::localeAwareCompare( a1.familyName(), a2.familyName() ) == 0
72 && TQString::localeAwareCompare( a1.givenName(), a2.givenName() ) == 0 );
73}
74
75bool SortingTraits::FamilyName::lt( const Addressee &a1, const Addressee &a2 )
76{
77 int family = TQString::localeAwareCompare( a1.familyName(), a2.familyName() );
78 if ( 0 == family ) {
79 return ( TQString::localeAwareCompare( a1.givenName(), a2.givenName() ) < 0 );
80 } else {
81 return family < 0;
82 }
83}
84
85bool SortingTraits::GivenName::eq( const Addressee &a1, const Addressee &a2 )
86{
87 return ( TQString::localeAwareCompare( a1.givenName(), a2.givenName() ) == 0
88 && TQString::localeAwareCompare( a1.familyName(), a2.familyName() ) == 0 );
89}
90
91bool SortingTraits::GivenName::lt( const Addressee &a1, const Addressee &a2 )
92{
93 int given = TQString::localeAwareCompare( a1.givenName(), a2.givenName() );
94 if ( 0 == given ) {
95 return ( TQString::localeAwareCompare( a1.familyName(), a2.familyName() ) < 0 );
96 } else {
97 return given < 0;
98 }
99}
100
101//
102//
103// AddresseeList
104//
105//
106
107static Field *sActiveField=0;
108
109AddresseeList::AddresseeList()
110 : TQValueList<Addressee>()
111{
112 mReverseSorting = false;
113 mActiveSortingCriterion = FormattedName;
114}
115
116AddresseeList::~AddresseeList()
117{
118}
119
120AddresseeList::AddresseeList( const AddresseeList &l )
121 : TQValueList<Addressee>( l )
122{
123 mReverseSorting = l.reverseSorting();
124 mActiveSortingCriterion = l.sortingCriterion();
125}
126
127AddresseeList::AddresseeList( const TQValueList<Addressee> &l )
128 : TQValueList<Addressee>( l )
129{
130 mReverseSorting = false;
131}
132
133void AddresseeList::dump() const
134{
135 kdDebug(5700) << "AddresseeList {" << endl;
136 kdDebug(5700) << "reverse order: " << ( mReverseSorting ? "true" : "false" ) << endl;
137
138 TQString crit;
139 if ( Uid == mActiveSortingCriterion ) {
140 crit = "Uid";
141 } else if ( Name == mActiveSortingCriterion ) {
142 crit = "Name";
143 } else if ( FormattedName == mActiveSortingCriterion ) {
144 crit = "FormattedName";
145 } else if ( FamilyName == mActiveSortingCriterion ) {
146 crit = "FamilyName";
147 } else if ( GivenName == mActiveSortingCriterion ) {
148 crit = "GivenName";
149 } else {
150 crit = "unknown -- update dump method";
151 }
152
153 kdDebug(5700) << "sorting criterion: " << crit << endl;
154
155 for ( const_iterator it = begin(); it != end(); ++it ) {
156 (*it).dump();
157 }
158
159 kdDebug(5700) << "}" << endl;
160}
161
162void AddresseeList::sortBy( SortingCriterion c )
163{
164 mActiveSortingCriterion = c;
165 if ( Uid == c ) {
166 sortByTrait<SortingTraits::Uid>();
167 } else if ( Name == c ) {
168 sortByTrait<SortingTraits::Name>();
169 } else if ( FormattedName == c ) {
170 sortByTrait<SortingTraits::FormattedName>();
171 } else if ( FamilyName == c ) {
172 sortByTrait<SortingTraits::FamilyName>();
173 } else if ( GivenName==c ) {
174 sortByTrait<SortingTraits::GivenName>();
175 } else {
176 kdError(5700) << "AddresseeList sorting criterion passed for which a trait is not known. No sorting done." << endl;
177 }
178}
179
180void AddresseeList::sort()
181{
182 sortBy( mActiveSortingCriterion );
183}
184
185template<class Trait>
186void AddresseeList::sortByTrait()
187{
188 // FIXME: better sorting algorithm, bubblesort is not acceptable for larger lists.
189 //
190 // for i := 1 to n - 1
191 // do for j := 1 to n - i
192 // do if A[j] > A[j+1]
193 // then temp := A[j]
194 // A[j] := A[j + 1]
195 // A[j + 1 ] := temp
196
197 iterator i1 = begin();
198 iterator endIt = end();
199 --endIt;
200 if ( i1 == endIt ) // don't need sorting
201 return;
202
203 iterator i2 = endIt;
204 while( i1 != endIt ) {
205 iterator j1 = begin();
206 iterator j2 = j1;
207 ++j2;
208 while( j1 != i2 ) {
209 if ( !mReverseSorting && Trait::lt( *j2, *j1 )
210 || mReverseSorting && Trait::lt( *j1, *j2 ) ) {
211 tqSwap( *j1, *j2 );
212 }
213 ++j1;
214 ++j2;
215 }
216 ++i1;
217 --i2;
218 }
219}
220
221void AddresseeList::sortByField( Field *field )
222{
223 if ( !field ) {
224 kdWarning(5700) << "sortByField called with no active sort field" << endl;
225 return;
226 }
227
228 sActiveField = field;
229
230 if ( count() == 0 )
231 return;
232
233 TDEABC::FieldSortMode *mode = new TDEABC::FieldSortMode( sActiveField, !mReverseSorting );
234
235 TDEABC::Addressee::setSortMode( mode );
236 qHeapSort( *this );
237 TDEABC::Addressee::setSortMode( 0 );
238
239 delete mode;
240}
241
242void AddresseeList::sortByMode( SortMode *mode )
243{
244 if ( count() == 0 )
245 return;
246
247 TDEABC::Addressee::setSortMode( mode );
248 qHeapSort( *this );
249 TDEABC::Addressee::setSortMode( 0 );
250}
251
252Field*
253AddresseeList::sortingField() const
254{
255 return sActiveField;
256}
TDEABC::AddresseeList
a TQValueList of Addressee, with sorting functionality
Definition: addresseelist.h:113
TDEABC::AddresseeList::sortingCriterion
SortingCriterion sortingCriterion() const
Returns the active sorting criterion, ie the sorting criterion that will be used by a sort call.
Definition: addresseelist.h:202
TDEABC::AddresseeList::sortByMode
void sortByMode(SortMode *mode=0)
Sorts this list by a specific sorting mode.
Definition: addresseelist.cpp:242
TDEABC::AddresseeList::sort
void sort()
Sorts this list by its active sorting criterion.
Definition: addresseelist.cpp:180
TDEABC::AddresseeList::sortByTrait
void sortByTrait()
Templated sort function.
Definition: addresseelist.cpp:186
TDEABC::AddresseeList::reverseSorting
bool reverseSorting() const
Returns the direction of sorting.
Definition: addresseelist.h:136
TDEABC::AddresseeList::sortBy
void sortBy(SortingCriterion c)
Sorts this list by a specific criterion.
Definition: addresseelist.cpp:162
TDEABC::AddresseeList::sortByField
void sortByField(Field *field=0)
Sorts this list by a specific field.
Definition: addresseelist.cpp:221
TDEABC::AddresseeList::sortingField
Field * sortingField() const
Returns the active sorting field, ie a pointer to the Field object which was used for the last sortBy...
Definition: addresseelist.cpp:253
TDEABC::Addressee
address book entry
Definition: addressee.src.h:75
TDEABC::Addressee::uid
TQString uid() const
Return unique identifier.
Definition: addressee.src.cpp:174
TDEABC::SortMode
Sort method for sorting an addressee list.
Definition: sortmode.h:37
kdWarning
kdbgstream kdWarning(int area=0)
kdError
kdbgstream kdError(int area=0)
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)
TDEABC
static data, shared by ALL addressee objects
Definition: address.h:48
TDEABC::SortingCriterion
SortingCriterion
Addressee attribute used for sorting.
Definition: addresseelist.h:87

tdeabc

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

tdeabc

Skip menu "tdeabc"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeabc by doxygen 1.9.4
This website is maintained by Timothy Pearson.