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.GoogleImages",
37 "/org/maemo/movieposter/GoogleImages",
38 "org.maemo.movieposter.Provider");
39 server.Fetched.connect (this.on_poster_fetched);
41 warning ("Couldn't connect to Google image downloader: %s\n", e.message);
43 gc = GConf.Client.get_default ();
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);
50 stdout.printf ("Error installing GConf notification: %s\n", e.message);
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 ();
58 public static Factory get_instance () {
59 if (the_factory == null)
60 the_factory = new MoviePoster.Factory ();
64 public int queue (Movie movie, RequestCallback callback) throws Error {
65 string path = get_path (movie);
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 ();
74 request.handle = server.Fetch (movie.title.down (), movie.year.to_string (), "movie");
75 request.movie = movie;
76 request.callback = callback;
79 requests.append (request);
84 public int queue_thumbnail (Movie movie, uint width, uint height, bool cropped, RequestCallback callback) throws Error {
85 string path = get_path_thumbnail (movie);
87 if (FileUtils.test (path, FileTest.IS_REGULAR)) {
88 // TODO: make this async?
89 var pixbuf = new Gdk.Pixbuf.from_file_at_size (path, (int) width, (int) height);
90 callback (pixbuf, movie);
91 } else if (server != null && download_posters) {
92 var request = new Request ();
94 request.handle = server.FetchThumbnail (movie.title.down (), movie.year.to_string (), "movie");
95 request.movie = movie;
96 request.callback = callback;
97 request.width = (int) width;
98 request.height = (int) height;
99 requests.append (request);
104 private void on_poster_fetched (dynamic DBus.Object server, int handle, string path) {
105 Request request = null;
106 foreach (Request r in requests) {
107 if (r.handle == handle) {
115 var pixbuf = new Gdk.Pixbuf.from_file_at_size (path, request.width, request.height);
117 requests.remove (request);
118 request.callback (pixbuf, request.movie);
121 warning ("Failed to open poster: %s\n", e.message);
125 public void join () {
128 public static void factory_remove (Movie movie) {
131 public static void factory_clean_cache (int max_size, time_t min_mtime) {
134 public void clear_queue () {
135 if (server != null) {
136 foreach (Request r in requests)
137 server.Unqueue (r.handle);
143 public class Request {
146 public RequestCallback callback;
150 public void unqueue () {
151 if (Factory.get_instance ().server != null)
152 Factory.get_instance ().server.Unqueue (this.handle);
154 Factory.get_instance ().requests.remove (this);
157 public void join () {
161 public static bool is_cached (Movie movie) {
162 string filename = get_path (movie);
163 if (FileUtils.test (filename, FileTest.IS_REGULAR))
169 public static string get_path (Movie movie) {
170 return Path.build_filename (Environment.get_user_cache_dir (), "media-art", "movie-" +
171 Checksum.compute_for_string (ChecksumType.MD5, movie.title.down ()) + "-" +
172 Checksum.compute_for_string (ChecksumType.MD5, movie.year.to_string ()) + ".jpeg");
175 public static string get_path_thumbnail (Movie movie) {
176 return Path.build_filename (Environment.get_tmp_dir (), "cinaest-thumbnails", "movie-" +
177 Checksum.compute_for_string (ChecksumType.MD5, movie.title.down ()) + "-" +
178 Checksum.compute_for_string (ChecksumType.MD5, movie.year.to_string ()) + ".jpeg");