Bumped copyright dates for 2013
[barry.git] / src / r_task.cc
blob55bf64dd649effb54ab680f83b7fc612f903181d
1 ///
2 /// \file r_task.cc
3 /// Record parsing class for the task database.
4 ///
6 /*
7 Copyright (C) 2005-2013, 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 "i18n.h"
24 #include "r_task.h"
25 #include "r_calendar.h" // for CAL_* defines
26 #include "r_recur_base-int.h"
27 #include "record-internal.h"
28 #include "protostructs.h"
29 #include "data.h"
30 #include "time.h"
31 #include "iconv.h"
32 #include "debug.h"
33 #include <ostream>
34 #include <iomanip>
35 #include <string.h>
36 #include "ios_state.h"
38 using namespace std;
39 using namespace Barry::Protocol;
41 namespace Barry {
44 ///////////////////////////////////////////////////////////////////////////////
45 // Task Class, static members
48 // Note! These functions currently only pass the same values through.
49 // In actuality, these are technically two different values:
50 // one on the raw protocol side, and the other part of the
51 // guaranteed Barry API. If the Blackberry ever changes the
52 // meanings for these codes, do the translation here.
55 Task::AlarmFlagType Task::AlarmProto2Rec(uint8_t a)
57 return (AlarmFlagType)a;
60 uint8_t Task::AlarmRec2Proto(AlarmFlagType a)
62 return a;
65 Task::PriorityFlagType Task::PriorityProto2Rec(uint8_t p)
67 return (PriorityFlagType)p;
70 uint8_t Task::PriorityRec2Proto(PriorityFlagType p)
72 return p;
75 Task::StatusFlagType Task::StatusProto2Rec(uint8_t s)
77 return (StatusFlagType)s;
80 uint8_t Task::StatusRec2Proto(StatusFlagType s)
82 return s;
86 ///////////////////////////////////////////////////////////////////////////////
87 // Task Class
89 // Task Field Codes
90 #define TSKFC_TASK_TYPE 0x01
91 #define TSKFC_TITLE 0x02
92 #define TSKFC_NOTES 0x03
93 #define TSKFC_DUE_TIME 0x05
94 #define TSKFC_START_TIME 0x06 // This is fuzzy... most devices seem
95 // to anchor this value == to DUE_TIME
96 #define TSKFC_DUE_FLAG 0x08
97 #define TSKFC_STATUS 0x09
98 #define TSKFC_PRIORITY 0x0a
99 #define TSKFC_ALARM_TYPE 0x0e
100 #define TSKFC_ALARM_TIME 0x0f
101 #define TSKFC_TIMEZONE_CODE 0x10
102 #define TSKFC_CATEGORIES 0x11
103 #define TSKFC_ALARM_FLAG 0x12
104 #define TSKFC_END 0xffff
106 static FieldLink<Task> TaskFieldLinks[] = {
107 { TSKFC_TITLE, N_("Summary"), 0, 0, &Task::Summary, 0, 0, 0, 0, true },
108 { TSKFC_NOTES, N_("Notes"), 0, 0, &Task::Notes, 0, 0, 0, 0, true },
109 { TSKFC_START_TIME, N_("Start Time"), 0, 0, 0, 0, &Task::StartTime, 0, 0, false },
110 { TSKFC_DUE_TIME, N_("Due Time"), 0, 0, 0, 0, &Task::DueTime, 0, 0, false },
111 { TSKFC_ALARM_TIME, N_("Alarm Time"), 0, 0, 0, 0, &Task::AlarmTime, 0, 0, false },
112 { TSKFC_END, N_("End of List"), 0, 0, 0, 0, 0, 0, 0, false },
115 Task::Task()
117 Clear();
120 Task::~Task()
124 const unsigned char* Task::ParseField(const unsigned char *begin,
125 const unsigned char *end,
126 const IConverter *ic)
128 const CommonField *field = (const CommonField *) begin;
130 // advance and check size
131 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
132 if( begin > end ) // if begin==end, we are ok
133 return begin;
135 if( !btohs(field->size) ) // if field has no size, something's up
136 return begin;
138 if( field->type == TSKFC_TASK_TYPE ) {
139 if( field->u.raw[0] != 't' ) {
140 throw Error(_("Task::ParseField: Task Type is not 't'"));
142 return begin;
145 // cycle through the type table
146 for( FieldLink<Task> *b = TaskFieldLinks;
147 b->type != TSKFC_END;
148 b++ )
150 if( b->type == field->type ) {
151 if( b->strMember ) {
152 std::string &s = this->*(b->strMember);
153 s = ParseFieldString(field);
154 if( b->iconvNeeded && ic )
155 s = ic->FromBB(s);
156 return begin; // done!
158 else if( b->timeMember && btohs(field->size) == 4 ) {
159 TimeT &t = this->*(b->timeMember);
160 t.Time = min2time(field->u.min1900);
161 return begin;
166 // handle special cases
167 switch( field->type )
169 case TSKFC_PRIORITY:
170 if( field->u.raw[0] > TR_PRIORITY_RANGE_HIGH ) {
171 throw Error(_("Task::ParseField: priority field out of bounds"));
173 else {
174 PriorityFlag = PriorityProto2Rec(field->u.raw[0]);
176 return begin;
178 case TSKFC_STATUS:
179 if( field->u.raw[0] > TR_STATUS_RANGE_HIGH ) {
180 throw Error(_("Task::ParseField: priority field out of bounds"));
182 else {
183 StatusFlag = StatusProto2Rec(field->u.raw[0]);
185 return begin;
187 case TSKFC_TIMEZONE_CODE:
188 if( btohs(field->size) == 4 ) {
189 TimeZoneCode = btohs(field->u.code);
190 TimeZoneValid = true;
192 else {
193 throw Error(_("Task::ParseField: not enough data in time zone code field"));
195 return begin;
197 case TSKFC_DUE_FLAG:
198 // the DueDateFlag is not available on really old devices
199 // such as the 7750, and if the DueTime is available,
200 // we'll just save it. There is no further need for this
201 // value that we know of yet, so just ignore it for now.
202 return begin;
204 case TSKFC_ALARM_FLAG:
205 // the AlarmFlag is not available on really old devices
206 // such as the 7750, and if the AlarmTime is available,
207 // we'll just save it. There is no further need for this
208 // value that we know of yet, so just ignore it for now.
209 return begin;
211 case TSKFC_ALARM_TYPE:
212 if( field->u.raw[0] > TR_ALARM_RANGE_HIGH ) {
213 throw Error(_("Task::ParseField: AlarmType out of bounds"));
215 else {
216 AlarmType = AlarmProto2Rec(field->u.raw[0]);
218 return begin;
220 case TSKFC_CATEGORIES:
222 std::string catstring = ParseFieldString(field);
223 if( ic )
224 catstring = ic->FromBB(catstring);
225 Categories.CategoryStr2List(catstring);
227 return begin;
230 // base class handles recurring data
231 if( RecurBase::ParseField(field->type, field->u.raw, btohs(field->size), ic) )
232 return begin;
234 // if still not handled, add to the Unknowns list
235 UnknownField uf;
236 uf.type = field->type;
237 uf.data.assign((const char*)field->u.raw, btohs(field->size));
238 Unknowns.push_back(uf);
240 // return new pointer for next field
241 return begin;
244 void Task::ParseHeader(const Data &data, size_t &offset)
246 // no header in Task records
249 void Task::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
251 const unsigned char *finish = ParseCommonFields(*this,
252 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
253 offset += finish - (data.GetData() + offset);
257 void Task::Validate() const
259 RecurBase::Validate();
262 void Task::BuildHeader(Data &data, size_t &offset) const
264 // no header in Task records
269 // Build
271 /// Build fields part of record.
273 void Task::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
275 data.Zap();
278 // Note: we do not use the FieldLink table, since the firmware
279 // on many devices apears flakey, so we try to write as close to
280 // the same order as we see coming from the device. Unfortunately,
281 // this doesn't really help, for the Tasks corruption bug, but
282 // we'll keep the flexibility for now.
285 // tack on the 't' task type field first
286 BuildField(data, offset, TSKFC_TASK_TYPE, 't');
288 // Summary / Title
289 if( Summary.size() ) {
290 std::string s = (ic) ? ic->ToBB(Summary) : Summary;
291 BuildField(data, offset, TSKFC_TITLE, s);
294 BuildField(data, offset, TSKFC_STATUS, (uint32_t) StatusRec2Proto(StatusFlag));
295 BuildField(data, offset, TSKFC_PRIORITY, (uint32_t) PriorityRec2Proto(PriorityFlag));
297 if( TimeZoneValid ) {
298 // the time zone code field is 4 bytes, but we only use
299 // the first two... pad it with zeros
300 uint32_t code = TimeZoneCode;
301 BuildField(data, offset, TSKFC_TIMEZONE_CODE, code);
304 // make sure StartTime matches DueTime, by writing it manually...
305 /// not sure why StartTime exists, but oh well. :-)
306 if( DueTime.IsValid() ) {
307 // we use DueTime here, with the START_TIME code,
308 // instead of StartTime, since the function is const
309 BuildField1900(data, offset, TSKFC_START_TIME, DueTime);
311 // then DueTime, with flag first, then time
312 BuildField(data, offset, TSKFC_DUE_FLAG, (uint32_t) 1);
313 BuildField1900(data, offset, TSKFC_DUE_TIME, DueTime);
316 if( AlarmTime.IsValid() ) {
317 BuildField(data, offset, TSKFC_ALARM_FLAG, (uint32_t) 1);
318 BuildField(data, offset, TSKFC_ALARM_TYPE, AlarmRec2Proto(AlarmType));
319 BuildField1900(data, offset, TSKFC_ALARM_TIME, AlarmTime);
322 // Categories
323 if( Categories.size() ) {
324 string store;
325 Categories.CategoryList2Str(store);
326 BuildField(data, offset, TSKFC_CATEGORIES, ic ? ic->ToBB(store) : store);
329 // Notes
330 if( Notes.size() ) {
331 std::string s = (ic) ? ic->ToBB(Notes) : Notes;
332 BuildField(data, offset, TSKFC_NOTES, s);
335 if( Recurring ) {
336 CalendarRecurrenceDataField recur;
337 BuildRecurrenceData(StartTime.Time, &recur);
338 BuildField(data, offset, RecurBase::RecurringFieldType(),
339 &recur, CALENDAR_RECURRENCE_DATA_FIELD_SIZE);
342 // and finally save unknowns
343 UnknownsType::const_iterator
344 ub = Unknowns.begin(), ue = Unknowns.end();
345 for( ; ub != ue; ub++ ) {
346 BuildField(data, offset, *ub);
349 data.ReleaseBuffer(offset);
354 void Task::Clear()
356 // clear the base class first
357 RecurBase::Clear();
359 // our variables...
360 RecType = GetDefaultRecType();
361 RecordId = 0;
363 Summary.clear();
364 Notes.clear();
365 Categories.clear();
366 UID.clear();
368 StartTime.clear();
369 DueTime.clear();
370 AlarmTime.clear();
372 TimeZoneCode = GetStaticTimeZoneCode( 0, 0 ); // default to GMT
373 TimeZoneValid = false;
375 AlarmType = Date;
376 PriorityFlag = Normal;
377 StatusFlag = NotStarted;
379 Unknowns.clear();
382 const FieldHandle<Task>::ListT& Task::GetFieldHandles()
384 static FieldHandle<Task>::ListT fhv;
386 if( fhv.size() )
387 return fhv;
389 #undef CONTAINER_OBJECT_NAME
390 #define CONTAINER_OBJECT_NAME fhv
392 #undef RECORD_CLASS_NAME
393 #define RECORD_CLASS_NAME Task
395 FHP(RecType, _("Record Type Code"));
396 FHP(RecordId, _("Unique Record ID"));
398 FHD(Summary, _("Summary"), TSKFC_TITLE, true);
399 FHD(Notes, _("Notes"), TSKFC_NOTES, true);
400 FHD(Categories, _("Categories"), TSKFC_CATEGORIES, true);
401 FHP(UID, _("UID")); // FIXME - not linked to any device field??
403 FHD(StartTime, _("Start Time"), TSKFC_START_TIME, false);
404 FHD(DueTime, _("Due Time"), TSKFC_DUE_TIME, false);
405 FHD(AlarmTime, _("Alarm Time"), TSKFC_ALARM_TIME, false);
406 FHD(TimeZoneCode, _("Time Zone Code"), TSKFC_TIMEZONE_CODE, false);
407 FHP(TimeZoneValid, _("Time Zone Code Valid"));
409 FHE(aft, AlarmFlagType, AlarmType, _("Alarm Type"));
410 FHE_CONST(aft, Date, _("Date"));
411 FHE_CONST(aft, Relative, _("Relative"));
413 FHE(pft, PriorityFlagType, PriorityFlag, _("Priority"));
414 FHE_CONST(pft, High, _("High"));
415 FHE_CONST(pft, Normal, _("Normal"));
416 FHE_CONST(pft, Low, _("Low"));
418 FHE(sft, StatusFlagType, StatusFlag, _("Status"));
419 FHE_CONST(sft, NotStarted, _("Not Started"));
420 FHE_CONST(sft, InProgress, _("In Progress"));
421 FHE_CONST(sft, Completed, _("Completed"));
422 FHE_CONST(sft, Waiting, _("Waiting"));
423 FHE_CONST(sft, Deferred, _("Deferred"));
425 FHP(Unknowns, _("Unknown Fields"));
427 // and finally, the RecurBase fields
428 RECUR_BASE_FIELD_HANDLES
430 return fhv;
433 std::string Task::GetDescription() const
435 return Summary;
438 void Task::Dump(std::ostream &os) const
440 ios_format_state state(os);
442 static const char *PriorityName[] = {
443 N_("High"),
444 N_("Normal"),
445 N_("Low")
447 static const char *StatusName[] = {
448 N_("Not Started"),
449 N_("In Progress"),
450 N_("Completed"),
451 N_("Waiting"),
452 N_("Deferred")
454 static const char *AlarmTypeName[] = {
455 N_("None"),
456 N_("By Date"),
457 N_("Relative")
460 os << _("Task entry: ") << "0x" << setbase(16) << RecordId
461 << " (" << (unsigned int)RecType << ")\n";
463 // cycle through the type table
464 for( const FieldLink<Task> *b = TaskFieldLinks;
465 b->type != TSKFC_END;
466 b++ )
468 if( b->strMember ) {
469 const std::string &s = this->*(b->strMember);
470 if( s.size() )
471 os << " " << gettext(b->name) << ": " << Cr2LfWrapper(s) << "\n";
473 else if( b->timeMember ) {
474 TimeT t = this->*(b->timeMember);
475 if( t.IsValid() )
476 os << " " << gettext(b->name) << ": " << t << "\n";
480 os << _(" Priority: ") << gettext(PriorityName[PriorityFlag]) << "\n";
481 os << _(" Status: ") << gettext(StatusName[StatusFlag]) << "\n";
482 if( AlarmType ) {
483 os << _(" Alarm Type: ") << gettext(AlarmTypeName[AlarmType]) << "\n";
485 if( TimeZoneValid )
486 os << _(" Time Zone: ") << gettext(GetStaticTimeZone(TimeZoneCode)->Name) << "\n";
488 // print recurrence data if available
489 RecurBase::Dump(os);
491 if( Categories.size() ) {
492 string display;
493 Categories.CategoryList2Str(display);
494 os << _(" Categories: ") << display << "\n";
497 os << Unknowns;
498 os << "\n\n";
501 bool Task::operator<(const Task &other) const
503 if( StartTime != other.StartTime )
504 return StartTime < other.StartTime;
505 if( AlarmTime != other.AlarmTime )
506 return AlarmTime < other.AlarmTime;
508 int cmp = Summary.compare(other.Summary);
509 if( cmp == 0 )
510 cmp = Notes.compare(other.Notes);
511 return cmp < 0;
514 } // namespace Barry