lib: add prefixes to enums to avoid name clashes on Windows (like ERROR)
[barry.git] / src / r_recur_base.cc
blobc802aa3b4cd31931c803fc899965b65c654cdfb4
1 ///
2 /// \file r_recur_base.cc
3 /// Base class for recurring calendar event data.
4 ///
6 /*
7 Copyright (C) 2005-2010, 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"
24 #include "error.h"
25 #include "endian.h"
26 #include "time.h"
27 #include <string.h>
29 #define __DEBUG_MODE__
30 #include "debug.h"
32 using namespace std;
33 using namespace Barry::Protocol;
36 #define FIELDCODE_RECURRENCE_DATA 0x0c
38 namespace Barry {
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.
49 return raw_field;
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.
57 return weekdays;
61 ///////////////////////////////////////////////////////////////////////////////
62 // RecurBase class
64 RecurBase::RecurBase()
66 Clear();
69 RecurBase::~RecurBase()
73 bool RecurBase::ParseField(uint8_t type,
74 const unsigned char *data,
75 size_t size,
76 const IConverter *ic)
78 // handle special cases
79 switch( type )
81 case FIELDCODE_RECURRENCE_DATA:
82 if( size >= CALENDAR_RECURRENCE_DATA_FIELD_SIZE ) {
83 // good data
84 ParseRecurrenceData(data);
86 else {
87 // not enough data!
88 throw Error("RecurBase::ParseField: not enough data in recurrence data field");
90 return true;
93 // unknown field
94 return false;
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);
104 if( Interval < 1 )
105 Interval = 1; // must always be >= 1
107 if( rec->endTime == 0xffffffff ) {
108 Perpetual = true;
110 else {
111 RecurringEndTime = min2time(rec->endTime);
112 Perpetual = false;
115 switch( rec->type )
117 case CRDF_TYPE_DAY:
118 RecurringType = Day;
119 // no extra data
120 break;
122 case CRDF_TYPE_MONTH_BY_DATE:
123 RecurringType = MonthByDate;
124 DayOfMonth = rec->u.month_by_date.monthDay;
125 break;
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;
131 break;
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;
137 break;
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;
144 break;
146 case CRDF_TYPE_WEEK:
147 RecurringType = Week;
148 WeekDays = WeekDayProto2Rec(rec->u.week.days);
149 break;
151 default:
152 eout("Unknown recurrence data type: 0x"
153 << setbase(16) << (unsigned int) rec->type);
154 throw Error("Unknown recurrence data type");
157 Recurring = true;
160 // this function assumes there is CALENDAR_RECURRENCE_DATA_FIELD_SIZE bytes
161 // available in data
162 void RecurBase::BuildRecurrenceData(time_t StartTime, void *data) const
164 if( !Recurring )
165 throw Error("RecurBase::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
167 CalendarRecurrenceDataField *rec = (CalendarRecurrenceDataField*) data;
169 // set all to zero
170 memset(data, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE);
172 rec->interval = htobs(Interval);
173 rec->startTime = time2min(StartTime);
174 if( Perpetual )
175 rec->endTime = 0xffffffff;
176 else
177 rec->endTime = time2min(RecurringEndTime);
179 switch( RecurringType )
181 case Day:
182 rec->type = CRDF_TYPE_DAY;
183 // no extra data
184 break;
186 case MonthByDate:
187 rec->type = CRDF_TYPE_MONTH_BY_DATE;
188 rec->u.month_by_date.monthDay = DayOfMonth;
189 break;
191 case MonthByDay:
192 rec->type = CRDF_TYPE_MONTH_BY_DAY;
193 rec->u.month_by_day.weekDay = DayOfWeek;
194 rec->u.month_by_day.week = WeekOfMonth;
195 break;
197 case YearByDate:
198 rec->type = CRDF_TYPE_YEAR_BY_DATE;
199 rec->u.year_by_date.monthDay = DayOfMonth;
200 rec->u.year_by_date.month = MonthOfYear;
201 break;
203 case YearByDay:
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;
208 break;
210 case Week:
211 rec->type = CRDF_TYPE_WEEK;
212 rec->u.week.days = WeekDayRec2Proto(WeekDays);
213 break;
215 default:
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()
230 Recurring = false;
231 RecurringType = RecurBase::Week;
232 Interval = 1;
233 RecurringEndTime = 0;
234 Perpetual = false;
235 DayOfWeek = WeekOfMonth = DayOfMonth = MonthOfYear = 0;
236 WeekDays = 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";
252 if( Recurring ) {
253 switch( RecurringType )
255 case Day:
256 os << " Every day.\n";
257 break;
259 case MonthByDate:
260 os << " Every month on the "
261 << DayOfMonth
262 << (DayOfMonth == 1 ? "st" : "")
263 << (DayOfMonth == 2 ? "nd" : "")
264 << (DayOfMonth == 3 ? "rd" : "")
265 << (DayOfMonth > 3 ? "th" : "")
266 << "\n";
267 break;
269 case MonthByDay:
270 os << " Every month on the "
271 << DayNames[DayOfWeek]
272 << " of week "
273 << WeekOfMonth
274 << "\n";
275 break;
277 case YearByDate:
278 os << " Every year on "
279 << MonthNames[MonthOfYear-1]
280 << " " << DayOfMonth << "\n";
281 break;
283 case YearByDay:
284 os << " Every year in " << MonthNames[MonthOfYear-1]
285 << " on "
286 << DayNames[DayOfWeek]
287 << " of week " << WeekOfMonth << "\n";
288 break;
290 case Week:
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 ";
299 os << "\n";
300 break;
302 default:
303 os << " Unknown recurrence type\n";
304 break;
307 os << " Interval: " << Interval << "\n";
309 if( Perpetual )
310 os << " Ends: never\n";
311 else
312 os << " Ends: " << ctime(&RecurringEndTime);
317 } // namespace Barry