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);
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 = {
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;
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";
103 if (target.contains("*")) {
104 var split = target.split("*");
105 src = "https://api.github.com/repos/roojs/app.Builder.js/contents/resources/" + split[0];
108 this.fetch_files.add(new ResourcesItem(src,target, ""));
115 public void fetchStart()
118 if (this.fetch_pos > 0) { // only fetch one at a time...
125 public void fetchNext()
127 var cur = this.fetch_pos;
129 this.updateProgress(this.fetch_pos); // min=0;
132 if (this.fetch_pos > this.fetch_files.size) {
133 this.updateProgress(0);
139 this.fetchResourceFrom ( this.fetch_files.get(cur) );
143 public void checkResources()
145 bool needsload = false;
147 // this has to check the required files, not the list...
148 string[] required = {
149 "bootstrap.builder.html",
152 "mailer.builder.html",
159 for (var i = 0; i < required.length; i++ ) {
162 BuilderApplication.configDirectory() + "/resources/" + required[i],
174 public void parseDirectory(string json, string target)
176 var pa = new Json.Parser();
177 pa.load_from_data(json);
178 var node = pa.get_root();
179 if (node.get_node_type () != Json.NodeType.ARRAY) {
181 //throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
184 var split = target.split("*");
185 var ar = node.get_array ();
186 for(var i = 0; i < ar.get_length(); i++) {
187 var ob = ar.get_object_element(i);
188 var n = ob.get_string_member("name");
189 if (ob.get_string_member("type") == "dir") {
192 if (split.length > 1 && !n.has_suffix(split[1])) {
196 if (this.files_has_target(split[0] + n)) {
199 var src = "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + split[0] + n;
200 var add = new ResourcesItem(src, split[0] + n, ob.get_string_member("sha") );
201 //add.new_sha = ob.get_string_member("sha");
202 this.fetch_files.add(add);
206 public bool files_has_target(string target)
208 for (var i = 0; i < this.fetch_files.size; i++ ) {
209 if (this.fetch_files.get(i).target == target) {
220 public void fetchResourceFrom(ResourcesItem item)
222 if (item.new_sha != "" && item.new_sha == item.cur_sha) {
228 print("downloading %s \nto : %s\n", item.src,item.target);
229 var session = new Soup.Session ();
230 session.user_agent = "App Builder ";
231 var message = new Soup.Message ("GET", item.src );
232 session.queue_message (message, (sess, mess) => {
234 if (item.target.contains("*")) {
235 // then it's a directory listing in JSON, and we need to add any new items to our list..
236 // it's used to fetch Editors (and maybe other stuff..)
237 this.parseDirectory((string) message.response_body.data,item.target );
243 var tfn = BuilderApplication.configDirectory() + "/resources/" + item.target;
246 // create parent directory if needed
247 if (!GLib.FileUtils.test (GLib.Path.get_dirname(tfn), FileTest.IS_DIR)) {
248 var f = GLib.File.new_for_path(GLib.Path.get_dirname(tfn));
249 f.make_directory_with_parents ();
255 // set data??? - if it's binary?
256 FileUtils.set_contents( tfn, (string) message.response_body.data );
258 switch (item.target) {
260 Palete.Gir.factory("Gtk").loadOverrides(true);
264 Palete.factory("Gtk").load();
268 Palete.factory("Roo").classes = null;
269 Palete.factory("Roo").load();