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