Tree-wide cleanup of trailing whitespace
[barry.git] / src / r_task.cc
blobedaa2d4ee8cc4f5bb51a6538431c081e3a9e434a
1 ///
2 /// \file r_task.cc
3 /// Record parsing class for the task database.
4 ///
6 /*
7 Copyright (C) 2005-2009, Net Direct Inc. (http://www.netdirect.ca/)
8 Copyright (C) 2007, Brian Edginton
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include "r_task.h"
24 #include "r_calendar.h" // for CAL_* defines
25 #include "record-internal.h"
26 #include "protostructs.h"
27 #include "data.h"
28 #include "time.h"
29 #include "iconv.h"
30 #include "debug.h"
31 #include <ostream>
32 #include <iomanip>
33 #include <string.h>
35 using namespace std;
36 using namespace Barry::Protocol;
38 namespace Barry {
40 ///////////////////////////////////////////////////////////////////////////////
41 // Task Class
43 // Task Field Codes
44 #define TSKFC_TASK_TYPE 0x01
45 #define TSKFC_TITLE 0x02
46 #define TSKFC_NOTES 0x03
47 #define TSKFC_START_TIME 0x05
48 #define TSKFC_DUE_TIME 0x06
49 #define TSKFC_DUE_FLAG 0x08
50 #define TSKFC_STATUS 0x09
51 #define TSKFC_PRIORITY 0x0a
52 #define TSKFC_ALARM_TYPE 0x0e
53 #define TSKFC_ALARM_TIME 0x0f
54 #define TSKFC_TIMEZONE_CODE 0x10
55 #define TSKFC_CATEGORIES 0x11
56 #define TSKFC_END 0xffff
58 static FieldLink<Task> TaskFieldLinks[] = {
59 { TSKFC_TITLE, "Summary", 0, 0, &Task::Summary, 0, 0, 0, 0, true },
60 { TSKFC_NOTES, "Notes", 0, 0, &Task::Notes, 0, 0, 0, 0, true },
61 { TSKFC_START_TIME, "Start Time", 0, 0, 0, 0, &Task::StartTime, 0, 0, false },
62 { TSKFC_DUE_TIME, "Due Time", 0, 0, 0, 0, &Task::DueTime, 0, 0, false },
63 { TSKFC_ALARM_TIME, "Alarm Time", 0, 0, 0, 0, &Task::AlarmTime, 0, 0, false },
64 { TSKFC_CATEGORIES, "Categories", 0, 0, &Task::Categories, 0, 0, 0, 0, false },
65 { TSKFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
68 Task::Task()
70 Clear();
73 Task::~Task()
77 const unsigned char* Task::ParseField(const unsigned char *begin,
78 const unsigned char *end,
79 const IConverter *ic)
81 const CommonField *field = (const CommonField *) begin;
83 // advance and check size
84 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
85 if( begin > end ) // if begin==end, we are ok
86 return begin;
88 if( !btohs(field->size) ) // if field has no size, something's up
89 return begin;
91 if( field->type == TSKFC_TASK_TYPE ) {
92 if( ( TaskType = field->u.raw[0] ) != 't' ) {
93 throw Error("Task::ParseField: Task Type is not 't'");
95 return begin;
98 // cycle through the type table
99 for( FieldLink<Task> *b = TaskFieldLinks;
100 b->type != TSKFC_END;
101 b++ )
103 if( b->type == field->type ) {
104 if( b->strMember ) {
105 std::string &s = this->*(b->strMember);
106 s = ParseFieldString(field);
107 if( b->iconvNeeded && ic )
108 s = ic->FromBB(s);
109 return begin; // done!
111 else if( b->timeMember && btohs(field->size) == 4 ) {
112 time_t &t = this->*(b->timeMember);
113 t = min2time(field->u.min1900);
114 return begin;
118 // handle special cases
119 switch( field->type )
121 case TSKFC_PRIORITY:
122 if( field->u.raw[0] > Low ) {
123 throw Error( "Task::ParseField: priority field out of bounds" );
125 else {
126 PriorityFlag = (PriorityFlagType)field->u.raw[0];
128 return begin;
130 case TSKFC_STATUS:
131 if( field->u.raw[0] > Deferred ) {
132 throw Error( "Task::ParseField: priority field out of bounds" );
134 else {
135 StatusFlag = (StatusFlagType)field->u.raw[0];
137 return begin;
139 case TSKFC_TIMEZONE_CODE:
140 if( btohs(field->size) == 4 ) {
141 TimeZoneCode = btohs(field->u.code);
143 else {
144 throw Error("Task::ParseField: not enough data in time zone code field");
146 return begin;
148 case TSKFC_DUE_FLAG:
149 DueDateFlag = field->u.raw[0];
150 return begin;
152 case TSKFC_ALARM_TYPE:
153 if( field->u.raw[0] > Relative ) {
154 throw Error("Task::ParseField: AlarmType out of bounds" );
156 else {
157 AlarmType = (AlarmFlagType)field->u.raw[0];
159 return begin;
162 // base class handles recurring data
163 if( RecurBase::ParseField(field->type, field->u.raw, btohs(field->size), ic) )
164 return begin;
166 // if still not handled, add to the Unknowns list
167 UnknownField uf;
168 uf.type = field->type;
169 uf.data.assign((const char*)field->u.raw, btohs(field->size));
170 Unknowns.push_back(uf);
172 // return new pointer for next field
173 return begin;
176 void Task::ParseHeader(const Data &data, size_t &offset)
178 // no header in Task records
181 void Task::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
183 const unsigned char *finish = ParseCommonFields(*this,
184 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
185 offset += finish - (data.GetData() + offset);
188 void Task::Clear()
190 RecurBase::Clear();
192 Summary.clear();
193 Notes.clear();
194 Categories.clear();
195 StartTime = DueTime = AlarmTime = 0;
197 PriorityFlag = (PriorityFlagType)0;
198 StatusFlag = (StatusFlagType)0;
199 AlarmType = (AlarmFlagType)0;
201 TaskType = 0;
203 DueDateFlag = false;
205 TimeZoneCode = GetTimeZoneCode( 0, 0 );
207 Unknowns.clear();
210 void Task::Dump(std::ostream &os) const
212 static const char *PriorityName[] = { "High", "Normal", "Low" };
213 static const char *StatusName[] = { "Not Started", "In Progress",
214 "Completed", "Waiting", "Deferred" };
215 static const char *DayNames[] = { "Sun", "Mon", "Tue", "Wed",
216 "Thu", "Fri", "Sat" };
217 static const char *MonthNames[] = { "Jan", "Feb", "Mar", "Apr",
218 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
219 static const char *AlarmTypeName[] = { "None", "By Date", "Relative" };
221 os << "Task entry: 0x" << setbase(16) << RecordId
222 << " (" << (unsigned int)RecType << ")\n";
224 // cycle through the type table
225 for( const FieldLink<Task> *b = TaskFieldLinks;
226 b->type != TSKFC_END;
227 b++ )
229 if( b->strMember ) {
230 const std::string &s = this->*(b->strMember);
231 if( s.size() )
232 os << " " << b->name << ": " << s << "\n";
234 else if( b->timeMember ) {
235 time_t t = this->*(b->timeMember);
236 if( t > 0 )
237 os << " " << b->name << ": " << ctime(&t);
241 os << " Priority: " << PriorityName[PriorityFlag] << "\n";
242 os << " Status: " << StatusName[StatusFlag] << "\n";
243 if( AlarmType ) {
244 os << " Alarm Type: " << AlarmTypeName[AlarmType] << "\n";
247 // print recurrence data if available
248 os << " Recurring: " << (Recurring ? "yes" : "no") << "\n";
249 if( Recurring ) {
250 switch( RecurringType )
252 case Day:
253 os << " Every day.\n";
254 break;
256 case MonthByDate:
257 os << " Every month on the "
258 << DayOfMonth
259 << (DayOfMonth == 1 ? "st" : "")
260 << (DayOfMonth == 2 ? "nd" : "")
261 << (DayOfMonth == 3 ? "rd" : "")
262 << (DayOfMonth > 3 ? "th" : "")
263 << "\n";
264 break;
266 case MonthByDay:
267 os << " Every month on the "
268 << DayNames[DayOfWeek]
269 << " of week "
270 << WeekOfMonth
271 << "\n";
272 break;
274 case YearByDate:
275 os << " Every year on "
276 << MonthNames[MonthOfYear-1]
277 << " " << DayOfMonth << "\n";
278 break;
280 case YearByDay:
281 os << " Every year in " << MonthNames[MonthOfYear-1]
282 << " on "
283 << DayNames[DayOfWeek]
284 << " of week " << WeekOfMonth << "\n";
285 break;
287 case Week:
288 os << " Every week on: ";
289 if( WeekDays & CAL_WD_SUN ) os << "Sun ";
290 if( WeekDays & CAL_WD_MON ) os << "Mon ";
291 if( WeekDays & CAL_WD_TUE ) os << "Tue ";
292 if( WeekDays & CAL_WD_WED ) os << "Wed ";
293 if( WeekDays & CAL_WD_THU ) os << "Thu ";
294 if( WeekDays & CAL_WD_FRI ) os << "Fri ";
295 if( WeekDays & CAL_WD_SAT ) os << "Sat ";
296 os << "\n";
297 break;
299 default:
300 os << " Unknown recurrence type\n";
301 break;
304 os << " Interval: " << Interval << "\n";
306 if( Perpetual )
307 os << " Ends: never\n";
308 else
309 os << " Ends: " << ctime(&RecurringEndTime);
312 os << Unknowns;
313 os << "\n\n";
316 } // namespace Barry