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 // on old old devices, such as the 7750, there is no DueDateFlag
166 // or AlarmFlag, // so we check here manually
167 if( DueTime
.IsValid() )
169 if( AlarmTime
.IsValid() )
172 // handle special cases
173 switch( field
->type
)
176 if( field
->u
.raw
[0] > TR_PRIORITY_RANGE_HIGH
) {
177 throw Error( "Task::ParseField: priority field out of bounds" );
180 PriorityFlag
= PriorityProto2Rec(field
->u
.raw
[0]);
185 if( field
->u
.raw
[0] > TR_STATUS_RANGE_HIGH
) {
186 throw Error( "Task::ParseField: priority field out of bounds" );
189 StatusFlag
= StatusProto2Rec(field
->u
.raw
[0]);
193 case TSKFC_TIMEZONE_CODE
:
194 if( btohs(field
->size
) == 4 ) {
195 TimeZoneCode
= btohs(field
->u
.code
);
196 TimeZoneValid
= true;
199 throw Error("Task::ParseField: not enough data in time zone code field");
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] )
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] )
219 case TSKFC_ALARM_TYPE
:
220 if( field
->u
.raw
[0] > TR_ALARM_RANGE_HIGH
) {
221 throw Error("Task::ParseField: AlarmType out of bounds" );
224 AlarmType
= AlarmProto2Rec(field
->u
.raw
[0]);
228 case TSKFC_CATEGORIES
:
230 std::string catstring
= ParseFieldString(field
);
232 catstring
= ic
->FromBB(catstring
);
233 Categories
.CategoryStr2List(catstring
);
237 // base class handles recurring data
238 if( RecurBase::ParseField(field
->type
, field
->u
.raw
, btohs(field
->size
), ic
) )
241 // if still not handled, add to the Unknowns list
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
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
278 /// Build fields part of record.
280 void Task::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
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);
294 BuildField(data
, offset
, TSKFC_ALARM_FLAG
, (char) 0);
296 if ( DueDateFlag
|| DueTime
.IsValid() )
297 BuildField(data
, offset
, TSKFC_DUE_FLAG
, (char) 1);
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
;
313 // print only fields with data
315 const std::string
&field
= this->*(b
->strMember
);
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
);
324 BuildField1900(data
, offset
, b
->type
, t
);
326 else if( b
->postMember
&& b
->postField
) {
327 const std::string
&field
= (this->*(b
->postMember
)).*(b
->postField
);
329 std::string s
= (b
->iconvNeeded
&& ic
) ? ic
->ToBB(field
) : field
;
330 BuildField(data
, offset
, b
->type
, s
);
336 if( Categories
.size() ) {
338 Categories
.CategoryList2Str(store
);
339 BuildField(data
, offset
, TSKFC_CATEGORIES
, ic
? ic
->ToBB(store
) : store
);
343 CalendarRecurrenceDataField recur
;
344 BuildRecurrenceData(StartTime
.Time
, &recur
);
345 BuildField(data
, offset
, RecurBase::RecurringFieldType(),
346 &recur
, CALENDAR_RECURRENCE_DATA_FIELD_SIZE
);
349 // and finally save unknowns
350 UnknownsType::const_iterator
351 ub
= Unknowns
.begin(), ue
= Unknowns
.end();
352 for( ; ub
!= ue
; ub
++ ) {
353 BuildField(data
, offset
, *ub
);
356 data
.ReleaseBuffer(offset
);
363 // clear the base class first
367 RecType
= GetDefaultRecType();
379 TimeZoneCode
= GetStaticTimeZoneCode( 0, 0 ); // default to GMT
380 TimeZoneValid
= false;
382 AlarmType
= (AlarmFlagType
)0;
383 PriorityFlag
= (PriorityFlagType
)0;
384 StatusFlag
= (StatusFlagType
)0;
392 const FieldHandle
<Task
>::ListT
& Task::GetFieldHandles()
394 static FieldHandle
<Task
>::ListT fhv
;
399 #undef CONTAINER_OBJECT_NAME
400 #define CONTAINER_OBJECT_NAME fhv
402 #undef RECORD_CLASS_NAME
403 #define RECORD_CLASS_NAME Task
405 FHP(RecType
, "Record Type Code");
406 FHP(RecordId
, "Unique Record ID");
408 FHD(Summary
, "Summary", TSKFC_TITLE
, true);
409 FHD(Notes
, "Notes", TSKFC_NOTES
, true);
410 FHD(Categories
, "Categories", TSKFC_CATEGORIES
, true);
411 FHP(UID
, "UID"); // FIXME - not linked to any device field??
413 FHD(StartTime
, "Start Time", TSKFC_START_TIME
, false);
414 FHD(DueTime
, "Due Time", TSKFC_DUE_TIME
, false);
415 FHD(AlarmTime
, "Alarm Time", TSKFC_ALARM_TIME
, false);
416 FHD(TimeZoneCode
, "Time Zone Code", TSKFC_TIMEZONE_CODE
, false);
417 FHP(TimeZoneValid
, "Time Zone Code Valid");
418 FHD(DueDateFlag
, "Due Date is Set", TSKFC_DUE_FLAG
, false);
420 FHE(aft
, AlarmFlagType
, AlarmType
, "Alarm Type");
421 FHE_CONST(aft
, Date
, "Date");
422 FHE_CONST(aft
, Relative
, "Relative");
424 FHE(pft
, PriorityFlagType
, PriorityFlag
, "Priority");
425 FHE_CONST(pft
, High
, "High");
426 FHE_CONST(pft
, Normal
, "Normal");
427 FHE_CONST(pft
, Low
, "Low");
429 FHE(sft
, StatusFlagType
, StatusFlag
, "Status");
430 FHE_CONST(sft
, NotStarted
, "Not Started");
431 FHE_CONST(sft
, InProgress
, "In Progress");
432 FHE_CONST(sft
, Completed
, "Completed");
433 FHE_CONST(sft
, Waiting
, "Waiting");
434 FHE_CONST(sft
, Deferred
, "Deferred");
436 FHP(Unknowns
, "Unknown Fields");
438 // and finally, the RecurBase fields
439 RECUR_BASE_FIELD_HANDLES
444 std::string
Task::GetDescription() const
449 void Task::Dump(std::ostream
&os
) const
451 ios_format_state
state(os
);
453 static const char *PriorityName
[] = { "High", "Normal", "Low" };
454 static const char *StatusName
[] = { "Not Started", "In Progress",
455 "Completed", "Waiting", "Deferred" };
456 static const char *DayNames
[] = { "Sun", "Mon", "Tue", "Wed",
457 "Thu", "Fri", "Sat" };
458 static const char *MonthNames
[] = { "Jan", "Feb", "Mar", "Apr",
459 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
460 static const char *AlarmTypeName
[] = { "None", "By Date", "Relative" };
462 os
<< "Task entry: 0x" << setbase(16) << RecordId
463 << " (" << (unsigned int)RecType
<< ")\n";
465 // cycle through the type table
466 for( const FieldLink
<Task
> *b
= TaskFieldLinks
;
467 b
->type
!= TSKFC_END
;
471 const std::string
&s
= this->*(b
->strMember
);
473 os
<< " " << b
->name
<< ": " << s
<< "\n";
475 else if( b
->timeMember
) {
476 TimeT t
= this->*(b
->timeMember
);
478 os
<< " " << b
->name
<< ": " << t
<< "\n";
482 os
<< " Due Date Flag: " << (DueDateFlag
? "true" : "false") << "\n";
483 os
<< " Alarm Flag: " << (AlarmFlag
? "true" : "false") << "\n";
484 os
<< " Priority: " << PriorityName
[PriorityFlag
] << "\n";
485 os
<< " Status: " << StatusName
[StatusFlag
] << "\n";
487 os
<< " Alarm Type: " << AlarmTypeName
[AlarmType
] << "\n";
490 os
<< " Time Zone: " << GetStaticTimeZone(TimeZoneCode
)->Name
<< "\n";
492 // print recurrence data if available
493 os
<< " Recurring: " << (Recurring
? "yes" : "no") << "\n";
495 switch( RecurringType
)
498 os
<< " Every day.\n";
502 os
<< " Every month on the "
504 << (DayOfMonth
== 1 ? "st" : "")
505 << (DayOfMonth
== 2 ? "nd" : "")
506 << (DayOfMonth
== 3 ? "rd" : "")
507 << (DayOfMonth
> 3 ? "th" : "")
512 os
<< " Every month on the "
513 << DayNames
[DayOfWeek
]
520 os
<< " Every year on "
521 << MonthNames
[MonthOfYear
-1]
522 << " " << DayOfMonth
<< "\n";
526 os
<< " Every year in " << MonthNames
[MonthOfYear
-1]
528 << DayNames
[DayOfWeek
]
529 << " of week " << WeekOfMonth
<< "\n";
533 os
<< " Every week on: ";
534 if( WeekDays
& CAL_WD_SUN
) os
<< "Sun ";
535 if( WeekDays
& CAL_WD_MON
) os
<< "Mon ";
536 if( WeekDays
& CAL_WD_TUE
) os
<< "Tue ";
537 if( WeekDays
& CAL_WD_WED
) os
<< "Wed ";
538 if( WeekDays
& CAL_WD_THU
) os
<< "Thu ";
539 if( WeekDays
& CAL_WD_FRI
) os
<< "Fri ";
540 if( WeekDays
& CAL_WD_SAT
) os
<< "Sat ";
545 os
<< " Unknown recurrence type\n";
549 os
<< " Interval: " << Interval
<< "\n";
552 os
<< " Ends: never\n";
554 os
<< " Ends: " << RecurringEndTime
<< "\n";
557 if( Categories
.size() ) {
559 Categories
.CategoryList2Str(display
);
560 os
<< " Categories: " << display
<< "\n";
567 bool Task::operator<(const Task
&other
) const
569 if( StartTime
!= other
.StartTime
)
570 return StartTime
< other
.StartTime
;
571 if( AlarmTime
!= other
.AlarmTime
)
572 return AlarmTime
< other
.AlarmTime
;
574 int cmp
= Summary
.compare(other
.Summary
);
576 cmp
= Notes
.compare(other
.Notes
);