summaryrefslogtreecommitdiff
path: root/libgupnp-av/gupnp-cds-last-change-parser.c
blob: 71dbbea1eb97c42f9f80c3a7120b03ab5f2d17e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
 * Copyright (C) 2012 Intel Corporation.
 *
 * Authors: Jens Georg <jensg@openismus.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

/**
 * SECTION:gupnp-cds-last-change-parser
 * @short_description: LastChange parser for the format used in
 * CDS:3
 *
 * #GUPnPCDSLastChangeParser parses XML strings from
 * CDS's LastChange state variable.
 *
 */

#include <libgupnp/gupnp-error.h>

#include "xml-util.h"
#include "gupnp-cds-last-change-parser.h"

/**
 * GUPnPCDSLastChangeEntry:
 *
 * Opaque struct which contains information about the event.
 **/
struct _GUPnPCDSLastChangeEntry {
        int ref_count;
        GUPnPCDSLastChangeEvent event;
        char *object_id;
        char *parent_id;
        char *class;
        guint32 update_id;
        gboolean is_subtree_update;
};

G_DEFINE_TYPE (GUPnPCDSLastChangeParser,
               gupnp_cds_last_change_parser,
               G_TYPE_OBJECT)

G_DEFINE_BOXED_TYPE (GUPnPCDSLastChangeEntry,
                     gupnp_cds_last_change_entry,
                     gupnp_cds_last_change_entry_ref,
                     gupnp_cds_last_change_entry_unref);

static void
gupnp_cds_last_change_parser_init (G_GNUC_UNUSED GUPnPCDSLastChangeParser *parser)
{
}

static void
gupnp_cds_last_change_parser_class_init (G_GNUC_UNUSED GUPnPCDSLastChangeParserClass *klass)
{
}

/**
 * gupnp_cds_last_change_parser_new:
 *
 * Create a new #GUPnPCDSLastChangeParser.
 *
 * This parser is able to parse LastChange as defined in the
 * ContentDirectory:3 specification.
 *
 * Returns:(transfer full): A new instance of #GUPnPCDSLastChangeParser.
 **/
GUPnPCDSLastChangeParser *
gupnp_cds_last_change_parser_new (void)
{
        return g_object_new (GUPNP_TYPE_CDS_LAST_CHANGE_PARSER,
                             NULL);
}

/**
 * gupnp_cds_last_change_parser_parse:
 * @parser: #GUPnPCDSLastChangeParser
 * @last_change: XML string to parse
 * @error: Return value for parser error or %NULL to ingore
 *
 * Parse a LastChange XML document in the flavor defined by the
 * ContentDirectory:3 specification.
 *
 * Returns: (element-type GUPnPCDSLastChangeEntry)(transfer full):
 * List of #GUPnPCDSLastChangeEntry<!-- -->s
 **/
GList *
gupnp_cds_last_change_parser_parse (GUPnPCDSLastChangeParser *parser,
                                    const char               *last_change,
                                    GError                  **error)

