[rygel] core: Configuration from Environment



commit c48bd27c4b7201c212284f30c07d70c9ad47650f
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Thu Mar 4 17:01:05 2010 +0200

    core: Configuration from Environment
    
    Now you can tweak rygel through environment variables too. \o/

 src/rygel/Makefile.am                   |    2 +
 src/rygel/rygel-environment-config.vala |  181 +++++++++++++++++++++++++++++++
 src/rygel/rygel-meta-config.vala        |    1 +
 3 files changed, 184 insertions(+), 0 deletions(-)
---
diff --git a/src/rygel/Makefile.am b/src/rygel/Makefile.am
index 0dfb9b4..f2fc3cd 100644
--- a/src/rygel/Makefile.am
+++ b/src/rygel/Makefile.am
@@ -43,6 +43,7 @@ VAPI_SOURCE_FILES = rygel-configuration.vala \
 		    rygel-user-config.vala \
 		    rygel-meta-config.vala \
 		    rygel-cmdline-config.vala \
+		    rygel-environment-config.vala \
 		    rygel-content-directory.vala \
 		    rygel-connection-manager.vala \
 		    rygel-source-connection-manager.vala \
@@ -128,6 +129,7 @@ librygel_configuration_a_SOURCES = rygel-configuration.c \
 				   rygel-user-config.c \
 				   rygel-meta-config.c \
 				   rygel-cmdline-config.c \
+				   rygel-environment-config.c \
 				   cstuff.c
 
 MAINTAINERCLEANFILES = Makefile.in $(BUILT_SOURCES)
