summaryrefslogtreecommitdiff
path: root/libgupnp-av/gupnp-didl-lite-item.c
blob: 35880528f6c4991f5bc393f9bc6fe313124d96e3 (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
/*
 * Copyright (C) 2009 Nokia Corporation.
 *
 * Authors: Zeeshan Ali (Khattak) <zeeshan.ali@nokia.com>
 *                                <zeeshanak@gnome.org>
 *
 * 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-item
 * @short_description: DIDL-Lite Item
 *
 * #GUPnPDIDLLiteItem respresents a DIDL-Lite item element.
 */

#include <string.h>

#include "gupnp-didl-lite-item.h"
#include "xml-util.h"
#include "time-utils.h"

G_DEFINE_TYPE (GUPnPDIDLLiteItem,
               gupnp_didl_lite_item,
               GUPNP_TYPE_DIDL_LITE_OBJECT);

enum {
        PROP_0,
        PROP_REF_ID,
        PROP_LIFETIME
};

static void
gupnp_didl_lite_item_init (G_GNUC_UNUSED GUPnPDIDLLiteItem *item)
{
        /* Nothing to initialize, yay! */
}

static void
gupnp_didl_lite_item_get_property (GObject    *object,
                                   guint       property_id,
                                   GValue     *value,
                                   GParamSpec *pspec)
{
        GUPnPDIDLLiteItem *item;

        item = GUPNP_DIDL_LITE_ITEM (object);

        switch (property_id) {
        case PROP_REF_ID:
                g_value_set_string
                        (value,
                         gupnp_didl_lite_item_get_ref_id (item));
                break;
        case PROP_LIFETIME:
                g_value_set_long
                        (value,
                         gupnp_didl_lite_item_get_lifetime (item));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                break;
        }
}

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

{
        GUPnPDIDLLiteItem *item;

        item = GUPNP_DIDL_LITE_ITEM (object);

        switch (property_id) {
        case PROP_REF_ID:
                gupnp_didl_lite_item_set_ref_id (item,
                                                 g_value_get_string (value));
                break;
        case PROP_LIFETIME:
                gupnp_didl_lite_item_set_lifetime (item,
                                                   g_value_get_long (value));
                break;
        default:
                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
                break;
        }
}

static void
gupnp_didl_lite_item_class_init (GUPnPDIDLLiteItemClass *klass)
{
        GObjectClass *object_class;

        object_class = G_OBJECT_CLASS (klass);

        object_class->get_property = gupnp_didl_lite_item_get_property;
        object_class->set_property = gupnp_didl_lite_item_set_property;

        /**
         * GUPnPDIDLLiteItem:ref-id:
         *
         * The ref ID of this item.
         **/
        g_object_class_install_property
                (object_class,
                 PROP_REF_ID,
                 g_param_spec_string ("ref-id",
                                      "RefID",
                                      "The ref ID of this item.",
                                      NULL,
                                      G_PARAM_READWRITE |
                                      G_PARAM_STATIC_NAME |
                                      G_PARAM_STATIC_NICK |
                                      G_PARAM_STATIC_BLURB));

        /**
         * GUPnPDIDLLiteItem:lifetime:
         *
         * The lifetime in seconds of this DIDLLite item in a media collection.
         **/
        g_object_class_install_property
                (object_class,
                 PROP_LIFETIME,
                 g_param_spec_long ("lifetime",
                                    "Lifetime",
                                    "The lifetime (in seconds) of this"
                                    " item.",
                                    -1,
                                    G_MAXLONG,
                                    -1,
                                    G_PARAM_READWRITE |
                                    G_PARAM_STATIC_STRINGS));
}

/**
 * gupnp_didl_lite_item_get_ref_id:
 * @item: #GUPnPDIDLLiteItem
 *
 * Get the ref ID of the @item.
 *
 * Return value: The ref ID of the @item, or %NULL.
 **/
const char *
gupnp_didl_lite_item_get_ref_id (GUPnPDIDLLiteItem *item)
{
        xmlNode *xml_node;

        g_return_val_if_fail (item != NULL, 0);
        g_return_val_if_fail (GUPNP_IS_DIDL_LITE_ITEM (item), NULL);

        xml_node = gupnp_didl_lite_object_get_xml_node
                                        (GUPNP_DIDL_LITE_OBJECT (item));

        return xml_util_get_attribute_content (xml_node, "refID");
}

/**
 * gupnp_didl_lite_item_set_ref_id:
 * @item: #GUPnPDIDLLiteItem
 * @ref_id: The ref ID
 *
 * Set the ref ID of the @item.
 **/
void
gupnp_didl_lite_item_set_ref_id (GUPnPDIDLLiteItem *item,
                                 const char        *ref_id)
{
        xmlNode *xml_node;

        g_return_if_fail (item != NULL);
        g_return_if_fail (GUPNP_IS_DIDL_LITE_ITEM (item));

        xml_node = gupnp_didl_lite_object_get_xml_node
                                (GUPNP_DIDL_LITE_OBJECT (item));

        xmlSetProp (xml_node,
                    (unsigned char *) "refID",
                    (unsigned char *) ref_id);

        g_object_notify (G_OBJECT (item), "ref-id");
}

/**
 * gupnp_didl_lite_item_set_lifetime:
 * @item: #GUPnPDIDLLiteItem
 * @lifetime: The lifetime (in seconds) of this item in a media collection.
 **/
void
gupnp_didl_lite_item_set_lifetime (GUPnPDIDLLiteItem *item,
                                   glong              lifetime)
{
        xmlNode *node = NULL;
        xmlNs *ns = NULL;
        GUPnPAVXMLDoc *doc = NULL;
        GUPnPDIDLLiteObject *object = NULL;

        g_return_if_fail (GUPNP_IS_DIDL_LITE_ITEM (item));

        object = GUPNP_DIDL_LITE_OBJECT (item);
        node = gupnp_didl_lite_object_get_xml_node (object);
        ns = gupnp_didl_lite_object_get_dlna_namespace (object);
        g_object_get (G_OBJECT (object), "xml-doc", &doc, NULL);

        if (lifetime < 0)
                xml_util_unset_child (node, "lifetime");
        else {
                char *str;

                str = seconds_to_time (lifetime);
                xml_util_set_child (node,
                                    GUPNP_XML_NAMESPACE_DLNA,
                                    &ns,
                                    doc->doc,
                                    "lifetime",
                                    str);
                g_free (str);
        }

        g_object_notify (G_OBJECT (object), "lifetime");
}

/**
 * gupnp_didl_lite_item_get_lifetime:
 * @item: #GUPnPDIDLLiteItem
 *
 * Returns: -1 if unset or the lifetime (in seconds) of the current item.
 **/
glong
gupnp_didl_lite_item_get_lifetime (GUPnPDIDLLiteItem *item)
{
        xmlNode *node = NULL;
        const char *lifetime_str;
        long lifetime;
        GUPnPDIDLLiteObject *object = NULL;

        g_return_val_if_fail (GUPNP_IS_DIDL_LITE_ITEM (item), -1);

        object = GUPNP_DIDL_LITE_OBJECT (item);
        node = gupnp_didl_lite_object_get_xml_node (object);

        lifetime_str = xml_util_get_child_element_content (node, "lifetime");
        lifetime = seconds_from_time (lifetime_str);

        return lifetime;
}