{
        xmlDoc *doc;
        xmlNode *state_event, *it;
        GList *result = NULL;
        GUPnPCDSLastChangeEntry *entry;

        g_return_val_if_fail (GUPNP_IS_CDS_LAST_CHANGE_PARSER (parser),
                              NULL);

        doc = xmlParseDoc ((const xmlChar *) last_change);
        if (doc == NULL) {
                g_set_error (error,
                             GUPNP_XML_ERROR,
                             GUPNP_XML_ERROR_PARSE,
                             "Could not parse LastChange XML");

                goto out;
        }

        state_event = xml_util_get_element ((xmlNode *) doc,
                                            "StateEvent",
                                            NULL);
        if (state_event == NULL) {
                g_set_error (error,
                             GUPNP_XML_ERROR,
                             GUPNP_XML_ERROR_PARSE,
                             "Missing StateEvent node");

                goto out;
        }

        for (it = state_event->children; it != NULL; it = it->next) {
                if (it->type == XML_TEXT_NODE)
                        continue;
                else if (g_ascii_strcasecmp ((const char *) it->name,
                                             "objAdd") == 0) {
                        const char *tmp;

                        entry = g_slice_new0 (GUPnPCDSLastChangeEntry);
                        entry->ref_count = 1;
                        entry->event = GUPNP_CDS_LAST_CHANGE_EVENT_OBJECT_ADDED;

                        tmp = xml_util_get_attribute_content (it, "objID");
                        entry->object_id = g_strdup (tmp);

                        tmp = xml_util_get_attribute_content (it,
                                                              "objParentID");
                        entry->parent_id = g_strdup (tmp);

                        tmp = xml_util_get_attribute_content (it, "objClass");
                        entry->class = g_strdup (tmp);

                        entry->update_id = (guint32) xml_util_get_uint_attribute
                                        (it,
                                         "updateID",
                                         0);
                        entry->is_subtree_update =
                                        xml_util_get_boolean_attribute
                                                (it,
                                                 "stUpdate");
                } else if (g_ascii_strcasecmp ((const char *) it->name,
                                               "objMod") == 0) {
                        const char *tmp;

                        entry = g_slice_new0 (GUPnPCDSLastChangeEntry);
                        entry->ref_count = 1;
                        entry->event = GUPNP_CDS_LAST_CHANGE_EVENT_OBJECT_MODIFIED;

                        tmp = xml_util_get_attribute_content (it, "objID");
                        entry->object_id = g_strdup (tmp);

                        entry->update_id = (guint32) xml_util_get_uint_attribute
                                        (it,
                                         "updateID",
                                         0);
                        entry->is_subtree_update =
                                        xml_util_get_boolean_attribute
                                                (it,
                                                 "stUpdate");
                } else if (g_ascii_strcasecmp ((const char *) it->name,
                                               "objDel") == 0) {
                        const char *tmp;

                        entry = g_slice_new0 (GUPnPCDSLastChangeEntry);
                        entry->ref_count = 1;
                        entry->event = GUPNP_CDS_LAST_CHANGE_EVENT_OBJECT_REMOVED;

                        tmp = xml_util_get_attribute_content (it, "objID");
                        entry->object_id = g_strdup (tmp);

                        entry->update_id = (guint32) xml_util_get_uint_attribute
                                        (it,
                                         "updateID",
                                         0);
                        entry->is_subtree_update =
                                        xml_util_get_boolean_attribute
                                                (it,
                                                 "stUpdate");
                } else if (g_ascii_strcasecmp ((const char *) it->name,
                                               "stDone") == 0) {
                        const char *tmp;

                        entry = g_slice_new0 (GUPnPCDSLastChangeEntry);
                        entry->ref_count = 1;
                        entry->event = GUPNP_CDS_LAST_CHANGE_EVENT_ST_DONE;

                        tmp = xml_util_get_attribute_content (it, "objID");
                        entry->object_id = g_strdup (tmp);

                        entry->update_id = (guint32) xml_util_get_uint_attribute
                                        (it,
                                         "updateID",
                                         0);
                } else {
                        g_warning ("Skipping invalid LastChange entry: %s",
                                   (const char *) it->name);
                        continue;
                }

                result = g_list_prepend (result, entry);
        }

        result = g_list_reverse (result);
out:
        if (doc != NULL) {
                xmlFreeDoc (doc);
        }

        return result;

}

/**
 * gupnp_cds_last_change_entry_ref:
 * @entry: A #GUPnPCDSLastChangeEntry
 *
 * Increase reference count of a #GUPnPCDSLastChangeEntry.
 *
 * Returns:(transfer full): The object passed in @entry.
 **/
GUPnPCDSLastChangeEntry *
gupnp_cds_last_change_entry_ref (GUPnPCDSLastChangeEntry *entry)
{
        g_return_val_if_fail (entry != NULL, NULL);
        g_return_val_if_fail (entry->ref_count > 0, NULL);

        g_atomic_int_inc (&entry->ref_count);

        return entry;
}

/**
 * gupnp_cds_last_change_entry_unref:
 * @entry: A #GUPnPCDSLastChangeEntry
 *
 * Decrease reference count of a #GUPnPCDSLastChangeEntry. If the reference
 * count drops to 0, @entry is freed.
 **/
