summaryrefslogtreecommitdiff
path: root/src/librygel-core/rygel-thumbnailer.vala
blob: 35d55458ce1d26468c5790f29332bb9f8c5ee798 (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
131
132
133
134
/*
 * Copyright (C) 2008 Zeeshan Ali <zeenix@gmail.com>.
 *
 * Author: Zeeshan Ali <zeenix@gmail.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.
 */

using Gst;

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;

    public string directory;

    private Thumbnail template;
    private string extension;

    private DbusThumbnailer thumbler = null;

    private Thumbnailer () throws ThumbnailerError {
        var dir = Path.build_filename (Environment.get_home_dir (),
                                       ".thumbnails",
                                       "cropped");
        var file = File.new_for_path (dir);
        this.template = new Thumbnail ();

        if (!file.query_exists (null)) {
            dir = Path.build_filename (Environment.get_home_dir (),
                                       ".thumbnails",
                                       "normal");
            file = File.new_for_path (dir);

            if (!file.query_exists (null)) {
                var message = _("Failed to find thumbnails folder.");

                throw new ThumbnailerError.NO_DIR (message);
            } else {
                this.template.mime_type = "image/png";
                this.template.dlna_profile = "PNG_TN";
                this.template.file_extension = "png";
                this.template.width = 128;
                this.template.height = 128;
                this.template.depth = 32;
                this.extension = "." + this.template.file_extension;
            }
        } else {
            this.template.width = 124;
            this.template.height = 124;
            this.template.depth = 24;
            this.extension = ".jpeg";
        }

        this.directory = dir;

        try {
            this.thumbler = new DbusThumbnailer ();
       } catch (GLib.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 {
        Thumbnail thumbnail = null;

        var path = Checksum.compute_for_string (ChecksumType.MD5, uri) +
                   this.extension;
        var full_path = Path.build_filename (this.directory, path);
        var file = File.new_for_path (full_path);

        // send a request to create thumbnail if it does not exist
        if ((this.thumbler != null) && (!file.query_exists ())) {
            this.thumbler.queue_thumbnail_task (uri, mime_type);
        }

        var 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"));
        }

        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 (full_path, null);
        thumbnail.size = (int64) info.get_attribute_uint64
                                        (FileAttribute.STANDARD_SIZE);

        return thumbnail;
    }
}