3 /// Record parsing class for the task database.
7 Copyright (C) 2005-2009, 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"
35 using namespace Barry::Protocol
;
39 ///////////////////////////////////////////////////////////////////////////////
43 #define TSKFC_TASK_TYPE 0x01
44 #define TSKFC_TITLE 0x02
45 #define TSKFC_NOTES 0x03
46 #define TSKFC_START_TIME 0x05
47 #define TSKFC_DUE_TIME 0x06
48 #define TSKFC_DUE_FLAG 0x08
49 #define TSKFC_STATUS 0x09
50 #define TSKFC_PRIORITY 0x0a
51 #define TSKFC_RECURRENCE_DATA 0x0c
52 #define TSKFC_ALARM_TYPE 0x0e
53 #define TSKFC_ALARM_TIME 0x0f
54 #define TSKFC_TIMEZONE_CODE 0x10
55 #define TSKFC_CATEGORIES 0x11
56 #define TSKFC_END 0xffff
58 static FieldLink
<Task
> TaskFieldLinks
[] = {
59 { TSKFC_TITLE
, "Summary", 0, 0, &Task::Summary
, 0, 0 },
60 { TSKFC_NOTES
, "Notes", 0, 0, &Task::Notes
, 0, 0 },
61 { TSKFC_START_TIME
, "Start Time", 0, 0, 0, 0, &Task::StartTime
},
62 { TSKFC_DUE_TIME
, "Due Time", 0, 0, 0, 0, &Task::DueTime
},
63 { TSKFC_ALARM_TIME
, "Alarm Time", 0, 0, 0, 0, &Task::AlarmTime
},
64 { TSKFC_CATEGORIES
, "Categories", 0, 0, &Task::Categories
, 0, 0 },
65 { TSKFC_END
, "End of List", 0, 0, 0, 0, 0 },
77 const unsigned char* Task::ParseField(const unsigned char *begin
,
78 const unsigned char *end
,
81 const CommonField
*field
= (const CommonField
*) begin
;
83 // advance and check size
84 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
85 if( begin
> end
) // if begin==end, we are ok
88 if( !btohs(field
->size
) ) // if field has no size, something's up
91 if( field
->type
== TSKFC_TASK_TYPE
) {
92 if( ( TaskType
= field
->u
.raw
[0] ) != 't' ) {
93 throw Error("Task::ParseField: Task Type is not 't'");
98 // cycle through the type table
99 for( FieldLink
<Task
> *b
= TaskFieldLinks
;
100 b
->type
!= TSKFC_END
;
103 if( b
->type
== field
->type
) {
105 std::string
&s
= this->*(b
->strMember
);
106 s
= ParseFieldString(field
);
107 return begin
; // done!
109 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
110 time_t &t
= this->*(b
->timeMember
);
111 t
= min2time(field
->u
.min1900
);
116 // handle special cases
117 switch( field
->type
)
120 if( field
->u
.raw
[0] > Low
) {
121 throw Error( "Task::ParseField: priority field out of bounds" );
124 PriorityFlag
= (PriorityFlagType
)field
->u
.raw
[0];
129 if( field
->u
.raw
[0] > Deferred
) {
130 throw Error( "Task::ParseField: priority field out of bounds" );
133 StatusFlag
= (StatusFlagType
)field
->u
.raw
[0];
137 case TSKFC_TIMEZONE_CODE
:
138 if( btohs(field
->size
) == 4 ) {
139 TimeZoneCode
= btohs(field
->u
.code
);
142 throw Error("Task::ParseField: not enough data in time zone code field");
146 case TSKFC_RECURRENCE_DATA
:
147 if( btohs(field
->size
) >= CALENDAR_RECURRENCE_DATA_FIELD_SIZE
) {
149 ParseRecurrenceData(&field
->u
.raw
[0]);
152 throw Error("Task::ParseField: not enough data in recurrence data field");
157 DueDateFlag
= field
->u
.raw
[0];
160 case TSKFC_ALARM_TYPE
:
161 if( field
->u
.raw
[0] > Relative
) {
162 throw Error("Task::ParseField: AlarmType out of bounds" );
165 AlarmType
= (AlarmFlagType
)field
->u
.raw
[0];
170 // if still not handled, add to the Unknowns list
172 uf
.type
= field
->type
;
173 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
174 Unknowns
.push_back(uf
);
176 // return new pointer for next field
180 // this function assumes the size has already been checked
181 void Task::ParseRecurrenceData(const void *data
)
183 const CalendarRecurrenceDataField
*rec
=
184 (const CalendarRecurrenceDataField
*) data
;
186 Interval
= btohs(rec
->interval
);
188 Interval
= 1; // must always be >= 1
190 if( rec
->endTime
== 0xffffffff ) {
194 RecurringEndTime
= min2time(rec
->endTime
);
205 case CRDF_TYPE_MONTH_BY_DATE
:
206 RecurringType
= MonthByDate
;
207 DayOfMonth
= rec
->u
.month_by_date
.monthDay
;
210 case CRDF_TYPE_MONTH_BY_DAY
:
211 RecurringType
= MonthByDay
;
212 DayOfWeek
= rec
->u
.month_by_day
.weekDay
;
213 WeekOfMonth
= rec
->u
.month_by_day
.week
;
216 case CRDF_TYPE_YEAR_BY_DATE
:
217 RecurringType
= YearByDate
;
218 DayOfMonth
= rec
->u
.year_by_date
.monthDay
;
219 MonthOfYear
= rec
->u
.year_by_date
.month
;
222 case CRDF_TYPE_YEAR_BY_DAY
:
223 RecurringType
= YearByDay
;
224 DayOfWeek
= rec
->u
.year_by_day
.weekDay
;
225 WeekOfMonth
= rec
->u
.year_by_day
.week
;
226 MonthOfYear
= rec
->u
.year_by_day
.month
;
230 RecurringType
= Week
;
232 // Note: this simple copy is only possible since
233 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
234 // If this ever changes, this code will need to change.
235 WeekDays
= rec
->u
.week
.days
;
239 eout("Unknown recurrence data type: 0x"
240 << setbase(16) << (unsigned int) rec
->type
);
241 throw Error("Unknown recurrence data type");
245 // this function assumes there is CALENDAR_RECURRENCE_DATA_FIELD_SIZE bytes
247 void Task::BuildRecurrenceData(void *data
)
250 throw Error("Task::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
252 CalendarRecurrenceDataField
*rec
= (CalendarRecurrenceDataField
*) data
;
255 memset(data
, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE
);
257 rec
->interval
= htobs(Interval
);
258 rec
->startTime
= time2min(StartTime
);
260 rec
->endTime
= 0xffffffff;
262 rec
->endTime
= time2min(RecurringEndTime
);
264 switch( RecurringType
)
267 rec
->type
= CRDF_TYPE_DAY
;
272 rec
->type
= CRDF_TYPE_MONTH_BY_DATE
;
273 rec
->u
.month_by_date
.monthDay
= DayOfMonth
;
277 rec
->type
= CRDF_TYPE_MONTH_BY_DAY
;
278 rec
->u
.month_by_day
.weekDay
= DayOfWeek
;
279 rec
->u
.month_by_day
.week
= WeekOfMonth
;
283 rec
->type
= CRDF_TYPE_YEAR_BY_DATE
;
284 rec
->u
.year_by_date
.monthDay
= DayOfMonth
;
285 rec
->u
.year_by_date
.month
= MonthOfYear
;
289 rec
->type
= CRDF_TYPE_YEAR_BY_DAY
;
290 rec
->u
.year_by_day
.weekDay
= DayOfWeek
;
291 rec
->u
.year_by_day
.week
= WeekOfMonth
;
292 rec
->u
.year_by_day
.month
= MonthOfYear
;
296 rec
->type
= CRDF_TYPE_WEEK
;
298 // Note: this simple copy is only possible since
299 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
300 // If this ever changes, this code will need to change.
301 rec
->u
.week
.days
= WeekDays
;
305 eout("Task::BuildRecurrenceData: "
306 "Unknown recurrence data type: " << rec
->type
);
307 throw Error("Task::BuildRecurrenceData: Unknown recurrence data type");
311 void Task::ParseHeader(const Data
&data
, size_t &offset
)
313 // no header in Task records
316 void Task::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
318 const unsigned char *finish
= ParseCommonFields(*this,
319 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
320 offset
+= finish
- (data
.GetData() + offset
);
328 StartTime
= DueTime
= AlarmTime
= 0;
330 PriorityFlag
= (PriorityFlagType
)0;
331 StatusFlag
= (StatusFlagType
)0;
332 AlarmType
= (AlarmFlagType
)0;
340 TimeZoneCode
= GetTimeZoneCode( 0, 0 );
345 void Task::Dump(std::ostream
&os
) const
347 static const char *PriorityName
[] = { "High", "Normal", "Low" };
348 static const char *StatusName
[] = { "Not Started", "In Progress",
349 "Completed", "Waiting", "Deferred" };
350 static const char *DayNames
[] = { "Sun", "Mon", "Tue", "Wed",
351 "Thu", "Fri", "Sat" };
352 static const char *MonthNames
[] = { "Jan", "Feb", "Mar", "Apr",
353 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
354 static const char *AlarmTypeName
[] = { "None", "By Date", "Relative" };
356 os
<< "Task entry: 0x" << setbase(16) << RecordId
357 << " (" << (unsigned int)RecType
<< ")\n";
359 // cycle through the type table
360 for( const FieldLink
<Task
> *b
= TaskFieldLinks
;
361 b
->type
!= TSKFC_END
;
365 const std::string
&s
= this->*(b
->strMember
);
367 os
<< " " << b
->name
<< ": " << s
<< "\n";
369 else if( b
->timeMember
) {
370 time_t t
= this->*(b
->timeMember
);
372 os
<< " " << b
->name
<< ": " << ctime(&t
);
376 os
<< " Priority: " << PriorityName
[PriorityFlag
] << "\n";
377 os
<< " Status: " << StatusName
[StatusFlag
] << "\n";
379 os
<< " Alarm Type: " << AlarmTypeName
[AlarmType
] << "\n";
382 // print recurrence data if available
383 os
<< " Recurring: " << (Recurring
? "yes" : "no") << "\n";
385 switch( RecurringType
)
388 os
<< " Every day.\n";
392 os
<< " Every month on the "
394 << (DayOfMonth
== 1 ? "st" : "")
395 << (DayOfMonth
== 2 ? "nd" : "")
396 << (DayOfMonth
== 3 ? "rd" : "")
397 << (DayOfMonth
> 3 ? "th" : "")
402 os
<< " Every month on the "
403 << DayNames
[DayOfWeek
]
410 os
<< " Every year on "
411 << MonthNames
[MonthOfYear
-1]
412 << " " << DayOfMonth
<< "\n";
416 os
<< " Every year in " << MonthNames
[MonthOfYear
-1]
418 << DayNames
[DayOfWeek
]
419 << " of week " << WeekOfMonth
<< "\n";
423 os
<< " Every week on: ";
424 if( WeekDays
& CAL_WD_SUN
) os
<< "Sun ";
425 if( WeekDays
& CAL_WD_MON
) os
<< "Mon ";
426 if( WeekDays
& CAL_WD_TUE
) os
<< "Tue ";
427 if( WeekDays
& CAL_WD_WED
) os
<< "Wed ";
428 if( WeekDays
& CAL_WD_THU
) os
<< "Thu ";
429 if( WeekDays
& CAL_WD_FRI
) os
<< "Fri ";
430 if( WeekDays
& CAL_WD_SAT
) os
<< "Sat ";
435 os
<< " Unknown recurrence type\n";
439 os
<< " Interval: " << Interval
<< "\n";
442 os
<< " Ends: never\n";
444 os
<< " Ends: " << ctime(&RecurringEndTime
);