kaddressbook

printsortmode.cpp

00001 /*
00002     This file is part of KAddressBook.
00003     Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>
00004 
00005     This program is free software; you can redistribute it and/or modify
00006     it under the terms of the GNU General Public License as published by
00007     the Free Software Foundation; either version 2 of the License, or
00008     (at your option) any later version.
00009 
00010     This program is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00013     GNU General Public License for more details.
00014 
00015     You should have received a copy of the GNU General Public License
00016     along with this program; if not, write to the Free Software
00017     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018 
00019     As a special exception, permission is given to link this program
00020     with any edition of Qt, and distribute the resulting executable,
00021     without including the source code for Qt in the source distribution.
00022 */
00023 
00024 #include <kabc/field.h>
00025 
00026 #include "printsortmode.h"
00027 
00028 #if KDE_IS_VERSION(3,3,91)
00029 
00030 PrintSortMode::PrintSortMode( KABC::Field *field, bool ascending )
00031   : mSortField( field ), mAscending( ascending )
00032 {
00033   const KABC::Field::List fields = KABC::Field::allFields();
00034   KABC::Field::List::ConstIterator it;
00035   for ( it = fields.begin(); it != fields.end(); ++it ) {
00036     if ( (*it)->label() == KABC::Addressee::givenNameLabel() )
00037       mGivenNameField = *it;
00038     else if ( (*it)->label() == KABC::Addressee::familyNameLabel() )
00039       mFamilyNameField = *it;
00040     else if ( (*it)->label() == KABC::Addressee::formattedNameLabel() )
00041       mFormattedNameField = *it;
00042   }
00043 }
00044 
00045 bool PrintSortMode::lesser( const KABC::Addressee &first,
00046                             const KABC::Addressee &second ) const
00047 {
00048   if ( !mSortField )
00049     return false;
00050 
00051   int result = QString::localeAwareCompare( mSortField->value( first ),
00052                                             mSortField->value( second ) );
00053   if ( result == 0 ) {
00054     int givenNameResult = QString::localeAwareCompare( mGivenNameField->value( first ),
00055                                                        mGivenNameField->value( second ) );
00056     if ( givenNameResult == 0 ) {
00057       int familyNameResult = QString::localeAwareCompare( mFamilyNameField->value( first ),
00058                                                           mFamilyNameField->value( second ) );
00059       if ( familyNameResult == 0 ) {
00060         result = QString::localeAwareCompare( mFormattedNameField->value( first ),
00061                                               mFormattedNameField->value( second ) );
00062       } else
00063         result = familyNameResult;
00064     } else
00065       result = givenNameResult;
00066   }
00067 
00068   bool lesser = result < 0;
00069 
00070   if ( !mAscending )
00071     lesser = !lesser;
00072 
00073   return lesser;
00074 }
00075 
00076 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys