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"
31 #define __DEBUG_MODE__
35 using namespace Barry::Protocol
;
38 #define FIELDCODE_RECURRENCE_DATA 0x0c
43 ///////////////////////////////////////////////////////////////////////////////
44 // RecurBase class, static members
46 unsigned char RecurBase::WeekDayProto2Rec(uint8_t raw_field
)
48 // Note: this simple copy is only possible since
49 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
50 // If this ever changes, this code will need to change.
54 uint8_t RecurBase::WeekDayRec2Proto(unsigned char weekdays
)
56 // Note: this simple copy is only possible since
57 // the CAL_WD_* constants are the same as CRDF_WD_* constants.
58 // If this ever changes, this code will need to change.
63 ///////////////////////////////////////////////////////////////////////////////
66 RecurBase::RecurBase()
71 RecurBase::~RecurBase()
75 bool RecurBase::ParseField(uint8_t type
,
76 const unsigned char *data
,
80 // handle special cases
83 case FIELDCODE_RECURRENCE_DATA
:
84 if( size
>= CALENDAR_RECURRENCE_DATA_FIELD_SIZE
) {
86 ParseRecurrenceData(data
);
90 throw Error("RecurBase::ParseField: not enough data in recurrence data field");
99 // this function assumes the size has already been checked
100 void RecurBase::ParseRecurrenceData(const void *data
)
102 const CalendarRecurrenceDataField
*rec
=
103 (const CalendarRecurrenceDataField
*) data
;
105 Interval
= btohs(rec
->interval
);
107 Interval
= 1; // must always be >= 1
109 if( rec
->endTime
== 0xffffffff ) {
113 RecurringEndTime
.Time
= min2time(rec
->endTime
);
124 case CRDF_TYPE_MONTH_BY_DATE
:
125 RecurringType
= MonthByDate
;
126 DayOfMonth
= rec
->u
.month_by_date
.monthDay
;
129 case CRDF_TYPE_MONTH_BY_DAY
:
130 RecurringType
= MonthByDay
;
131 DayOfWeek
= rec
->u
.month_by_day
.weekDay
;
132 WeekOfMonth
= rec
->u
.month_by_day
.week
;
135 case CRDF_TYPE_YEAR_BY_DATE
:
136 RecurringType
= YearByDate
;
137 DayOfMonth
= rec
->u
.year_by_date
.monthDay
;
138 MonthOfYear
= rec
->u
.year_by_date
.month
;
141 case CRDF_TYPE_YEAR_BY_DAY
:
142 RecurringType
= YearByDay
;
143 DayOfWeek
= rec
->u
.year_by_day
.weekDay
;
144 WeekOfMonth
= rec
->u
.year_by_day
.week
;
145 MonthOfYear
= rec
->u
.year_by_day
.month
;
149 RecurringType
= Week
;
150 WeekDays
= WeekDayProto2Rec(rec
->u
.week
.days
);
154 eout("Unknown recurrence data type: 0x"
155 << setbase(16) << (unsigned int) rec
->type
);
156 throw Error("Unknown recurrence data type");
162 void RecurBase::Validate() const
166 // this function assumes there is CALENDAR_RECURRENCE_DATA_FIELD_SIZE bytes
168 void RecurBase::BuildRecurrenceData(time_t StartTime
, void *data
) const
171 throw Error("RecurBase::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
173 CalendarRecurrenceDataField
*rec
= (CalendarRecurrenceDataField
*) data
;
176 memset(data
, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE
);
178 rec
->interval
= htobs(Interval
);
179 rec
->startTime
= time2min(StartTime
);
181 rec
->endTime
= 0xffffffff;
183 rec
->endTime
= time2min(RecurringEndTime
.Time
);
185 switch( RecurringType
)
188 rec
->type
= CRDF_TYPE_DAY
;
193 rec
->type
= CRDF_TYPE_MONTH_BY_DATE
;
194 rec
->u
.month_by_date
.monthDay
= DayOfMonth
;
198 rec
->type
= CRDF_TYPE_MONTH_BY_DAY
;
199 rec
->u
.month_by_day
.weekDay
= DayOfWeek
;
200 rec
->u
.month_by_day
.week
= WeekOfMonth
;
204 rec
->type
= CRDF_TYPE_YEAR_BY_DATE
;
205 rec
->u
.year_by_date
.monthDay
= DayOfMonth
;
206 rec
->u
.year_by_date
.month
= MonthOfYear
;
210 rec
->type
= CRDF_TYPE_YEAR_BY_DAY
;
211 rec
->u
.year_by_day
.weekDay
= DayOfWeek
;
212 rec
->u
.year_by_day
.week
= WeekOfMonth
;
213 rec
->u
.year_by_day
.month
= MonthOfYear
;
217 rec
->type
= CRDF_TYPE_WEEK
;
218 rec
->u
.week
.days
= WeekDayRec2Proto(WeekDays
);
222 eout("RecurBase::BuildRecurrenceData: "
223 "Unknown recurrence data type: 0x"
224 << setbase(16) << (unsigned int) rec
->type
);
225 throw Error("RecurBase::BuildRecurrenceData: Unknown recurrence data type");
229 uint8_t RecurBase::RecurringFieldType() const
231 return FIELDCODE_RECURRENCE_DATA
;
234 void RecurBase::Clear()
237 RecurringType
= RecurBase::Week
;
239 RecurringEndTime
.clear();
241 DayOfWeek
= WeekOfMonth
= DayOfMonth
= MonthOfYear
= 0;
245 void RecurBase::Dump(std::ostream
&os
) const
247 ios_format_state
state(os
);
249 static const char *DayNames
[] = { "Sun", "Mon", "Tue", "Wed",
250 "Thu", "Fri", "Sat" };
251 static const char *MonthNames
[] = { "Jan", "Feb", "Mar", "Apr",
252 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
254 // FIXME - need a "check all data" function that make sure that all
255 // recurrence data is within range. Then call that before using
256 // the data, such as in Build and in Dump.
258 // print recurrence data if available
259 os
<< " Recurring: " << (Recurring
? "yes" : "no") << "\n";
261 switch( RecurringType
)
264 os
<< " Every day.\n";
268 os
<< " Every month on the "
271 << (DayOfMonth
== 1 ? "st" : "")
272 << (DayOfMonth
== 2 ? "nd" : "")
273 << (DayOfMonth
== 3 ? "rd" : "")
274 << (DayOfMonth
> 3 ? "th" : "")
279 os
<< " Every month on the "
281 << DayNames
[DayOfWeek
]
288 os
<< " Every year on "
290 << MonthNames
[MonthOfYear
-1]
291 << " " << DayOfMonth
<< "\n";
295 os
<< " Every year in " << MonthNames
[MonthOfYear
-1]
298 << DayNames
[DayOfWeek
]
299 << " of week " << WeekOfMonth
<< "\n";
303 os
<< " Every week on: ";
304 if( WeekDays
& CAL_WD_SUN
) os
<< "Sun ";
305 if( WeekDays
& CAL_WD_MON
) os
<< "Mon ";
306 if( WeekDays
& CAL_WD_TUE
) os
<< "Tue ";
307 if( WeekDays
& CAL_WD_WED
) os
<< "Wed ";
308 if( WeekDays
& CAL_WD_THU
) os
<< "Thu ";
309 if( WeekDays
& CAL_WD_FRI
) os
<< "Fri ";
310 if( WeekDays
& CAL_WD_SAT
) os
<< "Sat ";
315 os
<< " Unknown recurrence type\n";
319 os
<< dec
<< " Interval: " << Interval
<< "\n";
322 os
<< " Ends: never\n";
324 os
<< " Ends: " << RecurringEndTime
<< "\n";