~ubuntu-branches/ubuntu/utopic/totem-plugin-arte/utopic-proposed

« back to all changes in this revision

Viewing changes to .pc/01_fix_valac-0.12.patch/cache.vala

  • Committer: Package Import Robot
  • Author(s): Nicolas Delvaux
  • Date: 2011-10-23 13:55:28 UTC
  • mfrom: (3.1.5 experimental)
  • Revision ID: package-import@ubuntu.com-20111023135528-l9lso2mnile0dk8u
Tags: 3.0.0-2
* Upload to unstable
* Fix FTBS by switching to valac-0.14 and libpeas 1.1 (bump dependencies)
* Update the user-agent string
* Rework the "disable gsettings schema compilation" patch (use upstream patch)
* Packaging:
  - Tweak the long description (some videos are playable everywhere)
  - Remove VCS links in Control (there is no VCS for this package)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Totem Arte Plugin allows you to watch streams from arte.tv
3
 
 * Copyright (C) 2010 Simon Wenner <simon@wenner.ch>
4
 
 *
5
 
 * This program is free software; you can redistribute it and/or modify
6
 
 * it under the terms of the GNU General Public License as published by
7
 
 * the Free Software Foundation; either version 2 of the License, or
8
 
 * (at your option) any later version.
9
 
 *
10
 
 * This program is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 * GNU General Public License for more details.
14
 
 *
15
 
 * You should have received a copy of the GNU General Public License
16
 
 * along with this program; if not, write to the Free Software
17
 
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
18
 
 *
19
 
 * The Totem Arte Plugin project hereby grants permission for non-GPL compatible
20
 
 * GStreamer plugins to be used and distributed together with GStreamer, Totem
21
 
 * and Totem Arte Plugin. This permission is above and beyond the permissions
22
 
 * granted by the GPL license by which Totem Arte Plugin is covered.
23
 
 * If you modify this code, you may extend this exception to your version of the
24
 
 * code, but you are not obligated to do so. If you do not wish to do so,
25
 
 * delete this exception statement from your version.
26
 
 *
27
 
 */
28
 
 
29
 
using GLib;
30
 
using Gtk;
31
 
using Soup;
32
 
 
33
 
public class Cache : GLib.Object
34
 
