64
64
yaml_emitter_set_output_file (&emitter, fd);
66
if (!yaml_document_initialize (&document, NULL, NULL, NULL, 0, 0))
66
if (yaml_document_initialize (&document, NULL, NULL, NULL, 0, 0) == 0) {
67
67
throw YamlEmitterException ("Could not initialize yaml document while saving configuration");
69
70
// Init the main configuration mapping
70
if ( (topLevelMapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
71
if ( (topLevelMapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0) {
71
72
throw YamlEmitterException ("Could not create top level mapping");
74
void YamlEmitter::close()
76
void YamlEmitter::close() throw(YamlEmitterException)
76
78
_debug ("YamlEmitter: Close");
85
87
throw YamlEmitterException ("Error closing file descriptor");
88
_debug ("Config: Configuration file closed successfully");
92
void YamlEmitter::read() {}
94
void YamlEmitter::write()
99
void YamlEmitter::serializeData()
90
_debug ("YamlEmitter: Configuration file closed successfully");
94
void YamlEmitter::serializeData() throw(YamlEmitterException)
101
96
// Document object is destroyed once its content is emitted
102
if (!yaml_emitter_dump (&emitter, &document))
97
if (yaml_emitter_dump (&emitter, &document) == 0) {
103
98
throw YamlEmitterException ("Error while emitting configuration yaml document");
107
void YamlEmitter::serializeAccount (MappingNode *map)
102
void YamlEmitter::serializeAccount (MappingNode *map) throw(YamlEmitterException)
110
105
std::string accountstr ("accounts");
112
107
int accountid, accountmapping;
114
_debug ("YamlEmitter: Serialize account");
116
if (map->getType() != MAPPING)
109
if (map->getType() != MAPPING) {
117
110
throw YamlEmitterException ("Node type is not a mapping while writing account");
119
113
if (isFirstAccount) {
114
_debug("YamlEmitter: Create account sequence");
120
116
// accountSequence need to be static outside this scope since reused each time an account is written
121
if ( (accountid = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) accountstr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
117
if ( (accountid = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) accountstr.c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0) {
122
118
throw YamlEmitterException ("Could not add preference scalar to document");
124
if ( (accountSequence = yaml_document_add_sequence (&document, NULL, YAML_BLOCK_SEQUENCE_STYLE)) == 0)
121
if ( (accountSequence = yaml_document_add_sequence (&document, NULL, YAML_BLOCK_SEQUENCE_STYLE)) == 0) {
125
122
throw YamlEmitterException ("Could not add sequence to document");
127
if (!yaml_document_append_mapping_pair (&document, topLevelMapping, accountid, accountSequence))
125
if (yaml_document_append_mapping_pair (&document, topLevelMapping, accountid, accountSequence) == 0) {
128
126
throw YamlEmitterException ("Could not add mapping pair to top level mapping");
130
129
isFirstAccount = false;
133
if ( (accountmapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
132
if ( (accountmapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0) {
134
133
throw YamlEmitterException ("Could not add account mapping to document");
136
if (!yaml_document_append_sequence_item (&document, accountSequence, accountmapping))
136
if (yaml_document_append_sequence_item (&document, accountSequence, accountmapping) == 0) {
137
137
throw YamlEmitterException ("Could not append account mapping to sequence");
139
140
Mapping *internalmap = map->getMapping();
140
141
Mapping::iterator iter = internalmap->begin();
142
while (iter != internalmap->end()) {
143
addMappingItem (accountmapping, iter->first, iter->second);
144
while (iter != internalmap->end()) {
145
addMappingItem (accountmapping, iter->first, iter->second);
149
catch(YamlEmitterException &e) {
150
_error("YamlEmitterException: %s", e.what());
149
void YamlEmitter::serializePreference (MappingNode *map)
155
void YamlEmitter::serializePreference (MappingNode *map) throw(YamlEmitterException)
151
157
std::string preferencestr ("preferences");
153
159
int preferenceid, preferencemapping;
155
_debug ("YamlEmitter: Serialize preference");
157
161
if (map->getType() != MAPPING)
158
162
throw YamlEmitterException ("Node type is not a mapping while writing preferences");
163
167
if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
164
168
throw YamlEmitterException ("Could not add mapping to document");
166
if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
170
if (yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping) == 0)
167
171
throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
169
173
Mapping *internalmap = map->getMapping();
170
174
Mapping::iterator iter = internalmap->begin();
172
while (iter != internalmap->end()) {
173
addMappingItem (preferencemapping, iter->first, iter->second);
177
while (iter != internalmap->end()) {
178
addMappingItem (preferencemapping, iter->first, iter->second);
182
catch(YamlEmitterException &e) {
179
void YamlEmitter::serializeVoipPreference (MappingNode *map)
188
void YamlEmitter::serializeVoipPreference (MappingNode *map) throw(YamlEmitterException)
181
190
std::string preferencestr ("voipPreferences");
183
192
int preferenceid, preferencemapping;
185
_debug ("YamlEmitter: Serialize voip preference");
187
194
if (map->getType() != MAPPING)
188
195
throw YamlEmitterException ("Node type is not a mapping while writing preferences");
193
200
if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
194
201
throw YamlEmitterException ("Could not add mapping to document");
196
if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
203
if (yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping) == 0)
197
204
throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
199
206
Mapping *internalmap = map->getMapping();
200
207
Mapping::iterator iter = internalmap->begin();
202
while (iter != internalmap->end()) {
203
addMappingItem (preferencemapping, iter->first, iter->second);
210
while (iter != internalmap->end()) {
211
addMappingItem (preferencemapping, iter->first, iter->second);
215
catch (YamlEmitterException &e) {
209
void YamlEmitter::serializeAddressbookPreference (MappingNode *map)
221
void YamlEmitter::serializeAddressbookPreference (MappingNode *map) throw(YamlEmitterException)
211
223
std::string preferencestr ("addressbook");
213
225
int preferenceid, preferencemapping;
215
_debug ("YamlEmitter: Serialize addressbook preferences");
217
227
if (map->getType() != MAPPING)
218
228
throw YamlEmitterException ("Node type is not a mapping while writing preferences");
223
233
if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
224
234
throw YamlEmitterException ("Could not add mapping to document");
226
if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
236
if (yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping) == 0)
227
237
throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
229
239
Mapping *internalmap = map->getMapping();
230
240
Mapping::iterator iter = internalmap->begin();
232
while (iter != internalmap->end()) {
233
addMappingItem (preferencemapping, iter->first, iter->second);
243
while (iter != internalmap->end()) {
244
addMappingItem (preferencemapping, iter->first, iter->second);
248
catch(YamlEmitterException &e) {
239
void YamlEmitter::serializeHooksPreference (MappingNode *map)
253
void YamlEmitter::serializeHooksPreference (MappingNode *map) throw(YamlEmitterException)
241
255
std::string preferencestr ("hooks");
243
257
int preferenceid, preferencemapping;
245
_debug ("YamlEmitter: Serialize hooks preferences");
247
259
if (map->getType() != MAPPING)
248
260
throw YamlEmitterException ("Node type is not a mapping while writing preferences");
253
265
if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
254
266
throw YamlEmitterException ("Could not add mapping to document");
256
if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
268
if (yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping) == 0)
257
269
throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
259
271
Mapping *internalmap = map->getMapping();
260
272
Mapping::iterator iter = internalmap->begin();
262
while (iter != internalmap->end()) {
263
addMappingItem (preferencemapping, iter->first, iter->second);
275
while (iter != internalmap->end()) {
276
addMappingItem (preferencemapping, iter->first, iter->second);
280
catch(YamlEmitterException &e) {
270
void YamlEmitter::serializeAudioPreference (MappingNode *map)
286
void YamlEmitter::serializeAudioPreference (MappingNode *map) throw(YamlEmitterException)
272
288
std::string preferencestr ("audio");
274
290
int preferenceid, preferencemapping;
276
_debug ("YamlEmitter: Serialize hooks preferences");
278
292
if (map->getType() != MAPPING)
279
293
throw YamlEmitterException ("Node type is not a mapping while writing preferences");
284
298
if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
285
299
throw YamlEmitterException ("Could not add mapping to document");
287
if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
301
if (yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping) == 0)
288
302
throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
290
304
Mapping *internalmap = map->getMapping();
291
305
Mapping::iterator iter = internalmap->begin();
293
while (iter != internalmap->end()) {
294
addMappingItem (preferencemapping, iter->first, iter->second);
308
while (iter != internalmap->end()) {
309
addMappingItem (preferencemapping, iter->first, iter->second);
313
catch(YamlEmitterException &e) {
301
void YamlEmitter::serializeShortcutPreference (MappingNode *map)
320
void YamlEmitter::serializeShortcutPreference (MappingNode *map) throw(YamlEmitterException)
303
322
std::string preferencestr ("shortcuts");
305
324
int preferenceid, preferencemapping;
307
_debug ("YamlEmitter: Serialize shortcuts preferences");
309
326
if (map->getType() != MAPPING)
310
327
throw YamlEmitterException ("Node type is not a mapping while writing preferences");
315
332
if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
316
333
throw YamlEmitterException ("Could not add mapping to document");
318
if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
335
if (yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping) == 0)
319
336
throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
321
338
Mapping *internalmap = map->getMapping();
322
339
Mapping::iterator iter = internalmap->begin();
324
while (iter != internalmap->end()) {
325
addMappingItem (preferencemapping, iter->first, iter->second);
342
while (iter != internalmap->end()) {
343
addMappingItem (preferencemapping, iter->first, iter->second);
347
catch(YamlEmitterException &e) {
344
365
if ( (temp2 = yaml_document_add_scalar (&document, NULL, (yaml_char_t *) sclr->getValue().c_str(), -1, YAML_PLAIN_SCALAR_STYLE)) == 0)
345
366
throw YamlEmitterException ("Could not add scalar to document");
347
if (!yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2))
368
if (yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2) == 0)
348
369
throw YamlEmitterException ("Could not append mapping pair to mapping");
350
371
} else if (node->getType() == MAPPING) {
357
378
if ( (temp2 = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
358
379
throw YamlEmitterException ("Could not add scalar to document");
360
if (!yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2))
381
if (yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2) == 0)
361
382
throw YamlEmitterException ("Could not add mapping pair to mapping");
363
384
MappingNode *map = (MappingNode *) node;
364
385
Mapping *internalmap = map->getMapping();
365
386
Mapping::iterator iter = internalmap->begin();
367
while (iter != internalmap->end()) {
368
addMappingItem (temp2, iter->first, iter->second);
389
while (iter != internalmap->end()) {
390
addMappingItem (temp2, iter->first, iter->second);
394
catch(YamlEmitterException) {
372
398
throw YamlEmitterException ("Unknown node type while adding mapping node");