[rygel/config: 1/6] Put user configuration access into a separate object.



commit 0311ee30d19e969cf726cb3ad1f2bb721ac6f838
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Thu Apr 16 17:30:48 2009 +0300

    Put user configuration access into a separate object.
---
 src/rygel/Makefile.am                     |    3 +
 src/rygel/rygel-configuration.vala        |   90 +++++++++++++++++++++++++
 src/rygel/rygel-media-server-factory.vala |  104 +++++-----------------------
 3 files changed, 112 insertions(+), 85 deletions(-)

diff --git a/src/rygel/Makefile.am b/src/rygel/Makefile.am
index f1a0074..19b89ad 100644
--- a/src/rygel/Makefile.am
+++ b/src/rygel/Makefile.am
@@ -28,6 +28,7 @@ BUILT_SOURCES = rygel-1.0.vapi \
 		rygel.h \
 		rygel-media-server.c \
 		rygel-media-server-factory.c \
+		rygel-configuration.c \
 		rygel-main.c \
 		rygel-content-directory.c \
 		rygel-browse.c \
@@ -58,6 +59,8 @@ rygel_SOURCES = $(VAPI_SOURCE_FILES) \
 		rygel-media-server.vala \
 		rygel-media-server-factory.c \
 		rygel-media-server-factory.vala \
+		rygel-configuration.c \
+		rygel-configuration.vala \
 		rygel-main.c \
 		rygel-main.vala \
 		rygel-content-directory.c \
diff --git a/src/rygel/rygel-configuration.vala b/src/rygel/rygel-configuration.vala
new file mode 100644
index 0000000..f160e71
--- /dev/null
+++ b/src/rygel/rygel-configuration.vala
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2008,2009 Nokia Corporation, all rights reserved.
+ * Copyright (C) 2008,2009 Zeeshan Ali (Khattak) <zeeshanak gnome org>.
+ *
+ * Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
+ *                               <zeeshan ali nokia com>
+ *
+ * This file is part of Rygel.
+ *
+ * Rygel is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Rygel 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+using GConf;
+using CStuff;
+
+/**
+ * User configuration for Rygel.
+ */
+public class Rygel.Configuration {
+    public static const string ROOT_GCONF_PATH = "/apps/rygel/";
+
+    private GConf.Client gconf;
+
+    public bool enable_xbox;
+    public string host_ip;
+    public int port;
+
+    public Configuration () {
+        this.gconf = GConf.Client.get_default ();
+
+        try {
+            this.enable_xbox = this.gconf.get_bool (ROOT_GCONF_PATH +
+                                                    "enable-xbox");
+            this.host_ip = this.gconf.get_string (ROOT_GCONF_PATH + "host-ip");
+            this.port = this.gconf.get_int (ROOT_GCONF_PATH + "port");
+        } catch (GLib.Error error) {
+            this.enable_xbox = false;
+            this.host_ip = null;
+            this.port = 0;
+        }
+    }
+
+    public string get_title (string section) {
+        return this.get_string (section, "Title", section);
+    }
+
+    public string get_udn (string section) {
+        var new_udn = Utils.generate_random_udn ();
+
+        return this.get_string (section, "UDN", new_udn);
+    }
+
+    private string get_string (string section,
+                               string key,
+                               string default_value) {
+        string val;
+        var path = ROOT_GCONF_PATH + section + "/" + key;
+
+        try {
+            val = this.gconf.get_string (path);
+        } catch (GLib.Error error) {
+            val = null;
+        }
+
+        if (val == null) {
+            val = default_value;
+        }
+
+        try {
+            this.gconf.set_string (path, val);
+        } catch (GLib.Error error) {
+            // No big deal
+        }
+
+        return val;
+    }
+}
+
diff --git a/src/rygel/rygel-media-server-factory.vala b/src/rygel/rygel-media-server-factory.vala
index 64504ec..1468eec 100644
--- a/src/rygel/rygel-media-server-factory.vala
+++ b/src/rygel/rygel-media-server-factory.vala
@@ -23,8 +23,8 @@
  */
 
 using GUPnP;
-using GConf;
 using CStuff;
