Bumped copyright dates to 2012
[barry/progweb.git] / src / r_task.cc
blob6c5c6fa298ebf8ae1e5d6556290dbddab49d28c6
1 ///
2 /// \file r_task.cc
3 /// Record parsing class for the task database.
4 ///
6 /*
7 Copyright (C) 2005-2012, 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>
34 #include "ios_state.h"
36 using namespace std;
37 using namespace Barry::Protocol;
39 namespace Barry {
42 ///////////////////////////////////////////////////////////////////////////////
43 // Task Class, static members
46 // Note! These functions currently only pass the same values through.
47 // In actuality, these are technically two different values:
48 // one on the raw protocol side, and the other part of the
49 // guaranteed Barry API. If the Blackberry ever changes the
50 // meanings for these codes, do the translation here.
53 Task::AlarmFlagType Task::AlarmProto2Rec(uint8_t a)
55 return (AlarmFlagType)a;
58 uint8_t Task::AlarmRec2Proto(AlarmFlagType a)
60 return a;
63 Task::PriorityFlagType Task::PriorityProto2Rec(uint8_t p)
65 return (PriorityFlagType)p;
68 uint8_t Task::PriorityRec2Proto(PriorityFlagType p)
70 return p;
73 Task::StatusFlagType Task::StatusProto2Rec(uint8_t s)
75 return (StatusFlagType)s;
78 uint8_t Task::StatusRec2Proto(StatusFlagType s)
80 return s;
84 ///////////////////////////////////////////////////////////////////////////////
85 // Task Class
87 // Task Field Codes
88 #define TSKFC_TASK_TYPE 0x01
89 #define TSKFC_TITLE 0x02
90 #define TSKFC_NOTES 0x03
91 #define TSKFC_DUE_TIME 0x05
92 #define TSKFC_START_TIME 0x06 // This is fuzzy... most devices seem
93 // to anchor this value == to DUE_TIME
94 #define TSKFC_DUE_FLAG 0x08
95 #define TSKFC_STATUS 0x09
96 #define TSKFC_PRIORITY 0x0a
97 #define TSKFC_ALARM_TYPE 0x0e
98 #define TSKFC_ALARM_TIME 0x0f
99 #define TSKFC_TIMEZONE_CODE 0x10
100 #define TSKFC_CATEGORIES 0x11
101 #define TSKFC_END 0xffff
103 static FieldLink<Task> TaskFieldLinks[] = {
104 { TSKFC_TITLE, "Summary", 0, 0, &Task::Summary, 0, 0, 0, 0, true },
105 { TSKFC_NOTES, "Notes", 0, 0, &Task::Notes, 0, 0, 0, 0, true },
106 { TSKFC_START_TIME, "Start Time", 0, 0, 0, 0, &Task::StartTime, 0, 0, false },
107 { TSKFC_DUE_TIME, "Due Time", 0, 0, 0, 0, &Task::DueTime, 0, 0, false },
108 { TSKFC_ALARM_TIME, "Alarm Time", 0, 0, 0, 0, &Task::AlarmTime, 0, 0, false },
109 { TSKFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
112 Task::Task()
114 Clear();
117 Task::~Task()
121 const unsigned char* Task::ParseField(const unsigned char *begin,
122 const unsigned char *end,
123 const IConverter *ic)
125 const CommonField *field = (const CommonField *) begin;
127 // advance and check size
128 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
129 if( begin > end ) // if begin==end, we are ok
130 return begin;
132 if( !btohs(field->size) ) // if field has no size, something's up
133 return begin;
135 if( field->type == TSKFC_TASK_TYPE ) {
136 if( field->u.raw[0] != 't' ) {
137 throw Error("Task::ParseField: Task Type is not 't'");
139 return begin;
142 // cycle through the type table
143 for( FieldLink<Task> *b = TaskFieldLinks;
144 b->type != TSKFC_END;
145 b++ )
147 if( b->type == field->type ) {
148 if( b->strMember ) {
149 std::string &s = this->*(b->strMember);
150 s = ParseFieldString(field);
151 if( b->iconvNeeded && ic )
152 s = ic->FromBB(s);
153 return begin; // done!
155 else if( b->timeMember && btohs(field->size) == 4 ) {
156 time_t &t = this->*(b->timeMember);
157 t = min2time(field->u.min1900);
158 return begin;
162 // handle special cases
163 switch( field->type )
165 case TSKFC_PRIORITY:
166 if( field->u.raw[0] > TR_PRIORITY_RANGE_HIGH ) {
167 throw Error( "Task::ParseField: priority field out of bounds" );
169 else {
170 PriorityFlag = PriorityProto2Rec(field->u.raw[0]);
172 return begin;
174 case TSKFC_STATUS:
175 if( field->u.raw[0] > TR_STATUS_RANGE_HIGH ) {
176 throw Error( "Task::ParseField: priority field out of bounds" );
178 else {
179 StatusFlag = StatusProto2Rec(field->u.raw[0]);
181 return begin;
183 case TSKFC_TIMEZONE_CODE:
184 if( btohs(field->size) == 4 ) {
185 TimeZoneCode = btohs(field->u.code);
186 TimeZoneValid = true;
188 else {
189 throw Error("Task::ParseField: not enough data in time zone code field");
191 return begin;
193 case TSKFC_DUE_FLAG:
194 DueDateFlag = field->u.raw[0];
195 return begin;
197 case TSKFC_ALARM_TYPE:
198 if( field->u.raw[0] > TR_ALARM_RANGE_HIGH ) {
199 throw Error("Task::ParseField: AlarmType out of bounds" );
201 else {
202 AlarmType = AlarmProto2Rec(field->u.raw[0]);
204 return begin;
206 case TSKFC_CATEGORIES:
208 std::string catstring = ParseFieldString(field);
209 if( ic )
210 catstring = ic->FromBB(catstring);
211 Categories.CategoryStr2List(catstring);
213 return begin;
215 // base class handles recurring data
216 if( RecurBase::ParseField(field->type, field->u.raw, btohs(field->size), ic) )
217 return begin;
219 // if still not handled, add to the Unknowns list
220 UnknownField uf;
221 uf.type = field->type;
222 uf.data.assign((const char*)field->u.raw, btohs(field->size));
223 Unknowns.push_back(uf);
225 // return new pointer for next field
226 return begin;
229 void Task::ParseHeader(const Data &data, size_t &offset)
231 // no header in Task records
234 void Task::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
236 const unsigned char *finish = ParseCommonFields(*this,
237 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
238 offset += finish - (data.GetData() + offset);
242 void Task::BuildHeader(Data &data, size_t &offset) const
244 // no header in Task records
249 // Build
251 /// Build fields part of record.
253 void Task::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
255 data.Zap();
257 // tack on the 't' task type field first
258 BuildField(data, offset, TSKFC_TASK_TYPE, 't');
260 BuildField(data, offset, TSKFC_STATUS, StatusRec2Proto(StatusFlag));
261 BuildField(data, offset, TSKFC_PRIORITY, PriorityRec2Proto(PriorityFlag));
262 BuildField(data, offset, TSKFC_ALARM_TYPE, AlarmRec2Proto(AlarmType));
264 if ( DueDateFlag )
265 BuildField(data, offset, TSKFC_DUE_FLAG, (char) 1);
266 else
267 BuildField(data, offset, TSKFC_DUE_FLAG, (char) 0);
269 if( TimeZoneValid ) {
270 // the time zone code field is 4 bytes, but we only use
271 // the first two... pad it with zeros
272 uint32_t code = TimeZoneCode;
273 BuildField(data, offset, TSKFC_TIMEZONE_CODE, code);
276 // cycle through the type table
277 for( FieldLink<Task> *b = TaskFieldLinks;
278 b->type != TSKFC_END;
279 b++ )
281 // print only fields with data
282 if( b->strMember ) {
283 const std::string &field = this->*(b->strMember);
284 if( field.size() ) {
285 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
286 BuildField(data, offset, b->type, s);
289 else if( b->timeMember ) {
290 time_t t = this->*(b->timeMember);
291 if( t > 0 )
292 BuildField1900(data, offset, b->type, t);
294 else if( b->postMember && b->postField ) {
295 const std::string &field = (this->*(b->postMember)).*(b->postField);
296 if( field.size() ) {
297 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
298 BuildField(data, offset, b->type, s);
303 // Categories
304 if( Categories.size() ) {
305 string store;
306 Categories.CategoryList2Str(store);
307 BuildField(data, offset, TSKFC_CATEGORIES, ic ? ic->ToBB(store) : store);
310 // and finally save unknowns
311 UnknownsType::const_iterator
312 ub = Unknowns.begin(), ue = Unknowns.end();
313 for( ; ub != ue; ub++ ) {
314 BuildField(data, offset, *ub);
317 data.ReleaseBuffer(offset);
322 void Task::Clear()
324 // clear the base class first
325 RecurBase::Clear();
327 // our variables...
328 RecType = GetDefaultRecType();
329 RecordId = 0;
331 Summary.clear();
332 Notes.clear();
333 Categories.clear();
334 UID.clear();
336 StartTime = DueTime = AlarmTime = 0;
337 TimeZoneCode = GetTimeZoneCode( 0, 0 ); // default to GMT
338 TimeZoneValid = false;
340 AlarmType = (AlarmFlagType)0;
341 PriorityFlag = (PriorityFlagType)0;
342 StatusFlag = (StatusFlagType)0;
344 DueDateFlag = false;
346 Unknowns.clear();
349 std::string Task::GetDescription() const
351 return Summary;
354 void Task::Dump(std::ostream &os) const
356 ios_format_state state(os);
358 static const char *PriorityName[] = { "High", "Normal", "Low" };
359 static const char *StatusName[] = { "Not Started", "In Progress",
360 "Completed", "Waiting", "Deferred" };
361 static const char *DayNames[] = { "Sun", "Mon", "Tue", "Wed",
362 "Thu", "Fri", "Sat" };
363 static const char *MonthNames[] = { "Jan", "Feb", "Mar", "Apr",
364 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
365 static const char *AlarmTypeName[] = { "None", "By Date", "Relative" };
367 os << "Task entry: 0x" << setbase(16) << RecordId
368 << " (" << (unsigned int)RecType << ")\n";
370 // cycle through the type table
371 for( const FieldLink<Task> *b = TaskFieldLinks;
372 b->type != TSKFC_END;
373 b++ )
375 if( b->strMember ) {
376 const std::string &s = this->*(b->strMember);
377 if( s.size() )
378 os << " " << b->name << ": " << s << "\n";
380 else if( b->timeMember ) {
381 time_t t = this->*(b->timeMember);
382 if( t > 0 )
383 os << " " << b->name << ": " << ctime(&t);
387 os << " Due Date Flag: " << (DueDateFlag ? "true" : "false") << "\n";
388 os << " Priority: " << PriorityName[PriorityFlag] << "\n";
389 os << " Status: " << StatusName[StatusFlag] << "\n";
390 if( AlarmType ) {
391 os << " Alarm Type: " << AlarmTypeName[AlarmType] << "\n";
393 if( TimeZoneValid )
394 os << " Time Zone: " << GetTimeZone(TimeZoneCode)->Name << "\n";
396 // print recurrence data if available
397 os << " Recurring: " << (Recurring ? "yes" : "no") << "\n";
398 if( Recurring ) {
399 switch( RecurringType )
401 case Day:
402 os << " Every day.\n";
403 break;
405 case MonthByDate:
406 os << " Every month on the "
407 << DayOfMonth
408 << (DayOfMonth == 1 ? "st" : "")
409 << (DayOfMonth == 2 ? "nd" : "")
410 << (DayOfMonth == 3 ? "rd" : "")
411 << (DayOfMonth > 3 ? "th" : "")
412 << "\n";
413 break;
415 case MonthByDay:
416 os << " Every month on the "
417 << DayNames[DayOfWeek]
418 << " of week "
419 << WeekOfMonth
420 << "\n";
421 break;
423 case YearByDate:
424 os << " Every year on "
425 << MonthNames[MonthOfYear-1]
426 << " " << DayOfMonth << "\n";
427 break;
429 case YearByDay:
430 os << " Every year in " << MonthNames[MonthOfYear-1]
431 << " on "
432 << DayNames[DayOfWeek]
433 << " of week " << WeekOfMonth << "\n";
434 break;
436 case Week:
437 os << " Every week on: ";
438 if( WeekDays & CAL_WD_SUN ) os << "Sun ";
439 if( WeekDays & CAL_WD_MON ) os << "Mon ";
440 if( WeekDays & CAL_WD_TUE ) os << "Tue ";
441 if( WeekDays & CAL_WD_WED ) os << "Wed ";
442 if( WeekDays & CAL_WD_THU ) os << "Thu ";
443 if( WeekDays & CAL_WD_FRI ) os << "Fri ";
444 if( WeekDays & CAL_WD_SAT ) os << "Sat ";
445 os << "\n";
446 break;
448 default:
449 os << " Unknown recurrence type\n";
450 break;
453 os << " Interval: " << Interval << "\n";
455 if( Perpetual )
456 os << " Ends: never\n";
457 else
458 os << " Ends: " << ctime(&RecurringEndTime);
461 if( Categories.size() ) {
462 string display;
463 Categories.CategoryList2Str(display);
464 os << " Categories: " << display << "\n";
467 os << Unknowns;
468 os << "\n\n";
471 bool Task::operator<(const Task &other) const
473 if( StartTime != other.StartTime )
474 return StartTime < other.StartTime;
475 if( AlarmTime != other.AlarmTime )
476 return AlarmTime < other.AlarmTime;
478 int cmp = Summary.compare(other.Summary);
479 if( cmp == 0 )
480 cmp = Notes.compare(other.Notes);
481 return cmp < 0;
484 } // namespace Barry