Add Cristian Greco as uploader
[debian/pino.git] / src / cache.vala
1 /* cache.vala
2  *
3  * Copyright (C) 2009-2010  troorl
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author:
19  *      troorl <troorl@gmail.com>
20  */
21
22 using Gee;
23 using Auth;
24
25 public class Cache : Object {
26         
27         public enum Method {SYNC, ASYNC}
28         
29         public signal void pic_downloaded(string url, string path);
30         
31         string cache_path;
32         
33         private HashMap<string, string> map;
34         private Regex url_re;
35         
36         public Cache() {
37                 map = new HashMap<string, string>();
38                 check_cache_dir();
39                 
40                 url_re = new Regex("(http://)([a-zA-Z0-9-\\.]+)/(.*)");
41         }
42         
43         public string get_or_download(string url, Method method, bool need_emit) {
44                 //first look in hash
45                 var quick_response = map.get(url);
46                 if(quick_response != null)
47                         return quick_response;
48                 
49                 //get a file name
50                 string save_name = url.replace("/", "");//screen_name + service;
51                 
52                 //then look on disk
53                 string file_path = cache_path + "/" + save_name;
54                 var file =  File.new_for_path(file_path);
55                 if(file.query_exists(null)) {
56                         map.set(url, file_path);
57                         return file_path;
58                 }
59                 
60                 //and then try to download
61                 if(method == Method.ASYNC) {
62                         download_async.begin(url, save_name, need_emit);
63                         return url;
64                 }
65                 
66                 download(url, save_name);
67                 
68                 return file_path;
69         }
70         
71         private string encode_url(string url) {
72                 string old_url_path = url_re.replace(url, -1, 0, "\\3");
73                 string new_url_path = Soup.form_encode("", old_url_path).split("=")[1];
74                 new_url_path = url.replace(old_url_path, new_url_path);
75                 
76                 new_url_path = new_url_path.replace("%2F", "/");
77                 
78                 return new_url_path;
79         }
80         
81         private void download(string url, string save_name) {
82                 //var enc_name = Soup.form_encode("", save_name).split("=")[1];
83                 
84                 var pick = File.new_for_uri(url);
85                 var pick_file = File.new_for_path(cache_path + "/" + save_name);
86                 
87                 if(!pick_file.query_exists(null)) {
88                         pick.copy(pick_file, FileCopyFlags.NONE, null, null);
89                 }
90         }
91         
92         private async void download_async(string url, string save_name, bool need_emit) {
93                 //var utf_name = url.split("/")[5];
94                 //var enc_name = Soup.form_encode("", utf_name).split("=")[1];
95                 
96                 var pick = File.new_for_uri(encode_url(url));
97                 var pick_file = File.new_for_path(cache_path + "/" + save_name);
98                 
99                 if(!pick_file.query_exists(null)) {
100                         yield pick.copy_async(pick_file, FileCopyFlags.NONE, 1, null, null);
101                 }
102                 
103                 if(need_emit)
104                         pic_downloaded(url, cache_path + "/" + save_name);
105         }
106         
107         private void check_cache_dir() {
108                 //string conf_dir = Environment.get_home_dir() + "/.config/pino/";
109                 
110                 //cache_path = conf_dir + "/cache";
111                 cache_path = Environment.get_user_cache_dir() + "/%s".printf(Config.APPNAME);
112                 
113                 var cache_dir = File.new_for_path(cache_path);
114                 if(!cache_dir.query_exists(null))
115                         cache_dir.make_directory(null);
116         }
117         
118         public void delete_cache() {
119                 var cache_dir = File.new_for_path(cache_path);
120                 var en = cache_dir.enumerate_children(FILE_ATTRIBUTE_STANDARD_NAME, 0, null);
121                 
122                 FileInfo file_info;
123                 while((file_info = en.next_file(null)) != null) {
124                         var d_file = File.new_for_path(cache_path + "/" + file_info.get_name ());
125                         d_file.delete(null);
126                 }
127                 
128                 //clear hash
129                 map.clear();
130         }
131 }