src/Resources.vala
[app.Builder.js] / src / Resources.vala
1 /**
2  * Resources
3  * 
4  * Idea is to manage resourse used by the app.
5  * 
6  * The original code downloaded all the resources before it renders the main window
7  * 
8  * This is a bit annoying as although they do change quite a bit, it's not on every app start
9  * 
10  * So the Resource fetching behaviour should be a button on the File view page
11  * That starts the refresh of the resources.
12  * 
13  * I'm not quite sure how we should flow this - if we do them all at once.. might be a bit of a server 
14  * overload... so sequentially may be best...
15 */
16
17 public class ResourcesItem : Object {
18         public string target;
19         public string src;
20         public string new_sha;
21         public string cur_sha;
22         public ResourcesItem(string src, string target, string new_sha) 
23         {
24                 this.target = target;
25                 this.src = src;
26                 this.new_sha = new_sha;
27                 this.cur_sha = "";
28                 this.update_cur_sha();
29                 print("New ResourcesItem %s (%s) => (%s) %s\n", target , this.cur_sha , new_sha, src);
30         }
31         public void update_cur_sha()
32         {
33                 if (this.target.contains("*")) {
34                         return;
35                 }
36                 var tfn = BuilderApplication.configDirectory() + "/resources/" + this.target;
37                 if (!GLib.FileUtils.test (tfn, FileTest.IS_REGULAR)) {
38                         return;
39                 }
40                 uint8[] data;
41                 uint8[] zero = { 0 };
42                 GLib.FileUtils.get_data(tfn, out data);
43                 
44                 var  file = File.new_for_path (tfn);
45                  
46                 var info = file.query_info(
47                                  "standard::*",
48                                 FileQueryInfoFlags.NONE
49                 );
50                 var csdata = new GLib.ByteArray.take("blob %s".printf(info.get_size().to_string()).data);
51                 csdata.append(zero);
52                 csdata.append(data);
53                  
54                 // git method... blob %d\0...string...
55                 this.cur_sha = GLib.Checksum.compute_for_data(GLib.ChecksumType.SHA1, csdata.data       );
56         }
57         
58 }
59
60
61 public class Resources : Object
62 {
63
64      public signal void updateProgress(uint cur_pos);
65
66      static Resources singleton_val;
67      
68       
69      Gee.ArrayList<ResourcesItem> fetch_files;
70      
71      public static Resources singleton()
72      {
73         if (singleton_val == null) {
74             singleton_val = new Resources();
75             singleton_val.ref();
76         }
77         return singleton_val;
78             
79      }
80          public Resources ()
81          {
82                 this.initFiles();
83         }
84                 
85                 
86                 
87         }       
88         public void initFiles()
89         {       
90                 string[] avail_files = { 
91                         "roodata.json",
92                         "*",
93                         "Editors/*.js"
94                         
95                 };
96                 this.fetch_files = new Gee.ArrayList<ResourcesItem>();
97                 for (var i=0;i < avail_files.length; i++) {
98                         var target = avail_files[i];
99                         var src = "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + target;
100                          
101                         if (target == "roodata.json") {
102                                 src = "https://raw.githubusercontent.com/roojs/roojs1/master/docs/json/roodata.json";
103                                 //src = "http://git.roojs.org/?p=roojs1;a=blob_plain;f=docs/json/roodata.json";
104                         }
105                         if (target.contains("*")) {
106                                 var split = target.split("*");
107                                 src = "https://api.github.com/repos/roojs/app.Builder.js/contents/resources/" + split[0];
108                         }
109                         
110                         this.fetch_files.add(new ResourcesItem(src,target, ""));
111                 }
112         
113         }        
114                  
115     
116      int fetch_pos = 0;
117      public void fetchStart()
118      {
119             this.initFiles();
120             if (this.fetch_pos > 0) { // only fetch one at a time...
121                 return;
122             }
123             this.fetch_pos =0;
124             this.fetchNext();
125          
126      }
127      public void fetchNext()
128     {
129         var cur = this.fetch_pos;
130         this.fetch_pos++;
131         this.updateProgress(this.fetch_pos); // min=0;
132         
133         
134         if (this.fetch_pos > this.fetch_files.size) {
135                          this.updateProgress(0);
136                      this.fetch_pos = 0;
137                      return;
138                         
139                 }
140          
141                 this.fetchResourceFrom ( this.fetch_files.get(cur) );
142                  
143
144          }
145          public void checkResources()
146          {
147                 bool needsload = false;
148                  
149                         
150                 for (var i = 0; i <  this.fetch_files.size; i++ ) { 
151                         if (this.fetch_files.get(i).target.contains("*")) {
152                                 continue;
153                         }
154                         if (!FileUtils.test(
155                                 BuilderApplication.configDirectory() + "/resources/"  + this.fetch_files.get(i).target,
156                                 FileTest.EXISTS
157                                 )) {
158                                 needsload = true;
159                         }
160                 }
161                 if (!needsload) {
162                         return;
163                 }
164                 this.fetchStart();
165          }
166                  
167         public void parseDirectory(string json, string target)
168         {
169                 var pa = new Json.Parser();
170                 pa.load_from_data(json);
171                 var node = pa.get_root();
172                 if (node.get_node_type () != Json.NodeType.ARRAY) {
173                         return;
174                         //throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
175                 }
176                 
177                 var split = target.split("*");
178                 var ar = node.get_array ();
179                 for(var i = 0; i < ar.get_length(); i++) {
180                         var ob = ar.get_object_element(i);
181                         var n = ob.get_string_member("name");
182                         if (ob.get_string_member("type") == "dir") {
183                                 continue;
184                         }
185                         if (split.length > 1 && !n.has_suffix(split[1])) {
186                                 // not needed..
187                                 continue;
188                         }
189                         if (this.files_has_target(split[0] + n)) {
190                                 continue;
191                         }
192                         var src = "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + split[0] + n;
193                         var add = new ResourcesItem(src, split[0] + n, ob.get_string_member("sha") );
194                         //add.new_sha = ob.get_string_member("sha");
195                         this.fetch_files.add(add);
196                         
197                 }
198         }
199         public bool files_has_target(string target)
200         {
201                 for (var i = 0; i <  this.fetch_files.size; i++ ) { 
202                         if (this.fetch_files.get(i).target == target) { 
203                                 return true;
204                         }
205                 }
206                 return false;
207                 
208         }
209         
210
211
212     
213     public void fetchResourceFrom(ResourcesItem item)
214     {
215                 if (item.new_sha != "" && item.new_sha == item.cur_sha) {
216                         this.fetchNext();
217                         return;
218                 }
219                  
220                 // fetch...
221                 print("downloading %s \nto : %s\n", item.src,item.target);
222                 var session = new Soup.Session ();
223                 session.user_agent = "App Builder ";
224             var message = new Soup.Message ("GET",  item.src );
225         session.queue_message (message, (sess, mess) => {
226                         
227                         if (item.target.contains("*")) {
228                                 // then it's a directory listing in JSON, and we need to add any new items to our list..
229                                 // it's used to fetch Editors (and maybe other stuff..)
230                                 this.parseDirectory((string) message.response_body.data,item.target );
231                                 this.fetchNext();
232                                 return;
233                         }
234                         
235                         
236                         var tfn = BuilderApplication.configDirectory() + "/resources/" + item.target;
237                         
238                         
239                         // create parent directory if needed
240                         if (!GLib.FileUtils.test (GLib.Path.get_dirname(tfn), FileTest.IS_DIR)) {
241                                 var f =  GLib.File.new_for_path(GLib.Path.get_dirname(tfn));
242                                 f.make_directory_with_parents ();
243                         }
244                         
245                         
246                         
247                         
248                         // set data??? - if it's binary?
249             FileUtils.set_contents(  tfn, (string) message.response_body.data );
250             
251             switch (item.target) {
252                                 case "Gir.overides":
253                                         Palete.Gir.factory("Gtk").loadOverrides(true);
254                                         break;
255                                         
256                                 case "GtkUsage.txt":
257                                         Palete.factory("Gtk").load();
258                                         break;
259                                         
260                                 case "roodata.json":
261                                         Palete.factory("Roo").classes  = null;
262                                         Palete.factory("Roo").load();
263                                         break;
264                                         
265                                 default:
266                                         break;
267                         }
268             
269             
270             
271             
272             
273             
274             this.fetchNext();
275              
276         });
277                      
278
279     }
280 }