Google plugin: enable the settings dialog "Done"-button string for translation
[cinaest] / src / plugins / google-plugin.vala
index 7a165f1..060dd1b 100644 (file)
@@ -21,8 +21,10 @@ using Hildon;
 
 class GooglePlugin : Plugin {
        List<MovieSource> sources;
+       List<string> locations;
+       string last_location;
 
-       public override void hello (Gtk.Window window) {
+       public override void hello (Gtk.Window window, Osso.Context context) {
                stdout.printf ("Google Plugin Loaded.\n");
 
                var source = new GoogleSource ();
@@ -30,6 +32,26 @@ class GooglePlugin : Plugin {
                sources = new List<MovieSource> ();
                sources.append (source);
 
+               locations = null;
+               try {
+                       var config_file = Path.build_filename (Environment.get_user_config_dir (), "cinaest", "cinaest.cfg");
+                       var keyfile = new KeyFile ();
+                       if (keyfile.load_from_file (config_file, KeyFileFlags.NONE)
+                           && keyfile.has_group ("GooglePlugin")) {
+                               if (keyfile.has_key ("GooglePlugin", "KnownLocations")) {
+                                       var l = keyfile.get_string_list ("GooglePlugin", "KnownLocations");
+                                       for (int i = 0; i < l.length; i++)
+                                               locations.append (l[i]);
+                               }
+                               if (keyfile.has_key ("GooglePlugin", "LastLocation")) {
+                                       source.location = last_location = keyfile.get_string ("GooglePlugin", "LastLocation");
+                               }
+                       }
+               } catch (Error e) {
+                       if (!(e is KeyFileError.NOT_FOUND))
+                               stdout.printf ("Error loading configuration: %s\n", e.message);
+               }
+
                // FIXME - this forces the inclusion of config.h
                (void) Config.GETTEXT_PACKAGE;
        }
@@ -38,6 +60,79 @@ class GooglePlugin : Plugin {
                return sources;
        }
 
+       public override List<MovieAction> get_actions (Movie _movie, Gtk.Window window) {
+               List<MovieAction> list = null;
+               var movie = _movie as GoogleMovie;
+
+               if (movie != null) {
+                       list.append (new MovieAction (_("Add to calendar"), on_add_calendar_event, movie, window));
+                       if (movie.cinema != null && movie.cinema.phone != null)
+                               list.append (new MovieAction (_("Call cinema"), on_call_cinema, movie, window));
+               }
+
+               return list;
+       }
+
+       private void on_add_calendar_event (Movie _movie, Gtk.Window window) {
+               var movie = (GoogleMovie) _movie;
+
+               var dialog = new PickerDialog (window);
+               dialog.set_title (_("Add showtime to calendar"));
+
+               var selector = new TouchSelector.text ();
+               var showtimes = movie.showtimes.split (", ");
+               foreach (string s in showtimes) {
+                       selector.append_text (s);
+               }
+               dialog.set_selector (selector);
+
+               var res = dialog.run ();
+               if (res == ResponseType.OK) {
+                       string s = selector.get_current_text ();
+                        int hour = s.to_int ();
+                        int min = s.str (":").offset (1).to_int ();
+
+                       var showtime = time_t ();
+                       var timeinfo = Time.local (showtime);
+                       timeinfo.second = 0;
+                       timeinfo.minute = min;
+                       timeinfo.hour = hour;
+                       showtime = timeinfo.mktime ();
+
+                       int runtime = 3600 * movie.runtime.to_int () + 60 * movie.runtime.str ("hr ").offset (3).to_int ();
+                       if (runtime == 0) {
+                               // Default to 120min if we failed to parse the runtime
+                               runtime = 7200;
+                       }
+
+                       res = Calendar.add_event (movie.title, _("Movie"), movie.cinema.name, showtime, showtime + runtime);
+                       var banner = (Banner) Banner.show_information_with_markup (window, null, (res == 0) ?
+                                                                                  _("Added calendar event at %d:%02d").printf (hour, min) :
+                                                                                  _("Failed to add calendar event"));
+                       banner.set_timeout (1500);
+               }
+               dialog.destroy ();
+       }
+
+       private void on_call_cinema (Movie _movie, Gtk.Window window) {
+               var movie = (GoogleMovie) _movie;
+               var url = "tel://" + movie.cinema.phone;
+
+               try {
+                       var action = Hildon.URIAction.get_default_action_by_uri (url);
+                       if (action != null) {
+                               action.open (url);
+                       } else {
+                               var banner = (Banner) Banner.show_information_with_markup (window, null, "Failed to get tel:// URI action");
+                               banner.set_timeout (1500);
+                       }
+               } catch (Error e) {
+                       if (e is Hildon.URIError) {
+                               stdout.printf ("Error: %s\n", e.message);
+                       }
+               }
+       }
+
        public override void settings_dialog (Gtk.Window window) {
                GoogleSource source = (GoogleSource) sources.data;
                var dialog = new Gtk.Dialog ();
@@ -45,7 +140,9 @@ class GooglePlugin : Plugin {
                dialog.set_title (_("Google plugin settings"));
 
                var selector = new TouchSelectorEntry.text ();
-               selector.append_text ("Berlin");
+               insert_location_sorted (source.location);
+               foreach (string l in locations)
+                       selector.append_text (l);
 
                var button = new PickerButton (SizeType.FINGER_HEIGHT, ButtonArrangement.HORIZONTAL);
                button.set_title (_("Location"));
@@ -55,16 +152,67 @@ class GooglePlugin : Plugin {
                var content = (VBox) dialog.get_content_area ();
                content.pack_start (button, true, true, 0);
 
-               dialog.add_button ("Done", ResponseType.ACCEPT);
+               dialog.add_button (_("Done"), ResponseType.ACCEPT);
 
                dialog.show_all ();
                int res = dialog.run ();
                if (res == ResponseType.ACCEPT) {
                        source.location = button.get_value ();
+                       if (insert_location_sorted (source.location) || source.location != last_location) {
+                               var config_dir = Path.build_filename (Environment.get_user_config_dir (), "cinaest");
+                               var config_file = Path.build_filename (config_dir, "cinaest.cfg");
+
+                               // Make sure the directory is available
+                               DirUtils.create_with_parents (config_dir, 0770);
+
+                               var keyfile = new KeyFile ();
+                               try {
+                                       keyfile.load_from_file (config_file, KeyFileFlags.NONE);
+                               } catch (Error e) {
+                                       if (!(e is KeyFileError.NOT_FOUND))
+                                               stdout.printf ("Error loading configuration: %s\n", e.message);
+                               }
+                               var l = new string[locations.length ()];
+                               for (int i = 0; i < l.length; i++) {
+                                       l[i] = locations.nth_data (i);
+                               }
+                               keyfile.set_string_list ("GooglePlugin", "KnownLocations", l);
+                               keyfile.set_string ("GooglePlugin", "LastLocation", source.location);
+                               last_location = source.location;
+
+                               try {
+                                       var file = File.new_for_path (config_file + ".part");
+                                       var stream = file.create (FileCreateFlags.REPLACE_DESTINATION, null);
+                                       var data = keyfile.to_data ();
+
+                                       stream.write (data, data.length, null);
+                                       FileUtils.rename (config_file + ".part", config_file);
+                               } catch (Error e) {
+                                       stdout.printf ("Failed to store configuration: %s\n", e.message);
+                               }
+                       }
                }
                dialog.destroy ();
        }
 
+       private bool insert_location_sorted (string? location) {
+               if (location == null)
+                       return false;
+               if (locations != null) {
+                       for (unowned List<string> l = locations.first (); l != null; l = l.next) {
+                               if (l.data == location) {
+                                       return false;
+                               }
+                               if (l.data > location) {
+                                       l.insert (location, 0);
+                                       return true;
+                               }
+                       }
+               }
+               locations.append (location);
+               return true;
+       }
+
        public override unowned string get_name () {
                return "Google";
        }
@@ -74,13 +222,27 @@ class GoogleSource : MovieSource {
        public string location;
        public string description;
 
-       public override void get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
-               var parser = new GoogleParser (filter, location, callback, cancellable);
+       public override bool active { get; set construct; }
+
+       public GoogleSource () {
+               GLib.Object (active: true);
+       }
+
+       public override async void get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
+               var parser = new GoogleParser ();
+
+               yield parser.query (filter, location, callback, cancellable);
+               if (location == null) {
+                       location = parser.location;
+               }
        }
 
        public override void add_movie (Movie movie) {
        }
 
+       public override void delete_movie (Movie movie) {
+       }
+
        public override unowned string get_name () {
                return _("Google");
        }
@@ -93,6 +255,10 @@ class GoogleSource : MovieSource {
                }
                return description;
        }
+
+       public override bool get_editable () {
+               return false;
+       }
 }
 
 [ModuleInit]