lib: added new common record function: GetDescription()
[barry.git] / src / r_folder.cc
blob84bc5c88be5cfb81a8e9d21415ca53a195ac58fa
1 ///
2 /// \file r_folder.cc
3 /// Record parsing class for the folders database.
4 ///
6 /*
7 Copyright (C) 2005-2011, 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>
34 using namespace std;
35 using namespace Barry::Protocol;
37 namespace Barry {
40 ///////////////////////////////////////////////////////////////////////////////
41 // Folder Class, static members
44 // Note! These functions currently only pass the same values through.
45 // In actuality, these are technically two different values:
46 // one on the raw protocol side, and the other part of the
47 // guaranteed Barry API. If the Blackberry ever changes the
48 // meanings for these codes, do the translation here.
51 Folder::FolderType Folder::TypeProto2Rec(uint8_t t)
53 return (FolderType)t;
56 uint8_t Folder::TypeRec2Proto(FolderType t)
58 return t;
63 ///////////////////////////////////////////////////////////////////////////////
64 // Folder Class
66 // Folder Field Codes
68 #define FFC_NUMBER 0x0a
69 #define FFC_LEVEL 0x0b
70 #define FFC_NAME 0x0c
71 #define FFC_ADDRESS1 0x0d
72 #define FFC_ADDRESS2 0x0e
73 #define FFC_TYPE 0x0f
74 #define FFC_END 0xffff
76 #define INVALID -1
78 #define SEPARATOR 0x2f
79 #define ROOT_SEPARATOR 0x3a
81 static FieldLink<Folder> FolderFieldLinks[] = {
82 { FFC_NAME, "FolderName", 0, 0, &Folder::Name, 0, 0, 0, 0, true },
83 { FFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false },
86 Folder::Folder()
88 Clear();
92 Folder::~Folder()
96 const unsigned char* Folder::ParseField(const unsigned char *begin,
97 const unsigned char *end,
98 const IConverter *ic)
100 const CommonField *field = (const CommonField *) begin;
102 // advance and check size
103 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
104 if( begin > end ) // if begin==end, we are ok
105 return begin;
107 if( !btohs(field->size) ) // if field has no size, something's up
108 return begin;
110 // cycle through the type table
111 for( FieldLink<Folder> *b = FolderFieldLinks;
112 b->type != FFC_END;
113 b++ )
115 if( b->type == field->type ) {
116 if( b->strMember ) {
117 std::string &s = this->*(b->strMember);
118 s = ParseFieldString(field);
119 if( b->iconvNeeded && ic )
120 s = ic->FromBB(s);
121 return begin; // done!
123 else if( b->timeMember && btohs(field->size) == 4 ) {
124 time_t &t = this->*(b->timeMember);
125 t = min2time(field->u.min1900);
126 return begin;
130 // handle special cases
131 switch( field->type )
133 case FFC_TYPE:
134 Type = TypeProto2Rec(field->u.raw[0]);
135 return begin;
136 case FFC_NUMBER:
137 Number = field->u.raw[0]; // two's complement
138 return begin;
139 case FFC_LEVEL:
140 Level = field->u.raw[0];
141 return begin;
144 // if still not handled, add to the Unknowns list
145 UnknownField uf;
146 uf.type = field->type;
147 uf.data.assign((const char*)field->u.raw, btohs(field->size));
148 Unknowns.push_back(uf);
150 // return new pointer for next field
151 return begin;
154 void Folder::ParseHeader(const Data &data, size_t &offset)
156 // no header in Folder records
159 void Folder::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
161 const unsigned char *finish = ParseCommonFields(*this,
162 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
163 offset += finish - (data.GetData() + offset);
166 void Folder::Clear()
168 RecType = GetDefaultRecType();
169 RecordId = 0;
171 Name.clear();
172 Number = 0;
173 Level = 0;
175 Type = FolderSubtree;
177 Unknowns.clear();
180 std::string Folder::GetDescription() const
182 ostringstream oss;
183 oss << Name << " (" << Level << ")";
184 return oss.str();
187 void Folder::Dump(std::ostream &os) const
189 static const char *FolderTypeString[] = { "Subtree", "Deleted", "Inbox", "Outbox", "Sent", "Other"};
190 // static const char *FolderStatusString[] = { "Orphan", "Unfiled", "Filed" };
192 os << "Folder Records\n\n";
193 os << "Folder Name: " << Name << "\n";
194 os << "Folder Type: ";
195 if( Type < FolderDraft )
196 os << FolderTypeString[Type] << "\n";
197 else if( Type == FolderDraft )
198 os << "Draft\n";
199 else
200 os << "Unknown (" << std::hex << Type << ")\n";
201 os << "Folder Number: " << std::dec << Number << "\n";
202 os << "Folder Level: " << std::dec << Level << "\n";
203 os << "\n";
204 os << Unknowns;
205 os << "\n\n";
208 } // namespace Barry