Removed unused ClassType variable from Task record class
[barry.git] / src / r_task.cc
blobeb8934a537804574505a06ce183a098026d61466
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 "iconv.h"
30 #include "debug.h"
31 #include <ostream>
32 #include <iomanip>
33 #include <string.h>
35 using namespace std;
36 using namespace Barry::Protocol;
38 namespace Barry {
40 ///////////////////////////////////////////////////////////////////////////////
41 // Task Class
43 // Task Field Codes
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 },
69 Task::Task()
71 Clear();
74 Task::~Task()
78 const unsigned char* Task::ParseField(const unsigned char *begin,
79 const unsigned char *end,
80 const IConverter *ic)
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
87 return begin;
89 if( !btohs(field->size) ) // if field has no size, something's up
90 return begin;
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'");
96 return begin;
99 // cycle through the type table
100 for( FieldLink<Task> *b = TaskFieldLinks;
101 b->type != TSKFC_END;
102 b++ )
104 if( b->type == field->type ) {
105 if( b->strMember ) {
106 std::string &s = this->*(b->strMember);
107 s = ParseFieldString(field);
108 if( b->iconvNeeded && ic )
109 s = ic->FromBB(s);
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);
115 return begin;
119 // handle special cases
120 switch( field->type )
122 case TSKFC_PRIORITY:
123 if( field->u.raw[0] > Low ) {
124 throw Error( "Task::ParseField: priority field out of bounds" );
126 else {
127 PriorityFlag = (PriorityFlagType)field->u.raw[0];
129 return begin;
131 case TSKFC_STATUS:
132 if( field->u.raw[0] > Deferred ) {
133 throw Error( "Task::ParseField: priority field out of bounds" );
135 else {
136 StatusFlag = (StatusFlagType)field->u.raw[0];
138 return begin;
140 case TSKFC_TIMEZONE_CODE:
141 if( btohs(field->size) == 4 ) {
142 TimeZoneCode = btohs(field->u.code);
144 else {
145 throw Error("Task::ParseField: not enough data in time zone code field");
147 return begin;
149 case TSKFC_RECURRENCE_DATA:
150 if( btohs(field->size) >= CALENDAR_RECURRENCE_DATA_FIELD_SIZE ) {
151 Recurring = true;
152 ParseRecurrenceData(&field->u.raw[0]);
154 else {
155 throw Error("Task::ParseField: not enough data in recurrence data field");
157 return begin;
159 case TSKFC_DUE_FLAG:
160 DueDateFlag = field->u.raw[0];
161 return begin;
163 case TSKFC_ALARM_TYPE:
164 if( field->u.raw[0] > Relative ) {
165 throw Error("Task::ParseField: AlarmType out of bounds" );
167 else {
168 AlarmType = (AlarmFlagType)field->u.raw[0];
170 return begin;
173 // if still not handled, add to the Unknowns list
174 UnknownField uf;
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
180 return begin;
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);
190 if( Interval < 1 )
191 Interval = 1; // must always be >= 1
193 if( rec->endTime == 0xffffffff ) {
194 Perpetual = true;
196 else {
197 RecurringEndTime = min2time(rec->endTime);
198 Perpetual = false;
201 switch( rec->type )
203 case CRDF_TYPE_DAY:
204 RecurringType = Day;
205 // no extra data
206 break;
208 case CRDF_TYPE_MONTH_BY_DATE:
209 RecurringType = MonthByDate;
210 DayOfMonth = rec->u.month_by_date.monthDay;
211 break;
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;
217 break;
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;
223 break;
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;
230 break;
232 case CRDF_TYPE_WEEK:
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;
239 break;
241 default:
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
249 // available in data
250 void Task::BuildRecurrenceData(void *data)
252 if( !Recurring )
253 throw Error("Task::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
255 CalendarRecurrenceDataField *rec = (CalendarRecurrenceDataField*) data;
257 // set all to zero
258 memset(data, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE);
260 rec->interval = htobs(Interval);
261 rec->startTime = time2min(StartTime);
262 if( Perpetual )
263 rec->endTime = 0xffffffff;
264 else
265 rec->endTime = time2min(RecurringEndTime);
267 switch( RecurringType )
269 case Day:
270 rec->type = CRDF_TYPE_DAY;
271 // no extra data
272 break;
274 case MonthByDate:
275 rec->type = CRDF_TYPE_MONTH_BY_DATE;
276 rec->u.month_by_date.monthDay = DayOfMonth;
277 break;
279 case MonthByDay:
280 rec->type = CRDF_TYPE_MONTH_BY_DAY;
281 rec->u.month_by_day.weekDay = DayOfWeek;
282 rec->u.month_by_day.week = WeekOfMonth;
283 break;
285 case YearByDate:
286 rec->type = CRDF_TYPE_YEAR_BY_DATE;
287 rec->u.year_by_date.monthDay = DayOfMonth;
288 rec->u.year_by_date.month = MonthOfYear;
289 break;
291 case YearByDay:
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;
296 break;
298 case Week:
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;
305 break;
307 default:
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);
326 void Task::Clear()
328 Summary.clear();
329 Notes.clear();
330 Categories.clear();
331 StartTime = DueTime = AlarmTime = 0;
333 PriorityFlag = (PriorityFlagType)0;
334 StatusFlag = (StatusFlagType)0;
335 AlarmType = (AlarmFlagType)0;
337 TaskType = 0;
339 Perpetual = false;
340 DueDateFlag = false;
341 Recurring = false;
343 TimeZoneCode = GetTimeZoneCode( 0, 0 );
345 Unknowns.clear();
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;
365 b++ )
367 if( b->strMember ) {
368 const std::string &s = this->*(b->strMember);
369 if( s.size() )
370 os << " " << b->name << ": " << s << "\n";
372 else if( b->timeMember ) {
373 time_t t = this->*(b->timeMember);
374 if( t > 0 )
375 os << " " << b->name << ": " << ctime(&t);
379 os << " Priority: " << PriorityName[PriorityFlag] << "\n";
380 os << " Status: " << StatusName[StatusFlag] << "\n";
381 if( AlarmType ) {
382 os << " Alarm Type: " << AlarmTypeName[AlarmType] << "\n";
385 // print recurrence data if available
386 os << " Recurring: " << (Recurring ? "yes" : "no") << "\n";
387 if( Recurring ) {
388 switch( RecurringType )
390 case Day:
391 os << " Every day.\n";
392 break;
394 case MonthByDate:
395 os << " Every month on the "
396 << DayOfMonth
397 << (DayOfMonth == 1 ? "st" : "")
398 << (DayOfMonth == 2 ? "nd" : "")
399 << (DayOfMonth == 3 ? "rd" : "")
400 << (DayOfMonth > 3 ? "th" : "")
401 << "\n";
402 break;
404 case MonthByDay:
405 os << " Every month on the "
406 << DayNames[DayOfWeek]
407 << " of week "
408 << WeekOfMonth
409 << "\n";
410 break;
412 case YearByDate:
413 os << " Every year on "
414 << MonthNames[MonthOfYear-1]
415 << " " << DayOfMonth << "\n";
416 break;
418 case YearByDay:
419 os << " Every year in " << MonthNames[MonthOfYear-1]
420 << " on "
421 << DayNames[DayOfWeek]
422 << " of week " << WeekOfMonth << "\n";
423 break;
425 case Week:
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 ";
434 os << "\n";
435 break;
437 default:
438 os << " Unknown recurrence type\n";
439 break;
442 os << " Interval: " << Interval << "\n";
444 if( Perpetual )
445 os << " Ends: never\n";
446 else
447 os << " Ends: " << ctime(&RecurringEndTime);
450 os << Unknowns;
451 os << "\n\n";
454 } // namespace Barry