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)) {
42 GLib.FileUtils.get_data(tfn, out data);
44 var file = File.new_for_path (tfn);
46 var info = file.query_info(
48 FileQueryInfoFlags.NONE
50 var csdata = new GLib.ByteArray.take("blob %s".printf(info.get_size().to_string()).data);
54 // git method... blob %d\0...string...
55 this.cur_sha = GLib.Checksum.compute_for_data(GLib.ChecksumType.SHA1, csdata.data );
61 public class Resources : Object
64 public signal void updateProgress(uint cur_pos, uint total);
66 static Resources singleton_val;
69 Gee.ArrayList<ResourcesItem> fetch_files;
71 public static Resources singleton()
73 if (singleton_val == null) {
74 singleton_val = new Resources();
86 public void initFiles()
88 string[] avail_files = {
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;
101 if (target == "roodata.json") {
102 src = "https://raw.githubusercontent.com/roojs/roojs1/master/docs/json/roodata.json";
104 if (target == "flutter_tree.json") {
105 src = "https://raw.githubusercontent.com/roojs/flutter-docs-json/master/tree.json";
108 if (target.contains("*")) {
109 var split = target.split("*");
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";
119 this.fetch_files.add(new ResourcesItem(src,target, ""));
126 public void fetchStart()
129 if (this.fetch_pos > 0) { // only fetch one at a time...
136 public void fetchNext()
138 var cur = this.fetch_pos;
140 this.updateProgress(this.fetch_pos, this.fetch_files.size); // min=0;
143 if (this.fetch_pos > this.fetch_files.size) {
144 this.updateProgress(0,0);
150 this.fetchResourceFrom ( this.fetch_files.get(cur) );
155 * called on start to check we have all the required files..
157 public void checkResources()
159 bool needsload = false;
161 // this has to check the required files, not the list...
162 string[] required = {
163 "bootstrap.builder.html",
164 "bootstrap4.builder.html",
166 "mailer.builder.html",
173 //"RooUsage.txt" ?? not needed it's doen from roodata.
174 "Gir.overides" //?? needed anymnore?
178 for (var i = 0; i < required.length; i++ ) {
181 BuilderApplication.configDirectory() + "/resources/" + required[i],
193 public void parseDirectory(string json, string target)
196 var pa = new Json.Parser();
197 pa.load_from_data(json);
198 var node = pa.get_root();
199 if (node.get_node_type () != Json.NodeType.ARRAY) {
201 //throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
204 var split = target.split("*");
205 var ar = node.get_array ();
206 for(var i = 0; i < ar.get_length(); i++) {
207 var ob = ar.get_object_element(i);
208 var n = ob.get_string_member("name");
209 if (ob.get_string_member("type") == "dir") {
212 if (split.length > 1 && !n.has_suffix(split[1])) {
216 if (this.files_has_target(split[0] + n)) {
222 var src = ob.get_string_member("download_url");
223 // "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + split[0] + n;
224 var add = new ResourcesItem(src, split[0] + n, ob.get_string_member("sha") );
225 //add.new_sha = ob.get_string_member("sha");
226 this.fetch_files.add(add);
230 public bool files_has_target(string target)
232 for (var i = 0; i < this.fetch_files.size; i++ ) {
233 if (this.fetch_files.get(i).target == target) {
244 public void fetchResourceFrom(ResourcesItem item)
246 if (item.new_sha != "" && item.new_sha == item.cur_sha) {
252 print("downloading %s \nto : %s\n", item.src,item.target);
253 var session = new Soup.Session ();
254 session.user_agent = "App Builder ";
255 var message = new Soup.Message ("GET", item.src );
256 session.queue_message (message, (sess, mess) => {
258 if (item.target.contains("*")) {
259 // then it's a directory listing in JSON, and we need to add any new items to our list..
260 // it's used to fetch Editors (and maybe other stuff..)
261 this.parseDirectory((string) message.response_body.data,item.target );
267 var tfn = BuilderApplication.configDirectory() + "/resources/" + item.target;
270 // create parent directory if needed
271 if (!GLib.FileUtils.test (GLib.Path.get_dirname(tfn), FileTest.IS_DIR)) {
272 var f = GLib.File.new_for_path(GLib.Path.get_dirname(tfn));
273 f.make_directory_with_parents ();
279 // set data??? - if it's binary?
280 FileUtils.set_contents( tfn, (string) message.response_body.data );
282 switch (item.target) {
284 // clear all the project caches....
285 foreach(var p in Project.Project.allProjectsByName()) {
286 if (p is Project.Gtk) {
287 ((Project.Gtk)p).gir_cache = new Gee.HashMap<string,Palete.Gir>();
295 Palete.Roo.classes_cache = null; // clear the cache.
296 foreach(var p in Project.Project.allProjectsByName()) {
297 if (p is Project.Roo) {
298 p.palete = new Palete.Roo(p);