IMDb plugin: split MovieSource from Plugin and implement get_movies using SQLite
[cinaest] / src / plugins / imdb-plugin.vala
index c4a3f01..cd045e8 100644 (file)
  * along with Cinaest. If not, see <http://www.gnu.org/licenses/>.
  */
 
+using Gtk;
 using Hildon;
 
 class IMDbPlugin : Plugin {
+       private dynamic DBus.Object server;
+       List<MovieSource> sources;
+
        public override void hello (Gtk.Window window) {
                string filename = Path.build_filename (Environment.get_user_cache_dir(),
                                                       "cinaest", "imdb.db", null);
@@ -27,16 +31,88 @@ class IMDbPlugin : Plugin {
                if (!FileUtils.test (filename, FileTest.EXISTS)) {
                        var note = new Note.confirmation (window, "There is no local copy of the Internet Movie Database (IMDb). Download it now?\n\nThis can be started later from the Settings dialog.");
 
-                       note.run ();
+                       var response = note.run ();
                        note.destroy ();
+
+                       if (response == ResponseType.OK) {
+                               download_imdb (window);
+                       }
                }
+
+               var source = new IMDBSource ();
+
+               sources = new List<MovieSource> ();
+               sources.append (source);
        }
 
-       public override void get_movies (string filter, Plugin.ReceiveMovieFunction callback, int limit) {
+       private void download_imdb (Gtk.Window window) {
+               var picker = new PickerDialog (window);
+               var selector = new TouchSelector.text ();
+               string[] mirrors;
+
+               try {
+                       var conn = DBus.Bus.get (DBus.BusType.SESSION);
+                       server = conn.get_object (IMDbDownloader.DBUS_SERVICE,
+                                                 IMDbDownloader.DBUS_OBJECT,
+                                                 IMDbDownloader.DBUS_IFACE);
+
+                       mirrors = server.get_mirrors ();
+               } catch (DBus.Error e) {
+                       warning ("Failed connect to IMDb downloader: %s", e.message);
+                       return;
+               }
+
+               foreach (string mirror in mirrors) {
+                       selector.append_text (mirror);
+               }
+
+               picker.set_title ("Please select a source mirror");
+               picker.set_selector (selector);
+
+               int res = picker.run();
+               string mirror = selector.get_current_text ();
+               picker.destroy();
+
+               if (res == ResponseType.OK) {
+                       var download = new IMDbDownloadDialog (window);
+                       download.run (server, mirror);
+                       download.destroy ();
+               }
+       }
+
+       public override unowned List<MovieSource> get_sources () {
+               return sources;
+       }
+}
+
+class IMDBSource : MovieSource {
+       MovieSource.ReceiveMovieFunction _get_callback;
+       public override void get_movies (string filter, MovieSource.ReceiveMovieFunction callback, int limit) {
+               var sqlite = new IMDbSqlite (Path.build_filename (Environment.get_user_cache_dir (),
+                                            "cinaest", "imdb.db", null));
+
+               _get_callback = callback;
+               sqlite.query (filter, receive_movie);
+       }
+
+       private void receive_movie (string title, int year, int rating) {
+               Movie movie = new Movie ();
+               movie.title = title;
+               movie.year = year;
+               movie.rating = rating;
+               _get_callback (movie);
        }
 
        public override void add_movie (Movie movie) {
        }
+
+       public override unowned string get_name () {
+               return "IMDb";
+       }
+
+       public override unowned string get_description () {
+               return "Movies on IMDb";
+       }
 }
 
 [ModuleInit]