• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kaddressbook

  • sources
  • kde-4.12
  • kdepim
  • kaddressbook
contactsorter.cpp
Go to the documentation of this file.
1 /*
2  This file is part of KAddressBook.
3 
4  Copyright (c) 2009 Tobias Koenig <tokoe@kde.org>
5 
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.
10 
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.
15 
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.
19 */
20 
21 #include "contactsorter.h"
22 
23 #include <KABC/Addressee>
24 
25 class ContactSortHelper
26 {
27  public:
28  ContactSortHelper( ContactFields::Field field, Qt::SortOrder sortOrder )
29  : mSortField( field ), mSortOrder( sortOrder )
30  {
31  }
32 
33  inline bool operator()( const KABC::Addressee &contact,
34  const KABC::Addressee &otherContact ) const
35  {
36  int result =
37  QString::localeAwareCompare(
38  ContactFields::value( mSortField, contact ),
39  ContactFields::value( mSortField, otherContact ) );
40 
41  if ( result == 0 ) {
42  int givenNameResult =
43  QString::localeAwareCompare(
44  ContactFields::value( ContactFields::GivenName, contact ),
45  ContactFields::value( ContactFields::GivenName, otherContact ) );
46 
47  if ( givenNameResult == 0 ) {
48  int familyNameResult =
49  QString::localeAwareCompare(
50  ContactFields::value( ContactFields::FamilyName, contact ),
51  ContactFields::value( ContactFields::FamilyName, otherContact ) );
52 
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;
60  }
61  } else {
62  result = givenNameResult;
63  }
64  }
65 
66  bool lesser = result < 0;
67 
68  if ( mSortOrder == Qt::DescendingOrder ) {
69  lesser = !lesser;
70  }
71 
72  return lesser;
73  }
74 
75  private:
76  const ContactFields::Field mSortField;
77  const Qt::SortOrder mSortOrder;
78 };
79 
80 ContactSorter::ContactSorter( ContactFields::Field field, Qt::SortOrder sortOrder )
81  : mSortField( field ), mSortOrder( sortOrder )
82 {
83 }
84 
85 void ContactSorter::sort( QList<KABC::Addressee> &contacts ) const
86 {
87  qStableSort( contacts.begin(), contacts.end(), ContactSortHelper( mSortField, mSortOrder ) );
88 }
ContactFields::FamilyName
Definition: contactfields.h:40
ContactFields::FormattedName
Definition: contactfields.h:36
ContactSorter::sort
void sort(QList< KABC::Addressee > &contacts) const
Definition: contactsorter.cpp:85
contactsorter.h
ContactFields::value
static QString value(Field field, const KABC::Addressee &contact)
Returns the value for the field of the contact.
Definition: contactfields.cpp:480
ContactSorter::ContactSorter
ContactSorter(ContactFields::Field field, Qt::SortOrder sortOrder=Qt::AscendingOrder)
Definition: contactsorter.cpp:80
ContactFields::Field
Field
Describes the standard fields that are available for every contact.
Definition: contactfields.h:33
ContactFields::GivenName
Definition: contactfields.h:38
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:51 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kaddressbook

Skip menu "kaddressbook"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal