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_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 },
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
133 if( !btohs(field
->size
) ) // if field has no size, something's up
136 if( field
->type
== TSKFC_TASK_TYPE
) {
137 if( field
->u
.raw
[0] != 't' ) {
138 throw Error("Task::ParseField: Task Type is not 't'");
143 // cycle through the type table
144 for( FieldLink
<Task
> *b
= TaskFieldLinks
;
145 b
->type
!= TSKFC_END
;
148 if( b
->type
== field
->type
) {
150 std::string
&s
= this->*(b
->strMember
);
151 s
= ParseFieldString(field
);
152 if( b
->iconvNeeded
&& ic
)
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
);
163 // handle special cases
164 switch( field
->type
)
167 if( field
->u
.raw
[0] > TR_PRIORITY_RANGE_HIGH
) {
168 throw Error( "Task::ParseField: priority field out of bounds" );
171 PriorityFlag
= PriorityProto2Rec(field
->u
.raw
[0]);
176 if( field
->u
.raw
[0] > TR_STATUS_RANGE_HIGH
) {
177 throw Error( "Task::ParseField: priority field out of bounds" );
180 StatusFlag
= StatusProto2Rec(field
->u
.raw
[0]);
184 case TSKFC_TIMEZONE_CODE
:
185 if( btohs(field
->size
) == 4 ) {
186 TimeZoneCode
= btohs(field
->u
.code
);
187 TimeZoneValid
= true;
190 throw Error("Task::ParseField: not enough data in time zone code field");
195 DueDateFlag
= field
->u
.raw
[0];
198 case TSKFC_ALARM_TYPE
:
199 if( field
->u
.raw
[0] > TR_ALARM_RANGE_HIGH
) {
200 throw Error("Task::ParseField: AlarmType out of bounds" );
203 AlarmType
= AlarmProto2Rec(field
->u
.raw
[0]);
207 case TSKFC_CATEGORIES
:
209 std::string catstring
= ParseFieldString(field
);
211 catstring
= ic
->FromBB(catstring
);
212 Categories
.CategoryStr2List(catstring
);
216 // base class handles recurring data
217 if( RecurBase::ParseField(field
->type
, field
->u
.raw
, btohs(field
->size
), ic
) )
220 // if still not handled, add to the Unknowns list
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
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
257 /// Build fields part of record.
259 void Task::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
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
));
271 BuildField(data
, offset
, TSKFC_DUE_FLAG
, (char) 1);
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
;
287 // print only fields with data
289 const std::string
&field
= this->*(b
->strMember
);
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
);
298 BuildField1900(data
, offset
, b
->type
, t
);
300 else if( b
->postMember
&& b
->postField
) {
301 const std::string
&field
= (this->*(b
->postMember
)).*(b
->postField
);
303 std::string s
= (b
->iconvNeeded
&& ic
) ? ic
->ToBB(field
) : field
;
304 BuildField(data
, offset
, b
->type
, s
);
310 if( Categories
.size() ) {
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
);
330 // clear the base class first
334 RecType
= GetDefaultRecType();
346 TimeZoneCode
= GetStaticTimeZoneCode( 0, 0 ); // default to GMT
347 TimeZoneValid
= false;
349 AlarmType
= (AlarmFlagType
)0;
350 PriorityFlag
= (PriorityFlagType
)0;
351 StatusFlag
= (StatusFlagType
)0;
358 const FieldHandle
<Task
>::ListT
& Task::GetFieldHandles()
360 static FieldHandle
<Task
>::ListT 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
410 std::string
Task::GetDescription() const
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
;
437 const std::string
&s
= this->*(b
->strMember
);
439 os
<< " " << b
->name
<< ": " << s
<< "\n";
441 else if( b
->timeMember
) {
442 TimeT t
= this->*(b
->timeMember
);
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";
452 os
<< " Alarm Type: " << AlarmTypeName
[AlarmType
] << "\n";
455 os
<< " Time Zone: " << GetStaticTimeZone(TimeZoneCode
)->Name
<< "\n";
457 // print recurrence data if available
458 os
<< " Recurring: " << (Recurring
? "yes" : "no") << "\n";
460 switch( RecurringType
)
463 os
<< " Every day.\n";
467 os
<< " Every month on the "
469 << (DayOfMonth
== 1 ? "st" : "")
470 << (DayOfMonth
== 2 ? "nd" : "")
471 << (DayOfMonth
== 3 ? "rd" : "")
472 << (DayOfMonth
> 3 ? "th" : "")
477 os
<< " Every month on the "
478 << DayNames
[DayOfWeek
]
485 os
<< " Every year on "
486 << MonthNames
[MonthOfYear
-1]
487 << " " << DayOfMonth
<< "\n";
491 os
<< " Every year in " << MonthNames
[MonthOfYear
-1]
493 << DayNames
[DayOfWeek
]
494 << " of week " << WeekOfMonth
<< "\n";
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 ";
510 os
<< " Unknown recurrence type\n";
514 os
<< " Interval: " << Interval
<< "\n";
517 os
<< " Ends: never\n";
519 os
<< " Ends: " << RecurringEndTime
<< "\n";
522 if( Categories
.size() ) {
524 Categories
.CategoryList2Str(display
);
525 os
<< " Categories: " << display
<< "\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
);
541 cmp
= Notes
.compare(other
.Notes
);