-rw-r--r-- | library/backend/task.cpp | 173 |
1 files changed, 155 insertions, 18 deletions
diff --git a/library/backend/task.cpp b/library/backend/task.cpp index f0a38f1..a00adb3 100644 --- a/library/backend/task.cpp +++ b/library/backend/task.cpp | |||
@@ -1,85 +1,200 @@ | |||
1 | /********************************************************************** | 1 | /********************************************************************** |
2 | ** Copyright (C) 2001 Trolltech AS. All rights reserved. | 2 | ** Copyright (C) 2000-2002 Trolltech AS. All rights reserved. |
3 | ** | 3 | ** |
4 | ** This file is part of Qtopia Environment. | 4 | ** This file is part of the Qtopia Environment. |
5 | ** | 5 | ** |
6 | ** This file may be distributed and/or modified under the terms of the | 6 | ** This file may be distributed and/or modified under the terms of the |
7 | ** GNU General Public License version 2 as published by the Free Software | 7 | ** GNU General Public License version 2 as published by the Free Software |
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | 8 | ** Foundation and appearing in the file LICENSE.GPL included in the |
9 | ** packaging of this file. | 9 | ** packaging of this file. |
10 | ** | 10 | ** |
11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE | 11 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | 12 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
13 | ** | 13 | ** |
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | 14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. |
15 | ** | 15 | ** |
16 | ** Contact info@trolltech.com if any conditions of this licensing are | 16 | ** Contact info@trolltech.com if any conditions of this licensing are |
17 | ** not clear to you. | 17 | ** not clear to you. |
18 | ** | 18 | ** |
19 | **********************************************************************/ | 19 | **********************************************************************/ |
20 | 20 | ||
21 | #include <qpe/task.h> | 21 | #include "task.h" |
22 | #include <qregexp.h> | 22 | #include "recordfields.h" |
23 | #include <qstring.h> | ||
24 | #include <qpe/recordfields.h> | ||
25 | #include "vobject_p.h" | 23 | #include "vobject_p.h" |
26 | #include "timeconversion.h" | ||
27 | #include "qfiledirect_p.h" | 24 | #include "qfiledirect_p.h" |
28 | 25 | ||
26 | #include <qtopia/timeconversion.h> | ||
27 | |||
28 | #include <qregexp.h> | ||
29 | #include <qstring.h> | ||
30 | |||
29 | #include <stdio.h> | 31 | #include <stdio.h> |
30 | 32 | ||
31 | using namespace Qtopia; | 33 | using namespace Qtopia; |
32 | UidGen Task::sUidGen( UidGen::Qtopia ); | 34 | UidGen Task::sUidGen( UidGen::Qtopia ); |
33 | 35 | ||
36 | /*! | ||
37 | \class Task | ||
38 | \brief The Task class holds the data of a todo entry. | ||
39 | |||
40 | This data includes the priority of the task, a description, an optional due | ||
41 | date, and whether the task is completed or not. | ||
42 | |||
43 | \ingroup qtopiaemb | ||
44 | \ingroup qtopiadesktop | ||
45 | */ | ||
46 | |||
47 | /*! | ||
48 | Creates a new, empty task. | ||
49 | */ | ||
34 | Task::Task() : Record(), mDue( FALSE ), | 50 | Task::Task() : Record(), mDue( FALSE ), |
35 | mDueDate( QDate::currentDate() ), | 51 | mDueDate( QDate::currentDate() ), |
36 | mCompleted( FALSE ), mPriority( 3 ), mDesc() | 52 | mCompleted( FALSE ), mPriority( 3 ), mDesc() |
37 | { | 53 | { |
38 | } | 54 | } |
39 | 55 | ||
56 | /*! | ||
57 | \fn void Task::setPriority( int priority ) | ||
58 | |||
59 | Sets the priority of the task to \a priority. | ||
60 | */ | ||
61 | |||
62 | /*! | ||
63 | \fn int Task::priority() const | ||
64 | |||
65 | Returns the priority of the task. | ||
66 | */ | ||
67 | |||
68 | /*! | ||
69 | \fn void Task::setDescription( const QString &description ) | ||
70 | |||
71 | Sets the description of the task to \a description. | ||
72 | */ | ||
73 | |||
74 | /*! | ||
75 | \fn const QString &Task::description() const | ||
76 | |||
77 | Returns the description of the task. | ||
78 | */ | ||
79 | |||
80 | /*! | ||
81 | \fn void Task::setDueDate( const QDate &date, bool hasDue ) | ||
82 | |||
83 | \internal | ||
84 | If \a hasDue is TRUE sets the due date of the task to \a date. | ||
85 | Otherwise clears the due date of the task. | ||
86 | */ | ||
87 | |||
88 | /*! | ||
89 | \fn void Task::setDueDate( const QDate &date ) | ||
90 | |||
91 | Sets the due date of the task to \a date. | ||
92 | */ | ||
93 | |||
94 | /*! | ||
95 | \fn void Task::clearDueDate( ) | ||
96 | |||
97 | Clears the due date of the task. | ||
98 | */ | ||
99 | |||
100 | /*! | ||
101 | \fn void Task::setCompleted( bool b ) | ||
102 | |||
103 | If \a b is TRUE marks the task as completed. Otherwise marks the task as | ||
104 | uncompleted. | ||
105 | */ | ||
106 | |||
107 | /*! | ||
108 | \fn bool Task::isCompleted() const | ||
109 | |||
110 | Returns TRUE if the task is completed. Otherwise returns FALSE. | ||
111 | */ | ||
112 | |||
113 | /*! | ||
114 | \fn const QDate &Task::dueDate() const | ||
115 | |||
116 | Returns the due date of the task. | ||
117 | */ | ||
118 | |||
119 | /*! | ||
120 | \fn bool Task::hasDueDate() const | ||
121 | |||
122 | Returns TRUE if there is a due date set for the task. Otherwise returns | ||
123 | FALSE. | ||
124 | */ | ||
125 | |||
126 | /*! | ||
127 | \fn void Task::setHasDueDate( bool b ) | ||
128 | |||
129 | \internal | ||
130 | Just don't ask. I really can't justify the function. | ||
131 | */ | ||
132 | |||
133 | |||
134 | /*! | ||
135 | \internal | ||
136 | Creates a new task. The properties of the task are set from \a m. | ||
137 | */ | ||
138 | |||
40 | Task::Task( const QMap<int, QString> &m ) : Record(), mDue( FALSE ), | 139 | Task::Task( const QMap<int, QString> &m ) : Record(), mDue( FALSE ), |
41 | mDueDate( QDate::currentDate() ), mCompleted( FALSE ), mPriority( 3 ), mDesc() | 140 | mDueDate( QDate::currentDate() ), mCompleted( FALSE ), mPriority( 3 ), mDesc() |
42 | { | 141 | { |
43 | //qDebug("Task::Task fromMap"); | 142 | //qDebug("Task::Task fromMap"); |
44 | //dump( m ); | 143 | //dump( m ); |
45 | for ( QMap<int,QString>::ConstIterator it = m.begin(); it != m.end();++it ) | 144 | for ( QMap<int,QString>::ConstIterator it = m.begin(); it != m.end();++it ) |
46 | switch ( (TaskFields) it.key() ) { | 145 | switch ( (TaskFields) it.key() ) { |
47 | case HasDate: if ( *it == "1" ) mDue = TRUE; break; | 146 | case HasDate: if ( *it == "1" ) mDue = TRUE; break; |
48 | case Completed: setCompleted( *it == "1" ); break; | 147 | case Completed: setCompleted( *it == "1" ); break; |
49 | case TaskCategory: setCategories( idsFromString( *it ) ); break; | 148 | case TaskCategory: setCategories( idsFromString( *it ) ); break; |
50 | case TaskDescription: setDescription( *it ); break; | 149 | case TaskDescription: setDescription( *it ); break; |
51 | case Priority: setPriority( (*it).toInt() ); break; | 150 | case Priority: setPriority( (*it).toInt() ); break; |
52 | case Date: mDueDate = TimeConversion::fromString( (*it) ); break; | 151 | case Date: mDueDate = TimeConversion::fromString( (*it) ); break; |
53 | case TaskUid: setUid( (*it).toInt() ); break; | 152 | case TaskUid: setUid( (*it).toInt() ); break; |
54 | default: break; | 153 | case TaskRid: |
154 | case TaskRinfo: | ||
155 | break; | ||
55 | } | 156 | } |
56 | } | 157 | } |
57 | 158 | ||
159 | /*! | ||
160 | Destroys a task. | ||
161 | */ | ||
58 | Task::~Task() | 162 | Task::~Task() |
59 | { | 163 | { |
60 | } | 164 | } |
61 | 165 | ||
166 | /*! | ||
167 | \internal | ||
168 | Returns the task as a map of field ids to property values. | ||
169 | */ | ||
62 | QMap<int, QString> Task::toMap() const | 170 | QMap<int, QString> Task::toMap() const |
63 | { | 171 | { |
64 | QMap<int, QString> m; | 172 | QMap<int, QString> m; |
65 | m.insert( HasDate, hasDueDate() ? "1" : "0" ); | 173 | m.insert( HasDate, hasDueDate() ? "1" : "0" ); |
66 | m.insert( Completed, isCompleted() ? "1" : "0" ); | 174 | m.insert( Completed, isCompleted() ? "1" : "0" ); |
67 | m.insert( TaskCategory, idsToString( categories() ) ); | 175 | if ( categories().count() ) |
68 | m.insert( TaskDescription, description() ); | 176 | m.insert( TaskCategory, idsToString( categories() ) ); |
177 | if ( !description().isEmpty() ) | ||
178 | m.insert( TaskDescription, description() ); | ||
69 | m.insert( Priority, QString::number( priority() ) ); | 179 | m.insert( Priority, QString::number( priority() ) ); |
70 | m.insert( Date, TimeConversion::toString( dueDate() ) ); | 180 | if ( hasDueDate() ) |
181 | m.insert( Date, TimeConversion::toString( dueDate() ) ); | ||
71 | m.insert( TaskUid, QString::number(uid()) ); | 182 | m.insert( TaskUid, QString::number(uid()) ); |
72 | 183 | ||
73 | //qDebug("Task::toMap"); | 184 | //qDebug("Task::toMap"); |
74 | //dump( m ); | 185 | //dump( m ); |
75 | return m; | 186 | return m; |
76 | } | 187 | } |
77 | 188 | ||
189 | /*! | ||
190 | \internal | ||
191 | Appends the task information to \a buf. | ||
192 | */ | ||
78 | void Task::save( QString& buf ) const | 193 | void Task::save( QString& buf ) const |
79 | { | 194 | { |
80 | buf += " Completed=\""; | 195 | buf += " Completed=\""; |
81 | // qDebug( "writing %d", complete ); | 196 | // qDebug( "writing %d", complete ); |
82 | buf += QString::number( (int)mCompleted ); | 197 | buf += QString::number( (int)mCompleted ); |
83 | buf += "\""; | 198 | buf += "\""; |
84 | buf += " HasDate=\""; | 199 | buf += " HasDate=\""; |
85 | // qDebug( "writing %d", ); | 200 | // qDebug( "writing %d", ); |
@@ -112,64 +227,79 @@ void Task::save( QString& buf ) const | |||
112 | buf += customToXml(); | 227 | buf += customToXml(); |
113 | // qDebug ("writing uid %d", uid() ); | 228 | // qDebug ("writing uid %d", uid() ); |
114 | buf += " Uid=\""; | 229 | buf += " Uid=\""; |
115 | buf += QString::number( uid() ); | 230 | buf += QString::number( uid() ); |
116 | // terminate it in the application... | 231 | // terminate it in the application... |
117 | buf += "\""; | 232 | buf += "\""; |
118 | } | 233 | } |
119 | 234 | ||
120 | bool Task::match ( const QRegExp &r ) const | 235 | /*! |
236 | Returns TRUE if the task matches the regular expressions \a regexp. | ||
237 | Otherwise returns FALSE. | ||
238 | */ | ||
239 | bool Task::match ( const QRegExp ®exp ) const | ||
121 | { | 240 | { |
122 | // match on priority, description on due date... | 241 | // match on priority, description on due date... |
123 | bool match; | 242 | bool match; |
124 | match = false; | 243 | match = false; |
125 | if ( QString::number( mPriority ).find( r ) > -1 ) | 244 | if ( QString::number( mPriority ).find( regexp ) > -1 ) |
126 | match = true; | 245 | match = true; |
127 | else if ( mDue && mDueDate.toString().find( r ) > -1 ) | 246 | else if ( mDue && mDueDate.toString().find( regexp ) > -1 ) |
128 | match = true; | 247 | match = true; |
129 | else if ( mDesc.find( r ) > -1 ) | 248 | else if ( mDesc.find( regexp ) > -1 ) |
130 | match = true; | 249 | match = true; |
131 | return match; | 250 | return match; |
132 | } | 251 | } |
133 | 252 | ||
253 | /*! | ||
254 | \internal | ||
255 | */ | ||
134 | static inline VObject *safeAddPropValue( VObject *o, const char *prop, const QString &value ) | 256 | static inline VObject *safeAddPropValue( VObject *o, const char *prop, const QString &value ) |
135 | { | 257 | { |
136 | VObject *ret = 0; | 258 | VObject *ret = 0; |
137 | if ( o && !value.isEmpty() ) | 259 | if ( o && !value.isEmpty() ) |
138 | ret = addPropValue( o, prop, value.latin1() ); | 260 | ret = addPropValue( o, prop, value.latin1() ); |
139 | return ret; | 261 | return ret; |
140 | } | 262 | } |
141 | 263 | ||
264 | /*! | ||
265 | \internal | ||
266 | */ | ||
142 | static inline VObject *safeAddProp( VObject *o, const char *prop) | 267 | static inline VObject *safeAddProp( VObject *o, const char *prop) |
143 | { | 268 | { |
144 | VObject *ret = 0; | 269 | VObject *ret = 0; |
145 | if ( o ) | 270 | if ( o ) |
146 | ret = addProp( o, prop ); | 271 | ret = addProp( o, prop ); |
147 | return ret; | 272 | return ret; |
148 | } | 273 | } |
149 | 274 | ||
150 | 275 | ||
276 | /*! | ||
277 | \internal | ||
278 | */ | ||
151 | static VObject *createVObject( const Task &t ) | 279 | static VObject *createVObject( const Task &t ) |
152 | { | 280 | { |
153 | VObject *vcal = newVObject( VCCalProp ); | 281 | VObject *vcal = newVObject( VCCalProp ); |
154 | safeAddPropValue( vcal, VCVersionProp, "1.0" ); | 282 | safeAddPropValue( vcal, VCVersionProp, "1.0" ); |
155 | VObject *task = safeAddProp( vcal, VCTodoProp ); | 283 | VObject *task = safeAddProp( vcal, VCTodoProp ); |
156 | 284 | ||
157 | if ( t.hasDueDate() ) | 285 | if ( t.hasDueDate() ) |
158 | safeAddPropValue( task, VCDueProp, TimeConversion::toISO8601( t.dueDate() ) ); | 286 | safeAddPropValue( task, VCDueProp, TimeConversion::toISO8601( t.dueDate() ) ); |
159 | safeAddPropValue( task, VCDescriptionProp, t.description() ); | 287 | safeAddPropValue( task, VCDescriptionProp, t.description() ); |
160 | if ( t.isCompleted() ) | 288 | if ( t.isCompleted() ) |
161 | safeAddPropValue( task, VCStatusProp, "COMPLETED" ); | 289 | safeAddPropValue( task, VCStatusProp, "COMPLETED" ); |
162 | safeAddPropValue( task, VCPriorityProp, QString::number( t.priority() ) ); | 290 | safeAddPropValue( task, VCPriorityProp, QString::number( t.priority() ) ); |
163 | 291 | ||
164 | return vcal; | 292 | return vcal; |
165 | } | 293 | } |
166 | 294 | ||
167 | 295 | /*! | |
296 | \internal | ||
297 | */ | ||
168 | static Task parseVObject( VObject *obj ) | 298 | static Task parseVObject( VObject *obj ) |
169 | { | 299 | { |
170 | Task t; | 300 | Task t; |
171 | 301 | ||
172 | VObjectIterator it; | 302 | VObjectIterator it; |
173 | initPropIterator( &it, obj ); | 303 | initPropIterator( &it, obj ); |
174 | while( moreIteration( &it ) ) { | 304 | while( moreIteration( &it ) ) { |
175 | VObject *o = nextVObject( &it ); | 305 | VObject *o = nextVObject( &it ); |
@@ -202,17 +332,19 @@ static Task parseVObject( VObject *obj ) | |||
202 | } | 332 | } |
203 | #endif | 333 | #endif |
204 | } | 334 | } |
205 | 335 | ||
206 | return t; | 336 | return t; |
207 | } | 337 | } |
208 | 338 | ||
209 | 339 | ||
210 | 340 | /*! | |
341 | Writes the list of \a tasks as a set of VCards to the file \a filename. | ||
342 | */ | ||
211 | void Task::writeVCalendar( const QString &filename, const QValueList<Task> &tasks) | 343 | void Task::writeVCalendar( const QString &filename, const QValueList<Task> &tasks) |
212 | { | 344 | { |
213 | QFileDirect f( filename.utf8().data() ); | 345 | QFileDirect f( filename.utf8().data() ); |
214 | if ( !f.open( IO_WriteOnly ) ) { | 346 | if ( !f.open( IO_WriteOnly ) ) { |
215 | qWarning("Unable to open vcard write"); | 347 | qWarning("Unable to open vcard write"); |
216 | return; | 348 | return; |
217 | } | 349 | } |
218 | 350 | ||
@@ -221,32 +353,37 @@ void Task::writeVCalendar( const QString &filename, const QValueList<Task> &task | |||
221 | VObject *obj = createVObject( *it ); | 353 | VObject *obj = createVObject( *it ); |
222 | writeVObject(f.directHandle() , obj ); | 354 | writeVObject(f.directHandle() , obj ); |
223 | cleanVObject( obj ); | 355 | cleanVObject( obj ); |
224 | } | 356 | } |
225 | 357 | ||
226 | cleanStrTbl(); | 358 | cleanStrTbl(); |
227 | } | 359 | } |
228 | 360 | ||
361 | /*! | ||
362 | Writes \a task as a VCard to the file \a filename. | ||
363 | */ | ||
229 | void Task::writeVCalendar( const QString &filename, const Task &task) | 364 | void Task::writeVCalendar( const QString &filename, const Task &task) |
230 | { | 365 | { |
231 | QFileDirect f( filename.utf8().data() ); | 366 | QFileDirect f( filename.utf8().data() ); |
232 | if ( !f.open( IO_WriteOnly ) ) { | 367 | if ( !f.open( IO_WriteOnly ) ) { |
233 | qWarning("Unable to open vcard write"); | 368 | qWarning("Unable to open vcard write"); |
234 | return; | 369 | return; |
235 | } | 370 | } |
236 | 371 | ||
237 | VObject *obj = createVObject( task ); | 372 | VObject *obj = createVObject( task ); |
238 | writeVObject(f.directHandle() , obj ); | 373 | writeVObject(f.directHandle() , obj ); |
239 | cleanVObject( obj ); | 374 | cleanVObject( obj ); |
240 | 375 | ||
241 | cleanStrTbl(); | 376 | cleanStrTbl(); |
242 | } | 377 | } |
243 | 378 | ||
244 | 379 | /*! | |
380 | Returns the set of tasks read as VCards from the file \a filename. | ||
381 | */ | ||
245 | QValueList<Task> Task::readVCalendar( const QString &filename ) | 382 | QValueList<Task> Task::readVCalendar( const QString &filename ) |
246 | { | 383 | { |
247 | VObject *obj = Parse_MIME_FromFileName( (char *)filename.utf8().data() ); | 384 | VObject *obj = Parse_MIME_FromFileName( (char *)filename.utf8().data() ); |
248 | 385 | ||
249 | QValueList<Task> tasks; | 386 | QValueList<Task> tasks; |
250 | 387 | ||
251 | while ( obj ) { | 388 | while ( obj ) { |
252 | QCString name = vObjectName( obj ); | 389 | QCString name = vObjectName( obj ); |