1 /* This file is part of Cinaest.
3 * Copyright (C) 2009 Philipp Zabel
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.
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.
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/>.
21 namespace MoviePoster {
23 public delegate void RequestCallback (Gdk.Pixbuf movieposter, Movie movie);
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;
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);
42 warning ("Couldn't connect to IMDb poster downloader: %s\n", e.message);
44 gc = GConf.Client.get_default ();
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);
51 stdout.printf ("Error installing GConf notification: %s\n", e.message);
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 ();
59 public static Factory get_instance () {
60 if (the_factory == null)
61 the_factory = new MoviePoster.Factory ();
65 public int queue (Movie movie, RequestCallback callback) throws Error {
66 string path = get_path (movie);
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 ();
75 request.handle = server.Fetch (movie.title.down (), movie.year.to_string (), "movie");
76 request.movie = movie;
77 request.callback = callback;
80 requests.append (request);
85 public int queue_thumbnail (Movie movie, uint width, uint height, bool cropped, RequestCallback callback) throws Error {
86 string path = get_path_thumbnail (movie);
88 foreach (Request request in requests)
89 if (request.movie == movie)
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);
96 callback (pixbuf, movie);
97 } else if (server != null && download_posters) {
98 var request = new Request ();
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);
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) {
121 var pixbuf = new Gdk.Pixbuf.from_file_at_size (path, request.width, request.height);
123 requests.remove (request);
124 request.callback (pixbuf, request.movie);
127 warning ("Failed to open poster: %s\n", e.message);
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) {
141 requests.remove (request);
142 // request.callback (pixbuf, request.movie);
145 public void join () {
148 public static void factory_remove (Movie movie) {
151 public static void factory_clean_cache (int max_size, time_t min_mtime) {
154 public void clear_queue () {
155 if (server != null) {
156 foreach (Request r in requests)
157 server.Unqueue (r.handle);
163 public class Request {
166 public RequestCallback callback;
170 public void unqueue () {
171 if (Factory.get_instance ().server != null)
172 Factory.get_instance ().server.Unqueue (this.handle);
174 Factory.get_instance ().requests.remove (this);
177 public void join () {
181 public static bool is_cached (Movie movie) {
182 string filename = get_path (movie);
183 if (FileUtils.test (filename, FileTest.IS_REGULAR))
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");
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");