Add GConf option to control poster downloads
[cinaest] / src / poster / movie-poster-factory.vala
1 using GLib;
2
3 namespace MoviePoster {
4
5         public delegate void RequestCallback (Gdk.Pixbuf movieposter, Movie movie);
6
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;
13                 private uint cxnid;
14
15                 construct {
16                         try {
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;
22                         } catch (Error e) {
23                                 warning ("Couldn't connect to Google image downloader: %s\n", e.message);
24                         }
25                         gc = GConf.Client.get_default ();
26
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);
30                 }
31
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 ();
34                 }
35
36                 public static Factory get_instance () {
37                         if (the_factory == null)
38                                 the_factory = new MoviePoster.Factory ();
39                         return the_factory;
40                 }
41
42                 public int queue (Movie movie, RequestCallback callback) throws Error {
43                         string path = get_path (movie);
44
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 ();
51
52                                 request.handle = server.fetch (movie.title.down (), movie.year.to_string (), "movie");
53                                 request.movie = movie;
54                                 request.callback = callback;
55                                 request.width = 268;
56                                 request.height = 424;
57                                 requests.append (request);
58                         }
59                         return 0;
60                 }
61
62                 public int queue_thumbnail (Movie movie, uint width, uint height, bool cropped, RequestCallback callback) throws Error {
63                         string path = get_path_thumbnail (movie);
64
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 ();
71
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);
78                         }
79                         return 0;
80                 }
81
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)
85                                         continue;
86                                 try {
87                                         var pixbuf = new Gdk.Pixbuf.from_file_at_size (path, request.width, request.height);
88
89                                         requests.remove (request);
90                                         request.callback (pixbuf, request.movie);
91                                         return;
92                                 } catch (Error e) {
93                                         warning ("Failed to open poster: %s\n", e.message);
94                                 }
95                         }
96                 }
97
98                 public void join () {
99                 }
100
101                 public static void factory_remove (Movie movie) {
102                 }
103
104                 public static void factory_clean_cache (int max_size, time_t min_mtime) {
105                 }
106         }
107
108         public class Request {
109                 public int handle;
110                 public Movie movie;
111                 public RequestCallback callback;
112                 public int width;
113                 public int height;
114
115                 public void unqueue () {
116                 }
117
118                 public void join () {
119                 }
120         }
121
122         public static bool is_cached (Movie movie) {
123                 string filename = get_path (movie);
124                 if (FileUtils.test (filename, FileTest.IS_REGULAR))
125                         return true;
126                 else
127                         return false;
128         }
129
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");
134         }
135
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");
140         }
141 }
142