lib: fixed field builder bug in Tasks which created a short TIMEZONE_CODE
[barry.git] / src / r_task.cc
blob32c68e463d6f4353f247a0374a6690df215180ab
1 ///
2 /// \file r_task.cc
3 /// Record parsing class for the task database.
4 ///
6 /*
7 Copyright (C) 2005-2010, 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 {
41 ///////////////////////////////////////////////////////////////////////////////
42 // Task Class, static members
45 // Note! These functions currently only pass the same values through.
46 // In actuality, these are technically two different values:
47 // one on the raw protocol side, and the other part of the
48 // guaranteed Barry API. If the Blackberry ever changes the
49 // meanings for these codes, do the translation here.
52 Task::AlarmFlagType Task::AlarmProto2Rec(uint8_t a)
54 return (AlarmFlagType)a;
57 uint8_t Task::AlarmRec2Proto(AlarmFlagType a)
59 return a;
62 Task::PriorityFlagType Task::PriorityProto2Rec(uint8_t p)
64 return (PriorityFlagType)p;
67 uint8_t Task::PriorityRec2Proto(PriorityFlagType p)
69 return p;
72 Task::StatusFlagType Task::StatusProto2Rec(uint8_t s)
74 return (StatusFlagType)s;
77 uint8_t Task::StatusRec2Proto(StatusFlagType s)
79 return s;
83 ///////////////////////////////////////////////////////////////////////////////
84 // Task Class
86 // Task Field Codes
87 #define TSKFC_TASK_TYPE 0x01
88 #define TSKFC_TITLE 0x02
89 #define TSKFC_NOTES 0x03
90 #define TSKFC_DUE_TIME 0x05
91 #define TSKFC_START_TIME 0x06 // This is fuzzy... most devices seem
92 // to anchor this value == to DUE_TIME
93 #define TSKFC_DUE_FLAG 0x08
94 #define TSKFC_STATUS 0x09
95 #define TSKFC_PRIORITY 0x0a
96 #define TSKFC_ALARM_TYPE 0x0e
97 #define TSKFC_ALARM_TIME 0x0f
98 #define TSKFC_TIMEZONE_CODE 0x10
99 #define TSKFC_CATEGORIES 0x11
100 #define TSKFC_END 0xffff
102 static FieldLink<Task> TaskFieldLinks[] = {
103 { TSKFC_TITLE, "Summary", 0, 0, &Task::Summary, 0, 0, 0, 0, true },
104 { TSKFC_NOTES, "Notes", 0, 0, &Task::Notes, 0, 0, 0, 0, true },
105 { TSKFC_START_TIME, "Start Time", 0, 0, 0, 0, &Task::StartTime, 0, 0, false },
106 { TSKFC_DUE_TIME, "Due Time", 0, 0, 0, 0, &Task::DueTime, 0, 0, false },
107 { TSKFC_ALARM_TIME, "Alarm Time", 0, 0, 0, 0, &Task::AlarmTime, 0, 0, false },
108 { TSKFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
111 Task::Task()
113 Clear();
116 Task::~Task()
120 const unsigned char* Task::ParseField(const unsigned char *begin,
121 const unsigned char *end,
122 const IConverter *ic)
124 const CommonField *field = (const CommonField *) begin;
126 // advance and check size
127 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
128 if( begin > end ) // if begin==end, we are ok
129 return begin;
131 if( !btohs(field->size) ) // if field has no size, something's up
132 return begin;
134 if( field->type == TSKFC_TASK_TYPE ) {
135 if( field->u.raw[0] != 't' ) {
136 throw Error("Task::ParseField: Task Type is not 't'");
138 return begin;
141 // cycle through the type table
142 for( FieldLink<Task> *b = TaskFieldLinks;
143 b->type != TSKFC_END;
144 b++ )
146 if( b->type == field->type ) {
147 if( b->strMember ) {
148 std::string &s = this->*(b->strMember);
149 s = ParseFieldString(field);
150 if( b->iconvNeeded && ic )
151 s = ic->FromBB(s);
152 return begin; // done!
154 else if( b->timeMember && btohs(field->size) == 4 ) {
155 time_t &t = this->*(b->timeMember);
156 t = min2time(field->u.min1900);
157 return begin;
161 // handle special cases
162 switch( field->type )
164 case TSKFC_PRIORITY:
165 if( field->u.raw[0] > TR_PRIORITY_RANGE_HIGH ) {
166 throw Error( "Task::ParseField: priority field out of bounds" );
168 else {
169 PriorityFlag = PriorityProto2Rec(field->u.raw[0]);
171 return begin;
173 case TSKFC_STATUS:
174 if( field->u.raw[0] > TR_STATUS_RANGE_HIGH ) {
175 throw Error( "Task::ParseField: priority field out of bounds" );
177 else {
178 StatusFlag = StatusProto2Rec(field->u.raw[0]);
180 return begin;
182 case TSKFC_TIMEZONE_CODE:
183 if( btohs(field->size) == 4 ) {
184 TimeZoneCode = btohs(field->u.code);
185 TimeZoneValid = true;
187 else {
188 throw Error("Task::ParseField: not enough data in time zone code field");
190 return begin;
192 case TSKFC_DUE_FLAG:
193 DueDateFlag = field->u.raw[0];
194 return begin;
196 case TSKFC_ALARM_TYPE:
197 if( field->u.raw[0] > TR_ALARM_RANGE_HIGH ) {
198 throw Error("Task::ParseField: AlarmType out of bounds" );
200 else {
201 AlarmType = AlarmProto2Rec(field->u.raw[0]);
203 return begin;
205 case TSKFC_CATEGORIES:
207 std::string catstring = ParseFieldString(field);
208 if( ic )
209 catstring = ic->FromBB(catstring);
210 Categories.CategoryStr2List(catstring);
212 return begin;
214 // base class handles recurring data
215 if( RecurBase::ParseField(field->type, field->u.raw, btohs(field->size), ic) )
216 return begin;
218 // if still not handled, add to the Unknowns list
219 UnknownField uf;
220 uf.type = field->type;
221 uf.data.assign((const char*)field->u.raw, btohs(field->size));
222 Unknowns.push_back(uf);
224 // return new pointer for next field
225 return begin;
228 void Task::ParseHeader(const Data &data, size_t &offset)
230 // no header in Task records
233 void Task::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
235 const unsigned char *finish = ParseCommonFields(*this,
236 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
237 offset += finish - (data.GetData() + offset);
241 void Task::BuildHeader(Data &data, size_t &offset) const
243 // no header in Task records
248 // Build
250 /// Build fields part of record.
252 void Task::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
254 data.Zap();
256 // tack on the 't' task type field first
257 BuildField(data, offset, TSKFC_TASK_TYPE, 't');
259 BuildField(data, offset, TSKFC_STATUS, StatusRec2Proto(StatusFlag));
260 BuildField(data, offset, TSKFC_PRIORITY, PriorityRec2Proto(PriorityFlag));
261 BuildField(data, offset, TSKFC_ALARM_TYPE, AlarmRec2Proto(AlarmType));
263 if ( DueDateFlag )
264 BuildField(data, offset, TSKFC_DUE_FLAG, (char) 1);
265 else
266 BuildField(data, offset, TSKFC_DUE_FLAG, (char) 0);
268 if( TimeZoneValid ) {
269 // the time zone code field is 4 bytes, but we only use
270 // the first two... pad it with zeros
271 uint32_t code = TimeZoneCode;
272 BuildField(data, offset, TSKFC_TIMEZONE_CODE, code);
275 // cycle through the type table
276 for( FieldLink<Task> *b = TaskFieldLinks;
277 b->type != TSKFC_END;
278 b++ )
280 // print only fields with data
281 if( b->strMember ) {
282 const std::string &field = this->*(b->strMember);
283 if( field.size() ) {
284 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
285 BuildField(data, offset, b->type, s);
288 else if( b->timeMember ) {
289 time_t t = this->*(b->timeMember);
290 if( t > 0 )
291 BuildField1900(data, offset, b->type, t);
293 else if( b->postMember && b->postField ) {
294 const std::string &field = (this->*(b->postMember)).*(b->postField);
295 if( field.size() ) {
296 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
297 BuildField(data, offset, b->type, s);
302 // Categories
303 if( Categories.size() ) {
304 string store;
305 Categories.CategoryList2Str(store);
306 BuildField(data, offset, TSKFC_CATEGORIES, ic ? ic->ToBB(store) : store);
309 // and finally save unknowns
310 UnknownsType::const_iterator
311 ub = Unknowns.begin(), ue = Unknowns.end();
312 for( ; ub != ue; ub++ ) {
313 BuildField(data, offset, *ub);
316 data.ReleaseBuffer(offset);
321 void Task::Clear()
323 RecurBase::Clear();
325 Summary.clear();
326 Notes.clear();
327 Categories.clear();
328 StartTime = DueTime = AlarmTime = 0;
330 PriorityFlag = (PriorityFlagType)0;
331 StatusFlag = (StatusFlagType)0;
332 AlarmType = (AlarmFlagType)0;
334 DueDateFlag = false;
336 TimeZoneCode = GetTimeZoneCode( 0, 0 ); // default to GMT
337 TimeZoneValid = false;
339 Unknowns.clear();
342 void Task::Dump(std::ostream &os) const
344 static const char *PriorityName[] = { "High", "Normal", "Low" };
345 static const char *StatusName[] = { "Not Started", "In Progress",
346 "Completed", "Waiting", "Deferred" };
347 static const char *DayNames[] = { "Sun", "Mon", "Tue", "Wed",
348 "Thu", "Fri", "Sat" };
349 static const char *MonthNames[] = { "Jan", "Feb", "Mar", "Apr",
350 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
351 static const char *AlarmTypeName[] = { "None", "By Date", "Relative" };
353 os << "Task entry: 0x" << setbase(16) << RecordId
354 << " (" << (unsigned int)RecType << ")\n";
356 // cycle through the type table
357 for( const FieldLink<Task> *b = TaskFieldLinks;
358 b->type != TSKFC_END;
359 b++ )
361 if( b->strMember ) {
362 const std::string &s = this->*(b->strMember);
363 if( s.size() )
364 os << " " << b->name << ": " << s << "\n";
366 else if( b->timeMember ) {
367 time_t t = this->*(b->timeMember);
368 if( t > 0 )
369 os << " " << b->name << ": " << ctime(&t);
373 os << " Due Date Flag: " << (DueDateFlag ? "true" : "false") << "\n";
374 os << " Priority: " << PriorityName[PriorityFlag] << "\n";
375 os << " Status: " << StatusName[StatusFlag] << "\n";
376 if( AlarmType ) {
377 os << " Alarm Type: " << AlarmTypeName[AlarmType] << "\n";
379 if( TimeZoneValid )
380 os << " Time Zone: " << GetTimeZone(TimeZoneCode)->Name << "\n";
382 // print recurrence data if available
383 os << " Recurring: " << (Recurring ? "yes" : "no") << "\n";
384 if( Recurring ) {
385 switch( RecurringType )
387 case Day:
388 os << " Every day.\n";
389 break;
391 case MonthByDate:
392 os << " Every month on the "
393 << DayOfMonth
394 << (DayOfMonth == 1 ? "st" : "")
395 << (DayOfMonth == 2 ? "nd" : "")
396 << (DayOfMonth == 3 ? "rd" : "")
397 << (DayOfMonth > 3 ? "th" : "")
398 << "\n";
399 break;
401 case MonthByDay:
402 os << " Every month on the "
403 << DayNames[DayOfWeek]
404 << " of week "
405 << WeekOfMonth
406 << "\n";
407 break;
409 case YearByDate:
410 os << " Every year on "
411 << MonthNames[MonthOfYear-1]
412 << " " << DayOfMonth << "\n";
413 break;
415 case YearByDay:
416 os << " Every year in " << MonthNames[MonthOfYear-1]
417 << " on "
418 << DayNames[DayOfWeek]
419 << " of week " << WeekOfMonth << "\n";
420 break;
422 case Week:
423 os << " Every week on: ";
424 if( WeekDays & CAL_WD_SUN ) os << "Sun ";
425 if( WeekDays & CAL_WD_MON ) os << "Mon ";
426 if( WeekDays & CAL_WD_TUE ) os << "Tue ";
427 if( WeekDays & CAL_WD_WED ) os << "Wed ";
428 if( WeekDays & CAL_WD_THU ) os << "Thu ";
429 if( WeekDays & CAL_WD_FRI ) os << "Fri ";
430 if( WeekDays & CAL_WD_SAT ) os << "Sat ";
431 os << "\n";
432 break;
434 default:
435 os << " Unknown recurrence type\n";
436 break;
439 os << " Interval: " << Interval << "\n";
441 if( Perpetual )
442 os << " Ends: never\n";
443 else
444 os << " Ends: " << ctime(&RecurringEndTime);
447 if( Categories.size() ) {
448 string display;
449 Categories.CategoryList2Str(display);
450 os << " Categories: " << display << "\n";
453 os << Unknowns;
454 os << "\n\n";
457 bool Task::operator<(const Task &other) const
459 if( StartTime != other.StartTime )
460 return StartTime < other.StartTime;
461 if( AlarmTime != other.AlarmTime )
462 return AlarmTime < other.AlarmTime;
464 int cmp = Summary.compare(other.Summary);
465 if( cmp == 0 )
466 cmp = Notes.compare(other.Notes);
467 return cmp < 0;
470 } // namespace Barry