Merged barry-b1-socket-arch-branch into MAIN.
[barry.git] / src / r_folder.cc
blob3904306373dc1c958d2b06631fc6601fc526fff9
1 ///
2 /// \file r_folder.cc
3 /// Record parsing class for the folders database.
4 ///
6 /*
7 Copyright (C) 2005-2008, 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 <ostream>
30 #include <iomanip>
32 using namespace std;
33 using namespace Barry::Protocol;
35 namespace Barry {
37 ///////////////////////////////////////////////////////////////////////////////
38 // Folder Class
40 // Folder Field Codes
42 #define FFC_NUMBER 0x0a
43 #define FFC_LEVEL 0x0b
44 #define FFC_NAME 0x0c
45 #define FFC_ADDRESS1 0x0d
46 #define FFC_ADDRESS2 0x0e
47 #define FFC_TYPE 0x0f
48 #define FFC_END 0xffff
50 // Folder Types
51 #define SUBTREE 0x00
52 #define DELETED 0x01
53 #define INBOX 0x02
54 #define OUTBOX 0x03
55 #define SENT 0x04
56 #define OTHER 0x05
57 #define DRAFT 0x0a
59 // Folder Status
60 #define ORPHAN 0x50
61 #define UNFILED 0x51
62 #define FILED 0x52
64 #define INVALID -1
66 #define SEPARATOR 0x2f
67 #define ROOT_SEPARATOR 0x3a
69 FieldLink<Folder> FolderFieldLinks[] = {
70 { FFC_NAME, "FolderName", 0, 0, &Folder::FolderName, 0, 0 },
71 { FFC_END, "End of List", 0, 0, 0, 0, 0 },
74 Folder::Folder()
76 Clear();
80 Folder::~Folder()
84 const unsigned char* Folder::ParseField(const unsigned char *begin,
85 const unsigned char *end)
87 const CommonField *field = (const CommonField *) begin;
89 // advance and check size
90 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
91 if( begin > end ) // if begin==end, we are ok
92 return begin;
94 if( !btohs(field->size) ) // if field has no size, something's up
95 return begin;
97 // cycle through the type table
98 for( FieldLink<Folder> *b = FolderFieldLinks;
99 b->type != FFC_END;
100 b++ )
102 if( b->type == field->type ) {
103 if( b->strMember ) {
104 std::string &s = this->*(b->strMember);
105 s = ParseFieldString(field);
106 return begin; // done!
108 else if( b->timeMember && btohs(field->size) == 4 ) {
109 time_t &t = this->*(b->timeMember);
110 t = min2time(field->u.min1900);
111 return begin;
115 // handle special cases
116 switch( field->type )
118 case FFC_TYPE:
119 FolderType = (FolderTypeEnum)field->u.raw[0];
120 return begin;
121 case FFC_NUMBER:
122 FolderNumber = field->u.raw[0]; // two's complement
123 return begin;
124 case FFC_LEVEL:
125 FolderLevel = field->u.raw[0];
126 return begin;
129 // if still not handled, add to the Unknowns list
130 UnknownField uf;
131 uf.type = field->type;
132 uf.data.assign((const char*)field->u.raw, btohs(field->size));
133 Unknowns.push_back(uf);
135 // return new pointer for next field
136 return begin;
139 void Folder::ParseHeader(const Data &data, size_t &offset)
141 // no header in Folder records
144 void Folder::ParseFields(const Data &data, size_t &offset)
146 const unsigned char *finish = ParseCommonFields(*this,
147 data.GetData() + offset, data.GetData() + data.GetSize());
148 offset += finish - (data.GetData() + offset);
151 void Folder::Clear()
153 FolderName.clear();
154 Unknowns.clear();
155 FolderType = FolderSubtree;
158 void Folder::Dump(std::ostream &os) const
160 static const char *FolderTypeString[] = { "Subtree", "Deleted", "Inbox", "Outbox", "Sent", "Other"};
161 // static const char *FolderStatusString[] = { "Orphan", "Unfiled", "Filed" };
163 os << "Folder Records\n\n";
164 os << "Folder Name: " << FolderName << "\n";
165 os << "Folder Type: ";
166 if( FolderType < FolderDraft )
167 os << FolderTypeString[FolderType] << "\n";
168 else if( FolderType == FolderDraft )
169 os << "Draft\n";
170 else
171 os << "Unknown (" << std::hex << FolderType << ")\n";
172 os << "Folder Number: " << std::dec << FolderNumber << "\n";
173 os << "Folder Level: " << std::dec << FolderLevel << "\n";
174 os << "\n";
175 os << Unknowns;
176 os << "\n\n";
179 } // namespace Barry