e5b79761785633c209987d4545e2aa88ba57e305
[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         private weak Osso.Context osso_context;
26
27         public override void hello (Gtk.Window window, Osso.Context context) {
28                 string filename = Path.build_filename (Environment.get_user_cache_dir(),
29                                                        "cinaest", "imdb.db", null);
30                 stdout.printf ("IMDb Plugin Loaded. Cache Db: %s\n", filename);
31
32                 if (!FileUtils.test (filename, FileTest.EXISTS)) {
33                         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.");
34
35                         var response = note.run ();
36                         note.destroy ();
37
38                         if (response == ResponseType.OK) {
39                                 download_imdb (window, false);
40                         }
41                 }
42
43                 var source = new IMDBSource ();
44
45                 sources = new List<MovieSource> ();
46                 sources.append (source);
47
48                 osso_context = context;
49
50                 // FIXME - this forces the inclusion of config.h
51                 (void) Config.GETTEXT_PACKAGE;
52         }
53
54         private void download_imdb (Gtk.Window window, bool plots) {
55                 var picker = new PickerDialog (window);
56                 var selector = new TouchSelector.text ();
57                 string[] mirrors;
58
59                 try {
60                         var conn = DBus.Bus.get (DBus.BusType.SESSION);
61                         server = conn.get_object (IMDbDownloader.DBUS_SERVICE,
62                                                   IMDbDownloader.DBUS_OBJECT,
63                                                   IMDbDownloader.DBUS_IFACE);
64
65                         mirrors = server.get_mirrors ();
66                 } catch (DBus.Error e) {
67                         warning ("Failed connect to IMDb downloader: %s", e.message);
68                         return;
69                 }
70
71                 foreach (string mirror in mirrors) {
72                         selector.append_text (mirror);
73                 }
74
75                 picker.set_title ("Please select a source mirror");
76                 picker.set_selector (selector);
77
78                 int res = picker.run();
79                 string mirror = selector.get_current_text ();
80                 picker.destroy();
81
82                 if (res == ResponseType.OK) {
83                         var download = new IMDbDownloadDialog (window, plots);
84                         download.run (server, mirror);
85                         download.destroy ();
86                 }
87         }
88
89         public override unowned List<MovieSource> get_sources () {
90                 return sources;
91         }
92
93         public override List<MovieAction> get_actions (Movie movie, Gtk.Window window) {
94                 List<MovieAction> list = null;
95
96                 if (movie.year > 0)
97                         list.append (new MovieAction (_("IMDb page"), on_visit_imdb, movie, window));
98                 else
99                         list.append (new MovieAction (_("Lookup on IMDb"), on_visit_imdb, movie, window));
100
101                 return list;
102         }
103
104         private void on_visit_imdb (Movie movie, Gtk.Window window) {
105                 var url = "http://www.imdb.com/find?s=tt&q=" + movie.title.replace(" ", "+");
106                 if (movie.year > 0)
107                         url += "+(%d)".printf (movie.year);
108
109                 var status = osso_context.rpc_run_with_defaults ("osso_browser", "open_new_window", null, 's', url, 'b', false);
110                 if (status != Osso.Status.OK) {
111                         var banner = (Banner) Hildon.Banner.show_information_with_markup (window, null, "Failed to open browser.");
112                         banner.set_timeout (1500);
113                 }
114         }
115
116         public override void settings_dialog (Gtk.Window window) {
117                 var dialog = new Gtk.Dialog ();
118                 dialog.set_transient_for (window);
119                 dialog.set_title (_("IMDb plugin settings"));
120
121                 bool download_plots;
122                 try {
123                         var sqlite = new IMDbSqlite (Path.build_filename (Environment.get_user_cache_dir (),
124                                                      "cinaest", "imdb.db", null));
125                         download_plots = sqlite.has_plots ();
126                 } catch (Error e) {
127                         download_plots = false;
128                 }
129
130                 var plots = new Hildon.CheckButton (SizeType.FINGER_HEIGHT);
131                 plots.set_label (_("Download and store movie plots"));
132                 plots.set_active (download_plots);
133
134                 string updated;
135                 string filename = Path.build_filename (Environment.get_user_cache_dir(),
136                                                        "cinaest", "imdb.db", null);
137                 var file = File.new_for_path (filename);
138                 try {
139                         var info = file.query_info (FILE_ATTRIBUTE_TIME_MODIFIED, FileQueryInfoFlags.NONE, null);
140                         TimeVal tv;
141                         info.get_modification_time (out tv);
142
143                         var date = Date ();
144                         date.set_time_val (tv);
145                         char[] s = new char[64];
146                         date.strftime (s, "%x");
147                         updated = (string) s;
148                 } catch (Error e) {
149                         updated = _("unknown");
150                 }
151
152                 var download = new Hildon.Button.with_text (SizeType.FINGER_HEIGHT, ButtonArrangement.VERTICAL, _("Download"), _("Last update: ") + updated);
153
154                 VBox content = (VBox) dialog.get_content_area ();
155                 content.pack_start (plots, true, true, 0);
156                 content.pack_start (download, true, true, 0);
157
158                 var sizegroup = new Gtk.SizeGroup (SizeGroupMode.HORIZONTAL);
159
160                 // User name
161                 var hbox = new Gtk.HBox (false, MARGIN_DOUBLE);
162                 var label = new Gtk.Label (_("User name"));
163                 label.set_alignment (0, 0.5f);
164                 sizegroup.add_widget (label);
165                 var entry = new Hildon.Entry (SizeType.FINGER_HEIGHT);
166                 hbox.pack_start (label, false, false, 0);
167                 hbox.pack_start (entry, true, true, 0);
168                 content.pack_start (hbox, true, true, 0);
169
170                 // Password
171                 hbox = new Gtk.HBox (false, MARGIN_DOUBLE);
172                 label = new Gtk.Label ("Password");
173                 label.set_alignment (0, 0.5f);
174                 sizegroup.add_widget (label);
175                 entry = new Hildon.Entry (SizeType.FINGER_HEIGHT);
176                 hbox.pack_start (label, false, false, 0);
177                 hbox.pack_start (entry, true, true, 0);
178                 content.pack_start (hbox, true, true, 0);
179
180                 dialog.add_button (_("Save"), ResponseType.ACCEPT);
181
182                 // Connect signals
183                 plots.toggled.connect (() => {
184                         if (download_plots != plots.get_active ())
185                                 Hildon.Banner.show_information (window, null, _("Redownload the IMDb for this change to take effect."));
186                 });
187                 download.clicked.connect (() => {
188                         download_imdb (window, plots.get_active ());
189                 });
190
191                 dialog.show_all ();
192                 dialog.run ();
193                 dialog.destroy ();
194         }
195
196         public override unowned string get_name () {
197                 return "IMDb";
198         }
199 }
200
201 class IMDbMovie : Movie {
202         public override string get_plot () {
203                 var sqlite = new IMDbSqlite (Path.build_filename (Environment.get_user_cache_dir (),
204                                              "cinaest", "imdb.db", null));
205                 print ("IMDb get_plot(\"%s (%d)\")\n", title, year);
206                 return sqlite.get_plot ("%s (%d)".printf (title, year));
207         }
208 }
209
210 class IMDBSource : MovieSource {
211         SList<Movie> result;
212         int results_waiting;
213
214         public override bool active { get; set construct; }
215
216         public IMDBSource () {
217                 GLib.Object (active: true);
218         }
219
220         MovieSource.ReceiveMovieFunction _get_callback;
221         public override async int get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
222                 var sqlite = new IMDbSqlite (Path.build_filename (Environment.get_user_cache_dir (),
223                                              "cinaest", "imdb.db", null));
224
225                 _get_callback = callback;
226                 result = new SList<Movie> ();
227                 results_waiting = 0;
228                 int n = yield sqlite.query (filter, receive_movie, limit, cancellable);
229                 if (results_waiting > 0)
230                         _get_callback (result);
231                 result = new SList<Movie> ();
232                 return n;
233         }
234
235         private void receive_movie (string title, string? aka, int year, int rating, int genres) {
236                 var movie = new IMDbMovie ();
237                 movie.title = title;
238                 movie.year = year;
239                 movie.rating = rating;
240                 movie.genres.field = genres;
241                 // TODO - depending on settings, this could be something else, like director info or runtime
242                 if (aka != null) {
243                         movie.secondary = "aka \"%s\" - %s".printf (aka, movie.genres.to_string ());
244                 } else {
245                         movie.secondary = movie.genres.to_string ();
246                 }
247                 result.append (movie);
248                 if (++results_waiting >= 10) {
249                         _get_callback (result);
250                         result = new SList<Movie> ();
251                         results_waiting = 0;
252                 }
253         }
254
255         public override void add_movie (Movie movie) {
256         }
257
258         public override void delete_movie (Movie movie) {
259         }
260
261         public override unowned string get_name () {
262                 return "IMDb";
263         }
264
265         public override unowned string get_description () {
266                 return _("Movies on IMDb");
267         }
268
269         public override SourceFlags get_flags () {
270                 return 0;
271         }
272 }
273
274 [ModuleInit]
275 public Type register_plugin (TypeModule module) {
276         // types are registered automatically
277         return typeof (IMDbPlugin);
278 }