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, uint total);
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         public void initFiles()
87         {       
88                 string[] avail_files = { 
89                         "roodata.json",
90                         "*",
91                         "Editors/*.js"
92                         
93                 };
94                 this.fetch_files = new Gee.ArrayList<ResourcesItem>();
95                 for (var i=0;i < avail_files.length; i++) {
96                         var target = avail_files[i];
97                         var src = "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + target;
98                          
99                         if (target == "roodata.json") {
100                                 src = "https://raw.githubusercontent.com/roojs/roojs1/master/docs/json/roodata.json";
101                                 //src = "http://git.roojs.org/?p=roojs1;a=blob_plain;f=docs/json/roodata.json";
102                         }
103                         if (target.contains("*")) {
104                                 var split = target.split("*");
105                                 src = "https://api.github.com/repos/roojs/app.Builder.js/contents/resources/" + split[0];
106                         }
107                         
108                         this.fetch_files.add(new ResourcesItem(src,target, ""));
109                 }
110         
111         }        
112                  
113     
114      int fetch_pos = 0;
115      public void fetchStart()
116      {
117             this.initFiles();
118             if (this.fetch_pos > 0) { // only fetch one at a time...
119                 return;
120             }
121             this.fetch_pos =0;
122             this.fetchNext();
123          
124      }
125      public void fetchNext()
126     {
127         var cur = this.fetch_pos;
128         this.fetch_pos++;
129         this.updateProgress(this.fetch_pos, this.fetch_files.size); // min=0;
130         
131         
132         if (this.fetch_pos > this.fetch_files.size) {
133                          this.updateProgress(0);
134                      this.fetch_pos = 0;
135                      return;
136                         
137                 }
138          
139                 this.fetchResourceFrom ( this.fetch_files.get(cur) );
140                  
141
142          }
143          /**
144           *  called on start to check we have all the required files..
145           */
146          public void checkResources()
147          {
148                 bool needsload = false;
149                 
150                 // this has to check the required files, not the list...
151                 string[] required =  {
152                         "bootstrap.builder.html",
153                         "Gir.overides",
154                         "GtkUsage.txt",
155                         "mailer.builder.html",
156                         "roo.builder.html",
157                         "roo.builder.js",
158                         "roodata.json",
159                         "RooUsage.txt"
160                 };
161
162                 for (var i = 0; i <  required.length; i++ ) { 
163                         
164                         if (!FileUtils.test(
165                                 BuilderApplication.configDirectory() + "/resources/"  + required[i],
166                                 FileTest.EXISTS
167                                 )) {
168                                 needsload = true;
169                         }
170                 }
171                 if (!needsload) {
172                         return;
173                 }
174                 this.fetchStart();
175          }
176                  
177         public void parseDirectory(string json, string target)
178         {
179                 var pa = new Json.Parser();
180                 pa.load_from_data(json);
181                 var node = pa.get_root();
182                 if (node.get_node_type () != Json.NodeType.ARRAY) {
183                         return;
184                         //throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
185                 }
186                 
187                 var split = target.split("*");
188                 var ar = node.get_array ();
189                 for(var i = 0; i < ar.get_length(); i++) {
190                         var ob = ar.get_object_element(i);
191                         var n = ob.get_string_member("name");
192                         if (ob.get_string_member("type") == "dir") {
193                                 continue;
194                         }
195                         if (split.length > 1 && !n.has_suffix(split[1])) {
196                                 // not needed..
197                                 continue;
198                         }
199                         if (this.files_has_target(split[0] + n)) {
200                                 continue;
201                         }
202                         var src = "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + split[0] + n;
203                         var add = new ResourcesItem(src, split[0] + n, ob.get_string_member("sha") );
204                         //add.new_sha = ob.get_string_member("sha");
205                         this.fetch_files.add(add);
206                         
207                 }
208         }
209         public bool files_has_target(string target)
210         {
211                 for (var i = 0; i <  this.fetch_files.size; i++ ) { 
212                         if (this.fetch_files.get(i).target == target) { 
213                                 return true;
214                         }
215                 }
216                 return false;
217                 
218         }
219         
220
221
222     
223     public void fetchResourceFrom(ResourcesItem item)
224     {
225                 if (item.new_sha != "" && item.new_sha == item.cur_sha) {
226                         this.fetchNext();
227                         return;
228                 }
229                  
230                 // fetch...
231                 print("downloading %s \nto : %s\n", item.src,item.target);
232                 var session = new Soup.Session ();
233                 session.user_agent = "App Builder ";
234             var message = new Soup.Message ("GET",  item.src );
235         session.queue_message (message, (sess, mess) => {
236                         
237                         if (item.target.contains("*")) {
238                                 // then it's a directory listing in JSON, and we need to add any new items to our list..
239                                 // it's used to fetch Editors (and maybe other stuff..)
240                                 this.parseDirectory((string) message.response_body.data,item.target );
241                                 this.fetchNext();
242                                 return;
243                         }
244                         
245                         
246                         var tfn = BuilderApplication.configDirectory() + "/resources/" + item.target;
247                         
248                         
249                         // create parent directory if needed
250                         if (!GLib.FileUtils.test (GLib.Path.get_dirname(tfn), FileTest.IS_DIR)) {
251                                 var f =  GLib.File.new_for_path(GLib.Path.get_dirname(tfn));
252                                 f.make_directory_with_parents ();
253                         }
254                         
255                         
256                         
257                         
258                         // set data??? - if it's binary?
259             FileUtils.set_contents(  tfn, (string) message.response_body.data );
260             
261             switch (item.target) {
262                                 case "Gir.overides":
263                                         Palete.Gir.factory("Gtk").loadOverrides(true);
264                                         break;
265                                         
266                                 case "GtkUsage.txt":
267                                         Palete.factory("Gtk").load();
268                                         break;
269                                         
270                                 case "roodata.json":
271                                         Palete.factory("Roo").classes  = null;
272                                         Palete.factory("Roo").load();
273                                         break;
274                                         
275                                 default:
276                                         break;
277                         }
278             
279             
280             
281             
282             
283             
284             this.fetchNext();
285              
286         });
287                      
288
289     }
290 }