[rygel] ui,core,i18n: Move config writing to UI



commit 387678420c91465a076e270e9f8eba77d5ed05e5
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Tue Jan 11 19:08:08 2011 +0200

    ui,core,i18n: Move config writing to UI
    
    Core and plugins didn't do any user configuration writing, so better
    separate out all writing part to a subclass in UI code.

 po/POTFILES.in                              |    1 +
 po/POTFILES.skip                            |    1 +
 src/rygel/rygel-user-config.vala            |  177 +---------------------
 src/ui/Makefile.am                          |    6 +-
 src/ui/rygel-general-pref-section.vala      |    4 +-
 src/ui/rygel-media-export-pref-section.vala |    4 +-
 src/ui/rygel-plugin-pref-section.vala       |    6 +-
 src/ui/rygel-preferences-dialog.vala        |    4 +-
 src/ui/rygel-preferences-section.vala       |    6 +-
 src/ui/rygel-tracker-pref-section.vala      |    2 +-
 src/ui/rygel-writable-user-config.vala      |  218 +++++++++++++++++++++++++++
 11 files changed, 238 insertions(+), 191 deletions(-)
---
diff --git a/po/POTFILES.in b/po/POTFILES.in
index c97e162..96badf2 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -152,3 +152,4 @@ src/ui/rygel-plugin-pref-section.vala
 src/ui/rygel-preferences-dialog.vala
 src/ui/rygel-preferences-section.vala
 src/ui/rygel-tracker-pref-section.vala
+src/ui/rygel-writable-user-config.vala
diff --git a/po/POTFILES.skip b/po/POTFILES.skip
index dd52d65..709ba2d 100644
--- a/po/POTFILES.skip
+++ b/po/POTFILES.skip
@@ -88,6 +88,7 @@ src/rygel/rygel-xbox-hacks.c
 src/rygel/rygel-changelog.c
 src/rygel/rygel-media-query-action.c
 src/ui/rygel-preferences-dialog.c
+src/ui/rygel-writable-user-config.c
 data/rygel.desktop.in
 data/rygel-preferences.desktop.in
 data/xml/AVTransport2.xml
