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