Google plugin: add "Add to calendar" movie action
authorPhilipp Zabel <philipp.zabel@gmail.com>
Tue, 24 Nov 2009 14:22:59 +0000 (15:22 +0100)
committerPhilipp Zabel <philipp.zabel@gmail.com>
Tue, 24 Nov 2009 14:54:57 +0000 (15:54 +0100)
Allows to add calendar events for movie showtimes from the movie window.

src/plugins/google-plugin.vala

index 38be5c1..bc00fee 100644 (file)
@@ -60,12 +60,58 @@ class GooglePlugin : Plugin {
                return sources;
        }
 
-       public override List<MovieAction> get_actions (Movie movie, Gtk.Window window) {
+       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));
+               }
 
                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, 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 ();
+       }
+
        public override void settings_dialog (Gtk.Window window) {
                GoogleSource source = (GoogleSource) sources.data;
                var dialog = new Gtk.Dialog ();