Diffstat (limited to 'libopie2/opiepim/private/opimtodosortvector.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | libopie2/opiepim/private/opimtodosortvector.cpp | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/libopie2/opiepim/private/opimtodosortvector.cpp b/libopie2/opiepim/private/opimtodosortvector.cpp new file mode 100644 index 0000000..8d15710 --- a/dev/null +++ b/libopie2/opiepim/private/opimtodosortvector.cpp | |||
@@ -0,0 +1,163 @@ | |||
1 | /* | ||
2 | This file is part of the Opie Project | ||
3 | Copyright (C) 2004 Holger Freyther <freyther@handhelds.org> | ||
4 | =. Copyright (C) The Opie Team <opie-devel@handhelds.org> | ||
5 | .=l. | ||
6 | .>+-= | ||
7 | _;:, .> :=|. This program is free software; you can | ||
8 | .> <`_, > . <= redistribute it and/or modify it under | ||
9 | :`=1 )Y*s>-.-- : the terms of the GNU Library General Public | ||
10 | .="- .-=="i, .._ License as published by the Free Software | ||
11 | - . .-<_> .<> Foundation; either version 2 of the License, | ||
12 | ._= =} : or (at your option) any later version. | ||
13 | .%`+i> _;_. | ||
14 | .i_,=:_. -<s. This program is distributed in the hope that | ||
15 | + . -:. = it will be useful, but WITHOUT ANY WARRANTY; | ||
16 | : .. .:, . . . without even the implied warranty of | ||
17 | =_ + =;=|` MERCHANTABILITY or FITNESS FOR A | ||
18 | _.=:. : :=>`: PARTICULAR PURPOSE. See the GNU | ||
19 | ..}^=.= = ; Library General Public License for more | ||
20 | ++= -. .` .: details. | ||
21 | : = ...= . :.=- | ||
22 | -. .:....=;==+<; You should have received a copy of the GNU | ||
23 | -_. . . )=. = Library General Public License along with | ||
24 | -- :-=` this library; see the file COPYING.LIB. | ||
25 | If not, write to the Free Software Foundation, | ||
26 | Inc., 59 Temple Place - Suite 330, | ||
27 | Boston, MA 02111-1307, USA. | ||
28 | */ | ||
29 | |||
30 | #include "opimtodosortvector.h" | ||
31 | #include <opie2/otodoaccess.h> | ||
32 | |||
33 | namespace Opie { | ||
34 | namespace Internal { | ||
35 | |||
36 | inline QString string( const OPimTodo& todo) { | ||
37 | return todo.summary().isEmpty() ? | ||
38 | todo.description().left(20 ) : | ||
39 | todo.summary(); | ||
40 | } | ||
41 | |||
42 | inline int completed( const OPimTodo& todo1, const OPimTodo& todo2) { | ||
43 | int ret = 0; | ||
44 | if ( todo1.isCompleted() ) ret++; | ||
45 | if ( todo2.isCompleted() ) ret--; | ||
46 | return ret; | ||
47 | } | ||
48 | |||
49 | inline int priority( const OPimTodo& t1, const OPimTodo& t2) { | ||
50 | return ( t1.priority() - t2.priority() ); | ||
51 | } | ||
52 | |||
53 | inline int summary( const OPimTodo& t1, const OPimTodo& t2) { | ||
54 | return QString::compare( string(t1), string(t2) ); | ||
55 | } | ||
56 | |||
57 | inline int deadline( const OPimTodo& t1, const OPimTodo& t2) { | ||
58 | int ret = 0; | ||
59 | if ( t1.hasDueDate() && | ||
60 | t2.hasDueDate() ) | ||
61 | ret = t2.dueDate().daysTo( t1.dueDate() ); | ||
62 | else if ( t1.hasDueDate() ) | ||
63 | ret = -1; | ||
64 | else if ( t2.hasDueDate() ) | ||
65 | ret = 1; | ||
66 | else | ||
67 | ret = 0; | ||
68 | |||
69 | return ret; | ||
70 | } | ||
71 | |||
72 | |||
73 | OPimTodoSortVector::OPimTodoSortVector( uint size, bool asc, int sort ) | ||
74 | : OPimSortVector<OPimTodo>( size, asc, sort ) | ||
75 | {} | ||
76 | |||
77 | int OPimTodoSortVector::compareItems( const OPimTodo& con1, const OPimTodo& con2 ) { | ||
78 | bool seComp, sePrio, seSum, seDeadline; | ||
79 | seComp = sePrio = seDeadline = seSum = false; | ||
80 | int ret =0; | ||
81 | bool asc = sortAscending(); | ||
82 | |||
83 | /* same item */ | ||
84 | if ( con1.uid() == con2.uid() ) | ||
85 | return 0; | ||
86 | |||
87 | switch ( sortOrder() ) { | ||
88 | case OPimTodoAccess::Completed: { | ||
89 | ret = completed( con1, con2 ); | ||
90 | seComp = TRUE; | ||
91 | break; | ||
92 | } | ||
93 | case OPimTodoAccess::Priority: { | ||
94 | ret = priority( con1, con2 ); | ||
95 | sePrio = TRUE; | ||
96 | break; | ||
97 | } | ||
98 | case OPimTodoAccess::SortSummary: { | ||
99 | ret = summary( con1, con2 ); | ||
100 | seSum = TRUE; | ||
101 | break; | ||
102 | } | ||
103 | case OPimTodoAccess::SortByDate: | ||
104 | case OPimTodoAccess::Deadline: { | ||
105 | ret = deadline( con1, con2 ); | ||
106 | seDeadline = TRUE; | ||
107 | break; | ||
108 | } | ||
109 | default: | ||
110 | ret = 0; | ||
111 | break; | ||
112 | }; | ||
113 | /* | ||
114 | * FIXME do better sorting if the first sort criteria | ||
115 | * ret equals 0 start with complete and so on... | ||
116 | */ | ||
117 | |||
118 | /* twist it we're not ascending*/ | ||
119 | if (!asc) | ||
120 | ret = ret * -1; | ||
121 | |||
122 | if ( ret ) | ||
123 | return ret; | ||
124 | |||
125 | // default did not gave difference let's try it other way around | ||
126 | /* | ||
127 | * General try if already checked if not test | ||
128 | * and return | ||
129 | * 1.Completed | ||
130 | * 2.Priority | ||
131 | * 3.Description | ||
132 | * 4.DueDate | ||
133 | */ | ||
134 | if (!seComp ) { | ||
135 | if ( (ret = completed( con1, con2 ) ) ) { | ||
136 | if (!asc ) ret *= -1; | ||
137 | return ret; | ||
138 | } | ||
139 | } | ||
140 | if (!sePrio ) { | ||
141 | if ( (ret = priority( con1, con2 ) ) ) { | ||
142 | if (!asc ) ret *= -1; | ||
143 | return ret; | ||
144 | } | ||
145 | } | ||
146 | if (!seSum ) { | ||
147 | if ( (ret = summary(con1, con2 ) ) ) { | ||
148 | if (!asc) ret *= -1; | ||
149 | return ret; | ||
150 | } | ||
151 | } | ||
152 | if (!seDeadline) { | ||
153 | if ( (ret = deadline( con1, con2 ) ) ) { | ||
154 | if (!asc) ret *= -1; | ||
155 | return ret; | ||
156 | } | ||
157 | } | ||
158 | |||
159 | return 0; | ||
160 | } | ||
161 | |||
162 | } | ||
163 | } | ||