Movie poster factory: use the new IMDb poster downloader
[cinaest] / src / poster / movie-poster-factory.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 GLib;
20
21 namespace MoviePoster {
22
23         public delegate void RequestCallback (Gdk.Pixbuf movieposter, Movie movie);
24
25         public class Factory : Object {
26                 private static Factory the_factory = null;
27                 internal List<Request> requests;
28                 internal dynamic DBus.Object server;
29                 internal bool download_posters;
30                 private GConf.Client gc;
31                 private uint cxnid;
32
33                 construct {
34                         try {
35                                 var conn = DBus.Bus.get (DBus.BusType.SESSION);
36                                 server = conn.get_object ("org.maemo.movieposter.IMDb",
37                                                           "/org/maemo/movieposter/IMDb",
38                                                           "org.maemo.movieposter.Provider");
39                                 server.Fetched.connect (this.on_poster_fetched);
40                         } catch (Error e) {
41                                 warning ("Couldn't connect to IMDb poster downloader: %s\n", e.message);
42                         }
43                         gc = GConf.Client.get_default ();
44
45                         try {
46                                 download_posters = gc.get_bool ("/apps/cinaest/download_posters");
47                                 gc.add_dir ("/apps/cinaest", GConf.ClientPreloadType.ONELEVEL);
48                                 cxnid = gc.notify_add ("/apps/cinaest/download_posters", on_download_posters_changed);
49                         } catch (Error e) {
50                                 stdout.printf ("Error installing GConf notification: %s\n", e.message);
51                         }
52                 }
53
54                 private static void on_download_posters_changed (GConf.Client gc, uint cxnid, GConf.Entry entry) {
55                         the_factory.download_posters = entry.get_value ().get_bool ();
56                 }
57
58                 public static Factory get_instance () {
59                         if (the_factory == null)
60                                 the_factory = new MoviePoster.Factory ();
61                         return the_factory;
62                 }
63
64                 public int queue (Movie movie, RequestCallback callback) throws Error {
65                         string path = get_path (movie);
66
67                         if (FileUtils.test (path, FileTest.IS_REGULAR)) {
68                                 // TODO: make this async?
69                                 var pixbuf = new Gdk.Pixbuf.from_file_at_size (path, 268, 424);
70                                 callback (pixbuf, movie);
71                         } else if (server != null && download_posters) {
72                                 var request = new Request ();
73
74                                 request.handle = server.Fetch (movie.title.down (), movie.year.to_string (), "movie");
75                                 request.movie = movie;
76                                 request.callback = callback;
77                                 request.width = 268;
78                                 request.height = 424;
79                                 requests.append (request);
80                         }
81                         return 0;
82                 }
83
84                 public int queue_thumbnail (Movie movie, uint width, uint height, bool cropped, RequestCallback callback) throws Error {
85                         string path = get_path_thumbnail (movie);
86
87                         foreach (Request request in requests)
88                                 if (request.movie == movie)
89                                         return 0;
90
91                         if (FileUtils.test (path, FileTest.IS_REGULAR)) {
92                                 // TODO: make this async?
93                                 var pixbuf = new Gdk.Pixbuf.from_file_at_scale (path, (int) width, (int) height, true);
94
95                                 callback (pixbuf, movie);
96                         } else if (server != null && download_posters) {
97                                 var request = new Request ();
98
99                                 request.handle = server.FetchThumbnail (movie.title, movie.year.to_string (), "movie");
100                                 request.movie = movie;
101                                 request.callback = callback;
102                                 request.width = (int) width;
103                                 request.height = (int) height;
104                                 requests.append (request);
105                         }
106                         return 0;
107                 }
108
109                 private void on_poster_fetched (dynamic DBus.Object server, int handle, string path) {
110                         Request request = null;
111                         foreach (Request r in requests) {
112                                 if (r.handle == handle) {
113                                         request = r;
114                                         break;
115                                 }
116                         }
117                         if (request == null)
118                                 return;
119                         try {
120                                 var pixbuf = new Gdk.Pixbuf.from_file_at_size (path, request.width, request.height);
121
122                                 requests.remove (request);
123                                 request.callback (pixbuf, request.movie);
124                                 return;
125                         } catch (Error e) {
126                                 warning ("Failed to open poster: %s\n", e.message);
127                         }
128                 }
129
130                 public void join () {
131                 }
132
133                 public static void factory_remove (Movie movie) {
134                 }
135
136                 public static void factory_clean_cache (int max_size, time_t min_mtime) {
137                 }
138
139                 public void clear_queue () {
140                         if (server != null) {
141                                 foreach (Request r in requests)
142                                         server.Unqueue (r.handle);
143                         }
144                         requests = null;
145                 }
146         }
147
148         public class Request {
149                 public int handle;
150                 public Movie movie;
151                 public RequestCallback callback;
152                 public int width;
153                 public int height;
154
155                 public void unqueue () {
156                         if (Factory.get_instance ().server != null)
157                                 Factory.get_instance ().server.Unqueue (this.handle);
158
159                         Factory.get_instance ().requests.remove (this);
160                 }
161
162                 public void join () {
163                 }
164         }
165
166         public static bool is_cached (Movie movie) {
167                 string filename = get_path (movie);
168                 if (FileUtils.test (filename, FileTest.IS_REGULAR))
169                         return true;
170                 else
171                         return false;
172         }
173
174         public static string get_path (Movie movie) {
175                 return Path.build_filename (Environment.get_user_cache_dir (), "media-art", "movie-" +
176                                             Checksum.compute_for_string (ChecksumType.MD5, movie.title.down ()) + "-" +
177                                             Checksum.compute_for_string (ChecksumType.MD5, movie.year.to_string ()) + ".jpeg");
178         }
179
180         public static string get_path_thumbnail (Movie movie) {
181                 return Path.build_filename (Environment.get_tmp_dir (), "cinaest-thumbnails", "movie-" +
182                                             Checksum.compute_for_string (ChecksumType.MD5, movie.title.down ()) + "-" +
183                                             Checksum.compute_for_string (ChecksumType.MD5, movie.year.to_string ()) + ".jpeg");
184         }
185 }
186