IMDb plugin: localize last update string, catch file info query error
[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         private Gtk.Window _window;
27
28         public override void hello (Gtk.Window window, Osso.Context context) {
29                 string filename = Path.build_filename (Environment.get_user_cache_dir(),
30                                                        "cinaest", "imdb.db", null);
31                 stdout.printf ("IMDb Plugin Loaded. Cache Db: %s\n", filename);
32
33                 if (!FileUtils.test (filename, FileTest.EXISTS)) {
34                         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
36                         var response = note.run ();
37                         note.destroy ();
38
39                         if (response == ResponseType.OK) {
40                                 download_imdb (window);
41                         }
42                 }
43
44                 var source = new IMDBSource ();
45
46                 sources = new List<MovieSource> ();
47                 sources.append (source);
48
49                 osso_context = context;
50                 _window = window;
51
52                 // FIXME - this forces the inclusion of config.h
53                 (void) Config.GETTEXT_PACKAGE;
54         }
55
56         private void download_imdb (Gtk.Window window) {
57                 var picker = new PickerDialog (window);
58                 var selector = new TouchSelector.text ();
59                 string[] mirrors;
60
61                 try {
62                         var conn = DBus.Bus.get (DBus.BusType.SESSION);
63                         server = conn.get_object (IMDbDownloader.DBUS_SERVICE,
64                                                   IMDbDownloader.DBUS_OBJECT,
65                                                   IMDbDownloader.DBUS_IFACE);
66
67                         mirrors = server.get_mirrors ();
68                 } catch (DBus.Error e) {
69                         warning ("Failed connect to IMDb downloader: %s", e.message);
70                         return;
71                 }
72
73                 foreach (string mirror in mirrors) {
74                         selector.append_text (mirror);
75                 }
76
77                 picker.set_title ("Please select a source mirror");
78                 picker.set_selector (selector);
79
80                 int res = picker.run();
81                 string mirror = selector.get_current_text ();
82                 picker.destroy();
83
84                 if (res == ResponseType.OK) {
85                         var download = new IMDbDownloadDialog (window);
86                         download.run (server, mirror);
87                         download.destroy ();
88                 }
89         }
90
91         public override unowned List<MovieSource> get_sources () {
92                 return sources;
93         }
94
95         public override List<MovieAction> get_actions (Movie movie) {
96                 List<MovieAction> list = null;
97
98                 if (movie.year > 0)
99                         list.append (new MovieAction (_("IMDb page"), on_visit_imdb, movie));
100                 else
101                         list.append (new MovieAction (_("Lookup on IMDb"), on_visit_imdb, movie));
102
103                 return list;
104         }
105
106         private void on_visit_imdb (Movie movie) {
107                 var url = "http://www.imdb.com/find?s=tt&q=" + movie.title.replace(" ", "+");
108                 if (movie.year > 0)
109                         url += "+(%d)".printf (movie.year);
110
111                 var status = osso_context.rpc_run_with_defaults ("osso_browser", "open_new_window", null, 's', url, 'b', false);
112                 if (status != Osso.Status.OK) {
113                         var banner = (Banner) Hildon.Banner.show_information_with_markup (_window, null, "Failed to open browser.");
114                         banner.set_timeout (1500);
115                 }
116         }
117
118         public override void settings_dialog (Gtk.Window window) {
119                 var dialog = new Gtk.Dialog ();
120                 dialog.set_transient_for (window);
121                 dialog.set_title (_("IMDb plugin settings"));
122
123                 string updated;
124                 string filename = Path.build_filename (Environment.get_user_cache_dir(),
125                                                        "cinaest", "imdb.db", null);
126                 var file = File.new_for_path (filename);
127                 try {
128                         var info = file.query_info (FILE_ATTRIBUTE_TIME_MODIFIED, FileQueryInfoFlags.NONE, null);
129                         TimeVal tv;
130                         info.get_modification_time (out tv);
131
132                         var date = Date ();
133                         date.set_time_val (tv);
134                         char[] s = new char[64];
135                         date.strftime (s, "%x");
136                         updated = (string) s;
137                 } catch (Error e) {
138                         updated = _("unknown");
139                 }
140
141                 var download = new Hildon.Button.with_text (SizeType.FINGER_HEIGHT, ButtonArrangement.VERTICAL, _("Download"), _("Last update: ") + updated);
142
143                 VBox content = (VBox) dialog.get_content_area ();
144                 content.pack_start (download, true, true, 0);
145
146                 download.clicked.connect (() => {
147                         download_imdb (window);
148                 });
149
150                 dialog.show_all ();
151                 dialog.run ();
152                 dialog.destroy ();
153         }
154
155         public override unowned string get_name () {
156                 return "IMDb";
157         }
158 }
159
160 class IMDBSource : MovieSource {
161         MovieSource.ReceiveMovieFunction _get_callback;
162         public override async void get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
163                 // IMDb has too many movies
164                 if (filter.title == "")
165                         return;
166                 var sqlite = new IMDbSqlite (Path.build_filename (Environment.get_user_cache_dir (),
167                                              "cinaest", "imdb.db", null));
168
169                 _get_callback = callback;
170                 yield sqlite.query (filter, receive_movie, cancellable);
171         }
172
173         private void receive_movie (string title, int year, int rating, int genres) {
174                 Movie movie = new Movie ();
175                 movie.title = title;
176                 movie.year = year;
177                 movie.rating = rating;
178                 movie.genres.field = genres;
179                 // TODO - depending on settings, this could be something else, like director info or runtime
180                 movie.secondary = movie.genres.to_string ();
181                 _get_callback (movie);
182         }
183
184         public override void add_movie (Movie movie) {
185         }
186
187         public override unowned string get_name () {
188                 return "IMDb";
189         }
190
191         public override unowned string get_description () {
192                 return "Movies on IMDb";
193         }
194 }
195
196 [ModuleInit]
197 public Type register_plugin () {
198         // types are registered automatically
199         return typeof (IMDbPlugin);
200 }