Czech translation update (via transifex.net)
[cinaest] / src / main.vala
1 /* This file is part of Cinaest.
2  *
3  * Copyright (C) 2009 Philipp Zabel
4  *
5  * Cinaest is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Cinaest is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Cinaest. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using Hildon;
20
21 public class CinaestProgram : Hildon.Program {
22         internal SourceListWindow window;
23         internal WindowStack secondary_stack;
24         public static List<Plugin> plugins;
25         public static MovieSource reference;
26         public static Orientation orientation;
27
28         construct {
29                 Environment.set_application_name ("Cinæst");
30
31                 window = new SourceListWindow ();
32                 window.destroy.connect (Gtk.main_quit);
33
34                 reference = null;
35
36                 orientation = new Orientation ();
37                 orientation.changed.connect (on_orientation_changed);
38
39                 add_window (window);
40
41                 secondary_stack = new WindowStack ();
42         }
43
44         private void on_orientation_changed () {
45                 if (orientation.portrait) {
46                         Hildon.gtk_window_set_portrait_flags (window, PortraitFlags.REQUEST);
47                 } else {
48                         Hildon.gtk_window_set_portrait_flags (window, PortraitFlags.SUPPORT);
49                 }
50         }
51
52         public void register_plugins (Osso.Context context) {
53                 string plugin_path = Config.PKGLIBDIR;
54                 try {
55                         var directory = File.new_for_path (plugin_path);
56                         var enumerator = directory.enumerate_children (FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
57
58                         FileInfo file_info;
59                         while ((file_info = enumerator.next_file (null)) != null) {
60                                 string name = file_info.get_name ();
61
62                                 if (name.has_suffix (".so")) {
63                                         Plugin plugin;
64                                         string path = Path.build_filename (plugin_path, name, null);
65
66                                         var registrar = new PluginRegistrar<Plugin> (path);
67                                         registrar.load ();
68
69                                         plugin = registrar.new_object ();
70                                         plugins.append (plugin);
71                                         plugin.hello (window, context);
72                                 }
73                         }
74
75                 } catch (Error e) {
76                         stderr.printf ("Error: %s\n", e.message);
77                 }
78         }
79
80         static int osso_callback (string iface, string method, GLib.Array arguments, void* data, out Osso.Rpc rpc) {
81                 var app = (CinaestProgram) data;
82                 if (method == "OpenMovie") {
83                         if (arguments.length < 1) {
84                                 rpc.type = DBus.RawType.STRING;
85                                 ((OssoFix.Rpc) rpc).s = "OpenMovie missing movie parameter";
86                                 return Osso.Status.ERROR;
87                         }
88                         // Vala doesn't support GLib.Array<Osso.Rpc>, so we have to cast:
89                         Osso.Rpc* arg = *(Osso.Rpc**) arguments;
90                         if (arg[0].type != DBus.RawType.STRING) {
91                                 rpc.type = DBus.RawType.STRING;
92                                 ((OssoFix.Rpc) rpc).s = "OpenMovie first parameter must be string";
93                                 return Osso.Status.ERROR;
94                         }
95                         string title = arg[0].s;
96                         int year = 0;
97                         if (arguments.length >= 2) {
98                                 if (arg[1].type != DBus.RawType.INT32) {
99                                         rpc.type = DBus.RawType.STRING;
100                                         ((OssoFix.Rpc) rpc).s = "OpenMovie second parameter must be int";
101                                         return Osso.Status.ERROR;
102                                 }
103                                 year = arg[1].i;
104                         }
105                         if (reference == null) {
106                                 rpc.type = DBus.RawType.STRING;
107                                 ((OssoFix.Rpc) rpc).s = "No reference source installed";
108                                 return Osso.Status.ERROR;
109                         }
110                         print ("OpenMovie (\"%s\"", title);
111                         if (year > 0)
112                                 print (", %d", year);
113                         print (")\n");
114
115                         var list_window = new MovieListWindow (reference, title, year);
116                         var widget = app.secondary_stack.pop_1 ();
117                         while (widget != null) {
118                                 widget.destroy ();
119                                 widget = app.secondary_stack.pop_1 ();
120                         }
121                         app.secondary_stack.push_1 (list_window);
122                 }
123                 rpc.type = DBus.RawType.INVALID;
124                 return Osso.Status.OK;
125         }
126
127         public void run () {
128                 foreach (Plugin plugin in plugins) {
129                         window.add_sources (plugin.get_sources ());
130                         foreach (MovieSource source in plugin.get_sources ()) {
131                                 if (reference == null && SourceFlags.REFERENCE in source.get_flags ())
132                                         reference = source;
133                         }
134                 }
135                 Gtk.main ();
136         }
137
138         static int main (string[] args) {
139                 Gtk.init (ref args);
140
141                 Intl.setlocale (LocaleCategory.ALL, "");
142                 Intl.bindtextdomain (Config.GETTEXT_PACKAGE, Config.LOCALEDIR);
143                 Intl.textdomain (Config.GETTEXT_PACKAGE);
144
145                 var osso_context = new Osso.Context ("org.maemo.cinaest", Config.VERSION, true, null);
146                 if (osso_context == null) {
147                         return Osso.Status.ERROR;
148                 }
149
150                 CinaestProgram app = new CinaestProgram ();
151                 app.register_plugins (osso_context);
152                 osso_context.set_rpc_default_callback (osso_callback, app);
153                 app.run ();
154
155                 return 0;
156         }
157 }
158