3 /// Record parsing class for the task database.
7 Copyright (C) 2005-2013, 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.
25 #include "r_calendar.h" // for CAL_* defines
26 #include "r_recur_base-int.h"
27 #include "record-internal.h"
28 #include "protostructs.h"
36 #include "ios_state.h"
39 using namespace Barry::Protocol
;
44 ///////////////////////////////////////////////////////////////////////////////
45 // Task Class, static members
48 // Note! These functions currently only pass the same values through.
49 // In actuality, these are technically two different values:
50 // one on the raw protocol side, and the other part of the
51 // guaranteed Barry API. If the Blackberry ever changes the
52 // meanings for these codes, do the translation here.
55 Task::AlarmFlagType
Task::AlarmProto2Rec(uint8_t a
)
57 return (AlarmFlagType
)a
;
60 uint8_t Task::AlarmRec2Proto(AlarmFlagType a
)
65 Task::PriorityFlagType
Task::PriorityProto2Rec(uint8_t p
)
67 return (PriorityFlagType
)p
;
70 uint8_t Task::PriorityRec2Proto(PriorityFlagType p
)
75 Task::StatusFlagType
Task::StatusProto2Rec(uint8_t s
)
77 return (StatusFlagType
)s
;
80 uint8_t Task::StatusRec2Proto(StatusFlagType s
)
86 ///////////////////////////////////////////////////////////////////////////////
90 #define TSKFC_TASK_TYPE 0x01
91 #define TSKFC_TITLE 0x02
92 #define TSKFC_NOTES 0x03
93 #define TSKFC_DUE_TIME 0x05
94 #define TSKFC_START_TIME 0x06 // This is fuzzy... most devices seem
95 // to anchor this value == to DUE_TIME
96 #define TSKFC_DUE_FLAG 0x08
97 #define TSKFC_STATUS 0x09
98 #define TSKFC_PRIORITY 0x0a
99 #define TSKFC_ALARM_TYPE 0x0e
100 #define TSKFC_ALARM_TIME 0x0f
101 #define TSKFC_TIMEZONE_CODE 0x10
102 #define TSKFC_CATEGORIES 0x11
103 #define TSKFC_ALARM_FLAG 0x12
104 #define TSKFC_END 0xffff
106 static FieldLink
<Task
> TaskFieldLinks
[] = {
107 { TSKFC_TITLE
, N_("Summary"), 0, 0, &Task::Summary
, 0, 0, 0, 0, true },
108 { TSKFC_NOTES
, N_("Notes"), 0, 0, &Task::Notes
, 0, 0, 0, 0, true },
109 { TSKFC_START_TIME
, N_("Start Time"), 0, 0, 0, 0, &Task::StartTime
, 0, 0, false },
110 { TSKFC_DUE_TIME
, N_("Due Time"), 0, 0, 0, 0, &Task::DueTime
, 0, 0, false },
111 { TSKFC_ALARM_TIME
, N_("Alarm Time"), 0, 0, 0, 0, &Task::AlarmTime
, 0, 0, false },
112 { TSKFC_END
, N_("End of List"), 0, 0, 0, 0, 0, 0, 0, false },
124 const unsigned char* Task::ParseField(const unsigned char *begin
,
125 const unsigned char *end
,
126 const IConverter
*ic
)
128 const CommonField
*field
= (const CommonField
*) begin
;
130 // advance and check size
131 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
132 if( begin
> end
) // if begin==end, we are ok
135 if( !btohs(field
->size
) ) // if field has no size, something's up
138 if( field
->type
== TSKFC_TASK_TYPE
) {
139 if( field
->u
.raw
[0] != 't' ) {
140 throw Error(_("Task::ParseField: Task Type is not 't'"));
145 // cycle through the type table
146 for( FieldLink
<Task
> *b
= TaskFieldLinks
;
147 b
->type
!= TSKFC_END
;
150 if( b
->type
== field
->type
) {
152 std::string
&s
= this->*(b
->strMember
);
153 s
= ParseFieldString(field
);
154 if( b
->iconvNeeded
&& ic
)
156 return begin
; // done!
158 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
159 TimeT
&t
= this->*(b
->timeMember
);
160 t
.Time
= min2time(field
->u
.min1900
);
166 // handle special cases
167 switch( field
->type
)
170 if( field
->u
.raw
[0] > TR_PRIORITY_RANGE_HIGH
) {
171 throw Error(_("Task::ParseField: priority field out of bounds"));
174 PriorityFlag
= PriorityProto2Rec(field
->u
.raw
[0]);
179 if( field
->u
.raw
[0] > TR_STATUS_RANGE_HIGH
) {
180 throw Error(_("Task::ParseField: priority field out of bounds"));
183 StatusFlag
= StatusProto2Rec(field
->u
.raw
[0]);
187 case TSKFC_TIMEZONE_CODE
:
188 if( btohs(field
->size
) == 4 ) {
189 TimeZoneCode
= btohs(field
->u
.code
);
190 TimeZoneValid
= true;
193 throw Error(_("Task::ParseField: not enough data in time zone code field"));
198 // the DueDateFlag is not available on really old devices
199 // such as the 7750, and if the DueTime is available,
200 // we'll just save it. There is no further need for this
201 // value that we know of yet, so just ignore it for now.
204 case TSKFC_ALARM_FLAG
:
205 // the AlarmFlag is not available on really old devices
206 // such as the 7750, and if the AlarmTime is available,
207 // we'll just save it. There is no further need for this
208 // value that we know of yet, so just ignore it for now.
211 case TSKFC_ALARM_TYPE
:
212 if( field
->u
.raw
[0] > TR_ALARM_RANGE_HIGH
) {
213 throw Error(_("Task::ParseField: AlarmType out of bounds"));
216 AlarmType
= AlarmProto2Rec(field
->u
.raw
[0]);
220 case TSKFC_CATEGORIES
:
222 std::string catstring
= ParseFieldString(field
);
224 catstring
= ic
->FromBB(catstring
);
225 Categories
.CategoryStr2List(catstring
);
230 // base class handles recurring data
231 if( RecurBase::ParseField(field
->type
, field
->u
.raw
, btohs(field
->size
), ic
) )
234 // if still not handled, add to the Unknowns list
236 uf
.type
= field
->type
;
237 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
238 Unknowns
.push_back(uf
);
240 // return new pointer for next field
244 void Task::ParseHeader(const Data
&data
, size_t &offset
)
246 // no header in Task records
249 void Task::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
251 const unsigned char *finish
= ParseCommonFields(*this,
252 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
253 offset
+= finish
- (data
.GetData() + offset
);
257 void Task::Validate() const
259 RecurBase::Validate();
262 void Task::BuildHeader(Data
&data
, size_t &offset
) const
264 // no header in Task records
271 /// Build fields part of record.
273 void Task::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
278 // Note: we do not use the FieldLink table, since the firmware
279 // on many devices apears flakey, so we try to write as close to
280 // the same order as we see coming from the device. Unfortunately,
281 // this doesn't really help, for the Tasks corruption bug, but
282 // we'll keep the flexibility for now.
285 // tack on the 't' task type field first
286 BuildField(data
, offset
, TSKFC_TASK_TYPE
, 't');
289 if( Summary
.size() ) {
290 std::string s
= (ic
) ? ic
->ToBB(Summary
) : Summary
;
291 BuildField(data
, offset
, TSKFC_TITLE
, s
);
294 BuildField(data
, offset
, TSKFC_STATUS
, (uint32_t) StatusRec2Proto(StatusFlag
));
295 BuildField(data
, offset
, TSKFC_PRIORITY
, (uint32_t) PriorityRec2Proto(PriorityFlag
));
297 if( TimeZoneValid
) {
298 // the time zone code field is 4 bytes, but we only use
299 // the first two... pad it with zeros
300 uint32_t code
= TimeZoneCode
;
301 BuildField(data
, offset
, TSKFC_TIMEZONE_CODE
, code
);
304 // make sure StartTime matches DueTime, by writing it manually...
305 /// not sure why StartTime exists, but oh well. :-)
306 if( DueTime
.IsValid() ) {
307 // we use DueTime here, with the START_TIME code,
308 // instead of StartTime, since the function is const
309 BuildField1900(data
, offset
, TSKFC_START_TIME
, DueTime
);
311 // then DueTime, with flag first, then time
312 BuildField(data
, offset
, TSKFC_DUE_FLAG
, (uint32_t) 1);
313 BuildField1900(data
, offset
, TSKFC_DUE_TIME
, DueTime
);
316 if( AlarmTime
.IsValid() ) {
317 BuildField(data
, offset
, TSKFC_ALARM_FLAG
, (uint32_t) 1);
318 BuildField(data
, offset
, TSKFC_ALARM_TYPE
, AlarmRec2Proto(AlarmType
));
319 BuildField1900(data
, offset
, TSKFC_ALARM_TIME
, AlarmTime
);
323 if( Categories
.size() ) {
325 Categories
.CategoryList2Str(store
);
326 BuildField(data
, offset
, TSKFC_CATEGORIES
, ic
? ic
->ToBB(store
) : store
);
331 std::string s
= (ic
) ? ic
->ToBB(Notes
) : Notes
;
332 BuildField(data
, offset
, TSKFC_NOTES
, s
);
336 CalendarRecurrenceDataField recur
;
337 BuildRecurrenceData(StartTime
.Time
, &recur
);
338 BuildField(data
, offset
, RecurBase::RecurringFieldType(),
339 &recur
, CALENDAR_RECURRENCE_DATA_FIELD_SIZE
);
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
);
356 // clear the base class first
360 RecType
= GetDefaultRecType();
372 TimeZoneCode
= GetStaticTimeZoneCode( 0, 0 ); // default to GMT
373 TimeZoneValid
= false;
376 PriorityFlag
= Normal
;
377 StatusFlag
= NotStarted
;
382 const FieldHandle
<Task
>::ListT
& Task::GetFieldHandles()
384 static FieldHandle
<Task
>::ListT fhv
;
389 #undef CONTAINER_OBJECT_NAME
390 #define CONTAINER_OBJECT_NAME fhv
392 #undef RECORD_CLASS_NAME
393 #define RECORD_CLASS_NAME Task
395 FHP(RecType
, _("Record Type Code"));
396 FHP(RecordId
, _("Unique Record ID"));
398 FHD(Summary
, _("Summary"), TSKFC_TITLE
, true);
399 FHD(Notes
, _("Notes"), TSKFC_NOTES
, true);
400 FHD(Categories
, _("Categories"), TSKFC_CATEGORIES
, true);
401 FHP(UID
, _("UID")); // FIXME - not linked to any device field??
403 FHD(StartTime
, _("Start Time"), TSKFC_START_TIME
, false);
404 FHD(DueTime
, _("Due Time"), TSKFC_DUE_TIME
, false);
405 FHD(AlarmTime
, _("Alarm Time"), TSKFC_ALARM_TIME
, false);
406 FHD(TimeZoneCode
, _("Time Zone Code"), TSKFC_TIMEZONE_CODE
, false);
407 FHP(TimeZoneValid
, _("Time Zone Code Valid"));
409 FHE(aft
, AlarmFlagType
, AlarmType
, _("Alarm Type"));
410 FHE_CONST(aft
, Date
, _("Date"));
411 FHE_CONST(aft
, Relative
, _("Relative"));
413 FHE(pft
, PriorityFlagType
, PriorityFlag
, _("Priority"));
414 FHE_CONST(pft
, High
, _("High"));
415 FHE_CONST(pft
, Normal
, _("Normal"));
416 FHE_CONST(pft
, Low
, _("Low"));
418 FHE(sft
, StatusFlagType
, StatusFlag
, _("Status"));
419 FHE_CONST(sft
, NotStarted
, _("Not Started"));
420 FHE_CONST(sft
, InProgress
, _("In Progress"));
421 FHE_CONST(sft
, Completed
, _("Completed"));
422 FHE_CONST(sft
, Waiting
, _("Waiting"));
423 FHE_CONST(sft
, Deferred
, _("Deferred"));
425 FHP(Unknowns
, _("Unknown Fields"));
427 // and finally, the RecurBase fields
428 RECUR_BASE_FIELD_HANDLES
433 std::string
Task::GetDescription() const
438 void Task::Dump(std::ostream
&os
) const
440 ios_format_state
state(os
);
442 static const char *PriorityName
[] = {
447 static const char *StatusName
[] = {
454 static const char *AlarmTypeName
[] = {
460 os
<< _("Task entry: ") << "0x" << setbase(16) << RecordId
461 << " (" << (unsigned int)RecType
<< ")\n";
463 // cycle through the type table
464 for( const FieldLink
<Task
> *b
= TaskFieldLinks
;
465 b
->type
!= TSKFC_END
;
469 const std::string
&s
= this->*(b
->strMember
);
471 os
<< " " << gettext(b
->name
) << ": " << Cr2LfWrapper(s
) << "\n";
473 else if( b
->timeMember
) {
474 TimeT t
= this->*(b
->timeMember
);
476 os
<< " " << gettext(b
->name
) << ": " << t
<< "\n";
480 os
<< _(" Priority: ") << gettext(PriorityName
[PriorityFlag
]) << "\n";
481 os
<< _(" Status: ") << gettext(StatusName
[StatusFlag
]) << "\n";
483 os
<< _(" Alarm Type: ") << gettext(AlarmTypeName
[AlarmType
]) << "\n";
486 os
<< _(" Time Zone: ") << gettext(GetStaticTimeZone(TimeZoneCode
)->Name
) << "\n";
488 // print recurrence data if available
491 if( Categories
.size() ) {
493 Categories
.CategoryList2Str(display
);
494 os
<< _(" Categories: ") << display
<< "\n";
501 bool Task::operator<(const Task
&other
) const
503 if( StartTime
!= other
.StartTime
)
504 return StartTime
< other
.StartTime
;
505 if( AlarmTime
!= other
.AlarmTime
)
506 return AlarmTime
< other
.AlarmTime
;
508 int cmp
= Summary
.compare(other
.Summary
);
510 cmp
= Notes
.compare(other
.Notes
);