From 628976934ea4d7188ca16e5df6395e49e8a887a5 Mon Sep 17 00:00:00 2001 From: Jens Georg Date: Thu, 30 Aug 2012 08:43:29 +0200 Subject: core: Move executable configuration out of core The files are only ever used by the rygel executable and are not necessary in libcore. --- src/librygel-core/filelist.am | 5 +- src/librygel-core/rygel-cmdline-config.vala | 452 ------------------------ src/librygel-core/rygel-environment-config.vala | 216 ----------- src/librygel-core/rygel-meta-config.vala | 21 -- src/librygel-core/rygel-user-config.vala | 296 ---------------- src/rygel/Makefile.am | 3 + src/rygel/rygel-cmdline-config.vala | 452 ++++++++++++++++++++++++ src/rygel/rygel-environment-config.vala | 216 +++++++++++ src/rygel/rygel-main.vala | 24 +- src/rygel/rygel-user-config.vala | 296 ++++++++++++++++ src/ui/Makefile.am | 3 +- src/ui/rygel-preferences-dialog.vala | 2 +- src/ui/rygel-user-config.vala | 1 + 13 files changed, 995 insertions(+), 992 deletions(-) delete mode 100644 src/librygel-core/rygel-cmdline-config.vala delete mode 100644 src/librygel-core/rygel-environment-config.vala delete mode 100644 src/librygel-core/rygel-user-config.vala create mode 100644 src/rygel/rygel-cmdline-config.vala create mode 100644 src/rygel/rygel-environment-config.vala create mode 100644 src/rygel/rygel-user-config.vala create mode 120000 src/ui/rygel-user-config.vala (limited to 'src') diff --git a/src/librygel-core/filelist.am b/src/librygel-core/filelist.am index d98cb746..a3490723 100644 --- a/src/librygel-core/filelist.am +++ b/src/librygel-core/filelist.am @@ -1,5 +1,4 @@ LIBRYGEL_CORE_VAPI_SOURCE_FILES = \ - rygel-cmdline-config.vala \ rygel-connection-manager.vala \ rygel-description-file.vala \ rygel-root-device.vala \ @@ -10,7 +9,6 @@ LIBRYGEL_CORE_VAPI_SOURCE_FILES = \ rygel-plugin-loader.vala \ rygel-plugin.vala \ rygel-resource-info.vala \ - rygel-user-config.vala \ rygel-media-device.vala \ rygel-configuration.vala \ rygel-free-desktop-interfaces.vala \ @@ -18,8 +16,7 @@ LIBRYGEL_CORE_VAPI_SOURCE_FILES = \ LIBRYGEL_CORE_NONVAPI_SOURCE_FILES = \ rygel-icon-info.vala \ - rygel-xml-utils.vala \ - rygel-environment-config.vala + rygel-xml-utils.vala LIBRYGEL_CORE_VALAFLAGS_PKG = \ --pkg gio-2.0 \ diff --git a/src/librygel-core/rygel-cmdline-config.vala b/src/librygel-core/rygel-cmdline-config.vala deleted file mode 100644 index ccf7d97b..00000000 --- a/src/librygel-core/rygel-cmdline-config.vala +++ /dev/null @@ -1,452 +0,0 @@ -/* - * Copyright (C) 2008,2009 Nokia Corporation. - * Copyright (C) 2008,2009 Zeeshan Ali (Khattak) . - * Copyright (C) 2012 Openismus GmbH. - * - * Author: Zeeshan Ali (Khattak) - * - * Jens Georg - * - * 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 Gee; - -public errordomain Rygel.CmdlineConfigError { - VERSION_ONLY -} - -/** - * Manages configuration from Commandline arguments. - */ -public class Rygel.CmdlineConfig : GLib.Object, Configuration { - private static string iface; - private static int port; - - private static bool no_upnp; - private static bool no_transcoding; - private static bool no_mp3_trans; - private static bool no_mp2ts_trans; - private static bool no_lpcm_trans; - private static bool no_wmv_trans; - private static bool no_aac_trans; - private static bool no_avc_trans; - - private static bool disallow_upload; - private static bool disallow_deletion; - - private static string log_levels; - - private static string plugin_path; - - private static bool version; - - private static string config_file; - - [CCode (array_length = false, array_null_terminated = true)] - [NoArrayLength] - private static string[] disabled_plugins; - [CCode (array_length = false, array_null_terminated = true)] - [NoArrayLength] - private static string[] plugin_titles; - [CCode (array_length = false, array_null_terminated = true)] - [NoArrayLength] - private static string[] plugin_options; - - // Our singleton - private static CmdlineConfig config; - - // Command-line options - const OptionEntry[] options = { - { "version", 0, 0, OptionArg.NONE, ref version, - "Display version number", null }, - { "network-interface", 'n', 0, OptionArg.STRING, ref iface, - "Network Interface", "INTERFACE" }, - { "port", 'p', 0, OptionArg.INT, ref port, - "Port", "PORT" }, - { "disable-transcoding", 't', 0, OptionArg.NONE, ref no_transcoding, - "Disable transcoding", null }, - { "disable-mp3-transcoder", 'm', 0, OptionArg.NONE, ref no_mp3_trans, - "Disable MP3 transcoder", null }, - { "disable-mp2ts-transcoder", 's', 0, OptionArg.NONE, - ref no_mp2ts_trans, - "Disable mpeg2 transport stream transcoder", null }, - { "disable-lpcm-transcoder", 'l', 0, OptionArg.NONE, ref no_lpcm_trans, - "Disable Linear PCM transcoder", null }, - { "disable-wmv-transcoder", 'w', 0, OptionArg.NONE, ref no_wmv_trans, - "Disable WMV transcoder", null }, - { "disable-aac-transcoder", 'a', 0, OptionArg.NONE, ref no_aac_trans, - "Disable AAC_ISO_320 transcoder", null }, - { "disable-avc-transcoder", 'z', 0, OptionArg.NONE, ref no_avc_trans, - "Disable AVC_MP4 transcoder", null }, - { "disallow-upload", 'U', 0, OptionArg.NONE, - ref disallow_upload, "Disallow upload", null }, - { "disallow-deletion", 'D', 0, OptionArg.NONE, - ref disallow_deletion, "Disallow deletion", null }, - { "log-level", 'g', 0, OptionArg.STRING, ref log_levels, - "Comma-separated list of domain:level pairs to specify log level " + - "thresholds for individual domains. domain could be either " + - "'rygel', name of a plugin or '*' for all domains. " + - " Allowed levels are: " + - "0=critical,2=error,3=warning,4=message/info,5=debug.", - "DOMAIN1:LEVEL1[,DOMAIN2:LEVEL2,..]" }, - { "plugin-path", 'u', 0, OptionArg.STRING, ref plugin_path, - "Plugin Path", "PLUGIN_PATH" }, - { "disable-plugin", 'd', 0, OptionArg.STRING_ARRAY, - ref disabled_plugins, - "Disable plugin", "PluginName" }, - { "title", 'i', 0, OptionArg.STRING_ARRAY, ref plugin_titles, - "Set plugin titles", "PluginName:TITLE" }, - { "plugin-option", 'o', 0, OptionArg.STRING_ARRAY, ref plugin_options, - "Set plugin options", "PluginName:OPTION:VALUE1[,VALUE2,..]" }, - { "disable-upnp", 'P', 0, OptionArg.NONE, ref no_upnp, - "Disable UPnP (streaming-only)", null }, - { "config", 'c', 0, OptionArg.FILENAME, ref config_file, - "Use configuration file instead of user configuration", null }, - { null } - }; - - public static CmdlineConfig get_default () { - if (config == null) { - config = new CmdlineConfig (); - } - - return config; - } - - public static void parse_args (ref unowned string[] args) - throws CmdlineConfigError.VERSION_ONLY, - OptionError { - var parameter_string = "- " + BuildConfig.PACKAGE_NAME; - var opt_context = new OptionContext (parameter_string); - opt_context.set_help_enabled (true); - opt_context.set_ignore_unknown_options (true); - opt_context.add_main_entries (options, null); - opt_context.add_group (Gst.init_get_option_group ()); - - try { - opt_context.parse (ref args); - } catch (OptionError.BAD_VALUE err) { - stdout.printf (opt_context.get_help (true, null)); - - throw new CmdlineConfigError.VERSION_ONLY (""); - } - - if (version) { - stdout.printf ("%s\n", BuildConfig.PACKAGE_STRING); - - throw new CmdlineConfigError.VERSION_ONLY (""); - } - } - - public bool get_upnp_enabled () throws GLib.Error { - if (!no_upnp) { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } else { - return false; - } - } - - public string get_interface () throws GLib.Error { - if (iface == null) { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - - return iface; - } - - public int get_port () throws GLib.Error { - if (port <= 0) { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - - return port; - } - - public bool get_transcoding () throws GLib.Error { - if (!no_transcoding) { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } else { - return false; - } - } - - public bool get_mp3_transcoder () throws GLib.Error { - if (!no_mp3_trans) { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } else { - return false; - } - } - - public bool get_mp2ts_transcoder () throws GLib.Error { - if (!no_mp2ts_trans) { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } else { - return false; - } - } - - public bool get_lpcm_transcoder () throws GLib.Error { - if (!no_lpcm_trans) { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } else { - return false; - } - } - - public bool get_wmv_transcoder () throws GLib.Error { - if (!no_wmv_trans) { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } else { - return false; - } - } - - public bool get_aac_transcoder () throws GLib.Error { - if (!no_aac_trans) { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } else { - return false; - } - } - - public bool get_avc_transcoder () throws GLib.Error { - if (!no_avc_trans) { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } else { - return false; - } - } - - public bool get_allow_upload () throws GLib.Error { - if (!disallow_upload) { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } else { - return false; - } - } - - public bool get_allow_deletion () throws GLib.Error { - if (!disallow_deletion) { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } else { - return false; - } - } - - public string get_log_levels () throws GLib.Error { - if (log_levels == null) { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - - return log_levels; - } - - public string get_plugin_path () throws GLib.Error { - if (plugin_path == null) { - throw new ConfigurationError.NO_VALUE_SET ("No value available"); - } - - return plugin_path; - } - - public bool get_enabled (string section) throws GLib.Error { - var disabled = false; - foreach (var plugin in disabled_plugins) { - if (plugin == section) { - disabled = true; - break; - } - } - - if (disabled) { - return false; - } else { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - } - - public string get_title (string section) throws GLib.Error { - string title = null; - foreach (var plugin_title in plugin_titles) { - var tokens = plugin_title.split (":", 2); - if (tokens[0] != null && - tokens[1] != null && - tokens[0] == section) { - title = tokens[1]; - break; - } - } - - if (title != null) { - return title; - } else { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - } - - public string get_config_file () throws GLib.Error { - if (config_file == null) { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - - return config_file; - } - - public string get_video_upload_folder () throws GLib.Error { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - - public string get_music_upload_folder () throws GLib.Error { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - - public string get_picture_upload_folder () throws GLib.Error { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - - // Dynamic options - // FIXME: How to handle them? - public string get_string (string section, - string key) throws GLib.Error { - string value = null; - foreach (var option in plugin_options) { - var tokens = option.split (":", 3); - if (tokens[0] != null && - tokens[1] != null && - tokens[2] != null && - tokens[0] == section && - tokens[1] == key) { - value = tokens[2]; - break; - } - } - - if (value != null) { - return value; - } else { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - } - - public Gee.ArrayList get_string_list (string section, - string key) - throws GLib.Error { - ArrayList value = null; - foreach (var option in plugin_options) { - var tokens = option.split (":", 3); - if (tokens[0] != null && - tokens[1] != null && - tokens[2] != null && - tokens[0] == section && - tokens[1] == key) { - value = new ArrayList (); - foreach (var val_token in tokens[2].split (",", -1)) { - value.add (val_token); - } - break; - } - } - - if (value != null) { - return value; - } else { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - } - - public int get_int (string section, - string key, - int min, - int max) - throws GLib.Error { - int value = 0; - bool value_set = false; - foreach (var option in plugin_options) { - var tokens = option.split (":", 3); - if (tokens[0] != null && - tokens[1] != null && - tokens[2] != null && - tokens[0] == section && - tokens[1] == key) { - value = int.parse (tokens[2]); - if (value >= min && value <= max) { - value_set = true; - } - break; - } - } - - if (value_set) { - return value; - } else { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - } - - public Gee.ArrayList get_int_list (string section, - string key) - throws GLib.Error { - ArrayList value = null; - foreach (var option in plugin_options) { - var tokens = option.split (":", 3); - if (tokens[0] != null && - tokens[1] != null && - tokens[2] != null && - tokens[0] == section && - tokens[1] == key) { - value = new ArrayList (); - foreach (var val_token in tokens[2].split (",", -1)) { - value.add (int.parse (val_token)); - } - break; - } - } - - if (value != null) { - return value; - } else { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - } - - public bool get_bool (string section, - string key) - throws GLib.Error { - bool value = false; - bool value_set = false; - foreach (var option in plugin_options) { - var tokens = option.split (":", 3); - if (tokens[0] != null && - tokens[1] != null && - tokens[2] != null && - tokens[0] == section && - tokens[1] == key) { - value = bool.parse (tokens[2]); - value_set = true; - break; - } - } - - if (value_set) { - return value; - } else { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - } -} diff --git a/src/librygel-core/rygel-environment-config.vala b/src/librygel-core/rygel-environment-config.vala deleted file mode 100644 index 35d991b9..00000000 --- a/src/librygel-core/rygel-environment-config.vala +++ /dev/null @@ -1,216 +0,0 @@ -/* - * Copyright (C) 2008-2010 Nokia Corporation. - * Copyright (C) 2008 Zeeshan Ali (Khattak) . - * - * Author: Zeeshan Ali (Khattak) - * - * - * 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 Gee; - -/** - * Manages configuration from Environment. - */ -internal class Rygel.EnvironmentConfig : GLib.Object, Configuration { - private static string RYGEL_PREFIX = "RYGEL"; - private static string TITLE_KEY = RYGEL_PREFIX + "_TITLE"; - private static string DISABLE_PREFIX = RYGEL_PREFIX + "_DISABLE"; - 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 DISABLE_UPNP_ENV = DISABLE_PREFIX + "_UPNP"; - private static string TRANSCODING_ENV = DISABLE_PREFIX + "_TRANSCODING"; - private static string MP3_TRANSCODING_ENV = DISABLE_PREFIX + "_MP3_TRANS"; - private static string LPCM_TRANSCODING_ENV = DISABLE_PREFIX + "_LPCM_TRANS"; - private static string MP2TS_TRANSCODING_ENV = DISABLE_PREFIX + - "_MP2TS_TRANS"; - private static string WMV_TRANSCODING_ENV = DISABLE_PREFIX + "_WMV_TRANS"; - private static string AAC_TRANSCODING_ENV = DISABLE_PREFIX + "_AAC_TRANS"; - private static string AVC_TRANSCODING_ENV = DISABLE_PREFIX + "_AVC_TRANS"; - private static string DISALLOW_UPLOAD_ENV = DISABLE_PREFIX + "_UPLOAD"; - private static string DISALLOW_DELETION_ENV = DISABLE_PREFIX + "_DELETION"; - private static string LOG_LEVELS_ENV = RYGEL_PREFIX + "_LOG"; - private static string PLUGIN_PATH_ENV = RYGEL_PREFIX + "_PLUGIN_PATH"; - - // Our singleton - private static EnvironmentConfig config; - - public static EnvironmentConfig get_default () { - if (config == null) { - config = new EnvironmentConfig (); - } - - return config; - } - - public bool get_upnp_enabled () throws GLib.Error { - return !this.get_bool_variable (DISABLE_UPNP_ENV); - } - - 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_aac_transcoder () throws GLib.Error { - return !this.get_bool_variable (AAC_TRANSCODING_ENV); - } - - public bool get_avc_transcoder () throws GLib.Error { - return !this.get_bool_variable (AVC_TRANSCODING_ENV); - } - - public bool get_lpcm_transcoder () throws GLib.Error { - return !this.get_bool_variable (LPCM_TRANSCODING_ENV); - } - - public bool get_allow_upload () throws GLib.Error { - return !this.get_bool_variable (DISALLOW_UPLOAD_ENV); - } - - public bool get_allow_deletion () throws GLib.Error { - return !this.get_bool_variable (DISALLOW_DELETION_ENV); - } - - public string get_log_levels () throws GLib.Error { - return this.get_string_variable (LOG_LEVELS_ENV); - } - - public string get_plugin_path () throws GLib.Error { - return this.get_string_variable (PLUGIN_PATH_ENV); - } - - public string get_video_upload_folder () throws GLib.Error { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - - public string get_music_upload_folder () throws GLib.Error { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - - public string get_picture_upload_folder () throws GLib.Error { - throw new ConfigurationError.NO_VALUE_SET (_("No value available")); - } - - 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.up ()); - } - - public Gee.ArrayList get_string_list (string section, - string key) - throws GLib.Error { - var str = this.get_string (section, key); - var value = new ArrayList (); - 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 get_int_list (string section, - string key) - throws GLib.Error { - var str = this.get_string (section, key); - var value = new ArrayList (); - foreach (var token in str.split (",", -1)) { - value.add (int.parse (token)); - } - - 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 int.parse (val).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 true; - } -} diff --git a/src/librygel-core/rygel-meta-config.vala b/src/librygel-core/rygel-meta-config.vala index 9d055863..3bae6c1a 100644 --- a/src/librygel-core/rygel-meta-config.vala +++ b/src/librygel-core/rygel-meta-config.vala @@ -58,27 +58,6 @@ public class Rygel.MetaConfig : GLib.Object, Configuration { configs.add (config); } - public static void register_default_configurations () { - - var cmdline_config = CmdlineConfig.get_default (); - - MetaConfig.register_configuration (cmdline_config); - MetaConfig.register_configuration (EnvironmentConfig.get_default ()); - - try { - var config_file = cmdline_config.get_config_file (); - var user_config = new UserConfig (config_file); - MetaConfig.register_configuration (user_config); - } catch (Error error) { - try { - var user_config = UserConfig.get_default (); - MetaConfig.register_configuration (user_config); - } catch (Error err) { - warning (_("Failed to load user configuration: %s"), err.message); - } - } - } - public bool get_upnp_enabled () throws GLib.Error { bool val = true; bool unavailable = true; diff --git a/src/librygel-core/rygel-user-config.vala b/src/librygel-core/rygel-user-config.vala deleted file mode 100644 index 06efffd7..00000000 --- a/src/librygel-core/rygel-user-config.vala +++ /dev/null @@ -1,296 +0,0 @@ -/* - * Copyright (C) 2008,2009 Nokia Corporation. - * Copyright (C) 2008,2009 Zeeshan Ali (Khattak) . - * - * Author: Zeeshan Ali (Khattak) - * - * - * 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.UserConfig : GLib.Object, Configuration { - public static const string CONFIG_FILE = "rygel.conf"; - public static const string IFACE_KEY = "interface"; - public static const string PORT_KEY = "port"; - public static const string ENABLED_KEY = "enabled"; - public static const string UPNP_ENABLED_KEY = "upnp-" + ENABLED_KEY; - public static const string TITLE_KEY = "title"; - public static const string TRANSCODING_KEY = "enable-transcoding"; - public static const string MP3_TRANSCODER_KEY = "enable-mp3-transcoder"; - public static const string MP2TS_TRANSCODER_KEY = "enable-mp2ts-transcoder"; - public static const string LPCM_TRANSCODER_KEY = "enable-lpcm-transcoder"; - public static const string WMV_TRANSCODER_KEY = "enable-wmv-transcoder"; - public static const string AAC_TRANSCODER_KEY = "enable-aac-transcoder"; - public static const string AVC_TRANSCODER_KEY = "enable-avc-transcoder"; - public static const string ALLOW_UPLOAD_KEY = "allow-upload"; - public static const string ALLOW_DELETION_KEY = "allow-deletion"; - public static const string LOG_LEVELS_KEY = "log-level"; - public static const string PLUGIN_PATH_KEY = "plugin-path"; - public static const string UPLOAD_FOLDER_KEY = "upload-folder"; - public static const string VIDEO_UPLOAD_DIR_PATH_KEY = - "video-" + UPLOAD_FOLDER_KEY; - public static const string MUSIC_UPLOAD_DIR_PATH_KEY = - "music-" + UPLOAD_FOLDER_KEY; - public static const string PICTURE_UPLOAD_DIR_PATH_KEY = - "picture-" + UPLOAD_FOLDER_KEY; - - // Our singleton - private static UserConfig config; - - protected KeyFile key_file; - protected KeyFile sys_key_file; - - public bool get_upnp_enabled () throws GLib.Error { - return this.get_bool ("general", UPNP_ENABLED_KEY); - } - - public string get_interface () throws GLib.Error { - return this.get_string ("general", IFACE_KEY); - } - - public int get_port () throws GLib.Error { - return this.get_int ("general", PORT_KEY, uint16.MIN, uint16.MAX); - } - - public bool get_transcoding () throws GLib.Error { - return this.get_bool ("general", TRANSCODING_KEY); - } - - public bool get_mp3_transcoder () throws GLib.Error { - return this.get_bool ("general", MP3_TRANSCODER_KEY); - } - - public bool get_mp2ts_transcoder () throws GLib.Error { - return this.get_bool ("general", MP2TS_TRANSCODER_KEY); - } - - public bool get_lpcm_transcoder () throws GLib.Error { - return this.get_bool ("general", LPCM_TRANSCODER_KEY); - } - - public bool get_wmv_transcoder () throws GLib.Error { - return this.get_bool ("general", WMV_TRANSCODER_KEY); - } - - public bool get_aac_transcoder () throws GLib.Error { - return this.get_bool ("general", AAC_TRANSCODER_KEY); - } - - public bool get_avc_transcoder () throws GLib.Error { - return this.get_bool ("general", AVC_TRANSCODER_KEY); - } - - public bool get_allow_upload () throws GLib.Error { - return this.get_bool ("general", ALLOW_UPLOAD_KEY); - } - - public bool get_allow_deletion () throws GLib.Error { - return this.get_bool ("general", ALLOW_DELETION_KEY); - } - - public string get_log_levels () throws GLib.Error { - return this.get_string ("general", LOG_LEVELS_KEY); - } - - public string get_plugin_path () throws GLib.Error { - return this.get_string ("general", PLUGIN_PATH_KEY); - } - - public string get_video_upload_folder () throws GLib.Error { - return this.get_string ("general", VIDEO_UPLOAD_DIR_PATH_KEY); - } - - public string get_music_upload_folder () throws GLib.Error { - return this.get_string ("general", MUSIC_UPLOAD_DIR_PATH_KEY); - } - - public string get_picture_upload_folder () throws GLib.Error { - return this.get_string ("general", PICTURE_UPLOAD_DIR_PATH_KEY); - } - - public static UserConfig get_default () throws Error { - if (config == null) { - var path = Path.build_filename - (Environment.get_user_config_dir (), - CONFIG_FILE); - config = new UserConfig (path); - } - - return config; - } - - public UserConfig (string file) throws Error { - this.key_file = new KeyFile (); - this.sys_key_file = new KeyFile (); - - var path = Path.build_filename (BuildConfig.SYS_CONFIG_DIR, - CONFIG_FILE); - - this.sys_key_file.load_from_file (path, - KeyFileFlags.KEEP_COMMENTS | - KeyFileFlags.KEEP_TRANSLATIONS); - debug ("Loaded system configuration from file '%s'", path); - - try { - this.key_file.load_from_file (file, - KeyFileFlags.KEEP_COMMENTS | - KeyFileFlags.KEEP_TRANSLATIONS); - - debug ("Loaded user configuration from file '%s'", file); - } catch (Error error) { - debug ("Failed to load user configuration from file '%s': %s", - file, - error.message); - size_t size; - - var data = this.sys_key_file.to_data (out size); - this.key_file.load_from_data (data, - size, - KeyFileFlags.KEEP_COMMENTS | - KeyFileFlags.KEEP_TRANSLATIONS); - } - } - - public bool get_enabled (string section) throws GLib.Error { - return this.get_bool (section, ENABLED_KEY); - } - - public string get_title (string section) throws GLib.Error { - return this.get_string (section, TITLE_KEY); - } - - public string get_string (string section, - string key) throws GLib.Error { - string val; - - try { - val = this.key_file.get_string (section, key); - } catch (KeyFileError error) { - if (error is KeyFileError.KEY_NOT_FOUND || - error is KeyFileError.GROUP_NOT_FOUND) { - val = this.sys_key_file.get_string (section, key); - } else { - throw error; - } - } - - if (val == null || val == "") { - throw new ConfigurationError.NO_VALUE_SET - (_("No value available for '%s'"), key); - } - - return val; - } - - public Gee.ArrayList get_string_list (string section, - string key) - throws GLib.Error { - var str_list = new Gee.ArrayList (); - string[] strings; - - try { - strings = this.key_file.get_string_list (section, key); - } catch (KeyFileError error) { - if (error is KeyFileError.KEY_NOT_FOUND || - error is KeyFileError.GROUP_NOT_FOUND) { - strings = this.sys_key_file.get_string_list (section, key); - } else { - throw error; - } - } - - foreach (var str in strings) { - str_list.add (str); - } - - return str_list; - } - - public int get_int (string section, - string key, - int min, - int max) - throws GLib.Error { - int val; - - try { - val = this.key_file.get_integer (section, key); - } catch (KeyFileError error) { - if (error is KeyFileError.KEY_NOT_FOUND || - error is KeyFileError.GROUP_NOT_FOUND) { - val = this.sys_key_file.get_integer (section, key); - } else { - throw error; - } - } - - if (val == 0 || val < min || val > max) { - throw new ConfigurationError.VALUE_OUT_OF_RANGE - (_("Value of '%s' out of range"), key); - } - - return val; - } - - public Gee.ArrayList get_int_list (string section, - string key) - throws GLib.Error { - var int_list = new Gee.ArrayList (); - int[] ints; - - try { - ints = this.key_file.get_integer_list (section, key); - } catch (KeyFileError error) { - if (error is KeyFileError.KEY_NOT_FOUND || - error is KeyFileError.GROUP_NOT_FOUND) { - ints = this.sys_key_file.get_integer_list (section, key); - } else { - throw error; - } - } - - foreach (var num in ints) { - int_list.add (num); - } - - return int_list; - } - - public bool get_bool (string section, - string key) - throws GLib.Error { - bool val; - - try { - val = this.key_file.get_boolean (section, key); - } catch (KeyFileError error) { - if (error is KeyFileError.KEY_NOT_FOUND || - error is KeyFileError.GROUP_NOT_FOUND) { - val = this.sys_key_file.get_boolean (section, key); - } else { - throw error; - } - } - - return val; - } -} diff --git a/src/rygel/Makefile.am b/src/rygel/Makefile.am index 6388d13d..d2ceb4f4 100644 --- a/src/rygel/Makefile.am +++ b/src/rygel/Makefile.am @@ -14,6 +14,9 @@ bin_PROGRAMS = rygel rygel_SOURCES = \ rygel-dbus-service.vala \ rygel-main.vala \ + rygel-cmdline-config.vala \ + rygel-environment-config.vala \ + rygel-user-config.vala \ $(top_builddir)/src/librygel-core/rygel-core-1.0.vapi rygel_VALAFLAGS = \ diff --git a/src/rygel/rygel-cmdline-config.vala b/src/rygel/rygel-cmdline-config.vala new file mode 100644 index 00000000..ccf7d97b --- /dev/null +++ b/src/rygel/rygel-cmdline-config.vala @@ -0,0 +1,452 @@ +/* + * Copyright (C) 2008,2009 Nokia Corporation. + * Copyright (C) 2008,2009 Zeeshan Ali (Khattak) . + * Copyright (C) 2012 Openismus GmbH. + * + * Author: Zeeshan Ali (Khattak) + * + * Jens Georg + * + * 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 Gee; + +public errordomain Rygel.CmdlineConfigError { + VERSION_ONLY +} + +/** + * Manages configuration from Commandline arguments. + */ +public class Rygel.CmdlineConfig : GLib.Object, Configuration { + private static string iface; + private static int port; + + private static bool no_upnp; + private static bool no_transcoding; + private static bool no_mp3_trans; + private static bool no_mp2ts_trans; + private static bool no_lpcm_trans; + private static bool no_wmv_trans; + private static bool no_aac_trans; + private static bool no_avc_trans; + + private static bool disallow_upload; + private static bool disallow_deletion; + + private static string log_levels; + + private static string plugin_path; + + private static bool version; + + private static string config_file; + + [CCode (array_length = false, array_null_terminated = true)] + [NoArrayLength] + private static string[] disabled_plugins; + [CCode (array_length = false, array_null_terminated = true)] + [NoArrayLength] + private static string[] plugin_titles; + [CCode (array_length = false, array_null_terminated = true)] + [NoArrayLength] + private static string[] plugin_options; + + // Our singleton + private static CmdlineConfig config; + + // Command-line options + const OptionEntry[] options = { + { "version", 0, 0, OptionArg.NONE, ref version, + "Display version number", null }, + { "network-interface", 'n', 0, OptionArg.STRING, ref iface, + "Network Interface", "INTERFACE" }, + { "port", 'p', 0, OptionArg.INT, ref port, + "Port", "PORT" }, + { "disable-transcoding", 't', 0, OptionArg.NONE, ref no_transcoding, + "Disable transcoding", null }, + { "disable-mp3-transcoder", 'm', 0, OptionArg.NONE, ref no_mp3_trans, + "Disable MP3 transcoder", null }, + { "disable-mp2ts-transcoder", 's', 0, OptionArg.NONE, + ref no_mp2ts_trans, + "Disable mpeg2 transport stream transcoder", null }, + { "disable-lpcm-transcoder", 'l', 0, OptionArg.NONE, ref no_lpcm_trans, + "Disable Linear PCM transcoder", null }, + { "disable-wmv-transcoder", 'w', 0, OptionArg.NONE, ref no_wmv_trans, + "Disable WMV transcoder", null }, + { "disable-aac-transcoder", 'a', 0, OptionArg.NONE, ref no_aac_trans, + "Disable AAC_ISO_320 transcoder", null }, + { "disable-avc-transcoder", 'z', 0, OptionArg.NONE, ref no_avc_trans, + "Disable AVC_MP4 transcoder", null }, + { "disallow-upload", 'U', 0, OptionArg.NONE, + ref disallow_upload, "Disallow upload", null }, + { "disallow-deletion", 'D', 0, OptionArg.NONE, + ref disallow_deletion, "Disallow deletion", null }, + { "log-level", 'g', 0, OptionArg.STRING, ref log_levels, + "Comma-separated list of domain:level pairs to specify log level " + + "thresholds for individual domains. domain could be either " + + "'rygel', name of a plugin or '*' for all domains. " + + " Allowed levels are: " + + "0=critical,2=error,3=warning,4=message/info,5=debug.", + "DOMAIN1:LEVEL1[,DOMAIN2:LEVEL2,..]" }, + { "plugin-path", 'u', 0, OptionArg.STRING, ref plugin_path, + "Plugin Path", "PLUGIN_PATH" }, + { "disable-plugin", 'd', 0, OptionArg.STRING_ARRAY, + ref disabled_plugins, + "Disable plugin", "PluginName" }, + { "title", 'i', 0, OptionArg.STRING_ARRAY, ref plugin_titles, + "Set plugin titles", "PluginName:TITLE" }, + { "plugin-option", 'o', 0, OptionArg.STRING_ARRAY, ref plugin_options, + "Set plugin options", "PluginName:OPTION:VALUE1[,VALUE2,..]" }, + { "disable-upnp", 'P', 0, OptionArg.NONE, ref no_upnp, + "Disable UPnP (streaming-only)", null }, + { "config", 'c', 0, OptionArg.FILENAME, ref config_file, + "Use configuration file instead of user configuration", null }, + { null } + }; + + public static CmdlineConfig get_default () { + if (config == null) { + config = new CmdlineConfig (); + } + + return config; + } + + public static void parse_args (ref unowned string[] args) + throws CmdlineConfigError.VERSION_ONLY, + OptionError { + var parameter_string = "- " + BuildConfig.PACKAGE_NAME; + var opt_context = new OptionContext (parameter_string); + opt_context.set_help_enabled (true); + opt_context.set_ignore_unknown_options (true); + opt_context.add_main_entries (options, null); + opt_context.add_group (Gst.init_get_option_group ()); + + try { + opt_context.parse (ref args); + } catch (OptionError.BAD_VALUE err) { + stdout.printf (opt_context.get_help (true, null)); + + throw new CmdlineConfigError.VERSION_ONLY (""); + } + + if (version) { + stdout.printf ("%s\n", BuildConfig.PACKAGE_STRING); + + throw new CmdlineConfigError.VERSION_ONLY (""); + } + } + + public bool get_upnp_enabled () throws GLib.Error { + if (!no_upnp) { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } else { + return false; + } + } + + public string get_interface () throws GLib.Error { + if (iface == null) { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + + return iface; + } + + public int get_port () throws GLib.Error { + if (port <= 0) { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + + return port; + } + + public bool get_transcoding () throws GLib.Error { + if (!no_transcoding) { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } else { + return false; + } + } + + public bool get_mp3_transcoder () throws GLib.Error { + if (!no_mp3_trans) { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } else { + return false; + } + } + + public bool get_mp2ts_transcoder () throws GLib.Error { + if (!no_mp2ts_trans) { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } else { + return false; + } + } + + public bool get_lpcm_transcoder () throws GLib.Error { + if (!no_lpcm_trans) { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } else { + return false; + } + } + + public bool get_wmv_transcoder () throws GLib.Error { + if (!no_wmv_trans) { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } else { + return false; + } + } + + public bool get_aac_transcoder () throws GLib.Error { + if (!no_aac_trans) { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } else { + return false; + } + } + + public bool get_avc_transcoder () throws GLib.Error { + if (!no_avc_trans) { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } else { + return false; + } + } + + public bool get_allow_upload () throws GLib.Error { + if (!disallow_upload) { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } else { + return false; + } + } + + public bool get_allow_deletion () throws GLib.Error { + if (!disallow_deletion) { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } else { + return false; + } + } + + public string get_log_levels () throws GLib.Error { + if (log_levels == null) { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + + return log_levels; + } + + public string get_plugin_path () throws GLib.Error { + if (plugin_path == null) { + throw new ConfigurationError.NO_VALUE_SET ("No value available"); + } + + return plugin_path; + } + + public bool get_enabled (string section) throws GLib.Error { + var disabled = false; + foreach (var plugin in disabled_plugins) { + if (plugin == section) { + disabled = true; + break; + } + } + + if (disabled) { + return false; + } else { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + } + + public string get_title (string section) throws GLib.Error { + string title = null; + foreach (var plugin_title in plugin_titles) { + var tokens = plugin_title.split (":", 2); + if (tokens[0] != null && + tokens[1] != null && + tokens[0] == section) { + title = tokens[1]; + break; + } + } + + if (title != null) { + return title; + } else { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + } + + public string get_config_file () throws GLib.Error { + if (config_file == null) { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + + return config_file; + } + + public string get_video_upload_folder () throws GLib.Error { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + + public string get_music_upload_folder () throws GLib.Error { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + + public string get_picture_upload_folder () throws GLib.Error { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + + // Dynamic options + // FIXME: How to handle them? + public string get_string (string section, + string key) throws GLib.Error { + string value = null; + foreach (var option in plugin_options) { + var tokens = option.split (":", 3); + if (tokens[0] != null && + tokens[1] != null && + tokens[2] != null && + tokens[0] == section && + tokens[1] == key) { + value = tokens[2]; + break; + } + } + + if (value != null) { + return value; + } else { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + } + + public Gee.ArrayList get_string_list (string section, + string key) + throws GLib.Error { + ArrayList value = null; + foreach (var option in plugin_options) { + var tokens = option.split (":", 3); + if (tokens[0] != null && + tokens[1] != null && + tokens[2] != null && + tokens[0] == section && + tokens[1] == key) { + value = new ArrayList (); + foreach (var val_token in tokens[2].split (",", -1)) { + value.add (val_token); + } + break; + } + } + + if (value != null) { + return value; + } else { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + } + + public int get_int (string section, + string key, + int min, + int max) + throws GLib.Error { + int value = 0; + bool value_set = false; + foreach (var option in plugin_options) { + var tokens = option.split (":", 3); + if (tokens[0] != null && + tokens[1] != null && + tokens[2] != null && + tokens[0] == section && + tokens[1] == key) { + value = int.parse (tokens[2]); + if (value >= min && value <= max) { + value_set = true; + } + break; + } + } + + if (value_set) { + return value; + } else { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + } + + public Gee.ArrayList get_int_list (string section, + string key) + throws GLib.Error { + ArrayList value = null; + foreach (var option in plugin_options) { + var tokens = option.split (":", 3); + if (tokens[0] != null && + tokens[1] != null && + tokens[2] != null && + tokens[0] == section && + tokens[1] == key) { + value = new ArrayList (); + foreach (var val_token in tokens[2].split (",", -1)) { + value.add (int.parse (val_token)); + } + break; + } + } + + if (value != null) { + return value; + } else { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + } + + public bool get_bool (string section, + string key) + throws GLib.Error { + bool value = false; + bool value_set = false; + foreach (var option in plugin_options) { + var tokens = option.split (":", 3); + if (tokens[0] != null && + tokens[1] != null && + tokens[2] != null && + tokens[0] == section && + tokens[1] == key) { + value = bool.parse (tokens[2]); + value_set = true; + break; + } + } + + if (value_set) { + return value; + } else { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + } +} diff --git a/src/rygel/rygel-environment-config.vala b/src/rygel/rygel-environment-config.vala new file mode 100644 index 00000000..35d991b9 --- /dev/null +++ b/src/rygel/rygel-environment-config.vala @@ -0,0 +1,216 @@ +/* + * Copyright (C) 2008-2010 Nokia Corporation. + * Copyright (C) 2008 Zeeshan Ali (Khattak) . + * + * Author: Zeeshan Ali (Khattak) + * + * + * 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 Gee; + +/** + * Manages configuration from Environment. + */ +internal class Rygel.EnvironmentConfig : GLib.Object, Configuration { + private static string RYGEL_PREFIX = "RYGEL"; + private static string TITLE_KEY = RYGEL_PREFIX + "_TITLE"; + private static string DISABLE_PREFIX = RYGEL_PREFIX + "_DISABLE"; + 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 DISABLE_UPNP_ENV = DISABLE_PREFIX + "_UPNP"; + private static string TRANSCODING_ENV = DISABLE_PREFIX + "_TRANSCODING"; + private static string MP3_TRANSCODING_ENV = DISABLE_PREFIX + "_MP3_TRANS"; + private static string LPCM_TRANSCODING_ENV = DISABLE_PREFIX + "_LPCM_TRANS"; + private static string MP2TS_TRANSCODING_ENV = DISABLE_PREFIX + + "_MP2TS_TRANS"; + private static string WMV_TRANSCODING_ENV = DISABLE_PREFIX + "_WMV_TRANS"; + private static string AAC_TRANSCODING_ENV = DISABLE_PREFIX + "_AAC_TRANS"; + private static string AVC_TRANSCODING_ENV = DISABLE_PREFIX + "_AVC_TRANS"; + private static string DISALLOW_UPLOAD_ENV = DISABLE_PREFIX + "_UPLOAD"; + private static string DISALLOW_DELETION_ENV = DISABLE_PREFIX + "_DELETION"; + private static string LOG_LEVELS_ENV = RYGEL_PREFIX + "_LOG"; + private static string PLUGIN_PATH_ENV = RYGEL_PREFIX + "_PLUGIN_PATH"; + + // Our singleton + private static EnvironmentConfig config; + + public static EnvironmentConfig get_default () { + if (config == null) { + config = new EnvironmentConfig (); + } + + return config; + } + + public bool get_upnp_enabled () throws GLib.Error { + return !this.get_bool_variable (DISABLE_UPNP_ENV); + } + + 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_aac_transcoder () throws GLib.Error { + return !this.get_bool_variable (AAC_TRANSCODING_ENV); + } + + public bool get_avc_transcoder () throws GLib.Error { + return !this.get_bool_variable (AVC_TRANSCODING_ENV); + } + + public bool get_lpcm_transcoder () throws GLib.Error { + return !this.get_bool_variable (LPCM_TRANSCODING_ENV); + } + + public bool get_allow_upload () throws GLib.Error { + return !this.get_bool_variable (DISALLOW_UPLOAD_ENV); + } + + public bool get_allow_deletion () throws GLib.Error { + return !this.get_bool_variable (DISALLOW_DELETION_ENV); + } + + public string get_log_levels () throws GLib.Error { + return this.get_string_variable (LOG_LEVELS_ENV); + } + + public string get_plugin_path () throws GLib.Error { + return this.get_string_variable (PLUGIN_PATH_ENV); + } + + public string get_video_upload_folder () throws GLib.Error { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + + public string get_music_upload_folder () throws GLib.Error { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + + public string get_picture_upload_folder () throws GLib.Error { + throw new ConfigurationError.NO_VALUE_SET (_("No value available")); + } + + 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.up ()); + } + + public Gee.ArrayList get_string_list (string section, + string key) + throws GLib.Error { + var str = this.get_string (section, key); + var value = new ArrayList (); + 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 get_int_list (string section, + string key) + throws GLib.Error { + var str = this.get_string (section, key); + var value = new ArrayList (); + foreach (var token in str.split (",", -1)) { + value.add (int.parse (token)); + } + + 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 int.parse (val).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 true; + } +} diff --git a/src/rygel/rygel-main.vala b/src/rygel/rygel-main.vala index 74451993..8207d826 100644 --- a/src/rygel/rygel-main.vala +++ b/src/rygel/rygel-main.vala @@ -242,6 +242,28 @@ internal class Rygel.Main : Object { } } + private static void register_default_configurations () { + + var cmdline_config = CmdlineConfig.get_default (); + + MetaConfig.register_configuration (cmdline_config); + MetaConfig.register_configuration (EnvironmentConfig.get_default ()); + + try { + var config_file = cmdline_config.get_config_file (); + var user_config = new UserConfig (config_file); + MetaConfig.register_configuration (user_config); + } catch (Error error) { + try { + var user_config = UserConfig.get_default (); + MetaConfig.register_configuration (user_config); + } catch (Error err) { + warning (_("Failed to load user configuration: %s"), err.message); + } + } + } + + private static int main (string[] args) { Main main = null; DBusService service = null; @@ -257,7 +279,7 @@ internal class Rygel.Main : Object { try { // Parse commandline options CmdlineConfig.parse_args (ref args); - MetaConfig.register_default_configurations (); + Main.register_default_configurations (); main = new Main (); service = new DBusService (main); diff --git a/src/rygel/rygel-user-config.vala b/src/rygel/rygel-user-config.vala new file mode 100644 index 00000000..06efffd7 --- /dev/null +++ b/src/rygel/rygel-user-config.vala @@ -0,0 +1,296 @@ +/* + * Copyright (C) 2008,2009 Nokia Corporation. + * Copyright (C) 2008,2009 Zeeshan Ali (Khattak) . + * + * Author: Zeeshan Ali (Khattak) + * + * + * 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.UserConfig : GLib.Object, Configuration { + public static const string CONFIG_FILE = "rygel.conf"; + public static const string IFACE_KEY = "interface"; + public static const string PORT_KEY = "port"; + public static const string ENABLED_KEY = "enabled"; + public static const string UPNP_ENABLED_KEY = "upnp-" + ENABLED_KEY; + public static const string TITLE_KEY = "title"; + public static const string TRANSCODING_KEY = "enable-transcoding"; + public static const string MP3_TRANSCODER_KEY = "enable-mp3-transcoder"; + public static const string MP2TS_TRANSCODER_KEY = "enable-mp2ts-transcoder"; + public static const string LPCM_TRANSCODER_KEY = "enable-lpcm-transcoder"; + public static const string WMV_TRANSCODER_KEY = "enable-wmv-transcoder"; + public static const string AAC_TRANSCODER_KEY = "enable-aac-transcoder"; + public static const string AVC_TRANSCODER_KEY = "enable-avc-transcoder"; + public static const string ALLOW_UPLOAD_KEY = "allow-upload"; + public static const string ALLOW_DELETION_KEY = "allow-deletion"; + public static const string LOG_LEVELS_KEY = "log-level"; + public static const string PLUGIN_PATH_KEY = "plugin-path"; + public static const string UPLOAD_FOLDER_KEY = "upload-folder"; + public static const string VIDEO_UPLOAD_DIR_PATH_KEY = + "video-" + UPLOAD_FOLDER_KEY; + public static const string MUSIC_UPLOAD_DIR_PATH_KEY = + "music-" + UPLOAD_FOLDER_KEY; + public static const string PICTURE_UPLOAD_DIR_PATH_KEY = + "picture-" + UPLOAD_FOLDER_KEY; + + // Our singleton + private static UserConfig config; + + protected KeyFile key_file; + protected KeyFile sys_key_file; + + public bool get_upnp_enabled () throws GLib.Error { + return this.get_bool ("general", UPNP_ENABLED_KEY); + } + + public string get_interface () throws GLib.Error { + return this.get_string ("general", IFACE_KEY); + } + + public int get_port () throws GLib.Error { + return this.get_int ("general", PORT_KEY, uint16.MIN, uint16.MAX); + } + + public bool get_transcoding () throws GLib.Error { + return this.get_bool ("general", TRANSCODING_KEY); + } + + public bool get_mp3_transcoder () throws GLib.Error { + return this.get_bool ("general", MP3_TRANSCODER_KEY); + } + + public bool get_mp2ts_transcoder () throws GLib.Error { + return this.get_bool ("general", MP2TS_TRANSCODER_KEY); + } + + public bool get_lpcm_transcoder () throws GLib.Error { + return this.get_bool ("general", LPCM_TRANSCODER_KEY); + } + + public bool get_wmv_transcoder () throws GLib.Error { + return this.get_bool ("general", WMV_TRANSCODER_KEY); + } + + public bool get_aac_transcoder () throws GLib.Error { + return this.get_bool ("general", AAC_TRANSCODER_KEY); + } + + public bool get_avc_transcoder () throws GLib.Error { + return this.get_bool ("general", AVC_TRANSCODER_KEY); + } + + public bool get_allow_upload () throws GLib.Error { + return this.get_bool ("general", ALLOW_UPLOAD_KEY); + } + + public bool get_allow_deletion () throws GLib.Error { + return this.get_bool ("general", ALLOW_DELETION_KEY); + } + + public string get_log_levels () throws GLib.Error { + return this.get_string ("general", LOG_LEVELS_KEY); + } + + public string get_plugin_path () throws GLib.Error { + return this.get_string ("general", PLUGIN_PATH_KEY); + } + + public string get_video_upload_folder () throws GLib.Error { + return this.get_string ("general", VIDEO_UPLOAD_DIR_PATH_KEY); + } + + public string get_music_upload_folder () throws GLib.Error { + return this.get_string ("general", MUSIC_UPLOAD_DIR_PATH_KEY); + } + + public string get_picture_upload_folder () throws GLib.Error { + return this.get_string ("general", PICTURE_UPLOAD_DIR_PATH_KEY); + } + + public static UserConfig get_default () throws Error { + if (config == null) { + var path = Path.build_filename + (Environment.get_user_config_dir (), + CONFIG_FILE); + config = new UserConfig (path); + } + + return config; + } + + public UserConfig (string file) throws Error { + this.key_file = new KeyFile (); + this.sys_key_file = new KeyFile (); + + var path = Path.build_filename (BuildConfig.SYS_CONFIG_DIR, + CONFIG_FILE); + + this.sys_key_file.load_from_file (path, + KeyFileFlags.KEEP_COMMENTS | + KeyFileFlags.KEEP_TRANSLATIONS); + debug ("Loaded system configuration from file '%s'", path); + + try { + this.key_file.load_from_file (file, + KeyFileFlags.KEEP_COMMENTS | + KeyFileFlags.KEEP_TRANSLATIONS); + + debug ("Loaded user configuration from file '%s'", file); + } catch (Error error) { + debug ("Failed to load user configuration from file '%s': %s", + file, + error.message); + size_t size; + + var data = this.sys_key_file.to_data (out size); + this.key_file.load_from_data (data, + size, + KeyFileFlags.KEEP_COMMENTS | + KeyFileFlags.KEEP_TRANSLATIONS); + } + } + + public bool get_enabled (string section) throws GLib.Error { + return this.get_bool (section, ENABLED_KEY); + } + + public string get_title (string section) throws GLib.Error { + return this.get_string (section, TITLE_KEY); + } + + public string get_string (string section, + string key) throws GLib.Error { + string val; + + try { + val = this.key_file.get_string (section, key); + } catch (KeyFileError error) { + if (error is KeyFileError.KEY_NOT_FOUND || + error is KeyFileError.GROUP_NOT_FOUND) { + val = this.sys_key_file.get_string (section, key); + } else { + throw error; + } + } + + if (val == null || val == "") { + throw new ConfigurationError.NO_VALUE_SET + (_("No value available for '%s'"), key); + } + + return val; + } + + public Gee.ArrayList get_string_list (string section, + string key) + throws GLib.Error { + var str_list = new Gee.ArrayList (); + string[] strings; + + try { + strings = this.key_file.get_string_list (section, key); + } catch (KeyFileError error) { + if (error is KeyFileError.KEY_NOT_FOUND || + error is KeyFileError.GROUP_NOT_FOUND) { + strings = this.sys_key_file.get_string_list (section, key); + } else { + throw error; + } + } + + foreach (var str in strings) { + str_list.add (str); + } + + return str_list; + } + + public int get_int (string section, + string key, + int min, + int max) + throws GLib.Error { + int val; + + try { + val = this.key_file.get_integer (section, key); + } catch (KeyFileError error) { + if (error is KeyFileError.KEY_NOT_FOUND || + error is KeyFileError.GROUP_NOT_FOUND) { + val = this.sys_key_file.get_integer (section, key); + } else { + throw error; + } + } + + if (val == 0 || val < min || val > max) { + throw new ConfigurationError.VALUE_OUT_OF_RANGE + (_("Value of '%s' out of range"), key); + } + + return val; + } + + public Gee.ArrayList get_int_list (string section, + string key) + throws GLib.Error { + var int_list = new Gee.ArrayList (); + int[] ints; + + try { + ints = this.key_file.get_integer_list (section, key); + } catch (KeyFileError error) { + if (error is KeyFileError.KEY_NOT_FOUND || + error is KeyFileError.GROUP_NOT_FOUND) { + ints = this.sys_key_file.get_integer_list (section, key); + } else { + throw error; + } + } + + foreach (var num in ints) { + int_list.add (num); + } + + return int_list; + } + + public bool get_bool (string section, + string key) + throws GLib.Error { + bool val; + + try { + val = this.key_file.get_boolean (section, key); + } catch (KeyFileError error) { + if (error is KeyFileError.KEY_NOT_FOUND || + error is KeyFileError.GROUP_NOT_FOUND) { + val = this.sys_key_file.get_boolean (section, key); + } else { + throw error; + } + } + + return val; + } +} diff --git a/src/ui/Makefile.am b/src/ui/Makefile.am index 644dde6f..a5ad8273 100644 --- a/src/ui/Makefile.am +++ b/src/ui/Makefile.am @@ -12,7 +12,8 @@ rygel_preferences_SOURCES = \ rygel-preferences-section.vala \ rygel-media-pref-section.vala \ rygel-writable-user-config.vala \ - rygel-network-pref-section.vala + rygel-network-pref-section.vala \ + rygel-user-config.vala rygel_preferences_VALAFLAGS = \ --thread --pkg gio-2.0 \ diff --git a/src/ui/rygel-preferences-dialog.vala b/src/ui/rygel-preferences-dialog.vala index c80dcd30..7cdd0fcd 100644 --- a/src/ui/rygel-preferences-dialog.vala +++ b/src/ui/rygel-preferences-dialog.vala @@ -86,7 +86,7 @@ public class Rygel.PreferencesDialog : GLib.Object { Gtk.init (ref args); try { - MetaConfig.register_default_configurations (); + MetaConfig.register_configuration (UserConfig.get_default ()); var dialog = new PreferencesDialog (); dialog.run (); diff --git a/src/ui/rygel-user-config.vala b/src/ui/rygel-user-config.vala new file mode 120000 index 00000000..11bb1570 --- /dev/null +++ b/src/ui/rygel-user-config.vala @@ -0,0 +1 @@ +../rygel/rygel-user-config.vala \ No newline at end of file -- cgit v1.2.1