~elementary-apps/pantheon-files/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/***
    Copyright (c) 2016 elementary LLC (http://launchpad.net/elementary)

    Largely transcribed from marlin-thumbnailer
    Copyright (c) 2009-2011 Jannis Pohlmann <jannis@xfce.org>
    Originaly Written in gtk+: gtkcellrendererpixbuf

    Pantheon Files is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.

    Pantheon Files is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

    You should have received a copy of the GNU General Public
    License along with this program; see the Timeoutfile COPYING.  If not,
    write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1335 USA.

    Author(s):  Jeremy Wootten <jeremy@elementaryos.org>

***/

/**
 * WARNING: The source code in this file may do harm to animals. Dead kittens
 * are to be expected.
 *
 *
 * MarlinThumbnailer is a class for requesting thumbnails from org.xfce.tumbler.*
 * D-Bus services. This header contains an in-depth description of its design to make
 * it easier to understand why things are the way they are.
 *
 * Please note that all D-Bus calls are performed asynchronously.
 *
 *
 * When a request call is sent out, an internal request ID is created and
 * associated with the corresponding DBusGProxyCall via the request_call_mapping hash
 * table.
 *
 * The D-Bus reply handler then checks if there was an delivery error or
 * not. If the request method was sent successfully, the handle returned by the
 * D-Bus thumbnailer is associated bidirectionally with the internal request ID via
 * the request_handle_mapping and handle_request_mappings. In both cases, the
 * association of the internal request ID with the DBusGProxyCall is removed from
 * request_call_mapping.
 *
 * These hash tables play a major role in the Finished, Error and Ready
 * signal handlers.
 *
 *
 * Ready / Error
 * =============
 *
 * The Ready and Error signal handlers work exactly like Started except that
 * the Ready idle function sets the thumb state of the corresponding
 * GOFFile objects to _READY and the Error signal sets the state to _NONE.
 *
 *
 * Finished
 * ========
 *
 * The Finished signal handler looks up the internal request ID based on
 * the D-Bus thumbnailer handle. It then drops all corresponding information
 * from handle_request_mapping and request_handle_mapping.
 */


namespace Marlin {
    [DBus (name = "org.freedesktop.thumbnails.Thumbnailer1")]
    public interface ThumbnailerDaemon : GLib.DBusProxy {
        public signal void started (uint handle);
        public signal void finished (uint handle);
        public signal void ready (uint handle, string[] uris);
        public signal void error (uint handle, string[] failed_uris, int error_code, string message);

        public abstract async uint queue (string[] uris, string[] mime_types, string flavor,
                                          string scheduler, uint handle_to_unqueue) throws IOError;
        public abstract async void dequeue (uint handle) throws IOError;
        public abstract void get_supported (out string[] uri_schemes, out string[] mime_types) throws IOError;
    }

    public class Thumbnailer : GLib.Object {

        enum IdleType {
            ERROR,
            READY,
            STARTED,
            FINISHED
        }

        struct Idle {
            uint id;
            IdleType type;
            string[] uris;
            uint handle;
        }


        private static Thumbnailer? instance;
        private static Mutex thumbnailer_lock;
        private static GLib.HashTable<uint, uint> request_handle_mapping;
        private static GLib.HashTable<uint, uint> handle_request_mapping;
        private static GLib.List<Idle?> idles;

        private ThumbnailerDaemon proxy;
        private string [] supported_schemes = null;
        private string [] supported_types = null;

        private uint last_request = 0;

        public signal void finished (uint request);

        private Thumbnailer () {
            if (request_handle_mapping == null) {
                request_handle_mapping = new GLib.HashTable<uint, uint>.full (direct_hash, direct_equal, null, null);
                handle_request_mapping = new GLib.HashTable<uint, uint>.full (direct_hash, direct_equal, null,null);
                thumbnailer_lock = Mutex ();
            }
        }

        private void init () {
            if (proxy == null) {
                try {
                    proxy = GLib.Bus.get_proxy_sync (GLib.BusType.SESSION,
                                                     "org.freedesktop.thumbnails.Thumbnailer1",
                                                     "/org/freedesktop/thumbnails/Thumbnailer1");
                }
                catch (GLib.Error e) {
                    critical ("Failed to connect to system thumbnailing service (tumbler) - %s", e.message);
                    proxy = null;
                    return;
                }

                if (proxy != null) {
                    proxy.started.connect (on_proxy_started);
                    proxy.finished.connect (on_proxy_finished);
                    proxy.ready.connect (on_proxy_ready);
                    proxy.error.connect (on_proxy_error);
                }
            }
        }

        ~Thumbnailer () {
            thumbnailer_lock.@lock ();
            foreach (var idle in idles) {
                GLib.Source.remove (idle.id);
            }
            thumbnailer_lock.unlock ();
        }

        public new static Thumbnailer? @get () {
            if (instance == null) {
                instance =  new Thumbnailer ();
                instance.init ();
            }
            return instance;
        }

        public bool queue_file (GOF.File file, out int request, bool large) {
            GLib.List<GOF.File> files = null;
            files.append (file);
            int this_request;
            bool success = queue_files (files, out this_request, large);
            request = this_request;
            return success;
        }