diff --git a/src/rygel/rygel-user-config.vala b/src/rygel/rygel-user-config.vala
index 101576c..fb8f0a3 100644
--- a/src/rygel/rygel-user-config.vala
+++ b/src/rygel/rygel-user-config.vala
@@ -43,104 +43,51 @@ public class Rygel.UserConfig : GLib.Object, Configuration {
     public static const string LOG_LEVEL_KEY = "log-level";
     public static const string PLUGIN_PATH_KEY = "plugin-path";
 
-    private const string RYGEL_SERVICE = "org.gnome.Rygel1";
-    private const string RYGEL_PATH = "/org/gnome/Rygel1";
-    private const string RYGEL_INTERFACE = "org.gnome.Rygel1";
-
     // Our singleton
     private static UserConfig config;
 
     protected KeyFile key_file;
-    private bool read_only;
 
     public bool get_upnp_enabled () throws GLib.Error {
         return this.get_bool ("general", ENABLED_KEY);
     }
 
-    public void set_upnp_enabled (bool value) {
-        bool enabled = false;
-
-        try {
-            enabled = this.get_upnp_enabled ();
-        } catch (GLib.Error err) {}
-
-        if (value != enabled) {
-            this.enable_upnp (value);
-        }
-    }
-
     public string get_interface () throws GLib.Error {
         return this.get_string ("general", IFACE_KEY);
     }
 
-    public void set_interface (string value) {
-        this.set_string ("general", IFACE_KEY, value);
-    }
-
     public int get_port () throws GLib.Error {
         return this.get_int ("general", PORT_KEY, uint16.MIN, uint16.MAX);
     }
 
-    public void set_port (int value) {
-        this.set_int ("general", PORT_KEY, value);
-    }
-
     public bool get_transcoding () throws GLib.Error {
         return this.get_bool ("general", TRANSCODING_KEY);
     }
 
-    public void set_transcoding (bool value) {
-        this.set_bool ("general", TRANSCODING_KEY, value);
-    }
-
     public bool get_mp3_transcoder () throws GLib.Error {
         return this.get_bool ("general", MP3_TRANSCODER_KEY);
     }
 
-    public void set_mp3_transcoder (bool value) {
-        this.set_bool ("general", MP3_TRANSCODER_KEY, value);
-    }
-
     public bool get_mp2ts_transcoder () throws GLib.Error {
         return this.get_bool ("general", MP2TS_TRANSCODER_KEY);
     }
 
-    public void set_mp2ts_transcoder (bool value) {
-        this.set_bool ("general", MP2TS_TRANSCODER_KEY, value);
-    }
-
     public bool get_lpcm_transcoder () throws GLib.Error {
         return this.get_bool ("general", LPCM_TRANSCODER_KEY);
     }
 
-    public void set_lpcm_transcoder (bool value) {
-        this.set_bool ("general", LPCM_TRANSCODER_KEY, value);
-    }
-
     public bool get_wmv_transcoder () throws GLib.Error {
         return this.get_bool ("general", WMV_TRANSCODER_KEY);
     }
 
-    public void set_wmv_transcoder (bool value) {
-        this.set_bool ("general", WMV_TRANSCODER_KEY, value);
-    }
-
     public bool get_allow_upload () throws GLib.Error {
         return this.get_bool ("general", ALLOW_UPLOAD_KEY);
     }
 
-    public void set_allow_upload (bool value) throws GLib.Error {
-        this.set_bool ("general", ALLOW_UPLOAD_KEY, value);
-    }
-
     public bool get_allow_deletion () throws GLib.Error {
         return this.get_bool ("general", ALLOW_DELETION_KEY);
     }
 
-    public void set_allow_deletion (bool value) throws GLib.Error {
-        this.set_bool ("general", ALLOW_DELETION_KEY, value);
-    }
-
     public LogLevel get_log_level () throws GLib.Error {
         return (LogLevel) this.get_int ("general",
                                         LOG_LEVEL_KEY,
@@ -160,8 +107,7 @@ public class Rygel.UserConfig : GLib.Object, Configuration {
         return config;
     }
 
-    public UserConfig (bool read_only=true) throws Error {
-        this.read_only = read_only;
+    public UserConfig () throws Error {
         this.key_file = new KeyFile ();
 
         var dirs = new string[2];
@@ -177,25 +123,6 @@ public class Rygel.UserConfig : GLib.Object, Configuration {
         debug ("Loaded user configuration from file '%s'", path);
     }
 
-    public void save () {
-        return_if_fail (!this.read_only);
-
-        // Always write to user's config
-        string path = Path.build_filename (Environment.get_user_config_dir (),
-                                           CONFIG_FILE);
-
-        size_t length;
-        var data = this.key_file.to_data (out length);
-
-        try {
-            FileUtils.set_contents (path, data, (long) length);
-        } catch (FileError err) {
-            critical (_("Failed to save configuration data to file '%s': %s"),
-                      path,
-                      err.message);
-        }
-    }
-
     public bool get_enabled (string section) throws GLib.Error {
         return this.get_bool (section, ENABLED_KEY);
     }
@@ -262,107 +189,5 @@ public class Rygel.UserConfig : GLib.Object, Configuration {
                           throws GLib.Error {
         return this.key_file.get_boolean (section, key);
     }
-
-    public void set_string (string section,
-                            string key,
-                            string value) {
-        this.key_file.set_string (section, key, value);
-    }
-
-    public void set_string_list (string                section,
-                                 string                key,
-                                 Gee.ArrayList<string> str_list) {
-        // GConf requires us to provide it GLib.SList
-        var strings = new string[str_list.size];
-        int i = 0;
-
-        foreach (var str in str_list) {
-            if (str != "") {
-                strings[i++] = str;
-            }
-        }
-
-        this.key_file.set_string_list (section, key, strings);
-    }
-
-    public void set_int (string section,
-                         string key,
-                         int    value) {
-        this.key_file.set_integer (section, key, value);
-    }
-
-    public void set_bool (string section,
-                          string key,
-                          bool   value) {
-        this.key_file.set_boolean (section, key, value);
-    }
-
-    private void enable_upnp (bool enable) {
-        try {
-            var config_dir = Environment.get_user_config_dir ();
-            this.ensure_dir_exists (config_dir);
-            var dest_dir = Path.build_filename (config_dir, "autostart");
-            this.ensure_dir_exists (dest_dir);
-
-            var dest_path = Path.build_filename (dest_dir, "rygel.desktop");
-            var dest = File.new_for_path (dest_path);
-
-            if (enable) {
-                // Creating the proxy starts the service
-                DBusObject dbus = Bus.get_proxy_sync
-                                        (BusType.SESSION,
-                                         DBUS_SERVICE,
-                                         DBUS_OBJECT,
-                                         DBusProxyFlags.DO_NOT_LOAD_PROPERTIES);
-                dbus.start_service_by_name (DBusInterface.SERVICE_NAME, 0);
-
-                // Then symlink the desktop file to user's autostart dir
-                var source_path = Path.build_filename (BuildConfig.DESKTOP_DIR,
-                                                       "rygel.desktop");
-                try {
-                    dest.make_symbolic_link (source_path, null);
-                } catch (IOError.EXISTS err) {}
-
-                this.set_bool ("general", ENABLED_KEY, true);
-            } else {
-                // Stop service only if already running
-                if (this.get_enabled ("general")) {
-                    // Create proxy to Rygel
-                    DBusInterface rygel_proxy = Bus.get_proxy_sync
-                                        (BusType.SESSION,
-                                         DBusInterface.SERVICE_NAME,
-                                         DBusInterface.OBJECT_PATH,
-                                         DBusProxyFlags.DO_NOT_LOAD_PROPERTIES);
-
-                    rygel_proxy.shutdown ();
-                }
-
-                // Then delete the symlink from user's autostart dir
-                try {
-                    dest.delete (null);
-                } catch (IOError.NOT_FOUND err) {}
-
-                this.set_bool ("general", ENABLED_KEY, false);
-            }
-        } catch (GLib.Error err) {
-            string message;
-
-            if (enable) {
-                message = _("Failed to start Rygel service: %s");
-            } else {
-                message = _("Failed to stop Rygel service: %s");
-            }
-
-            warning (message, err.message);
-        }
-    }
-
-    private void ensure_dir_exists (string dir_path) throws GLib.Error {
-        var dir = File.new_for_path (dir_path);
-
-        try {
-            dir.make_directory (null);
-        } catch (IOError.EXISTS err) { /* Thats OK */ }
-    }
 }
 
diff --git a/src/ui/Makefile.am b/src/ui/Makefile.am
index 6c77cad..b6cac2a 100644
--- a/src/ui/Makefile.am
+++ b/src/ui/Makefile.am
@@ -17,7 +17,8 @@ AM_CFLAGS = $(LIBGUPNP_CFLAGS) \
 	    $(UUID_CFLAGS) \
 	    $(LIBSOUP_CFLAGS) \
 	    -I$(top_srcdir) -I$(rygeldir) -DDATA_DIR='"$(shareddir)"' \
-	    -DSMALL_ICON_DIR='"$(icondir)"' -include config.h \
+	    -DSMALL_ICON_DIR='"$(icondir)"' -DSYS_CONFIG_DIR='"$(sysconfdir)"' \
+	    -DDESKTOP_DIR='"$(desktopdir)"' -include config.h \
 	    -DLOCALEDIR=\""$(datadir)/locale"\"
 
 bin_PROGRAMS = rygel-preferences
@@ -28,7 +29,8 @@ rygel_preferences_SOURCES = \
 			    rygel-general-pref-section.vala \
 			    rygel-plugin-pref-section.vala \
 			    rygel-media-export-pref-section.vala \
-			    rygel-tracker-pref-section.vala
+			    rygel-tracker-pref-section.vala \
+			    rygel-writable-user-config.vala
 
 rygel.stamp: $(rygel_preferences_VALASOURCES)
 rygel_preferences_VALAFLAGS = \
diff --git a/src/ui/rygel-general-pref-section.vala b/src/ui/rygel-general-pref-section.vala
index 07ad69c..03eecec 100644
--- a/src/ui/rygel-general-pref-section.vala
+++ b/src/ui/rygel-general-pref-section.vala
@@ -44,8 +44,8 @@ public class Rygel.GeneralPrefSection : PreferencesSection {
 
     private ContextManager context_manager;
 
-    public GeneralPrefSection (Builder    builder,
-                               UserConfig config) throws Error {
+    public GeneralPrefSection (Builder            builder,
+                               WritableUserConfig config) throws Error {
         base (config, "general");
 
         this.upnp_check = (CheckButton) builder.get_object (UPNP_CHECKBUTTON);
diff --git a/src/ui/rygel-media-export-pref-section.vala b/src/ui/rygel-media-export-pref-section.vala
index 2358f4c..37a4e3b 100644
--- a/src/ui/rygel-media-export-pref-section.vala
+++ b/src/ui/rygel-media-export-pref-section.vala
@@ -38,8 +38,8 @@ public class Rygel.MediaExportPrefSection : Rygel.PluginPrefSection {
     private ListStore liststore;
     private FileChooserDialog dialog;
 
-    public MediaExportPrefSection (Builder    builder,
-                                   UserConfig config) {
+    public MediaExportPrefSection (Builder            builder,
+                                   WritableUserConfig config) {
         base (builder, config, NAME);
 
         this.treeview = (TreeView) builder.get_object (URIS_TEXTVIEW);
diff --git a/src/ui/rygel-plugin-pref-section.vala b/src/ui/rygel-plugin-pref-section.vala
index 328da97..c048128 100644
--- a/src/ui/rygel-plugin-pref-section.vala
+++ b/src/ui/rygel-plugin-pref-section.vala
@@ -33,9 +33,9 @@ public class Rygel.PluginPrefSection : PreferencesSection {
 
     protected ArrayList<Widget> widgets; // All widgets in this section
 
-    public PluginPrefSection (Builder    builder,
-                              UserConfig config,
-                              string     name) {
+    public PluginPrefSection (Builder            builder,
+                              WritableUserConfig config,
+                              string             name) {
         base (config, name);
 
         this.widgets = new ArrayList<Widget> ();
diff --git a/src/ui/rygel-preferences-dialog.vala b/src/ui/rygel-preferences-dialog.vala
index b94a707..2f1e175 100644
--- a/src/ui/rygel-preferences-dialog.vala
+++ b/src/ui/rygel-preferences-dialog.vala
@@ -28,13 +28,13 @@ public class Rygel.PreferencesDialog : GLib.Object {
     const string DIALOG = "preferences-dialog";
     const string ICON = BuildConfig.SMALL_ICON_DIR + "/rygel.png";
 
-    UserConfig config;
+    WritableUserConfig config;
     Builder builder;
     Dialog dialog;
     ArrayList<PreferencesSection> sections;
 
     public PreferencesDialog () throws Error {
-        this.config = new UserConfig (false);
+        this.config = new WritableUserConfig ();
         this.builder = new Builder ();
 
         this.builder.add_from_file (UI_FILE);
diff --git a/src/ui/rygel-preferences-section.vala b/src/ui/rygel-preferences-section.vala
index 409a0e3..e42214a 100644
--- a/src/ui/rygel-preferences-section.vala
+++ b/src/ui/rygel-preferences-section.vala
@@ -23,12 +23,12 @@
 using Gtk;
 
 public abstract class Rygel.PreferencesSection : GLib.Object {
-    protected UserConfig config;
+    protected WritableUserConfig config;
 
     public string name;
 
-    public PreferencesSection (UserConfig config,
-                               string     name) {
+    public PreferencesSection (WritableUserConfig config,
+                               string             name) {
         this.name = name;
         this.config = config;
     }
diff --git a/src/ui/rygel-tracker-pref-section.vala b/src/ui/rygel-tracker-pref-section.vala
index ad07b49..a2d56ed 100644
--- a/src/ui/rygel-tracker-pref-section.vala
+++ b/src/ui/rygel-tracker-pref-section.vala
@@ -37,7 +37,7 @@ public class Rygel.TrackerPrefSection : Rygel.PluginPrefSection {
     private CheckButton music_check;
     private CheckButton pictures_check;
 
-    public TrackerPrefSection (Builder builder, UserConfig config) {
+    public TrackerPrefSection (Builder builder, WritableUserConfig config) {
         base (builder, config, NAME);
 
         this.videos_check = (CheckButton) builder.get_object (VIDEOS_CHECK);
diff --git a/src/ui/rygel-writable-user-config.vala b/src/ui/rygel-writable-user-config.vala
new file mode 100644
index 0000000..d837c20
--- /dev/null
+++ b/src/ui/rygel-writable-user-config.vala
@@ -0,0 +1,218 @@
+/*
+ * Copyright (C) 2008-2011 Nokia Corporation.
+ * 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 FreeDesktop;
+
+/**
+ * Manages the user configuration for Rygel.
+ */
+public class Rygel.WritableUserConfig : Rygel.UserConfig {
+    private const string RYGEL_SERVICE = "org.gnome.Rygel1";
+    private const string RYGEL_PATH = "/org/gnome/Rygel1";
+    private const string RYGEL_INTERFACE = "org.gnome.Rygel1";
+
+    public void set_upnp_enabled (bool value) {
+        bool enabled = false;
+
+        try {
+            enabled = this.get_upnp_enabled ();
+        } catch (GLib.Error err) {}
+
+        if (value != enabled) {
+            this.enable_upnp (value);
+        }
+    }
+
+    public void set_interface (string value) {
+        this.set_string ("general", IFACE_KEY, value);
+    }
+
+    public void set_port (int value) {
+        this.set_int ("general", PORT_KEY, value);
+    }
+
+    public void set_transcoding (bool value) {
+        this.set_bool ("general", TRANSCODING_KEY, value);
+    }
+
+    public void set_mp3_transcoder (bool value) {
+        this.set_bool ("general", MP3_TRANSCODER_KEY, value);
+    }
+
+    public void set_mp2ts_transcoder (bool value) {
+        this.set_bool ("general", MP2TS_TRANSCODER_KEY, value);
+    }
+
+    public void set_lpcm_transcoder (bool value) {
+        this.set_bool ("general", LPCM_TRANSCODER_KEY, value);
+    }
+
+    public void set_wmv_transcoder (bool value) {
+        this.set_bool ("general", WMV_TRANSCODER_KEY, value);
+    }
+
+    public void set_allow_upload (bool value) throws GLib.Error {
+        this.set_bool ("general", ALLOW_UPLOAD_KEY, value);
+    }
+
+    public void set_allow_deletion (bool value) throws GLib.Error {
+        this.set_bool ("general", ALLOW_DELETION_KEY, value);
+    }
+
+    public WritableUserConfig () throws Error {
+        this.key_file = new KeyFile ();
+
+        var dirs = new string[2];
+        dirs[0] = Environment.get_user_config_dir ();
+        dirs[1] = BuildConfig.SYS_CONFIG_DIR;
+
+        string path;
+        this.key_file.load_from_dirs (CONFIG_FILE,
+                                      dirs,
+                                      out path,
+                                      KeyFileFlags.KEEP_COMMENTS |
+                                      KeyFileFlags.KEEP_TRANSLATIONS);
+        debug ("Loaded user configuration from file '%s'", path);
+    }
+
+    public void save () {
+        // Always write to user's config
+        string path = Path.build_filename (Environment.get_user_config_dir (),
+                                           CONFIG_FILE);
+
+        size_t length;
+        var data = this.key_file.to_data (out length);
+
+        try {
+            FileUtils.set_contents (path, data, (long) length);
+        } catch (FileError err) {
+            critical (_("Failed to save configuration data to file '%s': %s"),
+                      path,
+                      err.message);
+        }
+    }
+
+    public void set_string (string section,
+                            string key,
+                            string value) {
+        this.key_file.set_string (section, key, value);
+    }
+
+    public void set_string_list (string                section,
+                                 string                key,
+                                 Gee.ArrayList<string> str_list) {
+        // GConf requires us to provide it GLib.SList
+        var strings = new string[str_list.size];
+        int i = 0;
+
+        foreach (var str in str_list) {
+            if (str != "") {
+                strings[i++] = str;
+            }
+        }
+
+        this.key_file.set_string_list (section, key, strings);
+    }
+
+    public void set_int (string section,
+                         string key,
+                         int    value) {
+        this.key_file.set_integer (section, key, value);
+    }
+
+    public void set_bool (string section,
+                          string key,
+                          bool   value) {
+        this.key_file.set_boolean (section, key, value);
+    }
+
+    private void enable_upnp (bool enable) {
+        try {
+            var config_dir = Environment.get_user_config_dir ();
+            this.ensure_dir_exists (config_dir);
+            var dest_dir = Path.build_filename (config_dir, "autostart");
+            this.ensure_dir_exists (dest_dir);
+
+            var dest_path = Path.build_filename (dest_dir, "rygel.desktop");
+            var dest = File.new_for_path (dest_path);
+
+            if (enable) {
+                // Creating the proxy starts the service
+                DBusObject dbus = Bus.get_proxy_sync
+                                        (BusType.SESSION,
+                                         DBUS_SERVICE,
+                                         DBUS_OBJECT,
+                                         DBusProxyFlags.DO_NOT_LOAD_PROPERTIES);
+                dbus.start_service_by_name (DBusInterface.SERVICE_NAME, 0);
+
+                // Then symlink the desktop file to user's autostart dir
+                var source_path = Path.build_filename (BuildConfig.DESKTOP_DIR,
+                                                       "rygel.desktop");
+                try {
+                    dest.make_symbolic_link (source_path, null);
+                } catch (IOError.EXISTS err) {}
+
+                this.set_bool ("general", ENABLED_KEY, true);
+            } else {
+                // Stop service only if already running
+                if (this.get_enabled ("general")) {
+                    // Create proxy to Rygel
+                    DBusInterface rygel_proxy = Bus.get_proxy_sync
+                                        (BusType.SESSION,
+                                         DBusInterface.SERVICE_NAME,
+                                         DBusInterface.OBJECT_PATH,
+                                         DBusProxyFlags.DO_NOT_LOAD_PROPERTIES);
+
+                    rygel_proxy.shutdown ();
+                }
+
+                // Then delete the symlink from user's autostart dir
+                try {
+                    dest.delete (null);
+                } catch (IOError.NOT_FOUND err) {}
+
+                this.set_bool ("general", ENABLED_KEY, false);
+            }
+        } catch (GLib.Error err) {
+            string message;
+
+            if (enable) {
+                message = _("Failed to start Rygel service: %s");
+            } else {
+                message = _("Failed to stop Rygel service: %s");
+            }
+
+            warning (message, err.message);
+        }
+    }
+
+    private void ensure_dir_exists (string dir_path) throws GLib.Error {
+        var dir = File.new_for_path (dir_path);
+
+        try {
+            dir.make_directory (null);
+        } catch (IOError.EXISTS err) { /* Thats OK */ }
+    }
+}
+



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