2 /// \file r_recur_base.cc
3 /// Base class for recurring calendar event data.
7 Copyright (C) 2005-2009, 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_recur_base.h"
23 #include "protostructs.h"
29 #define __DEBUG_MODE__
33 using namespace Barry::Protocol
;
36 #define FIELDCODE_RECURRENCE_DATA 0x0c
41 ///////////////////////////////////////////////////////////////////////////////
42 // RecurBase class, static members
44 unsigned char RecurBase::WeekDayProto2Rec(uint8_t raw_field
)
46 // Note: this simple copy is only possible since
47 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
48 // If this ever changes, this code will need to change.
52 uint8_t RecurBase::WeekDayRec2Proto(unsigned char weekdays
)
54 // Note: this simple copy is only possible since
55 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
56 // If this ever changes, this code will need to change.
61 ///////////////////////////////////////////////////////////////////////////////
64 RecurBase::RecurBase()
69 RecurBase::~RecurBase()
73 bool RecurBase::ParseField(uint8_t type
,
74 const unsigned char *data
,
78 // handle special cases
81 case FIELDCODE_RECURRENCE_DATA
:
82 if( size
>= CALENDAR_RECURRENCE_DATA_FIELD_SIZE
) {
84 ParseRecurrenceData(data
);
88 throw Error("RecurBase::ParseField: not enough data in recurrence data field");
97 // this function assumes the size has already been checked
98 void RecurBase::ParseRecurrenceData(const void *data
)
100 const CalendarRecurrenceDataField
*rec
=
101 (const CalendarRecurrenceDataField
*) data
;
103 Interval
= btohs(rec
->interval
);
105 Interval
= 1; // must always be >= 1
107 if( rec
->endTime
== 0xffffffff ) {
111 RecurringEndTime
= min2time(rec
->endTime
);
122 case CRDF_TYPE_MONTH_BY_DATE
:
123 RecurringType
= MonthByDate
;
124 DayOfMonth
= rec
->u
.month_by_date
.monthDay
;
127 case CRDF_TYPE_MONTH_BY_DAY
:
128 RecurringType
= MonthByDay
;
129 DayOfWeek
= rec
->u
.month_by_day
.weekDay
;
130 WeekOfMonth
= rec
->u
.month_by_day
.week
;
133 case CRDF_TYPE_YEAR_BY_DATE
:
134 RecurringType
= YearByDate
;
135 DayOfMonth
= rec
->u
.year_by_date
.monthDay
;
136 MonthOfYear
= rec
->u
.year_by_date
.month
;
139 case CRDF_TYPE_YEAR_BY_DAY
:
140 RecurringType
= YearByDay
;
141 DayOfWeek
= rec
->u
.year_by_day
.weekDay
;
142 WeekOfMonth
= rec
->u
.year_by_day
.week
;
143 MonthOfYear
= rec
->u
.year_by_day
.month
;
147 RecurringType
= Week
;
148 WeekDays
= WeekDayProto2Rec(rec
->u
.week
.days
);
152 eout("Unknown recurrence data type: 0x"
153 << setbase(16) << (unsigned int) rec
->type
);
154 throw Error("Unknown recurrence data type");
160 // this function assumes there is CALENDAR_RECURRENCE_DATA_FIELD_SIZE bytes
162 void RecurBase::BuildRecurrenceData(time_t StartTime
, void *data
) const
165 throw Error("RecurBase::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
167 CalendarRecurrenceDataField
*rec
= (CalendarRecurrenceDataField
*) data
;
170 memset(data
, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE
);
172 rec
->interval
= htobs(Interval
);
173 rec
->startTime
= time2min(StartTime
);
175 rec
->endTime
= 0xffffffff;
177 rec
->endTime
= time2min(RecurringEndTime
);
179 switch( RecurringType
)
182 rec
->type
= CRDF_TYPE_DAY
;
187 rec
->type
= CRDF_TYPE_MONTH_BY_DATE
;
188 rec
->u
.month_by_date
.monthDay
= DayOfMonth
;
192 rec
->type
= CRDF_TYPE_MONTH_BY_DAY
;
193 rec
->u
.month_by_day
.weekDay
= DayOfWeek
;
194 rec
->u
.month_by_day
.week
= WeekOfMonth
;
198 rec
->type
= CRDF_TYPE_YEAR_BY_DATE
;
199 rec
->u
.year_by_date
.monthDay
= DayOfMonth
;
200 rec
->u
.year_by_date
.month
= MonthOfYear
;
204 rec
->type
= CRDF_TYPE_YEAR_BY_DAY
;
205 rec
->u
.year_by_day
.weekDay
= DayOfWeek
;
206 rec
->u
.year_by_day
.week
= WeekOfMonth
;
207 rec
->u
.year_by_day
.month
= MonthOfYear
;
211 rec
->type
= CRDF_TYPE_WEEK
;
212 rec
->u
.week
.days
= WeekDayRec2Proto(WeekDays
);
216 eout("RecurBase::BuildRecurrenceData: "
217 "Unknown recurrence data type: 0x"
218 << setbase(16) << (unsigned int) rec
->type
);
219 throw Error("RecurBase::BuildRecurrenceData: Unknown recurrence data type");
223 uint8_t RecurBase::RecurringFieldType() const
225 return FIELDCODE_RECURRENCE_DATA
;
228 void RecurBase::Clear()
231 RecurringType
= RecurBase::Week
;
233 RecurringEndTime
= 0;
235 DayOfWeek
= WeekOfMonth
= DayOfMonth
= MonthOfYear
= 0;
239 void RecurBase::Dump(std::ostream
&os
) const
241 static const char *DayNames
[] = { "Sun", "Mon", "Tue", "Wed",
242 "Thu", "Fri", "Sat" };
243 static const char *MonthNames
[] = { "Jan", "Feb", "Mar", "Apr",
244 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
246 // FIXME - need a "check all data" function that make sure that all
247 // recurrence data is within range. Then call that before using
248 // the data, such as in Build and in Dump.
250 // print recurrence data if available
251 os
<< " Recurring: " << (Recurring
? "yes" : "no") << "\n";
253 switch( RecurringType
)
256 os
<< " Every day.\n";
260 os
<< " Every month on the "
262 << (DayOfMonth
== 1 ? "st" : "")
263 << (DayOfMonth
== 2 ? "nd" : "")
264 << (DayOfMonth
== 3 ? "rd" : "")
265 << (DayOfMonth
> 3 ? "th" : "")
270 os
<< " Every month on the "
271 << DayNames
[DayOfWeek
]
278 os
<< " Every year on "
279 << MonthNames
[MonthOfYear
-1]
280 << " " << DayOfMonth
<< "\n";
284 os
<< " Every year in " << MonthNames
[MonthOfYear
-1]
286 << DayNames
[DayOfWeek
]
287 << " of week " << WeekOfMonth
<< "\n";
291 os
<< " Every week on: ";
292 if( WeekDays
& CAL_WD_SUN
) os
<< "Sun ";
293 if( WeekDays
& CAL_WD_MON
) os
<< "Mon ";
294 if( WeekDays
& CAL_WD_TUE
) os
<< "Tue ";
295 if( WeekDays
& CAL_WD_WED
) os
<< "Wed ";
296 if( WeekDays
& CAL_WD_THU
) os
<< "Thu ";
297 if( WeekDays
& CAL_WD_FRI
) os
<< "Fri ";
298 if( WeekDays
& CAL_WD_SAT
) os
<< "Sat ";
303 os
<< " Unknown recurrence type\n";
307 os
<< " Interval: " << Interval
<< "\n";
310 os
<< " Ends: never\n";
312 os
<< " Ends: " << ctime(&RecurringEndTime
);