summaryrefslogtreecommitdiff
path: root/src/librygel-server/rygel-thumbnailer.vala
blob: 17dfac94bf17ebffe1f632fd64b5c131fe67b9fd (plain)
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
/*
 * Copyright (C) 2008 Zeeshan Ali <zeenix@gmail.com>.
 * Copyright (C) 2010-2011 Nokia Corporation.
 * Copyright (C) 2012 Intel Corporation.
 *
 * Author: Zeeshan Ali <zeenix@gmail.com>
 *         Jens Georg <jensg@openismus.com>
 *
 * This file is part of Rygel.
 *
 * Rygel is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Rygel 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

internal errordomain ThumbnailerError {
    NO_DIR,
    NO_THUMBNAIL
}

/**
 * Provides thumbnails for images and vidoes.
 */
internal class Rygel.Thumbnailer : GLib.Object {
    private static Thumbnailer thumbnailer; // Our singleton object
    private static bool first_time = true;

    private Thumbnail template;
    private string extension;

    private DbusThumbnailer thumbler = null;

    private Thumbnailer () throws ThumbnailerError {
        this.template = new Thumbnail ("image/png", "PNG_TN", "png");
        this.template.width = 128;
        this.template.height = 128;
        this.template.depth = 32;
        this.extension = "." + this.template.file_extension;

        try {
            this.thumbler = new DbusThumbnailer ();
            this.thumbler.ready.connect (this.on_dbus_thumbnailer_ready);
        } catch (Error error) {}
    }

    public static Thumbnailer? get_default () {
        if (first_time) {
            try {
                thumbnailer = new Thumbnailer ();
            } catch (ThumbnailerError err) {
                warning (_("No thumbnailer available: %s"), err.message);
            }

            first_time = false;
        }

        return thumbnailer;
    }

    public Thumbnail get_thumbnail (string uri, string mime_type) throws Error {
        var file = File.new_for_uri (uri);
        var info = file.query_info (FileAttribute.THUMBNAIL_PATH + "," +
                                    FileAttribute.THUMBNAILING_FAILED,
                                    FileQueryInfoFlags.NONE);
        var path = info.get_attribute_as_string (FileAttribute.THUMBNAIL_PATH);
        var failed = info.get_attribute_boolean
                                        (FileAttribute.THUMBNAILING_FAILED);

        if (failed) {
            // Thumbnailing failed previously, so there's no current thumbnail
            // and it doesn't make any sense to request one.
            throw new ThumbnailerError.NO_THUMBNAIL
                                        (_("No thumbnail available"));
        }

        // Send a request to create thumbnail if it does not exist, signalize
        // that there's no thumbnail available now.
        if (this.thumbler != null && path == null) {
            this.thumbler.queue_thumbnail_task (uri, mime_type);

            throw new ThumbnailerError.NO_THUMBNAIL
                                        (_("No thumbnail available"));
        }

        if (path == null) {
            throw new ThumbnailerError.NO_THUMBNAIL
                                        (_("No thumbnail available"));
        }

        file = File.new_for_path (path);
        info = file.query_info (FileAttribute.ACCESS_CAN_READ + "," +
                                FileAttribute.STANDARD_SIZE,
                                FileQueryInfoFlags.NONE,
                                null);

        if (!info.get_attribute_boolean (FileAttribute.ACCESS_CAN_READ)) {
            throw new ThumbnailerError.NO_THUMBNAIL
                                        (_("No thumbnail available"));
        }

        var thumbnail = new Thumbnail (this.template.mime_type,
                                       this.template.dlna_profile,
                                       this.template.file_extension);
        thumbnail.width = this.template.width;
        thumbnail.height = this.template.height;
        thumbnail.depth = this.template.depth;
        thumbnail.uri = Filename.to_uri (path, null);
        thumbnail.size = (int64) info.get_attribute_uint64
                                        (FileAttribute.STANDARD_SIZE);

        return thumbnail;
    }

    private void on_dbus_thumbnailer_ready (bool available) {
        if (!available) {
            this.thumbler = null;
            message (_("No D-Bus thumbnailer available"));
        }
    }
}