2 /// \file r_calendar.cc
3 /// Blackberry database record parser class for calendar records.
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"
26 #include "protostructs.h"
37 #include "ios_state.h"
39 #define __DEBUG_MODE__
43 using namespace Barry::Protocol
;
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
)
69 Calendar::ClassFlagType
Calendar::ClassFlagProto2Rec(uint8_t f
)
71 return (ClassFlagType
)f
;
74 uint8_t Calendar::ClassFlagRec2Proto(ClassFlagType f
)
81 ///////////////////////////////////////////////////////////////////////////////
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 }
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
137 if( !btohs(field
->size
) ) // if field has no size, something's up
140 // cycle through the type table
141 for( FieldLink
<Calendar
> *b
= CalendarFieldLinks
;
142 b
->type
!= CALFC_END
;
145 if( b
->type
== field
->type
) {
147 std::string
&s
= this->*(b
->strMember
);
148 s
= ParseFieldString(field
);
149 if( b
->iconvNeeded
&& ic
)
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
);
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
));
170 // assign first string, using null terminator
171 // letting std::string add it for us if it
173 a
.Email
= dual
.c_str();
175 // assign second string, using first size
177 a
.Name
= dual
.c_str() + a
.Email
.size() + 1;
179 // if the address is non-empty, add to list
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
);
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
206 case '*': // recurring appointment
211 throw Error("Calendar::ParseField: unknown appointment type");
215 case CALFC_ALLDAYEVENT_FLAG
:
216 AllDayEvent
= field
->u
.raw
[0] == 1;
219 case CALFC_TIMEZONE_CODE
:
220 if( btohs(field
->size
) == 2 ) {
222 TimeZoneCode
= btohs(field
->u
.code
);
223 TimeZoneValid
= true;
226 throw Error("Calendar::ParseField: not enough data in time zone code field");
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]);
237 case CALFC_CALENDAR_ID
:
238 if( btohs(field
->size
) == 8 ) {
239 CalendarID
= btohll(field
->u
.uint64
);
242 throw Error("Calendar::ParseField: size data unknown in calendar field");
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]);
254 // base class handles recurring data
255 if( RecurBase::ParseField(field
->type
, field
->u
.raw
, btohs(field
->size
), ic
) )
258 // if still not handled, add to the Unknowns list
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
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
288 /// Build fields part of record.
290 void Calendar::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
294 // output the type first
295 BuildField(data
, offset
, CALFC_APPT_TYPE_FLAG
, Recurring
? '*' : 'a');
297 // output all day event flag only if set
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
;
307 const std::string
&s
= this->*(b
->strMember
);
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
);
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
327 std::string Name
= lb
->Name
,
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
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
351 CalendarRecurrenceDataField recur
;
352 BuildRecurrenceData(StartTime
, &recur
);
353 BuildField(data
, offset
, RecurBase::RecurringFieldType(),
354 &recur
, CALENDAR_RECURRENCE_DATA_FIELD_SIZE
);
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
384 RecType
= GetDefaultRecType();
391 NotificationTime
= StartTime
= EndTime
= 0;
399 CalendarID
= btohll((uint64_t) -1);
401 TimeZoneCode
= GetTimeZoneCode(0, 0); // default to GMT
402 TimeZoneValid
= false;
407 const std::vector
<FieldHandle
<Calendar
> >& Calendar::GetFieldHandles()
409 static std::vector
<FieldHandle
<Calendar
> > 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
463 std::string
Calendar::GetDescription() const
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";
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
;
501 const std::string
&s
= this->*(b
->strMember
);
503 os
<< " " << b
->name
<< ": " << s
<< "\n";
505 else if( b
->timeMember
) {
506 time_t t
= this->*(b
->timeMember
);
508 os
<< " " << b
->name
<< ": " << ctime(&t
);
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
) {
520 os
<< " " << b
->name
<< ": " << *lb
<< "\n";
525 // print recurrence data if available
528 // print any unknowns
532 bool Calendar::operator<(const Calendar
&other
) const
534 if( StartTime
< other
.StartTime
)
536 else if( StartTime
> other
.StartTime
)
539 int cmp
= Subject
.compare(other
.Subject
);
541 cmp
= Location
.compare(other
.Location
);
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()
563 const std::vector
<FieldHandle
<CalendarAll
> >& CalendarAll::GetFieldHandles()
565 static std::vector
<FieldHandle
<CalendarAll
> > 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
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
603 if( !btohs(field
->size
) ) // if field has no size, something's up
606 // handle special cases
607 if( field
->type
== CALALLFC_CAL_OBJECT
)
609 b
-= btohs(field
->size
);
614 switch( field
->type
)
616 case CALALLFC_CALENDAR_ID
:
617 if( btohs(field
->size
) == 8 ) {
618 CalendarID
= btohll(field
->u
.uint64
);
621 throw Error("CalendarAll::ParseField: size data unknown in calendar field");
625 case CALALLFC_MAIL_ACCOUNT
:
626 MailAccount
= ParseFieldString(field
);
629 case CALALLFC_UNIQUEID
:
630 if( btohs(field
->size
) == 4 ) {
631 RecordId
= btohl(field
->u
.uint32
);
634 throw Error("CalendarAll::ParseHeader: size data unknown in calendar field");
639 // if still not handled, add to the Unknowns list
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";