Fix #8037 - bug on zoom
[roobuilder] / 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                 try {
43                         GLib.FileUtils.get_data(tfn, out data);
44                         
45                         var  file = File.new_for_path (tfn);
46                          
47                         var info = file.query_info(
48                                          "standard::*",
49                                         FileQueryInfoFlags.NONE
50                         );
51                         var csdata = new GLib.ByteArray.take("blob %s".printf(info.get_size().to_string()).data);
52                         csdata.append(zero);
53                         csdata.append(data);
54                          
55                         // git method... blob %d\0...string...
56                         this.cur_sha = GLib.Checksum.compute_for_data(GLib.ChecksumType.SHA1, csdata.data       );
57                 } catch (GLib.Error e) {
58                         GLib.debug("Failed to update SHA: %s", e.message);
59                 }
60         }
61         
62 }
63
64
65 public class Resources : Object
66 {
67
68      public signal void updateProgress(uint cur_pos, uint total);
69
70      static Resources singleton_val;
71      
72       
73      Gee.ArrayList<ResourcesItem> fetch_files;
74      
75      public static Resources singleton()
76      {
77         if (singleton_val == null) {
78             singleton_val = new Resources();
79             singleton_val.ref();
80         }
81         return singleton_val;
82             
83      }
84          public Resources ()
85          {
86                 this.initFiles();
87         }
88                 
89                  
90         public void initFiles()
91         {       
92                 string[] avail_files = { 
93                         "roodata.json", /// << this is the only one we get now..
94 //                      "flutter_tree.json",
95 //                      "*",
96         //              "Editors/*.js",
97 //                      "vapi/*"
98                         
99                 };
100                 this.fetch_files = new Gee.ArrayList<ResourcesItem>();
101                 for (var i=0;i < avail_files.length; i++) {
102                         var target = avail_files[i];
103                         var src = "https://raw.githubusercontent.com/roojs/roobuilder/master/resources/" + target;
104                          
105                         if (target == "roodata.json") {
106                                 src = "https://raw.githubusercontent.com/roojs/roojs1/master/docs/json/roodata.json";
107                         }
108                         if (target == "flutter_tree.json") {
109                                 src = "https://raw.githubusercontent.com/roojs/flutter-docs-json/master/tree.json";
110                         }
111                         /*
112                         if (target.contains("*")) {
113                                 var split = target.split("*");
114                                 
115                                 src = "https://api.github.com/repos/roojs/roobuilder/contents/resources/" + split[0];
116                                 if (split[0] == "vapi/") {
117                                         src = "https://api.github.com/repos/roojs/roobuilder/contents/src/vapi";
118                                         
119                                 }
120                                 
121                         }
122                         */
123                         this.fetch_files.add(new ResourcesItem(src,target, ""));
124                 }
125         
126         }        
127                  
128     
129      int fetch_pos = 0;
130      public void fetchStart()
131      {
132             this.initFiles();
133             if (this.fetch_pos > 0) { // only fetch one at a time...
134                 return;
135             }
136             this.fetch_pos =0;
137             this.fetchNext();
138          
139      }
140      public void fetchNext()
141     {
142         var cur = this.fetch_pos;
143         this.fetch_pos++;
144         this.updateProgress(this.fetch_pos, this.fetch_files.size); // min=0;
145         
146         
147         if (this.fetch_pos > this.fetch_files.size) {
148                          this.updateProgress(0,0);
149                      this.fetch_pos = 0;
150                      return;
151                         
152                 }
153          
154                 this.fetchResourceFrom ( this.fetch_files.get(cur) );
155                  
156
157          }
158          /**
159           *  called on start to check we have all the required files..
160           */
161          public void checkResources()
162          {
163                 bool needsload = false;
164                 
165                 // this has to check the required files, not the list...
166                 string[] required =  {
167                         /*"bootstrap.builder.html",
168                         "bootstrap4.builder.html",
169                          
170                         "mailer.builder.html",
171                         "roo.builder.html",
172                         "roo.builder.js",
173                         */
174                         
175                         "roodata.json",
176                         
177                         //"RooUsage.txt" ?? not needed it's doen from roodata.
178                 //      "Gir.overides" //?? needed anymnore?
179                         
180                 };
181
182                 for (var i = 0; i <  required.length; i++ ) { 
183                         
184                         if (!FileUtils.test(
185                                 BuilderApplication.configDirectory() + "/resources/"  + required[i],
186                                 FileTest.EXISTS
187                                 )) {
188                                 needsload = true;
189                         }
190                 }
191                 if (!needsload) {
192                         return;
193                 }
194                 this.fetchStart();
195          }
196                  
197         public void parseDirectory(string json, string target)
198         {
199                 GLib.debug("%s\n", json);
200                 var pa = new Json.Parser();
201                 try {
202                         pa.load_from_data(json);
203                 } catch (GLib.Error e) {
204                         GLib.debug("Faile dto load json file %s", e.message);
205                         return;
206                 }
207                 var node = pa.get_root();
208                 if (node.get_node_type () != Json.NodeType.ARRAY) {
209                         return;
210                         //throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
211                 }
212                 
213                 var split = target.split("*");
214                 var ar = node.get_array ();
215                 for(var i = 0; i < ar.get_length(); i++) {
216                         var ob = ar.get_object_element(i);
217                         var n = ob.get_string_member("name");
218                         if (ob.get_string_member("type") == "dir") {
219                                 continue;
220                         }
221                         if (split.length > 1 && !n.has_suffix(split[1])) {
222                                 // not needed..
223                                 continue;
224                         }
225                         if (this.files_has_target(split[0] + n)) {
226                                 continue;
227                         }
228                         
229                         
230                         
231                         var src = ob.get_string_member("download_url"); 
232                                         // "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + split[0] + n;
233                         var add = new ResourcesItem(src, split[0] + n, ob.get_string_member("sha") );
234                         //add.new_sha = ob.get_string_member("sha");
235                         this.fetch_files.add(add);
236                         
237                 }
238         }
239         public bool files_has_target(string target)
240         {
241                 for (var i = 0; i <  this.fetch_files.size; i++ ) { 
242                         if (this.fetch_files.get(i).target == target) { 
243                                 return true;
244                         }
245                 }
246                 return false;
247                 
248         }
249         
250
251
252     
253     public void fetchResourceFrom(ResourcesItem item)
254     {
255                 if (item.new_sha != "" && item.new_sha == item.cur_sha) {
256                         this.fetchNext();
257                         return;
258                 }
259                  
260                 // fetch...
261                 print("downloading %s \nto : %s\n", item.src,item.target);
262                 var session = new Soup.Session ();
263                 session.user_agent = "App Builder ";
264             var message = new Soup.Message ("GET",  item.src );
265         session.send_and_read_async.begin( message, 0,  null, (obj, res) => {
266                 GLib.Bytes bytes;
267                  try {
268                                 bytes = session.send_and_read_async.end(res);
269                           } catch (GLib.Error e) {
270                                 GLib.debug("Failed to fetch stream %s" , e.message);
271                                 this.fetchNext();
272                                 return;
273                         }
274                                 
275                         
276                 
277                         if (item.target.contains("*")) {
278                                 // then it's a directory listing in JSON, and we need to add any new items to our list..
279                                 // it's used to fetch Editors (and maybe other stuff..)
280                                 this.parseDirectory( (string) bytes.get_data(),item.target );
281                                 this.fetchNext();
282                                 return;
283                         }
284                         
285                          
286                         var tfn = BuilderApplication.configDirectory() + "/resources/" + item.target;
287                         
288                         
289                         // create parent directory if needed
290                         if (!GLib.FileUtils.test (GLib.Path.get_dirname(tfn), FileTest.IS_DIR)) {
291                                 var f =  GLib.File.new_for_path(GLib.Path.get_dirname(tfn));
292                                 try {
293                                         f.make_directory_with_parents ();
294                                 } catch(GLib.Error e) {
295                                         GLib.error("Problem creating directory %s", e.message);
296                                 }
297                         }
298                         
299                         
300                         // set data??? - if it's binary?
301                         try {
302                          FileUtils.set_contents(  tfn,  (string) bytes.get_data());
303             } catch(GLib.Error e) {
304                                         GLib.error("Problem writing data %s", e.message);
305                                 }
306             switch (item.target) {
307             /*
308                                 case "Gir.overides":
309                                         // clear all the project caches....
310                                         foreach(var p in Project.Project.allProjectsByName()) { 
311                                                 if (p is Project.Gtk) {
312                                                         ((Project.Gtk)p).gir_cache = new Gee.HashMap<string,Palete.Gir>();
313                                                 }
314                                         }
315
316                                         break;
317                                 */ 
318                                         
319                                 case "roodata.json":
320                                         Palete.Roo.classes_cache = null; // clear the cache.
321                                         foreach(var p in Project.Project.allProjectsByName()) { 
322                                                 if (p is Project.Roo) {
323                                                         p.palete = new Palete.Roo(p);
324                                                         //p.palete.load();
325                                                 }
326                                         }
327                                         break;
328                                         
329                                 default:
330                                         break;
331                         }
332             
333             
334             
335             
336             
337             
338             this.fetchNext();
339              
340         });
341                      
342
343     }
344 }