maint: remove -j2 flags on Fedora/OpenSUSE to workaround Make 3.82 borkage
[barry/progweb.git] / src / r_calendar.cc
blobf403ba6678f8dbe3d657ab6c550c81d4b7d637d2
1 ///
2 /// \file r_calendar.cc
3 /// Blackberry database record parser class for calendar records.
4 ///
6 /*
7 Copyright (C) 2005-2012, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "r_calendar.h"
23 #include "r_recur_base-int.h"
24 #include "record-internal.h"
25 #include "protocol.h"
26 #include "protostructs.h"
27 #include "data.h"
28 #include "time.h"
29 #include "error.h"
30 #include "endian.h"
31 #include "iconv.h"
32 #include <ostream>
33 #include <iomanip>
34 #include <time.h>
35 #include <string.h>
36 #include <stdexcept>
37 #include "ios_state.h"
39 #define __DEBUG_MODE__
40 #include "debug.h"
42 using namespace std;
43 using namespace Barry::Protocol;
45 namespace Barry {
48 ///////////////////////////////////////////////////////////////////////////////
49 // Calendar class, static members
52 // Note! These functions currently only pass the same values through.
53 // In actuality, these are technically two different values:
54 // one on the raw protocol side, and the other part of the
55 // guaranteed Barry API. If the Blackberry ever changes the
56 // meanings for these codes, do the translation here.
59 Calendar::FreeBusyFlagType Calendar::FreeBusyFlagProto2Rec(uint8_t f)
61 return (FreeBusyFlagType)f;
64 uint8_t Calendar::FreeBusyFlagRec2Proto(FreeBusyFlagType f)
66 return f;
69 Calendar::ClassFlagType Calendar::ClassFlagProto2Rec(uint8_t f)
71 return (ClassFlagType)f;
74 uint8_t Calendar::ClassFlagRec2Proto(ClassFlagType f)
76 return f;
81 ///////////////////////////////////////////////////////////////////////////////
82 // Calendar class
84 // calendar field codes
85 #define CALFC_APPT_TYPE_FLAG 0x01
86 #define CALFC_SUBJECT 0x02
87 #define CALFC_NOTES 0x03
88 #define CALFC_LOCATION 0x04
89 #define CALFC_NOTIFICATION_TIME 0x05
90 #define CALFC_START_TIME 0x06
91 #define CALFC_END_TIME 0x07
92 #define CALFC_ACCEPTED_BY 0x0b
93 #define CALFC_VERSION_DATA 0x10
94 #define CALFC_INVITED 0x15
95 #define CALFC_ORGANIZER 0x16
96 #define CALFC_NOTIFICATION_DATA 0x1a
97 #define CALFC_FREEBUSY_FLAG 0x1c
98 #define CALFC_TIMEZONE_CODE 0x1e // only seems to show up if recurring
99 #define CALFC_CLASS_FLAG 0x28 // private flag from outlook
100 #define CALFC_CALENDAR_ID 0x2b // Calendar using (new devices have several calendar)
101 #define CALFC_ALLDAYEVENT_FLAG 0xff
102 #define CALFC_END 0xffff
104 static FieldLink<Calendar> CalendarFieldLinks[] = {
105 { CALFC_SUBJECT, "Subject", 0, 0, &Calendar::Subject, 0, 0, 0, 0, true },
106 { CALFC_NOTES, "Notes", 0, 0, &Calendar::Notes, 0, 0, 0, 0, true },
107 { CALFC_LOCATION, "Location", 0, 0, &Calendar::Location, 0, 0, 0, 0, true },
108 { CALFC_NOTIFICATION_TIME,"Notification Time",0,0, 0, 0, &Calendar::NotificationTime, 0, 0, false },
109 { CALFC_START_TIME, "Start Time", 0, 0, 0, 0, &Calendar::StartTime, 0, 0, false },
110 { CALFC_END_TIME, "End Time", 0, 0, 0, 0, &Calendar::EndTime, 0, 0, false },
111 { CALFC_ORGANIZER, "Organizer", 0, 0, 0, &Calendar::Organizer, 0, 0, 0, true },
112 { CALFC_ACCEPTED_BY,"Accepted By",0, 0, 0, &Calendar::AcceptedBy, 0, 0, 0, true },
113 { CALFC_INVITED, "Invited", 0, 0, 0, &Calendar::Invited, 0, 0, 0, true },
114 { CALFC_END, "End of List",0, 0, 0, 0, 0, 0, 0, false }
117 Calendar::Calendar()
119 Clear();
122 Calendar::~Calendar()
126 const unsigned char* Calendar::ParseField(const unsigned char *begin,
127 const unsigned char *end,
128 const IConverter *ic)
130 const CommonField *field = (const CommonField *) begin;
132 // advance and check size
133 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
134 if( begin > end ) // if begin==end, we are ok
135 return begin;
137 if( !btohs(field->size) ) // if field has no size, something's up
138 return begin;
140 // cycle through the type table
141 for( FieldLink<Calendar> *b = CalendarFieldLinks;
142 b->type != CALFC_END;
143 b++ )
145 if( b->type == field->type ) {
146 if( b->strMember ) {
147 std::string &s = this->*(b->strMember);
148 s = ParseFieldString(field);
149 if( b->iconvNeeded && ic )
150 s = ic->FromBB(s);
151 return begin; // done!
153 else if( b->timeMember && btohs(field->size) == 4 ) {
154 time_t &t = this->*(b->timeMember);
155 dout("min1900: " << field->u.min1900);
156 t = min2time(field->u.min1900);
157 return begin;
159 else if( b->addrMember ) {
161 // parse email address
162 // get dual addr+name string first
163 // Note: this is a different format than
164 // used in r_message*.cc
166 std::string dual((const char*)field->u.raw, btohs(field->size));
168 EmailAddress a;
170 // assign first string, using null terminator
171 // letting std::string add it for us if it
172 // doesn't exist
173 a.Email = dual.c_str();
175 // assign second string, using first size
176 // as starting point
177 a.Name = dual.c_str() + a.Email.size() + 1;
179 // if the address is non-empty, add to list
180 if( a.size() ) {
181 // i18n convert if needed
182 if( b->iconvNeeded && ic ) {
183 a.Name = ic->FromBB(a.Name);
184 a.Email = ic->FromBB(a.Email);
187 EmailAddressList &al = this->*(b->addrMember);
188 al.push_back(a);
191 return begin;
196 // handle special cases
197 switch( field->type )
199 case CALFC_APPT_TYPE_FLAG:
200 switch( field->u.raw[0] )
202 case 'a': // regular non-recurring appointment
203 Recurring = false;
204 return begin;
206 case '*': // recurring appointment
207 Recurring = true;
208 return begin;
210 default:
211 throw Error("Calendar::ParseField: unknown appointment type");
213 break;
215 case CALFC_ALLDAYEVENT_FLAG:
216 AllDayEvent = field->u.raw[0] == 1;
217 return begin;
219 case CALFC_TIMEZONE_CODE:
220 if( btohs(field->size) == 2 ) {
221 // good data
222 TimeZoneCode = btohs(field->u.code);
223 TimeZoneValid = true;
225 else {
226 throw Error("Calendar::ParseField: not enough data in time zone code field");
228 return begin;
230 case CALFC_FREEBUSY_FLAG:
231 if( field->u.raw[0] > CR_FREEBUSY_RANGE_HIGH ) {
232 throw Error("Calendar::ParseField: FreeBusyFlag out of range" );
234 FreeBusyFlag = FreeBusyFlagProto2Rec(field->u.raw[0]);
235 return begin;
237 case CALFC_CALENDAR_ID:
238 if( btohs(field->size) == 8 ) {
239 CalendarID = btohll(field->u.uint64);
241 else {
242 throw Error("Calendar::ParseField: size data unknown in calendar field");
244 return begin;
246 case CALFC_CLASS_FLAG:
247 if( field->u.raw[0] > CR_CLASS_RANGE_HIGH ) {
248 throw Error("Calendar::ParseField: ClassFlag out of range" );
250 ClassFlag = ClassFlagProto2Rec(field->u.raw[0]);
251 return begin;
254 // base class handles recurring data
255 if( RecurBase::ParseField(field->type, field->u.raw, btohs(field->size), ic) )
256 return begin;
258 // if still not handled, add to the Unknowns list
259 UnknownField uf;
260 uf.type = field->type;
261 uf.data.assign((const char*)field->u.raw, btohs(field->size));
262 Unknowns.push_back(uf);
264 // return new pointer for next field
265 return begin;
268 void Calendar::ParseHeader(const Data &data, size_t &offset)
270 // no header in Calendar records
273 void Calendar::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
275 const unsigned char *finish = ParseCommonFields(*this,
276 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
277 offset += finish - (data.GetData() + offset);
280 void Calendar::BuildHeader(Data &data, size_t &offset) const
282 // no header in Calendar records
286 // Build
288 /// Build fields part of record.
290 void Calendar::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
292 data.Zap();
294 // output the type first
295 BuildField(data, offset, CALFC_APPT_TYPE_FLAG, Recurring ? '*' : 'a');
297 // output all day event flag only if set
298 if( AllDayEvent )
299 BuildField(data, offset, CALFC_ALLDAYEVENT_FLAG, (char)1);
301 // cycle through the type table
302 for( const FieldLink<Calendar> *b = CalendarFieldLinks;
303 b->type != CALFC_END;
304 b++ )
306 if( b->strMember ) {
307 const std::string &s = this->*(b->strMember);
308 if( s.size() )
309 BuildField(data, offset, b->type, (b->iconvNeeded && ic) ? ic->ToBB(s) : s);
311 else if( b->timeMember ) {
312 time_t t = this->*(b->timeMember);
313 if( t > 0 )
314 BuildField1900(data, offset, b->type, t);
316 else if( b->addrMember ) {
317 const EmailAddressList &al = this->*(b->addrMember);
318 EmailAddressList::const_iterator lb = al.begin(), le = al.end();
320 // add all entries in list
321 for( ; lb != le; ++lb ) {
323 // skip empty entries
324 if( !lb->size() )
325 continue;
327 std::string Name = lb->Name,
328 Email = lb->Email;
330 // do i18n conversion only if needed
331 if( b->iconvNeeded && ic ) {
332 Name = ic->ToBB(Name);
333 Email = ic->ToBB(Email);
337 // Build an addr+name field, each string
338 // null terminated.
339 // Note: this is a different format than
340 // what is used in r_message*.cc
342 std::string field(lb->Email.c_str(), lb->Email.size() + 1);
343 field.append(lb->Name.c_str(), lb->Name.size() + 1);
344 BuildField(data, offset, b->type, field.data(), field.size());
349 // handle special cases
350 if( Recurring ) {
351 CalendarRecurrenceDataField recur;
352 BuildRecurrenceData(StartTime, &recur);
353 BuildField(data, offset, RecurBase::RecurringFieldType(),
354 &recur, CALENDAR_RECURRENCE_DATA_FIELD_SIZE);
357 if( TimeZoneValid )
358 BuildField(data, offset, CALFC_TIMEZONE_CODE, TimeZoneCode);
360 BuildField(data, offset, CALFC_FREEBUSY_FLAG, FreeBusyFlagRec2Proto(FreeBusyFlag));
361 BuildField(data, offset, CALFC_CLASS_FLAG, ClassFlagRec2Proto(ClassFlag));
363 // If CalendarID is defined and most of supported !
364 // (by default 0xffff ffff ffff ffff)
365 if( CalendarID != (uint64_t) -1 )
366 BuildField(data, offset, CALFC_CALENDAR_ID, CalendarID);
368 // and finally save unknowns
369 UnknownsType::const_iterator
370 ub = Unknowns.begin(), ue = Unknowns.end();
371 for( ; ub != ue; ub++ ) {
372 BuildField(data, offset, *ub);
375 data.ReleaseBuffer(offset);
378 void Calendar::Clear()
380 // clear the base class too
381 RecurBase::Clear();
383 // clear our fields
384 RecType = GetDefaultRecType();
385 RecordId = 0;
387 AllDayEvent = false;
388 Subject.clear();
389 Notes.clear();
390 Location.clear();
391 NotificationTime = StartTime = EndTime = 0;
392 Organizer.clear();
393 AcceptedBy.clear();
394 Invited.clear();
396 FreeBusyFlag = Free;
397 ClassFlag = Public;
399 CalendarID = btohll((uint64_t) -1);
401 TimeZoneCode = GetTimeZoneCode(0, 0); // default to GMT
402 TimeZoneValid = false;
404 Unknowns.clear();
407 const std::vector<FieldHandle<Calendar> >& Calendar::GetFieldHandles()
409 static std::vector<FieldHandle<Calendar> > fhv;
411 if( fhv.size() )
412 return fhv;
414 #undef CONTAINER_OBJECT_NAME
415 #define CONTAINER_OBJECT_NAME fhv
417 #undef RECORD_CLASS_NAME
418 #define RECORD_CLASS_NAME Calendar
420 #define ALL_COMMON_CALENDAR_FIELDS \
421 FHP(RecType, "Record Type Code"); \
422 FHP(RecordId, "Unique Record ID"); \
424 FHP(AllDayEvent, "All Day Event"); \
425 FHD(Subject, "Subject", CALFC_SUBJECT, true); \
426 FHD(Notes, "Notes", CALFC_NOTES, true); \
427 FHD(Location, "Location", CALFC_LOCATION, true); \
428 FHD(NotificationTime, "Notification Time (0 is off)", \
429 CALFC_NOTIFICATION_TIME, false); \
430 FHD(StartTime, "Start Time", CALFC_START_TIME, false); \
431 FHD(EndTime, "End Time", CALFC_END_TIME, false); \
432 FHD(Organizer, "Organizer", CALFC_ORGANIZER, true); \
433 FHD(AcceptedBy, "Accepted By", CALFC_ACCEPTED_BY, true); \
434 FHD(Invited, "Invited", CALFC_INVITED, true); \
436 FHE(fbf, FreeBusyFlagType, FreeBusyFlag, "Free or Busy Flag"); \
437 FHE_CONST(fbf, Free, "Free"); \
438 FHE_CONST(fbf, Tentative, "Tentative"); \
439 FHE_CONST(fbf, Busy, "Busy"); \
440 FHE_CONST(fbf, OutOfOffice, "Out of Office"); \
442 FHE(cf, ClassFlagType, ClassFlag, "Event Class"); \
443 FHE_CONST(cf, Public, "Public"); \
444 FHE_CONST(cf, Confidential, "Confidential"); \
445 FHE_CONST(cf, Private, "Private"); \
447 FHP(TimeZoneCode, "Time Zone Code"); \
448 FHP(TimeZoneValid, "Time Zone Validity"); \
450 FHP(Unknowns, "Unknown Fields");
452 ALL_COMMON_CALENDAR_FIELDS
454 // the fields unique to Calendar, or different in CalendarALL
455 FHD(CalendarID, "Calendar ID", CALFC_CALENDAR_ID, false);
457 // and finally, the RecurBase fields
458 RECUR_BASE_FIELD_HANDLES
460 return fhv;
463 std::string Calendar::GetDescription() const
465 return Subject;
468 void Calendar::DumpSpecialFields(std::ostream &os) const
470 ios_format_state state(os);
472 static const char *ClassTypes[] = { "Public", "Confidential", "Private" };
473 static const char *FreeBusy[] = { "Free", "Tentative", "Busy", "Out of Office" };
475 os << " Calendar ID: 0x" << setbase(16) << CalendarID << "\n";
476 os << " All Day Event: " << (AllDayEvent ? "yes" : "no") << "\n";
477 os << " Class: " << ClassTypes[ClassFlag] << "\n";
478 os << " Free/Busy: " << FreeBusy[FreeBusyFlag] << "\n";
479 if( TimeZoneValid )
480 os << " Time Zone: " << GetTimeZone(TimeZoneCode)->Name << "\n";
483 void Calendar::Dump(std::ostream &os) const
485 ios_format_state state(os);
487 // FIXME - need a "check all data" function that make sure that all
488 // recurrence data is within range. Then call that before using
489 // the data, such as in Build and in Dump.
491 os << "Calendar entry: 0x" << setbase(16) << RecordId
492 << " (" << (unsigned int)RecType << ")\n";
493 DumpSpecialFields(os);
495 // cycle through the type table
496 for( const FieldLink<Calendar> *b = CalendarFieldLinks;
497 b->type != CALFC_END;
498 b++ )
500 if( b->strMember ) {
501 const std::string &s = this->*(b->strMember);
502 if( s.size() )
503 os << " " << b->name << ": " << s << "\n";
505 else if( b->timeMember ) {
506 time_t t = this->*(b->timeMember);
507 if( t > 0 )
508 os << " " << b->name << ": " << ctime(&t);
509 else
510 os << " " << b->name << ": disabled\n";
512 else if( b->addrMember ) {
513 const EmailAddressList &al = this->*(b->addrMember);
514 EmailAddressList::const_iterator lb = al.begin(), le = al.end();
516 for( ; lb != le; ++lb ) {
517 if( !lb->size() )
518 continue;
520 os << " " << b->name << ": " << *lb << "\n";
525 // print recurrence data if available
526 RecurBase::Dump(os);
528 // print any unknowns
529 os << Unknowns;
532 bool Calendar::operator<(const Calendar &other) const
534 if( StartTime < other.StartTime )
535 return true;
536 else if( StartTime > other.StartTime )
537 return false;
539 int cmp = Subject.compare(other.Subject);
540 if( cmp == 0 )
541 cmp = Location.compare(other.Location);
542 return cmp < 0;
546 ///////////////////////////////////////////////////////////////////////////////
547 // Calendar-All class
549 // calendar-all field codes
550 #define CALALLFC_CALENDAR_ID 0x02 // Calendar using (new devices have several calendar)
551 #define CALALLFC_MAIL_ACCOUNT 0x03
552 #define CALALLFC_UNIQUEID 0x05
553 #define CALALLFC_CAL_OBJECT 0x0a
554 #define CALALLFC_END 0xffff
556 void CalendarAll::Clear()
558 Calendar::Clear();
560 MailAccount.clear();
563 const std::vector<FieldHandle<CalendarAll> >& CalendarAll::GetFieldHandles()
565 static std::vector<FieldHandle<CalendarAll> > fhv;
567 if( fhv.size() )
568 return fhv;
570 #undef CONTAINER_OBJECT_NAME
571 #define CONTAINER_OBJECT_NAME fhv
573 #undef RECORD_CLASS_NAME
574 #define RECORD_CLASS_NAME CalendarAll
576 ALL_COMMON_CALENDAR_FIELDS
578 // Calendar:: field, but with a CalendarAll ID
579 FHD(CalendarID, "Calendar ID", CALALLFC_CALENDAR_ID, false);
581 // add the fields specific to CalendarAll
582 FHD(MailAccount, "Mail Account", CALALLFC_MAIL_ACCOUNT, true);
584 // and finally, the RecurBase fields
585 RECUR_BASE_FIELD_HANDLES
587 return fhv;
590 void CalendarAll::ParseHeader(const Data &data, size_t &offset)
592 const unsigned char *b = (const unsigned char*) (data.GetData() + offset);
593 const unsigned char *e = (const unsigned char*) (data.GetData() + data.GetSize());
595 while( (b + COMMON_FIELD_HEADER_SIZE) < e ) {
596 const CommonField *field = (const CommonField *) b;
598 // advance and check size
599 b += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
600 if( b > e ) // if begin==end, we are ok
601 continue;
603 if( !btohs(field->size) ) // if field has no size, something's up
604 continue;
606 // handle special cases
607 if( field->type == CALALLFC_CAL_OBJECT )
609 b -= btohs(field->size);
610 // end of header
611 break;
614 switch( field->type )
616 case CALALLFC_CALENDAR_ID:
617 if( btohs(field->size) == 8 ) {
618 CalendarID = btohll(field->u.uint64);
620 else {
621 throw Error("CalendarAll::ParseField: size data unknown in calendar field");
623 continue;
625 case CALALLFC_MAIL_ACCOUNT:
626 MailAccount = ParseFieldString(field);
627 continue;
629 case CALALLFC_UNIQUEID:
630 if( btohs(field->size) == 4 ) {
631 RecordId = btohl(field->u.uint32);
633 else {
634 throw Error("CalendarAll::ParseHeader: size data unknown in calendar field");
636 continue;
639 // if still not handled, add to the Unknowns list
640 UnknownField uf;
641 uf.type = field->type;
642 uf.data.assign((const char*)field->u.raw, btohs(field->size));
643 Unknowns.push_back(uf);
646 offset += b - (data.GetData() + offset);
649 void CalendarAll::DumpSpecialFields(std::ostream &os) const
651 ios_format_state state(os);
653 Calendar::DumpSpecialFields(os);
654 os << " Mail Account: " << MailAccount << "\n";
657 } // namespace Barry