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"
36 using namespace Barry::Protocol
;
40 ///////////////////////////////////////////////////////////////////////////////
44 #define TSKFC_TASK_TYPE 0x01
45 #define TSKFC_TITLE 0x02
46 #define TSKFC_NOTES 0x03
47 #define TSKFC_START_TIME 0x05
48 #define TSKFC_DUE_TIME 0x06
49 #define TSKFC_DUE_FLAG 0x08
50 #define TSKFC_STATUS 0x09
51 #define TSKFC_PRIORITY 0x0a
52 #define TSKFC_RECURRENCE_DATA 0x0c
53 #define TSKFC_ALARM_TYPE 0x0e
54 #define TSKFC_ALARM_TIME 0x0f
55 #define TSKFC_TIMEZONE_CODE 0x10
56 #define TSKFC_CATEGORIES 0x11
57 #define TSKFC_END 0xffff
59 static FieldLink
<Task
> TaskFieldLinks
[] = {
60 { TSKFC_TITLE
, "Summary", 0, 0, &Task::Summary
, 0, 0, 0, 0, true },
61 { TSKFC_NOTES
, "Notes", 0, 0, &Task::Notes
, 0, 0, 0, 0, true },
62 { TSKFC_START_TIME
, "Start Time", 0, 0, 0, 0, &Task::StartTime
, 0, 0, false },
63 { TSKFC_DUE_TIME
, "Due Time", 0, 0, 0, 0, &Task::DueTime
, 0, 0, false },
64 { TSKFC_ALARM_TIME
, "Alarm Time", 0, 0, 0, 0, &Task::AlarmTime
, 0, 0, false },
65 { TSKFC_CATEGORIES
, "Categories", 0, 0, &Task::Categories
, 0, 0, 0, 0, false },
66 { TSKFC_END
, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
78 const unsigned char* Task::ParseField(const unsigned char *begin
,
79 const unsigned char *end
,
82 const CommonField
*field
= (const CommonField
*) begin
;
84 // advance and check size
85 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
86 if( begin
> end
) // if begin==end, we are ok
89 if( !btohs(field
->size
) ) // if field has no size, something's up
92 if( field
->type
== TSKFC_TASK_TYPE
) {
93 if( ( TaskType
= field
->u
.raw
[0] ) != 't' ) {
94 throw Error("Task::ParseField: Task Type is not 't'");
99 // cycle through the type table
100 for( FieldLink
<Task
> *b
= TaskFieldLinks
;
101 b
->type
!= TSKFC_END
;
104 if( b
->type
== field
->type
) {
106 std::string
&s
= this->*(b
->strMember
);
107 s
= ParseFieldString(field
);
108 if( b
->iconvNeeded
&& ic
)
110 return begin
; // done!
112 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
113 time_t &t
= this->*(b
->timeMember
);
114 t
= min2time(field
->u
.min1900
);
119 // handle special cases
120 switch( field
->type
)
123 if( field
->u
.raw
[0] > Low
) {
124 throw Error( "Task::ParseField: priority field out of bounds" );
127 PriorityFlag
= (PriorityFlagType
)field
->u
.raw
[0];
132 if( field
->u
.raw
[0] > Deferred
) {
133 throw Error( "Task::ParseField: priority field out of bounds" );
136 StatusFlag
= (StatusFlagType
)field
->u
.raw
[0];
140 case TSKFC_TIMEZONE_CODE
:
141 if( btohs(field
->size
) == 4 ) {
142 TimeZoneCode
= btohs(field
->u
.code
);
145 throw Error("Task::ParseField: not enough data in time zone code field");
149 case TSKFC_RECURRENCE_DATA
:
150 if( btohs(field
->size
) >= CALENDAR_RECURRENCE_DATA_FIELD_SIZE
) {
152 ParseRecurrenceData(&field
->u
.raw
[0]);
155 throw Error("Task::ParseField: not enough data in recurrence data field");
160 DueDateFlag
= field
->u
.raw
[0];
163 case TSKFC_ALARM_TYPE
:
164 if( field
->u
.raw
[0] > Relative
) {
165 throw Error("Task::ParseField: AlarmType out of bounds" );
168 AlarmType
= (AlarmFlagType
)field
->u
.raw
[0];
173 // if still not handled, add to the Unknowns list
175 uf
.type
= field
->type
;
176 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
177 Unknowns
.push_back(uf
);
179 // return new pointer for next field
183 // this function assumes the size has already been checked
184 void Task::ParseRecurrenceData(const void *data
)
186 const CalendarRecurrenceDataField
*rec
=
187 (const CalendarRecurrenceDataField
*) data
;
189 Interval
= btohs(rec
->interval
);
191 Interval
= 1; // must always be >= 1
193 if( rec
->endTime
== 0xffffffff ) {
197 RecurringEndTime
= min2time(rec
->endTime
);
208 case CRDF_TYPE_MONTH_BY_DATE
:
209 RecurringType
= MonthByDate
;
210 DayOfMonth
= rec
->u
.month_by_date
.monthDay
;
213 case CRDF_TYPE_MONTH_BY_DAY
:
214 RecurringType
= MonthByDay
;
215 DayOfWeek
= rec
->u
.month_by_day
.weekDay
;
216 WeekOfMonth
= rec
->u
.month_by_day
.week
;
219 case CRDF_TYPE_YEAR_BY_DATE
:
220 RecurringType
= YearByDate
;
221 DayOfMonth
= rec
->u
.year_by_date
.monthDay
;
222 MonthOfYear
= rec
->u
.year_by_date
.month
;
225 case CRDF_TYPE_YEAR_BY_DAY
:
226 RecurringType
= YearByDay
;
227 DayOfWeek
= rec
->u
.year_by_day
.weekDay
;
228 WeekOfMonth
= rec
->u
.year_by_day
.week
;
229 MonthOfYear
= rec
->u
.year_by_day
.month
;
233 RecurringType
= Week
;
235 // Note: this simple copy is only possible since
236 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
237 // If this ever changes, this code will need to change.
238 WeekDays
= rec
->u
.week
.days
;
242 eout("Unknown recurrence data type: 0x"
243 << setbase(16) << (unsigned int) rec
->type
);
244 throw Error("Unknown recurrence data type");
248 // this function assumes there is CALENDAR_RECURRENCE_DATA_FIELD_SIZE bytes
250 void Task::BuildRecurrenceData(void *data
)
253 throw Error("Task::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
255 CalendarRecurrenceDataField
*rec
= (CalendarRecurrenceDataField
*) data
;
258 memset(data
, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE
);
260 rec
->interval
= htobs(Interval
);
261 rec
->startTime
= time2min(StartTime
);
263 rec
->endTime
= 0xffffffff;
265 rec
->endTime
= time2min(RecurringEndTime
);
267 switch( RecurringType
)
270 rec
->type
= CRDF_TYPE_DAY
;
275 rec
->type
= CRDF_TYPE_MONTH_BY_DATE
;
276 rec
->u
.month_by_date
.monthDay
= DayOfMonth
;
280 rec
->type
= CRDF_TYPE_MONTH_BY_DAY
;
281 rec
->u
.month_by_day
.weekDay
= DayOfWeek
;
282 rec
->u
.month_by_day
.week
= WeekOfMonth
;
286 rec
->type
= CRDF_TYPE_YEAR_BY_DATE
;
287 rec
->u
.year_by_date
.monthDay
= DayOfMonth
;
288 rec
->u
.year_by_date
.month
= MonthOfYear
;
292 rec
->type
= CRDF_TYPE_YEAR_BY_DAY
;
293 rec
->u
.year_by_day
.weekDay
= DayOfWeek
;
294 rec
->u
.year_by_day
.week
= WeekOfMonth
;
295 rec
->u
.year_by_day
.month
= MonthOfYear
;
299 rec
->type
= CRDF_TYPE_WEEK
;
301 // Note: this simple copy is only possible since
302 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
303 // If this ever changes, this code will need to change.
304 rec
->u
.week
.days
= WeekDays
;
308 eout("Task::BuildRecurrenceData: "
309 "Unknown recurrence data type: " << rec
->type
);
310 throw Error("Task::BuildRecurrenceData: Unknown recurrence data type");
314 void Task::ParseHeader(const Data
&data
, size_t &offset
)
316 // no header in Task records
319 void Task::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
321 const unsigned char *finish
= ParseCommonFields(*this,
322 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
323 offset
+= finish
- (data
.GetData() + offset
);
331 StartTime
= DueTime
= AlarmTime
= 0;
333 PriorityFlag
= (PriorityFlagType
)0;
334 StatusFlag
= (StatusFlagType
)0;
335 AlarmType
= (AlarmFlagType
)0;
343 TimeZoneCode
= GetTimeZoneCode( 0, 0 );
348 void Task::Dump(std::ostream
&os
) const
350 static const char *PriorityName
[] = { "High", "Normal", "Low" };
351 static const char *StatusName
[] = { "Not Started", "In Progress",
352 "Completed", "Waiting", "Deferred" };
353 static const char *DayNames
[] = { "Sun", "Mon", "Tue", "Wed",
354 "Thu", "Fri", "Sat" };
355 static const char *MonthNames
[] = { "Jan", "Feb", "Mar", "Apr",
356 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
357 static const char *AlarmTypeName
[] = { "None", "By Date", "Relative" };
359 os
<< "Task entry: 0x" << setbase(16) << RecordId
360 << " (" << (unsigned int)RecType
<< ")\n";
362 // cycle through the type table
363 for( const FieldLink
<Task
> *b
= TaskFieldLinks
;
364 b
->type
!= TSKFC_END
;
368 const std::string
&s
= this->*(b
->strMember
);
370 os
<< " " << b
->name
<< ": " << s
<< "\n";
372 else if( b
->timeMember
) {
373 time_t t
= this->*(b
->timeMember
);
375 os
<< " " << b
->name
<< ": " << ctime(&t
);
379 os
<< " Priority: " << PriorityName
[PriorityFlag
] << "\n";
380 os
<< " Status: " << StatusName
[StatusFlag
] << "\n";
382 os
<< " Alarm Type: " << AlarmTypeName
[AlarmType
] << "\n";
385 // print recurrence data if available
386 os
<< " Recurring: " << (Recurring
? "yes" : "no") << "\n";
388 switch( RecurringType
)
391 os
<< " Every day.\n";
395 os
<< " Every month on the "
397 << (DayOfMonth
== 1 ? "st" : "")
398 << (DayOfMonth
== 2 ? "nd" : "")
399 << (DayOfMonth
== 3 ? "rd" : "")
400 << (DayOfMonth
> 3 ? "th" : "")
405 os
<< " Every month on the "
406 << DayNames
[DayOfWeek
]
413 os
<< " Every year on "
414 << MonthNames
[MonthOfYear
-1]
415 << " " << DayOfMonth
<< "\n";
419 os
<< " Every year in " << MonthNames
[MonthOfYear
-1]
421 << DayNames
[DayOfWeek
]
422 << " of week " << WeekOfMonth
<< "\n";
426 os
<< " Every week on: ";
427 if( WeekDays
& CAL_WD_SUN
) os
<< "Sun ";
428 if( WeekDays
& CAL_WD_MON
) os
<< "Mon ";
429 if( WeekDays
& CAL_WD_TUE
) os
<< "Tue ";
430 if( WeekDays
& CAL_WD_WED
) os
<< "Wed ";
431 if( WeekDays
& CAL_WD_THU
) os
<< "Thu ";
432 if( WeekDays
& CAL_WD_FRI
) os
<< "Fri ";
433 if( WeekDays
& CAL_WD_SAT
) os
<< "Sat ";
438 os
<< " Unknown recurrence type\n";
442 os
<< " Interval: " << Interval
<< "\n";
445 os
<< " Ends: never\n";
447 os
<< " Ends: " << ctime(&RecurringEndTime
);