void
gupnp_cds_last_change_entry_unref (GUPnPCDSLastChangeEntry *entry)
{
        g_return_if_fail (entry != NULL);
        g_return_if_fail (entry->ref_count > 0);

        if (g_atomic_int_dec_and_test (&entry->ref_count)) {
                g_free (entry->class);
                g_free (entry->object_id);
                g_free (entry->parent_id);

                g_slice_free (GUPnPCDSLastChangeEntry, entry);
        }
}

/**
 * gupnp_cds_last_change_entry_get_event:
 * @entry: A #GUPnPCDSLastChangeEntry
 *
 * Get the type of the last change entry as defined in
 * #GUPnPCDSLastChangeEvent.
 *
 * Returns: An event from the #GUPnPCDSLastChangeEvent or
 * %GUPNP_CDS_LAST_CHANGE_EVENT_INVALID if the entry is not valid.
 **/
GUPnPCDSLastChangeEvent
gupnp_cds_last_change_entry_get_event (GUPnPCDSLastChangeEntry *entry)
{
        g_return_val_if_fail (entry != NULL,
                              GUPNP_CDS_LAST_CHANGE_EVENT_INVALID);

        return entry->event;
}

/**
 * gupnp_cds_last_change_entry_get_object_id:
 * @entry: A #GUPnPCDSLastChangeEntry
 *
 * Get the ID of the object in this change entry.
 *
 * Returns: (transfer none): The id of the object of this entry.
 **/
const char *
gupnp_cds_last_change_entry_get_object_id (GUPnPCDSLastChangeEntry *entry)
{
        g_return_val_if_fail (entry != NULL, NULL);

        return entry->object_id;
}

/**
 * gupnp_cds_last_change_entry_get_parent_id:
 * @entry: A #GUPnPCDSLastChangeEntry
 *
 * Get the parent object id of the object in this change entry. This is only
 * valid if gupnp_cds_last_change_entry_get_event() returns
 * %GUPNP_CDS_LAST_CHANGE_EVENT_OBJECT_ADDED.
 *
 * Returns: (transfer none): The id of the object's parent of this entry.
 **/
const char *
gupnp_cds_last_change_entry_get_parent_id (GUPnPCDSLastChangeEntry *entry)
{
        g_return_val_if_fail (entry != NULL, NULL);

        return entry->parent_id;
}

/**
 * gupnp_cds_last_change_entry_get_class:
 * @entry: A #GUPnPCDSLastChangeEntry
 *
 * Get the class of the object in this change entry. This is only
 * valid if gupnp_cds_last_change_entry_get_event() returns
 * %GUPNP_CDS_LAST_CHANGE_EVENT_OBJECT_ADDED.
 *
 * Returns: (transfer none): The upnp class of the object of this entry.
 **/
const char *
gupnp_cds_last_change_entry_get_class (GUPnPCDSLastChangeEntry *entry)
{
        g_return_val_if_fail (entry != NULL, NULL);

        return entry->class;
}

/**
 * gupnp_cds_last_change_entry_is_subtree_update:
 * @entry: A #GUPnPCDSLastChangeEntry
 *
 * Returns whether this entry is part of a subtree update.
 *
 * Returns: %TRUE, if the entry is part of a subtree update, %FALSE otherwise.
 **/
gboolean
gupnp_cds_last_change_entry_is_subtree_update (GUPnPCDSLastChangeEntry *entry)
{
        g_return_val_if_fail (entry != NULL, FALSE);

        return entry->is_subtree_update;
}

/**
 * gupnp_cds_last_change_entry_get_update_id:
 * @entry: A #GUPnPCDSLastChangeEntry
 *
 * Get the update id of the last change entry.
 *
 * Returns: update id of the entry or 0 if the entry is not valid.
 **/
guint32
gupnp_cds_last_change_entry_get_update_id (GUPnPCDSLastChangeEntry *entry)
{
        g_return_val_if_fail (entry != NULL, 0);

        return entry->update_id;
}