Google plugin: store complete cinema information when parsing
[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 class GooglePlugin : Plugin {
23         List<MovieSource> sources;
24         List<string> locations;
25         string last_location;
26
27         public override void hello (Gtk.Window window, Osso.Context context) {
28                 stdout.printf ("Google Plugin Loaded.\n");
29
30                 var source = new GoogleSource ();
31
32                 sources = new List<MovieSource> ();
33                 sources.append (source);
34
35                 locations = null;
36                 try {
37                         var config_file = Path.build_filename (Environment.get_user_config_dir (), "cinaest", "cinaest.cfg");
38                         var keyfile = new KeyFile ();
39                         if (keyfile.load_from_file (config_file, KeyFileFlags.NONE)
40                             && keyfile.has_group ("GooglePlugin")) {
41                                 if (keyfile.has_key ("GooglePlugin", "KnownLocations")) {
42                                         var l = keyfile.get_string_list ("GooglePlugin", "KnownLocations");
43                                         for (int i = 0; i < l.length; i++)
44                                                 locations.append (l[i]);
45                                 }
46                                 if (keyfile.has_key ("GooglePlugin", "LastLocation")) {
47                                         source.location = last_location = keyfile.get_string ("GooglePlugin", "LastLocation");
48                                 }
49                         }
50                 } catch (Error e) {
51                         if (!(e is KeyFileError.NOT_FOUND))
52                                 stdout.printf ("Error loading configuration: %s\n", e.message);
53                 }
54
55                 // FIXME - this forces the inclusion of config.h
56                 (void) Config.GETTEXT_PACKAGE;
57         }
58
59         public override unowned List<MovieSource> get_sources () {
60                 return sources;
61         }
62
63         public override List<MovieAction> get_actions (Movie _movie, Gtk.Window window) {
64                 List<MovieAction> list = null;
65                 var movie = _movie as GoogleMovie;
66
67                 if (movie != null) {
68                         list.append (new MovieAction (_("Add to calendar"), on_add_calendar_event, movie, window));
69                 }
70
71                 return list;
72         }
73
74         private void on_add_calendar_event (Movie _movie, Gtk.Window window) {
75                 var movie = (GoogleMovie) _movie;
76
77                 var dialog = new PickerDialog (window);
78                 dialog.set_title (_("Add showtime to calendar"));
79
80                 var selector = new TouchSelector.text ();
81                 var showtimes = movie.showtimes.split (", ");
82                 foreach (string s in showtimes) {
83                         selector.append_text (s);
84                 }
85                 dialog.set_selector (selector);
86
87                 var res = dialog.run ();
88                 if (res == ResponseType.OK) {
89                         string s = selector.get_current_text ();
90                         int hour = s.to_int ();
91                         int min = s.str (":").offset (1).to_int ();
92
93                         var showtime = time_t ();
94                         var timeinfo = Time.local (showtime);
95                         timeinfo.second = 0;
96                         timeinfo.minute = min;
97                         timeinfo.hour = hour;
98                         showtime = timeinfo.mktime ();
99
100                         int runtime = 3600 * movie.runtime.to_int () + 60 * movie.runtime.str ("hr ").offset (3).to_int ();
101                         if (runtime == 0) {
102                                 // Default to 120min if we failed to parse the runtime
103                                 runtime = 7200;
104                         }
105
106                         res = Calendar.add_event (movie.title, _("Movie"), movie.cinema.name, showtime, showtime + runtime);
107                         var banner = (Banner) Banner.show_information_with_markup (window, null, (res == 0) ?
108                                                                                    _("Added calendar event at %d:%02d").printf (hour, min) :
109                                                                                    _("Failed to add calendar event"));
110                         banner.set_timeout (1500);
111                 }
112                 dialog.destroy ();
113         }
114
115         public override void settings_dialog (Gtk.Window window) {
116                 GoogleSource source = (GoogleSource) sources.data;
117                 var dialog = new Gtk.Dialog ();
118                 dialog.set_transient_for (window);
119                 dialog.set_title (_("Google plugin settings"));
120
121                 var selector = new TouchSelectorEntry.text ();
122                 insert_location_sorted (source.location);
123                 foreach (string l in locations)
124                         selector.append_text (l);
125
126                 var button = new PickerButton (SizeType.FINGER_HEIGHT, ButtonArrangement.HORIZONTAL);
127                 button.set_title (_("Location"));
128                 button.set_selector (selector);
129                 button.set_value (source.location);
130
131                 var content = (VBox) dialog.get_content_area ();
132                 content.pack_start (button, true, true, 0);
133
134                 dialog.add_button ("Done", ResponseType.ACCEPT);
135
136                 dialog.show_all ();
137                 int res = dialog.run ();
138                 if (res == ResponseType.ACCEPT) {
139                         source.location = button.get_value ();
140                         if (insert_location_sorted (source.location) || source.location != last_location) {
141                                 var config_dir = Path.build_filename (Environment.get_user_config_dir (), "cinaest");
142                                 var config_file = Path.build_filename (config_dir, "cinaest.cfg");
143
144                                 // Make sure the directory is available
145                                 DirUtils.create_with_parents (config_dir, 0770);
146
147                                 var keyfile = new KeyFile ();
148                                 try {
149                                         keyfile.load_from_file (config_file, KeyFileFlags.NONE);
150                                 } catch (Error e) {
151                                         if (!(e is KeyFileError.NOT_FOUND))
152                                                 stdout.printf ("Error loading configuration: %s\n", e.message);
153                                 }
154                                 var l = new string[locations.length ()];
155                                 for (int i = 0; i < l.length; i++) {
156                                         l[i] = locations.nth_data (i);
157                                 }
158                                 keyfile.set_string_list ("GooglePlugin", "KnownLocations", l);
159                                 keyfile.set_string ("GooglePlugin", "LastLocation", source.location);
160                                 last_location = source.location;
161
162                                 try {
163                                         var file = File.new_for_path (config_file + ".part");
164                                         var stream = file.create (FileCreateFlags.REPLACE_DESTINATION, null);
165                                         var data = keyfile.to_data ();
166
167                                         stream.write (data, data.length, null);
168                                         FileUtils.rename (config_file + ".part", config_file);
169                                 } catch (Error e) {
170                                         stdout.printf ("Failed to store configuration: %s\n", e.message);
171                                 }
172                         }
173                 }
174                 dialog.destroy ();
175         }
176
177         private bool insert_location_sorted (string? location) {
178                 if (location == null)
179                         return false;
180                 if (locations != null) {
181                         for (unowned List<string> l = locations.first (); l != null; l = l.next) {
182                                 if (l.data == location) {
183                                         return false;
184                                 }
185                                 if (l.data > location) {
186                                         l.insert (location, 0);
187                                         return true;
188                                 }
189                         }
190                 }
191                 locations.append (location);
192                 return true;
193         }
194
195         public override unowned string get_name () {
196                 return "Google";
197         }
198 }
199
200 class GoogleSource : MovieSource {
201         public string location;
202         public string description;
203
204         public override async void get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
205                 var parser = new GoogleParser ();
206
207                 yield parser.query (filter, location, callback, cancellable);
208                 if (location == null) {
209                         location = parser.location;
210                 }
211         }
212
213         public override void add_movie (Movie movie) {
214         }
215
216         public override void delete_movie (Movie movie) {
217         }
218
219         public override unowned string get_name () {
220                 return _("Google");
221         }
222
223         public override unowned string get_description () {
224                 if (location != null && location != "") {
225                         description = _("Movie Showtimes near %s").printf (location);
226                 } else {
227                         description =  _("Movie Showtimes");
228                 }
229                 return description;
230         }
231
232         public override bool get_editable () {
233                 return false;
234         }
235 }
236
237 [ModuleInit]
238 public Type register_plugin () {
239         // types are registered automatically
240         return typeof (GooglePlugin);
241 }