<commit_before>/*
 * Copyright (C) 2008,2009 Nokia Corporation.
 * Copyright (C) 2008,2009 <NAME> (Khattak) <<EMAIL>>.
 *
 * Author: <NAME> (Khattak) <<EMAIL>>
 *                               <<EMAIL>>
 *
 * 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;

/**
 * Manages the user configuration for Rygel.
 */
public class Rygel.UserConfig : GLib.Object, Configuration {
    protected static const string CONFIG_FILE = "rygel.conf";
    protected static const string IP_KEY = "host-ip";
    protected static const string PORT_KEY = "port";
    protected static const string ENABLED_KEY = "enabled";
    protected static const string TITLE_KEY = "title";
    protected static const string TRANSCODING_KEY = "enable-transcoding";
    protected static const string MP3_TRANSCODER_KEY = "enable-mp3-transcoder";
    protected static const string MP2TS_TRANSCODER_KEY =
                                                    "enable-mp2ts-transcoder";
    protected static const string LPCM_TRANSCODER_KEY =
                                                    "enable-lpcm-transcoder";

    private const string DBUS_SERVICE = "org.freedesktop.DBus";
    private const string DBUS_PATH = "/org/freedesktop/DBus";
    private const string DBUS_INTERFACE = "org.freedesktop.DBus";

    private const string RYGEL_SERVICE = "org.gnome.Rygel";
    private const string RYGEL_PATH = "/org/gnome/Rygel";
    private const string RYGEL_INTERFACE = "org.gnome.Rygel";

    // Our singleton
    private static UserConfig config;

    protected KeyFile key_file;
    private string path;          // Path to configuration file

    private dynamic DBus.Object dbus_obj;
    private dynamic DBus.Object rygel_obj;

    public bool get_upnp_enabled () throws GLib.Error {
        return this.get_bool ("general", ENABLED_KEY);
    }

    public void set_upnp_enabled (bool value) {
        if (value != this.get_upnp_enabled ()) {
            this.enable_upnp (value);
        }
    }

    public string get_host_ip () throws GLib.Error {
        return this.get_string ("general", IP_KEY);
    }

    public void set_host_ip (string value) {
        this.set_string ("general", IP_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 static UserConfig get_default () throws Error {
        if (config == null) {
            config = new UserConfig ();
        }

        return config;
    }

    public UserConfig () 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;

        this.key_file.load_from_dirs (CONFIG_FILE,
                                      dirs,
                                      out this.path,
                                      KeyFileFlags.NONE);
        debug ("Loaded user configuration from file '%s'", this.path);

        DBus.Connection connection = DBus.Bus.get (DBus.BusType.SESSION);

        // Create proxy to Rygel
        this.rygel_obj = connection.get_object (RYGEL_SERVICE,
                                                RYGEL_PATH,
                                                RYGEL_INTERFACE);
        // and DBus
        this.dbus_obj = connection.get_object (DBUS_SERVICE,
                                               DBUS_PATH,
                                               DBUS_INTERFACE);
    }

    public void save () {
        size_t length;
        var data = this.key_file.to_data (out length);<commit_msg>core: Don't lose translations/comments in config<commit_after>/*
 * Copyright (C) 2008,2009 Nokia Corporation.
 * Copyright (C) 2008,2009 <NAME> (Khattak) <<EMAIL>>.
 *
 * Author: <NAME> (Khattak) <<EMAIL>>
 *                               <<EMAIL>>
 *
 * 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;

/**
 * Manages the user configuration for Rygel.
 */
public class Rygel.UserConfig : GLib.Object, Configuration {
    protected static const string CONFIG_FILE = "rygel.conf";
    protected static const string IP_KEY = "host-ip";
    protected static const string PORT_KEY = "port";
    protected static const string ENABLED_KEY = "enabled";
    protected static const string TITLE_KEY = "title";
    protected static const string TRANSCODING_KEY = "enable-transcoding";
    protected static const string MP3_TRANSCODER_KEY = "enable-mp3-transcoder";
    protected static const string MP2TS_TRANSCODER_KEY =
                                                    "enable-mp2ts-transcoder";
    protected static const string LPCM_TRANSCODER_KEY =
                                                    "enable-lpcm-transcoder";

    private const string DBUS_SERVICE = "org.freedesktop.DBus";
    private const string DBUS_PATH = "/org/freedesktop/DBus";
    private const string DBUS_INTERFACE = "org.freedesktop.DBus";

    private const string RYGEL_SERVICE = "org.gnome.Rygel";
    private const string RYGEL_PATH = "/org/gnome/Rygel";
    private const string RYGEL_INTERFACE = "org.gnome.Rygel";

    // Our singleton
    private static UserConfig config;

    protected KeyFile key_file;
    private string path;          // Path to configuration file

    private dynamic DBus.Object dbus_obj;
    private dynamic DBus.Object rygel_obj;

    public bool get_upnp_enabled () throws GLib.Error {
        return this.get_bool ("general", ENABLED_KEY);
    }

    public void set_upnp_enabled (bool value) {
        if (value != this.get_upnp_enabled ()) {
            this.enable_upnp (value);
        }
    }

    public string get_host_ip () throws GLib.Error {
        return this.get_string ("general", IP_KEY);
    }

    public void set_host_ip (string value) {
        this.set_string ("general", IP_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 static UserConfig get_default () throws Error {
        if (config == null) {
            config = new UserConfig ();
        }

        return config;
    }

    public UserConfig () 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;

        this.key_file.load_from_dirs (CONFIG_FILE,
                                      dirs,
                                      out this.path,
                                      KeyFileFlags.KEEP_COMMENTS |
                                      KeyFileFlags.KEEP_TRANSLATIONS);
        debug ("Loaded user configuration from file '%s'", this.path);

        DBus.Connection connection = DBus.Bus.get (DBus.BusType.SESSION);

        // Create proxy to Rygel
        this.rygel_obj = connection.get_object (RYGEL_SERVICE,
                                                RYGEL_PATH,
                                                RYGEL_INTERFACE);
        // and DBus
        this.dbus_obj = connection.get_object (DBUS_SERVICE,
                                               DBUS_PATH,
                                               DBUS_INTERFACE);
    }

    public void save () {
        size_t length;
        var data = this.key_file.to_data (out length);
{'fraction_non_alphanumeric': 0.08520179372197309, 'fraction_numerical': 0.008602544156676124, 'mean_word_length': 2.2659892408846383, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 41, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5607628', 'n_tokens_mistral': 3201, 'n_tokens_neox': 3069, 'n_words': 925}