{
35
 
    private Soup.SessionAsync session;
36
 
    public string cache_path {get; set;}
37
 
    public Gdk.Pixbuf default_thumbnail {get; private set;}
38
 
 
39
 
    public Cache (string path)
40
 
    {
41
 
        cache_path = path;
42
 
        session = create_session ();
43
 
 
44
 
        /* create the caching directory */
45
 
        var dir = GLib.File.new_for_path (cache_path);
46
 
        if (!dir.query_exists (null)) {
47
 
            try {
48
 
                dir.make_directory_with_parents (null);
49
 
                GLib.debug ("Directory '%s' created", dir.get_path ());
50
 
            } catch (Error e) {
51
 
                GLib.error ("Could not create caching directory.");
52
 
            }
53
 
        }
54
 
 
55
 
        /* load the default thumbnail */
56
 
        try {
57
 
            default_thumbnail = new Gdk.Pixbuf.from_file (DEFAULT_THUMBNAIL);
58
 
        } catch (Error e) {
59
 
            GLib.critical ("%s", e.message);
60
 
        }
61
 
    }
62
 
 
63
 
    public string? get_data_path (string url)
64
 
    {
65
 
        /* check if file exists in cache */
66
 
        string file_path = cache_path
67
 
                + Checksum.compute_for_string (ChecksumType.MD5, url);
68
 
 
69
 
        var file = GLib.File.new_for_path (file_path);
70
 
        if (file.query_exists (null)) {
71
 
            return file_path;
72
 
        }
73
 
 
74
 
        /* get file from the the net */
75
 
        var msg = new Soup.Message ("GET", url);
76
 
        session.send_message (msg);
77
 
 
78
 
        if (msg.response_body.data == null) {
79
 
            return null;
80
 
        }
81
 
 
82
 
        /* store the file on disk */
83
 
        try {
84
 
            var file_stream = file.create (FileCreateFlags.REPLACE_DESTINATION, null);
85
 
            var data_stream = new DataOutputStream (file_stream);
86
 
            data_stream.write (msg.response_body.data,
87
 
                    (ssize_t) msg.response_body.length, null);
88
 
 
89
 
        } catch (Error e) {
90
 
            GLib.error ("%s", e.message);
91
 
        }
92
 
 
93
 
        return file_path;
94
 
    }
95
 
 
96
 
    public Gdk.Pixbuf load_pixbuf (string? url)
97
 
    {
98
 
        if (url == null) {
99
 
            return default_thumbnail;
100
 
        }
101
 
 
102
 
        /* check if file exists in cache */
103
 
        string file_path = cache_path
104
 
                + Checksum.compute_for_string (ChecksumType.MD5, url);
105
 
        Gdk.Pixbuf pb = null;
106
 
 
107
 
        var file = GLib.File.new_for_path (file_path);
108
 
        if (file.query_exists (null)) {
109
 
            try {
110
 
                pb = new Gdk.Pixbuf.from_file (file_path);
111
 
            } catch (Error e) {
112
 
                GLib.critical ("%s", e.message);
113
 
                return default_thumbnail;
114
 
            }
115
 
            return pb;
116
 
        }
117
 
 
118
 
        /* otherwise, use the default thumbnail */
119
 
        return default_thumbnail;
120
 
    }
121
 
 
122
 
    public Gdk.Pixbuf download_pixbuf (string? url)
123
 
    {
124
 
        if (url == null) {
125
 
            return default_thumbnail;
126
 
        }
127
 
 
128
 
        string file_path = cache_path
129
 
                + Checksum.compute_for_string (ChecksumType.MD5, url);
130
 
        Gdk.Pixbuf pb = null;
131
 
 
132
 
        /* get file from the net */
133
 
        var msg = new Soup.Message ("GET", url);
134
 
        session.send_message (msg);
135
 
 
136
 
        if (msg.response_body.data == null) {
137
 
            return default_thumbnail;
138
 
        }
139
 
 
140
 
        /* rescale it */
141
 
        var img_stream = new MemoryInputStream.from_data (msg.response_body.data,
142
 
                (ssize_t) msg.response_body.length, null);
143
 
 
144
 
        try {
145
 
            /* original size: 720px × 406px */
146
 
            pb = new Gdk.Pixbuf.from_stream_at_scale (img_stream,
147
 
                    THUMBNAIL_WIDTH, -1, true, null);
148
 
        } catch (GLib.Error e) {
149
 
            GLib.critical ("%s", e.message);
150
 
            return default_thumbnail;
151
 
        }
152
 
 
153
 
        /* store the file on disk as PNG */
154
 
        try {
155
 
            pb.save (file_path, "png", null);
156
 
        } catch (Error e) {
157
 
            GLib.critical ("%s", e.message);
158
 
        }
159
 
 
160
 
        return pb;
161
 
    }
162
 
 
163
 
    /* Delete files that were created more than x days ago. */
164
 
    public void delete_cruft (int days) {
165
 
        GLib.debug ("Cache: Delete files that are older than %d days.", days);
166
 
        GLib.TimeVal now = TimeVal ();
167
 
        GLib.TimeVal mod_time = TimeVal ();
168
 
        now.get_current_time ();
169
 
        long deadline = now.tv_sec - days * 24 * 60 * 60;
170
 
 
171
 
        var directory = File.new_for_path (cache_path);
172
 
        try {
173
 
            var enumerator = directory.enumerate_children ("*",
174
 
                    GLib.FileQueryInfoFlags.NONE, null);
175
 
 
176
 
            GLib.FileInfo file_info;
177
 
            while ((file_info = enumerator.next_file (null)) != null) {
178
 
                file_info.get_modification_time (out mod_time);
179
 
                if (mod_time.tv_sec < deadline) {
180
 
                    var file = File.new_for_path (cache_path + file_info.get_name ());
181
 
                    file.delete (null);
182
 
                    GLib.debug ("Cache: Deleted: %s", file_info.get_name ());
183
 
                }
184
 
            }
185
 
            enumerator.close(null);
186
 
 
187
 
        } catch (Error e) {
188
 
            GLib.critical ("%s", e.message);
189
 
        }
190
 
    }
191
 
}