~ubuntu-branches/ubuntu/saucy/sflphone/saucy

« back to all changes in this revision

Viewing changes to sflphone-common/src/config/yamlemitter.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2010-12-24 16:33:55 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20101224163355-tkvvikqxbrbav6up
Tags: 0.9.11-1
* New upstream release
* Add new build dependencies on libwebkit-dev and libyaml-dev

* Bump Standards-Version up to 3.9.1
* Bump debhelper compatibility to 8
* Patch another typo in the upstream code (lintian notice)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
 
3
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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.
 
18
 *
 
19
 *  Additional permission under GNU GPL version 3 section 7:
 
20
 *
 
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.
 
29
 */
 
30
 
 
31
#include "yamlemitter.h"
 
32
#include <stdio.h>
 
33
#include "../global.h"
 
34
 
 
35
namespace Conf
 
36
{
 
37
 
 
38
YamlEmitter::YamlEmitter (const char *file) : filename (file), isFirstAccount (true)
 
39
{
 
40
    open();
 
41
}
 
42
 
 
43
YamlEmitter::~YamlEmitter()
 
44
{
 
45
    close();
 
46
}
 
47
 
 
48
void YamlEmitter::open()
 
49
{
 
50
 
 
51
    fd = fopen (filename.c_str(), "w");
 
52
 
 
53
    if (!fd)
 
54
        throw YamlEmitterException ("Could not open file descriptor");
 
55
 
 
56
    if (!yaml_emitter_initialize (&emitter))
 
57
        throw YamlEmitterException ("Could not initialize emitter");
 
58
 
 
59
    // Allows unescaped unicode characters
 
60
    yaml_emitter_set_unicode (&emitter, 1);
 
61
 
 
62
    yaml_emitter_set_output_file (&emitter, fd);
 
63
 
 
64
    if (!yaml_document_initialize (&document, NULL, NULL, NULL, 0, 0))
 
65
        throw YamlEmitterException ("Could not initialize yaml document while saving configuration");
 
66
 
 
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");
 
70
}
 
71
 
 
72
void YamlEmitter::close()
 
73
{
 
74
    // yaml_emitter_delete(&emitter);
 
75
 
 
76
    if (!fd)
 
77
        throw YamlEmitterException ("File descriptor not valid");
 
78
 
 
79
 
 
80
    if (fclose (fd))
 
81
        throw YamlEmitterException ("Error closing file descriptor");
 
82
 
 
83
 
 
84
    _debug ("Config: Configuration file closed successfully");
 
85
 
 
86
}
 
87
 
 
88
void YamlEmitter::read() {}
 
89
 
 
90
void YamlEmitter::write()
 
91
{
 
92
 
 
93
}
 
94
 
 
95
void YamlEmitter::serializeData()
 
96
{
 
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");
 
100
}
 
101
 
 
102
 
 
103
void YamlEmitter::serializeAccount (MappingNode *map)
 
104
{
 
105
 
 
106
    std::string accountstr ("accounts");
 
107
 
 
108
    int accountid, accountmapping;
 
109
 
 
110
    _debug ("YamlEmitter: Serialize account");
 
111
 
 
112
    if (map->getType() != MAPPING)
 
113
        throw YamlEmitterException ("Node type is not a mapping while writing account");
 
114
 
 
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");
 
119
 
 
120
        if ( (accountSequence = yaml_document_add_sequence (&document, NULL, YAML_BLOCK_SEQUENCE_STYLE)) == 0)
 
121
            throw YamlEmitterException ("Could not add sequence to document");
 
122
 
 
123
        if (!yaml_document_append_mapping_pair (&document, topLevelMapping, accountid, accountSequence))
 
124
            throw YamlEmitterException ("Could not add mapping pair to top level mapping");
 
125
 
 
126
        isFirstAccount = false;
 
127
    }
 
128
 
 
129
    if ( (accountmapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
 
130
        throw YamlEmitterException ("Could not add account mapping to document");
 
131
 
 
132
    if (!yaml_document_append_sequence_item (&document, accountSequence, accountmapping))
 
133
        throw YamlEmitterException ("Could not append account mapping to sequence");
 
134
 
 
135
    Mapping *internalmap = map->getMapping();
 
136
    Mapping::iterator iter = internalmap->begin();
 
137
 
 
138
    while (iter != internalmap->end()) {
 
139
        addMappingItem (accountmapping, iter->first, iter->second);
 
140
        iter++;
 
141
    }
 
142
 
 
143
}
 
144
 
 
145
void YamlEmitter::serializePreference (MappingNode *map)
 
146
{
 
147
    std::string preferencestr ("preferences");
 
148
 
 
149
    int preferenceid, preferencemapping;
 
150
 
 
151
    _debug ("YamlEmitter: Serialize preference");
 
152
 
 
153
    if (map->getType() != MAPPING)
 
154
        throw YamlEmitterException ("Node type is not a mapping while writing preferences");
 
155
 
 
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");
 
158
 
 
159
    if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
 
160
        throw YamlEmitterException ("Could not add mapping to document");
 
161
 
 
162
    if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
 
163
        throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
 
164
 
 
165
    Mapping *internalmap = map->getMapping();
 
166
    Mapping::iterator iter = internalmap->begin();
 
167
 
 
168
    while (iter != internalmap->end()) {
 
169
        addMappingItem (preferencemapping, iter->first, iter->second);
 
170
        iter++;
 
171
    }
 
172
 
 
173
}
 
174
 
 
175
void YamlEmitter::serializeVoipPreference (MappingNode *map)
 
176
{
 
177
    std::string preferencestr ("voipPreferences");
 
178
 
 
179
    int preferenceid, preferencemapping;
 
180
 
 
181
    _debug ("YamlEmitter: Serialize voip preference");
 
182
 
 
183
    if (map->getType() != MAPPING)
 
184
        throw YamlEmitterException ("Node type is not a mapping while writing preferences");
 
185
 
 
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");
 
188
 
 
189
    if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
 
190
        throw YamlEmitterException ("Could not add mapping to document");
 
191
 
 
192
    if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
 
193
        throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
 
194
 
 
195
    Mapping *internalmap = map->getMapping();
 
196
    Mapping::iterator iter = internalmap->begin();
 
197
 
 
198
    while (iter != internalmap->end()) {
 
199
        addMappingItem (preferencemapping, iter->first, iter->second);
 
200
        iter++;
 
201
    }
 
202
 
 
203
}
 
204
 
 
205
void YamlEmitter::serializeAddressbookPreference (MappingNode *map)
 
206
{
 
207
    std::string preferencestr ("addressbook");
 
208
 
 
209
    int preferenceid, preferencemapping;
 
210
 
 
211
    _debug ("YamlEmitter: Serialize addressbook preferences");
 
212
 
 
213
    if (map->getType() != MAPPING)
 
214
        throw YamlEmitterException ("Node type is not a mapping while writing preferences");
 
215
 
 
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");
 
218
 
 
219
    if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
 
220
        throw YamlEmitterException ("Could not add mapping to document");
 
221
 
 
222
    if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
 
223
        throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
 
224
 
 
225
    Mapping *internalmap = map->getMapping();
 
226
    Mapping::iterator iter = internalmap->begin();
 
227
 
 
228
    while (iter != internalmap->end()) {
 
229
        addMappingItem (preferencemapping, iter->first, iter->second);
 
230
        iter++;
 
231
    }
 
232
 
 
233
}
 
234
 
 
235
void YamlEmitter::serializeHooksPreference (MappingNode *map)
 
236
{
 
237
    std::string preferencestr ("hooks");
 
238
 
 
239
    int preferenceid, preferencemapping;
 
240
 
 
241
    _debug ("YamlEmitter: Serialize hooks preferences");
 
242
 
 
243
    if (map->getType() != MAPPING)
 
244
        throw YamlEmitterException ("Node type is not a mapping while writing preferences");
 
245
 
 
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");
 
248
 
 
249
    if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
 
250
        throw YamlEmitterException ("Could not add mapping to document");
 
251
 
 
252
    if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
 
253
        throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
 
254
 
 
255
    Mapping *internalmap = map->getMapping();
 
256
    Mapping::iterator iter = internalmap->begin();
 
257
 
 
258
    while (iter != internalmap->end()) {
 
259
        addMappingItem (preferencemapping, iter->first, iter->second);
 
260
        iter++;
 
261
    }
 
262
 
 
263
}
 
264
 
 
265
 
 
266
void YamlEmitter::serializeAudioPreference (MappingNode *map)
 
267
{
 
268
    std::string preferencestr ("audio");
 
269
 
 
270
    int preferenceid, preferencemapping;
 
271
 
 
272
    _debug ("YamlEmitter: Serialize hooks preferences");
 
273
 
 
274
    if (map->getType() != MAPPING)
 
275
        throw YamlEmitterException ("Node type is not a mapping while writing preferences");
 
276
 
 
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");
 
279
 
 
280
    if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
 
281
        throw YamlEmitterException ("Could not add mapping to document");
 
282
 
 
283
    if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
 
284
        throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
 
285
 
 
286
    Mapping *internalmap = map->getMapping();
 
287
    Mapping::iterator iter = internalmap->begin();
 
288
 
 
289
    while (iter != internalmap->end()) {
 
290
        addMappingItem (preferencemapping, iter->first, iter->second);
 
291
        iter++;
 
292
    }
 
293
 
 
294
}
 
295
 
 
296
 
 
297
void YamlEmitter::serializeShortcutPreference (MappingNode *map)
 
298
{
 
299
    std::string preferencestr ("shortcuts");
 
300
 
 
301
    int preferenceid, preferencemapping;
 
302
 
 
303
    _debug ("YamlEmitter: Serialize shortcuts preferences");
 
304
 
 
305
    if (map->getType() != MAPPING)
 
306
        throw YamlEmitterException ("Node type is not a mapping while writing preferences");
 
307
 
 
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");
 
310
 
 
311
    if ( (preferencemapping = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
 
312
        throw YamlEmitterException ("Could not add mapping to document");
 
313
 
 
314
    if (!yaml_document_append_mapping_pair (&document, topLevelMapping, preferenceid, preferencemapping))
 
315
        throw YamlEmitterException ("Could not add mapping pair to top leve mapping");
 
316
 
 
317
    Mapping *internalmap = map->getMapping();
 
318
    Mapping::iterator iter = internalmap->begin();
 
319
 
 
320
    while (iter != internalmap->end()) {
 
321
        addMappingItem (preferencemapping, iter->first, iter->second);
 
322
        iter++;
 
323
    }
 
324
 
 
325
}
 
326
 
 
327
 
 
328
void YamlEmitter::addMappingItem (int mappingid, Key key, YamlNode *node)
 
329
{
 
330
 
 
331
    if (node->getType() == SCALAR) {
 
332
 
 
333
        int temp1, temp2;
 
334
 
 
335
        ScalarNode *sclr = (ScalarNode *) node;
 
336
 
 
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");
 
339
 
 
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");
 
342
 
 
343
        if (!yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2))
 
344
            throw YamlEmitterException ("Could not append mapping pair to mapping");
 
345
 
 
346
    } else if (node->getType() == MAPPING) {
 
347
 
 
348
        int temp1, temp2;
 
349
 
 
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");
 
352
 
 
353
        if ( (temp2 = yaml_document_add_mapping (&document, NULL, YAML_BLOCK_MAPPING_STYLE)) == 0)
 
354
            throw YamlEmitterException ("Could not add scalar to document");
 
355
 
 
356
        if (!yaml_document_append_mapping_pair (&document, mappingid, temp1, temp2))
 
357
            throw YamlEmitterException ("Could not add mapping pair to mapping");
 
358
 
 
359
        MappingNode *map = (MappingNode *) node;
 
360
        Mapping *internalmap = map->getMapping();
 
361
        Mapping::iterator iter = internalmap->begin();
 
362
 
 
363
        while (iter != internalmap->end()) {
 
364
            addMappingItem (temp2, iter->first, iter->second);
 
365
            iter++;
 
366
        }
 
367
    } else
 
368
        throw YamlEmitterException ("Unknown node type while adding mapping node");
 
369
}
 
370
 
 
371
 
 
372
}