3 namespace MoviePoster {
5 public delegate void RequestCallback (Gdk.Pixbuf movieposter, Movie movie);
7 public class Factory : Object {
8 private static Factory the_factory = null;
9 private List<Request> requests;
10 dynamic DBus.Object server;
11 internal bool download_posters;
12 private GConf.Client gc;
17 var conn = DBus.Bus.get (DBus.BusType.SESSION);
18 server = conn.get_object ("org.maemo.movieposter.GoogleImages",
19 "/org/maemo/movieposter/GoogleImages",
20 "org.maemo.movieposter.Provider");
21 server.Fetched += this.on_poster_fetched;
23 warning ("Couldn't connect to Google image downloader: %s\n", e.message);
25 gc = GConf.Client.get_default ();
27 download_posters = gc.get_bool ("/apps/cinaest/download_posters");
28 gc.add_dir ("/apps/cinaest", GConf.ClientPreloadType.ONELEVEL);
29 cxnid = gc.notify_add ("/apps/cinaest/download_posters", on_download_posters_changed);
32 private static void on_download_posters_changed (GConf.Client gc, uint cxnid, GConf.Entry entry) {
33 the_factory.download_posters = entry.get_value ().get_bool ();
36 public static Factory get_instance () {
37 if (the_factory == null)
38 the_factory = new MoviePoster.Factory ();
42 public int queue (Movie movie, RequestCallback callback) throws Error {
43 string path = get_path (movie);
45 if (FileUtils.test (path, FileTest.IS_REGULAR)) {
46 // TODO: make this async?
47 var pixbuf = new Gdk.Pixbuf.from_file_at_size (path, 268, 424);
48 callback (pixbuf, movie);
49 } else if (server != null && download_posters) {
50 var request = new Request ();
52 request.handle = server.fetch (movie.title.down (), movie.year.to_string (), "movie");
53 request.movie = movie;
54 request.callback = callback;
57 requests.append (request);
62 public int queue_thumbnail (Movie movie, uint width, uint height, bool cropped, RequestCallback callback) throws Error {
63 string path = get_path_thumbnail (movie);
65 if (FileUtils.test (path, FileTest.IS_REGULAR)) {
66 // TODO: make this async?
67 var pixbuf = new Gdk.Pixbuf.from_file_at_size (path, (int) width, (int) height);
68 callback (pixbuf, movie);
69 } else if (server != null && download_posters) {
70 var request = new Request ();
72 request.handle = server.fetch_thumbnail (movie.title.down (), movie.year.to_string (), "movie");
73 request.movie = movie;
74 request.callback = callback;
75 request.width = (int) width;
76 request.height = (int) height;
77 requests.append (request);
82 private void on_poster_fetched (dynamic DBus.Object server, int handle, string path) {
83 foreach (Request request in requests) {
84 if (request.handle != handle)
87 var pixbuf = new Gdk.Pixbuf.from_file_at_size (path, request.width, request.height);
89 requests.remove (request);
90 request.callback (pixbuf, request.movie);
93 warning ("Failed to open poster: %s\n", e.message);
101 public static void factory_remove (Movie movie) {
104 public static void factory_clean_cache (int max_size, time_t min_mtime) {
108 public class Request {
111 public RequestCallback callback;
115 public void unqueue () {
118 public void join () {
122 public static bool is_cached (Movie movie) {
123 string filename = get_path (movie);
124 if (FileUtils.test (filename, FileTest.IS_REGULAR))
130 public static string get_path (Movie movie) {
131 return Path.build_filename (Environment.get_user_cache_dir (), "media-art", "movie-" +
132 Checksum.compute_for_string (ChecksumType.MD5, movie.title.down ()) + "-" +
133 Checksum.compute_for_string (ChecksumType.MD5, movie.year.to_string ()) + ".jpeg");
136 public static string get_path_thumbnail (Movie movie) {
137 return Path.build_filename (Environment.get_tmp_dir (), "cinaest-thumbnails", "movie-" +
138 Checksum.compute_for_string (ChecksumType.MD5, movie.title.down ()) + "-" +
139 Checksum.compute_for_string (ChecksumType.MD5, movie.year.to_string ()) + ".jpeg");