4 * Idea is to manage resourse used by the app.
6 * The original code downloaded all the resources before it renders the main window
8 * This is a bit annoying as although they do change quite a bit, it's not on every app start
10 * So the Resource fetching behaviour should be a button on the File view page
11 * That starts the refresh of the resources.
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...
17 public class ResourcesItem : Object {
20 public string new_sha;
21 public string cur_sha;
22 public ResourcesItem(string src, string target, string new_sha)
26 this.new_sha = new_sha;
28 this.update_cur_sha();
29 print("New ResourcesItem %s (%s) => (%s) %s\n", target , this.cur_sha , new_sha, src);
31 public void update_cur_sha()
33 if (this.target.contains("*")) {
36 var tfn = BuilderApplication.configDirectory() + "/resources/" + this.target;
37 if (!GLib.FileUtils.test (tfn, FileTest.IS_REGULAR)) {
43 GLib.FileUtils.get_data(tfn, out data);
45 var file = File.new_for_path (tfn);
47 var info = file.query_info(
49 FileQueryInfoFlags.NONE
51 var csdata = new GLib.ByteArray.take("blob %s".printf(info.get_size().to_string()).data);
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);
65 public class Resources : Object
68 public signal void updateProgress(uint cur_pos, uint total);
70 static Resources singleton_val;
73 Gee.ArrayList<ResourcesItem> fetch_files;
75 public static Resources singleton()
77 if (singleton_val == null) {
78 singleton_val = new Resources();
90 public void initFiles()
92 string[] avail_files = {
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;
105 if (target == "roodata.json") {
106 src = "https://raw.githubusercontent.com/roojs/roojs1/master/docs/json/roodata.json";
108 if (target == "flutter_tree.json") {
109 src = "https://raw.githubusercontent.com/roojs/flutter-docs-json/master/tree.json";
112 if (target.contains("*")) {
113 var split = target.split("*");
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";
123 this.fetch_files.add(new ResourcesItem(src,target, ""));
130 public void fetchStart()
133 if (this.fetch_pos > 0) { // only fetch one at a time...
140 public void fetchNext()
142 var cur = this.fetch_pos;
144 this.updateProgress(this.fetch_pos, this.fetch_files.size); // min=0;
147 if (this.fetch_pos > this.fetch_files.size) {
148 this.updateProgress(0,0);
154 this.fetchResourceFrom ( this.fetch_files.get(cur) );
159 * called on start to check we have all the required files..
161 public void checkResources()
163 bool needsload = false;
165 // this has to check the required files, not the list...
166 string[] required = {
167 "bootstrap.builder.html",
168 "bootstrap4.builder.html",
170 "mailer.builder.html",
177 //"RooUsage.txt" ?? not needed it's doen from roodata.
178 "Gir.overides" //?? needed anymnore?
182 for (var i = 0; i < required.length; i++ ) {
185 BuilderApplication.configDirectory() + "/resources/" + required[i],
197 public void parseDirectory(string json, string target)
199 GLib.debug("%s\n", json);
200 var pa = new Json.Parser();
202 pa.load_from_data(json);
203 } catch (GLib.Error e) {
204 GLib.debug("Faile dto load json file %s", e.message);
207 var node = pa.get_root();
208 if (node.get_node_type () != Json.NodeType.ARRAY) {
210 //throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
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") {
221 if (split.length > 1 && !n.has_suffix(split[1])) {
225 if (this.files_has_target(split[0] + n)) {
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);
239 public bool files_has_target(string target)
241 for (var i = 0; i < this.fetch_files.size; i++ ) {
242 if (this.fetch_files.get(i).target == target) {
253 public void fetchResourceFrom(ResourcesItem item)
255 if (item.new_sha != "" && item.new_sha == item.cur_sha) {
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) => {
268 bytes = session.send_and_read_async.end(res);
269 } catch (GLib.Error e) {
270 GLib.debug("Failed to fetch stream %s" , e.message);
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 );
286 var tfn = BuilderApplication.configDirectory() + "/resources/" + item.target;
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));
293 f.make_directory_with_parents ();
294 } catch(GLib.Error e) {
295 GLib.error("Problem creating directory %s", e.message);
300 // set data??? - if it's binary?
302 FileUtils.set_contents( tfn, (string) bytes.get_data());
303 } catch(GLib.Error e) {
304 GLib.error("Problem writing data %s", e.message);
306 switch (item.target) {
308 // clear all the project caches....
309 foreach(var p in Project.Project.allProjectsByName()) {
310 if (p is Project.Gtk) {
311 ((Project.Gtk)p).gir_cache = new Gee.HashMap<string,Palete.Gir>();
319 Palete.Roo.classes_cache = null; // clear the cache.
320 foreach(var p in Project.Project.allProjectsByName()) {
321 if (p is Project.Roo) {
322 p.palete = new Palete.Roo(p);