IMDb plugin: fill movie secondary info with genre list
[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                 // FIXME - this forces the inclusion of config.h
48                 (void) Config.GETTEXT_PACKAGE;
49         }
50
51         private void download_imdb (Gtk.Window window) {
52                 var picker = new PickerDialog (window);
53                 var selector = new TouchSelector.text ();
54                 string[] mirrors;
55
56                 try {
57                         var conn = DBus.Bus.get (DBus.BusType.SESSION);
58                         server = conn.get_object (IMDbDownloader.DBUS_SERVICE,
59                                                   IMDbDownloader.DBUS_OBJECT,
60                                                   IMDbDownloader.DBUS_IFACE);
61
62                         mirrors = server.get_mirrors ();
63                 } catch (DBus.Error e) {
64                         warning ("Failed connect to IMDb downloader: %s", e.message);
65                         return;
66                 }
67
68                 foreach (string mirror in mirrors) {
69                         selector.append_text (mirror);
70                 }
71
72                 picker.set_title ("Please select a source mirror");
73                 picker.set_selector (selector);
74
75                 int res = picker.run();
76                 string mirror = selector.get_current_text ();
77                 picker.destroy();
78
79                 if (res == ResponseType.OK) {
80                         var download = new IMDbDownloadDialog (window);
81                         download.run (server, mirror);
82                         download.destroy ();
83                 }
84         }
85
86         public override unowned List<MovieSource> get_sources () {
87                 return sources;
88         }
89
90         public override void settings_dialog (Gtk.Window window) {
91                 var dialog = new Gtk.Dialog ();
92                 dialog.set_transient_for (window);
93                 dialog.set_title (_("IMDb plugin settings"));
94
95                 string filename = Path.build_filename (Environment.get_user_cache_dir(),
96                                                        "cinaest", "imdb.db", null);
97                 var file = File.new_for_path (filename);
98                 var info = file.query_info (FILE_ATTRIBUTE_TIME_MODIFIED, FileQueryInfoFlags.NONE, null);
99                 TimeVal tv;
100                 info.get_modification_time (out tv);
101
102                 var date = Date ();
103                 date.set_time_val (tv);
104                 char[] s = new char[64];
105                 date.strftime (s, "%x");
106
107                 var download = new Hildon.Button.with_text (SizeType.FINGER_HEIGHT, ButtonArrangement.VERTICAL, _("Download"), "Last update: " + (string) s);
108
109                 VBox content = (VBox) dialog.get_content_area ();
110                 content.pack_start (download, true, true, 0);
111
112                 download.clicked.connect (() => {
113                         download_imdb (window);
114                 });
115
116                 dialog.show_all ();
117                 dialog.run ();
118                 dialog.destroy ();
119         }
120
121         public override unowned string get_name () {
122                 return "IMDb";
123         }
124 }
125
126 class IMDBSource : MovieSource {
127         MovieSource.ReceiveMovieFunction _get_callback;
128         public override void get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit) {
129                 // IMDb has too many movies
130                 if (filter.title == "")
131                         return;
132                 var sqlite = new IMDbSqlite (Path.build_filename (Environment.get_user_cache_dir (),
133                                              "cinaest", "imdb.db", null));
134
135                 _get_callback = callback;
136                 sqlite.query (filter, receive_movie);
137         }
138
139         private void receive_movie (string title, int year, int rating, int genres) {
140                 Movie movie = new Movie ();
141                 movie.title = title;
142                 movie.year = year;
143                 movie.rating = rating;
144                 movie.genres.field = genres;
145                 // TODO - depending on settings, this could be something else, like director info or runtime
146                 movie.secondary = movie.genres.to_string ();
147                 _get_callback (movie);
148         }
149
150         public override void add_movie (Movie movie) {
151         }
152
153         public override unowned string get_name () {
154                 return "IMDb";
155         }
156
157         public override unowned string get_description () {
158                 return "Movies on IMDb";
159         }
160 }
161
162 [ModuleInit]
163 public Type register_plugin () {
164         // types are registered automatically
165         return typeof (IMDbPlugin);
166 }