Bumped copyright dates for 2013
[barry.git] / src / r_folder.cc
blob8aacd7bc1bad280e123617e366f64f93e815ba18
1 ///
2 /// \file r_folder.cc
3 /// Record parsing class for the folders database.
4 ///
6 /*
7 Copyright (C) 2005-2013, 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 "i18n.h"
24 #include "r_folder.h"
25 #include "record-internal.h"
26 #include "protostructs.h"
27 #include "data.h"
28 #include "time.h"
29 #include "debug.h"
30 #include "iconv.h"
31 #include <iostream>
32 #include <sstream>
33 #include <iomanip>
34 #include "ios_state.h"
36 using namespace std;
37 using namespace Barry::Protocol;
39 namespace Barry {
42 ///////////////////////////////////////////////////////////////////////////////
43 // Folder Class, static members
46 // Note! These functions currently only pass the same values through.
47 // In actuality, these are technically two different values:
48 // one on the raw protocol side, and the other part of the
49 // guaranteed Barry API. If the Blackberry ever changes the
50 // meanings for these codes, do the translation here.
53 Folder::FolderType Folder::TypeProto2Rec(uint8_t t)
55 return (FolderType)t;
58 uint8_t Folder::TypeRec2Proto(FolderType t)
60 return t;
65 ///////////////////////////////////////////////////////////////////////////////
66 // Folder Class
68 // Folder Field Codes
70 #define FFC_NUMBER 0x0a
71 #define FFC_LEVEL 0x0b
72 #define FFC_NAME 0x0c
73 #define FFC_ADDRESS1 0x0d
74 #define FFC_ADDRESS2 0x0e
75 #define FFC_TYPE 0x0f
76 #define FFC_END 0xffff
78 #define INVALID -1
80 #define SEPARATOR 0x2f
81 #define ROOT_SEPARATOR 0x3a
83 static FieldLink<Folder> FolderFieldLinks[] = {
84 { FFC_NAME, N_("FolderName"), 0, 0, &Folder::Name, 0, 0, 0, 0, true },
85 { FFC_END, N_("End of List"), 0, 0, 0, 0, 0, 0, 0, false },
88 Folder::Folder()
90 Clear();
94 Folder::~Folder()
98 const unsigned char* Folder::ParseField(const unsigned char *begin,
99 const unsigned char *end,
100 const IConverter *ic)
102 const CommonField *field = (const CommonField *) begin;
104 // advance and check size
105 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
106 if( begin > end ) // if begin==end, we are ok
107 return begin;
109 if( !btohs(field->size) ) // if field has no size, something's up
110 return begin;
112 // cycle through the type table
113 for( FieldLink<Folder> *b = FolderFieldLinks;
114 b->type != FFC_END;
115 b++ )
117 if( b->type == field->type ) {
118 if( b->strMember ) {
119 std::string &s = this->*(b->strMember);
120 s = ParseFieldString(field);
121 if( b->iconvNeeded && ic )
122 s = ic->FromBB(s);
123 return begin; // done!
125 else if( b->timeMember && btohs(field->size) == 4 ) {
126 TimeT &t = this->*(b->timeMember);
127 t.Time= min2time(field->u.min1900);
128 return begin;
132 // handle special cases
133 switch( field->type )
135 case FFC_TYPE:
136 Type = TypeProto2Rec(field->u.raw[0]);
137 return begin;
138 case FFC_NUMBER:
139 Number = field->u.raw[0]; // two's complement
140 return begin;
141 case FFC_LEVEL:
142 Level = field->u.raw[0];
143 return begin;
146 // if still not handled, add to the Unknowns list
147 UnknownField uf;
148 uf.type = field->type;
149 uf.data.assign((const char*)field->u.raw, btohs(field->size));
150 Unknowns.push_back(uf);
152 // return new pointer for next field
153 return begin;
156 void Folder::ParseHeader(const Data &data, size_t &offset)
158 // no header in Folder records
161 void Folder::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
163 const unsigned char *finish = ParseCommonFields(*this,
164 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
165 offset += finish - (data.GetData() + offset);
168 void Folder::Validate() const
172 void Folder::BuildHeader(Data &data, size_t &offset) const
174 // not yet implemented
177 void Folder::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
179 // not yet implemented
182 void Folder::Clear()
184 RecType = GetDefaultRecType();
185 RecordId = 0;
187 Name.clear();
188 Number = 0;
189 Level = 0;
191 Type = FolderSubtree;
193 Unknowns.clear();
196 const FieldHandle<Folder>::ListT& Folder::GetFieldHandles()
198 static FieldHandle<Folder>::ListT fhv;
200 if( fhv.size() )
201 return fhv;
203 #undef CONTAINER_OBJECT_NAME
204 #define CONTAINER_OBJECT_NAME fhv
206 #undef RECORD_CLASS_NAME
207 #define RECORD_CLASS_NAME Folder
209 FHP(RecType, _("Record Type Code"));
210 FHP(RecordId, _("Unique Record ID"));
212 FHD(Name, _("Folder Name"), FFC_NAME, true);
213 FHD(Number, _("Order Number"), FFC_NUMBER, false);
214 FHD(Level, _("Folder Level"), FFC_LEVEL, false);
216 FHE(ft, FolderType, Type, _("Folder Type"));
217 FHE_CONST(ft, FolderSubtree, _("Subtree"));
218 FHE_CONST(ft, FolderDeleted, _("Deleted"));
219 FHE_CONST(ft, FolderInbox, _("Inbox"));
220 FHE_CONST(ft, FolderOutbox, _("Outbox"));
221 FHE_CONST(ft, FolderSent, _("Sent"));
222 FHE_CONST(ft, FolderOther, _("Other"));
223 FHE_CONST(ft, FolderDraft, _("Draft"));
225 // Not yet implemented
226 // FHE(fst, FolderStatusType, ..., _("Folder Status"));
227 // FHE_CONST(fst, FolderOrphan, _("Orphan"));
228 // FHE_CONST(fst, FolderUnfiled, _("Unfiled"));
229 // FHE_CONST(fst, FolderFiled, _("Filed"));
231 FHP(Unknowns, _("Unknown Fields"));
233 return fhv;
236 std::string Folder::GetDescription() const
238 ostringstream oss;
239 oss << Name << " (" << Level << ")";
240 return oss.str();
243 void Folder::Dump(std::ostream &os) const
245 ios_format_state state(os);
247 static const char *FolderTypeString[] = {
248 N_("Subtree"),
249 N_("Deleted"),
250 N_("Inbox"),
251 N_("Outbox"),
252 N_("Sent"),
253 N_("Other")
255 // static const char *FolderStatusString[] = { "Orphan", "Unfiled", "Filed" };
257 os << _("Folder Records\n\n");
258 os << _("Folder Name: ") << Name << "\n";
259 os << _("Folder Type: ");
260 if( Type < FolderDraft )
261 os << gettext( FolderTypeString[Type] ) << "\n";
262 else if( Type == FolderDraft )
263 os << _("Draft\n");
264 else
265 os << _("Unknown") << " (" << std::hex << Type << ")\n";
266 os << _("Folder Number: ") << std::dec << Number << "\n";
267 os << _("Folder Level: ") << std::dec << Level << "\n";
268 os << "\n";
269 os << Unknowns;
270 os << "\n\n";
273 } // namespace Barry