summaryrefslogtreecommitdiff
authorzecke <zecke>2004-11-18 21:46:17 (UTC)
committer zecke <zecke>2004-11-18 21:46:17 (UTC)
commit41fa1c196965f17f9748f835d90c307b7e594883 (patch) (unidiff)
tree1ea8a2cfe5bfb71f5c9456c7216dc5ff035dd811
parent7484344ff5be1f7c54e51715776d0e3cadeb1ed0 (diff)
downloadopie-41fa1c196965f17f9748f835d90c307b7e594883.zip
opie-41fa1c196965f17f9748f835d90c307b7e594883.tar.gz
opie-41fa1c196965f17f9748f835d90c307b7e594883.tar.bz2
Add generic sorting implementation based on QVector
for Contact and Todo
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--libopie2/opiepim/private/opimcontactsortvector.cpp104
-rw-r--r--libopie2/opiepim/private/opimcontactsortvector.h48
-rw-r--r--libopie2/opiepim/private/opimoccurrence_p.h33
-rw-r--r--libopie2/opiepim/private/opimsortvector.h138
-rw-r--r--libopie2/opiepim/private/opimtodosortvector.cpp163
-rw-r--r--libopie2/opiepim/private/opimtodosortvector.h48
-rw-r--r--libopie2/opiepim/private/private.pro9
7 files changed, 543 insertions, 0 deletions
diff --git a/libopie2/opiepim/private/opimcontactsortvector.cpp b/libopie2/opiepim/private/opimcontactsortvector.cpp
new file mode 100644
index 0000000..9e186fe
--- a/dev/null
+++ b/libopie2/opiepim/private/opimcontactsortvector.cpp
@@ -0,0 +1,104 @@
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 "opimcontactsortvector.h"
31#include <opie2/ocontactaccess.h>
32
33namespace Opie {
34namespace Internal {
35OPimContactSortVector::OPimContactSortVector( uint size, bool asc, int sort )
36 : OPimSortVector<OPimContact>( size, asc, sort ) {}
37
38int OPimContactSortVector::compareItems( const OPimContact& left,
39 const OPimContact& right ) {
40 if ( left.uid() == right.uid() )
41 return 0;
42
43 bool soTitle, soSummary, soFirstName, soMiddleName, soSuffix, soEmail,
44 soNick, soAnni, soBirth, soGender;
45 soTitle = soSummary = soFirstName = soMiddleName = soSuffix = soEmail =
46 soNick = soAnni = soBirth = soGender = false;
47 int ret = 0;
48 bool asc = sortAscending();
49
50 switch( sortOrder() ) {
51 case OPimContactAccess::SortSummary:
52 ret = testString( left.fileAs(), right.fileAs() );
53 soSummary = true;
54 break;
55 case OPimContactAccess::SortTitle:
56 ret = testString( left.title(), right.title() );
57 soTitle = true;
58 break;
59 case OPimContactAccess::SortFirstName:
60 ret = testString( left.firstName(), right.firstName() );
61 soFirstName = true;
62 break;
63 case OPimContactAccess::SortMiddleName:
64 ret = testString( left.middleName(), right.middleName() );
65 soMiddleName = true;
66 break;
67 case OPimContactAccess::SortSuffix:
68 ret = testString( left.suffix(), right.suffix() );
69 soSuffix = true;
70 break;
71 case OPimContactAccess::SortEmail:
72 ret = testString( left.defaultEmail(), right.defaultEmail() );
73 soEmail = true;
74 break;
75 case OPimContactAccess::SortNickname:
76 ret = testString( left.nickname(), right.nickname() );
77 soNick = true;
78 break;
79 case OPimContactAccess::SortAnniversary:
80 ret = testDate( left.anniversary(), right.anniversary() );
81 soAnni = true;
82 break;
83 case OPimContactAccess::SortByDate:
84 case OPimContactAccess::SortBirthday:
85 ret = testDate( left.birthday(), right.birthday() );
86 soBirth = true;
87 break;
88 case OPimContactAccess::SortGender:
89 ret = testString( left.gender(), right.gender() );
90 soGender = true;
91 break;
92 }
93
94 /* twist to honor ascending/descending setting as QVector only sorts ascending*/
95 if ( !asc )
96 ret *= -1;
97
98 // Maybe differentiate as in OPimTodoSortVector ### FIXME
99 // if( ret )
100 return ret;
101}
102
103}
104}
diff --git a/libopie2/opiepim/private/opimcontactsortvector.h b/libopie2/opiepim/private/opimcontactsortvector.h
new file mode 100644
index 0000000..3f40657
--- a/dev/null
+++ b/libopie2/opiepim/private/opimcontactsortvector.h
@@ -0,0 +1,48 @@
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#ifndef OPIM_CONTACT_SORT_VECTOR_H
31#define OPIM_CONTACT_SORT_VECTOR_H
32
33#include <opie2/opimcontact.h>
34#include <opie2/private/opimsortvector.h>
35
36namespace Opie {
37namespace Internal {
38class OPimContactSortVector : public OPimSortVector<OPimContact> {
39public:
40 OPimContactSortVector( uint size, bool asc, int sort );
41private:
42 int compareItems( const OPimContact&, const OPimContact& );
43};
44}
45}
46
47
48#endif
diff --git a/libopie2/opiepim/private/opimoccurrence_p.h b/libopie2/opiepim/private/opimoccurrence_p.h
new file mode 100644
index 0000000..3ccd97e
--- a/dev/null
+++ b/libopie2/opiepim/private/opimoccurrence_p.h
@@ -0,0 +1,33 @@
1// Private HEADER DON'T USE IT!!! WE MEAN IT!!!
2//
3
4#ifndef OPIE_PRIVATE_INTERNAL_OCCURRENCE_PIM_H
5#define OPIE_PRIVATE_INTERNAL_OCCURRENCE_PIM_H
6
7#include <qshared.h>
8
9
10namespace Opie{
11
12/*
13 * The Data is shared between Occurrences
14 * across multiple days.
15 */
16/**
17 * @internal
18 *
19 * DO NOT USE. IT IS NOT PART OF THE API
20 */
21struct OPimOccurrence::Data : public QShared {
22 Data() : uid( -1 ), backend( 0l ) {}
23
24 QString summary; // The Summary of this Occurrence
25 QString location; // The location of this Occurrence
26 QString note; // The note of this Occurrence
27 UID uid; // The UID of the Record
28 mutable Opie::Core::OSharedPointer<OPimRecord> record; // The Guarded Record
29 OPimBase *backend;
30};
31}
32
33#endif
diff --git a/libopie2/opiepim/private/opimsortvector.h b/libopie2/opiepim/private/opimsortvector.h
new file mode 100644
index 0000000..6c21339
--- a/dev/null
+++ b/libopie2/opiepim/private/opimsortvector.h
@@ -0,0 +1,138 @@
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#ifndef OPIE_PIM_SORT_VECTOR_H
31#define OPIE_PIM_SORT_VECTOR_H
32
33#include <opie2/opimglobal.h>
34
35#include <qvector.h>
36
37namespace Opie {
38namespace Internal {
39template<class T>
40struct OPimSortVectorContainer {
41 T item;
42};
43
44template<class T>
45class OPimSortVector : public QVector<OPimSortVectorContainer<T> > {
46 typedef OPimSortVectorContainer<T> VectorItem;
47public:
48 OPimSortVector( uint size, bool asc, int sort );
49 int compareItems( QCollection::Item d1, QCollection::Item d2 );
50 bool insert( uint, const T& t );
51 UID uidAt( uint i )const;
52
53protected:
54 int testString( const QString&, const QString& )const;
55 int testDate( const QDate&, const QDate& )const;
56protected:
57 bool sortAscending()const;
58 int sortOrder()const;
59
60private:
61 bool m_ascending : 1;
62 int m_sort;
63 virtual int compareItems( const T& item1, const T& item2 ) = 0;
64};
65
66template<class T>
67OPimSortVector<T>::OPimSortVector( uint size, bool asc, int sort )
68 : QVector<VectorItem>( size ), m_ascending( asc ),
69 m_sort( sort ) {
70 this->setAutoDelete( true );
71}
72
73/**
74 * Returns:
75 * 0 if item1 == item2
76 *
77 * non-zero if item1 != item2
78 *
79 * This function returns int rather than bool so that reimplementations
80 * can return one of three values and use it to sort by:
81 *
82 * 0 if item1 == item2
83 *
84 * > 0 (positive integer) if item1 > item2
85 *
86 * < 0 (negative integer) if item1 < item2
87 *
88 */
89template<class T>
90int OPimSortVector<T>::compareItems( QCollection::Item d1, QCollection::Item d2 ) {
91 return compareItems( ((VectorItem*)d1)->item,
92 ((VectorItem*)d2)->item );
93}
94
95template<class T>
96bool OPimSortVector<T>::sortAscending()const {
97 return m_ascending;
98}
99
100template<class T>
101int OPimSortVector<T>::sortOrder()const {
102 return m_sort;
103}
104
105template<class T>
106bool OPimSortVector<T>::insert( uint i, const T& record ) {
107 VectorItem *item = new VectorItem;
108 item->item = record;
109 return QVector<VectorItem>::insert( i, item );
110}
111
112template<class T>
113UID OPimSortVector<T>::uidAt( uint index )const {
114 return this->at( index )->item.uid();
115}
116
117template<class T>
118inline int OPimSortVector<T>::testString( const QString& left,
119 const QString& right )const {
120 return QString::compare( left, right );
121}
122
123template<class T>
124inline int OPimSortVector<T>::testDate( const QDate& left,
125 const QDate& right )const {
126 int ret = 0;
127 if ( !left .isValid() ) ret++;
128 if ( !right.isValid() ) ret--;
129
130 if ( left.isValid() && right.isValid() )
131 ret += left < right ? -1 : 1;
132
133 return ret;
134}
135}
136}
137
138#endif
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
33namespace Opie {
34namespace Internal {
35
36inline QString string( const OPimTodo& todo) {
37 return todo.summary().isEmpty() ?
38 todo.description().left(20 ) :
39 todo.summary();
40}
41
42inline 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
49inline int priority( const OPimTodo& t1, const OPimTodo& t2) {
50 return ( t1.priority() - t2.priority() );
51}
52
53inline int summary( const OPimTodo& t1, const OPimTodo& t2) {
54 return QString::compare( string(t1), string(t2) );
55}
56
57inline 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
73OPimTodoSortVector::OPimTodoSortVector( uint size, bool asc, int sort )
74 : OPimSortVector<OPimTodo>( size, asc, sort )
75{}
76
77int 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}
diff --git a/libopie2/opiepim/private/opimtodosortvector.h b/libopie2/opiepim/private/opimtodosortvector.h
new file mode 100644
index 0000000..11745ac
--- a/dev/null
+++ b/libopie2/opiepim/private/opimtodosortvector.h
@@ -0,0 +1,48 @@
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#ifndef OPIM_TODO_SORT_VECTOR_H
31#define OPIM_TODO_SORT_VECTOR_H
32
33#include <opie2/opimtodo.h>
34#include <opie2/private/opimsortvector.h>
35
36namespace Opie {
37namespace Internal {
38class OPimTodoSortVector : public OPimSortVector<OPimTodo> {
39public:
40 OPimTodoSortVector( uint size, bool asc, int sort );
41private:
42 int compareItems( const OPimTodo&, const OPimTodo& );
43};
44}
45}
46
47
48#endif
diff --git a/libopie2/opiepim/private/private.pro b/libopie2/opiepim/private/private.pro
new file mode 100644
index 0000000..618c2d6
--- a/dev/null
+++ b/libopie2/opiepim/private/private.pro
@@ -0,0 +1,9 @@
1HEADERS += private/vobject_p.h \
2 private/opimcontactsortvector.h \
3 private/opimoccurrence_p.h \
4 private/opimsortvector.h \
5 private/opimtodosortvector.h
6
7SOURCES += private/opimcontactsortvector.cpp \
8 private/opimtodosortvector.cpp
9