summaryrefslogtreecommitdiff
path: root/src/librygel-core/rygel-meta-config.vala
blob: 02f3aa05b1fc8e1d2e05dd6c6205477c573b5752 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/*
 * Copyright (C) 2008,2009 Nokia Corporation.
 * Copyright (C) 2008,2009 Zeeshan Ali (Khattak) <zeeshanak@gnome.org>.
 * Copyright (C) 2012 Intel Corporation.
 * Copyright (C) 2012 Intel Corporation
 *
 * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
 *                               <zeeshan.ali@nokia.com>
 *         Jens Georg <jensg@openismus.com>
 *         Krzesimir Nowak <krnowak@openismus.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 Gee;

/**
 * Manages all the configuration sources for Rygel.
 *
 * Abstracts Rygel and it's plugins from Configuration implementations. It keeps
 * all real configuration sources in a list and returns the value provided by
 * the first one. If none of them provides the value, it emits an error.
 */
public class Rygel.MetaConfig : GLib.Object, Configuration {
    // Our singleton
    private static MetaConfig meta_config;

    private static ArrayList<Configuration> configs;

    private void connect_signals (Configuration config) {
        config.configuration_changed.connect (this.on_configuration_changed);
        config.section_changed.connect (this.on_section_changed);
        config.setting_changed.connect (this.on_setting_changed);
    }

    public static MetaConfig get_default () {
        if (MetaConfig.configs == null) {
            MetaConfig.configs = new ArrayList<Configuration> ();
        }

        if (meta_config == null) {
            meta_config = new MetaConfig ();

            foreach (var config in MetaConfig.configs) {
                meta_config.connect_signals (config);
            }
        }

        return meta_config;
    }

    public static void register_configuration (Configuration config) {
        if (MetaConfig.configs == null) {
            MetaConfig.configs = new ArrayList<Configuration> ();
        }
        configs.add (config);

        if (meta_config != null) {
            meta_config.connect_signals (config);
        }
    }

    public bool get_upnp_enabled () throws GLib.Error {
        bool val = true;
        bool unavailable = true;

        foreach (var config in MetaConfig.configs) {
            try {
                val = config.get_upnp_enabled ();
                unavailable = false;
                break;
            } catch (GLib.Error err) {}
        }

        if (unavailable) {
            throw new ConfigurationError.NO_VALUE_SET (_("No value available"));
        }

        return val;
    }

    public string get_interface () throws GLib.Error {
        string val = null;
        bool unavailable = true;

        foreach (var config in MetaConfig.configs) {
            try {
                val = config.get_interface ();
                unavailable = false;
                break;
            } catch (GLib.Error err) {}
        }

        if (unavailable) {
            throw new ConfigurationError.NO_VALUE_SET (_("No value available"));
        }

        return val;
    }

    public int get_port () throws GLib.Error {
        int val = 0;
        bool unavailable = true;

        foreach (var config in MetaConfig.configs) {
            try {
                val = config.get_port ();
                unavailable = false;
                break;
            } catch (GLib.Error err) {}
        }

        if (unavailable) {
            throw new ConfigurationError.NO_VALUE_SET (_("No value available"));
        }

        return val;
    }

    public bool get_transcoding () throws GLib.Error {
        bool val = true;
        bool unavailable = true;

        foreach (var config in MetaConfig.configs) {
            try {
                val = config.get_transcoding ();
                unavailable = false;
                break;
            } catch (GLib.Error err) {}
        }

        if (unavailable) {
            throw new ConfigurationError.NO_VALUE_SET (_("No value available"));
        }

        return val;
    }

    public bool get_allow_upload () throws GLib.Error {
        bool val = true;
        bool unavailable = true;

        foreach (var config in MetaConfig.configs) {
            try {
                val = config.get_allow_upload ();
                unavailable = false;
                break;
            } catch (GLib.Error err) {}
        }

        if (unavailable) {
            throw new ConfigurationError.NO_VALUE_SET (_("No value available"));
        }

        return val;
    }

