Astyle kdelibs
[kdepim.git] / kaddressbook / contactsorter.cpp
blob6efab488f57f96cd7a4374ab22492aac3fd3f2e3
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 <KContacts/Addressee>
25 class ContactSortHelper
27 public:
28 ContactSortHelper(ContactFields::Field field, Qt::SortOrder sortOrder)
29 : mSortField(field), mSortOrder(sortOrder)
33 inline bool operator()(const KContacts::Addressee &contact,
34 const KContacts::Addressee &otherContact) const
36 int result =
37 QString::localeAwareCompare(
38 ContactFields::value(mSortField, contact),
39 ContactFields::value(mSortField, otherContact));
41 if (result == 0) {
42 int givenNameResult =
43 QString::localeAwareCompare(
44 ContactFields::value(ContactFields::GivenName, contact),
45 ContactFields::value(ContactFields::GivenName, otherContact));
47 if (givenNameResult == 0) {
48 int familyNameResult =
49 QString::localeAwareCompare(
50 ContactFields::value(ContactFields::FamilyName, contact),
51 ContactFields::value(ContactFields::FamilyName, otherContact));
53 if (familyNameResult == 0) {
54 result =
55 QString::localeAwareCompare(
56 ContactFields::value(ContactFields::FormattedName, contact),
57 ContactFields::value(ContactFields::FormattedName, otherContact));
58 } else {
59 result = familyNameResult;
61 } else {
62 result = givenNameResult;
66 bool lesser = result < 0;
68 if (mSortOrder == Qt::DescendingOrder) {
69 lesser = !lesser;
72 return lesser;
75 private:
76 const ContactFields::Field mSortField;
77 const Qt::SortOrder mSortOrder;
80 ContactSorter::ContactSorter(ContactFields::Field field, Qt::SortOrder sortOrder)
81 : mSortField(field), mSortOrder(sortOrder)
85 void ContactSorter::sort(KContacts::Addressee::List &contacts) const
87 qStableSort(contacts.begin(), contacts.end(), ContactSortHelper(mSortField, mSortOrder));