2 /// \file r_recur_base.cc
3 /// Base class for recurring calendar event data.
7 Copyright (C) 2005-2012, 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
.Time
= 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 void RecurBase::Validate() const
165 // this function assumes there is CALENDAR_RECURRENCE_DATA_FIELD_SIZE bytes
167 void RecurBase::BuildRecurrenceData(time_t StartTime
, void *data
) const
170 throw Error("RecurBase::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
172 CalendarRecurrenceDataField
*rec
= (CalendarRecurrenceDataField
*) data
;
175 memset(data
, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE
);
177 rec
->interval
= htobs(Interval
);
178 rec
->startTime
= time2min(StartTime
);
180 rec
->endTime
= 0xffffffff;
182 rec
->endTime
= time2min(RecurringEndTime
.Time
);
184 switch( RecurringType
)
187 rec
->type
= CRDF_TYPE_DAY
;
192 rec
->type
= CRDF_TYPE_MONTH_BY_DATE
;
193 rec
->u
.month_by_date
.monthDay
= DayOfMonth
;
197 rec
->type
= CRDF_TYPE_MONTH_BY_DAY
;
198 rec
->u
.month_by_day
.weekDay
= DayOfWeek
;
199 rec
->u
.month_by_day
.week
= WeekOfMonth
;
203 rec
->type
= CRDF_TYPE_YEAR_BY_DATE
;
204 rec
->u
.year_by_date
.monthDay
= DayOfMonth
;
205 rec
->u
.year_by_date
.month
= MonthOfYear
;
209 rec
->type
= CRDF_TYPE_YEAR_BY_DAY
;
210 rec
->u
.year_by_day
.weekDay
= DayOfWeek
;
211 rec
->u
.year_by_day
.week
= WeekOfMonth
;
212 rec
->u
.year_by_day
.month
= MonthOfYear
;
216 rec
->type
= CRDF_TYPE_WEEK
;
217 rec
->u
.week
.days
= WeekDayRec2Proto(WeekDays
);
221 eout("RecurBase::BuildRecurrenceData: "
222 "Unknown recurrence data type: 0x"
223 << setbase(16) << (unsigned int) rec
->type
);
224 throw Error("RecurBase::BuildRecurrenceData: Unknown recurrence data type");
228 uint8_t RecurBase::RecurringFieldType() const
230 return FIELDCODE_RECURRENCE_DATA
;
233 void RecurBase::Clear()
236 RecurringType
= RecurBase::Week
;
238 RecurringEndTime
.clear();
240 DayOfWeek
= WeekOfMonth
= DayOfMonth
= MonthOfYear
= 0;
244 void RecurBase::Dump(std::ostream
&os
) const
246 ios_format_state
state(os
);
248 static const char *DayNames
[] = { "Sun", "Mon", "Tue", "Wed",
249 "Thu", "Fri", "Sat" };
250 static const char *MonthNames
[] = { "Jan", "Feb", "Mar", "Apr",
251 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
253 // FIXME - need a "check all data" function that make sure that all
254 // recurrence data is within range. Then call that before using
255 // the data, such as in Build and in Dump.
257 // print recurrence data if available
258 os
<< " Recurring: " << (Recurring
? "yes" : "no") << "\n";
260 switch( RecurringType
)
263 os
<< " Every day.\n";
267 os
<< " Every month on the "
269 << (DayOfMonth
== 1 ? "st" : "")
270 << (DayOfMonth
== 2 ? "nd" : "")
271 << (DayOfMonth
== 3 ? "rd" : "")
272 << (DayOfMonth
> 3 ? "th" : "")
277 os
<< " Every month on the "
278 << DayNames
[DayOfWeek
]
285 os
<< " Every year on "
286 << MonthNames
[MonthOfYear
-1]
287 << " " << DayOfMonth
<< "\n";
291 os
<< " Every year in " << MonthNames
[MonthOfYear
-1]
293 << DayNames
[DayOfWeek
]
294 << " of week " << WeekOfMonth
<< "\n";
298 os
<< " Every week on: ";
299 if( WeekDays
& CAL_WD_SUN
) os
<< "Sun ";
300 if( WeekDays
& CAL_WD_MON
) os
<< "Mon ";
301 if( WeekDays
& CAL_WD_TUE
) os
<< "Tue ";
302 if( WeekDays
& CAL_WD_WED
) os
<< "Wed ";
303 if( WeekDays
& CAL_WD_THU
) os
<< "Thu ";
304 if( WeekDays
& CAL_WD_FRI
) os
<< "Fri ";
305 if( WeekDays
& CAL_WD_SAT
) os
<< "Sat ";
310 os
<< " Unknown recurrence type\n";
314 os
<< " Interval: " << Interval
<< "\n";
317 os
<< " Ends: never\n";
319 os
<< " Ends: " << RecurringEndTime
<< "\n";