3 /// Record parsing class for the task database.
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.
24 #include "r_calendar.h" // for CAL_* defines
25 #include "r_recur_base-int.h"
26 #include "record-internal.h"
27 #include "protostructs.h"
35 #include "ios_state.h"
38 using namespace Barry::Protocol
;
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
)
64 Task::PriorityFlagType
Task::PriorityProto2Rec(uint8_t p
)
66 return (PriorityFlagType
)p
;
69 uint8_t Task::PriorityRec2Proto(PriorityFlagType p
)
74 Task::StatusFlagType
Task::StatusProto2Rec(uint8_t s
)
76 return (StatusFlagType
)s
;
79 uint8_t Task::StatusRec2Proto(StatusFlagType s
)
85 ///////////////////////////////////////////////////////////////////////////////
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 },
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
134 if( !btohs(field
->size
) ) // if field has no size, something's up
137 if( field
->type
== TSKFC_TASK_TYPE
) {
138 if( field
->u
.raw
[0] != 't' ) {
139 throw Error("Task::ParseField: Task Type is not 't'");
144 // cycle through the type table
145 for( FieldLink
<Task
> *b
= TaskFieldLinks
;
146 b
->type
!= TSKFC_END
;
149 if( b
->type
== field
->type
) {
151 std::string
&s
= this->*(b
->strMember
);
152 s
= ParseFieldString(field
);
153 if( b
->iconvNeeded
&& ic
)
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
);
165 // handle special cases
166 switch( field
->type
)
169 if( field
->u
.raw
[0] > TR_PRIORITY_RANGE_HIGH
) {
170 throw Error( "Task::ParseField: priority field out of bounds" );
173 PriorityFlag
= PriorityProto2Rec(field
->u
.raw
[0]);
178 if( field
->u
.raw
[0] > TR_STATUS_RANGE_HIGH
) {
179 throw Error( "Task::ParseField: priority field out of bounds" );
182 StatusFlag
= StatusProto2Rec(field
->u
.raw
[0]);
186 case TSKFC_TIMEZONE_CODE
:
187 if( btohs(field
->size
) == 4 ) {
188 TimeZoneCode
= btohs(field
->u
.code
);
189 TimeZoneValid
= true;
192 throw Error("Task::ParseField: not enough data in time zone code field");
197 // the DueDateFlag is not available on really old devices
198 // such as the 7750, and if the DueTime is available,
199 // we'll just save it. There is no further need for this
200 // value that we know of yet, so just ignore it for now.
203 case TSKFC_ALARM_FLAG
:
204 // the AlarmFlag is not available on really old devices
205 // such as the 7750, and if the AlarmTime is available,
206 // we'll just save it. There is no further need for this
207 // value that we know of yet, so just ignore it for now.
210 case TSKFC_ALARM_TYPE
:
211 if( field
->u
.raw
[0] > TR_ALARM_RANGE_HIGH
) {
212 throw Error("Task::ParseField: AlarmType out of bounds" );
215 AlarmType
= AlarmProto2Rec(field
->u
.raw
[0]);
219 case TSKFC_CATEGORIES
:
221 std::string catstring
= ParseFieldString(field
);
223 catstring
= ic
->FromBB(catstring
);
224 Categories
.CategoryStr2List(catstring
);
229 // base class handles recurring data
230 if( RecurBase::ParseField(field
->type
, field
->u
.raw
, btohs(field
->size
), ic
) )
233 // if still not handled, add to the Unknowns list
235 uf
.type
= field
->type
;
236 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
237 Unknowns
.push_back(uf
);
239 // return new pointer for next field
243 void Task::ParseHeader(const Data
&data
, size_t &offset
)
245 // no header in Task records
248 void Task::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
250 const unsigned char *finish
= ParseCommonFields(*this,
251 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
252 offset
+= finish
- (data
.GetData() + offset
);
256 void Task::Validate() const
258 RecurBase::Validate();
261 void Task::BuildHeader(Data
&data
, size_t &offset
) const
263 // no header in Task records
270 /// Build fields part of record.
272 void Task::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
277 // Note: we do not use the FieldLink table, since the firmware
278 // on many devices apears flakey, so we try to write as close to
279 // the same order as we see coming from the device. Unfortunately,
280 // this doesn't really help, for the Tasks corruption bug, but
281 // we'll keep the flexibility for now.
284 // tack on the 't' task type field first
285 BuildField(data
, offset
, TSKFC_TASK_TYPE
, 't');
288 if( Summary
.size() ) {
289 std::string s
= (ic
) ? ic
->ToBB(Summary
) : Summary
;
290 BuildField(data
, offset
, TSKFC_TITLE
, s
);
293 BuildField(data
, offset
, TSKFC_STATUS
, (uint32_t) StatusRec2Proto(StatusFlag
));
294 BuildField(data
, offset
, TSKFC_PRIORITY
, (uint32_t) PriorityRec2Proto(PriorityFlag
));
296 if( TimeZoneValid
) {
297 // the time zone code field is 4 bytes, but we only use
298 // the first two... pad it with zeros
299 uint32_t code
= TimeZoneCode
;
300 BuildField(data
, offset
, TSKFC_TIMEZONE_CODE
, code
);
303 // make sure StartTime matches DueTime, by writing it manually...
304 /// not sure why StartTime exists, but oh well. :-)
305 if( DueTime
.IsValid() ) {
306 // we use DueTime here, with the START_TIME code,
307 // instead of StartTime, since the function is const
308 BuildField1900(data
, offset
, TSKFC_START_TIME
, DueTime
);
310 // then DueTime, with flag first, then time
311 BuildField(data
, offset
, TSKFC_DUE_FLAG
, (uint32_t) 1);
312 BuildField1900(data
, offset
, TSKFC_DUE_TIME
, DueTime
);
315 if( AlarmTime
.IsValid() ) {
316 BuildField(data
, offset
, TSKFC_ALARM_FLAG
, (uint32_t) 1);
317 BuildField(data
, offset
, TSKFC_ALARM_TYPE
, AlarmRec2Proto(AlarmType
));
318 BuildField1900(data
, offset
, TSKFC_ALARM_TIME
, AlarmTime
);
322 if( Categories
.size() ) {
324 Categories
.CategoryList2Str(store
);
325 BuildField(data
, offset
, TSKFC_CATEGORIES
, ic
? ic
->ToBB(store
) : store
);
330 std::string s
= (ic
) ? ic
->ToBB(Notes
) : Notes
;
331 BuildField(data
, offset
, TSKFC_NOTES
, s
);
335 CalendarRecurrenceDataField recur
;
336 BuildRecurrenceData(StartTime
.Time
, &recur
);
337 BuildField(data
, offset
, RecurBase::RecurringFieldType(),
338 &recur
, CALENDAR_RECURRENCE_DATA_FIELD_SIZE
);
341 // and finally save unknowns
342 UnknownsType::const_iterator
343 ub
= Unknowns
.begin(), ue
= Unknowns
.end();
344 for( ; ub
!= ue
; ub
++ ) {
345 BuildField(data
, offset
, *ub
);
348 data
.ReleaseBuffer(offset
);
355 // clear the base class first
359 RecType
= GetDefaultRecType();
371 TimeZoneCode
= GetStaticTimeZoneCode( 0, 0 ); // default to GMT
372 TimeZoneValid
= false;
375 PriorityFlag
= Normal
;
376 StatusFlag
= NotStarted
;
381 const FieldHandle
<Task
>::ListT
& Task::GetFieldHandles()
383 static FieldHandle
<Task
>::ListT fhv
;
388 #undef CONTAINER_OBJECT_NAME
389 #define CONTAINER_OBJECT_NAME fhv
391 #undef RECORD_CLASS_NAME
392 #define RECORD_CLASS_NAME Task
394 FHP(RecType
, "Record Type Code");
395 FHP(RecordId
, "Unique Record ID");
397 FHD(Summary
, "Summary", TSKFC_TITLE
, true);
398 FHD(Notes
, "Notes", TSKFC_NOTES
, true);
399 FHD(Categories
, "Categories", TSKFC_CATEGORIES
, true);
400 FHP(UID
, "UID"); // FIXME - not linked to any device field??
402 FHD(StartTime
, "Start Time", TSKFC_START_TIME
, false);
403 FHD(DueTime
, "Due Time", TSKFC_DUE_TIME
, false);
404 FHD(AlarmTime
, "Alarm Time", TSKFC_ALARM_TIME
, false);
405 FHD(TimeZoneCode
, "Time Zone Code", TSKFC_TIMEZONE_CODE
, false);
406 FHP(TimeZoneValid
, "Time Zone Code Valid");
408 FHE(aft
, AlarmFlagType
, AlarmType
, "Alarm Type");
409 FHE_CONST(aft
, Date
, "Date");
410 FHE_CONST(aft
, Relative
, "Relative");
412 FHE(pft
, PriorityFlagType
, PriorityFlag
, "Priority");
413 FHE_CONST(pft
, High
, "High");
414 FHE_CONST(pft
, Normal
, "Normal");
415 FHE_CONST(pft
, Low
, "Low");
417 FHE(sft
, StatusFlagType
, StatusFlag
, "Status");
418 FHE_CONST(sft
, NotStarted
, "Not Started");
419 FHE_CONST(sft
, InProgress
, "In Progress");
420 FHE_CONST(sft
, Completed
, "Completed");
421 FHE_CONST(sft
, Waiting
, "Waiting");
422 FHE_CONST(sft
, Deferred
, "Deferred");
424 FHP(Unknowns
, "Unknown Fields");
426 // and finally, the RecurBase fields
427 RECUR_BASE_FIELD_HANDLES
432 std::string
Task::GetDescription() const
437 void Task::Dump(std::ostream
&os
) const
439 ios_format_state
state(os
);
441 static const char *PriorityName
[] = { "High", "Normal", "Low" };
442 static const char *StatusName
[] = { "Not Started", "In Progress",
443 "Completed", "Waiting", "Deferred" };
444 static const char *DayNames
[] = { "Sun", "Mon", "Tue", "Wed",
445 "Thu", "Fri", "Sat" };
446 static const char *MonthNames
[] = { "Jan", "Feb", "Mar", "Apr",
447 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
448 static const char *AlarmTypeName
[] = { "None", "By Date", "Relative" };
450 os
<< "Task entry: 0x" << setbase(16) << RecordId
451 << " (" << (unsigned int)RecType
<< ")\n";
453 // cycle through the type table
454 for( const FieldLink
<Task
> *b
= TaskFieldLinks
;
455 b
->type
!= TSKFC_END
;
459 const std::string
&s
= this->*(b
->strMember
);
461 os
<< " " << b
->name
<< ": " << Cr2LfWrapper(s
) << "\n";
463 else if( b
->timeMember
) {
464 TimeT t
= this->*(b
->timeMember
);
466 os
<< " " << b
->name
<< ": " << t
<< "\n";
470 os
<< " Priority: " << PriorityName
[PriorityFlag
] << "\n";
471 os
<< " Status: " << StatusName
[StatusFlag
] << "\n";
473 os
<< " Alarm Type: " << AlarmTypeName
[AlarmType
] << "\n";
476 os
<< " Time Zone: " << GetStaticTimeZone(TimeZoneCode
)->Name
<< "\n";
478 // print recurrence data if available
479 os
<< " Recurring: " << (Recurring
? "yes" : "no") << "\n";
481 switch( RecurringType
)
484 os
<< " Every day.\n";
488 os
<< " Every month on the "
490 << (DayOfMonth
== 1 ? "st" : "")
491 << (DayOfMonth
== 2 ? "nd" : "")
492 << (DayOfMonth
== 3 ? "rd" : "")
493 << (DayOfMonth
> 3 ? "th" : "")
498 os
<< " Every month on the "
499 << DayNames
[DayOfWeek
]
506 os
<< " Every year on "
507 << MonthNames
[MonthOfYear
-1]
508 << " " << DayOfMonth
<< "\n";
512 os
<< " Every year in " << MonthNames
[MonthOfYear
-1]
514 << DayNames
[DayOfWeek
]
515 << " of week " << WeekOfMonth
<< "\n";
519 os
<< " Every week on: ";
520 if( WeekDays
& CAL_WD_SUN
) os
<< "Sun ";
521 if( WeekDays
& CAL_WD_MON
) os
<< "Mon ";
522 if( WeekDays
& CAL_WD_TUE
) os
<< "Tue ";
523 if( WeekDays
& CAL_WD_WED
) os
<< "Wed ";
524 if( WeekDays
& CAL_WD_THU
) os
<< "Thu ";
525 if( WeekDays
& CAL_WD_FRI
) os
<< "Fri ";
526 if( WeekDays
& CAL_WD_SAT
) os
<< "Sat ";
531 os
<< " Unknown recurrence type\n";
535 os
<< " Interval: " << Interval
<< "\n";
538 os
<< " Ends: never\n";
540 os
<< " Ends: " << RecurringEndTime
<< "\n";
543 if( Categories
.size() ) {
545 Categories
.CategoryList2Str(display
);
546 os
<< " Categories: " << display
<< "\n";
553 bool Task::operator<(const Task
&other
) const
555 if( StartTime
!= other
.StartTime
)
556 return StartTime
< other
.StartTime
;
557 if( AlarmTime
!= other
.AlarmTime
)
558 return AlarmTime
< other
.AlarmTime
;
560 int cmp
= Summary
.compare(other
.Summary
);
562 cmp
= Notes
.compare(other
.Notes
);