        public bool queue_files (GLib.List<GOF.File> files, out int request, bool large) {
            request = -1;
            if (proxy == null) {
                return false;
            }

            GLib.List<GOF.File> supported_files = null;

            uint file_count = 0;
            foreach (var file in files) {
                if (is_supported (file)) {
                    supported_files.prepend (file);
                    file.flags = GOF.File.ThumbState.LOADING;
                    file_count++;
                } else {
                    file.flags = GOF.File.ThumbState.NONE;
                }
            }

            if (file_count == 0) {
                return false;
            }

            var uris = new string[file_count];
            var mime_hints = new string[file_count];

            supported_files.reverse ();

            uint index = 0;
            foreach (var file in supported_files) {
                uris[index] = file.uri;
                mime_hints[index] = file.get_ftype ();
                index++;
            }

            uint this_request = ++last_request;
            var flavor = large ? "large" : "normal";
            var scheduler = "foreground";
            proxy.queue.begin (uris, mime_hints, flavor, scheduler, 0, (obj, res) => {
                try {
                    uint handle;
                    handle = proxy.queue.end (res);
                    request_handle_mapping.insert (this_request, handle);
                    handle_request_mapping.insert (handle, this_request);
                } catch (GLib.Error e) {
                    warning ("Thumbnailer proxy request %u failed - %s", this_request, e.message);
                }
            });

            request = (int)this_request;
            return true;
        }

        public void dequeue (int request) {
            if (proxy == null) {
                return;
            }

            uint req = (uint)request;
            thumbnailer_lock.@lock ();
            uint handle = request_handle_mapping.lookup (req);
            thumbnailer_lock.unlock ();

            proxy.dequeue (handle); /* hash tables will be updated when "finished" signal received. */
        }

        private bool is_supported (GOF.File file) {
            /* TODO cache supported combinations */
            var ftype = file.get_ftype ();
            if (proxy == null || ftype == null) {
                return false;
            }
            bool supported = false;

            if (supported_schemes == null) {
                try {
                    proxy.get_supported (out supported_schemes, out supported_types);
                } catch (GLib.Error e) {
                    warning ("Thumbnailer failed to get supported file list");
                    return false;
                }
            }
            if (supported_schemes != null && supported_types != null) {
                uint index = 0;
                foreach (string scheme in supported_schemes) {
                    if (file.location.has_uri_scheme (scheme) &&
                       GLib.ContentType.is_a (ftype, supported_types[index])) {
                        supported = true;
                        break;
                    }
                    index++;
                }
            } else {
                warning ("No supported schemes or types returned by proxy");
            }

            return supported;
        }

        private static void on_proxy_error (uint handle, string[] failed_uris,
                                     int error_code, string msg) {

            var idle = Idle ();
            idle.type = IdleType.ERROR;
            idle.uris = GLib.strdupv (failed_uris);
            idles.prepend (idle);

            /* TODO batch up errors? */
            idle.id = GLib.Idle.add_full (GLib.Priority.LOW, () => {
                handle_error_idle (idle);
                return false;
            });
        }


        private static void on_proxy_started (uint handle) {
            debug ("started %u", handle);
        }

        private static void on_proxy_ready (uint handle, string[] ready_uris) {
            if (ready_uris != null) {
                var idle = Idle ();
                idle.type = IdleType.READY;
                idle.uris = GLib.strdupv (ready_uris);
                idle.handle = handle;

                idles.prepend (idle);

                /* TODO batch up errors? */
                idle.id = GLib.Idle.add_full (GLib.Priority.HIGH, () => {
                    handle_ready_idle (idle);
                    return false;
                });
            } else {
                warning ("no ready uris");
            }
        }

        private static void on_proxy_finished (uint handle) {
            var idle = Idle ();
            idle.type = IdleType.FINISHED;
            idle.handle = handle;
            idles.prepend (idle);

            /* TODO batch up errors? */
            idle.id = GLib.Idle.add_full (GLib.Priority.LOW, () => {
                handle_finished_idle (idle);
                return false;
            });
        }

        private static void handle_error_idle (Idle error_idle) {
            foreach (string uri in error_idle.uris) {
                update_file_thumbstate (uri, GOF.File.ThumbState.NONE);
            }

            thumbnailer_lock.@lock ();
            idles.remove (error_idle);
            thumbnailer_lock.unlock ();
        }

        private static void handle_ready_idle (Idle ready_idle) {

            foreach (string uri in ready_idle.uris) {
                update_file_thumbstate (uri, GOF.File.ThumbState.READY);
            }

            thumbnailer_lock.@lock ();
            idles.remove (ready_idle);
            thumbnailer_lock.unlock ();
        }

        private static void handle_finished_idle (Idle finished_idle) {
            var handle = finished_idle.handle;
            thumbnailer_lock.@lock ();
            uint request = handle_request_mapping.lookup (handle);
            request_handle_mapping.remove (request);
            handle_request_mapping.remove (handle);
            thumbnailer_lock.unlock ();
            Thumbnailer.@get ().finished (request);
        }

        private static void update_file_thumbstate (string uri, GOF.File.ThumbState state) {
            var goffile = GOF.File.get_by_uri (uri);
            if (goffile != null) {
                goffile.flags = state;
                goffile.query_thumbnail_update ();
            }
        }
    }
}