Provide plugins with Osso context
[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);
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) {
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);
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 void settings_dialog (Gtk.Window window) {
94                 var dialog = new Gtk.Dialog ();
95                 dialog.set_transient_for (window);
96                 dialog.set_title (_("IMDb plugin settings"));
97
98                 string filename = Path.build_filename (Environment.get_user_cache_dir(),
99                                                        "cinaest", "imdb.db", null);
100                 var file = File.new_for_path (filename);
101                 var info = file.query_info (FILE_ATTRIBUTE_TIME_MODIFIED, FileQueryInfoFlags.NONE, null);
102                 TimeVal tv;
103                 info.get_modification_time (out tv);
104
105                 var date = Date ();
106                 date.set_time_val (tv);
107                 char[] s = new char[64];
108                 date.strftime (s, "%x");
109
110                 var download = new Hildon.Button.with_text (SizeType.FINGER_HEIGHT, ButtonArrangement.VERTICAL, _("Download"), "Last update: " + (string) s);
111
112                 VBox content = (VBox) dialog.get_content_area ();
113                 content.pack_start (download, true, true, 0);
114
115                 download.clicked.connect (() => {
116                         download_imdb (window);
117                 });
118
119                 dialog.show_all ();
120                 dialog.run ();
121                 dialog.destroy ();
122         }
123
124         public override unowned string get_name () {
125                 return "IMDb";
126         }
127 }
128
129 class IMDBSource : MovieSource {
130         MovieSource.ReceiveMovieFunction _get_callback;
131         public override async void get_movies (MovieFilter filter, MovieSource.ReceiveMovieFunction callback, int limit, Cancellable? cancellable) {
132                 // IMDb has too many movies
133                 if (filter.title == "")
134                         return;
135                 var sqlite = new IMDbSqlite (Path.build_filename (Environment.get_user_cache_dir (),
136                                              "cinaest", "imdb.db", null));
137
138                 _get_callback = callback;
139                 yield sqlite.query (filter, receive_movie, cancellable);
140         }
141
142         private void receive_movie (string title, int year, int rating, int genres) {
143                 Movie movie = new Movie ();
144                 movie.title = title;
145                 movie.year = year;
146                 movie.rating = rating;
147                 movie.genres.field = genres;
148                 // TODO - depending on settings, this could be something else, like director info or runtime
149                 movie.secondary = movie.genres.to_string ();
150                 _get_callback (movie);
151         }
152
153         public override void add_movie (Movie movie) {
154         }
155
156         public override unowned string get_name () {
157                 return "IMDb";
158         }
159
160         public override unowned string get_description () {
161                 return "Movies on IMDb";
162         }
163 }
164
165 [ModuleInit]
166 public Type register_plugin () {
167         // types are registered automatically
168         return typeof (IMDbPlugin);
169 }