desktop: fixed Evolution config writing, setting enabled only if URL is present
[barry.git] / src / r_recur_base.cc
blob22417b2d0a4683c83c7651de9d66a73e78c27f19
1 ///
2 /// \file r_recur_base.cc
3 /// Base class for recurring calendar event data.
4 ///
6 /*
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"
24 #include "error.h"
25 #include "endian.h"
26 #include "time.h"
27 #include "ios_state.h"
28 #include <string.h>
30 #define __DEBUG_MODE__
31 #include "debug.h"
33 using namespace std;
34 using namespace Barry::Protocol;
37 #define FIELDCODE_RECURRENCE_DATA 0x0c
39 namespace Barry {
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.
50 return raw_field;
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.
58 return weekdays;
62 ///////////////////////////////////////////////////////////////////////////////
63 // RecurBase class
65 RecurBase::RecurBase()
67 Clear();
70 RecurBase::~RecurBase()
74 bool RecurBase::ParseField(uint8_t type,
75 const unsigned char *data,
76 size_t size,
77 const IConverter *ic)
79 // handle special cases
80 switch( type )
82 case FIELDCODE_RECURRENCE_DATA:
83 if( size >= CALENDAR_RECURRENCE_DATA_FIELD_SIZE ) {
84 // good data
85 ParseRecurrenceData(data);
87 else {
88 // not enough data!
89 throw Error("RecurBase::ParseField: not enough data in recurrence data field");
91 return true;
94 // unknown field
95 return false;
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);
105 if( Interval < 1 )
106 Interval = 1; // must always be >= 1
108 if( rec->endTime == 0xffffffff ) {
109 Perpetual = true;
111 else {
112 RecurringEndTime.Time = min2time(rec->endTime);
113 Perpetual = false;
116 switch( rec->type )
118 case CRDF_TYPE_DAY:
119 RecurringType = Day;
120 // no extra data
121 break;
123 case CRDF_TYPE_MONTH_BY_DATE:
124 RecurringType = MonthByDate;
125 DayOfMonth = rec->u.month_by_date.monthDay;
126 break;
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;
132 break;
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;
138 break;
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;
145 break;
147 case CRDF_TYPE_WEEK:
148 RecurringType = Week;
149 WeekDays = WeekDayProto2Rec(rec->u.week.days);
150 break;
152 default:
153 eout("Unknown recurrence data type: 0x"
154 << setbase(16) << (unsigned int) rec->type);
155 throw Error("Unknown recurrence data type");
158 Recurring = true;
161 void RecurBase::Validate() const
165 // this function assumes there is CALENDAR_RECURRENCE_DATA_FIELD_SIZE bytes
166 // available in data
167 void RecurBase::BuildRecurrenceData(time_t StartTime, void *data) const
169 if( !Recurring )
170 throw Error("RecurBase::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
172 CalendarRecurrenceDataField *rec = (CalendarRecurrenceDataField*) data;
174 // set all to zero
175 memset(data, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE);
177 rec->interval = htobs(Interval);
178 rec->startTime = time2min(StartTime);
179 if( Perpetual )
180 rec->endTime = 0xffffffff;
181 else
182 rec->endTime = time2min(RecurringEndTime.Time);
184 switch( RecurringType )
186 case Day:
187 rec->type = CRDF_TYPE_DAY;
188 // no extra data
189 break;
191 case MonthByDate:
192 rec->type = CRDF_TYPE_MONTH_BY_DATE;
193 rec->u.month_by_date.monthDay = DayOfMonth;
194 break;
196 case MonthByDay:
197 rec->type = CRDF_TYPE_MONTH_BY_DAY;
198 rec->u.month_by_day.weekDay = DayOfWeek;
199 rec->u.month_by_day.week = WeekOfMonth;
200 break;
202 case YearByDate:
203 rec->type = CRDF_TYPE_YEAR_BY_DATE;
204 rec->u.year_by_date.monthDay = DayOfMonth;
205 rec->u.year_by_date.month = MonthOfYear;
206 break;
208 case YearByDay:
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;
213 break;
215 case Week:
216 rec->type = CRDF_TYPE_WEEK;
217 rec->u.week.days = WeekDayRec2Proto(WeekDays);
218 break;
220 default:
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()
235 Recurring = false;
236 RecurringType = RecurBase::Week;
237 Interval = 1;
238 RecurringEndTime.clear();
239 Perpetual = false;
240 DayOfWeek = WeekOfMonth = DayOfMonth = MonthOfYear = 0;
241 WeekDays = 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";
259 if( Recurring ) {
260 switch( RecurringType )
262 case Day:
263 os << " Every day.\n";
264 break;
266 case MonthByDate:
267 os << " Every month on the "
268 << DayOfMonth
269 << (DayOfMonth == 1 ? "st" : "")
270 << (DayOfMonth == 2 ? "nd" : "")
271 << (DayOfMonth == 3 ? "rd" : "")
272 << (DayOfMonth > 3 ? "th" : "")
273 << "\n";
274 break;
276 case MonthByDay:
277 os << " Every month on the "
278 << DayNames[DayOfWeek]
279 << " of week "
280 << WeekOfMonth
281 << "\n";
282 break;
284 case YearByDate:
285 os << " Every year on "
286 << MonthNames[MonthOfYear-1]
287 << " " << DayOfMonth << "\n";
288 break;
290 case YearByDay:
291 os << " Every year in " << MonthNames[MonthOfYear-1]
292 << " on "
293 << DayNames[DayOfWeek]
294 << " of week " << WeekOfMonth << "\n";
295 break;
297 case Week:
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 ";
306 os << "\n";
307 break;
309 default:
310 os << " Unknown recurrence type\n";
311 break;
314 os << " Interval: " << Interval << "\n";
316 if( Perpetual )
317 os << " Ends: never\n";
318 else
319 os << " Ends: " << RecurringEndTime << "\n";
324 } // namespace Barry