2
* Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
3
* Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
* Additional permission under GNU GPL version 3 section 7:
21
* If you modify this program, or any covered work, by linking or
22
* combining it with the OpenSSL project's OpenSSL library (or a
23
* modified version of that library), containing parts covered by the
24
* terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25
* grants you additional permission to convey the resulting work.
26
* Corresponding Source for a non-source form of such a combination
27
* shall include the source code for the parts of OpenSSL used as well
28
* as that of the covered work.
31
#include "yamlemitter.h"
33
#include "../global.h"
38
YamlEmitter::YamlEmitter (const char *file) : filename (file), isFirstAccount (true)
43
YamlEmitter::~YamlEmitter()
48
void YamlEmitter::open()
51
fd = fopen (filename.c_str(), "w");
54
throw YamlEmitterException ("Could not open file descriptor");
56
if (!yaml_emitter_initialize (&emitter))
57
throw YamlEmitterException ("Could not initialize emitter");
59
// Allows unescaped unicode characters
60
yaml_emitter_set_unicode (&emitter, 1);
62
yaml_emitter_set_output_file (&emitter, fd);
64
if (!yaml_document_initialize (&document, NULL, NULL, NULL, 0, 0))
65
throw YamlEmitterException ("Could not initialize yaml document while saving configuration");
67
// Init the main configuration mapping
68
if ( (topLevelMapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
69
throw YamlEmitterException ("Could not create top level mapping");
72
void YamlEmitter::close()
74
// yaml_emitter_delete(&emitter);
77
throw YamlEmitterException ("File descriptor not valid");
81
throw YamlEmitterException ("Error closing file descriptor");
84
_debug ("Config: Configuration file closed successfully");
88
void YamlEmitter::read() {}
90
void YamlEmitter::write()
95
void YamlEmitter::serializeData()
97
// Document object is destroyed once its content is emitted
98
if (!yaml_emitter_dump (&emitter, &document))
99
throw YamlEmitterException ("Error while emitting configuration yaml document");
103
void YamlEmitter::serializeAccount (MappingNode *map)
106
std::string accountstr ("accounts");
108
int accountid, accountmapping;
110
_debug ("YamlEmitter: Serialize account");
112
if (map->getType() != MAPPING)
113
throw YamlEmitterException ("Node type is not a mapping while writing account");
115
if (isFirstAccount) {
116
// accountSequence need to be static outside this scope since reused each time an account is written
117
if ( (accountid = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) accountstr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
118
throw YamlEmitterException ("Could not add preference scalar to document");
120
if ( (accountSequence = yaml_document_add_sequence (&document, NULL, YAML_BLOCK_SEQUENCE_STYLE)) == 0)
121
throw YamlEmitterException ("Could not add sequence to document");
123
if (!yaml_document_append_mapping_pair (&document, topLevelMapping, accountid, accountSequence))
124
throw YamlEmitterException ("Could not add mapping pair to top level mapping");
126
isFirstAccount = false;
129
if ( (accountmapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
130
throw YamlEmitterException ("Could not add account mapping to document");
132
if (!yaml_document_append_sequence_item (&document, accountSequence, accountmapping))
133
throw YamlEmitterException ("Could not append account mapping to sequence");
135
Mapping *internalmap = map->getMapping();
136
Mapping::iterator iter = internalmap->begin();
138
while (iter != internalmap->end()) {
139
addMappingItem (accountmapping, iter->first, iter->second);
145
void YamlEmitter::serializePreference (MappingNode *map)
147
std::string preferencestr ("preferences");
149
int preferenceid, preferencemapping;
151
_debug ("YamlEmitter: Serialize preference");
153
if (map->getType() != MAPPING)
154
throw YamlEmitterException ("Node type is not a mapping while writing preferences");
156
if ( (preferenceid = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) preferencestr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
157
throw YamlEmitterException ("Could not add scalar to document");
159
if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
160
throw YamlEmitterException ("Could not add mapping to document");
162
if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
163
throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
165
Mapping *internalmap = map->getMapping();
166
Mapping::iterator iter = internalmap->begin();
168
while (iter != internalmap->end()) {
169
addMappingItem (preferencemapping, iter->first, iter->second);
175
void YamlEmitter::serializeVoipPreference (MappingNode *map)
177
std::string preferencestr ("voipPreferences");
179
int preferenceid, preferencemapping;
181
_debug ("YamlEmitter: Serialize voip preference");
183
if (map->getType() != MAPPING)
184
throw YamlEmitterException ("Node type is not a mapping while writing preferences");
186
if ( (preferenceid = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) preferencestr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
187
throw YamlEmitterException ("Could not add scalar to document");
189
if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
190
throw YamlEmitterException ("Could not add mapping to document");
192
if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
193
throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
195
Mapping *internalmap = map->getMapping();
196
Mapping::iterator iter = internalmap->begin();
198
while (iter != internalmap->end()) {
199
addMappingItem (preferencemapping, iter->first, iter->second);
205
void YamlEmitter::serializeAddressbookPreference (MappingNode *map)
207
std::string preferencestr ("addressbook");
209
int preferenceid, preferencemapping;
211
_debug ("YamlEmitter: Serialize addressbook preferences");
213
if (map->getType() != MAPPING)
214
throw YamlEmitterException ("Node type is not a mapping while writing preferences");
216
if ( (preferenceid = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) preferencestr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
217
throw YamlEmitterException ("Could not add scalar to document");
219
if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
220
throw YamlEmitterException ("Could not add mapping to document");
222
if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
223
throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
225
Mapping *internalmap = map->getMapping();
226
Mapping::iterator iter = internalmap->begin();
228
while (iter != internalmap->end()) {
229
addMappingItem (preferencemapping, iter->first, iter->second);
235
void YamlEmitter::serializeHooksPreference (MappingNode *map)
237
std::string preferencestr ("hooks");
239
int preferenceid, preferencemapping;
241
_debug ("YamlEmitter: Serialize hooks preferences");
243
if (map->getType() != MAPPING)
244
throw YamlEmitterException ("Node type is not a mapping while writing preferences");
246
if ( (preferenceid = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) preferencestr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
247
throw YamlEmitterException ("Could not add scalar to document");
249
if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
250
throw YamlEmitterException ("Could not add mapping to document");
252
if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
253
throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
255
Mapping *internalmap = map->getMapping();
256
Mapping::iterator iter = internalmap->begin();
258
while (iter != internalmap->end()) {
259
addMappingItem (preferencemapping, iter->first, iter->second);
266
void YamlEmitter::serializeAudioPreference (MappingNode *map)
268
std::string preferencestr ("audio");
270
int preferenceid, preferencemapping;
272
_debug ("YamlEmitter: Serialize hooks preferences");
274
if (map->getType() != MAPPING)
275
throw YamlEmitterException ("Node type is not a mapping while writing preferences");
277
if ( (preferenceid = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) preferencestr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
278
throw YamlEmitterException ("Could not add scalar to document");
280
if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
281
throw YamlEmitterException ("Could not add mapping to document");
283
if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
284
throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
286
Mapping *internalmap = map->getMapping();
287
Mapping::iterator iter = internalmap->begin();
289
while (iter != internalmap->end()) {
290
addMappingItem (preferencemapping, iter->first, iter->second);
297
void YamlEmitter::serializeShortcutPreference (MappingNode *map)
299
std::string preferencestr ("shortcuts");
301
int preferenceid, preferencemapping;
303
_debug ("YamlEmitter: Serialize shortcuts preferences");
305
if (map->getType() != MAPPING)
306
throw YamlEmitterException ("Node type is not a mapping while writing preferences");
308
if ( (preferenceid = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) preferencestr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
309
throw YamlEmitterException ("Could not add scalar to document");
311
if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
312
throw YamlEmitterException ("Could not add mapping to document");
314
if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
315
throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
317
Mapping *internalmap = map->getMapping();
318
Mapping::iterator iter = internalmap->begin();
320
while (iter != internalmap->end()) {
321
addMappingItem (preferencemapping, iter->first, iter->second);
328
void YamlEmitter::addMappingItem (int mappingid, Key key, YamlNode *node)
331
if (node->getType() == SCALAR) {
335
ScalarNode *sclr = (ScalarNode *) node;
337
if ( (temp1 = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) key.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
338
throw YamlEmitterException ("Could not add scalar to document");
340
if ( (temp2 = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) sclr->getValue().c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
341
throw YamlEmitterException ("Could not add scalar to document");
343
if (!yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2))
344
throw YamlEmitterException ("Could not append mapping pair to mapping");
346
} else if (node->getType() == MAPPING) {
350
if ( (temp1 = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) key.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
351
throw YamlEmitterException ("Could not add scalar to document");
353
if ( (temp2 = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
354
throw YamlEmitterException ("Could not add scalar to document");
356
if (!yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2))
357
throw YamlEmitterException ("Could not add mapping pair to mapping");
359
MappingNode *map = (MappingNode *) node;
360
Mapping *internalmap = map->getMapping();
361
Mapping::iterator iter = internalmap->begin();
363
while (iter != internalmap->end()) {
364
addMappingItem (temp2, iter->first, iter->second);
368
throw YamlEmitterException ("Unknown node type while adding mapping node");