Updated copyright dates for 2009
[barry/progweb.git] / src / r_task.cc
blob64f723b0d9f04ac07dca05cd7dff62b5b39c7b00
1 ///
2 /// \file r_task.cc
3 /// Record parsing class for the task database.
4 ///
6 /*
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.
23 #include "r_task.h"
24 #include "r_calendar.h" // for CAL_* defines
25 #include "record-internal.h"
26 #include "protostructs.h"
27 #include "data.h"
28 #include "time.h"
29 #include "debug.h"
30 #include <ostream>
31 #include <iomanip>
32 #include <string.h>
34 using namespace std;
35 using namespace Barry::Protocol;
37 namespace Barry {
39 ///////////////////////////////////////////////////////////////////////////////
40 // Task Class
42 // Task Field Codes
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 },
68 Task::Task()
70 Clear();
73 Task::~Task()
77 const unsigned char* Task::ParseField(const unsigned char *begin,
78 const unsigned char *end,
79 const IConverter *ic)
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
86 return begin;
88 if( !btohs(field->size) ) // if field has no size, something's up
89 return begin;
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'");
95 return begin;
98 // cycle through the type table
99 for( FieldLink<Task> *b = TaskFieldLinks;
100 b->type != TSKFC_END;
101 b++ )
103 if( b->type == field->type ) {
104 if( b->strMember ) {
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);
112 return begin;
116 // handle special cases
117 switch( field->type )
119 case TSKFC_PRIORITY:
120 if( field->u.raw[0] > Low ) {
121 throw Error( "Task::ParseField: priority field out of bounds" );
123 else {
124 PriorityFlag = (PriorityFlagType)field->u.raw[0];
126 return begin;
128 case TSKFC_STATUS:
129 if( field->u.raw[0] > Deferred ) {
130 throw Error( "Task::ParseField: priority field out of bounds" );
132 else {
133 StatusFlag = (StatusFlagType)field->u.raw[0];
135 return begin;
137 case TSKFC_TIMEZONE_CODE:
138 if( btohs(field->size) == 4 ) {
139 TimeZoneCode = btohs(field->u.code);
141 else {
142 throw Error("Task::ParseField: not enough data in time zone code field");
144 return begin;
146 case TSKFC_RECURRENCE_DATA:
147 if( btohs(field->size) >= CALENDAR_RECURRENCE_DATA_FIELD_SIZE ) {
148 Recurring = true;
149 ParseRecurrenceData(&field->u.raw[0]);
151 else {
152 throw Error("Task::ParseField: not enough data in recurrence data field");
154 return begin;
156 case TSKFC_DUE_FLAG:
157 DueDateFlag = field->u.raw[0];
158 return begin;
160 case TSKFC_ALARM_TYPE:
161 if( field->u.raw[0] > Relative ) {
162 throw Error("Task::ParseField: AlarmType out of bounds" );
164 else {
165 AlarmType = (AlarmFlagType)field->u.raw[0];
167 return begin;
170 // if still not handled, add to the Unknowns list
171 UnknownField uf;
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
177 return begin;
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);
187 if( Interval < 1 )
188 Interval = 1; // must always be >= 1
190 if( rec->endTime == 0xffffffff ) {
191 Perpetual = true;
193 else {
194 RecurringEndTime = min2time(rec->endTime);
195 Perpetual = false;
198 switch( rec->type )
200 case CRDF_TYPE_DAY:
201 RecurringType = Day;
202 // no extra data
203 break;
205 case CRDF_TYPE_MONTH_BY_DATE:
206 RecurringType = MonthByDate;
207 DayOfMonth = rec->u.month_by_date.monthDay;
208 break;
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;
214 break;
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;
220 break;
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;
227 break;
229 case CRDF_TYPE_WEEK:
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;
236 break;
238 default:
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
246 // available in data
247 void Task::BuildRecurrenceData(void *data)
249 if( !Recurring )
250 throw Error("Task::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
252 CalendarRecurrenceDataField *rec = (CalendarRecurrenceDataField*) data;
254 // set all to zero
255 memset(data, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE);
257 rec->interval = htobs(Interval);
258 rec->startTime = time2min(StartTime);
259 if( Perpetual )
260 rec->endTime = 0xffffffff;
261 else
262 rec->endTime = time2min(RecurringEndTime);
264 switch( RecurringType )
266 case Day:
267 rec->type = CRDF_TYPE_DAY;
268 // no extra data
269 break;
271 case MonthByDate:
272 rec->type = CRDF_TYPE_MONTH_BY_DATE;
273 rec->u.month_by_date.monthDay = DayOfMonth;
274 break;
276 case MonthByDay:
277 rec->type = CRDF_TYPE_MONTH_BY_DAY;
278 rec->u.month_by_day.weekDay = DayOfWeek;
279 rec->u.month_by_day.week = WeekOfMonth;
280 break;
282 case YearByDate:
283 rec->type = CRDF_TYPE_YEAR_BY_DATE;
284 rec->u.year_by_date.monthDay = DayOfMonth;
285 rec->u.year_by_date.month = MonthOfYear;
286 break;
288 case YearByDay:
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;
293 break;
295 case Week:
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;
302 break;
304 default:
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);
323 void Task::Clear()
325 Summary.clear();
326 Notes.clear();
327 Categories.clear();
328 StartTime = DueTime = AlarmTime = 0;
330 PriorityFlag = (PriorityFlagType)0;
331 StatusFlag = (StatusFlagType)0;
332 AlarmType = (AlarmFlagType)0;
334 TaskType = 0;
336 Perpetual = false;
337 DueDateFlag = false;
338 Recurring = false;
340 TimeZoneCode = GetTimeZoneCode( 0, 0 );
342 Unknowns.clear();
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;
362 b++ )
364 if( b->strMember ) {
365 const std::string &s = this->*(b->strMember);
366 if( s.size() )
367 os << " " << b->name << ": " << s << "\n";
369 else if( b->timeMember ) {
370 time_t t = this->*(b->timeMember);
371 if( t > 0 )
372 os << " " << b->name << ": " << ctime(&t);
376 os << " Priority: " << PriorityName[PriorityFlag] << "\n";
377 os << " Status: " << StatusName[StatusFlag] << "\n";
378 if( AlarmType ) {
379 os << " Alarm Type: " << AlarmTypeName[AlarmType] << "\n";
382 // print recurrence data if available
383 os << " Recurring: " << (Recurring ? "yes" : "no") << "\n";
384 if( Recurring ) {
385 switch( RecurringType )
387 case Day:
388 os << " Every day.\n";
389 break;
391 case MonthByDate:
392 os << " Every month on the "
393 << DayOfMonth
394 << (DayOfMonth == 1 ? "st" : "")
395 << (DayOfMonth == 2 ? "nd" : "")
396 << (DayOfMonth == 3 ? "rd" : "")
397 << (DayOfMonth > 3 ? "th" : "")
398 << "\n";
399 break;
401 case MonthByDay:
402 os << " Every month on the "
403 << DayNames[DayOfWeek]
404 << " of week "
405 << WeekOfMonth
406 << "\n";
407 break;
409 case YearByDate:
410 os << " Every year on "
411 << MonthNames[MonthOfYear-1]
412 << " " << DayOfMonth << "\n";
413 break;
415 case YearByDay:
416 os << " Every year in " << MonthNames[MonthOfYear-1]
417 << " on "
418 << DayNames[DayOfWeek]
419 << " of week " << WeekOfMonth << "\n";
420 break;
422 case Week:
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 ";
431 os << "\n";
432 break;
434 default:
435 os << " Unknown recurrence type\n";
436 break;
439 os << " Interval: " << Interval << "\n";
441 if( Perpetual )
442 os << " Ends: never\n";
443 else
444 os << " Ends: " << ctime(&RecurringEndTime);
447 os << Unknowns;
448 os << "\n\n";
451 } // namespace Barry