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