Imported Upstream version 0.3~20101209
[debian/pino.git] / src / img_cache.vala
1 using Soup;
2 using Gee;
3
4 public class ImgCache : Object {
5         
6         private Regex url_re;
7         private string cache_path;
8         private HashMap<string, Gdk.Pixbuf> map;
9         
10         construct {
11                 try {
12                         url_re = new Regex("(http://)([a-zA-Z0-9-\\.]+)/(.*)");
13                 } catch(GLib.RegexError e) {
14                         debug(e.message);
15                 }
16                 
17                 cache_path = Environment.get_user_cache_dir() + "/%s".printf("pino/");
18                 
19                 var cache_dir = File.new_for_path(cache_path);
20                 
21                 if(!cache_dir.query_exists(null)) {
22                         try {
23                                 cache_dir.make_directory(null);
24                         } catch(GLib.Error e) {
25                                 debug(e.message); //TODO
26                         }
27                 }
28                 
29                 map = new HashMap<string, Gdk.Pixbuf>();
30         }
31         
32         private void load_pix(string path) {
33                 if(!map.has_key(path)) {
34                         map[path] = new Gdk.Pixbuf.from_file(path);
35                 }
36                 
37                 if(map[path].width > 48 || map[path].height > 48) {
38                         map[path] = map[path].scale_simple(48, 48, Gdk.InterpType.BILINEAR);
39                 }
40         }
41         
42         public Gdk.Pixbuf? from_cache(string path) {
43                 load_pix(path);
44                 
45                 return map[path];
46         }
47         
48         public bool exist(string url) {
49                 string new_path = cache_path + url_to_name(url);        
50                 File f = File.new_for_path(new_path);
51                 
52                 if(!f.query_exists(null))
53                         return false;
54                 else
55                         return true;
56         }
57         
58         public string? download(string url) {
59                 if(exist(url))
60                         return cache_path + url_to_name(url);
61                 
62                 Session session = new SessionSync();
63                 Message msg;
64                 
65                 try {
66                         msg = new Message("GET", encode_url(url));
67                 } catch(GLib.RegexError e) {
68                         debug(e.message);
69                         return null;
70                 }
71                 
72                 int status_code = 0;
73                 
74                 status_code = (int) session.send_message(msg);
75                 
76                 if(status_code != 200) {
77                         debug("status code is %d", status_code);
78                         return null;
79                 }
80                 
81                 //string data = (string) msg.response_body.flatten().data;
82                 string new_path = cache_path + url_to_name(url);
83                 
84                 if(!Soppa.save_soup_data(msg.response_body, new_path))
85                         return null;
86                 
87                 load_pix(new_path);
88                 
89                 return new_path;
90         }
91         
92         private string url_to_name(string url) {
93                 string save_name = url.replace("/", "");
94                 return save_name;
95         }
96         
97         private string encode_url(string url) throws GLib.RegexError {
98                 string old_url_path = url_re.replace(url, -1, 0, "\\3");
99                 string new_url_path = Soup.form_encode("", old_url_path).split("=")[1];
100                 new_url_path = url.replace(old_url_path, new_url_path);
101         
102                 new_url_path = new_url_path.replace("%2F", "/");
103                                 
104                 return new_url_path;
105         }
106 }