src/Resources.vala
[app.Builder.js] / src / Resources.vala
1 /**
2  * Resources
3  * 
4  * Idea is to manage resourse used by the app.
5  * 
6  * The original code downloaded all the resources before it renders the main window
7  * 
8  * This is a bit annoying as although they do change quite a bit, it's not on every app start
9  * 
10  * So the Resource fetching behaviour should be a button on the File view page
11  * That starts the refresh of the resources.
12  * 
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...
15 */
16
17 public class ResourcesItem : Object {
18         public string target;
19         public string src;
20         public string new_sha;
21         public string cur_sha;
22         public ResourcesItem(string src, string target) 
23         {
24                 this.target = target;
25                 this.src = src;
26                 this.new_sha = "";
27                 this.update_cur_sha();
28                 print("New ResourcesItem %s (%s) => %s\n", target , this.cur_sha ,src);
29         }
30         public void update_cur_sha()
31         {
32                 if (target.contains("*")) {
33                         return;
34                 }
35                 var tfn = BuilderApplication.configDirectory() + "/resources/" + target;
36                 if (!GLib.FileUtils.test (GLib.Path.get_dirname(tfn), FileTest.IS_REGULAR)) {
37                         return;
38                 }
39                 uint8[] data;
40                 GLib.FileUtils.get_data(tfn, out data);
41                 
42                 var  file = File.new_for_path (tfn);
43                  
44                 var info = file.query_info(
45                                  "standard::*",
46                                 FileQueryInfoFlags.NONE
47                 );
48                 var csdata = new GLib.ByteArray.take("blob %s\0".printf(info.get_size().to_string()).data);
49                 csdata.append(data);
50                  
51                 // git method... blob %d\0...string...
52                 this.cur_sha = GLib.Checksum.compute_for_data(GLib.ChecksumType.SHA1,
53                                 csdata.data
54                 );
55         }
56         
57 }
58
59
60 public class Resources : Object
61 {
62
63      public signal void updateProgress(uint cur_pos);
64
65      static Resources singleton_val;
66      
67       
68      Gee.ArrayList<ResourcesItem> fetch_files;
69      
70      public static Resources singleton()
71      {
72         if (singleton_val == null) {
73             singleton_val = new Resources();
74             singleton_val.ref();
75         }
76         return singleton_val;
77             
78      }
79          public Resources ()
80          {
81                  
82                 string[] avail_files = { 
83                         "roodata.json",
84                         "bootstrap.builder.html",
85                         "roo.builder.html",
86                         "roo.builder.js",
87                         "Gir.overides",
88                         "RooUsage.txt",
89                         "GtkUsage.txt",
90                         "Editors/*.js"
91                         //"Editors/Editor.Roo.grid.GridPanel.js"
92                 };
93                 this.fetch_files = new Gee.ArrayList<ResourcesItem>();
94                 for (var i=0;i < avail_files.length; i++) {
95                         var target = avail_files[i];
96                         var src = "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + target;
97                          
98                         if (target == "roodata.json") {
99                                 src = "https://raw.githubusercontent.com/roojs/roojs1/master/docs/json/roodata.json";
100                                 //src = "http://git.roojs.org/?p=roojs1;a=blob_plain;f=docs/json/roodata.json";
101                         }
102                         if (target.contains("*")) {
103                                 var split = target.split("*");
104                                 src = "https://api.github.com/repos/roojs/app.Builder.js/contents/resources/" + split[0];
105                         }
106                         
107                         this.fetch_files.add(new ResourcesItem(src,target));
108                 }
109                 
110                 
111                 
112         }       
113                  
114                  
115     
116      int fetch_pos = 0;
117      public void fetchStart()
118      {
119             if (this.fetch_pos > 0) { // only fetch one at a time...
120                 return;
121             }
122             this.fetch_pos =0;
123             this.fetchNext();
124          
125      }
126      public void fetchNext()
127     {
128         var cur = this.fetch_pos;
129         this.fetch_pos++;
130         this.updateProgress(this.fetch_pos); // min=0;
131         
132         
133         if (this.fetch_pos > this.fetch_files.size) {
134                          this.updateProgress(0);
135                      this.fetch_pos = 0;
136                      return;
137                         
138                 }
139          
140                 this.fetchResourceFrom ( this.fetch_files.get(cur) );
141                  
142
143          }
144          public void checkResources()
145          {
146                 bool needsload = false;
147                  
148                         
149                 for (var i = 0; i <  this.fetch_files.size; i++ ) { 
150                         
151                         if (!FileUtils.test(
152                                 BuilderApplication.configDirectory() + "/resources/"  + this.fetch_files.get(i).target,
153                                 FileTest.EXISTS
154                                 )) {
155                                 needsload = true;
156                         }
157                 }
158                 if (!needsload) {
159                         return;
160                 }
161                 this.fetchStart();
162          }
163                  
164         public void parseDirectory(string json, string target)
165         {
166                 var pa = new Json.Parser();
167                 pa.load_from_data(json);
168                 var node = pa.get_root();
169                 if (node.get_node_type () != Json.NodeType.ARRAY) {
170                         return;
171                         //throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
172                 }
173                 
174                 var split = target.split("*");
175                 var ar = node.get_array ();
176                 for(var i = 0; i < ar.get_length(); i++) {
177                         var ob = ar.get_object_element(i);
178                         var n = ob.get_string_member("name");
179                          
180                         if (split.length > 1 && !n.has_suffix(split[1])) {
181                                 // not needed..
182                                 continue;
183                         }
184                         if (this.files_has_target(split[0] + n)) {
185                                 continue;
186                         }
187                         var src = "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + split[0] + n;
188                         var add = new ResourcesItem(src, split[0] + n);
189                         add.new_sha = ob.get_string_member("sha");
190                         this.fetch_files.add(add);
191                         
192                 }
193         }
194         public bool files_has_target(string target)
195         {
196                 for (var i = 0; i <  this.fetch_files.size; i++ ) { 
197                         if (this.fetch_files.get(i).target == target) { 
198                                 return true;
199                         }
200                 }
201                 return false;
202                 
203         }
204         
205
206
207     
208     public void fetchResourceFrom(ResourcesItem item)
209     {
210                  
211                 // fetch...
212                 print("downloading %s \nto : %s\n", item.src,item.target);
213                 var session = new Soup.Session ();
214                 session.user_agent = "App Builder ";
215             var message = new Soup.Message ("GET",  item.src );
216         session.queue_message (message, (sess, mess) => {
217                         
218                         if (item.target.contains("*")) {
219                                 // then it's a directory listing in JSON, and we need to add any new items to our list..
220                                 // it's used to fetch Editors (and maybe other stuff..)
221                                 this.parseDirectory((string) message.response_body.data,item.target );
222                                 this.fetchNext();
223                                 return;
224                         }
225                         
226                         
227                         var tfn = BuilderApplication.configDirectory() + "/resources/" + item.target;
228                         
229                         
230                         // create parent directory if needed
231                         if (!GLib.FileUtils.test (GLib.Path.get_dirname(tfn), FileTest.IS_DIR)) {
232                                 var f =  GLib.File.new_for_path(GLib.Path.get_dirname(tfn));
233                                 f.make_directory_with_parents ();
234                         }
235                         
236                         
237                         
238                         
239                         // set data??? - if it's binary?
240             FileUtils.set_contents(  tfn, (string) message.response_body.data );
241             
242             switch (item.target) {
243                                 case "Gir.overides":
244                                         Palete.Gir.factory("Gtk").loadOverrides(true);
245                                         break;
246                                         
247                                 case "GtkUsage.txt":
248                                         Palete.factory("Gtk").load();
249                                         break;
250                                         
251                                 case "roodata.json":
252                                         Palete.factory("Roo").classes  = null;
253                                         Palete.factory("Roo").load();
254                                         break;
255                                         
256                                 default:
257                                         break;
258                         }
259             
260             
261             
262             
263             
264             
265             this.fetchNext();
266              
267         });
268                      
269
270     }
271 }