# r_recur_base.cc

Go to the documentation of this file.

    00001 ///
    00002 /// \file       r_recur_base.cc
    00003 ///             Base class for recurring calendar event data.
    00004 ///
    00005
    00006 /*
    00007     Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
    00008
    00009     This program is free software; you can redistribute it and/or modify
    00010     it under the terms of the GNU General Public License as published by
    00011     the Free Software Foundation; either version 2 of the License, or
    00012     (at your option) any later version.
    00013
    00014     This program is distributed in the hope that it will be useful,
    00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00017
    00018     See the GNU General Public License in the COPYING file at the
    00019     root directory of this project for more details.
    00020 */
    00021
    00022 #include "r_recur_base.h"
    00023 #include "protostructs.h"
    00024 #include "error.h"
    00025 #include "endian.h"
    00026 #include "time.h"
    00027 #include <string.h>
    00028
    00029 #define __DEBUG_MODE__
    00030 #include "debug.h"
    00031
    00032 using namespace std;
    00033 using namespace Barry::Protocol;
    00034
    00035
    00036 #define FIELDCODE_RECURRENCE_DATA       0x0c
    00037
    00038 namespace Barry {
    00039
    00040
    00041 ///////////////////////////////////////////////////////////////////////////////
    00042 // RecurBase class, static members
    00043
    00044 unsigned char RecurBase::WeekDayProto2Rec(uint8_t raw_field)
    00045 {
    00046         // Note: this simple copy is only possible since
    00047         // the CAL_WD_* constants are the same as CRDF_WD_* constants.
    00048         // If this ever changes, this code will need to change.
    00049         return raw_field;
    00050 }
    00051
    00052 uint8_t RecurBase::WeekDayRec2Proto(unsigned char weekdays)
    00053 {
    00054         // Note: this simple copy is only possible since
    00055         // the CAL_WD_* constants are the same as CRDF_WD_* constants.
    00056         // If this ever changes, this code will need to change.
    00057         return weekdays;
    00058 }
    00059
    00060
    00061 ///////////////////////////////////////////////////////////////////////////////
    00062 // RecurBase class
    00063
    00064 RecurBase::RecurBase()
    00065 {
    00066         Clear();
    00067 }
    00068
    00069 RecurBase::~RecurBase()
    00070 {
    00071 }
    00072
    00073 bool RecurBase::ParseField(uint8_t type,
    00074                            const unsigned char *data,
    00075                            size_t size,
    00076                            const IConverter *ic)
    00077 {
    00078         // handle special cases
    00079         switch( type )
    00080         {
    00081         case FIELDCODE_RECURRENCE_DATA:
    00082                 if( size >= CALENDAR_RECURRENCE_DATA_FIELD_SIZE ) {
    00083                         // good data
    00084                         ParseRecurrenceData(data);
    00085                 }
    00086                 else {
    00087                         // not enough data!
    00088                         throw Error("RecurBase::ParseField: not enough data in recurrence data field");
    00089                 }
    00090                 return true;
    00091         }
    00092
    00093         // unknown field
    00094         return false;
    00095 }
    00096
    00097 // this function assumes the size has already been checked
    00098 void RecurBase::ParseRecurrenceData(const void *data)
    00099 {
    00100         const CalendarRecurrenceDataField *rec =
    00101                 (const CalendarRecurrenceDataField*) data;
    00102
    00103         Interval = btohs(rec->interval);
    00104         if( Interval < 1 )
    00105                 Interval = 1;   // must always be >= 1
    00106
    00107         if( rec->endTime == 0xffffffff ) {
    00108                 Perpetual = true;
    00109         }
    00110         else {
    00111                 RecurringEndTime = min2time(rec->endTime);
    00112                 Perpetual = false;
    00113         }
    00114
    00115         switch( rec->type )
    00116         {
    00117         case CRDF_TYPE_DAY:
    00118                 RecurringType = Day;
    00119                 // no extra data
    00120                 break;
    00121
    00122         case CRDF_TYPE_MONTH_BY_DATE:
    00123                 RecurringType = MonthByDate;
    00124                 DayOfMonth = rec->u.month_by_date.monthDay;
    00125                 break;
    00126
    00127         case CRDF_TYPE_MONTH_BY_DAY:
    00128                 RecurringType = MonthByDay;
    00129                 DayOfWeek = rec->u.month_by_day.weekDay;
    00130                 WeekOfMonth = rec->u.month_by_day.week;
    00131                 break;
    00132
    00133         case CRDF_TYPE_YEAR_BY_DATE:
    00134                 RecurringType = YearByDate;
    00135                 DayOfMonth = rec->u.year_by_date.monthDay;
    00136                 MonthOfYear = rec->u.year_by_date.month;
    00137                 break;
    00138
    00139         case CRDF_TYPE_YEAR_BY_DAY:
    00140                 RecurringType = YearByDay;
    00141                 DayOfWeek = rec->u.year_by_day.weekDay;
    00142                 WeekOfMonth = rec->u.year_by_day.week;
    00143                 MonthOfYear = rec->u.year_by_day.month;
    00144                 break;
    00145
    00146         case CRDF_TYPE_WEEK:
    00147                 RecurringType = Week;
    00148                 WeekDays = WeekDayProto2Rec(rec->u.week.days);
    00149                 break;
    00150
    00151         default:
    00152                 eout("Unknown recurrence data type: 0x"
    00153                         << setbase(16) << (unsigned int) rec->type);
    00154                 throw Error("Unknown recurrence data type");
    00155         }
    00156
    00157         Recurring = true;
    00158 }
    00159
    00160 // this function assumes there is CALENDAR_RECURRENCE_DATA_FIELD_SIZE bytes
    00161 // available in data
    00162 void RecurBase::BuildRecurrenceData(time_t StartTime, void *data) const
    00163 {
    00164         if( !Recurring )
    00165                 throw Error("RecurBase::BuildRecurrenceData: Attempting to build recurrence data on non-recurring record.");
    00166
    00167         CalendarRecurrenceDataField *rec = (CalendarRecurrenceDataField*) data;
    00168
    00169         // set all to zero
    00170         memset(data, 0, CALENDAR_RECURRENCE_DATA_FIELD_SIZE);
    00171
    00172         rec->interval = htobs(Interval);
    00173         rec->startTime = time2min(StartTime);
    00174         if( Perpetual )
    00175                 rec->endTime = 0xffffffff;
    00176         else
    00177                 rec->endTime = time2min(RecurringEndTime);
    00178
    00179         switch( RecurringType )
    00180         {
    00181         case Day:
    00182                 rec->type = CRDF_TYPE_DAY;
    00183                 // no extra data
    00184                 break;
    00185
    00186         case MonthByDate:
    00187                 rec->type = CRDF_TYPE_MONTH_BY_DATE;
    00188                 rec->u.month_by_date.monthDay = DayOfMonth;
    00189                 break;
    00190
    00191         case MonthByDay:
    00192                 rec->type = CRDF_TYPE_MONTH_BY_DAY;
    00193                 rec->u.month_by_day.weekDay = DayOfWeek;
    00194                 rec->u.month_by_day.week = WeekOfMonth;
    00195                 break;
    00196
    00197         case YearByDate:
    00198                 rec->type = CRDF_TYPE_YEAR_BY_DATE;
    00199                 rec->u.year_by_date.monthDay = DayOfMonth;
    00200                 rec->u.year_by_date.month = MonthOfYear;
    00201                 break;
    00202
    00203         case YearByDay:
    00204                 rec->type = CRDF_TYPE_YEAR_BY_DAY;
    00205                 rec->u.year_by_day.weekDay = DayOfWeek;
    00206                 rec->u.year_by_day.week = WeekOfMonth;
    00207                 rec->u.year_by_day.month = MonthOfYear;
    00208                 break;
    00209
    00210         case Week:
    00211                 rec->type = CRDF_TYPE_WEEK;
    00212                 rec->u.week.days = WeekDayRec2Proto(WeekDays);
    00213                 break;
    00214
    00215         default:
    00216                 eout("RecurBase::BuildRecurrenceData: "
    00217                         "Unknown recurrence data type: 0x"
    00218                         << setbase(16) << (unsigned int) rec->type);
    00219                 throw Error("RecurBase::BuildRecurrenceData: Unknown recurrence data type");
    00220         }
    00221 }
    00222
    00223 uint8_t RecurBase::RecurringFieldType() const
    00224 {
    00225         return FIELDCODE_RECURRENCE_DATA;
    00226 }
    00227
    00228 void RecurBase::Clear()
    00229 {
    00230         Recurring = false;
    00231         RecurringType = RecurBase::Week;
    00232         Interval = 1;
    00233         RecurringEndTime = 0;
    00234         Perpetual = false;
    00235         DayOfWeek = WeekOfMonth = DayOfMonth = MonthOfYear = 0;
    00236         WeekDays = 0;
    00237 }
    00238
    00239 void RecurBase::Dump(std::ostream &os) const
    00240 {
    00241         static const char *DayNames[] = { "Sun", "Mon", "Tue", "Wed",
    00242                 "Thu", "Fri", "Sat" };
    00243         static const char *MonthNames[] = { "Jan", "Feb", "Mar", "Apr",
    00244                 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
    00245
    00246 // FIXME - need a "check all data" function that make sure that all
    00247 // recurrence data is within range.  Then call that before using
    00248 // the data, such as in Build and in Dump.
    00249
    00250         // print recurrence data if available
    00251         os << "   Recurring: " << (Recurring ? "yes" : "no") << "\n";
    00252         if( Recurring ) {
    00253                 switch( RecurringType )
    00254                 {
    00255                 case Day:
    00256                         os << "      Every day.\n";
    00257                         break;
    00258
    00259                 case MonthByDate:
    00260                         os << "      Every month on the "
    00261                            << DayOfMonth
    00262                            << (DayOfMonth == 1 ? "st" : "")
    00263                            << (DayOfMonth == 2 ? "nd" : "")
    00264                            << (DayOfMonth == 3 ? "rd" : "")
    00265                            << (DayOfMonth > 3  ? "th" : "")
    00266                            << "\n";
    00267                         break;
    00268
    00269                 case MonthByDay:
    00270                         os << "      Every month on the "
    00271                            << DayNames[DayOfWeek]
    00272                            << " of week "
    00273                            << WeekOfMonth
    00274                            << "\n";
    00275                         break;
    00276
    00277                 case YearByDate:
    00278                         os << "      Every year on "
    00279                            << MonthNames[MonthOfYear-1]
    00280                            << " " << DayOfMonth << "\n";
    00281                         break;
    00282
    00283                 case YearByDay:
    00284                         os << "      Every year in " << MonthNames[MonthOfYear-1]
    00285                            << " on "
    00286                            << DayNames[DayOfWeek]
    00287                            << " of week " << WeekOfMonth << "\n";
    00288                         break;
    00289
    00290                 case Week:
    00291                         os << "      Every week on: ";
    00292                         if( WeekDays & CAL_WD_SUN ) os << "Sun ";
    00293                         if( WeekDays & CAL_WD_MON ) os << "Mon ";
    00294                         if( WeekDays & CAL_WD_TUE ) os << "Tue ";
    00295                         if( WeekDays & CAL_WD_WED ) os << "Wed ";
    00296                         if( WeekDays & CAL_WD_THU ) os << "Thu ";
    00297                         if( WeekDays & CAL_WD_FRI ) os << "Fri ";
    00298                         if( WeekDays & CAL_WD_SAT ) os << "Sat ";
    00299                         os << "\n";
    00300                         break;
    00301
    00302                 default:
    00303                         os << "      Unknown recurrence type\n";
    00304                         break;
    00305                 }
    00306
    00307                 os << "      Interval: " << Interval << "\n";
    00308
    00309                 if( Perpetual )
    00310                         os << "      Ends: never\n";
    00311                 else
    00312                         os << "      Ends: " << ctime(&RecurringEndTime);
    00313         }
    00314 }
    00315
    00316
    00317 } // namespace Barry
    00318