2 /// \file r_recur_base.cc
3 /// Base class for recurring calendar event data.
7 Copyright (C) 2005-2011, 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"
27 #include "ios_state.h"
30 #define __DEBUG_MODE__
34 using namespace Barry::Protocol
;
37 #define FIELDCODE_RECURRENCE_DATA 0x0c
42 ///////////////////////////////////////////////////////////////////////////////
43 // RecurBase class, static members
45 unsigned char RecurBase::WeekDayProto2Rec(uint8_t raw_field
)
47 // Note: this simple copy is only possible since
48 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
49 // If this ever changes, this code will need to change.
53 uint8_t RecurBase::WeekDayRec2Proto(unsigned char weekdays
)
55 // Note: this simple copy is only possible since
56 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
57 // If this ever changes, this code will need to change.
62 ///////////////////////////////////////////////////////////////////////////////
65 RecurBase::RecurBase()
70 RecurBase::~RecurBase()
74 bool RecurBase::ParseField(uint8_t type
,
75 const unsigned char *data
,
79 // handle special cases
82 case FIELDCODE_RECURRENCE_DATA
:
83 if( size
>= CALENDAR_RECURRENCE_DATA_FIELD_SIZE
) {
85 ParseRecurrenceData(data
);
89 throw Error("RecurBase::ParseField: not enough data in recurrence data field");
98 // this function assumes the size has already been checked
99 void RecurBase::ParseRecurrenceData(const void *data
)
101 const CalendarRecurrenceDataField
*rec
=
102 (const CalendarRecurrenceDataField
*) data
;
104 Interval
= btohs(rec
->interval
);
106 Interval
= 1; // must always be >= 1
108 if( rec
->endTime
== 0xffffffff ) {
112 RecurringEndTime
= min2time(rec
->endTime
);
123 case CRDF_TYPE_MONTH_BY_DATE
:
124 RecurringType
= MonthByDate
;
125 DayOfMonth
= rec
->u
.month_by_date
.monthDay
;
128 case CRDF_TYPE_MONTH_BY_DAY
:
129 RecurringType
= MonthByDay
;
130 DayOfWeek
= rec
->u
.month_by_day
.weekDay
;
131 WeekOfMonth
= rec
->u
.month_by_day
.week
;
134 case CRDF_TYPE_YEAR_BY_DATE
:
135 RecurringType
= YearByDate
;
136 DayOfMonth
= rec
->u
.year_by_date
.monthDay
;
137 MonthOfYear
= rec
->u
.year_by_date
.month
;
140 case CRDF_TYPE_YEAR_BY_DAY
:
141 RecurringType
= YearByDay
;
142 DayOfWeek
= rec
->u
.year_by_day
.weekDay
;
143 WeekOfMonth
= rec
->u
.year_by_day
.week
;
144 MonthOfYear
= rec
->u
.year_by_day
.month
;
148 RecurringType
= Week
;
149 WeekDays
= WeekDayProto2Rec(rec
->u
.week
.days
);
153 eout("Unknown recurrence data type: 0x"
154 << setbase(16) << (unsigned int) rec
->type
);
155 throw Error("Unknown recurrence data type");
161 // this function assumes there is CALENDAR_RECURRENCE_DATA_FIELD_SIZE bytes
163 void RecurBase::BuildRecurrenceData(time_t StartTime
, void *data
) const
166 throw Error("RecurBase::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
168 CalendarRecurrenceDataField
*rec
= (CalendarRecurrenceDataField
*) data
;
171 memset(data
, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE
);
173 rec
->interval
= htobs(Interval
);
174 rec
->startTime
= time2min(StartTime
);
176 rec
->endTime
= 0xffffffff;
178 rec
->endTime
= time2min(RecurringEndTime
);
180 switch( RecurringType
)
183 rec
->type
= CRDF_TYPE_DAY
;
188 rec
->type
= CRDF_TYPE_MONTH_BY_DATE
;
189 rec
->u
.month_by_date
.monthDay
= DayOfMonth
;
193 rec
->type
= CRDF_TYPE_MONTH_BY_DAY
;
194 rec
->u
.month_by_day
.weekDay
= DayOfWeek
;
195 rec
->u
.month_by_day
.week
= WeekOfMonth
;
199 rec
->type
= CRDF_TYPE_YEAR_BY_DATE
;
200 rec
->u
.year_by_date
.monthDay
= DayOfMonth
;
201 rec
->u
.year_by_date
.month
= MonthOfYear
;
205 rec
->type
= CRDF_TYPE_YEAR_BY_DAY
;
206 rec
->u
.year_by_day
.weekDay
= DayOfWeek
;
207 rec
->u
.year_by_day
.week
= WeekOfMonth
;
208 rec
->u
.year_by_day
.month
= MonthOfYear
;
212 rec
->type
= CRDF_TYPE_WEEK
;
213 rec
->u
.week
.days
= WeekDayRec2Proto(WeekDays
);
217 eout("RecurBase::BuildRecurrenceData: "
218 "Unknown recurrence data type: 0x"
219 << setbase(16) << (unsigned int) rec
->type
);
220 throw Error("RecurBase::BuildRecurrenceData: Unknown recurrence data type");
224 uint8_t RecurBase::RecurringFieldType() const
226 return FIELDCODE_RECURRENCE_DATA
;
229 void RecurBase::Clear()
232 RecurringType
= RecurBase::Week
;
234 RecurringEndTime
= 0;
236 DayOfWeek
= WeekOfMonth
= DayOfMonth
= MonthOfYear
= 0;
240 void RecurBase::Dump(std::ostream
&os
) const
242 ios_format_state
state(os
);
244 static const char *DayNames
[] = { "Sun", "Mon", "Tue", "Wed",
245 "Thu", "Fri", "Sat" };
246 static const char *MonthNames
[] = { "Jan", "Feb", "Mar", "Apr",
247 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
249 // FIXME - need a "check all data" function that make sure that all
250 // recurrence data is within range. Then call that before using
251 // the data, such as in Build and in Dump.
253 // print recurrence data if available
254 os
<< " Recurring: " << (Recurring
? "yes" : "no") << "\n";
256 switch( RecurringType
)
259 os
<< " Every day.\n";
263 os
<< " Every month on the "
265 << (DayOfMonth
== 1 ? "st" : "")
266 << (DayOfMonth
== 2 ? "nd" : "")
267 << (DayOfMonth
== 3 ? "rd" : "")
268 << (DayOfMonth
> 3 ? "th" : "")
273 os
<< " Every month on the "
274 << DayNames
[DayOfWeek
]
281 os
<< " Every year on "
282 << MonthNames
[MonthOfYear
-1]
283 << " " << DayOfMonth
<< "\n";
287 os
<< " Every year in " << MonthNames
[MonthOfYear
-1]
289 << DayNames
[DayOfWeek
]
290 << " of week " << WeekOfMonth
<< "\n";
294 os
<< " Every week on: ";
295 if( WeekDays
& CAL_WD_SUN
) os
<< "Sun ";
296 if( WeekDays
& CAL_WD_MON
) os
<< "Mon ";
297 if( WeekDays
& CAL_WD_TUE
) os
<< "Tue ";
298 if( WeekDays
& CAL_WD_WED
) os
<< "Wed ";
299 if( WeekDays
& CAL_WD_THU
) os
<< "Thu ";
300 if( WeekDays
& CAL_WD_FRI
) os
<< "Fri ";
301 if( WeekDays
& CAL_WD_SAT
) os
<< "Sat ";
306 os
<< " Unknown recurrence type\n";
310 os
<< " Interval: " << Interval
<< "\n";
313 os
<< " Ends: never\n";
315 os
<< " Ends: " << ctime(&RecurringEndTime
);