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)
27 this.update_cur_sha();
28 print("New ResourcesItem %s (%s) => %s\n", target , this.cur_sha ,src);
30 public void update_cur_sha()
32 if (target.contains("*")) {
35 var tfn = BuilderApplication.configDirectory() + "/resources/" + target;
36 if (!GLib.FileUtils.test (GLib.Path.get_dirname(tfn), FileTest.IS_REGULAR)) {
40 GLib.FileUtils.get_data(tfn, out data);
42 var file = File.new_for_path (tfn);
44 var info = file.query_info(
46 FileQueryInfoFlags.NONE
48 var csdata = new GLib.ByteArray.take("blob %s\0".printf(info.get_size().to_string()).data);
51 // git method... blob %d\0...string...
52 this.cur_sha = GLib.Checksum.compute_for_data(GLib.ChecksumType.SHA1,
60 public class Resources : Object
63 public signal void updateProgress(uint cur_pos);
65 static Resources singleton_val;
68 Gee.ArrayList<ResourcesItem> fetch_files;
70 public static Resources singleton()
72 if (singleton_val == null) {
73 singleton_val = new Resources();
82 string[] avail_files = {
84 /*"bootstrap.builder.html",
93 //"Editors/Editor.Roo.grid.GridPanel.js"
95 this.fetch_files = new Gee.ArrayList<ResourcesItem>();
96 for (var i=0;i < avail_files.length; i++) {
97 var target = avail_files[i];
98 var src = "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + target;
100 if (target == "roodata.json") {
101 src = "https://raw.githubusercontent.com/roojs/roojs1/master/docs/json/roodata.json";
102 //src = "http://git.roojs.org/?p=roojs1;a=blob_plain;f=docs/json/roodata.json";
104 if (target.contains("*")) {
105 var split = target.split("*");
106 src = "https://api.github.com/repos/roojs/app.Builder.js/contents/resources/" + split[0];
109 this.fetch_files.add(new ResourcesItem(src,target));
119 public void fetchStart()
121 if (this.fetch_pos > 0) { // only fetch one at a time...
128 public void fetchNext()
130 var cur = this.fetch_pos;
132 this.updateProgress(this.fetch_pos); // min=0;
135 if (this.fetch_pos > this.fetch_files.size) {
136 this.updateProgress(0);
142 this.fetchResourceFrom ( this.fetch_files.get(cur) );
146 public void checkResources()
148 bool needsload = false;
151 for (var i = 0; i < this.fetch_files.size; i++ ) {
154 BuilderApplication.configDirectory() + "/resources/" + this.fetch_files.get(i).target,
166 public void parseDirectory(string json, string target)
168 var pa = new Json.Parser();
169 pa.load_from_data(json);
170 var node = pa.get_root();
171 if (node.get_node_type () != Json.NodeType.ARRAY) {
173 //throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
176 var split = target.split("*");
177 var ar = node.get_array ();
178 for(var i = 0; i < ar.get_length(); i++) {
179 var ob = ar.get_object_element(i);
180 var n = ob.get_string_member("name");
181 if (ob.get_string_member("type") == "dir") {
184 if (split.length > 1 && !n.has_suffix(split[1])) {
188 if (this.files_has_target(split[0] + n)) {
191 var src = "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + split[0] + n;
192 var add = new ResourcesItem(src, split[0] + n);
193 add.new_sha = ob.get_string_member("sha");
194 this.fetch_files.add(add);
198 public bool files_has_target(string target)
200 for (var i = 0; i < this.fetch_files.size; i++ ) {
201 if (this.fetch_files.get(i).target == target) {
212 public void fetchResourceFrom(ResourcesItem item)
216 print("downloading %s \nto : %s\n", item.src,item.target);
217 var session = new Soup.Session ();
218 session.user_agent = "App Builder ";
219 var message = new Soup.Message ("GET", item.src );
220 session.queue_message (message, (sess, mess) => {
222 if (item.target.contains("*")) {
223 // then it's a directory listing in JSON, and we need to add any new items to our list..
224 // it's used to fetch Editors (and maybe other stuff..)
225 this.parseDirectory((string) message.response_body.data,item.target );
231 var tfn = BuilderApplication.configDirectory() + "/resources/" + item.target;
234 // create parent directory if needed
235 if (!GLib.FileUtils.test (GLib.Path.get_dirname(tfn), FileTest.IS_DIR)) {
236 var f = GLib.File.new_for_path(GLib.Path.get_dirname(tfn));
237 f.make_directory_with_parents ();
243 // set data??? - if it's binary?
244 FileUtils.set_contents( tfn, (string) message.response_body.data );
246 switch (item.target) {
248 Palete.Gir.factory("Gtk").loadOverrides(true);
252 Palete.factory("Gtk").load();
256 Palete.factory("Roo").classes = null;
257 Palete.factory("Roo").load();