Add movie filter class
[cinaest] / src / plugins / imdb-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 IMDbPlugin : Plugin {
23         private dynamic DBus.Object server;
24         List<MovieSource> sources;
25
26         public override void hello (Gtk.Window window) {
27                 string filename = Path.build_filename (Environment.get_user_cache_dir(),
28                                                        "cinaest", "imdb.db", null);
29                 stdout.printf ("IMDb Plugin Loaded. Cache Db: %s\n", filename);
30
31                 if (!FileUtils.test (filename, FileTest.EXISTS)) {
32                         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.");
33
34                         var response = note.run ();
35                         note.destroy ();
36
37                         if (response == ResponseType.OK) {
38                                 download_imdb (window);
39                         }
40                 }
41
42                 var source = new IMDBSource ();
43
44                 sources = new List<MovieSource> ();
45                 sources.append (source);
46         }
47
48         private void download_imdb (Gtk.Window window) {
49                 var picker = new PickerDialog (window);
50                 var selector = new TouchSelector.text ();
51                 string[] mirrors;
52
53                 try {
54                         var conn = DBus.Bus.get (DBus.BusType.SESSION);
55                         server = conn.get_object (IMDbDownloader.DBUS_SERVICE,
56                                                   IMDbDownloader.DBUS_OBJECT,
57                                                   IMDbDownloader.DBUS_IFACE);
58
59                         mirrors = server.get_mirrors ();
60                 } catch (DBus.Error e) {
61                         warning ("Failed connect to IMDb downloader: %s", e.message);
62                         return;
63                 }
64
65                 foreach (string mirror in mirrors) {
66                         selector.append_text (mirror);
67                 }
68
69                 picker.set_title ("Please select a source mirror");
70                 picker.set_selector (selector);
71
72                 int res = picker.run();
73                 string mirror = selector.get_current_text ();
74                 picker.destroy();
75
76                 if (res == ResponseType.OK) {
77                         var download = new IMDbDownloadDialog (window);
78                         download.run (server, mirror);
79                         download.destroy ();
80                 }
81         }
82
83         public override unowned List<MovieSource> get_sources () {
84                 return sources;
85         }
86 }
87
88 class IMDBSource : MovieSource {
89         MovieSource.ReceiveMovieFunction _get_callback;
90         public override void get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit) {
91                 var sqlite = new IMDbSqlite (Path.build_filename (Environment.get_user_cache_dir (),
92                                              "cinaest", "imdb.db", null));
93
94                 _get_callback = callback;
95                 sqlite.query (filter, receive_movie);
96         }
97
98         private void receive_movie (string title, int year, int rating) {
99                 Movie movie = new Movie ();
100                 movie.title = title;
101                 movie.year = year;
102                 movie.rating = rating;
103                 _get_callback (movie);
104         }
105
106         public override void add_movie (Movie movie) {
107         }
108
109         public override unowned string get_name () {
110                 return "IMDb";
111         }
112
113         public override unowned string get_description () {
114                 return "Movies on IMDb";
115         }
116 }
117
118 [ModuleInit]
119 public Type register_plugin () {
120         // types are registered automatically
121         return typeof (IMDbPlugin);
122 }