summaryrefslogtreecommitdiff
path: root/src/ui/rygel-writable-user-config.vala
blob: 584174087100d185f418d44de1113098e3b4ee6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * 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.
 */

/**
 * 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";

    private string user_config;

    public WritableUserConfig () throws Error {
        var path = Path.build_filename (Environment.get_user_config_dir (),
                                        CONFIG_FILE);

        base (path);

        this.user_config = path;
    }

    public bool is_upnp_enabled () {
        try {
            var file = File.new_for_path (this.user_config);
            if (file.query_exists ()) {
                return this.get_upnp_enabled ();
            }

            return false;
        } catch (Error error) {
            return false;
        }
    }

    public void set_upnp_enabled (bool value) {
        bool enabled = false;

        enabled = this.is_upnp_enabled ();

        if (value != enabled) {
            this.enable_upnp (value);
        }
    }

    public void set_interface (string? value) {
        string iface;

        if (value != null) {
            iface = value;
        } else {
            iface = "";
        }

        this.set_string ("general", IFACE_KEY, iface);
    }

    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 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
                Bus.watch_name (BusType.SESSION,
                                DBusInterface.SERVICE_NAME,
                                BusNameWatcherFlags.AUTO_START);

                // 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", UPNP_ENABLED_KEY, true);
            } else {
                // Stop service only if already running
                // Then delete the symlink from user's autostart dir
                try {
                    dest.delete (null);
                } catch (IOError.NOT_FOUND err) {}

                this.set_bool ("general", UPNP_ENABLED_KEY, false);

                if (this.is_upnp_enabled ()) {
                    // 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 ();
                }
            }
        } 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 */ }
    }
}