+using Rygel;
 
 public errordomain MediaServerFactoryError {
     XML_PARSE
@@ -38,20 +38,18 @@ public class Rygel.MediaServerFactory {
     public static const string DESC_DOC = "xml/description.xml";
     public static const string XBOX_DESC_DOC = "xml/description-xbox360.xml";
     public static const string DESC_PREFIX = "Rygel";
-    public static const string ROOT_GCONF_PATH = "/apps/rygel/";
 
-    private GConf.Client gconf;
+    private Configuration config;
     private GUPnP.Context context;
 
     public MediaServerFactory () throws GLib.Error {
-        this.gconf = GConf.Client.get_default ();
+        this.config = new Configuration ();
 
         /* Set up GUPnP context */
         this.context = create_upnp_context ();
     }
 
     public MediaServer create_media_server (Plugin plugin) throws GLib.Error {
-        string gconf_path = ROOT_GCONF_PATH + plugin.name + "/";
         string modified_desc = DESC_PREFIX + "-" + plugin.name + ".xml";
 
         /* We store a modified description.xml in the user's config dir */
@@ -60,7 +58,7 @@ public class Rygel.MediaServerFactory {
                                      modified_desc);
 
         /* Create the description xml */
-        Xml.Doc *doc = this.create_desc (plugin, desc_path, gconf_path);
+        Xml.Doc *doc = this.create_desc (plugin, desc_path);
 
         /* Host our modified file */
         this.context.host_path (desc_path, "/" + modified_desc);
@@ -72,17 +70,10 @@ public class Rygel.MediaServerFactory {
     }
 
     private Xml.Doc * create_desc (Plugin plugin,
-                                   string desc_path,
-                                   string gconf_path) throws GLib.Error {
-        bool enable_xbox = false;
-        try {
-            enable_xbox = this.gconf.get_bool (gconf_path + "enable-xbox");
-        } catch (GLib.Error error) {
-            warning ("%s", error.message);
-        }
-
+                                   string desc_path) throws GLib.Error {
         string orig_desc_path;
-        if (enable_xbox)
+
+        if (this.config.enable_xbox)
             /* Use Xbox 360 specific description */
             orig_desc_path = Path.build_filename (BuildConfig.DATA_DIR,
                                                   XBOX_DESC_DOC);
@@ -98,9 +89,9 @@ public class Rygel.MediaServerFactory {
         }
 
         /* Modify description to include Plugin-specific stuff */
-        this.prepare_desc_for_plugin (doc, plugin, gconf_path);
+        this.prepare_desc_for_plugin (doc, plugin);
 
-        if (enable_xbox)
+        if (this.config.enable_xbox)
             /* Put/Set XboX specific stuff to description */
             add_xbox_specifics (doc);
 
@@ -110,25 +101,9 @@ public class Rygel.MediaServerFactory {
     }
 
     private GUPnP.Context create_upnp_context () throws GLib.Error {
-        string host_ip;
-        try {
-            host_ip = this.gconf.get_string (ROOT_GCONF_PATH + "host-ip");
-        } catch (GLib.Error error) {
-            warning ("%s", error.message);
-
-            host_ip = null;
-        }
-
-        int port;
-        try {
-            port = this.gconf.get_int (ROOT_GCONF_PATH + "port");
-        } catch (GLib.Error error) {
-            warning ("%s", error.message);
-
-            port = 0;
-        }
-
-        GUPnP.Context context = new GUPnP.Context (null, host_ip, port);
+        GUPnP.Context context = new GUPnP.Context (null,
+                                                   this.config.host_ip,
+                                                   this.config.port);
 
         /* Host UPnP dir */
         context.host_path (BuildConfig.DATA_DIR, "");
@@ -136,37 +111,6 @@ public class Rygel.MediaServerFactory {
         return context;
     }
 
-    private string get_str_from_gconf (string key,
-                                       string default_value) {
-        string str = null;
-
-        try {
-            str = this.gconf.get_string (key);
-        } catch (GLib.Error error) {
-            warning ("Error getting gconf key '%s': %s." +
-                     " Assuming default value '%s'.",
-                     key,
-                     error.message,
-                     default_value);
-
-            str = default_value;
-        }
-
-        if (str == null) {
-            str = default_value;
-
-            try {
-                this.gconf.set_string (key, default_value);
-            } catch (GLib.Error error) {
-                warning ("Error setting gconf key '%s': %s.",
-                        key,
-                        error.message);
-            }
-        }
-
-        return str;
-    }
-
     private void add_xbox_specifics (Xml.Doc doc) {
         Xml.Node *element;
 
@@ -184,9 +128,7 @@ public class Rygel.MediaServerFactory {
         element->add_content (": 1 : Windows Media Connect");
     }
 
-    private void prepare_desc_for_plugin (Xml.Doc doc,
-                                          Plugin  plugin,
-                                          string  gconf_path) {
+    private void prepare_desc_for_plugin (Xml.Doc doc, Plugin plugin) {
         Xml.Node *device_element;
 
         device_element = Utils.get_xml_element ((Xml.Node *) doc,
@@ -200,9 +142,7 @@ public class Rygel.MediaServerFactory {
         }
 
         /* First, set the Friendly name and UDN */
-        this.set_friendly_name_and_udn (device_element,
-                                        plugin.name,
-                                        gconf_path);
+        this.set_friendly_name_and_udn (device_element, plugin.name);
 
         /* Then list each icon */
         this.add_icons_to_desc (device_element, plugin);
@@ -216,21 +156,18 @@ public class Rygel.MediaServerFactory {
      * If these keys are not present in gconf, they are set with default values.
      */
     private void set_friendly_name_and_udn (Xml.Node *device_element,
-                                            string    plugin_name,
-                                            string    gconf_path) {
+                                            string    plugin_name) {
         /* friendlyName */
         Xml.Node *element = Utils.get_xml_element (device_element,
-                                         "friendlyName",
-                                         null);
+                                                   "friendlyName",
+                                                   null);
         if (element == null) {
             warning ("Element /root/device/friendlyName not found.");
 
             return;
         }
 
-        string str = get_str_from_gconf (gconf_path + "friendly-name",
-                                         plugin_name);
-        element->set_content (str);
+        element->set_content (this.config.get_title (plugin_name));
 
         /* UDN */
         element = Utils.get_xml_element (device_element, "UDN");
@@ -240,10 +177,7 @@ public class Rygel.MediaServerFactory {
             return;
         }
 
-        /* Generate new UUID */
-        string default_value = Utils.generate_random_udn ();
-        str = get_str_from_gconf (gconf_path + "UDN", default_value);
-        element->set_content (str);
+        element->set_content (this.config.get_udn (plugin_name));
     }
 
     private void add_services_to_desc (Xml.Node *device_element,



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