core,plugins,ui: Fix self-contradiction in headers
[profile/ivi/rygel.git] / src / rygel / rygel-main.vala
1 /*
2  * Copyright (C) 2008 Nokia Corporation.
3  * Copyright (C) 2008 Zeeshan Ali (Khattak) <zeeshanak@gnome.org>.
4  *
5  * Author: Zeeshan Ali (Khattak) <zeeshanak@gnome.org>
6  *
7  * This file is part of Rygel.
8  *
9  * Rygel is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Rygel is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23
24 using CStuff;
25 using Gee;
26
27 public class Rygel.Main : Object {
28     private PluginLoader plugin_loader;
29     private RootDeviceFactory device_factory;
30     private ArrayList<RootDevice> root_devices;
31
32     private MainLoop main_loop;
33
34     private int exit_code;
35
36     public Main () throws GLib.Error {
37         Environment.set_application_name (_(BuildConfig.PACKAGE_NAME));
38
39         this.root_devices = new ArrayList<RootDevice> ();
40         this.plugin_loader = new PluginLoader ();
41         this.device_factory = new RootDeviceFactory ();
42         this.main_loop = new GLib.MainLoop (null, false);
43
44         this.exit_code = 0;
45
46         this.plugin_loader.plugin_available += this.on_plugin_loaded;
47
48         Utils.on_application_exit (this.application_exit_cb);
49     }
50
51     public int run () {
52         this.plugin_loader.load_plugins ();
53
54         this.main_loop.run ();
55
56         return this.exit_code;
57     }
58
59     public void exit (int exit_code) {
60         this.exit_code = exit_code;
61         this.main_loop.quit ();
62     }
63
64     private void application_exit_cb () {
65         this.exit (0);
66     }
67
68     private void on_plugin_loaded (PluginLoader plugin_loader,
69                                    Plugin       plugin) {
70         try {
71             var device = this.device_factory.create (plugin);
72
73             device.available = plugin.available;
74
75             root_devices.add (device);
76
77             plugin.notify["available"] += this.on_plugin_notify;
78         } catch (GLib.Error error) {
79             warning ("Failed to create RootDevice for %s. Reason: %s\n",
80                      plugin.name,
81                      error.message);
82         }
83     }
84
85     private void on_plugin_notify (Plugin    plugin,
86                                    ParamSpec spec) {
87         foreach (var device in this.root_devices) {
88             if (device.resource_factory == plugin) {
89                 device.available = plugin.available;
90             }
91         }
92     }
93
94     public static int main (string[] args) {
95         Main main;
96         DBusService service;
97
98         try {
99             // Parse commandline options
100             CmdlineConfig.parse_args (ref args);
101
102             // initialize gstreamer
103             var dummy_args = new string[0];
104             Gst.init (ref dummy_args);
105
106             main = new Main ();
107             service = new DBusService (main);
108         } catch (CmdlineConfigError.VERSION_ONLY err) {
109             return 0;
110         } catch (GLib.Error err) {
111             error ("%s", err.message);
112
113             return -1;
114         }
115
116         int exit_code = main.run ();
117
118         return exit_code;
119     }
120 }
121