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)
28 print("New ResourcesItem %s => %s\n", target ,src);
30 public update_cur_sha()
33 var tfn = BuilderApplication.configDirectory() + "/resources/" + target;
34 if (!GLib.FileUtils.test (GLib.Path.get_dirname(tfn), FileTest.IS_REGULAR)) {
38 GLib.FileUtils.get_data(tfn, out data);
40 var file = File.new_for_path (tfn);
42 var info = file.query_info(
44 FileQueryInfoFlags.NONE
47 this.size = info.get_size();
48 // git method... blob %d\0...string...
49 var cs = GLib.Checksum.compute_for_data(GLib.ChecksumType.SHA1,
50 "blob %d\0".printf(info.get_size()).data + data
57 public class Resources : Object
60 public signal void updateProgress(uint cur_pos);
62 static Resources singleton_val;
65 Gee.ArrayList<ResourcesItem> fetch_files;
67 public static Resources singleton()
69 if (singleton_val == null) {
70 singleton_val = new Resources();
81 "bootstrap.builder.html",
88 //"Editors/Editor.Roo.grid.GridPanel.js"
90 this.fetch_files = new Gee.ArrayList<string>();
91 for (var i=0;i < avail_files.length; i++) {
92 var target = avail_files[i];
93 var src = "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + target;
95 if (target == "roodata.json") {
96 src = "https://raw.githubusercontent.com/roojs/roojs1/master/docs/json/roodata.json";
97 //src = "http://git.roojs.org/?p=roojs1;a=blob_plain;f=docs/json/roodata.json";
99 if (target.contains('*')) {
100 var split = target.split('*');
101 src = "https://api.github.com/repos/roojs/app.Builder.js/contents/resources/" + split[0];
104 this.fetch_files.add(new ResourcesItem(src,target));
114 public void fetchStart()
116 if (this.fetch_pos > 0) { // only fetch one at a time...
123 public void fetchNext()
125 var cur = this.fetch_pos;
127 this.updateProgress(this.fetch_pos); // min=0;
130 if (this.fetch_pos > this.fetch_files.size) {
131 this.updateProgress(0);
137 this.fetchResourceFrom ( src, target );
141 public void checkResources()
143 bool needsload = false;
144 string[] res = this.fetch_files;
146 for (var i = 0; i < this.fetch_files.length; i++ ) {
149 BuilderApplication.configDirectory() + "/resources/" + this.fetch_files.get(i).target
161 public void parseDirectory(string json, string target)
163 var pa = new Json.Parser();
164 pa.load_from_file(json);
165 var node = pa.get_root();
166 if (node.get_node_type () != Json.NodeType.ARRAY) {
168 //throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
171 var split = target.split('*');
172 var obj = node.get_array ();
173 for(var i = 0; i < node.get_length(); i++) {
174 var ob = node.get_object_element(i);
175 var n = ob.get_string_member("name");
177 if (split.length > 1 && !name.has_suffix(split[1])) {
181 if (this.files_has_target(split[0] + n)) {
184 var src = "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + split[0] + n;
185 this.fetch_files.add(new ResourcesItem(src, split[0] + n);
189 public bool files_has_target(string target)
191 for (var i = 0; i < this.fetch_files.length; i++ ) {
192 if (this.fetch_files.get(i).target == target) {
203 public void fetchResourceFrom(ResourcesItem item)
207 print("downloading %s \nto : %s\n", item.src,item.target);
208 var session = new Soup.Session ();
209 session.user_agent = "App Builder ";
210 var message = new Soup.Message ("GET", item.src );
211 session.queue_message (message, (sess, mess) => {
213 if (item.target.contains('*')) {
214 // then it's a directory listing in JSON, and we need to add any new items to our list..
215 // it's used to fetch Editors (and maybe other stuff..)
216 this.parseDirectory((string) message.response_body.data,item.target );
222 var tfn = BuilderApplication.configDirectory() + "/resources/" + target;
225 // create parent directory if needed
226 if (!GLib.FileUtils.test (GLib.Path.get_dirname(tfn), FileTest.IS_DIR)) {
227 var f = GLib.File.new_for_path(GLib.Path.get_dirname(tfn));
228 f.make_directory_with_parents ();
234 // set data??? - if it's binary?
235 FileUtils.set_contents( tfn, (string) message.response_body.data );
237 switch (item.target) {
239 Palete.Gir.factory("Gtk").loadOverrides(true);
243 Palete.factory("Gtk").load();
247 Palete.factory("Roo").classes = null;
248 Palete.factory("Roo").load();