change KTTSD to Jovie
[kdepim.git] / kaddressbook / contactsorter.cpp
blob7ec316bbc48d6f992ce067d96fed14b22982ccc6
1 /*
2 This file is part of KAddressBook.
4 Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program 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
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 #include "contactsorter.h"
23 #include <kabc/addressee.h>
25 class ContactSortHelper
27 public:
28 ContactSortHelper( ContactFields::Field field, Qt::SortOrder sortOrder )
29 : mSortField( field ), mSortOrder( sortOrder )
33 inline bool operator()( const KABC::Addressee &contact, const KABC::Addressee &otherContact ) const
35 int result = QString::localeAwareCompare( ContactFields::value( mSortField, contact ),
36 ContactFields::value( mSortField, otherContact ) );
37 if ( result == 0 ) {
38 int givenNameResult = QString::localeAwareCompare( ContactFields::value( ContactFields::GivenName, contact ),
39 ContactFields::value( ContactFields::GivenName, otherContact ) );
40 if ( givenNameResult == 0 ) {
41 int familyNameResult = QString::localeAwareCompare( ContactFields::value( ContactFields::FamilyName, contact ),
42 ContactFields::value( ContactFields::FamilyName, otherContact ) );
43 if ( familyNameResult == 0 ) {
44 result = QString::localeAwareCompare( ContactFields::value( ContactFields::FormattedName, contact ),
45 ContactFields::value( ContactFields::FormattedName, otherContact ) );
46 } else
47 result = familyNameResult;
48 } else
49 result = givenNameResult;
52 bool lesser = result < 0;
54 if ( mSortOrder == Qt::DescendingOrder )
55 lesser = !lesser;
57 return lesser;
60 private:
61 const ContactFields::Field mSortField;
62 const Qt::SortOrder mSortOrder;
65 ContactSorter::ContactSorter( ContactFields::Field field, Qt::SortOrder sortOrder )
66 : mSortField( field ), mSortOrder( sortOrder )
70 void ContactSorter::sort( QList<KABC::Addressee> &contacts ) const
72 qStableSort( contacts.begin(), contacts.end(), ContactSortHelper( mSortField, mSortOrder ) );