core: Re-introduce Configuration impl. as UserConfig
authorZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Mon, 8 Jun 2009 15:38:12 +0000 (18:38 +0300)
committerZeeshan Ali (Khattak) <zeeshanak@gnome.org>
Fri, 12 Jun 2009 11:31:42 +0000 (14:31 +0300)
Re-introduce old Configuration implementation as UserConfig class.

src/plugins/media-export/rygel-media-export-root-container.vala
src/plugins/mediathek/rygel-mediathek-root-container.vala
src/rygel/Makefile.am
src/rygel/rygel-root-device-factory.vala
src/rygel/rygel-transcode-manager.vala
src/rygel/rygel-user-config.vala [new file with mode: 0644]
src/ui/rygel-general-pref-section.vala
src/ui/rygel-media-export-pref-section.vala
src/ui/rygel-plugin-pref-section.vala
src/ui/rygel-preferences-dialog.vala
src/ui/rygel-preferences-section.vala

index 8d4cf2b..1200178 100644 (file)
@@ -92,7 +92,7 @@ public class Rygel.MediaExportRootContainer : MediaContainer {
 
         this.children = new ArrayList<MediaExportContainer> ();
 
-        var config = Rygel.Configuration.get_default ();
+        var config = Rygel.UserConfig.get_default ();
         var uris = config.get_string_list ("MediaExport", "uris");
 
         // either an error occured or the gconf key is not set
index ad0aa16..3688755 100644 (file)
@@ -100,7 +100,7 @@ public class Rygel.MediathekRootContainer : MediaContainer {
         this.session = new Soup.SessionAsync ();
         this.items = new ArrayList<MediathekRssContainer> ();
 
-        var config = Rygel.Configuration.get_default ();
+        var config = Rygel.UserConfig.get_default ();
         var feeds = config.get_int_list ("ZDFMediathek", "rss");
 
         if (feeds.size == 0) {
index ee05530..d88c611 100644 (file)
@@ -36,6 +36,7 @@ BUILT_SOURCES = rygel-1.0.vapi \
                rygel-root-device.c \
                rygel-root-device-factory.c \
                rygel-configuration.c \
+               rygel-user-config.c \
                rygel-main.c \
                rygel-dbus-service.c \
                rygel-content-directory.c \
@@ -119,6 +120,7 @@ VAPI_FILES = rygel-1.0.vapi
 DEPS_FILES = rygel-1.0.deps
 
 VAPI_SOURCE_FILES = rygel-configuration.vala \
+                   rygel-user-config.vala \
                    rygel-content-directory.vala \
                    rygel-connection-manager.vala \
                    rygel-transcode-manager.vala \
@@ -156,6 +158,7 @@ rygel-1.0.vapi: $(VAPI_SOURCE_FILES)
 noinst_LIBRARIES = librygel-configuration.a
 
 librygel_configuration_a_SOURCES = rygel-configuration.c \
+                                  rygel-user-config.c \
                                   cstuff.c \
                                    cstuff.h
 
index 556b358..0453004 100644 (file)
@@ -41,7 +41,7 @@ public class Rygel.RootDeviceFactory {
     private GUPnP.Context context;
 
     public RootDeviceFactory () throws GLib.Error {
-        this.config = Configuration.get_default ();
+        this.config = UserConfig.get_default ();
 
         /* Set up GUPnP context */
         this.context = create_upnp_context ();
index b53d7d6..4ac7006 100644 (file)
@@ -37,7 +37,7 @@ internal abstract class Rygel.TranscodeManager : GLib.Object {
     public TranscodeManager () {
         transcoders = new ArrayList<Transcoder> ();
 
-        var config = Configuration.get_default ();
+        var config = UserConfig.get_default ();
 
         if (config.transcoding) {
             if (config.lpcm_transcoder) {
diff --git a/src/rygel/rygel-user-config.vala b/src/rygel/rygel-user-config.vala
new file mode 100644 (file)
index 0000000..6a9934e
--- /dev/null
@@ -0,0 +1,359 @@
+/*
+ * 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;
+
+/**
+ * Manages the user configuration for Rygel.
+ */
+public class Rygel.UserConfig : GLib.Object, Configuration {
+    protected static const string ROOT_GCONF_PATH = "/apps/rygel/";
+    protected static const string IP_KEY = "host-ip";
+    protected static const string PORT_KEY = "port";
+    protected static const string ENABLED_KEY = "enabled";
+    protected static const string TITLE_KEY = "title";
+    protected static const string TRANSCODING_KEY = "enable-transcoding";
+    protected static const string MP3_TRANSCODER_KEY = "enable-mp3-transcoder";
+    protected static const string MP2TS_TRANSCODER_KEY =
+                                                    "enable-mp2ts-transcoder";
+    protected static const string LPCM_TRANSCODER_KEY =
+                                                    "enable-lpcm-transcoder";
+
+    private const string DBUS_SERVICE = "org.freedesktop.DBus";
+    private const string DBUS_PATH = "/org/freedesktop/DBus";
+    private const string DBUS_INTERFACE = "org.freedesktop.DBus";
+
+    private const string RYGEL_SERVICE = "org.gnome.Rygel";
+    private const string RYGEL_PATH = "/org/gnome/Rygel";
+    private const string RYGEL_INTERFACE = "org.gnome.Rygel";
+
+    // Our singleton
+    private static UserConfig config;
+
+    protected GConf.Client gconf;
+
+    private dynamic DBus.Object dbus_obj;
+    private dynamic DBus.Object rygel_obj;
+
+    public bool upnp_enabled {
+        get {
+            return this.get_bool ("general", ENABLED_KEY, true);
+        }
+        set {
+            if (value != this.upnp_enabled) {
+                this.enable_upnp (value);
+            }
+        }
+    }
+
+    private string _host_ip;
+    public string host_ip {
+        get {
+            _host_ip = this.get_string ("general", IP_KEY, null);
+            return _host_ip;
+        }
+        set {
+            this.set_string ("general", IP_KEY, value);
+        }
+    }
+
+    public int port {
+        get {
+            return this.get_int ("general",
+                                 PORT_KEY,
+                                 uint16.MIN,
+                                 uint16.MAX,
+                                 0);
+        }
+        set {
+            this.set_int ("general", PORT_KEY, value);
+        }
+    }
+
+    public bool transcoding {
+        get {
+            return this.get_bool ("general", TRANSCODING_KEY, true);
+        }
+        set {
+            this.set_bool ("general", TRANSCODING_KEY, value);
+        }
+    }
+
+    public bool mp3_transcoder {
+        get {
+            return this.get_bool ("general", MP3_TRANSCODER_KEY, true);
+        }
+        set {
+            this.set_bool ("general", MP3_TRANSCODER_KEY, value);
+        }
+    }
+
+    public bool mp2ts_transcoder {
+        get {
+            return this.get_bool ("general", MP2TS_TRANSCODER_KEY, true);
+        }
+        set {
+            this.set_bool ("general", MP2TS_TRANSCODER_KEY, value);
+        }
+    }
+
+    public bool lpcm_transcoder {
+        get {
+            return this.get_bool ("general", LPCM_TRANSCODER_KEY, true);
+        }
+        set {
+            this.set_bool ("general", LPCM_TRANSCODER_KEY, value);
+        }
+    }
+
+    public static UserConfig get_default () {
+        if (config == null) {
+            config = new UserConfig ();
+        }
+
+        return config;
+    }
+
+    public UserConfig () {
+        this.gconf = GConf.Client.get_default ();
+
+        DBus.Connection connection = DBus.Bus.get (DBus.BusType.SESSION);
+
+        // Create proxy to Rygel
+        this.rygel_obj = connection.get_object (RYGEL_SERVICE,
+                                                RYGEL_PATH,
+                                                RYGEL_INTERFACE);
+        // and DBus
+        this.dbus_obj = connection.get_object (DBUS_SERVICE,
+                                               DBUS_PATH,
+                                               DBUS_INTERFACE);
+    }
+
+    public bool get_enabled (string section) {
+        return this.get_bool (section, ENABLED_KEY, true);
+    }
+
+    public string get_title (string section, string default_title) {
+        return this.get_string (section, TITLE_KEY, default_title);
+    }
+
+    public 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 == "") {
+            val = default_value;
+        }
+
+        return val;
+    }
+
+    public Gee.ArrayList<string> get_string_list (string section,
+                                                  string key) {
+        var str_list = new Gee.ArrayList<string> ();
+        var path = ROOT_GCONF_PATH + section + "/" + key;
+
+        try {
+            unowned SList<string> strings = this.gconf.get_list (
+                                                        path,
+                                                        GConf.ValueType.STRING);
+            if (strings != null) {
+                foreach (var str in strings) {
+                    str_list.add (str);
+                }
+            }
+        } catch (GLib.Error error) {
+            warning ("Failed to get value for key: %s\n", path);
+        }
+
+        return str_list;
+    }
+
+    public int get_int (string section,
+                        string key,
+                        int    min,
+                        int    max,
+                        int    default_value) {
+        int val;
+        var path = ROOT_GCONF_PATH + section + "/" + key;
+
+        try {
+            val = this.gconf.get_int (path);
+        } catch (GLib.Error error) {
+            val = default_value;
+        }
+
+        if (val < min || val > max) {
+            val = default_value;
+        }
+
+        return val;
+    }
+
+    public Gee.ArrayList<int> get_int_list (string section,
+                                            string key) {
+        var int_list = new Gee.ArrayList<int> ();
+        var path = ROOT_GCONF_PATH + section + "/" + key;
+
+        try {
+            unowned SList<int> ints = this.gconf.get_list (
+                                                    path,
+                                                    GConf.ValueType.INT);
+            if (ints != null) {
+                foreach (var num in ints) {
+                    int_list.add (num);
+                }
+            }
+        } catch (GLib.Error error) {
+            warning ("Failed to get value for key: %s", path);
+        }
+
+        return int_list;
+    }
+
+    public bool get_bool (string section,
+                          string key,
+                          bool   default_value) {
+        bool val;
+        var path = ROOT_GCONF_PATH + section + "/" + key;
+
+        try {
+            unowned GConf.Value value = this.gconf.get (path);
+            if (value != null) {
+                val = value.get_bool ();
+            } else {
+                val = default_value;
+            }
+        } catch (GLib.Error error) {
+            val = default_value;
+        }
+
+        return val;
+    }
+
+    public void set_string (string section,
+                            string key,
+                            string value) {
+        var path = ROOT_GCONF_PATH + section + "/" + key;
+
+        try {
+            this.gconf.set_string (path, value);
+        } catch (GLib.Error error) {
+            // No big deal
+        }
+    }
+
+    public void set_string_list (string                section,
+                                 string                key,
+                                 Gee.ArrayList<string> str_list) {
+        var path = ROOT_GCONF_PATH + section + "/" + key;
+
+        // GConf requires us to provide it GLib.SList
+        SList<string> slist = null;
+
+        foreach (var str in str_list) {
+            if (str != "") {
+                slist.append (str);
+            }
+        }
+
+        try {
+            this.gconf.set_list (path, GConf.ValueType.STRING, slist);
+        } catch (GLib.Error error) {
+            // No big deal
+        }
+    }
+
+    public void set_int (string section,
+                         string key,
+                         int    value) {
+        var path = ROOT_GCONF_PATH + section + "/" + key;
+
+        try {
+            this.gconf.set_int (path, value);
+        } catch (GLib.Error error) {
+            // No big deal
+        }
+    }
+
+    public void set_bool (string section,
+                          string key,
+                          bool   value) {
+        var path = ROOT_GCONF_PATH + section + "/" + key;
+
+        try {
+            this.gconf.set_bool (path, value);
+        } catch (GLib.Error error) {
+            // No big deal
+        }
+    }
+
+    private void enable_upnp (bool enable) {
+        var dest_path = Path.build_filename (Environment.get_user_config_dir (),
+                                             "autostart",
+                                             "rygel.desktop");
+        var dest = File.new_for_path (dest_path);
+
+        try {
+            if (enable) {
+                uint32 res;
+
+                // Start service first
+                this.dbus_obj.StartServiceByName (RYGEL_SERVICE,
+                        (uint32) 0,
+                        out res);
+
+                // Then symlink the desktop file to user's autostart dir
+                var source_path = Path.build_filename (
+                        BuildConfig.DESKTOP_DIR,
+                        "rygel.desktop");
+                dest.make_symbolic_link (source_path, null);
+
+                this.set_bool ("general", ENABLED_KEY, true);
+            } else {
+                // Stop service first
+                this.rygel_obj.Shutdown ();
+
+                // Then delete the symlink from user's autostart dir
+                dest.delete (null);
+
+                this.set_bool ("general", ENABLED_KEY, false);
+            }
+        } catch (DBus.Error err) {
+            warning ("Failed to %s Rygel service: %s\n",
+                     enable? "start": "stop",
+                     err.message);
+        }
+    }
+}
+
index 4c77abe..705f13c 100644 (file)
@@ -41,8 +41,8 @@ public class Rygel.GeneralPrefSection : PreferencesSection {
     private CheckButton mp2ts_check;
     private CheckButton lpcm_check;
 
-    public GeneralPrefSection (Builder       builder,
-                               Configuration config) throws Error {
+    public GeneralPrefSection (Builder    builder,
+                               UserConfig config) throws Error {
         base (config, "general");
 
         this.upnp_check = (CheckButton) builder.get_object (UPNP_CHECKBUTTON);
index 55fa7be..b784d3a 100644 (file)
@@ -37,8 +37,8 @@ public class Rygel.MediaExportPrefSection : Rygel.PluginPrefSection {
     private ListStore liststore;
     private FileChooserDialog dialog;
 
-    public MediaExportPrefSection (Builder       builder,
-                                   Configuration config) {
+    public MediaExportPrefSection (Builder    builder,
+                                   UserConfig config) {
         base (builder, config, NAME);
 
         this.treeview = (TreeView) builder.get_object (URIS_TEXTVIEW);
index 4c4f61f..ce597b6 100644 (file)
@@ -29,9 +29,9 @@ public class Rygel.PluginPrefSection : PreferencesSection {
     private CheckButton enabled_check;
     private Entry title_entry;
 
-    public PluginPrefSection (Builder       builder,
-                              Configuration config,
-                              string        name) {
+    public PluginPrefSection (Builder    builder,
+                              UserConfig config,
+                              string     name) {
         base (config, name);
 
         this.enabled_check = (CheckButton) builder.get_object (name.down () +
@@ -53,13 +53,13 @@ public class Rygel.PluginPrefSection : PreferencesSection {
 
     public override void save () {
         this.config.set_bool (this.name,
-                              Configuration.ENABLED_KEY,
+                              UserConfig.ENABLED_KEY,
                               this.enabled_check.active);
 
         var title = this.title_entry.get_text ().replace ("%n", "@REALNAME@");
         title = title.replace ("%u", "@USERNAME@");
         title = title.replace ("%h", "@HOSTNAME@");
-        this.config.set_string (this.name, Configuration.TITLE_KEY, title);
+        this.config.set_string (this.name, UserConfig.TITLE_KEY, title);
     }
 
     protected virtual void on_enabled_check_toggled (
index 1167c2f..fa66dfa 100644 (file)
@@ -33,7 +33,7 @@ public class Rygel.PreferencesDialog : GLib.Object {
     ArrayList<PreferencesSection> sections;
 
     public PreferencesDialog () throws Error {
-        var config = Configuration.get_default ();
+        var config = UserConfig.get_default ();
 
         this.builder = new Builder ();
 
index 0c340b3..021ad3f 100644 (file)
 using Gtk;
 
 public abstract class Rygel.PreferencesSection : GLib.Object {
-    protected Configuration config;
+    protected UserConfig config;
 
     public string name;
 
-    public PreferencesSection (Configuration config,
-                               string        name) {
+    public PreferencesSection (UserConfig config,
+                               string     name) {
         this.name = name;
         this.config = config;
     }