summaryrefslogtreecommitdiff
path: root/libgupnp-av/gupnp-didl-lite-createclass.c
blob: ea4243620b901057767d4aefccb1d839972f5186 (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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
 * Copyright (C) 2012 Intel Corporation.
 * Copyright (C) 2009 Nokia Corporation.
 *
 * Author: Christophe Guiraud <christophe.guiraud@intel.com>
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *                               <zeeshan.ali@nokia.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-didl-lite-create-class
 * @short_description: DIDL-Lite CreateClass
 *
 * #GUPnPDIDLLiteCreateClass respresents a DIDL-Lite create class element.
 */

#include <string.h>

#include "gupnp-didl-lite-createclass.h"
#include "gupnp-didl-lite-createclass-private.h"
#include "xml-util.h"

G_DEFINE_TYPE (GUPnPDIDLLiteCreateClass,
               gupnp_didl_lite_create_class,
               G_TYPE_OBJECT);

struct _GUPnPDIDLLiteCreateClassPrivate {
        xmlNode     *xml_node;
        GUPnPAVXMLDoc *xml_doc;
};

enum {
        PROP_0,
        PROP_XML_NODE,
        PROP_XML_DOC,
        PROP_CONTENT,
        PROP_INCLUDE_DERIVED,
        PROP_FRIENDLY_NAME,
};

static void
gupnp_didl_lite_create_class_init (GUPnPDIDLLiteCreateClass *create_class)
{
        create_class->priv = G_TYPE_INSTANCE_GET_PRIVATE
                                        (create_class,
                                         GUPNP_TYPE_DIDL_LITE_CREATE_CLASS,
                                         GUPnPDIDLLiteCreateClassPrivate);
}

static void
gupnp_didl_lite_create_class_get_property (GObject    *object,
                                           guint       property_id,
                                           GValue     *value,
                                           GParamSpec *pspec)
{
        GUPnPDIDLLiteCreateClass *create_class;

        create_class = GUPNP_DIDL_LITE_CREATE_CLASS (object);

        switch (property_id) {
        case PROP_XML_NODE:
                g_value_set_pointer
                    (value,
                     gupnp_didl_lite_create_class_get_xml_node
                                                            (create_class));
                break;
        case PROP_CONTENT:
                g_value_set_string
                    (value,
                     gupnp_didl_lite_create_class_get_content
                                                            (create_class));
                break;
        case PROP_INCLUDE_DERIVED:
                g_value_set_boolean
                    (value,
                     gupnp_didl_lite_create_class_get_include_derived
                                                            (create_class));
                break;
        case PROP_FRIENDLY_NAME:
                g_value_set_string
                    (value,
                     gupnp_didl_lite_create_class_get_friendly_name
                                                            (create_class));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                break;
        }
}

static void
gupnp_didl_lite_create_class_set_property (GObject      *object,
                                            guint         property_id,
                                            const GValue *value,
                                            GParamSpec   *pspec)
{
        GUPnPDIDLLiteCreateClass *create_class;

        create_class = GUPNP_DIDL_LITE_CREATE_CLASS (object);

        switch (property_id) {
        case PROP_XML_NODE:
                create_class->priv->xml_node = g_value_get_pointer (value);
                break;
        case PROP_XML_DOC:
                create_class->priv->xml_doc = g_value_dup_boxed (value);
                break;
        case PROP_CONTENT:
                gupnp_didl_lite_create_class_set_content
                                                 (create_class,
                                                  g_value_get_string (value));
                break;
        case PROP_INCLUDE_DERIVED:
                gupnp_didl_lite_create_class_set_include_derived
                                                 (create_class,
                                                  g_value_get_boolean (value));
                break;
        case PROP_FRIENDLY_NAME:
                gupnp_didl_lite_create_class_set_friendly_name
                                                 (create_class,
                                                  g_value_get_string (value));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                break;
        }
}

static void
gupnp_didl_lite_create_class_dispose (GObject *object)
{
        GObjectClass                    *object_class;
        GUPnPDIDLLiteCreateClassPrivate *priv;

        priv = GUPNP_DIDL_LITE_CREATE_CLASS (object)->priv;

        g_clear_pointer (&priv->xml_doc, xml_doc_unref);

        object_class = G_OBJECT_CLASS
                                   (gupnp_didl_lite_create_class_parent_class);
        object_class->dispose (object);
}

static void
gupnp_didl_lite_create_class_class_init (GUPnPDIDLLiteCreateClassClass *klass)
{
        GObjectClass *object_class;

        object_class = G_OBJECT_CLASS (klass);

        object_class->get_property = gupnp_didl_lite_create_class_get_property;
        object_class->set_property = gupnp_didl_lite_create_class_set_property;
        object_class->dispose = gupnp_didl_lite_create_class_dispose;

        g_type_class_add_private (klass,
                                  sizeof (GUPnPDIDLLiteCreateClassPrivate));

        /**
         * GUPnPDIDLLiteCreateClass:xml-node:
         *
         * The pointer to desc node in XML document.
         **/
        g_object_class_install_property
                (object_class,
                 PROP_XML_NODE,
                 g_param_spec_pointer
                                   ("xml-node",
                                    "XMLNode",
                                    "The pointer to desc node in XML"
                                    " document.",
                                    G_PARAM_READWRITE |
                                    G_PARAM_CONSTRUCT_ONLY |
                                    G_PARAM_STATIC_NAME |
                                    G_PARAM_STATIC_NICK |
                                    G_PARAM_STATIC_BLURB));

        /**
         * GUPnPDIDLLiteCreateClass:xml-doc:
         *
         * The reference to XML document containing this object.
         *
         * Internal property.
         *
         * Stability: Private
         **/
        g_object_class_install_property
                (object_class,
                 PROP_XML_DOC,
                 g_param_spec_boxed
                                   ("xml-doc",
                                    "XMLDoc",
                                    "The reference to XML document"
                                    " containing this object.",
                                    xml_doc_get_type (),
                                    G_PARAM_WRITABLE |
                                    G_PARAM_CONSTRUCT_ONLY |
                                    G_PARAM_STATIC_NAME |
                                    G_PARAM_STATIC_NICK |
                                    G_PARAM_STATIC_BLURB));

        /**
         * GUPnPDIDLLiteCreateClass:content:
         *
         * The content of this create Class.
         **/
        g_object_class_install_property
                (object_class,
                 PROP_CONTENT,
                 g_param_spec_string
                                   ("content",
                                    "Content",
                                    "The content of this create Class.",
                                    NULL,
                                    G_PARAM_READWRITE |
                                    G_PARAM_STATIC_NAME |
                                    G_PARAM_STATIC_NICK |
                                    G_PARAM_STATIC_BLURB));

        /**
         * GUPnPDIDLLiteCreateClass:include-derived:
         *
         * Whether this create Class can be derived.
         **/
        g_object_class_install_property
                (object_class,
                 PROP_INCLUDE_DERIVED,
                 g_param_spec_boolean
                                   ("include-derived",
                                    "IncludeDerived",
                                    "Wether this create Class can be derived.",
                                    FALSE,
                                    G_PARAM_READWRITE |
                                    G_PARAM_STATIC_NAME |
                                    G_PARAM_STATIC_NICK |
                                    G_PARAM_STATIC_BLURB));

        /**
         * GUPnPDIDLLiteCreateClass:friendly-name:
         *
         * The friendly name of this create Class.
         **/
        g_object_class_install_property
                (object_class,
                 PROP_FRIENDLY_NAME,
                 g_param_spec_string
                                   ("friendly-name",
                                    "FriendlyName",
                                    "The friendly name of this create Class.",
                                    NULL,
                                    G_PARAM_READWRITE |
                                    G_PARAM_STATIC_NAME |
                                    G_PARAM_STATIC_NICK |
                                    G_PARAM_STATIC_BLURB));
}

/**
 * gupnp_didl_lite_create_class_get_content:
 * @create_class: #GUPnPDIDLLiteCreateClass
 *
 * Get the content of the @create_class.
 *
 * Return value: The Content of the @create_class, or %NULL.
 **/
const char *
gupnp_didl_lite_create_class_get_content
                                    (GUPnPDIDLLiteCreateClass *create_class)
{
        g_return_val_if_fail (GUPNP_IS_DIDL_LITE_CREATE_CLASS (create_class),
                              NULL);

        if (G_UNLIKELY (create_class->priv->xml_node->children == NULL))
                return NULL;

        return (const char *) create_class->priv->xml_node->children->content;
}

/**
 * gupnp_didl_lite_create_class_set_content:
 * @create_class: #GUPnPDIDLLiteCreateClass
 * @content: The content
 *
 * Set the content of the @create_class.
 **/
void
gupnp_didl_lite_create_class_set_content
                                    (GUPnPDIDLLiteCreateClass *create_class,
                                     const char               *content)
{
        xmlChar *escaped;

        g_return_if_fail (GUPNP_IS_DIDL_LITE_CREATE_CLASS (create_class));
        g_return_if_fail (create_class != NULL);

        escaped = xmlEncodeSpecialChars (create_class->priv->xml_doc->doc,
                                         (const unsigned char *) content);
        xmlNodeSetContent (create_class->priv->xml_node, escaped);
        xmlFree (escaped);

        g_object_notify (G_OBJECT (create_class), "content");
}

/**
 * gupnp_didl_lite_create_class_get_include_derived:
 * @create_class: #GUPnPDIDLLiteCreateClass
 *
 * Checks whether @create_class can be derived.
 *
 * Return value: #TRUE if @create_class can be derived.
 **/
gboolean
gupnp_didl_lite_create_class_get_include_derived
                                    (GUPnPDIDLLiteCreateClass *create_class)
{
        g_return_val_if_fail (GUPNP_IS_DIDL_LITE_CREATE_CLASS (create_class),
                              FALSE);

        return xml_util_get_boolean_attribute (create_class->priv->xml_node,
                                               "includeDerived");
}

/**
 * gupnp_didl_lite_create_class_set_include_derived:
 * @create_class: #GUPnPDIDLLiteCreateClass
 * @include_derived: the derivability
 *
 * (Un)set the derivability of create_class.
 **/
void
gupnp_didl_lite_create_class_set_include_derived
                                    (GUPnPDIDLLiteCreateClass *create_class,
                                     gboolean                 include_derived)
{
        const char *str;

        g_return_if_fail (create_class != NULL);
        g_return_if_fail (GUPNP_IS_DIDL_LITE_CREATE_CLASS (create_class));

        if (include_derived)
                str = "1";
        else
                str = "0";
        xmlSetProp (create_class->priv->xml_node,
                    (unsigned char *) "includeDerived",
                    (unsigned char *) str);

        g_object_notify (G_OBJECT (create_class), "include-derived");
}

/**
 * gupnp_didl_lite_create_class_get_friendly_name:
 * @create_class: #GUPnPDIDLLiteCreateClass
 *
 * Get the friendly name of the @create_class.
 *
 * Return value: The FriendlyName of the @create_class, or %NULL.
 **/
const char *
gupnp_didl_lite_create_class_get_friendly_name
                                    (GUPnPDIDLLiteCreateClass *create_class)
{
        g_return_val_if_fail (GUPNP_IS_DIDL_LITE_CREATE_CLASS (create_class),
                              NULL);
        return xml_util_get_attribute_content (create_class->priv->xml_node,
                                               "name");
}

/**
 * gupnp_didl_lite_create_class_set_friendly_name:
 * @create_class: #GUPnPDIDLLiteCreateClass
 * @friendly_name: The friendly name
 *
 * Set the friendly name of the @create_class.
 **/
void
gupnp_didl_lite_create_class_set_friendly_name
                                    (GUPnPDIDLLiteCreateClass *create_class,
                                     const char               *friendly_name)
{
        g_return_if_fail (create_class != NULL);
        g_return_if_fail (GUPNP_IS_DIDL_LITE_CREATE_CLASS (create_class));

        xmlSetProp (create_class->priv->xml_node,
                    (unsigned char *) "name",
                    (const unsigned char *) friendly_name);

        g_object_notify (G_OBJECT (create_class), "friendly-name");
}

/**
 * gupnp_didl_lite_create_class_new_from_xml:
 * @xml_node: The pointer to relevant node in XML document
 * @xml_doc: The reference to containing XML document
 *
 * Creates a new #GUPnPDIDLLiteCreateClass for the @xml_node.
 *
 * Returns: (transfer full): A new #GUPnPDIDLLiteCreateClass object. Unref after usage.
 **/
GUPnPDIDLLiteCreateClass *
gupnp_didl_lite_create_class_new_from_xml (xmlNode     *xml_node,
                                           GUPnPAVXMLDoc *xml_doc)
{
        return g_object_new (GUPNP_TYPE_DIDL_LITE_CREATE_CLASS,
                             "xml-node", xml_node,
                             "xml-doc", xml_doc,
                             NULL);
}

/**
 * gupnp_didl_lite_create_class_get_xml_node:
 * @create_class: The #GUPnPDIDLLiteCreateClass
 *
 * Get the pointer to relevant node in XML document.
 *
 * Returns: (transfer none): The pointer to relevant node in XML document.
 **/
xmlNode *
gupnp_didl_lite_create_class_get_xml_node
                                      (GUPnPDIDLLiteCreateClass *create_class)
{
        g_return_val_if_fail (GUPNP_IS_DIDL_LITE_CREATE_CLASS (create_class),
                              NULL);

        return create_class->priv->xml_node;
}