Moved platform specific detail into tools/platform.h
[barry.git] / src / r_folder.cc
blob3efea27adb8c996c967d4ec806649085e483e3f5
1 ///
2 /// \file r_folder.cc
3 /// Record parsing class for the folders database.
4 ///
6 /*
7 Copyright (C) 2005-2010, Net Direct Inc. (http://www.netdirect.ca/)
8 Copyright (C) 2007, Brian Edginton
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_folder.h"
24 #include "record-internal.h"
25 #include "protostructs.h"
26 #include "data.h"
27 #include "time.h"
28 #include "debug.h"
29 #include "iconv.h"
30 #include <ostream>
31 #include <iomanip>
33 using namespace std;
34 using namespace Barry::Protocol;
36 namespace Barry {
39 ///////////////////////////////////////////////////////////////////////////////
40 // Folder Class, static members
43 // Note! These functions currently only pass the same values through.
44 // In actuality, these are technically two different values:
45 // one on the raw protocol side, and the other part of the
46 // guaranteed Barry API. If the Blackberry ever changes the
47 // meanings for these codes, do the translation here.
50 Folder::FolderType Folder::TypeProto2Rec(uint8_t t)
52 return (FolderType)t;
55 uint8_t Folder::TypeRec2Proto(FolderType t)
57 return t;
62 ///////////////////////////////////////////////////////////////////////////////
63 // Folder Class
65 // Folder Field Codes
67 #define FFC_NUMBER 0x0a
68 #define FFC_LEVEL 0x0b
69 #define FFC_NAME 0x0c
70 #define FFC_ADDRESS1 0x0d
71 #define FFC_ADDRESS2 0x0e
72 #define FFC_TYPE 0x0f
73 #define FFC_END 0xffff
75 #define INVALID -1
77 #define SEPARATOR 0x2f
78 #define ROOT_SEPARATOR 0x3a
80 static FieldLink<Folder> FolderFieldLinks[] = {
81 { FFC_NAME, "FolderName", 0, 0, &Folder::Name, 0, 0, 0, 0, true },
82 { FFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
85 Folder::Folder()
87 Clear();
91 Folder::~Folder()
95 const unsigned char* Folder::ParseField(const unsigned char *begin,
96 const unsigned char *end,
97 const IConverter *ic)
99 const CommonField *field = (const CommonField *) begin;
101 // advance and check size
102 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
103 if( begin > end ) // if begin==end, we are ok
104 return begin;
106 if( !btohs(field->size) ) // if field has no size, something's up
107 return begin;
109 // cycle through the type table
110 for( FieldLink<Folder> *b = FolderFieldLinks;
111 b->type != FFC_END;
112 b++ )
114 if( b->type == field->type ) {
115 if( b->strMember ) {
116 std::string &s = this->*(b->strMember);
117 s = ParseFieldString(field);
118 if( b->iconvNeeded && ic )
119 s = ic->FromBB(s);
120 return begin; // done!
122 else if( b->timeMember && btohs(field->size) == 4 ) {
123 time_t &t = this->*(b->timeMember);
124 t = min2time(field->u.min1900);
125 return begin;
129 // handle special cases
130 switch( field->type )
132 case FFC_TYPE:
133 Type = TypeProto2Rec(field->u.raw[0]);
134 return begin;
135 case FFC_NUMBER:
136 Number = field->u.raw[0]; // two's complement
137 return begin;
138 case FFC_LEVEL:
139 Level = field->u.raw[0];
140 return begin;
143 // if still not handled, add to the Unknowns list
144 UnknownField uf;
145 uf.type = field->type;
146 uf.data.assign((const char*)field->u.raw, btohs(field->size));
147 Unknowns.push_back(uf);
149 // return new pointer for next field
150 return begin;
153 void Folder::ParseHeader(const Data &data, size_t &offset)
155 // no header in Folder records
158 void Folder::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
160 const unsigned char *finish = ParseCommonFields(*this,
161 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
162 offset += finish - (data.GetData() + offset);
165 void Folder::Clear()
167 Name.clear();
168 Unknowns.clear();
169 Type = FolderSubtree;
172 void Folder::Dump(std::ostream &os) const
174 static const char *FolderTypeString[] = { "Subtree", "Deleted", "Inbox", "Outbox", "Sent", "Other"};
175 // static const char *FolderStatusString[] = { "Orphan", "Unfiled", "Filed" };
177 os << "Folder Records\n\n";
178 os << "Folder Name: " << Name << "\n";
179 os << "Folder Type: ";
180 if( Type < FolderDraft )
181 os << FolderTypeString[Type] << "\n";
182 else if( Type == FolderDraft )
183 os << "Draft\n";
184 else
185 os << "Unknown (" << std::hex << Type << ")\n";
186 os << "Folder Number: " << std::dec << Number << "\n";
187 os << "Folder Level: " << std::dec << Level << "\n";
188 os << "\n";
189 os << Unknowns;
190 os << "\n\n";
193 } // namespace Barry