3 /// Blackberry database record classes. Help translate data
4 /// from data packets to useful structurs, and back.
8 Copyright (C) 2005-2007, Net Direct Inc. (http://www.netdirect.ca/)
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 #ifndef __BARRY_RECORD_H__
24 #define __BARRY_RECORD_H__
32 // forward declarations
33 namespace Barry
{ class Data
; }
38 // NOTE: All classes here must be container-safe! Perhaps add sorting
39 // operators in the future.
53 typedef std::vector
<Command
> CommandArrayType
;
55 CommandArrayType Commands
;
58 const unsigned char* ParseField(const unsigned char *begin
,
59 const unsigned char *end
);
64 void Parse(const Data
&data
, size_t offset
);
67 // returns 0 if unable to find command name, which is safe, since
68 // 0 is a special command that shouldn't be in the table anyway
69 unsigned int GetCommand(const std::string
&name
) const;
71 void Dump(std::ostream
&os
) const;
74 inline std::ostream
& operator<< (std::ostream
&os
, const CommandTable
&command
) {
81 class RecordStateTable
93 typedef unsigned int IndexType
;
94 typedef std::map
<IndexType
, State
> StateMapType
;
96 StateMapType StateMap
;
99 mutable IndexType m_LastNewRecordId
;
102 const unsigned char* ParseField(const unsigned char *begin
,
103 const unsigned char *end
);
109 void Parse(const Data
&data
);
112 bool GetIndex(uint32_t RecordId
, IndexType
*pFoundIndex
= 0) const;
113 uint32_t MakeNewRecordId() const;
115 void Dump(std::ostream
&os
) const;
118 inline std::ostream
& operator<< (std::ostream
&os
, const RecordStateTable
&rst
) {
125 class DatabaseDatabase
131 unsigned int RecordCount
;
135 typedef std::vector
<Database
> DatabaseArrayType
;
137 DatabaseArrayType Databases
;
140 template <class RecordType
, class FieldType
>
141 void ParseRec(const RecordType
&rec
, const unsigned char *end
);
143 template <class FieldType
>
144 const unsigned char* ParseField(const unsigned char *begin
,
145 const unsigned char *end
);
151 void Parse(const Data
&data
);
154 // returns true on success, and fills target
155 bool GetDBNumber(const std::string
&name
, unsigned int &number
) const;
156 bool GetDBName(unsigned int number
, std::string
&name
) const;
158 void Dump(std::ostream
&os
) const;
161 inline std::ostream
& operator<<(std::ostream
&os
, const DatabaseDatabase
&dbdb
) {
173 std::ostream
& operator<< (std::ostream
&os
, const std::vector
<UnknownField
> &unknowns
);
176 /// \addtogroup RecordParserClasses
177 /// Parser and data storage classes. These classes take a
178 /// Database Database record and convert them into C++ objects.
179 /// Each of these classes are safe to be used in standard
180 /// containers, and are meant to be used in conjunction with the
181 /// RecordParser<> template when calling Controller::LoadDatabase().
192 GroupLink() : Link(0), Unknown(0) {}
193 GroupLink(uint32_t link
, uint16_t unknown
)
194 : Link(link
), Unknown(unknown
)
198 typedef std::vector
<GroupLink
> GroupLinksType
;
199 typedef std::vector
<UnknownField
> UnknownsType
;
201 // contact specific data
216 DefaultCommunicationsMethod
,
228 GroupLinksType GroupLinks
;
229 UnknownsType Unknowns
;
232 bool m_FirstNameSeen
;
236 const unsigned char* ParseField(const unsigned char *begin
,
237 const unsigned char *end
);
243 uint32_t GetID() const { return RecordId
; }
244 std::string
GetPostalAddress() const;
246 // Parser / Builder API (see parser.h / builder.h)
247 uint8_t GetRecType() const { return RecType
; }
248 uint32_t GetUniqueId() const { return RecordId
; }
249 void SetIds(uint8_t Type
, uint32_t Id
) { RecType
= Type
; RecordId
= Id
; }
250 void ParseHeader(const Data
&data
, size_t &offset
);
251 void ParseFields(const Data
&data
, size_t &offset
);
252 void BuildHeader(Data
&data
, size_t &offset
) const;
253 void BuildFields(Data
&data
, size_t &offset
) const;
255 void Clear(); // erase everything
257 void Dump(std::ostream
&os
) const;
259 // sorting - put group links at the end
260 bool operator<(const Contact
&other
) const {
261 return GroupLinks
.size() == 0 && other
.GroupLinks
.size() > 0;
262 // // testing - put group links at the top
263 // return GroupLinks.size() > 0 && other.GroupLinks.size() == 0;
267 static const char * GetDBName() { return "Address Book"; }
268 static uint8_t GetDefaultRecType() { return 0; }
271 static void SplitName(const std::string
&full
, std::string
&first
, std::string
&last
);
274 inline std::ostream
& operator<< (std::ostream
&os
, const Contact
&contact
) {
294 std::vector
<UnknownField
> Unknowns
;
297 const unsigned char* ParseField(const unsigned char *begin
,
298 const unsigned char *end
);
304 // Parser / Builder API (see parser.h / builder.h)
305 uint8_t GetRecType() const;
306 uint32_t GetUniqueId() const; // empty API, not required by protocol
307 void SetIds(uint8_t Type
, uint32_t Id
); // empty API, not required by protocol
308 void ParseHeader(const Data
&data
, size_t &offset
);
309 void ParseFields(const Data
&data
, size_t &offset
);
310 void BuildHeader(Data
&data
, size_t &offset
) const;
311 void BuildFields(Data
&data
, size_t &offset
) const;
315 void Dump(std::ostream
&os
) const;
318 bool operator<(const Message
&other
) const { return Subject
< other
.Subject
; }
321 static const char * GetDBName() { return "Messages"; }
322 static uint8_t GetDefaultRecType() { return 0; }
325 inline std::ostream
& operator<<(std::ostream
&os
, const Message
&msg
) {
330 std::ostream
& operator<<(std::ostream
&os
, const Message::Address
&msga
);
336 typedef std::vector
<UnknownField
> UnknownsType
;
345 std::string Location
;
346 time_t NotificationTime
;
353 /// Note: interval can be used on all of these recurring types to
354 /// make it happen "every other time" or more, etc.
356 enum RecurringCodeType
{
357 Day
= 1, //< eg. every day
359 MonthByDate
= 3, //< eg. every month on the 12th
361 MonthByDay
= 4, //< eg. every month on 3rd Wed
362 //< set: DayOfWeek and WeekOfMonth
363 YearByDate
= 5, //< eg. every year on March 5
364 //< set: DayOfMonth and MonthOfYear
365 YearByDay
= 6, //< eg. every year on 3rd Wed of Jan
366 //< set: DayOfWeek, WeekOfMonth, and
368 Week
= 12 //< eg. every week on Mon and Fri
373 RecurringCodeType RecurringType
;
374 unsigned short Interval
; // must be >= 1
375 time_t RecurringEndTime
; // only pertains if Recurring is true
376 // sets the date and time when
377 // recurrence of this appointment
378 // should no longer occur
379 // If a perpetual appointment, this
380 // is 0xFFFFFFFF in the low level data
381 // Instead, set the following flag.
382 bool Perpetual
; // if true, this will always recur
383 unsigned short TimeZoneCode
; // the time zone originally used
384 // for the recurrence data...
385 // seems to have little use, but
386 // set to your current time zone
389 unsigned short // recurring details, depending on type
394 unsigned char WeekDays
; // bitmask, bit 0 = sunday
396 // FIXME - put these somewhere usable by both C and C++
397 #define CAL_WD_SUN 0x01
398 #define CAL_WD_MON 0x02
399 #define CAL_WD_TUE 0x04
400 #define CAL_WD_WED 0x08
401 #define CAL_WD_THU 0x10
402 #define CAL_WD_FRI 0x20
403 #define CAL_WD_SAT 0x40
406 UnknownsType Unknowns
;
409 const unsigned char* ParseField(const unsigned char *begin
,
410 const unsigned char *end
);
411 void ParseRecurrenceData(const void *data
);
412 void BuildRecurrenceData(void *data
);
418 // Parser / Builder API (see parser.h / builder.h)
419 uint8_t GetRecType() const { return RecType
; }
420 uint32_t GetUniqueId() const { return RecordId
; }
421 void SetIds(uint8_t Type
, uint32_t Id
) { RecType
= Type
; RecordId
= Id
; }
422 void ParseHeader(const Data
&data
, size_t &offset
);
423 void ParseFields(const Data
&data
, size_t &offset
);
424 void BuildHeader(Data
&data
, size_t &offset
) const;
425 void BuildFields(Data
&data
, size_t &offset
) const;
429 void Dump(std::ostream
&os
) const;
432 bool operator<(const Calendar
&other
) const { return StartTime
< other
.StartTime
; }
435 static const char * GetDBName() { return "Calendar"; }
436 static uint8_t GetDefaultRecType() { return 5; } // or 0?
439 inline std::ostream
& operator<<(std::ostream
&os
, const Calendar
&msg
) {
445 // This is a packed field, which is a group of fields packed in
446 // variable length records inside one larger field of a normal record.
447 class ServiceBookConfig
450 typedef std::vector
<UnknownField
> UnknownsType
;
454 UnknownsType Unknowns
;
457 const unsigned char* ParseField(const unsigned char *begin
,
458 const unsigned char *end
);
462 ~ServiceBookConfig();
464 // Parser / Builder API (see parser.h / builder.h)
465 void ParseHeader(const Data
&data
, size_t &offset
);
466 void ParseFields(const Data
&data
, size_t &offset
);
467 void BuildHeader(Data
&data
, size_t &offset
) const;
468 void BuildFields(Data
&data
, size_t &offset
) const;
472 void Dump(std::ostream
&os
) const;
475 inline std::ostream
& operator<<(std::ostream
&os
, const ServiceBookConfig
&msg
) {
483 int NameType
, DescType
, UniqueIdType
;
486 typedef std::vector
<UnknownField
> UnknownsType
;
491 std::string HiddenName
;
492 std::string Description
;
494 std::string BesDomain
;
495 std::string UniqueId
;
496 std::string ContentId
;
497 ServiceBookConfig Config
;
499 UnknownsType Unknowns
;
502 const unsigned char* ParseField(const unsigned char *begin
,
503 const unsigned char *end
);
509 // Parser / Builder API (see parser.h / builder.h)
510 uint8_t GetRecType() const { return RecType
; }
511 uint32_t GetUniqueId() const { return RecordId
; }
512 void SetIds(uint8_t Type
, uint32_t Id
) { RecType
= Type
; RecordId
= Id
; }
513 void ParseHeader(const Data
&data
, size_t &offset
);
514 void ParseFields(const Data
&data
, size_t &offset
);
515 void BuildHeader(Data
&data
, size_t &offset
) const;
516 void BuildFields(Data
&data
, size_t &offset
) const;
520 void Dump(std::ostream
&os
) const;
523 bool operator<(const ServiceBook
&other
) const { return RecordId
< RecordId
; }
526 static const char * GetDBName() { return "Service Book"; }
527 static uint8_t GetDefaultRecType() { return 0; }
530 inline std::ostream
& operator<<(std::ostream
&os
, const ServiceBook
&msg
) {