diff --git a/src/rygel/rygel-environment-config.vala b/src/rygel/rygel-environment-config.vala
new file mode 100644
index 0000000..80de37e
--- /dev/null
+++ b/src/rygel/rygel-environment-config.vala
@@ -0,0 +1,181 @@
+/*
+ * Copyright (C) 2008-2010 Nokia Corporation.
+ * Copyright (C) 2008 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 CStuff;
+using Gee;
+
+/**
+ * Manages configuration from Environment.
+ */
+public class Rygel.EnvironmentConfig : GLib.Object, Configuration {
+    private static string RYGEL_PREFIX = "RYGEL";
+    private static string TITLE_KEY = RYGEL_PREFIX + "_TITLE";
+    private static string ENABLED_KEY = "ENABLED";
+    private static string INTERFACE_ENV = RYGEL_PREFIX + "_IFACE";
+    private static string PORT_ENV = RYGEL_PREFIX + "_PORT";
+    private static string TRANSCODING_ENV = RYGEL_PREFIX + "_TRANSCODING";
+    private static string MP3_TRANSCODING_ENV = RYGEL_PREFIX + "_MP3_TRANS";
+    private static string LPCM_TRANSCODING_ENV = RYGEL_PREFIX + "_LPCM_TRANS";
+    private static string MP2TS_TRANSCODING_ENV = RYGEL_PREFIX + "_MP2TS_TRANS";
+    private static string WMV_TRANSCODING_ENV = RYGEL_PREFIX + "_WMV_TRANS";
+    private static string LOG_LEVEL_ENV = RYGEL_PREFIX + "_LOG";
+
+    // Our singleton
+    private static EnvironmentConfig config;
+
+    public static EnvironmentConfig get_default () {
+        if (config == null) {
+            config = new EnvironmentConfig ();
+        }
+
+        return config;
+    }
+
+    // This config doesn't make sense in this context.
+    public bool get_upnp_enabled () throws GLib.Error {
+        return true;
+    }
+
+    public string get_interface () throws GLib.Error {
+        return this.get_string_variable (INTERFACE_ENV);
+    }
+
+    public int get_port () throws GLib.Error {
+        return this.get_int_variable (PORT_ENV, 0, int16.MAX);
+    }
+
+    public bool get_transcoding () throws GLib.Error {
+        return this.get_bool_variable (TRANSCODING_ENV);
+    }
+
+    public bool get_mp3_transcoder () throws GLib.Error {
+        return this.get_bool_variable (MP3_TRANSCODING_ENV);
+    }
+
+    public bool get_mp2ts_transcoder () throws GLib.Error {
+        return this.get_bool_variable (MP2TS_TRANSCODING_ENV);
+    }
+
+    public bool get_wmv_transcoder () throws GLib.Error {
+        return this.get_bool_variable (WMV_TRANSCODING_ENV);
+    }
+
+    public bool get_lpcm_transcoder () throws GLib.Error {
+        return this.get_bool_variable (LPCM_TRANSCODING_ENV);
+    }
+
+    public LogLevel get_log_level () throws GLib.Error {
+        return (LogLevel) this.get_int_variable (LOG_LEVEL_ENV,
+                                                LogLevel.CRITICAL,
+                                                LogLevel.DEBUG);
+    }
+
+    public bool get_enabled (string section) throws GLib.Error {
+        return get_bool (section, ENABLED_KEY);
+    }
+
+    public string get_title (string section) throws GLib.Error {
+        return this.get_string (RYGEL_PREFIX + "_" + section, TITLE_KEY);
+    }
+
+    public string get_string (string section,
+                              string key) throws GLib.Error {
+        return this.get_string_variable (RYGEL_PREFIX + "_" +
+                                         section.up () + "_"  +
+                                         key);
+    }
+
+    public Gee.ArrayList<string> get_string_list (string section,
+                                                  string key)
+                                                  throws GLib.Error {
+        var str = this.get_string (section, key);
+        var value = new ArrayList<string> ();
+        foreach (var token in str.split (",", -1)) {
+            value.add (token);
+        }
+
+        return value;
+    }
+
+    public int get_int (string section,
+                        string key,
+                        int    min,
+                        int    max)
+                        throws GLib.Error {
+        return this.get_int_variable (RYGEL_PREFIX + "_" +
+                                      section.up () + "_"  + key,
+                                      min,
+                                      max);
+    }
+
+    public Gee.ArrayList<int> get_int_list (string section,
+                                            string key)
+                                            throws GLib.Error {
+        var str = this.get_string (section, key);
+        var value = new ArrayList<int> ();
+        foreach (var token in str.split (",", -1)) {
+            value.add (token.to_int ());
+        }
+
+        return value;
+    }
+
+    public bool get_bool (string section,
+                          string key)
+                          throws GLib.Error {
+        return this.get_bool_variable (RYGEL_PREFIX + "_" +
+                                       section.up () + "_"  +
+                                       key);
+    }
+
+    private string get_string_variable (string variable) throws GLib.Error {
+        var str = Environment.get_variable (variable);
+        if (str == null) {
+            throw new ConfigurationError.NO_VALUE_SET ("No value available");
+        }
+
+        return str;
+    }
+
+    private int get_int_variable (string variable,
+                                  int    min,
+                                  int    max) throws GLib.Error {
+        var val = Environment.get_variable (variable);
+        if (val == null) {
+            throw new ConfigurationError.NO_VALUE_SET ("No value available");
+        }
+
+        return val.to_int ().clamp (min, max);
+    }
+
+    private bool get_bool_variable (string variable) throws GLib.Error {
+        var enabled = Environment.get_variable (variable);
+        if (enabled == null) {
+            throw new ConfigurationError.NO_VALUE_SET ("No value available");
+        }
+
+        return enabled.to_bool ();
+    }
+}
+
diff --git a/src/rygel/rygel-meta-config.vala b/src/rygel/rygel-meta-config.vala
index ebaaf3c..748c303 100644
--- a/src/rygel/rygel-meta-config.vala
+++ b/src/rygel/rygel-meta-config.vala
@@ -50,6 +50,7 @@ public class Rygel.MetaConfig : GLib.Object, Configuration {
         this.configs = new ArrayList<Configuration> ();
 
         this.configs.add (CmdlineConfig.get_default ());
+        this.configs.add (EnvironmentConfig.get_default ());
         try {
             var user_config = UserConfig.get_default ();
             this.configs.add (user_config);



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