2
* Totem Arte Plugin allows you to watch streams from arte.tv
3
* Copyright (C) 2010 Simon Wenner <simon@wenner.ch>
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.
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.
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.
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.
33
public class Cache : GLib.Object
35
private Soup.SessionAsync session;
36
public string cache_path {get; set;}
37
public Gdk.Pixbuf default_thumbnail {get; private set;}
39
public Cache (string path)
42
session = create_session ();
44
/* create the caching directory */
45
var dir = GLib.File.new_for_path (cache_path);
46
if (!dir.query_exists (null)) {
48
dir.make_directory_with_parents (null);
49
GLib.debug ("Directory '%s' created", dir.get_path ());
51
GLib.error ("Could not create caching directory.");
55
/* load the default thumbnail */
57
default_thumbnail = new Gdk.Pixbuf.from_file (DEFAULT_THUMBNAIL);
59
GLib.critical ("%s", e.message);
63
public string? get_data_path (string url)
65
/* check if file exists in cache */
66
string file_path = cache_path
67
+ Checksum.compute_for_string (ChecksumType.MD5, url);
69
var file = GLib.File.new_for_path (file_path);
70
if (file.query_exists (null)) {
74
/* get file from the the net */
75
var msg = new Soup.Message ("GET", url);
76
session.send_message (msg);
78
if (msg.response_body.data == null) {
82
/* store the file on disk */
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);
90
GLib.error ("%s", e.message);
96
public Gdk.Pixbuf load_pixbuf (string? url)
99
return default_thumbnail;
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;
107
var file = GLib.File.new_for_path (file_path);
108
if (file.query_exists (null)) {
110
pb = new Gdk.Pixbuf.from_file (file_path);
112
GLib.critical ("%s", e.message);
113
return default_thumbnail;
118
/* otherwise, use the default thumbnail */
119
return default_thumbnail;
122
public Gdk.Pixbuf download_pixbuf (string? url)
125
return default_thumbnail;
128
string file_path = cache_path
129
+ Checksum.compute_for_string (ChecksumType.MD5, url);
130
Gdk.Pixbuf pb = null;
132
/* get file from the net */
133
var msg = new Soup.Message ("GET", url);
134
session.send_message (msg);
136
if (msg.response_body.data == null) {
137
return default_thumbnail;
141
var img_stream = new MemoryInputStream.from_data (msg.response_body.data,
142
(ssize_t) msg.response_body.length, null);
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;
153
/* store the file on disk as PNG */
155
pb.save (file_path, "png", null);
157
GLib.critical ("%s", e.message);
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;
171
var directory = File.new_for_path (cache_path);
173
var enumerator = directory.enumerate_children ("*",
174
GLib.FileQueryInfoFlags.NONE, null);
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 ());
182
GLib.debug ("Cache: Deleted: %s", file_info.get_name ());
185
enumerator.close(null);
188
GLib.critical ("%s", e.message);