Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

opimtodosortvector.cpp

Go to the documentation of this file.
00001 /*
00002                              This file is part of the Opie Project
00003                              Copyright (C) 2004 Holger Freyther <freyther@handhelds.org>
00004               =.             Copyright (C) The Opie Team <opie-devel@handhelds.org>
00005             .=l.
00006            .>+-=
00007  _;:,     .>    :=|.         This program is free software; you can
00008 .> <`_,   >  .   <=          redistribute it and/or  modify it under
00009 :`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
00010 .="- .-=="i,     .._         License as published by the Free Software
00011  - .   .-<_>     .<>         Foundation; either version 2 of the License,
00012      ._= =}       :          or (at your option) any later version.
00013     .%`+i>       _;_.
00014     .i_,=:_.      -<s.       This program is distributed in the hope that
00015      +  .  -:.       =       it will be useful,  but WITHOUT ANY WARRANTY;
00016     : ..    .:,     . . .    without even the implied warranty of
00017     =_        +     =;=|`    MERCHANTABILITY or FITNESS FOR A
00018   _.=:.       :    :=>`:     PARTICULAR PURPOSE. See the GNU
00019 ..}^=.=       =       ;      Library General Public License for more
00020 ++=   -.     .`     .:       details.
00021  :     =  ...= . :.=-
00022  -.   .:....=;==+<;          You should have received a copy of the GNU
00023   -_. . .   )=.  =           Library General Public License along with
00024     --        :-=`           this library; see the file COPYING.LIB.
00025                              If not, write to the Free Software Foundation,
00026                              Inc., 59 Temple Place - Suite 330,
00027                              Boston, MA 02111-1307, USA.
00028 */
00029 
00030 #include "opimtodosortvector.h"
00031 #include <opie2/otodoaccess.h>
00032 
00033 namespace Opie {
00034 namespace Internal {
00035 
00036 namespace{
00037 inline QString string( const OPimTodo& todo) {
00038     return  todo.summary().isEmpty() ?
00039         todo.description().left(20 ) :
00040         todo.summary();
00041 }
00042 
00043 inline int completed( const OPimTodo& todo1, const OPimTodo& todo2) {
00044     int ret = 0;
00045     if ( todo1.isCompleted() ) ret++;
00046     if ( todo2.isCompleted() ) ret--;
00047     return ret;
00048 }
00049 
00050 inline int priority( const OPimTodo& t1, const OPimTodo& t2) {
00051     return ( t1.priority() - t2.priority() );
00052 }
00053 
00054 inline int summary( const OPimTodo& t1, const OPimTodo& t2) {
00055     return QString::compare( string(t1), string(t2) );
00056 }
00057 
00058 inline int deadline( const OPimTodo& t1, const OPimTodo& t2) {
00059     int ret = 0;
00060     if ( t1.hasDueDate() &&
00061          t2.hasDueDate() )
00062         ret = t2.dueDate().daysTo( t1.dueDate() );
00063     else if ( t1.hasDueDate() )
00064         ret = -1;
00065     else if ( t2.hasDueDate() )
00066         ret = 1;
00067     else
00068         ret = 0;
00069 
00070     return ret;
00071 }
00072 
00073 }
00074 
00075 OPimTodoSortVector::OPimTodoSortVector( uint size, bool asc, int sort )
00076     : OPimSortVector<OPimTodo>( size, asc, sort )
00077 {}
00078 
00079 int OPimTodoSortVector::compareItems( const OPimTodo& con1, const OPimTodo& con2 ) {
00080     bool seComp, sePrio, seSum, seDeadline;
00081     seComp = sePrio = seDeadline = seSum = false;
00082     int ret =0;
00083     bool asc = sortAscending();
00084 
00085     /* same item */
00086     if ( con1.uid() == con2.uid() )
00087         return 0;
00088 
00089     switch ( sortOrder() ) {
00090     case OPimTodoAccess::Completed: {
00091         ret = completed( con1, con2 );
00092         seComp = TRUE;
00093         break;
00094     }
00095     case OPimTodoAccess::Priority: {
00096         ret = priority( con1, con2 );
00097         sePrio = TRUE;
00098         break;
00099     }
00100     case OPimTodoAccess::SortSummary: {
00101         ret  = summary( con1, con2 );
00102         seSum = TRUE;
00103         break;
00104     }
00105     case OPimTodoAccess::SortByDate:
00106     case OPimTodoAccess::Deadline: {
00107         ret = deadline( con1, con2 );
00108         seDeadline = TRUE;
00109         break;
00110     }
00111     default:
00112         ret = 0;
00113         break;
00114     };
00115     /*
00116      * FIXME do better sorting if the first sort criteria
00117      * ret equals 0 start with complete and so on...
00118      */
00119 
00120     /* twist it we're not ascending*/
00121     if (!asc)
00122         ret = ret * -1;
00123 
00124     if ( ret )
00125         return ret;
00126 
00127     // default did not gave difference let's try it other way around
00128     /*
00129      * General try if already checked if not test
00130      * and return
00131      * 1.Completed
00132      * 2.Priority
00133      * 3.Description
00134      * 4.DueDate
00135      */
00136     if (!seComp ) {
00137         if ( (ret = completed( con1, con2 ) ) ) {
00138             if (!asc ) ret *= -1;
00139             return ret;
00140         }
00141     }
00142     if (!sePrio ) {
00143         if ( (ret = priority( con1, con2 ) ) ) {
00144             if (!asc ) ret *= -1;
00145             return ret;
00146         }
00147     }
00148     if (!seSum ) {
00149         if ( (ret = summary(con1, con2 ) ) ) {
00150             if (!asc) ret *= -1;
00151             return ret;
00152         }
00153     }
00154     if (!seDeadline) {
00155         if ( (ret = deadline( con1, con2 ) ) ) {
00156             if (!asc) ret *= -1;
00157             return ret;
00158         }
00159     }
00160 
00161     return 0;
00162 }
00163 
00164 }
00165 }

Generated on Sat Nov 5 16:16:23 2005 for OPIE by  doxygen 1.4.2