kaddressbook

printsortmode.cpp
1/*
2 This file is part of KAddressBook.
3 Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 As a special exception, permission is given to link this program
20 with any edition of TQt, and distribute the resulting executable,
21 without including the source code for TQt in the source distribution.
22*/
23
24#include <tdeabc/field.h>
25
26#include "printsortmode.h"
27
28#if KDE_IS_VERSION(3,3,91)
29
30PrintSortMode::PrintSortMode( TDEABC::Field *field, bool ascending )
31 : mSortField( field ), mAscending( ascending )
32{
33 const TDEABC::Field::List fields = TDEABC::Field::allFields();
34 TDEABC::Field::List::ConstIterator it;
35 for ( it = fields.begin(); it != fields.end(); ++it ) {
36 if ( (*it)->label() == TDEABC::Addressee::givenNameLabel() )
37 mGivenNameField = *it;
38 else if ( (*it)->label() == TDEABC::Addressee::familyNameLabel() )
39 mFamilyNameField = *it;
40 else if ( (*it)->label() == TDEABC::Addressee::formattedNameLabel() )
41 mFormattedNameField = *it;
42 }
43}
44
45bool PrintSortMode::lesser( const TDEABC::Addressee &first,
46 const TDEABC::Addressee &second ) const
47{
48 if ( !mSortField )
49 return false;
50
51 int result = TQString::localeAwareCompare( mSortField->value( first ),
52 mSortField->value( second ) );
53 if ( result == 0 ) {
54 int givenNameResult = TQString::localeAwareCompare( mGivenNameField->value( first ),
55 mGivenNameField->value( second ) );
56 if ( givenNameResult == 0 ) {
57 int familyNameResult = TQString::localeAwareCompare( mFamilyNameField->value( first ),
58 mFamilyNameField->value( second ) );
59 if ( familyNameResult == 0 ) {
60 result = TQString::localeAwareCompare( mFormattedNameField->value( first ),
61 mFormattedNameField->value( second ) );
62 } else
63 result = familyNameResult;
64 } else
65 result = givenNameResult;
66 }
67
68 bool lesser = result < 0;
69
70 if ( !mAscending )
71 lesser = !lesser;
72
73 return lesser;
74}
75
76#endif