3 /// Record parsing class for the task database.
7 Copyright (C) 2005-2010, 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"
36 using namespace Barry::Protocol
;
41 ///////////////////////////////////////////////////////////////////////////////
42 // Task Class, static members
45 // Note! These functions currently only pass the same values through.
46 // In actuality, these are technically two different values:
47 // one on the raw protocol side, and the other part of the
48 // guaranteed Barry API. If the Blackberry ever changes the
49 // meanings for these codes, do the translation here.
52 Task::AlarmFlagType
Task::AlarmProto2Rec(uint8_t a
)
54 return (AlarmFlagType
)a
;
57 uint8_t Task::AlarmRec2Proto(AlarmFlagType a
)
62 Task::PriorityFlagType
Task::PriorityProto2Rec(uint8_t p
)
64 return (PriorityFlagType
)p
;
67 uint8_t Task::PriorityRec2Proto(PriorityFlagType p
)
72 Task::StatusFlagType
Task::StatusProto2Rec(uint8_t s
)
74 return (StatusFlagType
)s
;
77 uint8_t Task::StatusRec2Proto(StatusFlagType s
)
83 ///////////////////////////////////////////////////////////////////////////////
87 #define TSKFC_TASK_TYPE 0x01
88 #define TSKFC_TITLE 0x02
89 #define TSKFC_NOTES 0x03
90 #define TSKFC_DUE_TIME 0x05
91 #define TSKFC_START_TIME 0x06 // This is fuzzy... most devices seem
92 // to anchor this value == to DUE_TIME
93 #define TSKFC_DUE_FLAG 0x08
94 #define TSKFC_STATUS 0x09
95 #define TSKFC_PRIORITY 0x0a
96 #define TSKFC_ALARM_TYPE 0x0e
97 #define TSKFC_ALARM_TIME 0x0f
98 #define TSKFC_TIMEZONE_CODE 0x10
99 #define TSKFC_CATEGORIES 0x11
100 #define TSKFC_END 0xffff
102 static FieldLink
<Task
> TaskFieldLinks
[] = {
103 { TSKFC_TITLE
, "Summary", 0, 0, &Task::Summary
, 0, 0, 0, 0, true },
104 { TSKFC_NOTES
, "Notes", 0, 0, &Task::Notes
, 0, 0, 0, 0, true },
105 { TSKFC_START_TIME
, "Start Time", 0, 0, 0, 0, &Task::StartTime
, 0, 0, false },
106 { TSKFC_DUE_TIME
, "Due Time", 0, 0, 0, 0, &Task::DueTime
, 0, 0, false },
107 { TSKFC_ALARM_TIME
, "Alarm Time", 0, 0, 0, 0, &Task::AlarmTime
, 0, 0, false },
108 { TSKFC_END
, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
120 const unsigned char* Task::ParseField(const unsigned char *begin
,
121 const unsigned char *end
,
122 const IConverter
*ic
)
124 const CommonField
*field
= (const CommonField
*) begin
;
126 // advance and check size
127 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
128 if( begin
> end
) // if begin==end, we are ok
131 if( !btohs(field
->size
) ) // if field has no size, something's up
134 if( field
->type
== TSKFC_TASK_TYPE
) {
135 if( field
->u
.raw
[0] != 't' ) {
136 throw Error("Task::ParseField: Task Type is not 't'");
141 // cycle through the type table
142 for( FieldLink
<Task
> *b
= TaskFieldLinks
;
143 b
->type
!= TSKFC_END
;
146 if( b
->type
== field
->type
) {
148 std::string
&s
= this->*(b
->strMember
);
149 s
= ParseFieldString(field
);
150 if( b
->iconvNeeded
&& ic
)
152 return begin
; // done!
154 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
155 time_t &t
= this->*(b
->timeMember
);
156 t
= min2time(field
->u
.min1900
);
161 // handle special cases
162 switch( field
->type
)
165 if( field
->u
.raw
[0] > TR_PRIORITY_RANGE_HIGH
) {
166 throw Error( "Task::ParseField: priority field out of bounds" );
169 PriorityFlag
= PriorityProto2Rec(field
->u
.raw
[0]);
174 if( field
->u
.raw
[0] > TR_STATUS_RANGE_HIGH
) {
175 throw Error( "Task::ParseField: priority field out of bounds" );
178 StatusFlag
= StatusProto2Rec(field
->u
.raw
[0]);
182 case TSKFC_TIMEZONE_CODE
:
183 if( btohs(field
->size
) == 4 ) {
184 TimeZoneCode
= btohs(field
->u
.code
);
185 TimeZoneValid
= true;
188 throw Error("Task::ParseField: not enough data in time zone code field");
193 DueDateFlag
= field
->u
.raw
[0];
196 case TSKFC_ALARM_TYPE
:
197 if( field
->u
.raw
[0] > TR_ALARM_RANGE_HIGH
) {
198 throw Error("Task::ParseField: AlarmType out of bounds" );
201 AlarmType
= AlarmProto2Rec(field
->u
.raw
[0]);
205 case TSKFC_CATEGORIES
:
207 std::string catstring
= ParseFieldString(field
);
209 catstring
= ic
->FromBB(catstring
);
210 Categories
.CategoryStr2List(catstring
);
214 // base class handles recurring data
215 if( RecurBase::ParseField(field
->type
, field
->u
.raw
, btohs(field
->size
), ic
) )
218 // if still not handled, add to the Unknowns list
220 uf
.type
= field
->type
;
221 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
222 Unknowns
.push_back(uf
);
224 // return new pointer for next field
228 void Task::ParseHeader(const Data
&data
, size_t &offset
)
230 // no header in Task records
233 void Task::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
235 const unsigned char *finish
= ParseCommonFields(*this,
236 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
237 offset
+= finish
- (data
.GetData() + offset
);
241 void Task::BuildHeader(Data
&data
, size_t &offset
) const
243 // no header in Task records
250 /// Build fields part of record.
252 void Task::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
256 // tack on the 't' task type field first
257 BuildField(data
, offset
, TSKFC_TASK_TYPE
, 't');
259 BuildField(data
, offset
, TSKFC_STATUS
, StatusRec2Proto(StatusFlag
));
260 BuildField(data
, offset
, TSKFC_PRIORITY
, PriorityRec2Proto(PriorityFlag
));
261 BuildField(data
, offset
, TSKFC_ALARM_TYPE
, AlarmRec2Proto(AlarmType
));
264 BuildField(data
, offset
, TSKFC_DUE_FLAG
, (char) 1);
266 BuildField(data
, offset
, TSKFC_DUE_FLAG
, (char) 0);
269 BuildField(data
, offset
, TSKFC_TIMEZONE_CODE
, TimeZoneCode
);
271 // cycle through the type table
272 for( FieldLink
<Task
> *b
= TaskFieldLinks
;
273 b
->type
!= TSKFC_END
;
276 // print only fields with data
278 const std::string
&field
= this->*(b
->strMember
);
280 std::string s
= (b
->iconvNeeded
&& ic
) ? ic
->ToBB(field
) : field
;
281 BuildField(data
, offset
, b
->type
, s
);
284 else if( b
->timeMember
) {
285 time_t t
= this->*(b
->timeMember
);
287 BuildField1900(data
, offset
, b
->type
, t
);
289 else if( b
->postMember
&& b
->postField
) {
290 const std::string
&field
= (this->*(b
->postMember
)).*(b
->postField
);
292 std::string s
= (b
->iconvNeeded
&& ic
) ? ic
->ToBB(field
) : field
;
293 BuildField(data
, offset
, b
->type
, s
);
299 if( Categories
.size() ) {
301 Categories
.CategoryList2Str(store
);
302 BuildField(data
, offset
, TSKFC_CATEGORIES
, ic
? ic
->ToBB(store
) : store
);
305 // and finally save unknowns
306 UnknownsType::const_iterator
307 ub
= Unknowns
.begin(), ue
= Unknowns
.end();
308 for( ; ub
!= ue
; ub
++ ) {
309 BuildField(data
, offset
, *ub
);
312 data
.ReleaseBuffer(offset
);
324 StartTime
= DueTime
= AlarmTime
= 0;
326 PriorityFlag
= (PriorityFlagType
)0;
327 StatusFlag
= (StatusFlagType
)0;
328 AlarmType
= (AlarmFlagType
)0;
332 TimeZoneCode
= GetTimeZoneCode( 0, 0 ); // default to GMT
333 TimeZoneValid
= false;
338 void Task::Dump(std::ostream
&os
) const
340 static const char *PriorityName
[] = { "High", "Normal", "Low" };
341 static const char *StatusName
[] = { "Not Started", "In Progress",
342 "Completed", "Waiting", "Deferred" };
343 static const char *DayNames
[] = { "Sun", "Mon", "Tue", "Wed",
344 "Thu", "Fri", "Sat" };
345 static const char *MonthNames
[] = { "Jan", "Feb", "Mar", "Apr",
346 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
347 static const char *AlarmTypeName
[] = { "None", "By Date", "Relative" };
349 os
<< "Task entry: 0x" << setbase(16) << RecordId
350 << " (" << (unsigned int)RecType
<< ")\n";
352 // cycle through the type table
353 for( const FieldLink
<Task
> *b
= TaskFieldLinks
;
354 b
->type
!= TSKFC_END
;
358 const std::string
&s
= this->*(b
->strMember
);
360 os
<< " " << b
->name
<< ": " << s
<< "\n";
362 else if( b
->timeMember
) {
363 time_t t
= this->*(b
->timeMember
);
365 os
<< " " << b
->name
<< ": " << ctime(&t
);
369 os
<< " Due Date Flag: " << (DueDateFlag
? "true" : "false") << "\n";
370 os
<< " Priority: " << PriorityName
[PriorityFlag
] << "\n";
371 os
<< " Status: " << StatusName
[StatusFlag
] << "\n";
373 os
<< " Alarm Type: " << AlarmTypeName
[AlarmType
] << "\n";
376 os
<< " Time Zone: " << GetTimeZone(TimeZoneCode
)->Name
<< "\n";
378 // print recurrence data if available
379 os
<< " Recurring: " << (Recurring
? "yes" : "no") << "\n";
381 switch( RecurringType
)
384 os
<< " Every day.\n";
388 os
<< " Every month on the "
390 << (DayOfMonth
== 1 ? "st" : "")
391 << (DayOfMonth
== 2 ? "nd" : "")
392 << (DayOfMonth
== 3 ? "rd" : "")
393 << (DayOfMonth
> 3 ? "th" : "")
398 os
<< " Every month on the "
399 << DayNames
[DayOfWeek
]
406 os
<< " Every year on "
407 << MonthNames
[MonthOfYear
-1]
408 << " " << DayOfMonth
<< "\n";
412 os
<< " Every year in " << MonthNames
[MonthOfYear
-1]
414 << DayNames
[DayOfWeek
]
415 << " of week " << WeekOfMonth
<< "\n";
419 os
<< " Every week on: ";
420 if( WeekDays
& CAL_WD_SUN
) os
<< "Sun ";
421 if( WeekDays
& CAL_WD_MON
) os
<< "Mon ";
422 if( WeekDays
& CAL_WD_TUE
) os
<< "Tue ";
423 if( WeekDays
& CAL_WD_WED
) os
<< "Wed ";
424 if( WeekDays
& CAL_WD_THU
) os
<< "Thu ";
425 if( WeekDays
& CAL_WD_FRI
) os
<< "Fri ";
426 if( WeekDays
& CAL_WD_SAT
) os
<< "Sat ";
431 os
<< " Unknown recurrence type\n";
435 os
<< " Interval: " << Interval
<< "\n";
438 os
<< " Ends: never\n";
440 os
<< " Ends: " << ctime(&RecurringEndTime
);
443 if( Categories
.size() ) {
445 Categories
.CategoryList2Str(display
);
446 os
<< " Categories: " << display
<< "\n";
453 bool Task::operator<(const Task
&other
) const
455 if( StartTime
!= other
.StartTime
)
456 return StartTime
< other
.StartTime
;
457 if( AlarmTime
!= other
.AlarmTime
)
458 return AlarmTime
< other
.AlarmTime
;
460 int cmp
= Summary
.compare(other
.Summary
);
462 cmp
= Notes
.compare(other
.Notes
);