Add IMDb download dialog
[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
25         public override void hello (Gtk.Window window) {
26                 string filename = Path.build_filename (Environment.get_user_cache_dir(),
27                                                        "cinaest", "imdb.db", null);
28                 stdout.printf ("IMDb Plugin Loaded. Cache Db: %s\n", filename);
29
30                 if (!FileUtils.test (filename, FileTest.EXISTS)) {
31                         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.");
32
33                         var response = note.run ();
34                         note.destroy ();
35
36                         if (response == ResponseType.OK) {
37                                 download_imdb (window);
38                         }
39                 }
40         }
41
42         public override void get_movies (string filter, Plugin.ReceiveMovieFunction callback, int limit) {
43         }
44
45         public override void add_movie (Movie movie) {
46         }
47
48         private void download_imdb (Gtk.Window window) {
49                 var picker = new PickerDialog (window);
50                 var selector = new TouchSelector.text ();
51                 string[] mirrors;
52
53                 try {
54                         var conn = DBus.Bus.get (DBus.BusType.SESSION);
55                         server = conn.get_object (IMDbDownloader.DBUS_SERVICE,
56                                                   IMDbDownloader.DBUS_OBJECT,
57                                                   IMDbDownloader.DBUS_IFACE);
58
59                         mirrors = server.get_mirrors ();
60                 } catch (DBus.Error e) {
61                         warning ("Failed connect to IMDb downloader: %s", e.message);
62                         return;
63                 }
64
65                 foreach (string mirror in mirrors) {
66                         selector.append_text (mirror);
67                 }
68
69                 picker.set_title ("Please select a source mirror");
70                 picker.set_selector (selector);
71
72                 int res = picker.run();
73                 string mirror = selector.get_current_text ();
74                 picker.destroy();
75
76                 if (res == ResponseType.OK) {
77                         var download = new IMDbDownloadDialog (window);
78                         download.run (server, mirror);
79                         download.destroy ();
80                 }
81         }
82 }
83
84 [ModuleInit]
85 public Type register_plugin () {
86         // types are registered automatically
87         return typeof (IMDbPlugin);
88 }