Release tarball for barry-0.9
[barry.git] / src / record.h
blobcf7ae90d28c3747ce21c97d0e76ce4ef313ce809
1 ///
2 /// \file record.h
3 /// Blackberry database record classes. Help translate data
4 /// from data packets to useful structurs, and back.
5 /// This header provides the common types and classes
6 /// used by the general record parser classes in the
7 /// r_*.h files. Only application-safe API stuff goes in
8 /// here. Internal library types go in record-internal.h
9 ///
12 Copyright (C) 2005-2007, Net Direct Inc. (http://www.netdirect.ca/)
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 See the GNU General Public License in the COPYING file at the
24 root directory of this project for more details.
27 #ifndef __BARRY_RECORD_H__
28 #define __BARRY_RECORD_H__
30 #include <iosfwd>
31 #include <string>
32 #include <vector>
33 #include <map>
34 #include <stdint.h>
36 // forward declarations
37 namespace Barry { class Data; }
39 namespace Barry {
42 // NOTE: All classes here must be container-safe! Perhaps add sorting
43 // operators in the future.
48 struct CommandTableCommand
50 unsigned int Code;
51 std::string Name;
54 class CommandTable
56 public:
57 typedef CommandTableCommand Command;
58 typedef std::vector<Command> CommandArrayType;
60 CommandArrayType Commands;
62 private:
63 const unsigned char* ParseField(const unsigned char *begin,
64 const unsigned char *end);
65 public:
66 CommandTable();
67 ~CommandTable();
69 void Parse(const Data &data, size_t offset);
70 void Clear();
72 // returns 0 if unable to find command name, which is safe, since
73 // 0 is a special command that shouldn't be in the table anyway
74 unsigned int GetCommand(const std::string &name) const;
76 void Dump(std::ostream &os) const;
79 inline std::ostream& operator<< (std::ostream &os, const CommandTable &command) {
80 command.Dump(os);
81 return os;
86 struct RecordStateTableState
88 unsigned int Index;
89 uint32_t RecordId;
90 bool Dirty;
91 unsigned int RecType;
92 std::string Unknown2;
95 class RecordStateTable
97 public:
98 typedef RecordStateTableState State;
99 typedef unsigned int IndexType;
100 typedef std::map<IndexType, State> StateMapType;
102 StateMapType StateMap;
104 private:
105 mutable IndexType m_LastNewRecordId;
107 private:
108 const unsigned char* ParseField(const unsigned char *begin,
109 const unsigned char *end);
111 public:
112 RecordStateTable();
113 ~RecordStateTable();
115 void Parse(const Data &data);
116 void Clear();
118 bool GetIndex(uint32_t RecordId, IndexType *pFoundIndex = 0) const;
119 uint32_t MakeNewRecordId() const;
121 void Dump(std::ostream &os) const;
124 inline std::ostream& operator<< (std::ostream &os, const RecordStateTable &rst) {
125 rst.Dump(os);
126 return os;
131 struct DatabaseItem
133 unsigned int Number;
134 unsigned int RecordCount;
135 std::string Name;
138 class DatabaseDatabase
140 public:
141 typedef DatabaseItem Database;
142 typedef std::vector<Database> DatabaseArrayType;
144 DatabaseArrayType Databases;
146 private:
147 template <class RecordType, class FieldType>
148 void ParseRec(const RecordType &rec, const unsigned char *end);
150 template <class FieldType>
151 const unsigned char* ParseField(const unsigned char *begin,
152 const unsigned char *end);
154 public:
155 DatabaseDatabase();
156 ~DatabaseDatabase();
158 void Parse(const Data &data);
159 void Clear();
161 // returns true on success, and fills target
162 bool GetDBNumber(const std::string &name, unsigned int &number) const;
163 bool GetDBName(unsigned int number, std::string &name) const;
165 void Dump(std::ostream &os) const;
168 inline std::ostream& operator<<(std::ostream &os, const DatabaseDatabase &dbdb) {
169 dbdb.Dump(os);
170 return os;
173 struct UnknownField
175 uint8_t type;
176 std::string data;
178 std::ostream& operator<< (std::ostream &os, const std::vector<UnknownField> &unknowns);
180 struct EmailAddress
182 std::string Name;
183 std::string Email;
185 void clear()
187 Name.clear();
188 Email.clear();
191 std::ostream& operator<<(std::ostream &os, const EmailAddress &msga);
193 struct PostalAddress
195 std::string
196 Address1,
197 Address2,
198 Address3,
199 City,
200 Province,
201 PostalCode,
202 Country;
204 std::string GetLabel() const;
205 void Clear();
207 bool HasData() const { return Address1.size() || Address2.size() ||
208 Address3.size() || City.size() || Province.size() ||
209 PostalCode.size() || Country.size(); }
211 std::ostream& operator<<(std::ostream &os, const PostalAddress &msga);
214 /// \addtogroup RecordParserClasses
215 /// Parser and data storage classes. These classes take a
216 /// Database Database record and convert them into C++ objects.
217 /// Each of these classes are safe to be used in standard
218 /// containers, and are meant to be used in conjunction with the
219 /// RecordParser<> template when calling Controller::LoadDatabase().
220 /// @{
221 /// @}
223 } // namespace Barry
225 // Include all parser classes, to make it easy for the application to use.
226 #include "r_calendar.h"
227 #include "r_contact.h"
228 #include "r_memo.h"
229 #include "r_message.h"
230 #include "r_servicebook.h"
231 #include "r_task.h"
232 #include "r_pin_message.h"
233 #include "r_saved_message.h"
234 #include "r_folder.h"
236 #endif