desktop: modified record list so it is displayed in sorted order
[barry.git] / src / r_servicebook.cc
blob25cad7251c1b34b9b2e9bdc1c39afaa765371125
1 ///
2 /// \file r_servicebook.cc
3 /// Blackberry database record parser class for
4 /// Service Book records.
5 ///
7 /*
8 Copyright (C) 2005-2012, 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 #include "r_servicebook.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 <stdexcept>
36 #include "ios_state.h"
38 #define __DEBUG_MODE__
39 #include "debug.h"
41 using namespace std;
42 using namespace Barry::Protocol;
44 namespace Barry {
46 ///////////////////////////////////////////////////////////////////////////////
47 // ServiceBookConfig class
49 // service book packed field codes
50 #define SBFCC_END 0xffff
52 static FieldLink<ServiceBookConfig> ServiceBookConfigFieldLinks[] = {
53 // { SBFC_DSID, "DSID", 0, 0, &ServiceBook::DSID, 0, 0 },
54 { SBFCC_END, "End of List",0, 0, 0, 0, 0 }
57 ServiceBookConfig::ServiceBookConfig()
58 : Format(0)
60 Clear();
63 ServiceBookConfig::~ServiceBookConfig()
67 const unsigned char* ServiceBookConfig::ParseField(const unsigned char *begin,
68 const unsigned char *end,
69 const IConverter *ic)
71 const void *raw;
72 uint16_t size, type;
74 switch( Format )
76 case 0x01:
77 case 0x02:
79 const PackedField_02 *field = (const PackedField_02 *) begin;
80 raw = field->raw;
81 size = field->size;
82 type = field->type;
83 begin += PACKED_FIELD_02_HEADER_SIZE + size;
85 break;
87 case 0x10:
89 const PackedField_10 *field = (const PackedField_10 *) begin;
90 raw = field->raw;
91 size = field->size;
92 type = field->type;
93 begin += PACKED_FIELD_10_HEADER_SIZE + size;
95 break;
97 default:
98 eout("------> Unknown packed field format: 0x" << std::hex <<
99 (unsigned int) Format);
100 throw BadPackedFormat(Format);
101 return begin + 1;
105 // check size
106 if( begin > end ) // if begin==end, we are ok
107 return begin;
109 if( !size ) // if field has no size, something's up
110 return begin;
112 // cycle through the type table
113 for( FieldLink<ServiceBookConfig> *b = ServiceBookConfigFieldLinks;
114 b->type != SBFCC_END;
115 b++ )
117 if( b->type == type ) {
118 if( b->strMember ) {
119 std::string &s = this->*(b->strMember);
120 s = ParseFieldString(raw, size-1);
121 return begin; // done!
127 // handle special cases
128 switch( type )
133 // if still not handled, add to the Unknowns list
134 UnknownField uf;
135 uf.type = type;
136 uf.data.assign((const char*)raw, size);
137 Unknowns.push_back(uf);
139 // return new pointer for next field
140 return begin;
143 void ServiceBookConfig::ParseHeader(const Data &data, size_t &offset)
145 MAKE_RECORD(const Barry::Protocol::ServiceBookConfigField, sbc, data, offset);
146 offset += SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE;
147 if( data.GetSize() >= offset ) { // size check!
148 Format = sbc->format;
152 void ServiceBookConfig::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
154 const unsigned char *finish = ParseCommonFields(*this,
155 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
156 offset += finish - (data.GetData() + offset);
159 void ServiceBookConfig::BuildHeader(Data &data, size_t &offset) const
161 // make sure there is enough space
162 data.GetBuffer(offset + SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE);
164 MAKE_RECORD(Barry::Protocol::ServiceBookConfigField, sbc, data, offset);
165 sbc->format = Format;
167 offset += SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE;
171 // BuildFields
173 /// Build fields part of record
175 void ServiceBookConfig::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
177 throw std::logic_error("ServiceBookConfig::Build not yet implemented");
180 void ServiceBookConfig::Clear()
182 Format = 0;
183 Unknowns.clear();
186 void ServiceBookConfig::Dump(std::ostream &os) const
188 ios_format_state state(os);
190 os << " ServiceBookConfig Format: " << setbase(16) << (uint16_t)Format << "\n";
192 // cycle through the type table
193 for( const FieldLink<ServiceBookConfig> *b = ServiceBookConfigFieldLinks;
194 b->type != SBFCC_END;
195 b++ )
197 if( b->strMember ) {
198 const std::string &s = this->*(b->strMember);
199 if( s.size() )
200 os << " " << b->name << ": " << s << "\n";
202 else if( b->timeMember ) {
203 time_t t = this->*(b->timeMember);
204 if( t > 0 )
205 os << " " << b->name << ": " << ctime(&t);
209 // print any unknowns
210 os << Unknowns;
211 os << " ------------------- End of Config Field\n";
215 ///////////////////////////////////////////////////////////////////////////////
216 // ServiceBook class
218 // service book field codes
219 #define SBFC_OLD_NAME 0x01
220 #define SBFC_HIDDEN_NAME 0x02
221 #define SBFC_NAME 0x03
222 #define SBFC_OLD_UNIQUE_ID 0x06
223 #define SBFC_UNIQUE_ID 0x07
224 #define SBFC_CONTENT_ID 0x08
225 #define SBFC_CONFIG 0x09
226 #define SBFC_OLD_DESC 0x32
227 #define SBFC_DESCRIPTION 0x0f
228 #define SBFC_DSID 0xa1
229 #define SBFC_BES_DOMAIN 0xa2
230 #define SBFC_USER_ID 0xa3
231 #define SBFC_END 0xffff
233 // private data class, containing internal structures
234 class ServiceBookData
236 public:
237 FieldLink<ServiceBook> *m_typeSet;
238 ServiceBookData(FieldLink<ServiceBook> *typeSet) : m_typeSet(typeSet) {}
241 // The Old/New tables contain the same fields, but use different
242 // type codes. Keeping them separate yet linked makes it possible
243 // to convert between old and new type codes, while hopefully keeping
244 // things generic.
245 static FieldLink<ServiceBook> ServiceBookOldFieldLinks[] = {
246 { SBFC_OLD_NAME, "Old Name", 0, 0, &ServiceBook::Name, 0, 0, 0, 0, true },
247 { SBFC_OLD_DESC, "Old Desc", 0, 0, &ServiceBook::Description, 0, 0, 0, 0, true },
248 { SBFC_OLD_UNIQUE_ID, "Old UniqueId", 0, 0, &ServiceBook::UniqueId, 0, 0, 0, 0, false },
249 { SBFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
252 static FieldLink<ServiceBook> ServiceBookNewFieldLinks[] = {
253 { SBFC_NAME, "Name", 0, 0, &ServiceBook::Name, 0, 0, 0, 0, true },
254 { SBFC_DESCRIPTION, "Description", 0, 0, &ServiceBook::Description, 0, 0, 0, 0, true },
255 { SBFC_UNIQUE_ID, "UniqueId", 0, 0, &ServiceBook::UniqueId, 0, 0, 0, 0, false },
256 { SBFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
259 // This table holds all
260 static FieldLink<ServiceBook> ServiceBookFieldLinks[] = {
261 { SBFC_HIDDEN_NAME, "Hidden Name",0, 0, &ServiceBook::HiddenName, 0, 0, 0, 0, true },
262 { SBFC_DSID, "DSID", 0, 0, &ServiceBook::DSID, 0, 0, 0, 0, false },
263 { SBFC_CONTENT_ID, "ContentId", 0, 0, &ServiceBook::ContentId, 0, 0, 0, 0, false },
264 { SBFC_BES_DOMAIN, "BES Domain", 0, 0, &ServiceBook::BesDomain, 0, 0, 0, 0, false },
265 { SBFC_END, "End of List",0, 0, 0, 0, 0, 0, 0, false }
268 // Array of conflicting tables only
269 static FieldLink<ServiceBook> *ServiceBookLinkTable[] = {
270 ServiceBookOldFieldLinks,
271 ServiceBookNewFieldLinks,
275 #define FIELDLINK_END 0xffff
277 template <class RecordT>
278 FieldLink<RecordT>* ParseFieldByTable(RecordT *rec,
279 const CommonField *field,
280 const IConverter *ic,
281 FieldLink<RecordT> *links)
283 // cycle through the type table
284 for( FieldLink<RecordT> *b = links; b->type != FIELDLINK_END; b++ ) {
285 if( b->type == field->type ) {
286 if( b->strMember ) {
287 std::string &s = rec->*(b->strMember);
288 if( s.size() ) {
289 dout(RecordT::GetDBName() << ": field '" << b->name << "' already has data (" << s << "). Overwriting.");
291 s = ParseFieldString(field);
292 if( b->iconvNeeded && ic )
293 s = ic->FromBB(s);
294 return links;
296 else if( b->timeMember && btohs(field->size) == 4 ) {
297 time_t &t = rec->*(b->timeMember);
298 t = min2time(field->u.min1900);
299 return links;
303 return 0;
306 template <class RecordT>
307 FieldLink<RecordT>* ParseFieldByTable(RecordT *rec,
308 const CommonField *field,
309 const IConverter *ic,
310 FieldLink<RecordT> **b)
312 for( ; *b; b++ ) {
313 FieldLink<RecordT> *link =
314 ParseFieldByTable<RecordT>(rec, field, ic, *b);
315 if( link )
316 return link;
318 return 0;
321 ServiceBook::ServiceBook()
322 : m_data( new ServiceBookData(ServiceBookOldFieldLinks) )
323 , RecordId(0)
325 Clear();
328 ServiceBook::~ServiceBook()
332 const unsigned char* ServiceBook::ParseField(const unsigned char *begin,
333 const unsigned char *end,
334 const IConverter *ic)
336 const CommonField *field = (const CommonField *) begin;
338 // advance and check size
339 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
340 if( begin > end ) // if begin==end, we are ok
341 return begin;
343 if( !btohs(field->size) ) // if field has no size, something's up
344 return begin;
346 // cycle through the type tables
347 FieldLink<ServiceBook> *typeSet =
348 ParseFieldByTable(this, field, ic, ServiceBookLinkTable);
349 if( typeSet ) {
350 if( m_data->m_typeSet && m_data->m_typeSet != typeSet ) {
351 dout("ServiceBook record has a mix of old and new field types.");
353 m_data->m_typeSet = typeSet;
354 return begin;
356 else {
357 if( ParseFieldByTable(this, field, ic, ServiceBookFieldLinks) )
358 return begin; // done!
361 // handle special cases
362 switch( field->type )
364 case SBFC_CONFIG:
365 try {
366 Data config((const void *)field->u.raw, btohs(field->size));
367 size_t offset = 0;
368 Config.ParseHeader(config, offset);
369 Config.ParseFields(config, offset);
370 return begin; // success
372 catch( BadPackedFormat & ) {
373 // break here so unprocessed raw packet is still
374 // visible in dump
375 break;
379 // if still not handled, add to the Unknowns list
380 UnknownField uf;
381 uf.type = field->type;
382 uf.data.assign((const char*)field->u.raw, btohs(field->size));
383 Unknowns.push_back(uf);
385 // return new pointer for next field
386 return begin;
389 void ServiceBook::ParseHeader(const Data &data, size_t &offset)
391 // no header in this record (?)
394 void ServiceBook::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
396 const unsigned char *finish = ParseCommonFields(*this,
397 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
398 offset += finish - (data.GetData() + offset);
401 void ServiceBook::BuildHeader(Data &data, size_t &offset) const
403 // no header in this record (?)
407 // BuildFields
409 /// Build fields part of record
411 void ServiceBook::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
413 throw std::logic_error("ServiceBook::BuildFields not yet implemented");
416 void ServiceBook::Clear()
418 m_data->m_typeSet = ServiceBookOldFieldLinks;
419 Unknowns.clear();
420 Config.Clear();
423 std::string ServiceBook::GetDescription() const
425 return Name;
428 inline void FormatStr(std::ostream &os, const char *name, const std::string &str)
430 ios_format_state state(os);
432 if( str.size() ) {
433 os << " " << setw(20) << name;
434 os << ": " << str << "\n";
438 void ServiceBook::Dump(std::ostream &os) const
440 ios_format_state state(os);
442 os.setf(ios::left);
443 os.fill(' ');
445 os << "ServiceBook entry: 0x" << setbase(16) << RecordId
446 << " (" << (unsigned int)RecType << ")\n";
448 FormatStr(os, "Name", Name);
449 FormatStr(os, "Hidden Name", HiddenName);
450 FormatStr(os, "Description", Description);
451 FormatStr(os, "DSID", DSID);
452 FormatStr(os, "Unique ID", UniqueId);
453 FormatStr(os, "Content ID", ContentId);
454 FormatStr(os, "(BES) Domain", BesDomain);
456 os << Config;
458 // print any unknowns
459 os << Unknowns;
462 bool ServiceBook::operator<(const ServiceBook &other) const
464 int cmp = BesDomain.compare(other.BesDomain);
465 if( cmp == 0 )
466 cmp = DSID.compare(other.DSID);
467 if( cmp == 0 )
468 cmp = Name.compare(other.Name);
469 if( cmp == 0 )
470 cmp = UniqueId.compare(other.UniqueId);
471 return cmp < 0;
474 } // namespace Barry