maint: added proper sample of Barry yum repo file for Fedora
[barry/progweb.git] / src / r_task.cc
blobeee121289c8b861ffce2d6ec020ebed237b28833
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 "r_recur_base-int.h"
26 #include "record-internal.h"
27 #include "protostructs.h"
28 #include "data.h"
29 #include "time.h"
30 #include "iconv.h"
31 #include "debug.h"
32 #include <ostream>
33 #include <iomanip>
34 #include <string.h>
35 #include "ios_state.h"
37 using namespace std;
38 using namespace Barry::Protocol;
40 namespace Barry {
43 ///////////////////////////////////////////////////////////////////////////////
44 // Task Class, static members
47 // Note! These functions currently only pass the same values through.
48 // In actuality, these are technically two different values:
49 // one on the raw protocol side, and the other part of the
50 // guaranteed Barry API. If the Blackberry ever changes the
51 // meanings for these codes, do the translation here.
54 Task::AlarmFlagType Task::AlarmProto2Rec(uint8_t a)
56 return (AlarmFlagType)a;
59 uint8_t Task::AlarmRec2Proto(AlarmFlagType a)
61 return a;
64 Task::PriorityFlagType Task::PriorityProto2Rec(uint8_t p)
66 return (PriorityFlagType)p;
69 uint8_t Task::PriorityRec2Proto(PriorityFlagType p)
71 return p;
74 Task::StatusFlagType Task::StatusProto2Rec(uint8_t s)
76 return (StatusFlagType)s;
79 uint8_t Task::StatusRec2Proto(StatusFlagType s)
81 return s;
85 ///////////////////////////////////////////////////////////////////////////////
86 // Task Class
88 // Task Field Codes
89 #define TSKFC_TASK_TYPE 0x01
90 #define TSKFC_TITLE 0x02
91 #define TSKFC_NOTES 0x03
92 #define TSKFC_DUE_TIME 0x05
93 #define TSKFC_START_TIME 0x06 // This is fuzzy... most devices seem
94 // to anchor this value == to DUE_TIME
95 #define TSKFC_DUE_FLAG 0x08
96 #define TSKFC_STATUS 0x09
97 #define TSKFC_PRIORITY 0x0a
98 #define TSKFC_ALARM_TYPE 0x0e
99 #define TSKFC_ALARM_TIME 0x0f
100 #define TSKFC_TIMEZONE_CODE 0x10
101 #define TSKFC_CATEGORIES 0x11
102 #define TSKFC_ALARM_FLAG 0x12
103 #define TSKFC_END 0xffff
105 static FieldLink<Task> TaskFieldLinks[] = {
106 { TSKFC_TITLE, "Summary", 0, 0, &Task::Summary, 0, 0, 0, 0, true },
107 { TSKFC_NOTES, "Notes", 0, 0, &Task::Notes, 0, 0, 0, 0, true },
108 { TSKFC_START_TIME, "Start Time", 0, 0, 0, 0, &Task::StartTime, 0, 0, false },
109 { TSKFC_DUE_TIME, "Due Time", 0, 0, 0, 0, &Task::DueTime, 0, 0, false },
110 { TSKFC_ALARM_TIME, "Alarm Time", 0, 0, 0, 0, &Task::AlarmTime, 0, 0, false },
111 { TSKFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
114 Task::Task()
116 Clear();
119 Task::~Task()
123 const unsigned char* Task::ParseField(const unsigned char *begin,
124 const unsigned char *end,
125 const IConverter *ic)
127 const CommonField *field = (const CommonField *) begin;
129 // advance and check size
130 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
131 if( begin > end ) // if begin==end, we are ok
132 return begin;
134 if( !btohs(field->size) ) // if field has no size, something's up
135 return begin;
137 if( field->type == TSKFC_TASK_TYPE ) {
138 if( field->u.raw[0] != 't' ) {
139 throw Error("Task::ParseField: Task Type is not 't'");
141 return begin;
144 // cycle through the type table
145 for( FieldLink<Task> *b = TaskFieldLinks;
146 b->type != TSKFC_END;
147 b++ )
149 if( b->type == field->type ) {
150 if( b->strMember ) {
151 std::string &s = this->*(b->strMember);
152 s = ParseFieldString(field);
153 if( b->iconvNeeded && ic )
154 s = ic->FromBB(s);
155 return begin; // done!
157 else if( b->timeMember && btohs(field->size) == 4 ) {
158 TimeT &t = this->*(b->timeMember);
159 t.Time = min2time(field->u.min1900);
160 return begin;
165 // handle special cases
166 switch( field->type )
168 case TSKFC_PRIORITY:
169 if( field->u.raw[0] > TR_PRIORITY_RANGE_HIGH ) {
170 throw Error( "Task::ParseField: priority field out of bounds" );
172 else {
173 PriorityFlag = PriorityProto2Rec(field->u.raw[0]);
175 return begin;
177 case TSKFC_STATUS:
178 if( field->u.raw[0] > TR_STATUS_RANGE_HIGH ) {
179 throw Error( "Task::ParseField: priority field out of bounds" );
181 else {
182 StatusFlag = StatusProto2Rec(field->u.raw[0]);
184 return begin;
186 case TSKFC_TIMEZONE_CODE:
187 if( btohs(field->size) == 4 ) {
188 TimeZoneCode = btohs(field->u.code);
189 TimeZoneValid = true;
191 else {
192 throw Error("Task::ParseField: not enough data in time zone code field");
194 return begin;
196 case TSKFC_DUE_FLAG:
197 // the DueDateFlag is not available on really old devices
198 // such as the 7750, and if the DueTime is available,
199 // we'll just save it. There is no further need for this
200 // value that we know of yet, so just ignore it for now.
201 return begin;
203 case TSKFC_ALARM_FLAG:
204 // the AlarmFlag is not available on really old devices
205 // such as the 7750, and if the AlarmTime is available,
206 // we'll just save it. There is no further need for this
207 // value that we know of yet, so just ignore it for now.
208 return begin;
210 case TSKFC_ALARM_TYPE:
211 if( field->u.raw[0] > TR_ALARM_RANGE_HIGH ) {
212 throw Error("Task::ParseField: AlarmType out of bounds" );
214 else {
215 AlarmType = AlarmProto2Rec(field->u.raw[0]);
217 return begin;
219 case TSKFC_CATEGORIES:
221 std::string catstring = ParseFieldString(field);
222 if( ic )
223 catstring = ic->FromBB(catstring);
224 Categories.CategoryStr2List(catstring);
226 return begin;
229 // base class handles recurring data
230 if( RecurBase::ParseField(field->type, field->u.raw, btohs(field->size), ic) )
231 return begin;
233 // if still not handled, add to the Unknowns list
234 UnknownField uf;
235 uf.type = field->type;
236 uf.data.assign((const char*)field->u.raw, btohs(field->size));
237 Unknowns.push_back(uf);
239 // return new pointer for next field
240 return begin;
243 void Task::ParseHeader(const Data &data, size_t &offset)
245 // no header in Task records
248 void Task::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
250 const unsigned char *finish = ParseCommonFields(*this,
251 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
252 offset += finish - (data.GetData() + offset);
256 void Task::Validate() const
258 RecurBase::Validate();
261 void Task::BuildHeader(Data &data, size_t &offset) const
263 // no header in Task records
268 // Build
270 /// Build fields part of record.
272 void Task::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
274 data.Zap();
277 // Note: we do not use the FieldLink table, since the firmware
278 // on many devices apears flakey, so we try to write as close to
279 // the same order as we see coming from the device. Unfortunately,
280 // this doesn't really help, for the Tasks corruption bug, but
281 // we'll keep the flexibility for now.
284 // tack on the 't' task type field first
285 BuildField(data, offset, TSKFC_TASK_TYPE, 't');
287 // Summary / Title
288 if( Summary.size() ) {
289 std::string s = (ic) ? ic->ToBB(Summary) : Summary;
290 BuildField(data, offset, TSKFC_TITLE, s);
293 BuildField(data, offset, TSKFC_STATUS, (uint32_t) StatusRec2Proto(StatusFlag));
294 BuildField(data, offset, TSKFC_PRIORITY, (uint32_t) PriorityRec2Proto(PriorityFlag));
296 if( TimeZoneValid ) {
297 // the time zone code field is 4 bytes, but we only use
298 // the first two... pad it with zeros
299 uint32_t code = TimeZoneCode;
300 BuildField(data, offset, TSKFC_TIMEZONE_CODE, code);
303 // make sure StartTime matches DueTime, by writing it manually...
304 /// not sure why StartTime exists, but oh well. :-)
305 if( DueTime.IsValid() ) {
306 // we use DueTime here, with the START_TIME code,
307 // instead of StartTime, since the function is const
308 BuildField1900(data, offset, TSKFC_START_TIME, DueTime);
310 // then DueTime, with flag first, then time
311 BuildField(data, offset, TSKFC_DUE_FLAG, (uint32_t) 1);
312 BuildField1900(data, offset, TSKFC_DUE_TIME, DueTime);
315 if( AlarmTime.IsValid() ) {
316 BuildField(data, offset, TSKFC_ALARM_FLAG, (uint32_t) 1);
317 BuildField(data, offset, TSKFC_ALARM_TYPE, AlarmRec2Proto(AlarmType));
318 BuildField1900(data, offset, TSKFC_ALARM_TIME, AlarmTime);
321 // Categories
322 if( Categories.size() ) {
323 string store;
324 Categories.CategoryList2Str(store);
325 BuildField(data, offset, TSKFC_CATEGORIES, ic ? ic->ToBB(store) : store);
328 // Notes
329 if( Notes.size() ) {
330 std::string s = (ic) ? ic->ToBB(Notes) : Notes;
331 BuildField(data, offset, TSKFC_NOTES, s);
334 if( Recurring ) {
335 CalendarRecurrenceDataField recur;
336 BuildRecurrenceData(StartTime.Time, &recur);
337 BuildField(data, offset, RecurBase::RecurringFieldType(),
338 &recur, CALENDAR_RECURRENCE_DATA_FIELD_SIZE);
341 // and finally save unknowns
342 UnknownsType::const_iterator
343 ub = Unknowns.begin(), ue = Unknowns.end();
344 for( ; ub != ue; ub++ ) {
345 BuildField(data, offset, *ub);
348 data.ReleaseBuffer(offset);
353 void Task::Clear()
355 // clear the base class first
356 RecurBase::Clear();
358 // our variables...
359 RecType = GetDefaultRecType();
360 RecordId = 0;
362 Summary.clear();
363 Notes.clear();
364 Categories.clear();
365 UID.clear();
367 StartTime.clear();
368 DueTime.clear();
369 AlarmTime.clear();
371 TimeZoneCode = GetStaticTimeZoneCode( 0, 0 ); // default to GMT
372 TimeZoneValid = false;
374 AlarmType = Date;
375 PriorityFlag = Normal;
376 StatusFlag = NotStarted;
378 Unknowns.clear();
381 const FieldHandle<Task>::ListT& Task::GetFieldHandles()
383 static FieldHandle<Task>::ListT fhv;
385 if( fhv.size() )
386 return fhv;
388 #undef CONTAINER_OBJECT_NAME
389 #define CONTAINER_OBJECT_NAME fhv
391 #undef RECORD_CLASS_NAME
392 #define RECORD_CLASS_NAME Task
394 FHP(RecType, "Record Type Code");
395 FHP(RecordId, "Unique Record ID");
397 FHD(Summary, "Summary", TSKFC_TITLE, true);
398 FHD(Notes, "Notes", TSKFC_NOTES, true);
399 FHD(Categories, "Categories", TSKFC_CATEGORIES, true);
400 FHP(UID, "UID"); // FIXME - not linked to any device field??
402 FHD(StartTime, "Start Time", TSKFC_START_TIME, false);
403 FHD(DueTime, "Due Time", TSKFC_DUE_TIME, false);
404 FHD(AlarmTime, "Alarm Time", TSKFC_ALARM_TIME, false);
405 FHD(TimeZoneCode, "Time Zone Code", TSKFC_TIMEZONE_CODE, false);
406 FHP(TimeZoneValid, "Time Zone Code Valid");
408 FHE(aft, AlarmFlagType, AlarmType, "Alarm Type");
409 FHE_CONST(aft, Date, "Date");
410 FHE_CONST(aft, Relative, "Relative");
412 FHE(pft, PriorityFlagType, PriorityFlag, "Priority");
413 FHE_CONST(pft, High, "High");
414 FHE_CONST(pft, Normal, "Normal");
415 FHE_CONST(pft, Low, "Low");
417 FHE(sft, StatusFlagType, StatusFlag, "Status");
418 FHE_CONST(sft, NotStarted, "Not Started");
419 FHE_CONST(sft, InProgress, "In Progress");
420 FHE_CONST(sft, Completed, "Completed");
421 FHE_CONST(sft, Waiting, "Waiting");
422 FHE_CONST(sft, Deferred, "Deferred");
424 FHP(Unknowns, "Unknown Fields");
426 // and finally, the RecurBase fields
427 RECUR_BASE_FIELD_HANDLES
429 return fhv;
432 std::string Task::GetDescription() const
434 return Summary;
437 void Task::Dump(std::ostream &os) const
439 ios_format_state state(os);
441 static const char *PriorityName[] = { "High", "Normal", "Low" };
442 static const char *StatusName[] = { "Not Started", "In Progress",
443 "Completed", "Waiting", "Deferred" };
444 static const char *DayNames[] = { "Sun", "Mon", "Tue", "Wed",
445 "Thu", "Fri", "Sat" };
446 static const char *MonthNames[] = { "Jan", "Feb", "Mar", "Apr",
447 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
448 static const char *AlarmTypeName[] = { "None", "By Date", "Relative" };
450 os << "Task entry: 0x" << setbase(16) << RecordId
451 << " (" << (unsigned int)RecType << ")\n";
453 // cycle through the type table
454 for( const FieldLink<Task> *b = TaskFieldLinks;
455 b->type != TSKFC_END;
456 b++ )
458 if( b->strMember ) {
459 const std::string &s = this->*(b->strMember);
460 if( s.size() )
461 os << " " << b->name << ": " << Cr2LfWrapper(s) << "\n";
463 else if( b->timeMember ) {
464 TimeT t = this->*(b->timeMember);
465 if( t.IsValid() )
466 os << " " << b->name << ": " << t << "\n";
470 os << " Priority: " << PriorityName[PriorityFlag] << "\n";
471 os << " Status: " << StatusName[StatusFlag] << "\n";
472 if( AlarmType ) {
473 os << " Alarm Type: " << AlarmTypeName[AlarmType] << "\n";
475 if( TimeZoneValid )
476 os << " Time Zone: " << GetStaticTimeZone(TimeZoneCode)->Name << "\n";
478 // print recurrence data if available
479 os << " Recurring: " << (Recurring ? "yes" : "no") << "\n";
480 if( Recurring ) {
481 switch( RecurringType )
483 case Day:
484 os << " Every day.\n";
485 break;
487 case MonthByDate:
488 os << " Every month on the "
489 << DayOfMonth
490 << (DayOfMonth == 1 ? "st" : "")
491 << (DayOfMonth == 2 ? "nd" : "")
492 << (DayOfMonth == 3 ? "rd" : "")
493 << (DayOfMonth > 3 ? "th" : "")
494 << "\n";
495 break;
497 case MonthByDay:
498 os << " Every month on the "
499 << DayNames[DayOfWeek]
500 << " of week "
501 << WeekOfMonth
502 << "\n";
503 break;
505 case YearByDate:
506 os << " Every year on "
507 << MonthNames[MonthOfYear-1]
508 << " " << DayOfMonth << "\n";
509 break;
511 case YearByDay:
512 os << " Every year in " << MonthNames[MonthOfYear-1]
513 << " on "
514 << DayNames[DayOfWeek]
515 << " of week " << WeekOfMonth << "\n";
516 break;
518 case Week:
519 os << " Every week on: ";
520 if( WeekDays & CAL_WD_SUN ) os << "Sun ";
521 if( WeekDays & CAL_WD_MON ) os << "Mon ";
522 if( WeekDays & CAL_WD_TUE ) os << "Tue ";
523 if( WeekDays & CAL_WD_WED ) os << "Wed ";
524 if( WeekDays & CAL_WD_THU ) os << "Thu ";
525 if( WeekDays & CAL_WD_FRI ) os << "Fri ";
526 if( WeekDays & CAL_WD_SAT ) os << "Sat ";
527 os << "\n";
528 break;
530 default:
531 os << " Unknown recurrence type\n";
532 break;
535 os << " Interval: " << Interval << "\n";
537 if( Perpetual )
538 os << " Ends: never\n";
539 else
540 os << " Ends: " << RecurringEndTime << "\n";
543 if( Categories.size() ) {
544 string display;
545 Categories.CategoryList2Str(display);
546 os << " Categories: " << display << "\n";
549 os << Unknowns;
550 os << "\n\n";
553 bool Task::operator<(const Task &other) const
555 if( StartTime != other.StartTime )
556 return StartTime < other.StartTime;
557 if( AlarmTime != other.AlarmTime )
558 return AlarmTime < other.AlarmTime;
560 int cmp = Summary.compare(other.Summary);
561 if( cmp == 0 )
562 cmp = Notes.compare(other.Notes);
563 return cmp < 0;
566 } // namespace Barry