[gupnp-av/wip/didl-s: 3/4] Add dlna:lifetime property to items



commit b65d9dbd74c935663f7062fb3bee484740d082f9
Author: Jens Georg <mail jensge org>
Date:   Tue Oct 30 20:26:22 2012 +0100

    Add dlna:lifetime property to items

 libgupnp-av/Makefile.am                |    2 +
 libgupnp-av/gupnp-didl-lite-item.c     |   90 +++++++++++++++++++++++++++++++-
 libgupnp-av/gupnp-didl-lite-item.h     |    9 +++
 libgupnp-av/gupnp-didl-lite-resource.c |   34 +-----------
 4 files changed, 102 insertions(+), 33 deletions(-)
---
diff --git a/libgupnp-av/Makefile.am b/libgupnp-av/Makefile.am
index 8c5ea5c..0770705 100644
--- a/libgupnp-av/Makefile.am
+++ b/libgupnp-av/Makefile.am
@@ -74,6 +74,8 @@ libgupnp_av_1_0_la_SOURCES = gupnp-didl-lite-object.c \
 			     gvalue-util.h \
 			     fragment-util.c \
 			     fragment-util.h \
+			     time-utils.c \
+			     time-utils.h \
 			     xsd-data.c \
 			     xsd-data.h \
 			     $(BUILT_SOURCES)
diff --git a/libgupnp-av/gupnp-didl-lite-item.c b/libgupnp-av/gupnp-didl-lite-item.c
index 9e5d34c..80cc8b0 100644
--- a/libgupnp-av/gupnp-didl-lite-item.c
+++ b/libgupnp-av/gupnp-didl-lite-item.c
@@ -31,6 +31,7 @@
 
 #include "gupnp-didl-lite-item.h"
 #include "xml-util.h"
+#include "time-utils.h"
 
 G_DEFINE_TYPE (GUPnPDIDLLiteItem,
                gupnp_didl_lite_item,
@@ -38,7 +39,8 @@ G_DEFINE_TYPE (GUPnPDIDLLiteItem,
 
 enum {
         PROP_0,
-        PROP_REF_ID
+        PROP_REF_ID,
+        PROP_LIFETIME
 };
 
 static void
@@ -63,6 +65,11 @@ gupnp_didl_lite_item_get_property (GObject    *object,
                         (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;
@@ -85,6 +92,10 @@ gupnp_didl_lite_item_set_property (GObject      *object,
                 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;
@@ -117,6 +128,24 @@ gupnp_didl_lite_item_class_init (GUPnPDIDLLiteItemClass *klass)
                                       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));
 }
 
 /**
@@ -166,3 +195,62 @@ gupnp_didl_lite_item_set_ref_id (GUPnPDIDLLiteItem *item,
 
         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,
+                                   long               lifetime)
+{
+        xmlNode *node = NULL;
+        xmlNs *ns = NULL;
+        GUPnPXMLDoc *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, 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.
+ **/
+long
+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;
+}
diff --git a/libgupnp-av/gupnp-didl-lite-item.h b/libgupnp-av/gupnp-didl-lite-item.h
index 1719729..c839493 100644
--- a/libgupnp-av/gupnp-didl-lite-item.h
+++ b/libgupnp-av/gupnp-didl-lite-item.h
@@ -1,8 +1,10 @@
 /*
  * Copyright (C) 2009 Nokia Corporation.
+ * Copyright (C) 2012 Intel Corporation.
  *
  * Authors: Zeeshan Ali (Khattak) <zeeshan ali nokia com>
  *                                <zeeshanak gnome org>
+ *          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
@@ -73,6 +75,13 @@ void
 gupnp_didl_lite_item_set_ref_id         (GUPnPDIDLLiteItem *item,
                                          const char        *ref_id);
 
+void
+gupnp_didl_lite_item_set_lifetime       (GUPnPDIDLLiteItem *item,
+                                         long               lifetime);
+
+long
+gupnp_didl_lite_item_get_lifetime       (GUPnPDIDLLiteItem *item);
+
 G_END_DECLS
 
 #endif /* __GUPNP_DIDL_LITE_ITEM_H__ */
diff --git a/libgupnp-av/gupnp-didl-lite-resource.c b/libgupnp-av/gupnp-didl-lite-resource.c
index 8b4e064..4d79a0d 100644
--- a/libgupnp-av/gupnp-didl-lite-resource.c
+++ b/libgupnp-av/gupnp-didl-lite-resource.c
@@ -35,9 +35,7 @@
 
 #include "gupnp-didl-lite-resource.h"
 #include "xml-util.h"
-
-#define SEC_PER_MIN 60
-#define SEC_PER_HOUR 3600
+#include "time-utils.h"
 
 G_DEFINE_TYPE (GUPnPDIDLLiteResource,
                gupnp_didl_lite_resource,
@@ -106,31 +104,6 @@ return_point:
         g_strfreev (tokens);
 }
 
-static long
-seconds_from_time (const char *time_str)
-{
-        char **tokens;
-        gdouble seconds = -1;
-
-        if (time_str == NULL)
-                return -1;
-
-        tokens = g_strsplit (time_str, ":", -1);
-        if (tokens[0] == NULL ||
-            tokens[1] == NULL ||
-            tokens[2] == NULL)
-                goto return_point;
-
-        seconds = g_strtod (tokens[2], NULL);
-        seconds += g_strtod (tokens[1], NULL) * SEC_PER_MIN;
-        seconds += g_strtod (tokens[0], NULL) * SEC_PER_HOUR;
-
-return_point:
-        g_strfreev (tokens);
-
-        return (long) seconds;
-}
-
 static void
 on_protocol_info_changed (GUPnPProtocolInfo *info,
                           GParamSpec        *pspec,
@@ -1220,10 +1193,7 @@ gupnp_didl_lite_resource_set_duration (GUPnPDIDLLiteResource *resource,
         else {
                 char *str;
 
-                str = g_strdup_printf ("%ld:%.2ld:%.2ld.000",
-                                       duration / (60 * 60),
-                                       (duration / 60) % 60,
-                                       duration % 60);
+                str = seconds_to_time (duration);
                 xmlSetProp (resource->priv->xml_node,
                             (unsigned char *) "duration",
                             (unsigned char *) str);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]