updated change log to reflect reality
[barry.git] / src / r_calendar.cc
blob9a961c9ddf3fff076c1ab35d07289f982b81c794
1 ///
2 /// \file r_calendar.cc
3 /// Blackberry database record parser class for calendar records.
4 ///
6 /*
7 Copyright (C) 2005-2007, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "r_calendar.h"
23 #include "record-internal.h"
24 #include "protocol.h"
25 #include "protostructs.h"
26 #include "data.h"
27 #include "time.h"
28 #include "error.h"
29 #include "endian.h"
30 #include <ostream>
31 #include <iomanip>
32 #include <time.h>
33 #include <stdexcept>
35 #define __DEBUG_MODE__
36 #include "debug.h"
38 using namespace std;
39 using namespace Barry::Protocol;
41 namespace Barry {
44 ///////////////////////////////////////////////////////////////////////////////
45 // Calendar class
47 // calendar field codes
48 #define CALFC_APPT_TYPE_FLAG 0x01
49 #define CALFC_SUBJECT 0x02
50 #define CALFC_NOTES 0x03
51 #define CALFC_LOCATION 0x04
52 #define CALFC_NOTIFICATION_TIME 0x05
53 #define CALFC_START_TIME 0x06
54 #define CALFC_END_TIME 0x07
55 #define CALFC_RECURRENCE_DATA 0x0c
56 #define CALFC_VERSION_DATA 0x10
57 #define CALFC_NOTIFICATION_DATA 0x1a
58 #define CALFC_FREEBUSY_FLAG 0x1c
59 #define CALFC_TIMEZONE_CODE 0x1e // only seems to show up if recurring
60 #define CALFC_CLASS_FLAG 0x28 // private flag from outlook
61 #define CALFC_ALLDAYEVENT_FLAG 0xff
62 #define CALFC_END 0xffff
64 FieldLink<Calendar> CalendarFieldLinks[] = {
65 { CALFC_SUBJECT, "Subject", 0, 0, &Calendar::Subject, 0, 0 },
66 { CALFC_NOTES, "Notes", 0, 0, &Calendar::Notes, 0, 0 },
67 { CALFC_LOCATION, "Location", 0, 0, &Calendar::Location, 0, 0 },
68 { CALFC_NOTIFICATION_TIME,"Notification Time",0,0, 0, 0, &Calendar::NotificationTime },
69 { CALFC_START_TIME, "Start Time", 0, 0, 0, 0, &Calendar::StartTime },
70 { CALFC_END_TIME, "End Time", 0, 0, 0, 0, &Calendar::EndTime },
71 { CALFC_END, "End of List",0, 0, 0, 0, 0 }
74 Calendar::Calendar()
76 Clear();
79 Calendar::~Calendar()
83 const unsigned char* Calendar::ParseField(const unsigned char *begin,
84 const unsigned char *end)
86 const CommonField *field = (const CommonField *) begin;
88 // advance and check size
89 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
90 if( begin > end ) // if begin==end, we are ok
91 return begin;
93 if( !btohs(field->size) ) // if field has no size, something's up
94 return begin;
96 // cycle through the type table
97 for( FieldLink<Calendar> *b = CalendarFieldLinks;
98 b->type != CALFC_END;
99 b++ )
101 if( b->type == field->type ) {
102 if( b->strMember ) {
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 dout("min1900: " << field->u.min1900);
110 t = min2time(field->u.min1900);
111 return begin;
116 // handle special cases
117 switch( field->type )
119 case CALFC_APPT_TYPE_FLAG:
120 switch( field->u.raw[0] )
122 case 'a': // regular non-recurring appointment
123 Recurring = false;
124 return begin;
126 case '*': // recurring appointment
127 Recurring = true;
128 return begin;
130 default:
131 throw Error("Calendar::ParseField: unknown appointment type");
133 break;
135 case CALFC_ALLDAYEVENT_FLAG:
136 AllDayEvent = field->u.raw[0] == 1;
137 return begin;
139 case CALFC_RECURRENCE_DATA:
140 if( btohs(field->size) >= CALENDAR_RECURRENCE_DATA_FIELD_SIZE ) {
141 // good data
142 ParseRecurrenceData(&field->u.raw[0]);
144 else {
145 // not enough data!
146 throw Error("Calendar::ParseField: not enough data in recurrence data field");
148 return begin;
150 case CALFC_TIMEZONE_CODE:
151 if( btohs(field->size) == 2 ) {
152 // good data
153 TimeZoneCode = btohs(field->u.code);
155 else {
156 throw Error("Calendar::ParseField: not enough data in time zone code field");
158 return begin;
160 case CALFC_FREEBUSY_FLAG:
161 FreeBusyFlag = (FreeBusyFlagType)field->u.raw[0];
162 if( FreeBusyFlag > OutOfOffice ) {
163 throw Error("Calendar::ParseField: FreeBusyFlag out of range" );
165 return begin;
167 case CALFC_CLASS_FLAG:
168 ClassFlag = (ClassFlagType)field->u.raw[0];
169 if( ClassFlag > Private ) {
170 throw Error("Calendar::ParseField: ClassFlag out of range" );
172 return begin;
175 // if still not handled, add to the Unknowns list
176 UnknownField uf;
177 uf.type = field->type;
178 uf.data.assign((const char*)field->u.raw, btohs(field->size));
179 Unknowns.push_back(uf);
181 // return new pointer for next field
182 return begin;
185 // this function assumes the size has already been checked
186 void Calendar::ParseRecurrenceData(const void *data)
188 const CalendarRecurrenceDataField *rec =
189 (const CalendarRecurrenceDataField*) data;
191 Interval = btohs(rec->interval);
192 if( Interval < 1 )
193 Interval = 1; // must always be >= 1
195 if( rec->endTime == 0xffffffff ) {
196 Perpetual = true;
198 else {
199 RecurringEndTime = min2time(rec->endTime);
200 Perpetual = false;
203 switch( rec->type )
205 case CRDF_TYPE_DAY:
206 RecurringType = Day;
207 // no extra data
208 break;
210 case CRDF_TYPE_MONTH_BY_DATE:
211 RecurringType = MonthByDate;
212 DayOfMonth = rec->u.month_by_date.monthDay;
213 break;
215 case CRDF_TYPE_MONTH_BY_DAY:
216 RecurringType = MonthByDay;
217 DayOfWeek = rec->u.month_by_day.weekDay;
218 WeekOfMonth = rec->u.month_by_day.week;
219 break;
221 case CRDF_TYPE_YEAR_BY_DATE:
222 RecurringType = YearByDate;
223 DayOfMonth = rec->u.year_by_date.monthDay;
224 MonthOfYear = rec->u.year_by_date.month;
225 break;
227 case CRDF_TYPE_YEAR_BY_DAY:
228 RecurringType = YearByDay;
229 DayOfWeek = rec->u.year_by_day.weekDay;
230 WeekOfMonth = rec->u.year_by_day.week;
231 MonthOfYear = rec->u.year_by_day.month;
232 break;
234 case CRDF_TYPE_WEEK:
235 RecurringType = Week;
237 // Note: this simple copy is only possible since
238 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
239 // If this ever changes, this code will need to change.
240 WeekDays = rec->u.week.days;
241 break;
243 default:
244 eout("Unknown recurrence data type: " << rec->type);
245 throw Error("Unknown recurrence data type");
249 // this function assumes there is CALENDAR_RECURRENCE_DATA_FIELD_SIZE bytes
250 // available in data
251 void Calendar::BuildRecurrenceData(void *data)
253 if( !Recurring )
254 throw Error("Calendar::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
256 CalendarRecurrenceDataField *rec = (CalendarRecurrenceDataField*) data;
258 // set all to zero
259 memset(data, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE);
261 rec->interval = htobs(Interval);
262 rec->startTime = time2min(StartTime);
263 if( Perpetual )
264 rec->endTime = 0xffffffff;
265 else
266 rec->endTime = time2min(RecurringEndTime);
268 switch( RecurringType )
270 case Day:
271 rec->type = CRDF_TYPE_DAY;
272 // no extra data
273 break;
275 case MonthByDate:
276 rec->type = CRDF_TYPE_MONTH_BY_DATE;
277 rec->u.month_by_date.monthDay = DayOfMonth;
278 break;
280 case MonthByDay:
281 rec->type = CRDF_TYPE_MONTH_BY_DAY;
282 rec->u.month_by_day.weekDay = DayOfWeek;
283 rec->u.month_by_day.week = WeekOfMonth;
284 break;
286 case YearByDate:
287 rec->type = CRDF_TYPE_YEAR_BY_DATE;
288 rec->u.year_by_date.monthDay = DayOfMonth;
289 rec->u.year_by_date.month = MonthOfYear;
290 break;
292 case YearByDay:
293 rec->type = CRDF_TYPE_YEAR_BY_DAY;
294 rec->u.year_by_day.weekDay = DayOfWeek;
295 rec->u.year_by_day.week = WeekOfMonth;
296 rec->u.year_by_day.month = MonthOfYear;
297 break;
299 case Week:
300 rec->type = CRDF_TYPE_WEEK;
302 // Note: this simple copy is only possible since
303 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
304 // If this ever changes, this code will need to change.
305 rec->u.week.days = WeekDays;
306 break;
308 default:
309 eout("Calendar::BuildRecurrenceData: "
310 "Unknown recurrence data type: " << rec->type);
311 throw Error("Calendar::BuildRecurrenceData: Unknown recurrence data type");
315 void Calendar::ParseHeader(const Data &data, size_t &offset)
317 // no header in Calendar records
320 void Calendar::ParseFields(const Data &data, size_t &offset)
322 const unsigned char *finish = ParseCommonFields(*this,
323 data.GetData() + offset, data.GetData() + data.GetSize());
324 offset += finish - (data.GetData() + offset);
327 void Calendar::BuildHeader(Data &data, size_t &offset) const
329 // no header in Calendar records
333 // Build
335 /// Build fields part of record.
337 void Calendar::BuildFields(Data &data, size_t &offset) const
339 data.Zap();
341 // output the type first
342 BuildField(data, offset, CALFC_APPT_TYPE_FLAG, Recurring ? '*' : 'a');
344 // output all day event flag only if set
345 if( AllDayEvent )
346 BuildField(data, offset, CALFC_ALLDAYEVENT_FLAG, (char)1);
348 // cycle through the type table
349 for( const FieldLink<Calendar> *b = CalendarFieldLinks;
350 b->type != CALFC_END;
351 b++ )
353 if( b->strMember ) {
354 const std::string &s = this->*(b->strMember);
355 if( s.size() )
356 BuildField(data, offset, b->type, s);
358 else if( b->timeMember ) {
359 time_t t = this->*(b->timeMember);
360 if( t > 0 )
361 BuildField1900(data, offset, b->type, t);
365 // and finally save unknowns
366 UnknownsType::const_iterator
367 ub = Unknowns.begin(), ue = Unknowns.end();
368 for( ; ub != ue; ub++ ) {
369 BuildField(data, offset, ub->type, ub->data);
372 data.ReleaseBuffer(offset);
375 void Calendar::Clear()
377 RecType = Calendar::GetDefaultRecType();
379 AllDayEvent = false;
380 Subject.clear();
381 Notes.clear();
382 Location.clear();
383 NotificationTime = StartTime = EndTime = 0;
385 FreeBusyFlag = Free;
386 ClassFlag = Public;
388 Recurring = false;
389 RecurringType = Calendar::Week;
390 Interval = 1;
391 RecurringEndTime = 0;
392 Perpetual = false;
393 TimeZoneCode = GetTimeZoneCode(0, 0); // default to GMT
394 DayOfWeek = WeekOfMonth = DayOfMonth = MonthOfYear = 0;
395 WeekDays = 0;
397 Unknowns.clear();
400 void Calendar::Dump(std::ostream &os) const
402 static const char *DayNames[] = { "Sun", "Mon", "Tue", "Wed",
403 "Thu", "Fri", "Sat" };
404 static const char *MonthNames[] = { "Jan", "Feb", "Mar", "Apr",
405 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
406 static const char *ClassTypes[] = { "Public", "Confidential", "Private" };
407 static const char *FreeBusy[] = { "Free", "Tentative", "Busy", "Out of Office" };
409 // FIXME - need a "check all data" function that make sure that all
410 // recurrence data is within range. Then call that before using
411 // the data, such as in Build and in Dump.
413 os << "Calendar entry: 0x" << setbase(16) << RecordId
414 << " (" << (unsigned int)RecType << ")\n";
415 os << " All Day Event: " << (AllDayEvent ? "yes" : "no") << "\n";
416 os << " Class: " << ClassTypes[ClassFlag] << "\n";
417 os << " Free/Busy: " << FreeBusy[FreeBusyFlag] << "\n";
419 // cycle through the type table
420 for( const FieldLink<Calendar> *b = CalendarFieldLinks;
421 b->type != CALFC_END;
422 b++ )
424 if( b->strMember ) {
425 const std::string &s = this->*(b->strMember);
426 if( s.size() )
427 os << " " << b->name << ": " << s << "\n";
429 else if( b->timeMember ) {
430 time_t t = this->*(b->timeMember);
431 if( t > 0 )
432 os << " " << b->name << ": " << ctime(&t);
433 else
434 os << " " << b->name << ": disabled\n";
438 // print recurrence data if available
439 os << " Recurring: " << (Recurring ? "yes" : "no") << "\n";
440 if( Recurring ) {
441 switch( RecurringType )
443 case Day:
444 os << " Every day.\n";
445 break;
447 case MonthByDate:
448 os << " Every month on the "
449 << DayOfMonth
450 << (DayOfMonth == 1 ? "st" : "")
451 << (DayOfMonth == 2 ? "nd" : "")
452 << (DayOfMonth == 3 ? "rd" : "")
453 << (DayOfMonth > 3 ? "th" : "")
454 << "\n";
455 break;
457 case MonthByDay:
458 os << " Every month on the "
459 << DayNames[DayOfWeek]
460 << " of week "
461 << WeekOfMonth
462 << "\n";
463 break;
465 case YearByDate:
466 os << " Every year on "
467 << MonthNames[MonthOfYear-1]
468 << " " << DayOfMonth << "\n";
469 break;
471 case YearByDay:
472 os << " Every year in " << MonthNames[MonthOfYear-1]
473 << " on "
474 << DayNames[DayOfWeek]
475 << " of week " << WeekOfMonth << "\n";
476 break;
478 case Week:
479 os << " Every week on: ";
480 if( WeekDays & CAL_WD_SUN ) os << "Sun ";
481 if( WeekDays & CAL_WD_MON ) os << "Mon ";
482 if( WeekDays & CAL_WD_TUE ) os << "Tue ";
483 if( WeekDays & CAL_WD_WED ) os << "Wed ";
484 if( WeekDays & CAL_WD_THU ) os << "Thu ";
485 if( WeekDays & CAL_WD_FRI ) os << "Fri ";
486 if( WeekDays & CAL_WD_SAT ) os << "Sat ";
487 os << "\n";
488 break;
490 default:
491 os << " Unknown recurrence type\n";
492 break;
495 os << " Interval: " << Interval << "\n";
497 if( Perpetual )
498 os << " Ends: never\n";
499 else
500 os << " Ends: "
501 << ctime(&RecurringEndTime);
504 // print any unknowns
505 os << Unknowns;
509 } // namespace Barry