summaryrefslogtreecommitdiff
path: root/libgupnp-av/gupnp-didl-lite-writer.c
blob: baec6fc5e2128e9d99fbe519a8dbfd206acfab8b (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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
/*
 * Copyright (C) 2007, 2008 OpenedHand Ltd.
 *
 * Authors: Jorn Baayen <jorn@openedhand.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-writer
 * @short_description: DIDL-Lite fragment writer
 *
 * #GUPnPDIDLLiteWriter is a helper class for writing DIDL-Lite fragments.
 */

#include <string.h>

#include "gupnp-didl-lite-writer.h"
#include "gupnp-didl-lite-object.h"
#include "gupnp-didl-lite-object-private.h"
#include "gupnp-didl-lite-descriptor-private.h"

#include "xml-util.h"

G_DEFINE_TYPE (GUPnPDIDLLiteWriter,
               gupnp_didl_lite_writer,
               G_TYPE_OBJECT);

struct _GUPnPDIDLLiteWriterPrivate {
        xmlNode     *xml_node;
        GUPnPXMLDoc *xml_doc;

        xmlNs       *upnp_ns;
        xmlNs       *dc_ns;
        xmlNs       *dlna_ns;

        char        *language;
};

enum {
        PROP_0,
        PROP_XML_NODE,
        PROP_LANGUAGE,
};

static int
compare_prop (const char *a, xmlAttr *attr)
{
        const char *p;
        char *parent_name;
        char *attr_name;
        int ret = -1;

        if (attr->ns != NULL)
                attr_name = g_strjoin (":", attr->ns->prefix, attr->name, NULL);
        else
                attr_name = g_strdup ((const char *) attr->name);

        if (attr->parent->ns != NULL)
                parent_name = g_strjoin (":",
                                         attr->parent->ns->prefix,
                                         attr->parent->name,
                                         NULL);
        else
                parent_name = g_strdup ((const char *) attr->parent->name);

        p = strstr (a, "@");
        if (p)
                if (p == a)
                        /* Top-level property */
                        ret = strcmp (a + 1, attr_name);
                else
                        ret = strncmp (a, parent_name, p - a) ||
                              strcmp (p + 1, attr_name);
        else
                ret = strcmp (a, attr_name);

        g_free (attr_name);
        g_free (parent_name);

        return ret;
}

static gboolean
is_attribute_forbidden (xmlAttr *attr,
                        GList   *allowed)
{
        return g_list_find_custom (allowed,
                                   attr,
                                   (GCompareFunc) compare_prop) == NULL;
}

static int
compare_node_name (const char *a, const char *b)
{
        const char *p;
        int len;

        if (a[0] == '@')
                /* Filer is for top-level property */
                return -1;

        p = strstr (a, "@");
        if (p != NULL)
                /* Compare only the string before '@' */
                len = p - a;
        else
                len = strlen (a);

        return strncmp (a, b, len);
}

static gboolean
is_node_forbidden (xmlNode     *node,
                    GList      *allowed,
                    const char *ns)
{
        char *name;
        gboolean ret;

        if (ns != NULL)
                name = g_strjoin (":", ns, node->name, NULL);
        else
                name = g_strdup ((const char *) node->name);

        ret = g_list_find_custom (allowed,
                                  name,
                                  (GCompareFunc) compare_node_name) == NULL;

        g_free (name);

        return ret;
}

static gboolean
is_container_standard_prop (const char *name,
                            const char *namespace,
                            const char *upnp_class)
{
        return g_strcmp0 (upnp_class, "object.container.storageFolder") == 0 &&
               g_strcmp0 (namespace, "upnp") == 0 &&
               strcmp (name, "storageUsed") == 0;
}

static gboolean
is_standard_prop (const char *name,
                  const char *namespace,
                  const char *parent_name)
{
        return strcmp (name, "id") == 0 ||
               strcmp (name, "parentID") == 0 ||
               strcmp (name, "restricted") == 0 ||
               (g_strcmp0 (namespace, "dc") == 0 &&
                strcmp (name, "title") == 0) ||
               (g_strcmp0 (namespace, "upnp") == 0 &&
                strcmp (name, "class") == 0) ||
               (g_strcmp0 (parent_name, "res") == 0 &&
                strcmp (name, "protocolInfo") == 0);
}

static void
filter_attributes (xmlNode             *node,
                   GList               *allowed,
                   GUPnPDIDLLiteWriter *writer)
{
        xmlAttr *attr;
        GList   *forbidden = NULL;
        GList   *l;

        /* Find forbidden properties */
        for (attr = node->properties; attr != NULL; attr = attr->next)
                if (!is_standard_prop ((const char *) attr->name,
                                        NULL,
                                        (const char *) attr->parent->name) &&
                    is_attribute_forbidden (attr, allowed))
                        forbidden = g_list_append (forbidden, attr);

        /* Now unset forbidden properties */
        for (l = forbidden; l != NULL; l = l->next)
                xmlRemoveProp ((xmlAttr *) l->data);

        g_list_free (forbidden);
}

static void
filter_node (xmlNode             *node,
             GList               *allowed,
             GUPnPDIDLLiteWriter *writer)
{
        xmlNode *child;
        GList   *forbidden = NULL;
        GList   *l;
        gboolean is_container;
        const char *container_class = NULL;

        filter_attributes (node, allowed, writer);

        if (strcmp ((const char *) node->name, "container") == 0) {
            is_container = TRUE;
            container_class = xml_util_get_child_element_content (node,
                                                                  "class");
        }

        forbidden = NULL;
        for (child = node->children; child != NULL; child = child->next) {
                const char *ns = NULL;

                if (xmlNodeIsText (child))
                        continue;

                if (child->ns != NULL)
                        ns = (const char *) child->ns->prefix;

                if (!(is_container && is_container_standard_prop
                                            ((const char *) child->name,
                                             ns,
                                             container_class)) &&
                    !is_standard_prop ((const char *) child->name,
                                       ns,
                                       (const char *)  node->name) &&
                    is_node_forbidden (child, allowed, ns))
                        forbidden = g_list_append (forbidden, child);
        }

        /* Now remove the forbidden nodes */
        for (l = forbidden; l != NULL; l = l->next) {
                xmlNode *n;

                n = (xmlNode *) l->data;

                xmlUnlinkNode (n);
                xmlFreeNode (n);
        }

        g_list_free (forbidden);

        /* Recurse */
        for (child = node->children; child != NULL; child = child->next)
                if (!xmlNodeIsText (child))
                        filter_node (child, allowed, writer);
}

static void
gupnp_didl_lite_writer_init (GUPnPDIDLLiteWriter *writer)
{
        writer->priv = G_TYPE_INSTANCE_GET_PRIVATE (writer,
                                                    GUPNP_TYPE_DIDL_LITE_WRITER,
                                                    GUPnPDIDLLiteWriterPrivate);
}

static void
gupnp_didl_lite_writer_set_property (GObject      *object,
                                     guint         property_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)

{
        GUPnPDIDLLiteWriter *writer;

        writer = GUPNP_DIDL_LITE_WRITER (object);

        switch (property_id) {
        case PROP_LANGUAGE:
                writer->priv->language = g_value_dup_string (value);
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                break;
        }
}

static void
gupnp_didl_lite_writer_get_property (GObject    *object,
                                     guint       property_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
{
        GUPnPDIDLLiteWriter *writer;

        writer = GUPNP_DIDL_LITE_WRITER (object);

        switch (property_id) {
        case PROP_XML_NODE:
                g_value_set_pointer
                        (value, gupnp_didl_lite_writer_get_xml_node (writer));
                break;
        case PROP_LANGUAGE:
                g_value_set_string
                        (value, gupnp_didl_lite_writer_get_language (writer));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                break;
        }
}

static void
gupnp_didl_lite_writer_constructed (GObject *object)
{
        GObjectClass               *object_class;
        GUPnPDIDLLiteWriterPrivate *priv;
        xmlDoc                     *doc;

        priv = GUPNP_DIDL_LITE_WRITER (object)->priv;

        doc = xmlNewDoc ((unsigned char *) "1.0");
        priv->xml_doc = gupnp_xml_doc_new (doc);

        priv->xml_node = xmlNewDocNode (priv->xml_doc->doc,
                                        NULL,
                                        (unsigned char *) "DIDL-Lite",
                                        NULL);
        xmlDocSetRootElement (priv->xml_doc->doc, priv->xml_node);
        priv->dc_ns = xmlNewNs (priv->xml_node,
                                (unsigned char *)
                                "http://purl.org/dc/elements/1.1/",
                                (unsigned char *)
                                GUPNP_DIDL_LITE_WRITER_NAMESPACE_DC);
        priv->upnp_ns = xmlNewNs (priv->xml_node,
                                  (unsigned char *)
                                  "urn:schemas-upnp-org:metadata-1-0/upnp/",
                                  (unsigned char *)
                                  GUPNP_DIDL_LITE_WRITER_NAMESPACE_UPNP);
        priv->dlna_ns = xmlNewNs (priv->xml_node,
                                  (unsigned char *)
                                  "urn:schemas-dlna-org:metadata-1-0/",
                                  (unsigned char *)
                                  GUPNP_DIDL_LITE_WRITER_NAMESPACE_DLNA);
        xmlNewNs (priv->xml_node,
                  (unsigned char *)
                  "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/",
                  NULL);

        if (priv->language)
                xmlSetProp (priv->xml_node,
                            (unsigned char *) "lang",
                            (unsigned char *) priv->language);

        object_class = G_OBJECT_CLASS (gupnp_didl_lite_writer_parent_class);
        if (object_class->constructed != NULL)
                object_class->constructed (object);
}

static void
gupnp_didl_lite_writer_dispose (GObject *object)
{
        GObjectClass               *object_class;
        GUPnPDIDLLiteWriterPrivate *priv;

        priv = GUPNP_DIDL_LITE_WRITER (object)->priv;

        if (priv->xml_doc) {
                g_object_unref (priv->xml_doc);
                priv->xml_doc = NULL;
        }

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

static void
gupnp_didl_lite_writer_finalize (GObject *object)
{
        GObjectClass               *object_class;
        GUPnPDIDLLiteWriterPrivate *priv;

        priv = GUPNP_DIDL_LITE_WRITER (object)->priv;

        if (priv->language)
                g_free (priv->language);

        object_class = G_OBJECT_CLASS (gupnp_didl_lite_writer_parent_class);
        object_class->finalize (object);
}

static void
gupnp_didl_lite_writer_class_init (GUPnPDIDLLiteWriterClass *klass)
{
        GObjectClass *object_class;

        object_class = G_OBJECT_CLASS (klass);

        object_class->set_property = gupnp_didl_lite_writer_set_property;
        object_class->get_property = gupnp_didl_lite_writer_get_property;
        object_class->constructed = gupnp_didl_lite_writer_constructed;
        object_class->dispose = gupnp_didl_lite_writer_dispose;
        object_class->finalize = gupnp_didl_lite_writer_finalize;

        g_type_class_add_private (klass, sizeof (GUPnPDIDLLiteWriterPrivate));

        /**
         * GUPnPDIDLLiteWriter:xml-node:
         *
         * The pointer to root node in XML document.
         **/
        g_object_class_install_property
                (object_class,
                 PROP_XML_NODE,
                 g_param_spec_pointer ("xml-node",
                                       "XMLNode",
                                       "The pointer to root node in XML"
                                       " document.",
                                       G_PARAM_READABLE |
                                       G_PARAM_STATIC_NAME |
                                       G_PARAM_STATIC_NICK |
                                       G_PARAM_STATIC_BLURB));

        /**
         * GUPnPDIDLLiteWriter:language:
         *
         * The language the DIDL-Lite fragment is in.
         *
         **/
        g_object_class_install_property
                (object_class,
                 PROP_LANGUAGE,
                 g_param_spec_string ("language",
                                      "Language",
                                      "The language the DIDL-Lite fragment"
                                      " is in.",
                                      NULL,
                                      G_PARAM_CONSTRUCT_ONLY |
                                      G_PARAM_READWRITE |
                                      G_PARAM_STATIC_NAME |
                                      G_PARAM_STATIC_NICK |
                                      G_PARAM_STATIC_BLURB));
}

/**
 * gupnp_didl_lite_writer_new:
 * @language: The language the DIDL-Lite fragment is in, or NULL
 *
 * Return value: A new #GUPnPDIDLLiteWriter object.
 **/
GUPnPDIDLLiteWriter *
gupnp_didl_lite_writer_new (const char *language)
{
        return g_object_new (GUPNP_TYPE_DIDL_LITE_WRITER,
                             "language", language,
                             NULL);
}

/**
 * gupnp_didl_lite_writer_add_item:
 * @writer: A #GUPnPDIDLLiteWriter
 *
 * Creates a new item, attaches it to @writer and returns it.
 *
 * Returns: (transfer full): A new #GUPnPDIDLLiteItem object. Unref after usage.
 **/
GUPnPDIDLLiteItem *
gupnp_didl_lite_writer_add_item (GUPnPDIDLLiteWriter *writer)
{
        xmlNode *item_node;
        GUPnPDIDLLiteObject *object;

        g_return_val_if_fail (GUPNP_IS_DIDL_LITE_WRITER (writer), NULL);

        item_node = xmlNewChild (writer->priv->xml_node,
                                NULL,
                                (unsigned char *) "item",
                                NULL);

        object = gupnp_didl_lite_object_new_from_xml (item_node,
                                                      writer->priv->xml_doc,
                                                      writer->priv->upnp_ns,
                                                      writer->priv->dc_ns,
                                                      writer->priv->dlna_ns);
        return GUPNP_DIDL_LITE_ITEM (object);
}

/**
 * gupnp_didl_lite_writer_add_container:
 * @writer: A #GUPnPDIDLLiteWriter
 *
 * Creates a new container, attaches it to @writer and returns it.
 *
 * Returns: (transfer full): A new #GUPnPDIDLLiteContainer object. Unref after usage.
 **/
GUPnPDIDLLiteContainer *
gupnp_didl_lite_writer_add_container (GUPnPDIDLLiteWriter *writer)
{
        xmlNode *container_node;
        GUPnPDIDLLiteObject *object;

        g_return_val_if_fail (GUPNP_IS_DIDL_LITE_WRITER (writer), NULL);

        container_node = xmlNewChild (writer->priv->xml_node,
                                      NULL,
                                      (unsigned char *) "container",
                                      NULL);

        object = gupnp_didl_lite_object_new_from_xml (container_node,
                                                      writer->priv->xml_doc,
                                                      writer->priv->upnp_ns,
                                                      writer->priv->dc_ns,
                                                      writer->priv->dlna_ns);
        return GUPNP_DIDL_LITE_CONTAINER (object);
}

/**
 * gupnp_didl_lite_writer_add_descriptor:
 * @writer: A #GUPnPDIDLLiteWriter
 *
 * Creates a new descriptor, attaches it to @object and returns it.
 *
 * Returns: (transfer full): A new #GUPnPDIDLLiteDescriptor object. Unref after usage.
 **/
GUPnPDIDLLiteDescriptor *
gupnp_didl_lite_writer_add_descriptor (GUPnPDIDLLiteWriter *writer)
{
        xmlNode *desc_node;

        g_return_val_if_fail (GUPNP_IS_DIDL_LITE_WRITER (writer), NULL);

        desc_node = xmlNewChild (writer->priv->xml_node,
                                 NULL,
                                 (unsigned char *) "desc",
                                 NULL);

        return gupnp_didl_lite_descriptor_new_from_xml (desc_node,
                                                        writer->priv->xml_doc);
}

/**
 * gupnp_didl_lite_writer_get_string:
 * @writer: A #GUPnPDIDLLiteWriter
 *
 * Creates a string representation of the DIDL-Lite XML document.
 *
 * Return value: The DIDL-Lite XML string, or %NULL. #g_free after usage.
 **/
char *
gupnp_didl_lite_writer_get_string (GUPnPDIDLLiteWriter *writer)
{
        xmlBuffer *buffer;
        char      *ret;

        g_return_val_if_fail (GUPNP_IS_DIDL_LITE_WRITER (writer), NULL);

        buffer = xmlBufferCreate ();
        xmlNodeDump (buffer,
                     writer->priv->xml_doc->doc,
                     writer->priv->xml_node,
                     0,
                     0);
        ret = g_strndup ((char *) xmlBufferContent (buffer),
                         xmlBufferLength (buffer));
        xmlBufferFree (buffer);

        return ret;
}

/**
 * gupnp_didl_lite_writer_get_xml_node:
 * @writer: The #GUPnPDIDLLiteWriter
 *
 * Get the pointer to root node in XML document.
 *
 * Returns: (transfer none): The pointer to root node in XML document.
 **/
xmlNode *
gupnp_didl_lite_writer_get_xml_node (GUPnPDIDLLiteWriter *writer)
{
        g_return_val_if_fail (GUPNP_IS_DIDL_LITE_WRITER (writer), NULL);

        return writer->priv->xml_node;
}

/**
 * gupnp_didl_lite_writer_get_language:
 * @writer: #GUPnPDIDLLiteWriter
 *
 * Get the language the DIDL-Lite fragment is in.
 *
 * Returns: (transfer none): The language of the @writer, or %NULL.
 **/
const char *
gupnp_didl_lite_writer_get_language (GUPnPDIDLLiteWriter *writer)
{
        g_return_val_if_fail (GUPNP_IS_DIDL_LITE_WRITER (writer), NULL);

        return writer->priv->language;
}

/**
 * gupnp_didl_lite_writer_filter:
 * @writer: A #GUPnPDIDLLiteWriter
 * @filter: A filter string
 *
 * Clears the DIDL-Lite XML document of the properties not specified in the
 * @filter. The passed filter string would typically come from the 'Filter'
 * argument of Browse or Search actions from a ContentDirectory control point.
 * Please refer to Section 2.3.15 of UPnP AV ContentDirectory version 3
 * specification for details on this string.
 *
 * Return value: None.
 **/
void
gupnp_didl_lite_writer_filter (GUPnPDIDLLiteWriter *writer,
                               const char          *filter)
{
        char **tokens;
        GList *allowed = NULL;
        unsigned short i;
        xmlNode *node;

        g_return_if_fail (GUPNP_IS_DIDL_LITE_WRITER (writer));
        g_return_if_fail (filter != NULL);

        if (filter[0] == '*')
                return;         /* Wildcard */

        tokens = g_strsplit (filter, ",", -1);
        g_return_if_fail (tokens != NULL);

        for (i = 0; tokens[i] != NULL; i++)
                allowed = g_list_append (allowed, tokens[i]);

        for (node = writer->priv->xml_node->children;
             node != NULL;
             node = node->next)
                filter_node (node, allowed, writer);

        g_list_free (allowed);
        g_strfreev (tokens);
}