3 /// Record parsing class for the task database.
7 Copyright (C) 2005-2008, 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 "record-internal.h"
25 #include "protostructs.h"
34 using namespace Barry::Protocol
;
38 ///////////////////////////////////////////////////////////////////////////////
42 #define TSKFC_TASK_TYPE 0x01
43 #define TSKFC_TITLE 0x02
44 #define TSKFC_NOTES 0x03
45 #define TSKFC_START_TIME 0x05
46 #define TSKFC_DUE_TIME 0x06
47 #define TSKFC_DUE_FLAG 0x08
48 #define TSKFC_STATUS 0x09
49 #define TSKFC_PRIORITY 0x0a
50 #define TSKFC_RECURRENCE_DATA 0x0c
51 #define TSKFC_ALARM_TYPE 0x0e
52 #define TSKFC_ALARM_TIME 0x0f
53 #define TSKFC_TIMEZONE_CODE 0x10
54 #define TSKFC_CATEGORIES 0x11
55 #define TSKFC_END 0xffff
57 FieldLink
<Task
> TaskFieldLinks
[] = {
58 { TSKFC_TITLE
, "Summary", 0, 0, &Task::Summary
, 0, 0 },
59 { TSKFC_NOTES
, "Notes", 0, 0, &Task::Notes
, 0, 0 },
60 { TSKFC_START_TIME
, "Start Time", 0, 0, 0, 0, &Task::StartTime
},
61 { TSKFC_DUE_TIME
, "Due Time", 0, 0, 0, 0, &Task::DueTime
},
62 { TSKFC_ALARM_TIME
, "Alarm Time", 0, 0, 0, 0, &Task::AlarmTime
},
63 { TSKFC_CATEGORIES
, "Categories", 0, 0, &Task::Categories
, 0, 0 },
64 { TSKFC_END
, "End of List", 0, 0, 0, 0, 0 },
76 const unsigned char* Task::ParseField(const unsigned char *begin
,
77 const unsigned char *end
)
79 const CommonField
*field
= (const CommonField
*) begin
;
81 // advance and check size
82 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
83 if( begin
> end
) // if begin==end, we are ok
86 if( !btohs(field
->size
) ) // if field has no size, something's up
89 if( field
->type
== TSKFC_TASK_TYPE
) {
90 if( ( TaskType
= field
->u
.raw
[0] ) != 't' ) {
91 throw Error("Task::ParseField: Task Type is not 't'");
96 // cycle through the type table
97 for( FieldLink
<Task
> *b
= TaskFieldLinks
;
101 if( b
->type
== field
->type
) {
103 std::string
&s
= this->*(b
->strMember
);
104 s
= ParseFieldString(field
);
105 return begin
; // done!
107 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
108 time_t &t
= this->*(b
->timeMember
);
109 t
= min2time(field
->u
.min1900
);
114 // handle special cases
115 switch( field
->type
)
118 if( field
->u
.raw
[0] > Low
) {
119 throw Error( "Task::ParseField: priority field out of bounds" );
122 PriorityFlag
= (PriorityFlagType
)field
->u
.raw
[0];
127 if( field
->u
.raw
[0] > Deferred
) {
128 throw Error( "Task::ParseField: priority field out of bounds" );
131 StatusFlag
= (StatusFlagType
)field
->u
.raw
[0];
135 case TSKFC_TIMEZONE_CODE
:
136 if( btohs(field
->size
) == 4 ) {
137 TimeZoneCode
= btohs(field
->u
.code
);
140 throw Error("Task::ParseField: not enough data in time zone code field");
144 case TSKFC_RECURRENCE_DATA
:
145 if( btohs(field
->size
) >= CALENDAR_RECURRENCE_DATA_FIELD_SIZE
) {
147 ParseRecurrenceData(&field
->u
.raw
[0]);
150 throw Error("Task::ParseField: not enough data in recurrence data field");
155 DueDateFlag
= field
->u
.raw
[0];
158 case TSKFC_ALARM_TYPE
:
159 if( field
->u
.raw
[0] > Relative
) {
160 throw Error("Task::ParseField: AlarmType out of bounds" );
163 AlarmType
= (AlarmFlagType
)field
->u
.raw
[0];
168 // if still not handled, add to the Unknowns list
170 uf
.type
= field
->type
;
171 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
172 Unknowns
.push_back(uf
);
174 // return new pointer for next field
178 // this function assumes the size has already been checked
179 void Task::ParseRecurrenceData(const void *data
)
181 const CalendarRecurrenceDataField
*rec
=
182 (const CalendarRecurrenceDataField
*) data
;
184 Interval
= btohs(rec
->interval
);
186 Interval
= 1; // must always be >= 1
188 if( rec
->endTime
== 0xffffffff ) {
192 RecurringEndTime
= min2time(rec
->endTime
);
203 case CRDF_TYPE_MONTH_BY_DATE
:
204 RecurringType
= MonthByDate
;
205 DayOfMonth
= rec
->u
.month_by_date
.monthDay
;
208 case CRDF_TYPE_MONTH_BY_DAY
:
209 RecurringType
= MonthByDay
;
210 DayOfWeek
= rec
->u
.month_by_day
.weekDay
;
211 WeekOfMonth
= rec
->u
.month_by_day
.week
;
214 case CRDF_TYPE_YEAR_BY_DATE
:
215 RecurringType
= YearByDate
;
216 DayOfMonth
= rec
->u
.year_by_date
.monthDay
;
217 MonthOfYear
= rec
->u
.year_by_date
.month
;
220 case CRDF_TYPE_YEAR_BY_DAY
:
221 RecurringType
= YearByDay
;
222 DayOfWeek
= rec
->u
.year_by_day
.weekDay
;
223 WeekOfMonth
= rec
->u
.year_by_day
.week
;
224 MonthOfYear
= rec
->u
.year_by_day
.month
;
228 RecurringType
= Week
;
230 // Note: this simple copy is only possible since
231 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
232 // If this ever changes, this code will need to change.
233 WeekDays
= rec
->u
.week
.days
;
237 eout("Unknown recurrence data type: 0x"
238 << setbase(16) << (unsigned int) rec
->type
);
239 throw Error("Unknown recurrence data type");
243 // this function assumes there is CALENDAR_RECURRENCE_DATA_FIELD_SIZE bytes
245 void Task::BuildRecurrenceData(void *data
)
248 throw Error("Task::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
250 CalendarRecurrenceDataField
*rec
= (CalendarRecurrenceDataField
*) data
;
253 memset(data
, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE
);
255 rec
->interval
= htobs(Interval
);
256 rec
->startTime
= time2min(StartTime
);
258 rec
->endTime
= 0xffffffff;
260 rec
->endTime
= time2min(RecurringEndTime
);
262 switch( RecurringType
)
265 rec
->type
= CRDF_TYPE_DAY
;
270 rec
->type
= CRDF_TYPE_MONTH_BY_DATE
;
271 rec
->u
.month_by_date
.monthDay
= DayOfMonth
;
275 rec
->type
= CRDF_TYPE_MONTH_BY_DAY
;
276 rec
->u
.month_by_day
.weekDay
= DayOfWeek
;
277 rec
->u
.month_by_day
.week
= WeekOfMonth
;
281 rec
->type
= CRDF_TYPE_YEAR_BY_DATE
;
282 rec
->u
.year_by_date
.monthDay
= DayOfMonth
;
283 rec
->u
.year_by_date
.month
= MonthOfYear
;
287 rec
->type
= CRDF_TYPE_YEAR_BY_DAY
;
288 rec
->u
.year_by_day
.weekDay
= DayOfWeek
;
289 rec
->u
.year_by_day
.week
= WeekOfMonth
;
290 rec
->u
.year_by_day
.month
= MonthOfYear
;
294 rec
->type
= CRDF_TYPE_WEEK
;
296 // Note: this simple copy is only possible since
297 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
298 // If this ever changes, this code will need to change.
299 rec
->u
.week
.days
= WeekDays
;
303 eout("Task::BuildRecurrenceData: "
304 "Unknown recurrence data type: " << rec
->type
);
305 throw Error("Task::BuildRecurrenceData: Unknown recurrence data type");
309 void Task::ParseHeader(const Data
&data
, size_t &offset
)
311 // no header in Task records
314 void Task::ParseFields(const Data
&data
, size_t &offset
)
316 const unsigned char *finish
= ParseCommonFields(*this,
317 data
.GetData() + offset
, data
.GetData() + data
.GetSize());
318 offset
+= finish
- (data
.GetData() + offset
);
326 StartTime
= DueTime
= AlarmTime
= 0;
328 PriorityFlag
= (PriorityFlagType
)0;
329 StatusFlag
= (StatusFlagType
)0;
330 AlarmType
= (AlarmFlagType
)0;
338 TimeZoneCode
= GetTimeZoneCode( 0, 0 );
343 void Task::Dump(std::ostream
&os
) const
345 static const char *PriorityName
[] = { "High", "Normal", "Low" };
346 static const char *StatusName
[] = { "Not Started", "In Progress",
347 "Completed", "Waiting", "Deferred" };
348 static const char *DayNames
[] = { "Sun", "Mon", "Tue", "Wed",
349 "Thu", "Fri", "Sat" };
350 static const char *MonthNames
[] = { "Jan", "Feb", "Mar", "Apr",
351 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
352 static const char *AlarmTypeName
[] = { "None", "By Date", "Relative" };
354 os
<< "Task entry: 0x" << setbase(16) << RecordId
355 << " (" << (unsigned int)RecType
<< ")\n";
357 // cycle through the type table
358 for( const FieldLink
<Task
> *b
= TaskFieldLinks
;
359 b
->type
!= TSKFC_END
;
363 const std::string
&s
= this->*(b
->strMember
);
365 os
<< " " << b
->name
<< ": " << s
<< "\n";
367 else if( b
->timeMember
) {
368 time_t t
= this->*(b
->timeMember
);
370 os
<< " " << b
->name
<< ": " << ctime(&t
);
374 os
<< " Priority: " << PriorityName
[PriorityFlag
] << "\n";
375 os
<< " Status: " << StatusName
[StatusFlag
] << "\n";
377 os
<< " Alarm Type: " << AlarmTypeName
[AlarmType
] << "\n";
380 // print recurrence data if available
381 os
<< " Recurring: " << (Recurring
? "yes" : "no") << "\n";
383 switch( RecurringType
)
386 os
<< " Every day.\n";
390 os
<< " Every month on the "
392 << (DayOfMonth
== 1 ? "st" : "")
393 << (DayOfMonth
== 2 ? "nd" : "")
394 << (DayOfMonth
== 3 ? "rd" : "")
395 << (DayOfMonth
> 3 ? "th" : "")
400 os
<< " Every month on the "
401 << DayNames
[DayOfWeek
]
408 os
<< " Every year on "
409 << MonthNames
[MonthOfYear
-1]
410 << " " << DayOfMonth
<< "\n";
414 os
<< " Every year in " << MonthNames
[MonthOfYear
-1]
416 << DayNames
[DayOfWeek
]
417 << " of week " << WeekOfMonth
<< "\n";
421 os
<< " Every week on: ";
422 if( WeekDays
& CAL_WD_SUN
) os
<< "Sun ";
423 if( WeekDays
& CAL_WD_MON
) os
<< "Mon ";
424 if( WeekDays
& CAL_WD_TUE
) os
<< "Tue ";
425 if( WeekDays
& CAL_WD_WED
) os
<< "Wed ";
426 if( WeekDays
& CAL_WD_THU
) os
<< "Thu ";
427 if( WeekDays
& CAL_WD_FRI
) os
<< "Fri ";
428 if( WeekDays
& CAL_WD_SAT
) os
<< "Sat ";
433 os
<< " Unknown recurrence type\n";
437 os
<< " Interval: " << Interval
<< "\n";
440 os
<< " Ends: never\n";
442 os
<< " Ends: " << ctime(&RecurringEndTime
);