    public bool get_allow_deletion () throws GLib.Error {
        bool val = true;
        bool unavailable = true;

        foreach (var config in MetaConfig.configs) {
            try {
                val = config.get_allow_deletion ();
                unavailable = false;
                break;
            } catch (GLib.Error err) {}
        }

        if (unavailable) {
            throw new ConfigurationError.NO_VALUE_SET (_("No value available"));
        }

        return val;
    }

    public string get_log_levels () throws GLib.Error {
        string val = null;
        bool unavailable = true;

        foreach (var config in MetaConfig.configs) {
            try {
                val = config.get_log_levels ();
                unavailable = false;
                break;
            } catch (GLib.Error err) {}
        }

        if (unavailable) {
            throw new ConfigurationError.NO_VALUE_SET (_("No value available"));
        }

        return val;
    }

    public string get_plugin_path () throws GLib.Error {
        string val = null;
        bool unavailable = true;

        foreach (var config in MetaConfig.configs) {
            try {
                val = config.get_plugin_path ();
                unavailable = false;
                break;
            } catch (GLib.Error err) {}
        }

        if (unavailable) {
            throw new ConfigurationError.NO_VALUE_SET ("No value available");
        }

        return val;
    }

    public string get_video_upload_folder () throws GLib.Error {
        unowned string default = Environment.get_user_special_dir
                                        (UserDirectory.VIDEOS);
        var value = default;

        foreach (var config in MetaConfig.configs) {
            try {
                value = config.get_video_upload_folder ();
            } catch (GLib.Error err) { }
        }

        return value.replace ("@VIDEOS@", default);
    }

    public string get_music_upload_folder () throws GLib.Error {
        unowned string default = Environment.get_user_special_dir
                                        (UserDirectory.MUSIC);

        var value = default;

        foreach (var config in MetaConfig.configs) {
            try {
                value = config.get_music_upload_folder ();
            } catch (GLib.Error err) {};
        }

        return value.replace ("@MUSIC@", default);
    }

    public string get_picture_upload_folder () throws GLib.Error {
        unowned string default = Environment.get_user_special_dir
                                        (UserDirectory.PICTURES);
        var value = default;

        foreach (var config in MetaConfig.configs) {
            try {
                value = config.get_picture_upload_folder ();
            } catch (GLib.Error err) {};
        }

        return value.replace ("@PICTURES@", default);
    }


    public bool get_enabled (string section) throws GLib.Error {
        bool val = true;
        bool unavailable = true;

        foreach (var config in MetaConfig.configs) {
            try {
                val = config.get_enabled (section);
                unavailable = false;
                break;
            } catch (GLib.Error err) {}
        }

        if (unavailable) {
            throw new ConfigurationError.NO_VALUE_SET
                                        (_("No value set for '%s/enabled'"),
                                         section);
        }

        return val;
    }

    public string get_title (string section) throws GLib.Error {
        string val = null;

        foreach (var config in MetaConfig.configs) {
            try {
                val = config.get_title (section);
                break;
            } catch (GLib.Error err) {}
        }

        if (val == null) {
            throw new ConfigurationError.NO_VALUE_SET
                                        (_("No value set for '%s/title'"),
                                         section);
        }

        return val;
    }

    public string get_string (string section,
                              string key) throws GLib.Error {
        string val = null;

        foreach (var config in MetaConfig.configs) {
            try {
                val = config.get_string (section, key);
                break;
            } catch (GLib.Error err) {}
        }

        if (val == null) {
            throw new ConfigurationError.NO_VALUE_SET
                                        (_("No value available for '%s/%s'"),
                                         section,
                                         key);
        }

        return val;
    }

    public Gee.ArrayList<string> get_string_list (string section,
                                                  string key)
                                                  throws GLib.Error {
        Gee.ArrayList<string> val = null;

        foreach (var config in MetaConfig.configs) {
            try {
                val = config.get_string_list (section, key);
                break;
            } catch (GLib.Error err) {}
        }

        if (val == null) {
            throw new ConfigurationError.NO_VALUE_SET
                                        (_("No value available for '%s/%s'"),
                                         section,
                                         key);
        }

        return val;
    }

