00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "task.h"
00022 #include "recordfields.h"
00023 #include "vobject_p.h"
00024 #include "qfiledirect_p.h"
00025
00026 #include <qtopia/timeconversion.h>
00027
00028 #include <qregexp.h>
00029 #include <qstring.h>
00030
00031 #include <stdio.h>
00032
00033 using namespace Qtopia;
00034 UidGen Task::sUidGen( UidGen::Qtopia );
00035
00050 Task::Task() : Record(), mDue( FALSE ),
00051 mDueDate( QDate::currentDate() ),
00052 mCompleted( FALSE ), mPriority( 3 ), mDesc()
00053 {
00054 }
00055
00139 Task::Task( const QMap<int, QString> &m ) : Record(), mDue( FALSE ),
00140 mDueDate( QDate::currentDate() ), mCompleted( FALSE ), mPriority( 3 ), mDesc()
00141 {
00142
00143
00144 for ( QMap<int,QString>::ConstIterator it = m.begin(); it != m.end();++it )
00145 switch ( (TaskFields) it.key() ) {
00146 case HasDate: if ( *it == "1" ) mDue = TRUE; break;
00147 case Completed: setCompleted( *it == "1" ); break;
00148 case TaskCategory: setCategories( idsFromString( *it ) ); break;
00149 case TaskDescription: setDescription( *it ); break;
00150 case Priority: setPriority( (*it).toInt() ); break;
00151 case Date: mDueDate = TimeConversion::fromString( (*it) ); break;
00152 case TaskUid: setUid( (*it).toInt() ); break;
00153 case TaskRid:
00154 case TaskRinfo:
00155 break;
00156 }
00157 }
00158
00162 Task::~Task()
00163 {
00164 }
00165
00170 QMap<int, QString> Task::toMap() const
00171 {
00172 QMap<int, QString> m;
00173 m.insert( HasDate, hasDueDate() ? "1" : "0" );
00174 m.insert( Completed, isCompleted() ? "1" : "0" );
00175 if ( categories().count() )
00176 m.insert( TaskCategory, idsToString( categories() ) );
00177 if ( !description().isEmpty() )
00178 m.insert( TaskDescription, description() );
00179 m.insert( Priority, QString::number( priority() ) );
00180 if ( hasDueDate() )
00181 m.insert( Date, TimeConversion::toString( dueDate() ) );
00182 m.insert( TaskUid, QString::number(uid()) );
00183
00184
00185
00186 return m;
00187 }
00188
00193 void Task::save( QString& buf ) const
00194 {
00195 buf += " Completed=\"";
00196
00197 buf += QString::number( (int)mCompleted );
00198 buf += "\"";
00199 buf += " HasDate=\"";
00200
00201 buf += QString::number( (int)mDue );
00202 buf += "\"";
00203 buf += " Priority=\"";
00204
00205 buf += QString::number( mPriority );
00206 buf += "\"";
00207 buf += " Categories=\"";
00208 buf += Qtopia::Record::idsToString( categories() );
00209 buf += "\"";
00210 buf += " Description=\"";
00211
00212 buf += Qtopia::escapeString( mDesc );
00213 buf += "\"";
00214 if ( mDue ) {
00215
00216
00217 buf += " DateYear=\"";
00218 buf += QString::number( mDueDate.year() );
00219 buf += "\"";
00220 buf += " DateMonth=\"";
00221 buf += QString::number( mDueDate.month() );
00222 buf += "\"";
00223 buf += " DateDay=\"";
00224 buf += QString::number( mDueDate.day() );
00225 buf += "\"";
00226 }
00227 buf += customToXml();
00228
00229 buf += " Uid=\"";
00230 buf += QString::number( uid() );
00231
00232 buf += "\"";
00233 }
00234
00239 bool Task::match ( const QRegExp ®exp ) const
00240 {
00241
00242 bool match;
00243 match = false;
00244 if ( QString::number( mPriority ).find( regexp ) > -1 )
00245 match = true;
00246 else if ( mDue && mDueDate.toString().find( regexp ) > -1 )
00247 match = true;
00248 else if ( mDesc.find( regexp ) > -1 )
00249 match = true;
00250 return match;
00251 }
00252
00256 static inline VObject *safeAddPropValue( VObject *o, const char *prop, const QString &value )
00257 {
00258 VObject *ret = 0;
00259 if ( o && !value.isEmpty() )
00260 ret = addPropValue( o, prop, value.latin1() );
00261 return ret;
00262 }
00263
00267 static inline VObject *safeAddProp( VObject *o, const char *prop)
00268 {
00269 VObject *ret = 0;
00270 if ( o )
00271 ret = addProp( o, prop );
00272 return ret;
00273 }
00274
00275
00279 static VObject *createVObject( const Task &t )
00280 {
00281 VObject *vcal = newVObject( VCCalProp );
00282 safeAddPropValue( vcal, VCVersionProp, "1.0" );
00283 VObject *task = safeAddProp( vcal, VCTodoProp );
00284
00285 if ( t.hasDueDate() )
00286 safeAddPropValue( task, VCDueProp, TimeConversion::toISO8601( t.dueDate() ) );
00287 safeAddPropValue( task, VCDescriptionProp, t.description() );
00288 if ( t.isCompleted() )
00289 safeAddPropValue( task, VCStatusProp, "COMPLETED" );
00290 safeAddPropValue( task, VCPriorityProp, QString::number( t.priority() ) );
00291
00292 return vcal;
00293 }
00294
00298 static Task parseVObject( VObject *obj )
00299 {
00300 Task t;
00301
00302 VObjectIterator it;
00303 initPropIterator( &it, obj );
00304 while( moreIteration( &it ) ) {
00305 VObject *o = nextVObject( &it );
00306 QCString name = vObjectName( o );
00307 QCString value = vObjectStringZValue( o );
00308 if ( name == VCDueProp ) {
00309 t.setDueDate( TimeConversion::fromISO8601( value ).date(), TRUE );
00310 }
00311 else if ( name == VCDescriptionProp ) {
00312 t.setDescription( value );
00313 }
00314 else if ( name == VCStatusProp ) {
00315 if ( value == "COMPLETED" )
00316 t.setCompleted( TRUE );
00317 }
00318 else if ( name == VCPriorityProp ) {
00319 t.setPriority( value.toInt() );
00320 }
00321 #if 0
00322 else {
00323 printf("Name: %s, value=%s\n", name.data(), vObjectStringZValue( o ) );
00324 VObjectIterator nit;
00325 initPropIterator( &nit, o );
00326 while( moreIteration( &nit ) ) {
00327 VObject *o = nextVObject( &nit );
00328 QCString name = vObjectName( o );
00329 QString value = vObjectStringZValue( o );
00330 printf(" subprop: %s = %s\n", name.data(), value.latin1() );
00331 }
00332 }
00333 #endif
00334 }
00335
00336 return t;
00337 }
00338
00339
00343 void Task::writeVCalendar( const QString &filename, const QValueList<Task> &tasks)
00344 {
00345 QFileDirect f( filename.utf8().data() );
00346 if ( !f.open( IO_WriteOnly ) ) {
00347 qWarning("Unable to open vcard write");
00348 return;
00349 }
00350
00351 QValueList<Task>::ConstIterator it;
00352 for( it = tasks.begin(); it != tasks.end(); ++it ) {
00353 VObject *obj = createVObject( *it );
00354 writeVObject(f.directHandle() , obj );
00355 cleanVObject( obj );
00356 }
00357
00358 cleanStrTbl();
00359 }
00360
00364 void Task::writeVCalendar( const QString &filename, const Task &task)
00365 {
00366 QFileDirect f( filename.utf8().data() );
00367 if ( !f.open( IO_WriteOnly ) ) {
00368 qWarning("Unable to open vcard write");
00369 return;
00370 }
00371
00372 VObject *obj = createVObject( task );
00373 writeVObject(f.directHandle() , obj );
00374 cleanVObject( obj );
00375
00376 cleanStrTbl();
00377 }
00378
00382 QValueList<Task> Task::readVCalendar( const QString &filename )
00383 {
00384 VObject *obj = Parse_MIME_FromFileName( (char *)filename.utf8().data() );
00385
00386 QValueList<Task> tasks;
00387
00388 while ( obj ) {
00389 QCString name = vObjectName( obj );
00390 if ( name == VCCalProp ) {
00391 VObjectIterator nit;
00392 initPropIterator( &nit, obj );
00393 while( moreIteration( &nit ) ) {
00394 VObject *o = nextVObject( &nit );
00395 QCString name = vObjectName( o );
00396 if ( name == VCTodoProp )
00397 tasks.append( parseVObject( o ) );
00398 }
00399 } else if ( name == VCTodoProp ) {
00400
00401 tasks.append( parseVObject( obj ) );
00402 }
00403 VObject *t = obj;
00404 obj = nextVObjectInList(obj);
00405 cleanVObject( t );
00406 }
00407
00408 return tasks;
00409 }