summaryrefslogtreecommitdiff
path: root/libopie/pim
Unidiff
Diffstat (limited to 'libopie/pim') (more/less context) (ignore whitespace changes)
-rw-r--r--libopie/pim/otodoaccesssql.cpp9
-rw-r--r--libopie/pim/otodoaccessvcal.cpp192
-rw-r--r--libopie/pim/otodoaccessvcal.h35
3 files changed, 232 insertions, 4 deletions
diff --git a/libopie/pim/otodoaccesssql.cpp b/libopie/pim/otodoaccesssql.cpp
index ea8b3c9..9ef6b7c 100644
--- a/libopie/pim/otodoaccesssql.cpp
+++ b/libopie/pim/otodoaccesssql.cpp
@@ -1,539 +1,540 @@
1 1
2#include <qdatetime.h> 2#include <qdatetime.h>
3 3
4#include <qpe/global.h> 4#include <qpe/global.h>
5 5
6#include <opie/osqldriver.h> 6#include <opie/osqldriver.h>
7#include <opie/osqlresult.h> 7#include <opie/osqlresult.h>
8#include <opie/osqlmanager.h> 8#include <opie/osqlmanager.h>
9#include <opie/osqlquery.h> 9#include <opie/osqlquery.h>
10 10
11#include "otodoaccesssql.h" 11#include "otodoaccesssql.h"
12 12
13/* 13/*
14 * first some query 14 * first some query
15 * CREATE query 15 * CREATE query
16 * LOAD query 16 * LOAD query
17 * INSERT 17 * INSERT
18 * REMOVE 18 * REMOVE
19 * CLEAR 19 * CLEAR
20 */ 20 */
21namespace { 21namespace {
22 /** 22 /**
23 * CreateQuery for the Todolist Table 23 * CreateQuery for the Todolist Table
24 */ 24 */
25 class CreateQuery : public OSQLQuery { 25 class CreateQuery : public OSQLQuery {
26 public: 26 public:
27 CreateQuery(); 27 CreateQuery();
28 ~CreateQuery(); 28 ~CreateQuery();
29 QString query()const; 29 QString query()const;
30 }; 30 };
31 31
32 /** 32 /**
33 * LoadQuery 33 * LoadQuery
34 * this one queries for all uids 34 * this one queries for all uids
35 */ 35 */
36 class LoadQuery : public OSQLQuery { 36 class LoadQuery : public OSQLQuery {
37 public: 37 public:
38 LoadQuery(); 38 LoadQuery();
39 ~LoadQuery(); 39 ~LoadQuery();
40 QString query()const; 40 QString query()const;
41 }; 41 };
42 42
43 /** 43 /**
44 * inserts/adds a OTodo to the table 44 * inserts/adds a OTodo to the table
45 */ 45 */
46 class InsertQuery : public OSQLQuery { 46 class InsertQuery : public OSQLQuery {
47 public: 47 public:
48 InsertQuery(const OTodo& ); 48 InsertQuery(const OTodo& );
49 ~InsertQuery(); 49 ~InsertQuery();
50 QString query()const; 50 QString query()const;
51 private: 51 private:
52 OTodo m_todo; 52 OTodo m_todo;
53 }; 53 };
54 54
55 /** 55 /**
56 * removes one from the table 56 * removes one from the table
57 */ 57 */
58 class RemoveQuery : public OSQLQuery { 58 class RemoveQuery : public OSQLQuery {
59 public: 59 public:
60 RemoveQuery(int uid ); 60 RemoveQuery(int uid );
61 ~RemoveQuery(); 61 ~RemoveQuery();
62 QString query()const; 62 QString query()const;
63 private: 63 private:
64 int m_uid; 64 int m_uid;
65 }; 65 };
66 66
67 /** 67 /**
68 * Clears (delete) a Table 68 * Clears (delete) a Table
69 */ 69 */
70 class ClearQuery : public OSQLQuery { 70 class ClearQuery : public OSQLQuery {
71 public: 71 public:
72 ClearQuery(); 72 ClearQuery();
73 ~ClearQuery(); 73 ~ClearQuery();
74 QString query()const; 74 QString query()const;
75 75
76 }; 76 };
77 77
78 /** 78 /**
79 * a find query 79 * a find query
80 */ 80 */
81 class FindQuery : public OSQLQuery { 81 class FindQuery : public OSQLQuery {
82 public: 82 public:
83 FindQuery(int uid); 83 FindQuery(int uid);
84 FindQuery(const QArray<int>& ); 84 FindQuery(const QArray<int>& );
85 ~FindQuery(); 85 ~FindQuery();
86 QString query()const; 86 QString query()const;
87 private: 87 private:
88 QString single()const; 88 QString single()const;
89 QString multi()const; 89 QString multi()const;
90 QArray<int> m_uids; 90 QArray<int> m_uids;
91 int m_uid; 91 int m_uid;
92 }; 92 };
93 93
94 /** 94 /**
95 * overdue query 95 * overdue query
96 */ 96 */
97 class OverDueQuery : public OSQLQuery { 97 class OverDueQuery : public OSQLQuery {
98 public: 98 public:
99 OverDueQuery(); 99 OverDueQuery();
100 ~OverDueQuery(); 100 ~OverDueQuery();
101 QString query()const; 101 QString query()const;
102 }; 102 };
103 class EffQuery : public OSQLQuery { 103 class EffQuery : public OSQLQuery {
104 public: 104 public:
105 EffQuery( const QDate&, const QDate&, bool inc ); 105 EffQuery( const QDate&, const QDate&, bool inc );
106 ~EffQuery(); 106 ~EffQuery();
107 QString query()const; 107 QString query()const;
108 private: 108 private:
109 QString with()const; 109 QString with()const;
110 QString out()const; 110 QString out()const;
111 QDate m_start; 111 QDate m_start;
112 QDate m_end; 112 QDate m_end;
113 bool m_inc :1; 113 bool m_inc :1;
114 }; 114 };
115 115
116 116
117 CreateQuery::CreateQuery() : OSQLQuery() {} 117 CreateQuery::CreateQuery() : OSQLQuery() {}
118 CreateQuery::~CreateQuery() {} 118 CreateQuery::~CreateQuery() {}
119 QString CreateQuery::query()const { 119 QString CreateQuery::query()const {
120 QString qu; 120 QString qu;
121 qu += "create table todolist( uid, categories, completed, progress, "; 121 qu += "create table todolist( uid, categories, completed, progress, ";
122 qu += "summary, DueDate, priority, description )"; 122 qu += "summary, DueDate, priority, description )";
123 return qu; 123 return qu;
124 } 124 }
125 125
126 LoadQuery::LoadQuery() : OSQLQuery() {} 126 LoadQuery::LoadQuery() : OSQLQuery() {}
127 LoadQuery::~LoadQuery() {} 127 LoadQuery::~LoadQuery() {}
128 QString LoadQuery::query()const { 128 QString LoadQuery::query()const {
129 QString qu; 129 QString qu;
130 qu += "select distinct uid from todolist"; 130 qu += "select distinct uid from todolist";
131 131
132 return qu; 132 return qu;
133 } 133 }
134 134
135 InsertQuery::InsertQuery( const OTodo& todo ) 135 InsertQuery::InsertQuery( const OTodo& todo )
136 : OSQLQuery(), m_todo( todo ) { 136 : OSQLQuery(), m_todo( todo ) {
137 } 137 }
138 InsertQuery::~InsertQuery() { 138 InsertQuery::~InsertQuery() {
139 } 139 }
140 /* 140 /*
141 * converts from a OTodo to a query 141 * converts from a OTodo to a query
142 * we leave out X-Ref + Alarms 142 * we leave out X-Ref + Alarms
143 */ 143 */
144 QString InsertQuery::query()const{ 144 QString InsertQuery::query()const{
145 145
146 int year, month, day; 146 int year, month, day;
147 year = month = day = 0; 147 year = month = day = 0;
148 if (m_todo.hasDueDate() ) { 148 if (m_todo.hasDueDate() ) {
149 QDate date = m_todo.dueDate(); 149 QDate date = m_todo.dueDate();
150 year = date.year(); 150 year = date.year();
151 month = date.month(); 151 month = date.month();
152 day = date.day(); 152 day = date.day();
153 } 153 }
154 QString qu; 154 QString qu;
155 qu = "insert into todolist VALUES(" + QString::number( m_todo.uid() ) + ",'" + m_todo.idsToString( m_todo.categories() ) + "',"; 155 qu = "insert into todolist VALUES(" + QString::number( m_todo.uid() ) + ",'" + m_todo.idsToString( m_todo.categories() ) + "',";
156 qu += QString::number( m_todo.isCompleted() ) + "," + QString::number( m_todo.progress() ) + ","; 156 qu += QString::number( m_todo.isCompleted() ) + "," + QString::number( m_todo.progress() ) + ",";
157 qu += "'"+m_todo.summary()+"','"+QString::number(year)+"-"+QString::number(month)+"-"+QString::number(day)+"',"; 157 qu += "'"+m_todo.summary()+"','"+QString::number(year)+"-"+QString::number(month)+"-"+QString::number(day)+"',";
158 qu += QString::number(m_todo.priority() ) +",'" + m_todo.description() + "')"; 158 qu += QString::number(m_todo.priority() ) +",'" + m_todo.description() + "')";
159 159
160 qWarning("add %s", qu.latin1() ); 160 qWarning("add %s", qu.latin1() );
161 return qu; 161 return qu;
162 } 162 }
163 163
164 RemoveQuery::RemoveQuery(int uid ) 164 RemoveQuery::RemoveQuery(int uid )
165 : OSQLQuery(), m_uid( uid ) {} 165 : OSQLQuery(), m_uid( uid ) {}
166 RemoveQuery::~RemoveQuery() {} 166 RemoveQuery::~RemoveQuery() {}
167 QString RemoveQuery::query()const { 167 QString RemoveQuery::query()const {
168 QString qu = "DELETE from todolist where uid = " + QString::number(m_uid); 168 QString qu = "DELETE from todolist where uid = " + QString::number(m_uid);
169 return qu; 169 return qu;
170 } 170 }
171 171
172 172
173 ClearQuery::ClearQuery() 173 ClearQuery::ClearQuery()
174 : OSQLQuery() {} 174 : OSQLQuery() {}
175 ClearQuery::~ClearQuery() {} 175 ClearQuery::~ClearQuery() {}
176 QString ClearQuery::query()const { 176 QString ClearQuery::query()const {
177 QString qu = "drop table todolist"; 177 QString qu = "drop table todolist";
178 return qu; 178 return qu;
179 } 179 }
180 FindQuery::FindQuery(int uid) 180 FindQuery::FindQuery(int uid)
181 : OSQLQuery(), m_uid(uid ) { 181 : OSQLQuery(), m_uid(uid ) {
182 } 182 }
183 FindQuery::FindQuery(const QArray<int>& ints) 183 FindQuery::FindQuery(const QArray<int>& ints)
184 : OSQLQuery(), m_uids(ints){ 184 : OSQLQuery(), m_uids(ints){
185 } 185 }
186 FindQuery::~FindQuery() { 186 FindQuery::~FindQuery() {
187 } 187 }
188 QString FindQuery::query()const{ 188 QString FindQuery::query()const{
189 if (m_uids.count() == 0 ) 189 if (m_uids.count() == 0 )
190 return single(); 190 return single();
191 else 191 else
192 return multi(); 192 return multi();
193 } 193 }
194 QString FindQuery::single()const{ 194 QString FindQuery::single()const{
195 QString qu = "select uid, categories, completed, progress, summary, "; 195 QString qu = "select uid, categories, completed, progress, summary, ";
196 qu += "DueDate, priority, description from todolist where uid = " + QString::number(m_uid); 196 qu += "DueDate, priority, description from todolist where uid = " + QString::number(m_uid);
197 return qu; 197 return qu;
198 } 198 }
199 QString FindQuery::multi()const { 199 QString FindQuery::multi()const {
200 QString qu = "select uid, categories, completed, progress, summary, "; 200 QString qu = "select uid, categories, completed, progress, summary, ";
201 qu += "DueDate, priority, description from todolist where "; 201 qu += "DueDate, priority, description from todolist where ";
202 for (uint i = 0; i < m_uids.count(); i++ ) { 202 for (uint i = 0; i < m_uids.count(); i++ ) {
203 qu += " UID = " + QString::number( m_uids[i] ) + " OR"; 203 qu += " UID = " + QString::number( m_uids[i] ) + " OR";
204 } 204 }
205 qu.remove( qu.length()-2, 2 ); 205 qu.remove( qu.length()-2, 2 );
206 return qu; 206 return qu;
207 } 207 }
208 208
209 OverDueQuery::OverDueQuery(): OSQLQuery() {} 209 OverDueQuery::OverDueQuery(): OSQLQuery() {}
210 OverDueQuery::~OverDueQuery() {} 210 OverDueQuery::~OverDueQuery() {}
211 QString OverDueQuery::query()const { 211 QString OverDueQuery::query()const {
212 QDate date = QDate::currentDate(); 212 QDate date = QDate::currentDate();
213 QString str; 213 QString str;
214 str = QString("select uid from todolist where DueDate ='%1-%2-%3'").arg(date.year() ).arg(date.month() ).arg(date.day() ); 214 str = QString("select uid from todolist where DueDate ='%1-%2-%3'").arg(date.year() ).arg(date.month() ).arg(date.day() );
215 215
216 return str; 216 return str;
217 } 217 }
218 218
219 219
220 EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc ) 220 EffQuery::EffQuery( const QDate& start, const QDate& end, bool inc )
221 : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {} 221 : OSQLQuery(), m_start( start ), m_end( end ),m_inc(inc) {}
222 EffQuery::~EffQuery() {} 222 EffQuery::~EffQuery() {}
223 QString EffQuery::query()const { 223 QString EffQuery::query()const {
224 return m_inc ? with() : out(); 224 return m_inc ? with() : out();
225 } 225 }
226 QString EffQuery::with()const { 226 QString EffQuery::with()const {
227 QString str; 227 QString str;
228 str = QString("select uid from todolist where ( DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6' ) OR DueDate = '0-0-0' ") 228 str = QString("select uid from todolist where ( DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6' ) OR DueDate = '0-0-0' ")
229 .arg( m_start.year() ).arg( m_start.month() ).arg( m_start.day() ) 229 .arg( m_start.year() ).arg( m_start.month() ).arg( m_start.day() )
230 .arg( m_end .year() ).arg( m_end .month() ).arg( m_end .day() ); 230 .arg( m_end .year() ).arg( m_end .month() ).arg( m_end .day() );
231 return str; 231 return str;
232 } 232 }
233 QString EffQuery::out()const { 233 QString EffQuery::out()const {
234 QString str; 234 QString str;
235 str = QString("select uid from todolist where DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6'") 235 str = QString("select uid from todolist where DueDate >= '%1-%2-%3' AND DueDate <= '%4-%5-%6'")
236 .arg(m_start.year() ).arg(m_start.month() ).arg( m_start.day() ) 236 .arg(m_start.year() ).arg(m_start.month() ).arg( m_start.day() )
237 .arg(m_end. year() ).arg(m_end. month() ).arg(m_end.day() ); 237 .arg(m_end. year() ).arg(m_end. month() ).arg(m_end.day() );
238 238
239 return str; 239 return str;
240 } 240 }
241}; 241};
242 242
243OTodoAccessBackendSQL::OTodoAccessBackendSQL( const QString& file ) 243OTodoAccessBackendSQL::OTodoAccessBackendSQL( const QString& file )
244 : OTodoAccessBackend(), m_dict(15), m_dirty(true) 244 : OTodoAccessBackend(), m_dict(15), m_dirty(true)
245{ 245{
246 QString fi = file; 246 QString fi = file;
247 if ( fi.isEmpty() ) 247 if ( fi.isEmpty() )
248 fi = Global::applicationFileName( "todolist", "todolist.db" ); 248 fi = Global::applicationFileName( "todolist", "todolist.db" );
249 OSQLManager man; 249 OSQLManager man;
250 m_driver = man.standard(); 250 m_driver = man.standard();
251 m_driver->setUrl(fi); 251 m_driver->setUrl(fi);
252 fillDict(); 252 fillDict();
253} 253}
254 254
255OTodoAccessBackendSQL::~OTodoAccessBackendSQL(){ 255OTodoAccessBackendSQL::~OTodoAccessBackendSQL(){
256} 256}
257bool OTodoAccessBackendSQL::load(){ 257bool OTodoAccessBackendSQL::load(){
258 if (!m_driver->open() ) 258 if (!m_driver->open() )
259 return false; 259 return false;
260 260
261 CreateQuery creat; 261 CreateQuery creat;
262 OSQLResult res = m_driver->query(&creat ); 262 OSQLResult res = m_driver->query(&creat );
263 263
264 m_dirty = true; 264 m_dirty = true;
265 return true; 265 return true;
266} 266}
267bool OTodoAccessBackendSQL::reload(){ 267bool OTodoAccessBackendSQL::reload(){
268 return load(); 268 return load();
269} 269}
270 270
271bool OTodoAccessBackendSQL::save(){ 271bool OTodoAccessBackendSQL::save(){
272 return m_driver->close(); 272 return m_driver->close();
273} 273}
274QArray<int> OTodoAccessBackendSQL::allRecords()const { 274QArray<int> OTodoAccessBackendSQL::allRecords()const {
275 if (m_dirty ) 275 if (m_dirty )
276 update(); 276 update();
277 277
278 return m_uids; 278 return m_uids;
279} 279}
280QArray<int> OTodoAccessBackendSQL::queryByExample( const OTodo& , int ){ 280QArray<int> OTodoAccessBackendSQL::queryByExample( const OTodo& , int ){
281 QArray<int> ints(0); 281 QArray<int> ints(0);
282 return ints; 282 return ints;
283} 283}
284OTodo OTodoAccessBackendSQL::find(int uid ) const{ 284OTodo OTodoAccessBackendSQL::find(int uid ) const{
285 FindQuery query( uid ); 285 FindQuery query( uid );
286 return todo( m_driver->query(&query) ); 286 return todo( m_driver->query(&query) );
287 287
288} 288}
289#define CACHE 32
289OTodo OTodoAccessBackendSQL::find( int uid, const QArray<int>& ints, 290OTodo OTodoAccessBackendSQL::find( int uid, const QArray<int>& ints,
290 uint cur, Frontend::CacheDirection dir ) const{ 291 uint cur, Frontend::CacheDirection dir ) const{
291 qWarning("searching for %d", uid ); 292 qWarning("searching for %d", uid );
292 QArray<int> search( 8 ); 293 QArray<int> search( CACHE );
293 uint size =0; 294 uint size =0;
294 OTodo to; 295 OTodo to;
295 296
296 // we try to cache 8 items 297 // we try to cache CACHE items
297 switch( dir ) { 298 switch( dir ) {
298 /* forward */ 299 /* forward */
299 case 0: 300 case 0:
300 for (uint i = cur; i < ints.count() && size < 8; i++ ) { 301 for (uint i = cur; i < ints.count() && size < CACHE; i++ ) {
301 qWarning("size %d %d", size, ints[i] ); 302 qWarning("size %d %d", size, ints[i] );
302 search[size] = ints[i]; 303 search[size] = ints[i];
303 size++; 304 size++;
304 } 305 }
305 break; 306 break;
306 /* reverse */ 307 /* reverse */
307 case 1: 308 case 1:
308 for (uint i = cur; i != 0 && size < 8; i-- ) { 309 for (uint i = cur; i != 0 && size < CACHE; i-- ) {
309 search[size] = ints[i]; 310 search[size] = ints[i];
310 size++; 311 size++;
311 } 312 }
312 break; 313 break;
313 } 314 }
314 search.resize( size ); 315 search.resize( size );
315 FindQuery query( search ); 316 FindQuery query( search );
316 OSQLResult res = m_driver->query( &query ); 317 OSQLResult res = m_driver->query( &query );
317 if ( res.state() != OSQLResult::Success ) 318 if ( res.state() != OSQLResult::Success )
318 return to; 319 return to;
319 320
320 return todo( res ); 321 return todo( res );
321} 322}
322void OTodoAccessBackendSQL::clear() { 323void OTodoAccessBackendSQL::clear() {
323 ClearQuery cle; 324 ClearQuery cle;
324 OSQLResult res = m_driver->query( &cle ); 325 OSQLResult res = m_driver->query( &cle );
325 CreateQuery qu; 326 CreateQuery qu;
326 res = m_driver->query(&qu); 327 res = m_driver->query(&qu);
327} 328}
328bool OTodoAccessBackendSQL::add( const OTodo& t) { 329bool OTodoAccessBackendSQL::add( const OTodo& t) {
329 InsertQuery ins( t ); 330 InsertQuery ins( t );
330 OSQLResult res = m_driver->query( &ins ); 331 OSQLResult res = m_driver->query( &ins );
331 332
332 if ( res.state() == OSQLResult::Failure ) 333 if ( res.state() == OSQLResult::Failure )
333 return false; 334 return false;
334 int c = m_uids.count(); 335 int c = m_uids.count();
335 m_uids.resize( c+1 ); 336 m_uids.resize( c+1 );
336 m_uids[c] = t.uid(); 337 m_uids[c] = t.uid();
337 338
338 return true; 339 return true;
339} 340}
340bool OTodoAccessBackendSQL::remove( int uid ) { 341bool OTodoAccessBackendSQL::remove( int uid ) {
341 RemoveQuery rem( uid ); 342 RemoveQuery rem( uid );
342 OSQLResult res = m_driver->query(&rem ); 343 OSQLResult res = m_driver->query(&rem );
343 344
344 if ( res.state() == OSQLResult::Failure ) 345 if ( res.state() == OSQLResult::Failure )
345 return false; 346 return false;
346 347
347 m_dirty = true; 348 m_dirty = true;
348 return true; 349 return true;
349} 350}
350/* 351/*
351 * FIXME better set query 352 * FIXME better set query
352 * but we need the cache for that 353 * but we need the cache for that
353 * now we remove 354 * now we remove
354 */ 355 */
355bool OTodoAccessBackendSQL::replace( const OTodo& t) { 356bool OTodoAccessBackendSQL::replace( const OTodo& t) {
356 remove( t.uid() ); 357 remove( t.uid() );
357 bool b= add(t); 358 bool b= add(t);
358 m_dirty = false; // we changed some stuff but the UID stayed the same 359 m_dirty = false; // we changed some stuff but the UID stayed the same
359 return b; 360 return b;
360} 361}
361QArray<int> OTodoAccessBackendSQL::overDue() { 362QArray<int> OTodoAccessBackendSQL::overDue() {
362 OverDueQuery qu; 363 OverDueQuery qu;
363 return uids( m_driver->query(&qu ) ); 364 return uids( m_driver->query(&qu ) );
364} 365}
365QArray<int> OTodoAccessBackendSQL::effectiveToDos( const QDate& s, 366QArray<int> OTodoAccessBackendSQL::effectiveToDos( const QDate& s,
366 const QDate& t, 367 const QDate& t,
367 bool u) { 368 bool u) {
368 EffQuery ef(s, t, u ); 369 EffQuery ef(s, t, u );
369 return uids (m_driver->query(&ef) ); 370 return uids (m_driver->query(&ef) );
370} 371}
371/* 372/*
372 * 373 *
373 */ 374 */
374QArray<int> OTodoAccessBackendSQL::sorted( bool asc, int sortOrder, 375QArray<int> OTodoAccessBackendSQL::sorted( bool asc, int sortOrder,
375 int sortFilter, int cat ) { 376 int sortFilter, int cat ) {
376 qWarning("sorted %d, %d", asc, sortOrder ); 377 qWarning("sorted %d, %d", asc, sortOrder );
377 QString query; 378 QString query;
378 query = "select uid from todolist WHERE "; 379 query = "select uid from todolist WHERE ";
379 380
380 /* 381 /*
381 * Sort Filter stuff 382 * Sort Filter stuff
382 * not that straight forward 383 * not that straight forward
383 * 384 *
384 */ 385 */
385 /* Category */ 386 /* Category */
386 if ( sortFilter & 1 ) { 387 if ( sortFilter & 1 ) {
387 QString str; 388 QString str;
388 if (cat != 0 ) str = QString::number( cat ); 389 if (cat != 0 ) str = QString::number( cat );
389 query += " categories like '%" +str+"%' AND"; 390 query += " categories like '%" +str+"%' AND";
390 } 391 }
391 /* Show only overdue */ 392 /* Show only overdue */
392 if ( sortFilter & 2 ) { 393 if ( sortFilter & 2 ) {
393 QDate date = QDate::currentDate(); 394 QDate date = QDate::currentDate();
394 QString due; 395 QString due;
395 QString base; 396 QString base;
396 base = QString("DueDate <= '%1-%2-%3' AND completed = 0").arg( date.year() ).arg( date.month() ).arg( date.day() ); 397 base = QString("DueDate <= '%1-%2-%3' AND completed = 0").arg( date.year() ).arg( date.month() ).arg( date.day() );
397 query += " " + base + " AND"; 398 query += " " + base + " AND";
398 } 399 }
399 /* not show completed */ 400 /* not show completed */
400 if ( sortFilter & 4 ) { 401 if ( sortFilter & 4 ) {
401 query += " completed = 0 AND"; 402 query += " completed = 0 AND";
402 }else{ 403 }else{
403 query += " ( completed = 1 OR completed = 0) AND"; 404 query += " ( completed = 1 OR completed = 0) AND";
404 } 405 }
405 /* srtip the end */ 406 /* srtip the end */
406 query = query.remove( query.length()-3, 3 ); 407 query = query.remove( query.length()-3, 3 );
407 408
408 409
409 /* 410 /*
410 * sort order stuff 411 * sort order stuff
411 * quite straight forward 412 * quite straight forward
412 */ 413 */
413 query += "ORDER BY "; 414 query += "ORDER BY ";
414 switch( sortOrder ) { 415 switch( sortOrder ) {
415 /* completed */ 416 /* completed */
416 case 0: 417 case 0:
417 query += "completed"; 418 query += "completed";
418 break; 419 break;
419 case 1: 420 case 1:
420 query += "priority"; 421 query += "priority";
421 break; 422 break;
422 case 2: 423 case 2:
423 query += "description"; 424 query += "description";
424 break; 425 break;
425 case 3: 426 case 3:
426 query += "DueDate"; 427 query += "DueDate";
427 break; 428 break;
428 } 429 }
429 430
430 if ( !asc ) { 431 if ( !asc ) {
431 qWarning("not ascending!"); 432 qWarning("not ascending!");
432 query += " DESC"; 433 query += " DESC";
433 } 434 }
434 435
435 qWarning( query ); 436 qWarning( query );
436 OSQLRawQuery raw(query ); 437 OSQLRawQuery raw(query );
437 return uids( m_driver->query(&raw) ); 438 return uids( m_driver->query(&raw) );
438} 439}
439bool OTodoAccessBackendSQL::date( QDate& da, const QString& str ) const{ 440bool OTodoAccessBackendSQL::date( QDate& da, const QString& str ) const{
440 if ( str == "0-0-0" ) 441 if ( str == "0-0-0" )
441 return false; 442 return false;
442 else{ 443 else{
443 int day, year, month; 444 int day, year, month;
444 QStringList list = QStringList::split("-", str ); 445 QStringList list = QStringList::split("-", str );
445 year = list[0].toInt(); 446 year = list[0].toInt();
446 month = list[1].toInt(); 447 month = list[1].toInt();
447 day = list[2].toInt(); 448 day = list[2].toInt();
448 da.setYMD( year, month, day ); 449 da.setYMD( year, month, day );
449 return true; 450 return true;
450 } 451 }
451} 452}
452OTodo OTodoAccessBackendSQL::todo( const OSQLResult& res) const{ 453OTodo OTodoAccessBackendSQL::todo( const OSQLResult& res) const{
453 if ( res.state() == OSQLResult::Failure ) { 454 if ( res.state() == OSQLResult::Failure ) {
454 OTodo to; 455 OTodo to;
455 return to; 456 return to;
456 } 457 }
457 458
458 OSQLResultItem::ValueList list = res.results(); 459 OSQLResultItem::ValueList list = res.results();
459 OSQLResultItem::ValueList::Iterator it = list.begin(); 460 OSQLResultItem::ValueList::Iterator it = list.begin();
460 qWarning("todo1"); 461 qWarning("todo1");
461 OTodo to = todo( (*it) ); 462 OTodo to = todo( (*it) );
462 cache( to ); 463 cache( to );
463 ++it; 464 ++it;
464 465
465 for ( ; it != list.end(); ++it ) { 466 for ( ; it != list.end(); ++it ) {
466 qWarning("caching"); 467 qWarning("caching");
467 cache( todo( (*it) ) ); 468 cache( todo( (*it) ) );
468 } 469 }
469 return to; 470 return to;
470} 471}
471OTodo OTodoAccessBackendSQL::todo( OSQLResultItem& item )const { 472OTodo OTodoAccessBackendSQL::todo( OSQLResultItem& item )const {
472 qWarning("todo"); 473 qWarning("todo");
473 bool has = false; QDate da = QDate::currentDate(); 474 bool has = false; QDate da = QDate::currentDate();
474 has = date( da, item.data("DueDate") ); 475 has = date( da, item.data("DueDate") );
475 QStringList cats = QStringList::split(";", item.data("categories") ); 476 QStringList cats = QStringList::split(";", item.data("categories") );
476 477
477 OTodo to( (bool)item.data("completed").toInt(), item.data("priority").toInt(), 478 OTodo to( (bool)item.data("completed").toInt(), item.data("priority").toInt(),
478 cats, item.data("summary"), item.data("description"), 479 cats, item.data("summary"), item.data("description"),
479 item.data("progress").toUShort(), has, da, 480 item.data("progress").toUShort(), has, da,
480 item.data("uid").toInt() ); 481 item.data("uid").toInt() );
481 return to; 482 return to;
482} 483}
483OTodo OTodoAccessBackendSQL::todo( int uid )const { 484OTodo OTodoAccessBackendSQL::todo( int uid )const {
484 FindQuery find( uid ); 485 FindQuery find( uid );
485 return todo( m_driver->query(&find) ); 486 return todo( m_driver->query(&find) );
486} 487}
487/* 488/*
488 * update the dict 489 * update the dict
489 */ 490 */
490void OTodoAccessBackendSQL::fillDict() { 491void OTodoAccessBackendSQL::fillDict() {
491 /* initialize dict */ 492 /* initialize dict */
492 /* 493 /*
493 * UPDATE dict if you change anything!!! 494 * UPDATE dict if you change anything!!!
494 */ 495 */
495 m_dict.setAutoDelete( TRUE ); 496 m_dict.setAutoDelete( TRUE );
496 m_dict.insert("Categories" , new int(OTodo::Category) ); 497 m_dict.insert("Categories" , new int(OTodo::Category) );
497 m_dict.insert("Uid" , new int(OTodo::Uid) ); 498 m_dict.insert("Uid" , new int(OTodo::Uid) );
498 m_dict.insert("HasDate" , new int(OTodo::HasDate) ); 499 m_dict.insert("HasDate" , new int(OTodo::HasDate) );
499 m_dict.insert("Completed" , new int(OTodo::Completed) ); 500 m_dict.insert("Completed" , new int(OTodo::Completed) );
500 m_dict.insert("Description" , new int(OTodo::Description) ); 501 m_dict.insert("Description" , new int(OTodo::Description) );
501 m_dict.insert("Summary" , new int(OTodo::Summary) ); 502 m_dict.insert("Summary" , new int(OTodo::Summary) );
502 m_dict.insert("Priority" , new int(OTodo::Priority) ); 503 m_dict.insert("Priority" , new int(OTodo::Priority) );
503 m_dict.insert("DateDay" , new int(OTodo::DateDay) ); 504 m_dict.insert("DateDay" , new int(OTodo::DateDay) );
504 m_dict.insert("DateMonth" , new int(OTodo::DateMonth) ); 505 m_dict.insert("DateMonth" , new int(OTodo::DateMonth) );
505 m_dict.insert("DateYear" , new int(OTodo::DateYear) ); 506 m_dict.insert("DateYear" , new int(OTodo::DateYear) );
506 m_dict.insert("Progress" , new int(OTodo::Progress) ); 507 m_dict.insert("Progress" , new int(OTodo::Progress) );
507 m_dict.insert("Completed", new int(OTodo::Completed) ); 508 m_dict.insert("Completed", new int(OTodo::Completed) );
508 m_dict.insert("CrossReference", new int(OTodo::CrossReference) ); 509 m_dict.insert("CrossReference", new int(OTodo::CrossReference) );
509 m_dict.insert("HasAlarmDateTime",new int(OTodo::HasAlarmDateTime) ); 510 m_dict.insert("HasAlarmDateTime",new int(OTodo::HasAlarmDateTime) );
510 m_dict.insert("AlarmDateTime", new int(OTodo::AlarmDateTime) ); 511 m_dict.insert("AlarmDateTime", new int(OTodo::AlarmDateTime) );
511} 512}
512/* 513/*
513 * need to be const so let's fool the 514 * need to be const so let's fool the
514 * compiler :( 515 * compiler :(
515 */ 516 */
516void OTodoAccessBackendSQL::update()const { 517void OTodoAccessBackendSQL::update()const {
517 ((OTodoAccessBackendSQL*)this)->m_dirty = false; 518 ((OTodoAccessBackendSQL*)this)->m_dirty = false;
518 LoadQuery lo; 519 LoadQuery lo;
519 OSQLResult res = m_driver->query(&lo); 520 OSQLResult res = m_driver->query(&lo);
520 if ( res.state() != OSQLResult::Success ) 521 if ( res.state() != OSQLResult::Success )
521 return; 522 return;
522 523
523 ((OTodoAccessBackendSQL*)this)->m_uids = uids( res ); 524 ((OTodoAccessBackendSQL*)this)->m_uids = uids( res );
524} 525}
525QArray<int> OTodoAccessBackendSQL::uids( const OSQLResult& res) const{ 526QArray<int> OTodoAccessBackendSQL::uids( const OSQLResult& res) const{
526 527
527 OSQLResultItem::ValueList list = res.results(); 528 OSQLResultItem::ValueList list = res.results();
528 OSQLResultItem::ValueList::Iterator it; 529 OSQLResultItem::ValueList::Iterator it;
529 QArray<int> ints(list.count() ); 530 QArray<int> ints(list.count() );
530 qWarning(" count = %d", list.count() ); 531 qWarning(" count = %d", list.count() );
531 532
532 int i = 0; 533 int i = 0;
533 for (it = list.begin(); it != list.end(); ++it ) { 534 for (it = list.begin(); it != list.end(); ++it ) {
534 ints[i] = (*it).data("uid").toInt(); 535 ints[i] = (*it).data("uid").toInt();
535 i++; 536 i++;
536 } 537 }
537 return ints; 538 return ints;
538} 539}
539 540
diff --git a/libopie/pim/otodoaccessvcal.cpp b/libopie/pim/otodoaccessvcal.cpp
new file mode 100644
index 0000000..ac70ea0
--- a/dev/null
+++ b/libopie/pim/otodoaccessvcal.cpp
@@ -0,0 +1,192 @@
1#include <qfile.h>
2
3#include <qtopia/private/vobject_p.h>
4#include <qtopia/timeconversion.h>
5#include <qtopia/private/qfiledirect_p.h>
6
7#include "otodoaccessvcal.h"
8
9namespace {
10 static OTodo eventByVObj( VObject *obj ){
11 OTodo event;
12 VObject *ob;
13 QCString name;
14 // no uid, attendees, ... and no fun
15 // description
16 if( ( ob = isAPropertyOf( obj, VCDescriptionProp )) != 0 ){
17 name = vObjectStringZValue( ob );
18 event.setDescription( name );
19 }
20 // summary
21 if ( ( ob = isAPropertyOf( obj, VCSummaryProp ) ) != 0 ) {
22 name = vObjectStringZValue( ob );
23 event.setSummary( name );
24 }
25 // completed
26 if( ( ob = isAPropertyOf( obj, VCStatusProp )) != 0 ){
27 name = vObjectStringZValue( ob );
28 if( name == "COMPLETED" ){
29 event.setCompleted( true );
30 }else{
31 event.setCompleted( false );
32 }
33 }else
34 event.setCompleted( false );
35 // priority
36 if ((ob = isAPropertyOf(obj, VCPriorityProp))) {
37 name = vObjectStringZValue( ob );
38 bool ok;
39 event.setPriority(name.toInt(&ok) );
40 }
41 //due date
42 if((ob = isAPropertyOf(obj, VCDueProp)) ){
43 event.setHasDueDate( true );
44 name = vObjectStringZValue( ob );
45 event.setDueDate( TimeConversion::fromISO8601( name).date() );
46 }
47 // categories
48 if((ob = isAPropertyOf( obj, VCCategoriesProp )) != 0 ){
49 name = vObjectStringZValue( ob );
50 qWarning("Categories:%s", name.data() );
51 }
52
53 event.setUid( 1 );
54 return event;
55 };
56 static VObject *vobjByEvent( const OTodo &event ) {
57 VObject *task = newVObject( VCTodoProp );
58 if( task == 0 )
59 return 0l;
60
61 if( event.hasDueDate() )
62 addPropValue( task, VCDueProp,
63 TimeConversion::toISO8601( event.dueDate() ) );
64
65 if( event.isCompleted() )
66 addPropValue( task, VCStatusProp, "COMPLETED");
67
68 QString string = QString::number(event.priority() );
69 addPropValue( task, VCPriorityProp, string.local8Bit() );
70
71 addPropValue( task, VCCategoriesProp,
72 event.idsToString( event.categories() ).local8Bit() );
73
74 addPropValue( task, VCDescriptionProp,
75 event.description().local8Bit() );
76
77 addPropValue( task, VCSummaryProp,
78 event.summary().local8Bit() );
79 return task;
80};
81}
82
83OTodoAccessVCal::OTodoAccessVCal( const QString& path )
84 : m_dirty(false), m_file( path )
85{
86}
87OTodoAccessVCal::~OTodoAccessVCal() {
88}
89bool OTodoAccessVCal::load() {
90 m_map.clear();
91 m_dirty = false;
92
93 VObject* vcal = 0l;
94 vcal = Parse_MIME_FromFileName( QFile::encodeName(m_file).data() );
95 if (!vcal )
96 return false;
97
98 // Iterate over the list
99 VObjectIterator it;
100 VObject* vobj;
101
102 initPropIterator(&it, vcal);
103
104 while( moreIteration( &it ) ) {
105 vobj = ::nextVObject( &it );
106 QCString name = ::vObjectName( vobj );
107 if( name == VCTodoProp ){
108 OTodo to = eventByVObj( vobj );
109 m_map.insert( to.uid(), to );
110 }
111 }
112
113 // Should I do a delete vcal?
114
115 return true;
116}
117bool OTodoAccessVCal::reload() {
118 return load();
119}
120bool OTodoAccessVCal::save() {
121 if (!m_dirty )
122 return true;
123
124 QFileDirect file( m_file );
125 if (!file.open(IO_WriteOnly ) )
126 return false;
127
128 VObject *obj;
129 obj = newVObject( VCCalProp );
130 addPropValue( obj, VCVersionProp, "1.0" );
131 VObject *vo;
132 for(QMap<int, OTodo>::ConstIterator it=m_map.begin(); it !=m_map.end(); ++it ){
133 vo = vobjByEvent( it.data() );
134 addVObjectProp(obj, vo );
135 }
136 writeVObject( file.directHandle(), obj );
137 cleanVObject( obj );
138 cleanStrTbl();
139
140 m_dirty = false;
141 return true;
142}
143void OTodoAccessVCal::clear() {
144 m_map.clear();
145 m_dirty = true;
146}
147bool OTodoAccessVCal::add( const OTodo& to ) {
148 m_map.insert( to.uid(), to );
149 m_dirty = true;
150 return true;
151}
152bool OTodoAccessVCal::remove( int uid ) {
153 m_map.remove( uid );
154 m_dirty = true;
155 return true;
156}
157bool OTodoAccessVCal::replace( const OTodo& to ) {
158 m_map.replace( to.uid(), to );
159 m_dirty = true;
160 return true;
161}
162OTodo OTodoAccessVCal::find(int uid )const {
163 return m_map[uid];
164}
165QArray<int> OTodoAccessVCal::sorted( bool, int, int, int ) {
166 QArray<int> ar(0);
167 return ar;
168}
169QArray<int> OTodoAccessVCal::allRecords()const {
170 QArray<int> ar( m_map.count() );
171 QMap<int, OTodo>::ConstIterator it;
172 int i = 0;
173 for ( it = m_map.begin(); it != m_map.end(); ++it ) {
174 ar[i] = it.key();
175 i++;
176 }
177 return ar;
178}
179QArray<int> OTodoAccessVCal::queryByExample( const OTodo&, int ) {
180 QArray<int> ar(0);
181 return ar;
182}
183QArray<int> OTodoAccessVCal::effectiveToDos( const QDate& ,
184 const QDate& ,
185 bool ) {
186 QArray<int> ar(0);
187 return ar;
188}
189QArray<int> OTodoAccessVCal::overDue() {
190 QArray<int> ar(0);
191 return ar;
192}
diff --git a/libopie/pim/otodoaccessvcal.h b/libopie/pim/otodoaccessvcal.h
new file mode 100644
index 0000000..4499a7e
--- a/dev/null
+++ b/libopie/pim/otodoaccessvcal.h
@@ -0,0 +1,35 @@
1#ifndef OPIE_OTODO_ACCESS_VCAL_H
2#define OPIE_OTODO_ACCESS_VCAL_H
3
4#include "otodoaccessbackend.h"
5
6class OTodoAccessVCal : public OTodoAccessBackend {
7public:
8 OTodoAccessVCal(const QString& );
9 ~OTodoAccessVCal();
10
11 bool load();
12 bool reload();
13 bool save();
14
15 QArray<int> allRecords()const;
16 QArray<int> queryByExample( const OTodo& t, int sort );
17 QArray<int> effectiveToDos( const QDate& start,
18 const QDate& end,
19 bool includeNoDates );
20 QArray<int> overDue();
21 QArray<int> sorted( bool asc, int sortOrder, int sortFilter,
22 int cat );
23 OTodo find(int uid)const;
24 void clear();
25 bool add( const OTodo& );
26 bool remove( int uid );
27 bool replace( const OTodo& );
28
29private:
30 bool m_dirty : 1;
31 QString m_file;
32 QMap<int, OTodo> m_map;
33};
34
35#endif