Google plugin: report D-Bus errors
[cinaest] / src / plugins / google-plugin.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 Gtk;
20 using Hildon;
21
22 public class GoogleMovie : Movie {
23         public string cinema_name;
24         public string cinema_phone;
25         public int runtime;
26         public string showtimes;
27 }
28
29 class GooglePlugin : Plugin {
30         List<MovieSource> sources;
31         SList<string> locations;
32         string last_location;
33         GConf.Client gconf;
34
35         public override void hello (Gtk.Window window, Osso.Context context) {
36                 stdout.printf ("Google Plugin Loaded.\n");
37
38                 var source = new GoogleSource ();
39
40                 sources = new List<MovieSource> ();
41                 sources.append (source);
42
43                 locations = null;
44                 gconf = GConf.Client.get_default ();
45                 try {
46                         source.location = last_location = gconf.get_string ("/apps/cinaest/location");
47                         locations = gconf.get_list ("/apps/cinaest/locations", GConf.ValueType.STRING);
48                 } catch (Error e) {
49                         stdout.printf ("Error getting GConf option: %s\n", e.message);
50                 }
51
52                 // FIXME - this forces the inclusion of config.h
53                 (void) Config.GETTEXT_PACKAGE;
54         }
55
56         public override unowned List<MovieSource> get_sources () {
57                 return sources;
58         }
59
60         public override List<MovieAction> get_actions (Movie _movie, Gtk.Window window) {
61                 List<MovieAction> list = null;
62                 var movie = _movie as GoogleMovie;
63
64                 if (movie != null) {
65                         list.append (new MovieAction (_("Add to calendar"), on_add_calendar_event, movie, window));
66                         if (movie.cinema_phone != null)
67                                 list.append (new MovieAction (_("Call cinema"), on_call_cinema, movie, window));
68                 }
69
70                 return list;
71         }
72
73         private void on_add_calendar_event (Movie _movie, Gtk.Window window) {
74                 var movie = (GoogleMovie) _movie;
75
76                 var dialog = new PickerDialog (window);
77                 dialog.set_title (_("Add showtime to calendar"));
78
79                 var selector = new TouchSelector.text ();
80                 var showtimes = movie.showtimes.split (", ");
81                 foreach (string s in showtimes) {
82                         selector.append_text (s);
83                 }
84                 dialog.set_selector (selector);
85
86                 var res = dialog.run ();
87                 if (res == ResponseType.OK) {
88                         string s = selector.get_current_text ();
89                         int hour = s.to_int ();
90                         int min = s.str (":").offset (1).to_int ();
91
92                         var showtime = time_t ();
93                         var timeinfo = Time.local (showtime);
94                         timeinfo.second = 0;
95                         timeinfo.minute = min;
96                         timeinfo.hour = hour;
97                         showtime = timeinfo.mktime ();
98
99                         int runtime = movie.runtime;
100                         if (runtime == 0) {
101                                 // Default to 120min if we failed to parse the runtime
102                                 runtime = 7200;
103                         }
104
105                         res = Calendar.add_event (movie.title, _("Movie"), movie.cinema_name, showtime, showtime + runtime);
106                         var banner = (Banner) Banner.show_information_with_markup (window, null, (res == 0) ?
107                                                                                    _("Added calendar event at %d:%02d").printf (hour, min) :
108                                                                                    _("Failed to add calendar event"));
109                         banner.set_timeout (1500);
110                 }
111                 dialog.destroy ();
112         }
113
114         private void on_call_cinema (Movie _movie, Gtk.Window window) {
115                 var movie = (GoogleMovie) _movie;
116                 var url = "tel://" + movie.cinema_phone;
117
118                 try {
119                         var action = Hildon.URIAction.get_default_action_by_uri (url);
120                         if (action != null) {
121                                 action.open (url);
122                         } else {
123                                 var banner = (Banner) Banner.show_information_with_markup (window, null, "Failed to get tel:// URI action");
124                                 banner.set_timeout (1500);
125                         }
126                 } catch (Error e) {
127                         if (e is Hildon.URIError) {
128                                 stdout.printf ("Error: %s\n", e.message);
129                         }
130                 }
131         }
132
133         public override void settings_dialog (Gtk.Window window) {
134                 GoogleSource source = (GoogleSource) sources.data;
135                 var dialog = new Gtk.Dialog ();
136                 dialog.set_transient_for (window);
137                 dialog.set_title (_("Google plugin settings"));
138                 try {
139                         source.location = gconf.get_string ("/apps/cinaest/location");
140                         locations = gconf.get_list ("/apps/cinaest/locations", GConf.ValueType.STRING);
141                 } catch (Error e) {
142                         stdout.printf ("Error getting GConf option: %s\n", e.message);
143                 }
144
145                 var selector = new TouchSelectorEntry.text ();
146                 insert_location_sorted (source.location);
147                 foreach (string l in locations)
148                         selector.append_text (l);
149
150                 var button = new PickerButton (SizeType.FINGER_HEIGHT, ButtonArrangement.HORIZONTAL);
151                 button.set_title (_("Location"));
152                 button.set_selector (selector);
153                 button.set_value (source.location);
154
155                 var content = (VBox) dialog.get_content_area ();
156                 content.pack_start (button, true, true, 0);
157
158                 dialog.add_button (_("Done"), ResponseType.ACCEPT);
159
160                 dialog.show_all ();
161                 int res = dialog.run ();
162                 if (res == ResponseType.ACCEPT) {
163                         source.location = button.get_value ();
164                         try {
165                                 if (insert_location_sorted (source.location))
166                                         gconf.set_list ("/apps/cinaest/locations", GConf.ValueType.STRING, locations);
167                                 if (source.location != last_location)
168                                         gconf.set_string ("/apps/cinaest/location", source.location);
169                         } catch (Error e) {
170                                 stdout.printf ("Error setting GConf option: %s\n", e.message);
171                         }
172                 }
173                 dialog.destroy ();
174         }
175
176         private bool insert_location_sorted (string? location) {
177                 if (location == null)
178                         return false;
179                 if (locations != null) {
180                         for (unowned SList<string> l = locations; l != null; l = l.next) {
181                                 if (l.data == location) {
182                                         return false;
183                                 }
184                                 if (l.data > location) {
185                                         l.insert (location, 0);
186                                         return true;
187                                 }
188                         }
189                 }
190                 locations.append (location);
191                 return true;
192         }
193
194         public override unowned string get_name () {
195                 return "Google";
196         }
197 }
198
199 class GoogleSource : MovieSource {
200         public string location;
201         public string description;
202         public MovieSource.ReceiveMovieFunction callback;
203         dynamic DBus.Object search;
204
205         public override bool active { get; set construct; }
206
207         public GoogleSource () {
208                 GLib.Object (active: true);
209         }
210
211         SourceFunc get_movies_callback;
212         public override async int get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction _callback, int limit, Cancellable? cancellable) {
213                 try {
214                         string search_path;
215                         dynamic DBus.Object server;
216                         var conn = DBus.Bus.get (DBus.BusType.SESSION);
217
218                         server = conn.get_object ("org.maemo.cinaest.GoogleShowtimes",
219                                                   "/org/maemo/cinaest/googleshowtimes",
220                                                   "org.maemo.cinaest.MovieService");
221                         server.NewSearch (out search_path);
222
223                         search = conn.get_object ("org.maemo.cinaest.GoogleShowtimes",
224                                                   search_path,
225                                                   "org.maemo.cinaest.MovieSearch");
226
227                         callback = _callback;
228                         search.MoviesFound.connect (on_movies_found);
229                         search.start (filter.title);
230                 } catch (Error e1) {
231                         Banner.show_information (null, null, e1.message);
232                         return 0;
233                 }
234
235                 get_movies_callback = get_movies.callback;
236                 if (cancellable != null)
237                         cancellable.cancelled.connect (() => { search.abort (); Idle.add (get_movies_callback); });
238                 yield;
239
240                 var gc = GConf.Client.get_default ();
241                 try {
242                         location = gc.get_string ("/apps/cinaest/location");
243                 } catch (Error e) {
244                         stdout.printf ("Error getting GConf option: %s\n", e.message);
245                 }
246
247                 return 1 /*n*/;
248         }
249
250         private void on_movies_found (DBus.Object sender, string[] movies, bool finished) {
251                 print ("found %d movies\n", movies.length);
252                 var parser = new Json.Parser ();
253
254                 for (int i = 0; i < movies.length; i++) {
255                         var movie = new GoogleMovie ();
256                         try {
257                                 parser.load_from_data (movies[i], -1);
258                         } catch (Error e) {
259                                 stderr.printf ("Error: %s\n%s\n", e.message, movies[i]);
260                         }
261
262                         var object = parser.get_root ().get_object ();
263                         movie.title = object.get_string_member ("title");
264                         movie.rating = (int) object.get_double_member ("rating");
265                         movie.cinema_name = object.get_string_member ("cinema_name");
266                         movie.cinema_phone = object.get_string_member ("cinema_phone");
267                         movie.runtime = (int) object.get_int_member ("runtime");
268                         movie.showtimes = object.get_string_member ("showtimes");
269                         if (movie.runtime > 0)
270                                 movie.secondary = "%d min - %s - %s".printf (movie.runtime / 60, movie.cinema_name, movie.showtimes);
271                         else
272                                 movie.secondary = movie.cinema_name + " - " + movie.showtimes;
273
274                         callback (movie);
275                 }
276
277                 if (finished) {
278                         search = null;
279                         Idle.add (get_movies_callback);
280                 }
281         }
282
283         public override void add_movie (Movie movie) {
284         }
285
286         public override void delete_movie (Movie movie) {
287         }
288
289         public override unowned string get_name () {
290                 return _("Google");
291         }
292
293         public override unowned string get_description () {
294                 if (location != null && location != "") {
295                         description = _("Movie Showtimes near %s").printf (location);
296                 } else {
297                         description =  _("Movie Showtimes");
298                 }
299                 return description;
300         }
301
302         public override bool get_editable () {
303                 return false;
304         }
305 }
306
307 [ModuleInit]
308 public Type register_plugin (TypeModule module) {
309         // types are registered automatically
310         return typeof (GooglePlugin);
311 }