maint: updated 32 and 64bit build scripts for Ubuntu support
[barry/progweb.git] / src / r_folder.cc
blob9267b782b4444454c7ad76b597ca18d5a2aaaf8a
1 ///
2 /// \file r_folder.cc
3 /// Record parsing class for the folders database.
4 ///
6 /*
7 Copyright (C) 2005-2012, 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 <iostream>
31 #include <sstream>
32 #include <iomanip>
33 #include "ios_state.h"
35 using namespace std;
36 using namespace Barry::Protocol;
38 namespace Barry {
41 ///////////////////////////////////////////////////////////////////////////////
42 // Folder Class, static members
45 // Note! These functions currently only pass the same values through.
46 // In actuality, these are technically two different values:
47 // one on the raw protocol side, and the other part of the
48 // guaranteed Barry API. If the Blackberry ever changes the
49 // meanings for these codes, do the translation here.
52 Folder::FolderType Folder::TypeProto2Rec(uint8_t t)
54 return (FolderType)t;
57 uint8_t Folder::TypeRec2Proto(FolderType t)
59 return t;
64 ///////////////////////////////////////////////////////////////////////////////
65 // Folder Class
67 // Folder Field Codes
69 #define FFC_NUMBER 0x0a
70 #define FFC_LEVEL 0x0b
71 #define FFC_NAME 0x0c
72 #define FFC_ADDRESS1 0x0d
73 #define FFC_ADDRESS2 0x0e
74 #define FFC_TYPE 0x0f
75 #define FFC_END 0xffff
77 #define INVALID -1
79 #define SEPARATOR 0x2f
80 #define ROOT_SEPARATOR 0x3a
82 static FieldLink<Folder> FolderFieldLinks[] = {
83 { FFC_NAME, "FolderName", 0, 0, &Folder::Name, 0, 0, 0, 0, true },
84 { FFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
87 Folder::Folder()
89 Clear();
93 Folder::~Folder()
97 const unsigned char* Folder::ParseField(const unsigned char *begin,
98 const unsigned char *end,
99 const IConverter *ic)
101 const CommonField *field = (const CommonField *) begin;
103 // advance and check size
104 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
105 if( begin > end ) // if begin==end, we are ok
106 return begin;
108 if( !btohs(field->size) ) // if field has no size, something's up
109 return begin;
111 // cycle through the type table
112 for( FieldLink<Folder> *b = FolderFieldLinks;
113 b->type != FFC_END;
114 b++ )
116 if( b->type == field->type ) {
117 if( b->strMember ) {
118 std::string &s = this->*(b->strMember);
119 s = ParseFieldString(field);
120 if( b->iconvNeeded && ic )
121 s = ic->FromBB(s);
122 return begin; // done!
124 else if( b->timeMember && btohs(field->size) == 4 ) {
125 TimeT &t = this->*(b->timeMember);
126 t.Time= min2time(field->u.min1900);
127 return begin;
131 // handle special cases
132 switch( field->type )
134 case FFC_TYPE:
135 Type = TypeProto2Rec(field->u.raw[0]);
136 return begin;
137 case FFC_NUMBER:
138 Number = field->u.raw[0]; // two's complement
139 return begin;
140 case FFC_LEVEL:
141 Level = field->u.raw[0];
142 return begin;
145 // if still not handled, add to the Unknowns list
146 UnknownField uf;
147 uf.type = field->type;
148 uf.data.assign((const char*)field->u.raw, btohs(field->size));
149 Unknowns.push_back(uf);
151 // return new pointer for next field
152 return begin;
155 void Folder::ParseHeader(const Data &data, size_t &offset)
157 // no header in Folder records
160 void Folder::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
162 const unsigned char *finish = ParseCommonFields(*this,
163 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
164 offset += finish - (data.GetData() + offset);
167 void Folder::Validate() const
171 void Folder::BuildHeader(Data &data, size_t &offset) const
173 // not yet implemented
176 void Folder::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
178 // not yet implemented
181 void Folder::Clear()
183 RecType = GetDefaultRecType();
184 RecordId = 0;
186 Name.clear();
187 Number = 0;
188 Level = 0;
190 Type = FolderSubtree;
192 Unknowns.clear();
195 const FieldHandle<Folder>::ListT& Folder::GetFieldHandles()
197 static FieldHandle<Folder>::ListT fhv;
199 if( fhv.size() )
200 return fhv;
202 #undef CONTAINER_OBJECT_NAME
203 #define CONTAINER_OBJECT_NAME fhv
205 #undef RECORD_CLASS_NAME
206 #define RECORD_CLASS_NAME Folder
208 FHP(RecType, "Record Type Code");
209 FHP(RecordId, "Unique Record ID");
211 FHD(Name, "Folder Name", FFC_NAME, true);
212 FHD(Number, "Order Number", FFC_NUMBER, false);
213 FHD(Level, "Folder Level", FFC_LEVEL, false);
215 FHE(ft, FolderType, Type, "Folder Type");
216 FHE_CONST(ft, FolderSubtree, "Subtree");
217 FHE_CONST(ft, FolderDeleted, "Deleted");
218 FHE_CONST(ft, FolderInbox, "Inbox");
219 FHE_CONST(ft, FolderOutbox, "Outbox");
220 FHE_CONST(ft, FolderSent, "Sent");
221 FHE_CONST(ft, FolderOther, "Other");
222 FHE_CONST(ft, FolderDraft, "Draft");
224 // Not yet implemented
225 // FHE(fst, FolderStatusType, ..., "Folder Status");
226 // FHE_CONST(fst, FolderOrphan, "Orphan");
227 // FHE_CONST(fst, FolderUnfiled, "Unfiled");
228 // FHE_CONST(fst, FolderFiled, "Filed");
230 FHP(Unknowns, "Unknown Fields");
232 return fhv;
235 std::string Folder::GetDescription() const
237 ostringstream oss;
238 oss << Name << " (" << Level << ")";
239 return oss.str();
242 void Folder::Dump(std::ostream &os) const
244 ios_format_state state(os);
246 static const char *FolderTypeString[] = { "Subtree", "Deleted", "Inbox", "Outbox", "Sent", "Other"};
247 // static const char *FolderStatusString[] = { "Orphan", "Unfiled", "Filed" };
249 os << "Folder Records\n\n";
250 os << "Folder Name: " << Name << "\n";
251 os << "Folder Type: ";
252 if( Type < FolderDraft )
253 os << FolderTypeString[Type] << "\n";
254 else if( Type == FolderDraft )
255 os << "Draft\n";
256 else
257 os << "Unknown (" << std::hex << Type << ")\n";
258 os << "Folder Number: " << std::dec << Number << "\n";
259 os << "Folder Level: " << std::dec << Level << "\n";
260 os << "\n";
261 os << Unknowns;
262 os << "\n\n";
265 } // namespace Barry