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