desktop: CalEditDlg: fixed dialog title bar
[barry.git] / src / r_servicebook.cc
blob22a302d3ff57a552c0766021be947997b225ac90
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 dout("------> 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::Validate() const
163 void ServiceBookConfig::BuildHeader(Data &data, size_t &offset) const
165 // make sure there is enough space
166 data.GetBuffer(offset + SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE);
168 MAKE_RECORD(Barry::Protocol::ServiceBookConfigField, sbc, data, offset);
169 sbc->format = Format;
171 offset += SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE;
175 // BuildFields
177 /// Build fields part of record
179 void ServiceBookConfig::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
181 throw std::logic_error("ServiceBookConfig::Build not yet implemented");
184 void ServiceBookConfig::Clear()
186 Format = 0;
187 Unknowns.clear();
190 void ServiceBookConfig::Dump(std::ostream &os) const
192 ios_format_state state(os);
194 os << " ServiceBookConfig Format: " << setbase(16) << (uint16_t)Format << "\n";
196 // cycle through the type table
197 for( const FieldLink<ServiceBookConfig> *b = ServiceBookConfigFieldLinks;
198 b->type != SBFCC_END;
199 b++ )
201 if( b->strMember ) {
202 const std::string &s = this->*(b->strMember);
203 if( s.size() )
204 os << " " << b->name << ": " << s << "\n";
206 else if( b->timeMember ) {
207 TimeT t = this->*(b->timeMember);
208 if( t.Time> 0 )
209 os << " " << b->name << ": " << t << "\n";
213 // print any unknowns
214 os << Unknowns;
215 os << " ------------------- End of Config Field\n";
219 ///////////////////////////////////////////////////////////////////////////////
220 // ServiceBook class
222 // service book field codes
223 #define SBFC_OLD_NAME 0x01
224 #define SBFC_HIDDEN_NAME 0x02
225 #define SBFC_NAME 0x03
226 #define SBFC_OLD_UNIQUE_ID 0x06
227 #define SBFC_UNIQUE_ID 0x07
228 #define SBFC_CONTENT_ID 0x08
229 #define SBFC_CONFIG 0x09
230 #define SBFC_OLD_DESC 0x32
231 #define SBFC_DESCRIPTION 0x0f
232 #define SBFC_DSID 0xa1
233 #define SBFC_BES_DOMAIN 0xa2
234 #define SBFC_USER_ID 0xa3
235 #define SBFC_END 0xffff
237 // private data class, containing internal structures
238 class ServiceBookData
240 public:
241 FieldLink<ServiceBook> *m_typeSet;
242 ServiceBookData(FieldLink<ServiceBook> *typeSet) : m_typeSet(typeSet) {}
245 // The Old/New tables contain the same fields, but use different
246 // type codes. Keeping them separate yet linked makes it possible
247 // to convert between old and new type codes, while hopefully keeping
248 // things generic.
249 static FieldLink<ServiceBook> ServiceBookOldFieldLinks[] = {
250 { SBFC_OLD_NAME, "Old Name", 0, 0, &ServiceBook::Name, 0, 0, 0, 0, true },
251 { SBFC_OLD_DESC, "Old Desc", 0, 0, &ServiceBook::Description, 0, 0, 0, 0, true },
252 { SBFC_OLD_UNIQUE_ID, "Old UniqueId", 0, 0, &ServiceBook::UniqueId, 0, 0, 0, 0, false },
253 { SBFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
256 static FieldLink<ServiceBook> ServiceBookNewFieldLinks[] = {
257 { SBFC_NAME, "Name", 0, 0, &ServiceBook::Name, 0, 0, 0, 0, true },
258 { SBFC_DESCRIPTION, "Description", 0, 0, &ServiceBook::Description, 0, 0, 0, 0, true },
259 { SBFC_UNIQUE_ID, "UniqueId", 0, 0, &ServiceBook::UniqueId, 0, 0, 0, 0, false },
260 { SBFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
263 // This table holds all
264 static FieldLink<ServiceBook> ServiceBookFieldLinks[] = {
265 { SBFC_HIDDEN_NAME, "Hidden Name",0, 0, &ServiceBook::HiddenName, 0, 0, 0, 0, true },
266 { SBFC_DSID, "DSID", 0, 0, &ServiceBook::DSID, 0, 0, 0, 0, false },
267 { SBFC_CONTENT_ID, "ContentId", 0, 0, &ServiceBook::ContentId, 0, 0, 0, 0, false },
268 { SBFC_BES_DOMAIN, "BES Domain", 0, 0, &ServiceBook::BesDomain, 0, 0, 0, 0, false },
269 { SBFC_END, "End of List",0, 0, 0, 0, 0, 0, 0, false }
272 // Array of conflicting tables only
273 static FieldLink<ServiceBook> *ServiceBookLinkTable[] = {
274 ServiceBookOldFieldLinks,
275 ServiceBookNewFieldLinks,
279 #define FIELDLINK_END 0xffff
281 template <class RecordT>
282 FieldLink<RecordT>* ParseFieldByTable(RecordT *rec,
283 const CommonField *field,
284 const IConverter *ic,
285 FieldLink<RecordT> *links)
287 // cycle through the type table
288 for( FieldLink<RecordT> *b = links; b->type != FIELDLINK_END; b++ ) {
289 if( b->type == field->type ) {
290 if( b->strMember ) {
291 std::string &s = rec->*(b->strMember);
292 if( s.size() ) {
293 dout(RecordT::GetDBName() << ": field '" << b->name << "' already has data (" << s << "). Overwriting.");
295 s = ParseFieldString(field);
296 if( b->iconvNeeded && ic )
297 s = ic->FromBB(s);
298 return links;
300 else if( b->timeMember && btohs(field->size) == 4 ) {
301 TimeT &t = rec->*(b->timeMember);
302 t.Time = min2time(field->u.min1900);
303 return links;
307 return 0;
310 template <class RecordT>
311 FieldLink<RecordT>* ParseFieldByTable(RecordT *rec,
312 const CommonField *field,
313 const IConverter *ic,
314 FieldLink<RecordT> **b)
316 for( ; *b; b++ ) {
317 FieldLink<RecordT> *link =
318 ParseFieldByTable<RecordT>(rec, field, ic, *b);
319 if( link )
320 return link;
322 return 0;
325 ServiceBook::ServiceBook()
326 : m_data( new ServiceBookData(ServiceBookOldFieldLinks) )
327 , RecordId(0)
329 Clear();
332 ServiceBook::~ServiceBook()
336 const unsigned char* ServiceBook::ParseField(const unsigned char *begin,
337 const unsigned char *end,
338 const IConverter *ic)
340 const CommonField *field = (const CommonField *) begin;
342 // advance and check size
343 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
344 if( begin > end ) // if begin==end, we are ok
345 return begin;
347 if( !btohs(field->size) ) // if field has no size, something's up
348 return begin;
350 // cycle through the type tables
351 FieldLink<ServiceBook> *typeSet =
352 ParseFieldByTable(this, field, ic, ServiceBookLinkTable);
353 if( typeSet ) {
354 if( m_data->m_typeSet && m_data->m_typeSet != typeSet ) {
355 dout("ServiceBook record has a mix of old and new field types.");
357 m_data->m_typeSet = typeSet;
358 return begin;
360 else {
361 if( ParseFieldByTable(this, field, ic, ServiceBookFieldLinks) )
362 return begin; // done!
365 // handle special cases
366 switch( field->type )
368 case SBFC_CONFIG:
369 try {
370 Data config((const void *)field->u.raw, btohs(field->size));
371 size_t offset = 0;
372 Config.ParseHeader(config, offset);
373 Config.ParseFields(config, offset);
374 return begin; // success
376 catch( BadPackedFormat & ) {
377 // break here so unprocessed raw packet is still
378 // visible in dump
379 break;
383 // if still not handled, add to the Unknowns list
384 UnknownField uf;
385 uf.type = field->type;
386 uf.data.assign((const char*)field->u.raw, btohs(field->size));
387 Unknowns.push_back(uf);
389 // return new pointer for next field
390 return begin;
393 void ServiceBook::ParseHeader(const Data &data, size_t &offset)
395 // no header in this record (?)
398 void ServiceBook::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
400 const unsigned char *finish = ParseCommonFields(*this,
401 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
402 offset += finish - (data.GetData() + offset);
405 void ServiceBook::Validate() const
407 Config.Validate();
410 void ServiceBook::BuildHeader(Data &data, size_t &offset) const
412 // no header in this record (?)
416 // BuildFields
418 /// Build fields part of record
420 void ServiceBook::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
422 throw std::logic_error("ServiceBook::BuildFields not yet implemented");
425 void ServiceBook::Clear()
427 m_data->m_typeSet = ServiceBookOldFieldLinks;
428 Unknowns.clear();
429 Config.Clear();
432 const FieldHandle<ServiceBook>::ListT& ServiceBook::GetFieldHandles()
434 static FieldHandle<ServiceBook>::ListT fhv;
436 if( fhv.size() )
437 return fhv;
439 #undef CONTAINER_OBJECT_NAME
440 #define CONTAINER_OBJECT_NAME fhv
442 #undef RECORD_CLASS_NAME
443 #define RECORD_CLASS_NAME ServiceBook
445 FHP(RecType, "Record Type Code");
446 FHP(RecordId, "Unique Record ID");
448 FHP(Name, "Name");
449 FHP(HiddenName, "Hidden Name");
450 FHP(Description, "Description");
451 FHP(DSID, "DSID");
452 FHP(BesDomain, "BES Domain");
453 FHP(UniqueId, "Unique ID");
454 FHP(ContentId, "Content ID");
456 // FIXME - this config is not yet implmented fully... when it is,
457 // will need to add this as a field to FieldHandle<>, and maybe
458 // implement a ServiceBookConfig::GetFieldHandles()
459 //ServiceBookConfig Config;
461 FHP(Unknowns, "Unknown Fields");
463 return fhv;
466 std::string ServiceBook::GetDescription() const
468 return Name;
471 inline void FormatStr(std::ostream &os, const char *name, const std::string &str)
473 ios_format_state state(os);
475 if( str.size() ) {
476 os << " " << setw(20) << name;
477 os << ": " << str << "\n";
481 void ServiceBook::Dump(std::ostream &os) const
483 ios_format_state state(os);
485 os.setf(ios::left);
486 os.fill(' ');
488 os << "ServiceBook entry: 0x" << setbase(16) << RecordId
489 << " (" << (unsigned int)RecType << ")\n";
491 FormatStr(os, "Name", Name);
492 FormatStr(os, "Hidden Name", HiddenName);
493 FormatStr(os, "Description", Description);
494 FormatStr(os, "DSID", DSID);
495 FormatStr(os, "Unique ID", UniqueId);
496 FormatStr(os, "Content ID", ContentId);
497 FormatStr(os, "(BES) Domain", BesDomain);
499 os << Config;
501 // print any unknowns
502 os << Unknowns;
505 bool ServiceBook::operator<(const ServiceBook &other) const
507 int cmp = BesDomain.compare(other.BesDomain);
508 if( cmp == 0 )
509 cmp = DSID.compare(other.DSID);
510 if( cmp == 0 )
511 cmp = Name.compare(other.Name);
512 if( cmp == 0 )
513 cmp = UniqueId.compare(other.UniqueId);
514 return cmp < 0;
517 } // namespace Barry