lib: show offset and rectype in HexDumpParser
[barry.git] / src / r_task.cc
blobb10fbde9e8aa67c688bb4a3560b0be3440061434
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 BuildField(data, offset, TSKFC_TIMEZONE_CODE, TimeZoneCode);
271 // cycle through the type table
272 for( FieldLink<Task> *b = TaskFieldLinks;
273 b->type != TSKFC_END;
274 b++ )
276 // print only fields with data
277 if( b->strMember ) {
278 const std::string &field = this->*(b->strMember);
279 if( field.size() ) {
280 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
281 BuildField(data, offset, b->type, s);
284 else if( b->timeMember ) {
285 time_t t = this->*(b->timeMember);
286 if( t > 0 )
287 BuildField1900(data, offset, b->type, t);
289 else if( b->postMember && b->postField ) {
290 const std::string &field = (this->*(b->postMember)).*(b->postField);
291 if( field.size() ) {
292 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
293 BuildField(data, offset, b->type, s);
298 // Categories
299 if( Categories.size() ) {
300 string store;
301 Categories.CategoryList2Str(store);
302 BuildField(data, offset, TSKFC_CATEGORIES, ic ? ic->ToBB(store) : store);
305 // and finally save unknowns
306 UnknownsType::const_iterator
307 ub = Unknowns.begin(), ue = Unknowns.end();
308 for( ; ub != ue; ub++ ) {
309 BuildField(data, offset, *ub);
312 data.ReleaseBuffer(offset);
317 void Task::Clear()
319 RecurBase::Clear();
321 Summary.clear();
322 Notes.clear();
323 Categories.clear();
324 StartTime = DueTime = AlarmTime = 0;
326 PriorityFlag = (PriorityFlagType)0;
327 StatusFlag = (StatusFlagType)0;
328 AlarmType = (AlarmFlagType)0;
330 DueDateFlag = false;
332 TimeZoneCode = GetTimeZoneCode( 0, 0 ); // default to GMT
333 TimeZoneValid = false;
335 Unknowns.clear();
338 void Task::Dump(std::ostream &os) const
340 static const char *PriorityName[] = { "High", "Normal", "Low" };
341 static const char *StatusName[] = { "Not Started", "In Progress",
342 "Completed", "Waiting", "Deferred" };
343 static const char *DayNames[] = { "Sun", "Mon", "Tue", "Wed",
344 "Thu", "Fri", "Sat" };
345 static const char *MonthNames[] = { "Jan", "Feb", "Mar", "Apr",
346 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
347 static const char *AlarmTypeName[] = { "None", "By Date", "Relative" };
349 os << "Task entry: 0x" << setbase(16) << RecordId
350 << " (" << (unsigned int)RecType << ")\n";
352 // cycle through the type table
353 for( const FieldLink<Task> *b = TaskFieldLinks;
354 b->type != TSKFC_END;
355 b++ )
357 if( b->strMember ) {
358 const std::string &s = this->*(b->strMember);
359 if( s.size() )
360 os << " " << b->name << ": " << s << "\n";
362 else if( b->timeMember ) {
363 time_t t = this->*(b->timeMember);
364 if( t > 0 )
365 os << " " << b->name << ": " << ctime(&t);
369 os << " Due Date Flag: " << (DueDateFlag ? "true" : "false") << "\n";
370 os << " Priority: " << PriorityName[PriorityFlag] << "\n";
371 os << " Status: " << StatusName[StatusFlag] << "\n";
372 if( AlarmType ) {
373 os << " Alarm Type: " << AlarmTypeName[AlarmType] << "\n";
375 if( TimeZoneValid )
376 os << " Time Zone: " << GetTimeZone(TimeZoneCode)->Name << "\n";
378 // print recurrence data if available
379 os << " Recurring: " << (Recurring ? "yes" : "no") << "\n";
380 if( Recurring ) {
381 switch( RecurringType )
383 case Day:
384 os << " Every day.\n";
385 break;
387 case MonthByDate:
388 os << " Every month on the "
389 << DayOfMonth
390 << (DayOfMonth == 1 ? "st" : "")
391 << (DayOfMonth == 2 ? "nd" : "")
392 << (DayOfMonth == 3 ? "rd" : "")
393 << (DayOfMonth > 3 ? "th" : "")
394 << "\n";
395 break;
397 case MonthByDay:
398 os << " Every month on the "
399 << DayNames[DayOfWeek]
400 << " of week "
401 << WeekOfMonth
402 << "\n";
403 break;
405 case YearByDate:
406 os << " Every year on "
407 << MonthNames[MonthOfYear-1]
408 << " " << DayOfMonth << "\n";
409 break;
411 case YearByDay:
412 os << " Every year in " << MonthNames[MonthOfYear-1]
413 << " on "
414 << DayNames[DayOfWeek]
415 << " of week " << WeekOfMonth << "\n";
416 break;
418 case Week:
419 os << " Every week on: ";
420 if( WeekDays & CAL_WD_SUN ) os << "Sun ";
421 if( WeekDays & CAL_WD_MON ) os << "Mon ";
422 if( WeekDays & CAL_WD_TUE ) os << "Tue ";
423 if( WeekDays & CAL_WD_WED ) os << "Wed ";
424 if( WeekDays & CAL_WD_THU ) os << "Thu ";
425 if( WeekDays & CAL_WD_FRI ) os << "Fri ";
426 if( WeekDays & CAL_WD_SAT ) os << "Sat ";
427 os << "\n";
428 break;
430 default:
431 os << " Unknown recurrence type\n";
432 break;
435 os << " Interval: " << Interval << "\n";
437 if( Perpetual )
438 os << " Ends: never\n";
439 else
440 os << " Ends: " << ctime(&RecurringEndTime);
443 if( Categories.size() ) {
444 string display;
445 Categories.CategoryList2Str(display);
446 os << " Categories: " << display << "\n";
449 os << Unknowns;
450 os << "\n\n";
453 bool Task::operator<(const Task &other) const
455 if( StartTime != other.StartTime )
456 return StartTime < other.StartTime;
457 if( AlarmTime != other.AlarmTime )
458 return AlarmTime < other.AlarmTime;
460 int cmp = Summary.compare(other.Summary);
461 if( cmp == 0 )
462 cmp = Notes.compare(other.Notes);
463 return cmp < 0;
466 } // namespace Barry