desktop: TaskEditDlg: make dates recent, when enabled
[barry/progweb.git] / src / r_task.cc
blob4edf8e98e7d497bade731fad7a361387072212a3
1 ///
2 /// \file r_task.cc
3 /// Record parsing class for the task database.
4 ///
6 /*
7 Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
8 Copyright (C) 2007, Brian Edginton
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include "r_task.h"
24 #include "r_calendar.h" // for CAL_* defines
25 #include "r_recur_base-int.h"
26 #include "record-internal.h"
27 #include "protostructs.h"
28 #include "data.h"
29 #include "time.h"
30 #include "iconv.h"
31 #include "debug.h"
32 #include <ostream>
33 #include <iomanip>
34 #include <string.h>
35 #include "ios_state.h"
37 using namespace std;
38 using namespace Barry::Protocol;
40 namespace Barry {
43 ///////////////////////////////////////////////////////////////////////////////
44 // Task Class, static members
47 // Note! These functions currently only pass the same values through.
48 // In actuality, these are technically two different values:
49 // one on the raw protocol side, and the other part of the
50 // guaranteed Barry API. If the Blackberry ever changes the
51 // meanings for these codes, do the translation here.
54 Task::AlarmFlagType Task::AlarmProto2Rec(uint8_t a)
56 return (AlarmFlagType)a;
59 uint8_t Task::AlarmRec2Proto(AlarmFlagType a)
61 return a;
64 Task::PriorityFlagType Task::PriorityProto2Rec(uint8_t p)
66 return (PriorityFlagType)p;
69 uint8_t Task::PriorityRec2Proto(PriorityFlagType p)
71 return p;
74 Task::StatusFlagType Task::StatusProto2Rec(uint8_t s)
76 return (StatusFlagType)s;
79 uint8_t Task::StatusRec2Proto(StatusFlagType s)
81 return s;
85 ///////////////////////////////////////////////////////////////////////////////
86 // Task Class
88 // Task Field Codes
89 #define TSKFC_TASK_TYPE 0x01
90 #define TSKFC_TITLE 0x02
91 #define TSKFC_NOTES 0x03
92 #define TSKFC_DUE_TIME 0x05
93 #define TSKFC_START_TIME 0x06 // This is fuzzy... most devices seem
94 // to anchor this value == to DUE_TIME
95 #define TSKFC_DUE_FLAG 0x08
96 #define TSKFC_STATUS 0x09
97 #define TSKFC_PRIORITY 0x0a
98 #define TSKFC_ALARM_TYPE 0x0e
99 #define TSKFC_ALARM_TIME 0x0f
100 #define TSKFC_TIMEZONE_CODE 0x10
101 #define TSKFC_CATEGORIES 0x11
102 #define TSKFC_ALARM_FLAG 0x12
103 #define TSKFC_END 0xffff
105 static FieldLink<Task> TaskFieldLinks[] = {
106 { TSKFC_TITLE, "Summary", 0, 0, &Task::Summary, 0, 0, 0, 0, true },
107 { TSKFC_NOTES, "Notes", 0, 0, &Task::Notes, 0, 0, 0, 0, true },
108 { TSKFC_START_TIME, "Start Time", 0, 0, 0, 0, &Task::StartTime, 0, 0, false },
109 { TSKFC_DUE_TIME, "Due Time", 0, 0, 0, 0, &Task::DueTime, 0, 0, false },
110 { TSKFC_ALARM_TIME, "Alarm Time", 0, 0, 0, 0, &Task::AlarmTime, 0, 0, false },
111 { TSKFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
114 Task::Task()
116 Clear();
119 Task::~Task()
123 const unsigned char* Task::ParseField(const unsigned char *begin,
124 const unsigned char *end,
125 const IConverter *ic)
127 const CommonField *field = (const CommonField *) begin;
129 // advance and check size
130 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
131 if( begin > end ) // if begin==end, we are ok
132 return begin;
134 if( !btohs(field->size) ) // if field has no size, something's up
135 return begin;
137 if( field->type == TSKFC_TASK_TYPE ) {
138 if( field->u.raw[0] != 't' ) {
139 throw Error("Task::ParseField: Task Type is not 't'");
141 return begin;
144 // cycle through the type table
145 for( FieldLink<Task> *b = TaskFieldLinks;
146 b->type != TSKFC_END;
147 b++ )
149 if( b->type == field->type ) {
150 if( b->strMember ) {
151 std::string &s = this->*(b->strMember);
152 s = ParseFieldString(field);
153 if( b->iconvNeeded && ic )
154 s = ic->FromBB(s);
155 return begin; // done!
157 else if( b->timeMember && btohs(field->size) == 4 ) {
158 TimeT &t = this->*(b->timeMember);
159 t.Time = min2time(field->u.min1900);
160 return begin;
165 // on old old devices, such as the 7750, there is no DueDateFlag
166 // or AlarmFlag, // so we check here manually
167 if( DueTime.IsValid() )
168 DueDateFlag = true;
169 if( AlarmTime.IsValid() )
170 AlarmFlag = true;
172 // handle special cases
173 switch( field->type )
175 case TSKFC_PRIORITY:
176 if( field->u.raw[0] > TR_PRIORITY_RANGE_HIGH ) {
177 throw Error( "Task::ParseField: priority field out of bounds" );
179 else {
180 PriorityFlag = PriorityProto2Rec(field->u.raw[0]);
182 return begin;
184 case TSKFC_STATUS:
185 if( field->u.raw[0] > TR_STATUS_RANGE_HIGH ) {
186 throw Error( "Task::ParseField: priority field out of bounds" );
188 else {
189 StatusFlag = StatusProto2Rec(field->u.raw[0]);
191 return begin;
193 case TSKFC_TIMEZONE_CODE:
194 if( btohs(field->size) == 4 ) {
195 TimeZoneCode = btohs(field->u.code);
196 TimeZoneValid = true;
198 else {
199 throw Error("Task::ParseField: not enough data in time zone code field");
201 return begin;
203 case TSKFC_DUE_FLAG:
204 // the DueDateFlag is not available on really old devices
205 // such as the 7750, so we set it here only if true, and
206 // also set it when we actually find a DueDate in the data
207 if( field->u.raw[0] )
208 DueDateFlag = true;
209 return begin;
211 case TSKFC_ALARM_FLAG:
212 // the AlarmFlag is not available on really old devices
213 // such as the 7750, so we set it here only if true, and
214 // also set it when we actually find an AlarmDate in the data
215 if( field->u.raw[0] )
216 AlarmFlag = true;
217 return begin;
219 case TSKFC_ALARM_TYPE:
220 if( field->u.raw[0] > TR_ALARM_RANGE_HIGH ) {
221 throw Error("Task::ParseField: AlarmType out of bounds" );
223 else {
224 AlarmType = AlarmProto2Rec(field->u.raw[0]);
226 return begin;
228 case TSKFC_CATEGORIES:
230 std::string catstring = ParseFieldString(field);
231 if( ic )
232 catstring = ic->FromBB(catstring);
233 Categories.CategoryStr2List(catstring);
235 return begin;
237 // base class handles recurring data
238 if( RecurBase::ParseField(field->type, field->u.raw, btohs(field->size), ic) )
239 return begin;
241 // if still not handled, add to the Unknowns list
242 UnknownField uf;
243 uf.type = field->type;
244 uf.data.assign((const char*)field->u.raw, btohs(field->size));
245 Unknowns.push_back(uf);
247 // return new pointer for next field
248 return begin;
251 void Task::ParseHeader(const Data &data, size_t &offset)
253 // no header in Task records
256 void Task::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
258 const unsigned char *finish = ParseCommonFields(*this,
259 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
260 offset += finish - (data.GetData() + offset);
264 void Task::Validate() const
266 RecurBase::Validate();
269 void Task::BuildHeader(Data &data, size_t &offset) const
271 // no header in Task records
276 // Build
278 /// Build fields part of record.
280 void Task::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
282 data.Zap();
284 // tack on the 't' task type field first
285 BuildField(data, offset, TSKFC_TASK_TYPE, 't');
287 BuildField(data, offset, TSKFC_STATUS, StatusRec2Proto(StatusFlag));
288 BuildField(data, offset, TSKFC_PRIORITY, PriorityRec2Proto(PriorityFlag));
289 BuildField(data, offset, TSKFC_ALARM_TYPE, AlarmRec2Proto(AlarmType));
291 if( AlarmFlag || AlarmTime.IsValid() )
292 BuildField(data, offset, TSKFC_ALARM_FLAG, (char) 1);
293 else
294 BuildField(data, offset, TSKFC_ALARM_FLAG, (char) 0);
296 if ( DueDateFlag || DueTime.IsValid() )
297 BuildField(data, offset, TSKFC_DUE_FLAG, (char) 1);
298 else
299 BuildField(data, offset, TSKFC_DUE_FLAG, (char) 0);
301 if( TimeZoneValid ) {
302 // the time zone code field is 4 bytes, but we only use
303 // the first two... pad it with zeros
304 uint32_t code = TimeZoneCode;
305 BuildField(data, offset, TSKFC_TIMEZONE_CODE, code);
308 // cycle through the type table
309 for( FieldLink<Task> *b = TaskFieldLinks;
310 b->type != TSKFC_END;
311 b++ )
313 // print only fields with data
314 if( b->strMember ) {
315 const std::string &field = this->*(b->strMember);
316 if( field.size() ) {
317 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
318 BuildField(data, offset, b->type, s);
321 else if( b->timeMember ) {
322 TimeT t = this->*(b->timeMember);
323 if( t.IsValid() )
324 BuildField1900(data, offset, b->type, t);
326 else if( b->postMember && b->postField ) {
327 const std::string &field = (this->*(b->postMember)).*(b->postField);
328 if( field.size() ) {
329 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
330 BuildField(data, offset, b->type, s);
335 // Categories
336 if( Categories.size() ) {
337 string store;
338 Categories.CategoryList2Str(store);
339 BuildField(data, offset, TSKFC_CATEGORIES, ic ? ic->ToBB(store) : store);
342 // and finally save unknowns
343 UnknownsType::const_iterator
344 ub = Unknowns.begin(), ue = Unknowns.end();
345 for( ; ub != ue; ub++ ) {
346 BuildField(data, offset, *ub);
349 data.ReleaseBuffer(offset);
354 void Task::Clear()
356 // clear the base class first
357 RecurBase::Clear();
359 // our variables...
360 RecType = GetDefaultRecType();
361 RecordId = 0;
363 Summary.clear();
364 Notes.clear();
365 Categories.clear();
366 UID.clear();
368 StartTime.clear();
369 DueTime.clear();
370 AlarmTime.clear();
372 TimeZoneCode = GetStaticTimeZoneCode( 0, 0 ); // default to GMT
373 TimeZoneValid = false;
375 AlarmType = (AlarmFlagType)0;
376 PriorityFlag = (PriorityFlagType)0;
377 StatusFlag = (StatusFlagType)0;
379 DueDateFlag = false;
380 AlarmFlag = false;
382 Unknowns.clear();
385 const FieldHandle<Task>::ListT& Task::GetFieldHandles()
387 static FieldHandle<Task>::ListT fhv;
389 if( fhv.size() )
390 return fhv;
392 #undef CONTAINER_OBJECT_NAME
393 #define CONTAINER_OBJECT_NAME fhv
395 #undef RECORD_CLASS_NAME
396 #define RECORD_CLASS_NAME Task
398 FHP(RecType, "Record Type Code");
399 FHP(RecordId, "Unique Record ID");
401 FHD(Summary, "Summary", TSKFC_TITLE, true);
402 FHD(Notes, "Notes", TSKFC_NOTES, true);
403 FHD(Categories, "Categories", TSKFC_CATEGORIES, true);
404 FHP(UID, "UID"); // FIXME - not linked to any device field??
406 FHD(StartTime, "Start Time", TSKFC_START_TIME, false);
407 FHD(DueTime, "Due Time", TSKFC_DUE_TIME, false);
408 FHD(AlarmTime, "Alarm Time", TSKFC_ALARM_TIME, false);
409 FHD(TimeZoneCode, "Time Zone Code", TSKFC_TIMEZONE_CODE, false);
410 FHP(TimeZoneValid, "Time Zone Code Valid");
411 FHD(DueDateFlag, "Due Date is Set", TSKFC_DUE_FLAG, false);
413 FHE(aft, AlarmFlagType, AlarmType, "Alarm Type");
414 FHE_CONST(aft, Date, "Date");
415 FHE_CONST(aft, Relative, "Relative");
417 FHE(pft, PriorityFlagType, PriorityFlag, "Priority");
418 FHE_CONST(pft, High, "High");
419 FHE_CONST(pft, Normal, "Normal");
420 FHE_CONST(pft, Low, "Low");
422 FHE(sft, StatusFlagType, StatusFlag, "Status");
423 FHE_CONST(sft, NotStarted, "Not Started");
424 FHE_CONST(sft, InProgress, "In Progress");
425 FHE_CONST(sft, Completed, "Completed");
426 FHE_CONST(sft, Waiting, "Waiting");
427 FHE_CONST(sft, Deferred, "Deferred");
429 FHP(Unknowns, "Unknown Fields");
431 // and finally, the RecurBase fields
432 RECUR_BASE_FIELD_HANDLES
434 return fhv;
437 std::string Task::GetDescription() const
439 return Summary;
442 void Task::Dump(std::ostream &os) const
444 ios_format_state state(os);
446 static const char *PriorityName[] = { "High", "Normal", "Low" };
447 static const char *StatusName[] = { "Not Started", "In Progress",
448 "Completed", "Waiting", "Deferred" };
449 static const char *DayNames[] = { "Sun", "Mon", "Tue", "Wed",
450 "Thu", "Fri", "Sat" };
451 static const char *MonthNames[] = { "Jan", "Feb", "Mar", "Apr",
452 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
453 static const char *AlarmTypeName[] = { "None", "By Date", "Relative" };
455 os << "Task entry: 0x" << setbase(16) << RecordId
456 << " (" << (unsigned int)RecType << ")\n";
458 // cycle through the type table
459 for( const FieldLink<Task> *b = TaskFieldLinks;
460 b->type != TSKFC_END;
461 b++ )
463 if( b->strMember ) {
464 const std::string &s = this->*(b->strMember);
465 if( s.size() )
466 os << " " << b->name << ": " << s << "\n";
468 else if( b->timeMember ) {
469 TimeT t = this->*(b->timeMember);
470 if( t.Time > 0 )
471 os << " " << b->name << ": " << t << "\n";
475 os << " Due Date Flag: " << (DueDateFlag ? "true" : "false") << "\n";
476 os << " Alarm Flag: " << (AlarmFlag ? "true" : "false") << "\n";
477 os << " Priority: " << PriorityName[PriorityFlag] << "\n";
478 os << " Status: " << StatusName[StatusFlag] << "\n";
479 if( AlarmType ) {
480 os << " Alarm Type: " << AlarmTypeName[AlarmType] << "\n";
482 if( TimeZoneValid )
483 os << " Time Zone: " << GetStaticTimeZone(TimeZoneCode)->Name << "\n";
485 // print recurrence data if available
486 os << " Recurring: " << (Recurring ? "yes" : "no") << "\n";
487 if( Recurring ) {
488 switch( RecurringType )
490 case Day:
491 os << " Every day.\n";
492 break;
494 case MonthByDate:
495 os << " Every month on the "
496 << DayOfMonth
497 << (DayOfMonth == 1 ? "st" : "")
498 << (DayOfMonth == 2 ? "nd" : "")
499 << (DayOfMonth == 3 ? "rd" : "")
500 << (DayOfMonth > 3 ? "th" : "")
501 << "\n";
502 break;
504 case MonthByDay:
505 os << " Every month on the "
506 << DayNames[DayOfWeek]
507 << " of week "
508 << WeekOfMonth
509 << "\n";
510 break;
512 case YearByDate:
513 os << " Every year on "
514 << MonthNames[MonthOfYear-1]
515 << " " << DayOfMonth << "\n";
516 break;
518 case YearByDay:
519 os << " Every year in " << MonthNames[MonthOfYear-1]
520 << " on "
521 << DayNames[DayOfWeek]
522 << " of week " << WeekOfMonth << "\n";
523 break;
525 case Week:
526 os << " Every week on: ";
527 if( WeekDays & CAL_WD_SUN ) os << "Sun ";
528 if( WeekDays & CAL_WD_MON ) os << "Mon ";
529 if( WeekDays & CAL_WD_TUE ) os << "Tue ";
530 if( WeekDays & CAL_WD_WED ) os << "Wed ";
531 if( WeekDays & CAL_WD_THU ) os << "Thu ";
532 if( WeekDays & CAL_WD_FRI ) os << "Fri ";
533 if( WeekDays & CAL_WD_SAT ) os << "Sat ";
534 os << "\n";
535 break;
537 default:
538 os << " Unknown recurrence type\n";
539 break;
542 os << " Interval: " << Interval << "\n";
544 if( Perpetual )
545 os << " Ends: never\n";
546 else
547 os << " Ends: " << RecurringEndTime << "\n";
550 if( Categories.size() ) {
551 string display;
552 Categories.CategoryList2Str(display);
553 os << " Categories: " << display << "\n";
556 os << Unknowns;
557 os << "\n\n";
560 bool Task::operator<(const Task &other) const
562 if( StartTime != other.StartTime )
563 return StartTime < other.StartTime;
564 if( AlarmTime != other.AlarmTime )
565 return AlarmTime < other.AlarmTime;
567 int cmp = Summary.compare(other.Summary);
568 if( cmp == 0 )
569 cmp = Notes.compare(other.Notes);
570 return cmp < 0;
573 } // namespace Barry