1 /* This file is part of Cinaest.
3 * Copyright (C) 2009 Philipp Zabel
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.
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.
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/>.
22 public class GoogleMovie : Movie {
23 public string cinema_name;
24 public string cinema_phone;
26 public string showtimes;
29 class GooglePlugin : Plugin {
30 List<MovieSource> sources;
31 SList<string> locations;
35 public override void hello (Gtk.Window window, Osso.Context context) {
36 stdout.printf ("Google Plugin Loaded.\n");
38 var source = new GoogleSource ();
40 sources = new List<MovieSource> ();
41 sources.append (source);
44 gconf = GConf.Client.get_default ();
46 source.location = last_location = gconf.get_string ("/apps/cinaest/location");
47 locations = gconf.get_list ("/apps/cinaest/locations", GConf.ValueType.STRING);
49 stdout.printf ("Error getting GConf option: %s\n", e.message);
52 // FIXME - this forces the inclusion of config.h
53 (void) Config.GETTEXT_PACKAGE;
56 public override unowned List<MovieSource> get_sources () {
60 public override List<MovieAction> get_actions (Movie _movie, Gtk.Window window) {
61 List<MovieAction> list = null;
62 var movie = _movie as GoogleMovie;
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));
73 private void on_add_calendar_event (Movie _movie, Gtk.Window window) {
74 var movie = (GoogleMovie) _movie;
76 var dialog = new PickerDialog (window);
77 dialog.set_title (_("Add showtime to calendar"));
79 var selector = new TouchSelector.text ();
80 var showtimes = movie.showtimes.split (", ");
81 foreach (string s in showtimes) {
82 selector.append_text (s);
84 dialog.set_selector (selector);
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 ();
92 var showtime = time_t ();
93 var timeinfo = Time.local (showtime);
95 timeinfo.minute = min;
97 showtime = timeinfo.mktime ();
99 int runtime = movie.runtime;
101 // Default to 120min if we failed to parse the runtime
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);
114 private void on_call_cinema (Movie _movie, Gtk.Window window) {
115 var movie = (GoogleMovie) _movie;
116 var url = "tel://" + movie.cinema_phone;
119 var action = Hildon.URIAction.get_default_action_by_uri (url);
120 if (action != null) {
123 var banner = (Banner) Banner.show_information_with_markup (window, null, "Failed to get tel:// URI action");
124 banner.set_timeout (1500);
127 if (e is Hildon.URIError) {
128 stdout.printf ("Error: %s\n", e.message);
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"));
139 source.location = gconf.get_string ("/apps/cinaest/location");
140 locations = gconf.get_list ("/apps/cinaest/locations", GConf.ValueType.STRING);
142 stdout.printf ("Error getting GConf option: %s\n", e.message);
145 var selector = new TouchSelectorEntry.text ();
146 insert_location_sorted (source.location);
147 foreach (string l in locations)
148 selector.append_text (l);
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);
155 var content = (VBox) dialog.get_content_area ();
156 content.pack_start (button, true, true, 0);
158 dialog.add_button (_("Done"), ResponseType.ACCEPT);
161 int res = dialog.run ();
162 if (res == ResponseType.ACCEPT) {
163 source.location = button.get_value ();
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);
170 stdout.printf ("Error setting GConf option: %s\n", e.message);
176 private bool insert_location_sorted (string? location) {
177 if (location == null)
179 if (locations != null) {
180 for (unowned SList<string> l = locations; l != null; l = l.next) {
181 if (l.data == location) {
184 if (l.data > location) {
185 l.insert (location, 0);
190 locations.append (location);
194 public override unowned string get_name () {
199 class GoogleSource : MovieSource {
200 public string location;
201 public string description;
202 public MovieSource.ReceiveMovieFunction callback;
203 dynamic DBus.Object search;
205 public override bool active { get; set construct; }
207 public GoogleSource () {
208 GLib.Object (active: true);
211 SourceFunc get_movies_callback;
212 public override async int get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction _callback, int limit, Cancellable? cancellable) {
215 dynamic DBus.Object server;
216 var conn = DBus.Bus.get (DBus.BusType.SESSION);
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);
223 search = conn.get_object ("org.maemo.cinaest.GoogleShowtimes",
225 "org.maemo.cinaest.MovieSearch");
227 callback = _callback;
228 search.MoviesFound.connect (on_movies_found);
229 search.start (filter.title);
231 Banner.show_information (null, null, e1.message);
235 get_movies_callback = get_movies.callback;
236 if (cancellable != null)
237 cancellable.cancelled.connect (() => { search.abort (); Idle.add (get_movies_callback); });
240 var gc = GConf.Client.get_default ();
242 location = gc.get_string ("/apps/cinaest/location");
244 stdout.printf ("Error getting GConf option: %s\n", e.message);
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 ();
254 for (int i = 0; i < movies.length; i++) {
255 var movie = new GoogleMovie ();
257 parser.load_from_data (movies[i], -1);
259 stderr.printf ("Error: %s\n%s\n", e.message, movies[i]);
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);
272 movie.secondary = movie.cinema_name + " - " + movie.showtimes;
279 Idle.add (get_movies_callback);
283 public override void add_movie (Movie movie) {
286 public override void delete_movie (Movie movie) {
289 public override unowned string get_name () {
293 public override unowned string get_description () {
294 if (location != null && location != "") {
295 description = _("Movie Showtimes near %s").printf (location);
297 description = _("Movie Showtimes");
302 public override bool get_editable () {
308 public Type register_plugin (TypeModule module) {
309 // types are registered automatically
310 return typeof (GooglePlugin);