desktop: CalEditDlg: fixed dialog title bar
[barry.git] / src / r_task.cc
blob361eb19f2bd28c9879324c6722d65b999bde1524
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_END 0xffff
104 static FieldLink<Task> TaskFieldLinks[] = {
105 { TSKFC_TITLE, "Summary", 0, 0, &Task::Summary, 0, 0, 0, 0, true },
106 { TSKFC_NOTES, "Notes", 0, 0, &Task::Notes, 0, 0, 0, 0, true },
107 { TSKFC_START_TIME, "Start Time", 0, 0, 0, 0, &Task::StartTime, 0, 0, false },
108 { TSKFC_DUE_TIME, "Due Time", 0, 0, 0, 0, &Task::DueTime, 0, 0, false },
109 { TSKFC_ALARM_TIME, "Alarm Time", 0, 0, 0, 0, &Task::AlarmTime, 0, 0, false },
110 { TSKFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
113 Task::Task()
115 Clear();
118 Task::~Task()
122 const unsigned char* Task::ParseField(const unsigned char *begin,
123 const unsigned char *end,
124 const IConverter *ic)
126 const CommonField *field = (const CommonField *) begin;
128 // advance and check size
129 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
130 if( begin > end ) // if begin==end, we are ok
131 return begin;
133 if( !btohs(field->size) ) // if field has no size, something's up
134 return begin;
136 if( field->type == TSKFC_TASK_TYPE ) {
137 if( field->u.raw[0] != 't' ) {
138 throw Error("Task::ParseField: Task Type is not 't'");
140 return begin;
143 // cycle through the type table
144 for( FieldLink<Task> *b = TaskFieldLinks;
145 b->type != TSKFC_END;
146 b++ )
148 if( b->type == field->type ) {
149 if( b->strMember ) {
150 std::string &s = this->*(b->strMember);
151 s = ParseFieldString(field);
152 if( b->iconvNeeded && ic )
153 s = ic->FromBB(s);
154 return begin; // done!
156 else if( b->timeMember && btohs(field->size) == 4 ) {
157 TimeT &t = this->*(b->timeMember);
158 t.Time = min2time(field->u.min1900);
159 return begin;
163 // handle special cases
164 switch( field->type )
166 case TSKFC_PRIORITY:
167 if( field->u.raw[0] > TR_PRIORITY_RANGE_HIGH ) {
168 throw Error( "Task::ParseField: priority field out of bounds" );
170 else {
171 PriorityFlag = PriorityProto2Rec(field->u.raw[0]);
173 return begin;
175 case TSKFC_STATUS:
176 if( field->u.raw[0] > TR_STATUS_RANGE_HIGH ) {
177 throw Error( "Task::ParseField: priority field out of bounds" );
179 else {
180 StatusFlag = StatusProto2Rec(field->u.raw[0]);
182 return begin;
184 case TSKFC_TIMEZONE_CODE:
185 if( btohs(field->size) == 4 ) {
186 TimeZoneCode = btohs(field->u.code);
187 TimeZoneValid = true;
189 else {
190 throw Error("Task::ParseField: not enough data in time zone code field");
192 return begin;
194 case TSKFC_DUE_FLAG:
195 DueDateFlag = field->u.raw[0];
196 return begin;
198 case TSKFC_ALARM_TYPE:
199 if( field->u.raw[0] > TR_ALARM_RANGE_HIGH ) {
200 throw Error("Task::ParseField: AlarmType out of bounds" );
202 else {
203 AlarmType = AlarmProto2Rec(field->u.raw[0]);
205 return begin;
207 case TSKFC_CATEGORIES:
209 std::string catstring = ParseFieldString(field);
210 if( ic )
211 catstring = ic->FromBB(catstring);
212 Categories.CategoryStr2List(catstring);
214 return begin;
216 // base class handles recurring data
217 if( RecurBase::ParseField(field->type, field->u.raw, btohs(field->size), ic) )
218 return begin;
220 // if still not handled, add to the Unknowns list
221 UnknownField uf;
222 uf.type = field->type;
223 uf.data.assign((const char*)field->u.raw, btohs(field->size));
224 Unknowns.push_back(uf);
226 // return new pointer for next field
227 return begin;
230 void Task::ParseHeader(const Data &data, size_t &offset)
232 // no header in Task records
235 void Task::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
237 const unsigned char *finish = ParseCommonFields(*this,
238 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
239 offset += finish - (data.GetData() + offset);
243 void Task::Validate() const
245 RecurBase::Validate();
248 void Task::BuildHeader(Data &data, size_t &offset) const
250 // no header in Task records
255 // Build
257 /// Build fields part of record.
259 void Task::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
261 data.Zap();
263 // tack on the 't' task type field first
264 BuildField(data, offset, TSKFC_TASK_TYPE, 't');
266 BuildField(data, offset, TSKFC_STATUS, StatusRec2Proto(StatusFlag));
267 BuildField(data, offset, TSKFC_PRIORITY, PriorityRec2Proto(PriorityFlag));
268 BuildField(data, offset, TSKFC_ALARM_TYPE, AlarmRec2Proto(AlarmType));
270 if ( DueDateFlag )
271 BuildField(data, offset, TSKFC_DUE_FLAG, (char) 1);
272 else
273 BuildField(data, offset, TSKFC_DUE_FLAG, (char) 0);
275 if( TimeZoneValid ) {
276 // the time zone code field is 4 bytes, but we only use
277 // the first two... pad it with zeros
278 uint32_t code = TimeZoneCode;
279 BuildField(data, offset, TSKFC_TIMEZONE_CODE, code);
282 // cycle through the type table
283 for( FieldLink<Task> *b = TaskFieldLinks;
284 b->type != TSKFC_END;
285 b++ )
287 // print only fields with data
288 if( b->strMember ) {
289 const std::string &field = this->*(b->strMember);
290 if( field.size() ) {
291 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
292 BuildField(data, offset, b->type, s);
295 else if( b->timeMember ) {
296 TimeT t = this->*(b->timeMember);
297 if( t.Time > 0 )
298 BuildField1900(data, offset, b->type, t);
300 else if( b->postMember && b->postField ) {
301 const std::string &field = (this->*(b->postMember)).*(b->postField);
302 if( field.size() ) {
303 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
304 BuildField(data, offset, b->type, s);
309 // Categories
310 if( Categories.size() ) {
311 string store;
312 Categories.CategoryList2Str(store);
313 BuildField(data, offset, TSKFC_CATEGORIES, ic ? ic->ToBB(store) : store);
316 // and finally save unknowns
317 UnknownsType::const_iterator
318 ub = Unknowns.begin(), ue = Unknowns.end();
319 for( ; ub != ue; ub++ ) {
320 BuildField(data, offset, *ub);
323 data.ReleaseBuffer(offset);
328 void Task::Clear()
330 // clear the base class first
331 RecurBase::Clear();
333 // our variables...
334 RecType = GetDefaultRecType();
335 RecordId = 0;
337 Summary.clear();
338 Notes.clear();
339 Categories.clear();
340 UID.clear();
342 StartTime.clear();
343 DueTime.clear();
344 AlarmTime.clear();
346 TimeZoneCode = GetStaticTimeZoneCode( 0, 0 ); // default to GMT
347 TimeZoneValid = false;
349 AlarmType = (AlarmFlagType)0;
350 PriorityFlag = (PriorityFlagType)0;
351 StatusFlag = (StatusFlagType)0;
353 DueDateFlag = false;
355 Unknowns.clear();
358 const FieldHandle<Task>::ListT& Task::GetFieldHandles()
360 static FieldHandle<Task>::ListT fhv;
362 if( fhv.size() )
363 return fhv;
365 #undef CONTAINER_OBJECT_NAME
366 #define CONTAINER_OBJECT_NAME fhv
368 #undef RECORD_CLASS_NAME
369 #define RECORD_CLASS_NAME Task
371 FHP(RecType, "Record Type Code");
372 FHP(RecordId, "Unique Record ID");
374 FHD(Summary, "Summary", TSKFC_TITLE, true);
375 FHD(Notes, "Notes", TSKFC_NOTES, true);
376 FHD(Categories, "Categories", TSKFC_CATEGORIES, true);
377 FHP(UID, "UID"); // FIXME - not linked to any device field??
379 FHD(StartTime, "Start Time", TSKFC_START_TIME, false);
380 FHD(DueTime, "Due Time", TSKFC_DUE_TIME, false);
381 FHD(AlarmTime, "Alarm Time", TSKFC_ALARM_TIME, false);
382 FHD(TimeZoneCode, "Time Zone Code", TSKFC_TIMEZONE_CODE, false);
383 FHP(TimeZoneValid, "Time Zone Code Valid");
384 FHD(DueDateFlag, "Due Date is Set", TSKFC_DUE_FLAG, false);
386 FHE(aft, AlarmFlagType, AlarmType, "Alarm Type");
387 FHE_CONST(aft, Date, "Date");
388 FHE_CONST(aft, Relative, "Relative");
390 FHE(pft, PriorityFlagType, PriorityFlag, "Priority");
391 FHE_CONST(pft, High, "High");
392 FHE_CONST(pft, Normal, "Normal");
393 FHE_CONST(pft, Low, "Low");
395 FHE(sft, StatusFlagType, StatusFlag, "Status");
396 FHE_CONST(sft, NotStarted, "Not Started");
397 FHE_CONST(sft, InProgress, "In Progress");
398 FHE_CONST(sft, Completed, "Completed");
399 FHE_CONST(sft, Waiting, "Waiting");
400 FHE_CONST(sft, Deferred, "Deferred");
402 FHP(Unknowns, "Unknown Fields");
404 // and finally, the RecurBase fields
405 RECUR_BASE_FIELD_HANDLES
407 return fhv;
410 std::string Task::GetDescription() const
412 return Summary;
415 void Task::Dump(std::ostream &os) const
417 ios_format_state state(os);
419 static const char *PriorityName[] = { "High", "Normal", "Low" };
420 static const char *StatusName[] = { "Not Started", "In Progress",
421 "Completed", "Waiting", "Deferred" };
422 static const char *DayNames[] = { "Sun", "Mon", "Tue", "Wed",
423 "Thu", "Fri", "Sat" };
424 static const char *MonthNames[] = { "Jan", "Feb", "Mar", "Apr",
425 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
426 static const char *AlarmTypeName[] = { "None", "By Date", "Relative" };
428 os << "Task entry: 0x" << setbase(16) << RecordId
429 << " (" << (unsigned int)RecType << ")\n";
431 // cycle through the type table
432 for( const FieldLink<Task> *b = TaskFieldLinks;
433 b->type != TSKFC_END;
434 b++ )
436 if( b->strMember ) {
437 const std::string &s = this->*(b->strMember);
438 if( s.size() )
439 os << " " << b->name << ": " << s << "\n";
441 else if( b->timeMember ) {
442 TimeT t = this->*(b->timeMember);
443 if( t.Time > 0 )
444 os << " " << b->name << ": " << t << "\n";
448 os << " Due Date Flag: " << (DueDateFlag ? "true" : "false") << "\n";
449 os << " Priority: " << PriorityName[PriorityFlag] << "\n";
450 os << " Status: " << StatusName[StatusFlag] << "\n";
451 if( AlarmType ) {
452 os << " Alarm Type: " << AlarmTypeName[AlarmType] << "\n";
454 if( TimeZoneValid )
455 os << " Time Zone: " << GetStaticTimeZone(TimeZoneCode)->Name << "\n";
457 // print recurrence data if available
458 os << " Recurring: " << (Recurring ? "yes" : "no") << "\n";
459 if( Recurring ) {
460 switch( RecurringType )
462 case Day:
463 os << " Every day.\n";
464 break;
466 case MonthByDate:
467 os << " Every month on the "
468 << DayOfMonth
469 << (DayOfMonth == 1 ? "st" : "")
470 << (DayOfMonth == 2 ? "nd" : "")
471 << (DayOfMonth == 3 ? "rd" : "")
472 << (DayOfMonth > 3 ? "th" : "")
473 << "\n";
474 break;
476 case MonthByDay:
477 os << " Every month on the "
478 << DayNames[DayOfWeek]
479 << " of week "
480 << WeekOfMonth
481 << "\n";
482 break;
484 case YearByDate:
485 os << " Every year on "
486 << MonthNames[MonthOfYear-1]
487 << " " << DayOfMonth << "\n";
488 break;
490 case YearByDay:
491 os << " Every year in " << MonthNames[MonthOfYear-1]
492 << " on "
493 << DayNames[DayOfWeek]
494 << " of week " << WeekOfMonth << "\n";
495 break;
497 case Week:
498 os << " Every week on: ";
499 if( WeekDays & CAL_WD_SUN ) os << "Sun ";
500 if( WeekDays & CAL_WD_MON ) os << "Mon ";
501 if( WeekDays & CAL_WD_TUE ) os << "Tue ";
502 if( WeekDays & CAL_WD_WED ) os << "Wed ";
503 if( WeekDays & CAL_WD_THU ) os << "Thu ";
504 if( WeekDays & CAL_WD_FRI ) os << "Fri ";
505 if( WeekDays & CAL_WD_SAT ) os << "Sat ";
506 os << "\n";
507 break;
509 default:
510 os << " Unknown recurrence type\n";
511 break;
514 os << " Interval: " << Interval << "\n";
516 if( Perpetual )
517 os << " Ends: never\n";
518 else
519 os << " Ends: " << RecurringEndTime << "\n";
522 if( Categories.size() ) {
523 string display;
524 Categories.CategoryList2Str(display);
525 os << " Categories: " << display << "\n";
528 os << Unknowns;
529 os << "\n\n";
532 bool Task::operator<(const Task &other) const
534 if( StartTime != other.StartTime )
535 return StartTime < other.StartTime;
536 if( AlarmTime != other.AlarmTime )
537 return AlarmTime < other.AlarmTime;
539 int cmp = Summary.compare(other.Summary);
540 if( cmp == 0 )
541 cmp = Notes.compare(other.Notes);
542 return cmp < 0;
545 } // namespace Barry