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 ///////////////////////////////////////////////////////////////////////////////
44 RecurBase::RecurBase()
49 RecurBase::~RecurBase()
53 bool RecurBase::ParseField(uint8_t type
,
54 const unsigned char *data
,
58 // handle special cases
61 case FIELDCODE_RECURRENCE_DATA
:
62 if( size
>= CALENDAR_RECURRENCE_DATA_FIELD_SIZE
) {
64 ParseRecurrenceData(data
);
68 throw Error("RecurBase::ParseField: not enough data in recurrence data field");
77 // this function assumes the size has already been checked
78 void RecurBase::ParseRecurrenceData(const void *data
)
80 const CalendarRecurrenceDataField
*rec
=
81 (const CalendarRecurrenceDataField
*) data
;
83 Interval
= btohs(rec
->interval
);
85 Interval
= 1; // must always be >= 1
87 if( rec
->endTime
== 0xffffffff ) {
91 RecurringEndTime
= min2time(rec
->endTime
);
102 case CRDF_TYPE_MONTH_BY_DATE
:
103 RecurringType
= MonthByDate
;
104 DayOfMonth
= rec
->u
.month_by_date
.monthDay
;
107 case CRDF_TYPE_MONTH_BY_DAY
:
108 RecurringType
= MonthByDay
;
109 DayOfWeek
= rec
->u
.month_by_day
.weekDay
;
110 WeekOfMonth
= rec
->u
.month_by_day
.week
;
113 case CRDF_TYPE_YEAR_BY_DATE
:
114 RecurringType
= YearByDate
;
115 DayOfMonth
= rec
->u
.year_by_date
.monthDay
;
116 MonthOfYear
= rec
->u
.year_by_date
.month
;
119 case CRDF_TYPE_YEAR_BY_DAY
:
120 RecurringType
= YearByDay
;
121 DayOfWeek
= rec
->u
.year_by_day
.weekDay
;
122 WeekOfMonth
= rec
->u
.year_by_day
.week
;
123 MonthOfYear
= rec
->u
.year_by_day
.month
;
127 RecurringType
= Week
;
129 // Note: this simple copy is only possible since
130 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
131 // If this ever changes, this code will need to change.
132 WeekDays
= rec
->u
.week
.days
;
136 eout("Unknown recurrence data type: 0x"
137 << setbase(16) << (unsigned int) rec
->type
);
138 throw Error("Unknown recurrence data type");
144 // this function assumes there is CALENDAR_RECURRENCE_DATA_FIELD_SIZE bytes
146 void RecurBase::BuildRecurrenceData(time_t StartTime
, void *data
) const
149 throw Error("RecurBase::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
151 CalendarRecurrenceDataField
*rec
= (CalendarRecurrenceDataField
*) data
;
154 memset(data
, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE
);
156 rec
->interval
= htobs(Interval
);
157 rec
->startTime
= time2min(StartTime
);
159 rec
->endTime
= 0xffffffff;
161 rec
->endTime
= time2min(RecurringEndTime
);
163 switch( RecurringType
)
166 rec
->type
= CRDF_TYPE_DAY
;
171 rec
->type
= CRDF_TYPE_MONTH_BY_DATE
;
172 rec
->u
.month_by_date
.monthDay
= DayOfMonth
;
176 rec
->type
= CRDF_TYPE_MONTH_BY_DAY
;
177 rec
->u
.month_by_day
.weekDay
= DayOfWeek
;
178 rec
->u
.month_by_day
.week
= WeekOfMonth
;
182 rec
->type
= CRDF_TYPE_YEAR_BY_DATE
;
183 rec
->u
.year_by_date
.monthDay
= DayOfMonth
;
184 rec
->u
.year_by_date
.month
= MonthOfYear
;
188 rec
->type
= CRDF_TYPE_YEAR_BY_DAY
;
189 rec
->u
.year_by_day
.weekDay
= DayOfWeek
;
190 rec
->u
.year_by_day
.week
= WeekOfMonth
;
191 rec
->u
.year_by_day
.month
= MonthOfYear
;
195 rec
->type
= CRDF_TYPE_WEEK
;
197 // Note: this simple copy is only possible since
198 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
199 // If this ever changes, this code will need to change.
200 rec
->u
.week
.days
= WeekDays
;
204 eout("RecurBase::BuildRecurrenceData: "
205 "Unknown recurrence data type: 0x"
206 << setbase(16) << (unsigned int) rec
->type
);
207 throw Error("RecurBase::BuildRecurrenceData: Unknown recurrence data type");
211 uint8_t RecurBase::RecurringFieldType() const
213 return FIELDCODE_RECURRENCE_DATA
;
216 void RecurBase::Clear()
219 RecurringType
= RecurBase::Week
;
221 RecurringEndTime
= 0;
223 DayOfWeek
= WeekOfMonth
= DayOfMonth
= MonthOfYear
= 0;
227 void RecurBase::Dump(std::ostream
&os
) const
229 static const char *DayNames
[] = { "Sun", "Mon", "Tue", "Wed",
230 "Thu", "Fri", "Sat" };
231 static const char *MonthNames
[] = { "Jan", "Feb", "Mar", "Apr",
232 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
234 // FIXME - need a "check all data" function that make sure that all
235 // recurrence data is within range. Then call that before using
236 // the data, such as in Build and in Dump.
238 // print recurrence data if available
239 os
<< " Recurring: " << (Recurring
? "yes" : "no") << "\n";
241 switch( RecurringType
)
244 os
<< " Every day.\n";
248 os
<< " Every month on the "
250 << (DayOfMonth
== 1 ? "st" : "")
251 << (DayOfMonth
== 2 ? "nd" : "")
252 << (DayOfMonth
== 3 ? "rd" : "")
253 << (DayOfMonth
> 3 ? "th" : "")
258 os
<< " Every month on the "
259 << DayNames
[DayOfWeek
]
266 os
<< " Every year on "
267 << MonthNames
[MonthOfYear
-1]
268 << " " << DayOfMonth
<< "\n";
272 os
<< " Every year in " << MonthNames
[MonthOfYear
-1]
274 << DayNames
[DayOfWeek
]
275 << " of week " << WeekOfMonth
<< "\n";
279 os
<< " Every week on: ";
280 if( WeekDays
& CAL_WD_SUN
) os
<< "Sun ";
281 if( WeekDays
& CAL_WD_MON
) os
<< "Mon ";
282 if( WeekDays
& CAL_WD_TUE
) os
<< "Tue ";
283 if( WeekDays
& CAL_WD_WED
) os
<< "Wed ";
284 if( WeekDays
& CAL_WD_THU
) os
<< "Thu ";
285 if( WeekDays
& CAL_WD_FRI
) os
<< "Fri ";
286 if( WeekDays
& CAL_WD_SAT
) os
<< "Sat ";
291 os
<< " Unknown recurrence type\n";
295 os
<< " Interval: " << Interval
<< "\n";
298 os
<< " Ends: never\n";
300 os
<< " Ends: " << ctime(&RecurringEndTime
);