3 /// Record parsing class for the task database.
7 Copyright (C) 2005-2009, 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.
24 #include "r_calendar.h" // for CAL_* defines
25 #include "record-internal.h"
26 #include "protostructs.h"
36 using namespace Barry::Protocol
;
40 ///////////////////////////////////////////////////////////////////////////////
44 #define TSKFC_TASK_TYPE 0x01
45 #define TSKFC_TITLE 0x02
46 #define TSKFC_NOTES 0x03
47 #define TSKFC_START_TIME 0x05
48 #define TSKFC_DUE_TIME 0x06
49 #define TSKFC_DUE_FLAG 0x08
50 #define TSKFC_STATUS 0x09
51 #define TSKFC_PRIORITY 0x0a
52 #define TSKFC_ALARM_TYPE 0x0e
53 #define TSKFC_ALARM_TIME 0x0f
54 #define TSKFC_TIMEZONE_CODE 0x10
55 #define TSKFC_CATEGORIES 0x11
56 #define TSKFC_END 0xffff
58 static FieldLink
<Task
> TaskFieldLinks
[] = {
59 { TSKFC_TITLE
, "Summary", 0, 0, &Task::Summary
, 0, 0, 0, 0, true },
60 { TSKFC_NOTES
, "Notes", 0, 0, &Task::Notes
, 0, 0, 0, 0, true },
61 { TSKFC_START_TIME
, "Start Time", 0, 0, 0, 0, &Task::StartTime
, 0, 0, false },
62 { TSKFC_DUE_TIME
, "Due Time", 0, 0, 0, 0, &Task::DueTime
, 0, 0, false },
63 { TSKFC_ALARM_TIME
, "Alarm Time", 0, 0, 0, 0, &Task::AlarmTime
, 0, 0, false },
64 { TSKFC_CATEGORIES
, "Categories", 0, 0, &Task::Categories
, 0, 0, 0, 0, false },
65 { TSKFC_END
, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
77 const unsigned char* Task::ParseField(const unsigned char *begin
,
78 const unsigned char *end
,
81 const CommonField
*field
= (const CommonField
*) begin
;
83 // advance and check size
84 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
85 if( begin
> end
) // if begin==end, we are ok
88 if( !btohs(field
->size
) ) // if field has no size, something's up
91 if( field
->type
== TSKFC_TASK_TYPE
) {
92 if( ( TaskType
= field
->u
.raw
[0] ) != 't' ) {
93 throw Error("Task::ParseField: Task Type is not 't'");
98 // cycle through the type table
99 for( FieldLink
<Task
> *b
= TaskFieldLinks
;
100 b
->type
!= TSKFC_END
;
103 if( b
->type
== field
->type
) {
105 std::string
&s
= this->*(b
->strMember
);
106 s
= ParseFieldString(field
);
107 if( b
->iconvNeeded
&& ic
)
109 return begin
; // done!
111 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
112 time_t &t
= this->*(b
->timeMember
);
113 t
= min2time(field
->u
.min1900
);
118 // handle special cases
119 switch( field
->type
)
122 if( field
->u
.raw
[0] > Low
) {
123 throw Error( "Task::ParseField: priority field out of bounds" );
126 PriorityFlag
= (PriorityFlagType
)field
->u
.raw
[0];
131 if( field
->u
.raw
[0] > Deferred
) {
132 throw Error( "Task::ParseField: priority field out of bounds" );
135 StatusFlag
= (StatusFlagType
)field
->u
.raw
[0];
139 case TSKFC_TIMEZONE_CODE
:
140 if( btohs(field
->size
) == 4 ) {
141 TimeZoneCode
= btohs(field
->u
.code
);
144 throw Error("Task::ParseField: not enough data in time zone code field");
149 DueDateFlag
= field
->u
.raw
[0];
152 case TSKFC_ALARM_TYPE
:
153 if( field
->u
.raw
[0] > Relative
) {
154 throw Error("Task::ParseField: AlarmType out of bounds" );
157 AlarmType
= (AlarmFlagType
)field
->u
.raw
[0];
162 // base class handles recurring data
163 if( RecurBase::ParseField(field
->type
, field
->u
.raw
, btohs(field
->size
), ic
) )
166 // if still not handled, add to the Unknowns list
168 uf
.type
= field
->type
;
169 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
170 Unknowns
.push_back(uf
);
172 // return new pointer for next field
176 void Task::ParseHeader(const Data
&data
, size_t &offset
)
178 // no header in Task records
181 void Task::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
183 const unsigned char *finish
= ParseCommonFields(*this,
184 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
185 offset
+= finish
- (data
.GetData() + offset
);
195 StartTime
= DueTime
= AlarmTime
= 0;
197 PriorityFlag
= (PriorityFlagType
)0;
198 StatusFlag
= (StatusFlagType
)0;
199 AlarmType
= (AlarmFlagType
)0;
205 TimeZoneCode
= GetTimeZoneCode( 0, 0 );
210 void Task::Dump(std::ostream
&os
) const
212 static const char *PriorityName
[] = { "High", "Normal", "Low" };
213 static const char *StatusName
[] = { "Not Started", "In Progress",
214 "Completed", "Waiting", "Deferred" };
215 static const char *DayNames
[] = { "Sun", "Mon", "Tue", "Wed",
216 "Thu", "Fri", "Sat" };
217 static const char *MonthNames
[] = { "Jan", "Feb", "Mar", "Apr",
218 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
219 static const char *AlarmTypeName
[] = { "None", "By Date", "Relative" };
221 os
<< "Task entry: 0x" << setbase(16) << RecordId
222 << " (" << (unsigned int)RecType
<< ")\n";
224 // cycle through the type table
225 for( const FieldLink
<Task
> *b
= TaskFieldLinks
;
226 b
->type
!= TSKFC_END
;
230 const std::string
&s
= this->*(b
->strMember
);
232 os
<< " " << b
->name
<< ": " << s
<< "\n";
234 else if( b
->timeMember
) {
235 time_t t
= this->*(b
->timeMember
);
237 os
<< " " << b
->name
<< ": " << ctime(&t
);
241 os
<< " Priority: " << PriorityName
[PriorityFlag
] << "\n";
242 os
<< " Status: " << StatusName
[StatusFlag
] << "\n";
244 os
<< " Alarm Type: " << AlarmTypeName
[AlarmType
] << "\n";
247 // print recurrence data if available
248 os
<< " Recurring: " << (Recurring
? "yes" : "no") << "\n";
250 switch( RecurringType
)
253 os
<< " Every day.\n";
257 os
<< " Every month on the "
259 << (DayOfMonth
== 1 ? "st" : "")
260 << (DayOfMonth
== 2 ? "nd" : "")
261 << (DayOfMonth
== 3 ? "rd" : "")
262 << (DayOfMonth
> 3 ? "th" : "")
267 os
<< " Every month on the "
268 << DayNames
[DayOfWeek
]
275 os
<< " Every year on "
276 << MonthNames
[MonthOfYear
-1]
277 << " " << DayOfMonth
<< "\n";
281 os
<< " Every year in " << MonthNames
[MonthOfYear
-1]
283 << DayNames
[DayOfWeek
]
284 << " of week " << WeekOfMonth
<< "\n";
288 os
<< " Every week on: ";
289 if( WeekDays
& CAL_WD_SUN
) os
<< "Sun ";
290 if( WeekDays
& CAL_WD_MON
) os
<< "Mon ";
291 if( WeekDays
& CAL_WD_TUE
) os
<< "Tue ";
292 if( WeekDays
& CAL_WD_WED
) os
<< "Wed ";
293 if( WeekDays
& CAL_WD_THU
) os
<< "Thu ";
294 if( WeekDays
& CAL_WD_FRI
) os
<< "Fri ";
295 if( WeekDays
& CAL_WD_SAT
) os
<< "Sat ";
300 os
<< " Unknown recurrence type\n";
304 os
<< " Interval: " << Interval
<< "\n";
307 os
<< " Ends: never\n";
309 os
<< " Ends: " << ctime(&RecurringEndTime
);