-rw-r--r-- | library/backend/task.cpp | 271 |
1 files changed, 271 insertions, 0 deletions
diff --git a/library/backend/task.cpp b/library/backend/task.cpp new file mode 100644 index 0000000..e7d697d --- a/dev/null +++ b/library/backend/task.cpp | |||
@@ -0,0 +1,271 @@ | |||
1 | /********************************************************************** | ||
2 | ** Copyright (C) 2001 Trolltech AS. All rights reserved. | ||
3 | ** | ||
4 | ** This file is part of Qtopia Environment. | ||
5 | ** | ||
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 | ||
8 | ** Foundation and appearing in the file LICENSE.GPL included in the | ||
9 | ** packaging of this file. | ||
10 | ** | ||
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. | ||
13 | ** | ||
14 | ** See http://www.trolltech.com/gpl/ for GPL licensing information. | ||
15 | ** | ||
16 | ** Contact info@trolltech.com if any conditions of this licensing are | ||
17 | ** not clear to you. | ||
18 | ** | ||
19 | **********************************************************************/ | ||
20 | |||
21 | #include <qpe/task.h> | ||
22 | #include <qregexp.h> | ||
23 | #include <qstring.h> | ||
24 | #include <qpe/recordfields.h> | ||
25 | #include "vobject_p.h" | ||
26 | #include "timeconversion.h" | ||
27 | #include "qfiledirect_p.h" | ||
28 | |||
29 | #include <stdio.h> | ||
30 | |||
31 | using namespace Qtopia; | ||
32 | UidGen Task::sUidGen( UidGen::Qtopia ); | ||
33 | |||
34 | Task::Task() : Record(), mDue( FALSE ), | ||
35 | mDueDate( QDate::currentDate() ), | ||
36 | mCompleted( FALSE ), mPriority( 3 ), mDesc() | ||
37 | { | ||
38 | } | ||
39 | |||
40 | Task::Task( const QMap<int, QString> &m ) : Record(), mDue( FALSE ), | ||
41 | mDueDate( QDate::currentDate() ), mCompleted( FALSE ), mPriority( 3 ), mDesc() | ||
42 | { | ||
43 | //qDebug("Task::Task fromMap"); | ||
44 | //dump( m ); | ||
45 | for ( QMap<int,QString>::ConstIterator it = m.begin(); it != m.end();++it ) | ||
46 | switch ( (TaskFields) it.key() ) { | ||
47 | case HasDate: if ( *it == "1" ) mDue = TRUE; break; | ||
48 | case Completed: setCompleted( *it == "1" ); break; | ||
49 | case TaskCategory: setCategories( idsFromString( *it ) ); break; | ||
50 | case TaskDescription: setDescription( *it ); break; | ||
51 | case Priority: setPriority( (*it).toInt() ); break; | ||
52 | case Date: mDueDate = TimeConversion::fromString( (*it) ); break; | ||
53 | case TaskUid: setUid( (*it).toInt() ); break; | ||
54 | } | ||
55 | } | ||
56 | |||
57 | Task::~Task() | ||
58 | { | ||
59 | } | ||
60 | |||
61 | QMap<int, QString> Task::toMap() const | ||
62 | { | ||
63 | QMap<int, QString> m; | ||
64 | m.insert( HasDate, hasDueDate() ? "1" : "0" ); | ||
65 | m.insert( Completed, isCompleted() ? "1" : "0" ); | ||
66 | m.insert( TaskCategory, idsToString( categories() ) ); | ||
67 | m.insert( TaskDescription, description() ); | ||
68 | m.insert( Priority, QString::number( priority() ) ); | ||
69 | m.insert( Date, TimeConversion::toString( dueDate() ) ); | ||
70 | m.insert( TaskUid, QString::number(uid()) ); | ||
71 | |||
72 | //qDebug("Task::toMap"); | ||
73 | //dump( m ); | ||
74 | return m; | ||
75 | } | ||
76 | |||
77 | void Task::save( QString& buf ) const | ||
78 | { | ||
79 | buf += " Completed=\""; | ||
80 | // qDebug( "writing %d", complete ); | ||
81 | buf += QString::number( (int)mCompleted ); | ||
82 | buf += "\""; | ||
83 | buf += " HasDate=\""; | ||
84 | // qDebug( "writing %d", ); | ||
85 | buf += QString::number( (int)mDue ); | ||
86 | buf += "\""; | ||
87 | buf += " Priority=\""; | ||
88 | // qDebug ("writing %d", prior ); | ||
89 | buf += QString::number( mPriority ); | ||
90 | buf += "\""; | ||
91 | buf += " Categories=\""; | ||
92 | buf += Qtopia::Record::idsToString( categories() ); | ||
93 | buf += "\""; | ||
94 | buf += " Description=\""; | ||
95 | // qDebug( "writing note %s", note.latin1() ); | ||
96 | buf += Qtopia::escapeString( mDesc ); | ||
97 | buf += "\""; | ||
98 | if ( mDue ) { | ||
99 | // qDebug("saving ymd %d %d %d", mDueDate.year(), mDueDate.month(), | ||
100 | // mDueDate.day() ); | ||
101 | buf += " DateYear=\""; | ||
102 | buf += QString::number( mDueDate.year() ); | ||
103 | buf += "\""; | ||
104 | buf += " DateMonth=\""; | ||
105 | buf += QString::number( mDueDate.month() ); | ||
106 | buf += "\""; | ||
107 | buf += " DateDay=\""; | ||
108 | buf += QString::number( mDueDate.day() ); | ||
109 | buf += "\""; | ||
110 | } | ||
111 | buf += customToXml(); | ||
112 | // qDebug ("writing uid %d", uid() ); | ||
113 | buf += " Uid=\""; | ||
114 | buf += QString::number( uid() ); | ||
115 | // terminate it in the application... | ||
116 | buf += "\""; | ||
117 | } | ||
118 | |||
119 | bool Task::match ( const QRegExp &r ) const | ||
120 | { | ||
121 | // match on priority, description on due date... | ||
122 | bool match; | ||
123 | match = false; | ||
124 | if ( QString::number( mPriority ).find( r ) > -1 ) | ||
125 | match = true; | ||
126 | else if ( mDue && mDueDate.toString().find( r ) > -1 ) | ||
127 | match = true; | ||
128 | else if ( mDesc.find( r ) > -1 ) | ||
129 | match = true; | ||
130 | return match; | ||
131 | } | ||
132 | |||
133 | static inline VObject *safeAddPropValue( VObject *o, const char *prop, const QString &value ) | ||
134 | { | ||
135 | VObject *ret = 0; | ||
136 | if ( o && !value.isEmpty() ) | ||
137 | ret = addPropValue( o, prop, value.latin1() ); | ||
138 | return ret; | ||
139 | } | ||
140 | |||
141 | static inline VObject *safeAddProp( VObject *o, const char *prop) | ||
142 | { | ||
143 | VObject *ret = 0; | ||
144 | if ( o ) | ||
145 | ret = addProp( o, prop ); | ||
146 | return ret; | ||
147 | } | ||
148 | |||
149 | |||
150 | static VObject *createVObject( const Task &t ) | ||
151 | { | ||
152 | VObject *vcal = newVObject( VCCalProp ); | ||
153 | safeAddPropValue( vcal, VCVersionProp, "1.0" ); | ||
154 | VObject *task = safeAddProp( vcal, VCTodoProp ); | ||
155 | |||
156 | if ( t.hasDueDate() ) | ||
157 | safeAddPropValue( task, VCDueProp, TimeConversion::toISO8601( t.dueDate() ) ); | ||
158 | safeAddPropValue( task, VCDescriptionProp, t.description() ); | ||
159 | if ( t.isCompleted() ) | ||
160 | safeAddPropValue( task, VCStatusProp, "COMPLETED" ); | ||
161 | safeAddPropValue( task, VCPriorityProp, QString::number( t.priority() ) ); | ||
162 | |||
163 | return vcal; | ||
164 | } | ||
165 | |||
166 | |||
167 | static Task parseVObject( VObject *obj ) | ||
168 | { | ||
169 | Task t; | ||
170 | |||
171 | VObjectIterator it; | ||
172 | initPropIterator( &it, obj ); | ||
173 | while( moreIteration( &it ) ) { | ||
174 | VObject *o = nextVObject( &it ); | ||
175 | QCString name = vObjectName( o ); | ||
176 | QCString value = vObjectStringZValue( o ); | ||
177 | if ( name == VCDueProp ) { | ||
178 | t.setDueDate( TimeConversion::fromISO8601( value ).date(), TRUE ); | ||
179 | } | ||
180 | else if ( name == VCDescriptionProp ) { | ||
181 | t.setDescription( value ); | ||
182 | } | ||
183 | else if ( name == VCStatusProp ) { | ||
184 | if ( value == "COMPLETED" ) | ||
185 | t.setCompleted( TRUE ); | ||
186 | } | ||
187 | else if ( name == VCPriorityProp ) { | ||
188 | t.setPriority( value.toInt() ); | ||
189 | } | ||
190 | #if 0 | ||
191 | else { | ||
192 | printf("Name: %s, value=%s\n", name.data(), vObjectStringZValue( o ) ); | ||
193 | VObjectIterator nit; | ||
194 | initPropIterator( &nit, o ); | ||
195 | while( moreIteration( &nit ) ) { | ||
196 | VObject *o = nextVObject( &nit ); | ||
197 | QCString name = vObjectName( o ); | ||
198 | QString value = vObjectStringZValue( o ); | ||
199 | printf(" subprop: %s = %s\n", name.data(), value.latin1() ); | ||
200 | } | ||
201 | } | ||
202 | #endif | ||
203 | } | ||
204 | |||
205 | return t; | ||
206 | } | ||
207 | |||
208 | |||
209 | |||
210 | void Task::writeVCalendar( const QString &filename, const QValueList<Task> &tasks) | ||
211 | { | ||
212 | QFileDirect f( filename.utf8().data() ); | ||
213 | if ( !f.open( IO_WriteOnly ) ) { | ||
214 | qWarning("Unable to open vcard write"); | ||
215 | return; | ||
216 | } | ||
217 | |||
218 | QValueList<Task>::ConstIterator it; | ||
219 | for( it = tasks.begin(); it != tasks.end(); ++it ) { | ||
220 | VObject *obj = createVObject( *it ); | ||
221 | writeVObject(f.directHandle() , obj ); | ||
222 | cleanVObject( obj ); | ||
223 | } | ||
224 | |||
225 | cleanStrTbl(); | ||
226 | } | ||
227 | |||
228 | void Task::writeVCalendar( const QString &filename, const Task &task) | ||
229 | { | ||
230 | QFileDirect f( filename.utf8().data() ); | ||
231 | if ( !f.open( IO_WriteOnly ) ) { | ||
232 | qWarning("Unable to open vcard write"); | ||
233 | return; | ||
234 | } | ||
235 | |||
236 | VObject *obj = createVObject( task ); | ||
237 | writeVObject(f.directHandle() , obj ); | ||
238 | cleanVObject( obj ); | ||
239 | |||
240 | cleanStrTbl(); | ||
241 | } | ||
242 | |||
243 | |||
244 | QValueList<Task> Task::readVCalendar( const QString &filename ) | ||
245 | { | ||
246 | VObject *obj = Parse_MIME_FromFileName( (char *)filename.utf8().data() ); | ||
247 | |||
248 | QValueList<Task> tasks; | ||
249 | |||
250 | while ( obj ) { | ||
251 | QCString name = vObjectName( obj ); | ||
252 | if ( name == VCCalProp ) { | ||
253 | VObjectIterator nit; | ||
254 | initPropIterator( &nit, obj ); | ||
255 | while( moreIteration( &nit ) ) { | ||
256 | VObject *o = nextVObject( &nit ); | ||
257 | QCString name = vObjectName( o ); | ||
258 | if ( name == VCTodoProp ) | ||
259 | tasks.append( parseVObject( o ) ); | ||
260 | } | ||
261 | } else if ( name == VCTodoProp ) { | ||
262 | // shouldn't happen, but just to be sure | ||
263 | tasks.append( parseVObject( obj ) ); | ||
264 | } | ||
265 | VObject *t = obj; | ||
266 | obj = nextVObjectInList(obj); | ||
267 | cleanVObject( t ); | ||
268 | } | ||
269 | |||
270 | return tasks; | ||
271 | } | ||