    public int get_int (string section,
                        string key,
                        int    min,
                        int    max)
                        throws GLib.Error {
        int val = 0;
        bool unavailable = true;

        foreach (var config in MetaConfig.configs) {
            try {
                val = config.get_int (section, key, min, max);
                unavailable = false;
                break;
            } catch (GLib.Error err) {}
        }

        if (unavailable) {
            throw new ConfigurationError.NO_VALUE_SET
                                        (_("No value available for '%s/%s'"),
                                         section,
                                         key);
        }

        return val;
    }

    public Gee.ArrayList<int> get_int_list (string section,
                                            string key)
                                            throws GLib.Error {
        Gee.ArrayList<int> val = null;

        foreach (var config in MetaConfig.configs) {
            try {
                val = config.get_int_list (section, key);
                break;
            } catch (GLib.Error err) {}
        }

        if (val == null) {
            throw new ConfigurationError.NO_VALUE_SET
                                        (_("No value available for '%s/%s'"),
                                         section,
                                         key);
        }

        return val;
    }

    public bool get_bool (string section,
                          string key)
                          throws GLib.Error {
        bool val = false;
        bool unavailable = true;

        foreach (var config in MetaConfig.configs) {
            try {
                val = config.get_bool (section, key);
                unavailable = false;
                break;
            } catch (GLib.Error err) {}
        }

        if (unavailable) {
            throw new ConfigurationError.NO_VALUE_SET
                                        (_("No value available for '%s/%s'"),
                                         section,
                                         key);
        }

        return val;
    }

    private bool configuration_value_available (Configuration config,
                                                ConfigurationEntry entry) {
        try {
            switch (entry) {
            case ConfigurationEntry.UPNP_ENABLED:
                config.get_upnp_enabled ();
                break;

            case ConfigurationEntry.INTERFACE:
                config.get_interface ();
                break;

            case ConfigurationEntry.PORT:
                config.get_port ();
                break;

            case ConfigurationEntry.TRANSCODING:
                config.get_transcoding ();
                break;

            case ConfigurationEntry.ALLOW_UPLOAD:
                config.get_allow_upload ();
                break;

            case ConfigurationEntry.ALLOW_DELETION:
                config.get_allow_deletion ();
                break;

            case ConfigurationEntry.LOG_LEVELS:
                config.get_log_levels ();
                break;

            case ConfigurationEntry.PLUGIN_PATH:
                config.get_plugin_path ();
                break;

            case ConfigurationEntry.VIDEO_UPLOAD_FOLDER:
                config.get_video_upload_folder ();
                break;

            case ConfigurationEntry.MUSIC_UPLOAD_FOLDER:
                config.get_music_upload_folder ();
                break;

            case ConfigurationEntry.PICTURE_UPLOAD_FOLDER:
                config.get_picture_upload_folder ();
                break;

            default:
                assert_not_reached ();
            }
        } catch (GLib.Error e) {
            return false;
        }

        return true;
    }

    private void on_configuration_changed (Configuration affected_config,
                                           ConfigurationEntry entry) {
        foreach (var config in MetaConfig.configs) {
            if (config == affected_config) {
                this.configuration_changed (entry);
            } else {
                if (configuration_value_available (config, entry)) {
                    return;
                }
            }
        }
    }

    private bool setting_value_available (Configuration config,
                                          string section,
                                          SectionEntry entry) {
        try {
            switch (entry) {
            case SectionEntry.TITLE:
                config.get_title (section);
                break;

            case SectionEntry.ENABLED:
                config.get_enabled (section);
                break;

            default:
                assert_not_reached ();
            }
        } catch (GLib.Error e) {
            return false;
        }

        return true;
    }

    private void on_section_changed (Configuration affected_config,
                                     string section,
                                     SectionEntry entry) {
        foreach (var config in MetaConfig.configs) {
            if (config == affected_config) {
                this.section_changed (section, entry);
            } else {
                if (setting_value_available (config, section, entry)) {
                    return;
                }
            }
        }
    }

    private void on_setting_changed (Configuration affected_config,
                                     string section,
                                     string key) {
        // The section and key here is actually a catch-wrestling.
        // We emit the setting changed straight away.
        this.setting_changed (section, key);
    }
}