Google plugin: enable the settings dialog "Done"-button string for translation
[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                         if (movie.cinema != null && movie.cinema.phone != null)
70                                 list.append (new MovieAction (_("Call cinema"), on_call_cinema, movie, window));
71                 }
72
73                 return list;
74         }
75
76         private void on_add_calendar_event (Movie _movie, Gtk.Window window) {
77                 var movie = (GoogleMovie) _movie;
78
79                 var dialog = new PickerDialog (window);
80                 dialog.set_title (_("Add showtime to calendar"));
81
82                 var selector = new TouchSelector.text ();
83                 var showtimes = movie.showtimes.split (", ");
84                 foreach (string s in showtimes) {
85                         selector.append_text (s);
86                 }
87                 dialog.set_selector (selector);
88
89                 var res = dialog.run ();
90                 if (res == ResponseType.OK) {
91                         string s = selector.get_current_text ();
92                         int hour = s.to_int ();
93                         int min = s.str (":").offset (1).to_int ();
94
95                         var showtime = time_t ();
96                         var timeinfo = Time.local (showtime);
97                         timeinfo.second = 0;
98                         timeinfo.minute = min;
99                         timeinfo.hour = hour;
100                         showtime = timeinfo.mktime ();
101
102                         int runtime = 3600 * movie.runtime.to_int () + 60 * movie.runtime.str ("hr ").offset (3).to_int ();
103                         if (runtime == 0) {
104                                 // Default to 120min if we failed to parse the runtime
105                                 runtime = 7200;
106                         }
107
108                         res = Calendar.add_event (movie.title, _("Movie"), movie.cinema.name, showtime, showtime + runtime);
109                         var banner = (Banner) Banner.show_information_with_markup (window, null, (res == 0) ?
110                                                                                    _("Added calendar event at %d:%02d").printf (hour, min) :
111                                                                                    _("Failed to add calendar event"));
112                         banner.set_timeout (1500);
113                 }
114                 dialog.destroy ();
115         }
116
117         private void on_call_cinema (Movie _movie, Gtk.Window window) {
118                 var movie = (GoogleMovie) _movie;
119                 var url = "tel://" + movie.cinema.phone;
120
121                 try {
122                         var action = Hildon.URIAction.get_default_action_by_uri (url);
123                         if (action != null) {
124                                 action.open (url);
125                         } else {
126                                 var banner = (Banner) Banner.show_information_with_markup (window, null, "Failed to get tel:// URI action");
127                                 banner.set_timeout (1500);
128                         }
129                 } catch (Error e) {
130                         if (e is Hildon.URIError) {
131                                 stdout.printf ("Error: %s\n", e.message);
132                         }
133                 }
134         }
135
136         public override void settings_dialog (Gtk.Window window) {
137                 GoogleSource source = (GoogleSource) sources.data;
138                 var dialog = new Gtk.Dialog ();
139                 dialog.set_transient_for (window);
140                 dialog.set_title (_("Google plugin settings"));
141
142                 var selector = new TouchSelectorEntry.text ();
143                 insert_location_sorted (source.location);
144                 foreach (string l in locations)
145                         selector.append_text (l);
146
147                 var button = new PickerButton (SizeType.FINGER_HEIGHT, ButtonArrangement.HORIZONTAL);
148                 button.set_title (_("Location"));
149                 button.set_selector (selector);
150                 button.set_value (source.location);
151
152                 var content = (VBox) dialog.get_content_area ();
153                 content.pack_start (button, true, true, 0);
154
155                 dialog.add_button (_("Done"), ResponseType.ACCEPT);
156
157                 dialog.show_all ();
158                 int res = dialog.run ();
159                 if (res == ResponseType.ACCEPT) {
160                         source.location = button.get_value ();
161                         if (insert_location_sorted (source.location) || source.location != last_location) {
162                                 var config_dir = Path.build_filename (Environment.get_user_config_dir (), "cinaest");
163                                 var config_file = Path.build_filename (config_dir, "cinaest.cfg");
164
165                                 // Make sure the directory is available
166                                 DirUtils.create_with_parents (config_dir, 0770);
167
168                                 var keyfile = new KeyFile ();
169                                 try {
170                                         keyfile.load_from_file (config_file, KeyFileFlags.NONE);
171                                 } catch (Error e) {
172                                         if (!(e is KeyFileError.NOT_FOUND))
173                                                 stdout.printf ("Error loading configuration: %s\n", e.message);
174                                 }
175                                 var l = new string[locations.length ()];
176                                 for (int i = 0; i < l.length; i++) {
177                                         l[i] = locations.nth_data (i);
178                                 }
179                                 keyfile.set_string_list ("GooglePlugin", "KnownLocations", l);
180                                 keyfile.set_string ("GooglePlugin", "LastLocation", source.location);
181                                 last_location = source.location;
182
183                                 try {
184                                         var file = File.new_for_path (config_file + ".part");
185                                         var stream = file.create (FileCreateFlags.REPLACE_DESTINATION, null);
186                                         var data = keyfile.to_data ();
187
188                                         stream.write (data, data.length, null);
189                                         FileUtils.rename (config_file + ".part", config_file);
190                                 } catch (Error e) {
191                                         stdout.printf ("Failed to store configuration: %s\n", e.message);
192                                 }
193                         }
194                 }
195                 dialog.destroy ();
196         }
197
198         private bool insert_location_sorted (string? location) {
199                 if (location == null)
200                         return false;
201                 if (locations != null) {
202                         for (unowned List<string> l = locations.first (); l != null; l = l.next) {
203                                 if (l.data == location) {
204                                         return false;
205                                 }
206                                 if (l.data > location) {
207                                         l.insert (location, 0);
208                                         return true;
209                                 }
210                         }
211                 }
212                 locations.append (location);
213                 return true;
214         }
215
216         public override unowned string get_name () {
217                 return "Google";
218         }
219 }
220
221 class GoogleSource : MovieSource {
222         public string location;
223         public string description;
224
225         public override bool active { get; set construct; }
226
227         public GoogleSource () {
228                 GLib.Object (active: true);
229         }
230
231         public override async void get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
232                 var parser = new GoogleParser ();
233
234                 yield parser.query (filter, location, callback, cancellable);
235                 if (location == null) {
236                         location = parser.location;
237                 }
238         }
239
240         public override void add_movie (Movie movie) {
241         }
242
243         public override void delete_movie (Movie movie) {
244         }
245
246         public override unowned string get_name () {
247                 return _("Google");
248         }
249
250         public override unowned string get_description () {
251                 if (location != null && location != "") {
252                         description = _("Movie Showtimes near %s").printf (location);
253                 } else {
254                         description =  _("Movie Showtimes");
255                 }
256                 return description;
257         }
258
259         public override bool get_editable () {
260                 return false;
261         }
262 }
263
264 [ModuleInit]
265 public Type register_plugin () {
266         // types are registered automatically
267         return typeof (GooglePlugin);
268 }