Attach the availability of MediaServer and Plugin
[profile/ivi/rygel.git] / src / rygel / rygel-main.vala
1 /*
2  * Copyright (C) 2008 Nokia Corporation, all rights reserved.
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 GUPnP;
25 using GConf;
26 using CStuff;
27 using Gee;
28
29 public class Rygel.Main : Object {
30     private PluginLoader plugin_loader;
31     private MediaServerFactory ms_factory;
32     private ArrayList<MediaServer> media_servers;
33
34     private MainLoop main_loop;
35
36     private int exit_code;
37
38     public Main () throws GLib.Error {
39         Environment.set_application_name (_(BuildConfig.PACKAGE_NAME));
40
41         this.media_servers = new ArrayList<MediaServer> ();
42         this.plugin_loader = new PluginLoader ();
43         this.ms_factory = new MediaServerFactory ();
44         this.main_loop = new GLib.MainLoop (null, false);
45
46         this.exit_code = 0;
47
48         this.plugin_loader.plugin_available += this.on_plugin_loaded;
49
50         Utils.on_application_exit (this.application_exit_cb);
51     }
52
53     public int run () {
54         this.plugin_loader.load_plugins ();
55
56         this.main_loop.run ();
57
58         return this.exit_code;
59     }
60
61     public void exit (int exit_code) {
62         this.exit_code = exit_code;
63         this.main_loop.quit ();
64     }
65
66     private void application_exit_cb () {
67         this.exit (0);
68     }
69
70     private void on_plugin_loaded (PluginLoader plugin_loader,
71                                    Plugin       plugin) {
72         try {
73             var server = this.ms_factory.create_media_server (plugin);
74
75             server.available = plugin.available;
76
77             media_servers.add (server);
78
79             plugin.notify["available"] += this.on_plugin_notify;
80         } catch (GLib.Error error) {
81             warning ("Failed to create MediaServer for %s. Reason: %s\n",
82                      plugin.name,
83                      error.message);
84         }
85     }
86
87     private void on_plugin_notify (Plugin    plugin,
88                                    ParamSpec spec) {
89         foreach (var server in this.media_servers) {
90             if (server.resource_factory == plugin) {
91                 server.available = plugin.available;
92             }
93         }
94     }
95
96     public static int main (string[] args) {
97         Main main;
98
99         // initialize gstreamer
100         Gst.init (ref args);
101
102         try {
103             main = new Main ();
104         } catch (GLib.Error err) {
105             error ("%s", err.message);
106
107             return -1;
108         }
109
110         int exit_code = main.run ();
111
112         return exit_code;
113     }
114 }
115