3 /// Record parsing class for the task database.
7 Copyright (C) 2005-2011, 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 "record-internal.h"
26 #include "protostructs.h"
34 #include "ios_state.h"
37 using namespace Barry::Protocol
;
42 ///////////////////////////////////////////////////////////////////////////////
43 // Task Class, static members
46 // Note! These functions currently only pass the same values through.
47 // In actuality, these are technically two different values:
48 // one on the raw protocol side, and the other part of the
49 // guaranteed Barry API. If the Blackberry ever changes the
50 // meanings for these codes, do the translation here.
53 Task::AlarmFlagType
Task::AlarmProto2Rec(uint8_t a
)
55 return (AlarmFlagType
)a
;
58 uint8_t Task::AlarmRec2Proto(AlarmFlagType a
)
63 Task::PriorityFlagType
Task::PriorityProto2Rec(uint8_t p
)
65 return (PriorityFlagType
)p
;
68 uint8_t Task::PriorityRec2Proto(PriorityFlagType p
)
73 Task::StatusFlagType
Task::StatusProto2Rec(uint8_t s
)
75 return (StatusFlagType
)s
;
78 uint8_t Task::StatusRec2Proto(StatusFlagType s
)
84 ///////////////////////////////////////////////////////////////////////////////
88 #define TSKFC_TASK_TYPE 0x01
89 #define TSKFC_TITLE 0x02
90 #define TSKFC_NOTES 0x03
91 #define TSKFC_DUE_TIME 0x05
92 #define TSKFC_START_TIME 0x06 // This is fuzzy... most devices seem
93 // to anchor this value == to DUE_TIME
94 #define TSKFC_DUE_FLAG 0x08
95 #define TSKFC_STATUS 0x09
96 #define TSKFC_PRIORITY 0x0a
97 #define TSKFC_ALARM_TYPE 0x0e
98 #define TSKFC_ALARM_TIME 0x0f
99 #define TSKFC_TIMEZONE_CODE 0x10
100 #define TSKFC_CATEGORIES 0x11
101 #define TSKFC_END 0xffff
103 static FieldLink
<Task
> TaskFieldLinks
[] = {
104 { TSKFC_TITLE
, "Summary", 0, 0, &Task::Summary
, 0, 0, 0, 0, true },
105 { TSKFC_NOTES
, "Notes", 0, 0, &Task::Notes
, 0, 0, 0, 0, true },
106 { TSKFC_START_TIME
, "Start Time", 0, 0, 0, 0, &Task::StartTime
, 0, 0, false },
107 { TSKFC_DUE_TIME
, "Due Time", 0, 0, 0, 0, &Task::DueTime
, 0, 0, false },
108 { TSKFC_ALARM_TIME
, "Alarm Time", 0, 0, 0, 0, &Task::AlarmTime
, 0, 0, false },
109 { TSKFC_END
, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
121 const unsigned char* Task::ParseField(const unsigned char *begin
,
122 const unsigned char *end
,
123 const IConverter
*ic
)
125 const CommonField
*field
= (const CommonField
*) begin
;
127 // advance and check size
128 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
129 if( begin
> end
) // if begin==end, we are ok
132 if( !btohs(field
->size
) ) // if field has no size, something's up
135 if( field
->type
== TSKFC_TASK_TYPE
) {
136 if( field
->u
.raw
[0] != 't' ) {
137 throw Error("Task::ParseField: Task Type is not 't'");
142 // cycle through the type table
143 for( FieldLink
<Task
> *b
= TaskFieldLinks
;
144 b
->type
!= TSKFC_END
;
147 if( b
->type
== field
->type
) {
149 std::string
&s
= this->*(b
->strMember
);
150 s
= ParseFieldString(field
);
151 if( b
->iconvNeeded
&& ic
)
153 return begin
; // done!
155 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
156 time_t &t
= this->*(b
->timeMember
);
157 t
= min2time(field
->u
.min1900
);
162 // handle special cases
163 switch( field
->type
)
166 if( field
->u
.raw
[0] > TR_PRIORITY_RANGE_HIGH
) {
167 throw Error( "Task::ParseField: priority field out of bounds" );
170 PriorityFlag
= PriorityProto2Rec(field
->u
.raw
[0]);
175 if( field
->u
.raw
[0] > TR_STATUS_RANGE_HIGH
) {
176 throw Error( "Task::ParseField: priority field out of bounds" );
179 StatusFlag
= StatusProto2Rec(field
->u
.raw
[0]);
183 case TSKFC_TIMEZONE_CODE
:
184 if( btohs(field
->size
) == 4 ) {
185 TimeZoneCode
= btohs(field
->u
.code
);
186 TimeZoneValid
= true;
189 throw Error("Task::ParseField: not enough data in time zone code field");
194 DueDateFlag
= field
->u
.raw
[0];
197 case TSKFC_ALARM_TYPE
:
198 if( field
->u
.raw
[0] > TR_ALARM_RANGE_HIGH
) {
199 throw Error("Task::ParseField: AlarmType out of bounds" );
202 AlarmType
= AlarmProto2Rec(field
->u
.raw
[0]);
206 case TSKFC_CATEGORIES
:
208 std::string catstring
= ParseFieldString(field
);
210 catstring
= ic
->FromBB(catstring
);
211 Categories
.CategoryStr2List(catstring
);
215 // base class handles recurring data
216 if( RecurBase::ParseField(field
->type
, field
->u
.raw
, btohs(field
->size
), ic
) )
219 // if still not handled, add to the Unknowns list
221 uf
.type
= field
->type
;
222 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
223 Unknowns
.push_back(uf
);
225 // return new pointer for next field
229 void Task::ParseHeader(const Data
&data
, size_t &offset
)
231 // no header in Task records
234 void Task::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
236 const unsigned char *finish
= ParseCommonFields(*this,
237 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
238 offset
+= finish
- (data
.GetData() + offset
);
242 void Task::BuildHeader(Data
&data
, size_t &offset
) const
244 // no header in Task records
251 /// Build fields part of record.
253 void Task::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
257 // tack on the 't' task type field first
258 BuildField(data
, offset
, TSKFC_TASK_TYPE
, 't');
260 BuildField(data
, offset
, TSKFC_STATUS
, StatusRec2Proto(StatusFlag
));
261 BuildField(data
, offset
, TSKFC_PRIORITY
, PriorityRec2Proto(PriorityFlag
));
262 BuildField(data
, offset
, TSKFC_ALARM_TYPE
, AlarmRec2Proto(AlarmType
));
265 BuildField(data
, offset
, TSKFC_DUE_FLAG
, (char) 1);
267 BuildField(data
, offset
, TSKFC_DUE_FLAG
, (char) 0);
269 if( TimeZoneValid
) {
270 // the time zone code field is 4 bytes, but we only use
271 // the first two... pad it with zeros
272 uint32_t code
= TimeZoneCode
;
273 BuildField(data
, offset
, TSKFC_TIMEZONE_CODE
, code
);
276 // cycle through the type table
277 for( FieldLink
<Task
> *b
= TaskFieldLinks
;
278 b
->type
!= TSKFC_END
;
281 // print only fields with data
283 const std::string
&field
= this->*(b
->strMember
);
285 std::string s
= (b
->iconvNeeded
&& ic
) ? ic
->ToBB(field
) : field
;
286 BuildField(data
, offset
, b
->type
, s
);
289 else if( b
->timeMember
) {
290 time_t t
= this->*(b
->timeMember
);
292 BuildField1900(data
, offset
, b
->type
, t
);
294 else if( b
->postMember
&& b
->postField
) {
295 const std::string
&field
= (this->*(b
->postMember
)).*(b
->postField
);
297 std::string s
= (b
->iconvNeeded
&& ic
) ? ic
->ToBB(field
) : field
;
298 BuildField(data
, offset
, b
->type
, s
);
304 if( Categories
.size() ) {
306 Categories
.CategoryList2Str(store
);
307 BuildField(data
, offset
, TSKFC_CATEGORIES
, ic
? ic
->ToBB(store
) : store
);
310 // and finally save unknowns
311 UnknownsType::const_iterator
312 ub
= Unknowns
.begin(), ue
= Unknowns
.end();
313 for( ; ub
!= ue
; ub
++ ) {
314 BuildField(data
, offset
, *ub
);
317 data
.ReleaseBuffer(offset
);
324 // clear the base class first
328 RecType
= GetDefaultRecType();
336 StartTime
= DueTime
= AlarmTime
= 0;
337 TimeZoneCode
= GetTimeZoneCode( 0, 0 ); // default to GMT
338 TimeZoneValid
= false;
340 AlarmType
= (AlarmFlagType
)0;
341 PriorityFlag
= (PriorityFlagType
)0;
342 StatusFlag
= (StatusFlagType
)0;
349 std::string
Task::GetDescription() const
354 void Task::Dump(std::ostream
&os
) const
356 ios_format_state
state(os
);
358 static const char *PriorityName
[] = { "High", "Normal", "Low" };
359 static const char *StatusName
[] = { "Not Started", "In Progress",
360 "Completed", "Waiting", "Deferred" };
361 static const char *DayNames
[] = { "Sun", "Mon", "Tue", "Wed",
362 "Thu", "Fri", "Sat" };
363 static const char *MonthNames
[] = { "Jan", "Feb", "Mar", "Apr",
364 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
365 static const char *AlarmTypeName
[] = { "None", "By Date", "Relative" };
367 os
<< "Task entry: 0x" << setbase(16) << RecordId
368 << " (" << (unsigned int)RecType
<< ")\n";
370 // cycle through the type table
371 for( const FieldLink
<Task
> *b
= TaskFieldLinks
;
372 b
->type
!= TSKFC_END
;
376 const std::string
&s
= this->*(b
->strMember
);
378 os
<< " " << b
->name
<< ": " << s
<< "\n";
380 else if( b
->timeMember
) {
381 time_t t
= this->*(b
->timeMember
);
383 os
<< " " << b
->name
<< ": " << ctime(&t
);
387 os
<< " Due Date Flag: " << (DueDateFlag
? "true" : "false") << "\n";
388 os
<< " Priority: " << PriorityName
[PriorityFlag
] << "\n";
389 os
<< " Status: " << StatusName
[StatusFlag
] << "\n";
391 os
<< " Alarm Type: " << AlarmTypeName
[AlarmType
] << "\n";
394 os
<< " Time Zone: " << GetTimeZone(TimeZoneCode
)->Name
<< "\n";
396 // print recurrence data if available
397 os
<< " Recurring: " << (Recurring
? "yes" : "no") << "\n";
399 switch( RecurringType
)
402 os
<< " Every day.\n";
406 os
<< " Every month on the "
408 << (DayOfMonth
== 1 ? "st" : "")
409 << (DayOfMonth
== 2 ? "nd" : "")
410 << (DayOfMonth
== 3 ? "rd" : "")
411 << (DayOfMonth
> 3 ? "th" : "")
416 os
<< " Every month on the "
417 << DayNames
[DayOfWeek
]
424 os
<< " Every year on "
425 << MonthNames
[MonthOfYear
-1]
426 << " " << DayOfMonth
<< "\n";
430 os
<< " Every year in " << MonthNames
[MonthOfYear
-1]
432 << DayNames
[DayOfWeek
]
433 << " of week " << WeekOfMonth
<< "\n";
437 os
<< " Every week on: ";
438 if( WeekDays
& CAL_WD_SUN
) os
<< "Sun ";
439 if( WeekDays
& CAL_WD_MON
) os
<< "Mon ";
440 if( WeekDays
& CAL_WD_TUE
) os
<< "Tue ";
441 if( WeekDays
& CAL_WD_WED
) os
<< "Wed ";
442 if( WeekDays
& CAL_WD_THU
) os
<< "Thu ";
443 if( WeekDays
& CAL_WD_FRI
) os
<< "Fri ";
444 if( WeekDays
& CAL_WD_SAT
) os
<< "Sat ";
449 os
<< " Unknown recurrence type\n";
453 os
<< " Interval: " << Interval
<< "\n";
456 os
<< " Ends: never\n";
458 os
<< " Ends: " << ctime(&RecurringEndTime
);
461 if( Categories
.size() ) {
463 Categories
.CategoryList2Str(display
);
464 os
<< " Categories: " << display
<< "\n";
471 bool Task::operator<(const Task
&other
) const
473 if( StartTime
!= other
.StartTime
)
474 return StartTime
< other
.StartTime
;
475 if( AlarmTime
!= other
.AlarmTime
)
476 return AlarmTime
< other
.AlarmTime
;
478 int cmp
= Summary
.compare(other
.Summary
);
480 cmp
= Notes
.compare(other
.Notes
);