00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "r_task.h"
00024 #include "r_calendar.h"
00025 #include "record-internal.h"
00026 #include "protostructs.h"
00027 #include "data.h"
00028 #include "time.h"
00029 #include "iconv.h"
00030 #include "debug.h"
00031 #include <ostream>
00032 #include <iomanip>
00033 #include <string.h>
00034
00035 using namespace std;
00036 using namespace Barry::Protocol;
00037
00038 namespace Barry {
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 Task::AlarmFlagType Task::AlarmProto2Rec(uint8_t a)
00053 {
00054 return (AlarmFlagType)a;
00055 }
00056
00057 uint8_t Task::AlarmRec2Proto(AlarmFlagType a)
00058 {
00059 return a;
00060 }
00061
00062 Task::PriorityFlagType Task::PriorityProto2Rec(uint8_t p)
00063 {
00064 return (PriorityFlagType)p;
00065 }
00066
00067 uint8_t Task::PriorityRec2Proto(PriorityFlagType p)
00068 {
00069 return p;
00070 }
00071
00072 Task::StatusFlagType Task::StatusProto2Rec(uint8_t s)
00073 {
00074 return (StatusFlagType)s;
00075 }
00076
00077 uint8_t Task::StatusRec2Proto(StatusFlagType s)
00078 {
00079 return s;
00080 }
00081
00082
00083
00084
00085
00086
00087 #define TSKFC_TASK_TYPE 0x01
00088 #define TSKFC_TITLE 0x02
00089 #define TSKFC_NOTES 0x03
00090 #define TSKFC_DUE_TIME 0x05
00091 #define TSKFC_START_TIME 0x06 // This is fuzzy... most devices seem
00092
00093 #define TSKFC_DUE_FLAG 0x08
00094 #define TSKFC_STATUS 0x09
00095 #define TSKFC_PRIORITY 0x0a
00096 #define TSKFC_ALARM_TYPE 0x0e
00097 #define TSKFC_ALARM_TIME 0x0f
00098 #define TSKFC_TIMEZONE_CODE 0x10
00099 #define TSKFC_CATEGORIES 0x11
00100 #define TSKFC_END 0xffff
00101
00102 static FieldLink<Task> TaskFieldLinks[] = {
00103 { TSKFC_TITLE, "Summary", 0, 0, &Task::Summary, 0, 0, 0, 0, true },
00104 { TSKFC_NOTES, "Notes", 0, 0, &Task::Notes, 0, 0, 0, 0, true },
00105 { TSKFC_START_TIME, "Start Time", 0, 0, 0, 0, &Task::StartTime, 0, 0, false },
00106 { TSKFC_DUE_TIME, "Due Time", 0, 0, 0, 0, &Task::DueTime, 0, 0, false },
00107 { TSKFC_ALARM_TIME, "Alarm Time", 0, 0, 0, 0, &Task::AlarmTime, 0, 0, false },
00108 { TSKFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
00109 };
00110
00111 Task::Task()
00112 {
00113 Clear();
00114 }
00115
00116 Task::~Task()
00117 {
00118 }
00119
00120 const unsigned char* Task::ParseField(const unsigned char *begin,
00121 const unsigned char *end,
00122 const IConverter *ic)
00123 {
00124 const CommonField *field = (const CommonField *) begin;
00125
00126
00127 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
00128 if( begin > end )
00129 return begin;
00130
00131 if( !btohs(field->size) )
00132 return begin;
00133
00134 if( field->type == TSKFC_TASK_TYPE ) {
00135 if( field->u.raw[0] != 't' ) {
00136 throw Error("Task::ParseField: Task Type is not 't'");
00137 }
00138 return begin;
00139 }
00140
00141
00142 for( FieldLink<Task> *b = TaskFieldLinks;
00143 b->type != TSKFC_END;
00144 b++ )
00145 {
00146 if( b->type == field->type ) {
00147 if( b->strMember ) {
00148 std::string &s = this->*(b->strMember);
00149 s = ParseFieldString(field);
00150 if( b->iconvNeeded && ic )
00151 s = ic->FromBB(s);
00152 return begin;
00153 }
00154 else if( b->timeMember && btohs(field->size) == 4 ) {
00155 time_t &t = this->*(b->timeMember);
00156 t = min2time(field->u.min1900);
00157 return begin;
00158 }
00159 }
00160 }
00161
00162 switch( field->type )
00163 {
00164 case TSKFC_PRIORITY:
00165 if( field->u.raw[0] > TR_PRIORITY_RANGE_HIGH ) {
00166 throw Error( "Task::ParseField: priority field out of bounds" );
00167 }
00168 else {
00169 PriorityFlag = PriorityProto2Rec(field->u.raw[0]);
00170 }
00171 return begin;
00172
00173 case TSKFC_STATUS:
00174 if( field->u.raw[0] > TR_STATUS_RANGE_HIGH ) {
00175 throw Error( "Task::ParseField: priority field out of bounds" );
00176 }
00177 else {
00178 StatusFlag = StatusProto2Rec(field->u.raw[0]);
00179 }
00180 return begin;
00181
00182 case TSKFC_TIMEZONE_CODE:
00183 if( btohs(field->size) == 4 ) {
00184 TimeZoneCode = btohs(field->u.code);
00185 TimeZoneValid = true;
00186 }
00187 else {
00188 throw Error("Task::ParseField: not enough data in time zone code field");
00189 }
00190 return begin;
00191
00192 case TSKFC_DUE_FLAG:
00193 DueDateFlag = field->u.raw[0];
00194 return begin;
00195
00196 case TSKFC_ALARM_TYPE:
00197 if( field->u.raw[0] > TR_ALARM_RANGE_HIGH ) {
00198 throw Error("Task::ParseField: AlarmType out of bounds" );
00199 }
00200 else {
00201 AlarmType = AlarmProto2Rec(field->u.raw[0]);
00202 }
00203 return begin;
00204
00205 case TSKFC_CATEGORIES:
00206 {
00207 std::string catstring = ParseFieldString(field);
00208 if( ic )
00209 catstring = ic->FromBB(catstring);
00210 Categories.CategoryStr2List(catstring);
00211 }
00212 return begin;
00213 }
00214
00215 if( RecurBase::ParseField(field->type, field->u.raw, btohs(field->size), ic) )
00216 return begin;
00217
00218
00219 UnknownField uf;
00220 uf.type = field->type;
00221 uf.data.assign((const char*)field->u.raw, btohs(field->size));
00222 Unknowns.push_back(uf);
00223
00224
00225 return begin;
00226 }
00227
00228 void Task::ParseHeader(const Data &data, size_t &offset)
00229 {
00230
00231 }
00232
00233 void Task::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
00234 {
00235 const unsigned char *finish = ParseCommonFields(*this,
00236 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
00237 offset += finish - (data.GetData() + offset);
00238 }
00239
00240
00241 void Task::BuildHeader(Data &data, size_t &offset) const
00242 {
00243
00244 }
00245
00246
00247
00248
00249
00250
00251
00252 void Task::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
00253 {
00254 data.Zap();
00255
00256
00257 BuildField(data, offset, TSKFC_TASK_TYPE, 't');
00258
00259 BuildField(data, offset, TSKFC_STATUS, StatusRec2Proto(StatusFlag));
00260 BuildField(data, offset, TSKFC_PRIORITY, PriorityRec2Proto(PriorityFlag));
00261 BuildField(data, offset, TSKFC_ALARM_TYPE, AlarmRec2Proto(AlarmType));
00262
00263 if ( DueDateFlag )
00264 BuildField(data, offset, TSKFC_DUE_FLAG, (char) 1);
00265 else
00266 BuildField(data, offset, TSKFC_DUE_FLAG, (char) 0);
00267
00268 if( TimeZoneValid )
00269 BuildField(data, offset, TSKFC_TIMEZONE_CODE, TimeZoneCode);
00270
00271
00272 for( FieldLink<Task> *b = TaskFieldLinks;
00273 b->type != TSKFC_END;
00274 b++ )
00275 {
00276
00277 if( b->strMember ) {
00278 const std::string &field = this->*(b->strMember);
00279 if( field.size() ) {
00280 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
00281 BuildField(data, offset, b->type, s);
00282 }
00283 }
00284 else if( b->timeMember ) {
00285 time_t t = this->*(b->timeMember);
00286 if( t > 0 )
00287 BuildField1900(data, offset, b->type, t);
00288 }
00289 else if( b->postMember && b->postField ) {
00290 const std::string &field = (this->*(b->postMember)).*(b->postField);
00291 if( field.size() ) {
00292 std::string s = (b->iconvNeeded && ic) ? ic->ToBB(field) : field;
00293 BuildField(data, offset, b->type, s);
00294 }
00295 }
00296 }
00297
00298
00299 if( Categories.size() ) {
00300 string store;
00301 Categories.CategoryList2Str(store);
00302 BuildField(data, offset, TSKFC_CATEGORIES, ic ? ic->ToBB(store) : store);
00303 }
00304
00305
00306 UnknownsType::const_iterator
00307 ub = Unknowns.begin(), ue = Unknowns.end();
00308 for( ; ub != ue; ub++ ) {
00309 BuildField(data, offset, *ub);
00310 }
00311
00312 data.ReleaseBuffer(offset);
00313 }
00314
00315
00316
00317 void Task::Clear()
00318 {
00319 RecurBase::Clear();
00320
00321 Summary.clear();
00322 Notes.clear();
00323 Categories.clear();
00324 StartTime = DueTime = AlarmTime = 0;
00325
00326 PriorityFlag = (PriorityFlagType)0;
00327 StatusFlag = (StatusFlagType)0;
00328 AlarmType = (AlarmFlagType)0;
00329
00330 DueDateFlag = false;
00331
00332 TimeZoneCode = GetTimeZoneCode( 0, 0 );
00333 TimeZoneValid = false;
00334
00335 Unknowns.clear();
00336 }
00337
00338 void Task::Dump(std::ostream &os) const
00339 {
00340 static const char *PriorityName[] = { "High", "Normal", "Low" };
00341 static const char *StatusName[] = { "Not Started", "In Progress",
00342 "Completed", "Waiting", "Deferred" };
00343 static const char *DayNames[] = { "Sun", "Mon", "Tue", "Wed",
00344 "Thu", "Fri", "Sat" };
00345 static const char *MonthNames[] = { "Jan", "Feb", "Mar", "Apr",
00346 "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
00347 static const char *AlarmTypeName[] = { "None", "By Date", "Relative" };
00348
00349 os << "Task entry: 0x" << setbase(16) << RecordId
00350 << " (" << (unsigned int)RecType << ")\n";
00351
00352
00353 for( const FieldLink<Task> *b = TaskFieldLinks;
00354 b->type != TSKFC_END;
00355 b++ )
00356 {
00357 if( b->strMember ) {
00358 const std::string &s = this->*(b->strMember);
00359 if( s.size() )
00360 os << " " << b->name << ": " << s << "\n";
00361 }
00362 else if( b->timeMember ) {
00363 time_t t = this->*(b->timeMember);
00364 if( t > 0 )
00365 os << " " << b->name << ": " << ctime(&t);
00366 }
00367 }
00368
00369 os << " Due Date Flag: " << (DueDateFlag ? "true" : "false") << "\n";
00370 os << " Priority: " << PriorityName[PriorityFlag] << "\n";
00371 os << " Status: " << StatusName[StatusFlag] << "\n";
00372 if( AlarmType ) {
00373 os << " Alarm Type: " << AlarmTypeName[AlarmType] << "\n";
00374 }
00375 if( TimeZoneValid )
00376 os << " Time Zone: " << GetTimeZone(TimeZoneCode)->Name << "\n";
00377
00378
00379 os << " Recurring: " << (Recurring ? "yes" : "no") << "\n";
00380 if( Recurring ) {
00381 switch( RecurringType )
00382 {
00383 case Day:
00384 os << " Every day.\n";
00385 break;
00386
00387 case MonthByDate:
00388 os << " Every month on the "
00389 << DayOfMonth
00390 << (DayOfMonth == 1 ? "st" : "")
00391 << (DayOfMonth == 2 ? "nd" : "")
00392 << (DayOfMonth == 3 ? "rd" : "")
00393 << (DayOfMonth > 3 ? "th" : "")
00394 << "\n";
00395 break;
00396
00397 case MonthByDay:
00398 os << " Every month on the "
00399 << DayNames[DayOfWeek]
00400 << " of week "
00401 << WeekOfMonth
00402 << "\n";
00403 break;
00404
00405 case YearByDate:
00406 os << " Every year on "
00407 << MonthNames[MonthOfYear-1]
00408 << " " << DayOfMonth << "\n";
00409 break;
00410
00411 case YearByDay:
00412 os << " Every year in " << MonthNames[MonthOfYear-1]
00413 << " on "
00414 << DayNames[DayOfWeek]
00415 << " of week " << WeekOfMonth << "\n";
00416 break;
00417
00418 case Week:
00419 os << " Every week on: ";
00420 if( WeekDays & CAL_WD_SUN ) os << "Sun ";
00421 if( WeekDays & CAL_WD_MON ) os << "Mon ";
00422 if( WeekDays & CAL_WD_TUE ) os << "Tue ";
00423 if( WeekDays & CAL_WD_WED ) os << "Wed ";
00424 if( WeekDays & CAL_WD_THU ) os << "Thu ";
00425 if( WeekDays & CAL_WD_FRI ) os << "Fri ";
00426 if( WeekDays & CAL_WD_SAT ) os << "Sat ";
00427 os << "\n";
00428 break;
00429
00430 default:
00431 os << " Unknown recurrence type\n";
00432 break;
00433 }
00434
00435 os << " Interval: " << Interval << "\n";
00436
00437 if( Perpetual )
00438 os << " Ends: never\n";
00439 else
00440 os << " Ends: " << ctime(&RecurringEndTime);
00441 }
00442
00443 if( Categories.size() ) {
00444 string display;
00445 Categories.CategoryList2Str(display);
00446 os << " Categories: " << display << "\n";
00447 }
00448
00449 os << Unknowns;
00450 os << "\n\n";
00451 }
00452
00453 bool Task::operator<(const Task &other) const
00454 {
00455 if( StartTime != other.StartTime )
00456 return StartTime < other.StartTime;
00457 if( AlarmTime != other.AlarmTime )
00458 return AlarmTime < other.AlarmTime;
00459
00460 int cmp = Summary.compare(other.Summary);
00461 if( cmp == 0 )
00462 cmp = Notes.compare(other.Notes);
00463 return cmp < 0;
00464 }
00465
00466 }
00467