1 /* This file is part of Cinaest.
3 * Copyright (C) 2009 Philipp Zabel
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.
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.
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/>.
22 class IMDbPlugin : Plugin {
23 private dynamic DBus.Object server;
24 List<MovieSource> sources;
25 private weak Osso.Context osso_context;
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);
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.");
35 var response = note.run ();
38 if (response == ResponseType.OK) {
39 download_imdb (window, false, false);
43 var source = new IMDBSource ();
45 sources = new List<MovieSource> ();
46 sources.append (source);
48 osso_context = context;
50 // FIXME - this forces the inclusion of config.h
51 (void) Config.GETTEXT_PACKAGE;
54 private void download_imdb (Gtk.Window window, bool plots, bool actors) {
55 var picker = new PickerDialog (window);
56 var selector = new TouchSelector.text ();
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);
65 mirrors = server.get_mirrors ();
66 } catch (DBus.Error e) {
67 warning ("Failed connect to IMDb downloader: %s", e.message);
71 foreach (string mirror in mirrors) {
72 selector.append_text (mirror);
75 picker.set_title ("Please select a source mirror");
76 picker.set_selector (selector);
78 int res = picker.run();
79 string mirror = selector.get_current_text ();
82 if (res == ResponseType.OK) {
83 var download = new IMDbDownloadDialog (window, plots, actors);
84 download.run (server, mirror);
89 public override unowned List<MovieSource> get_sources () {
93 public override List<MovieAction> get_actions (Movie movie, Gtk.Window window) {
94 List<MovieAction> list = null;
97 list.append (new MovieAction (_("IMDb page"), on_visit_imdb, movie, window));
99 list.append (new MovieAction (_("Lookup on IMDb"), on_visit_imdb, movie, window));
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(" ", "+");
107 url += "+(%d)".printf (movie.year);
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);
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"));
122 bool download_actors;
124 var sqlite = new IMDbSqlite (Path.build_filename (Environment.get_user_cache_dir (),
125 "cinaest", "imdb.db", null));
126 download_plots = sqlite.has_table ("Plots");
127 download_actors = sqlite.has_table ("Actors");
129 download_plots = false;
130 download_actors = false;
133 var plots = new Hildon.CheckButton (SizeType.FINGER_HEIGHT);
134 plots.set_label (_("Download and store movie plots"));
135 plots.set_active (download_plots);
137 var actors = new Hildon.CheckButton (SizeType.FINGER_HEIGHT);
138 actors.set_label (_("Download and store actors and actresses"));
139 actors.set_active (download_actors);
142 string filename = Path.build_filename (Environment.get_user_cache_dir(),
143 "cinaest", "imdb.db", null);
144 var file = File.new_for_path (filename);
146 var info = file.query_info (FILE_ATTRIBUTE_TIME_MODIFIED, FileQueryInfoFlags.NONE, null);
148 info.get_modification_time (out tv);
151 date.set_time_val (tv);
152 char[] s = new char[64];
153 date.strftime (s, "%x");
154 updated = (string) s;
156 updated = _("unknown");
159 var download = new Hildon.Button.with_text (SizeType.FINGER_HEIGHT, ButtonArrangement.VERTICAL, _("Download"), _("Last update: ") + updated);
161 VBox content = (VBox) dialog.get_content_area ();
162 content.pack_start (plots, true, true, 0);
163 content.pack_start (actors, true, true, 0);
164 content.pack_start (download, true, true, 0);
166 var sizegroup = new Gtk.SizeGroup (SizeGroupMode.HORIZONTAL);
169 var hbox = new Gtk.HBox (false, MARGIN_DOUBLE);
170 var label = new Gtk.Label (_("User name"));
171 label.set_alignment (0, 0.5f);
172 sizegroup.add_widget (label);
173 var entry = new Hildon.Entry (SizeType.FINGER_HEIGHT);
174 hbox.pack_start (label, false, false, 0);
175 hbox.pack_start (entry, true, true, 0);
176 content.pack_start (hbox, true, true, 0);
179 hbox = new Gtk.HBox (false, MARGIN_DOUBLE);
180 label = new Gtk.Label (_("Password"));
181 label.set_alignment (0, 0.5f);
182 sizegroup.add_widget (label);
183 entry = new Hildon.Entry (SizeType.FINGER_HEIGHT);
184 hbox.pack_start (label, false, false, 0);
185 hbox.pack_start (entry, true, true, 0);
186 content.pack_start (hbox, true, true, 0);
188 dialog.add_button (_("Save"), ResponseType.ACCEPT);
191 plots.toggled.connect (() => {
192 if (download_plots != plots.get_active ())
193 Hildon.Banner.show_information (window, null, _("Redownload the IMDb for this change to take effect."));
195 actors.toggled.connect (() => {
196 if (download_actors != actors.get_active ())
197 Hildon.Banner.show_information (window, null, _("Redownload the IMDb for this change to take effect."));
199 download.clicked.connect (() => {
200 download_imdb (window, plots.get_active (), actors.get_active ());
208 public override unowned string get_name () {
213 class IMDbMovie : Movie {
214 public override string get_plot () {
215 var sqlite = new IMDbSqlite (Path.build_filename (Environment.get_user_cache_dir (),
216 "cinaest", "imdb.db", null));
217 print ("IMDb get_plot(\"%s (%d)\")\n", title, year);
218 return sqlite.get_plot ("%s (%d)".printf (title, year));
221 public override List<Role> get_cast () {
222 var sqlite = new IMDbSqlite (Path.build_filename (Environment.get_user_cache_dir (),
223 "cinaest", "imdb.db", null));
224 print ("IMDb get_cast(\"%s (%d)\")\n", title, year);
225 return sqlite.get_cast ("%s (%d)".printf (title, year));
229 class IMDBSource : MovieSource {
233 public override bool active { get; set construct; }
235 public IMDBSource () {
236 GLib.Object (active: true);
239 MovieSource.ReceiveMovieFunction _get_callback;
240 public override async int get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
241 var sqlite = new IMDbSqlite (Path.build_filename (Environment.get_user_cache_dir (),
242 "cinaest", "imdb.db", null));
244 _get_callback = callback;
245 result = new SList<Movie> ();
247 int n = yield sqlite.query (filter, receive_movie, limit, cancellable);
248 if (results_waiting > 0)
249 _get_callback (result);
250 result = new SList<Movie> ();
254 private void receive_movie (string title, string? aka, int year, int rating, int genres) {
255 var movie = new IMDbMovie ();
258 movie.rating = rating;
259 movie.genres.field = genres;
260 // TODO - depending on settings, this could be something else, like director info or runtime
262 movie.secondary = "aka \"%s\" - %s".printf (aka, movie.genres.to_string ());
264 movie.secondary = movie.genres.to_string ();
266 result.append (movie);
267 if (++results_waiting >= 10) {
268 _get_callback (result);
269 result = new SList<Movie> ();
274 public override void add_movie (Movie movie) {
277 public override void delete_movie (Movie movie) {
280 public override unowned string get_name () {
284 public override unowned string get_description () {
285 return _("Movies on IMDb");
288 public override SourceFlags get_flags () {
294 public Type register_plugin (TypeModule module) {
295 // types are registered automatically
296 return typeof (IMDbPlugin);