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\n", target ,src);
29         }
30         public 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                 
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                  
47                 this.size = info.get_size();
48                 // git method... blob %d\0...string...
49                 this.cur_sha = GLib.Checksum.compute_for_data(GLib.ChecksumType.SHA1,
50                                 "blob %d\0".printf(info.get_size()).data + data
51                 );
52         
53         
54 }
55
56
57 public class Resources : Object
58 {
59
60      public signal void updateProgress(uint cur_pos);
61
62      static Resources singleton_val;
63      
64       
65      Gee.ArrayList<ResourcesItem> fetch_files;
66      
67      public static Resources singleton()
68      {
69         if (singleton_val == null) {
70             singleton_val = new Resources();
71             singleton_val.ref();
72         }
73         return singleton_val;
74             
75      }
76          public Resources ()
77          {
78                  
79                 var avail_files = { 
80                         "roodata.json",
81                         "bootstrap.builder.html",
82                         "roo.builder.html",
83                         "roo.builder.js",
84                         "Gir.overides",
85                         "RooUsage.txt",
86                         "GtkUsage.txt",
87                         "Editors/*.js"
88                         //"Editors/Editor.Roo.grid.GridPanel.js"
89                 };
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;
94                          
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";
98                         }
99                         if (target.contains('*')) {
100                                 var split = target.split('*');
101                                 src = "https://api.github.com/repos/roojs/app.Builder.js/contents/resources/" + split[0];
102                         }
103                         
104                         this.fetch_files.add(new ResourcesItem(src,target));
105                 }
106                 
107                 
108                 
109         }       
110                  
111                  
112     
113      uint fetch_pos = 0;
114      public void fetchStart()
115      {
116             if (this.fetch_pos > 0) { // only fetch one at a time...
117                 return;
118             }
119             this.fetch_pos =0;
120             this.fetchNext();
121          
122      }
123      public void fetchNext()
124     {
125         var cur = this.fetch_pos;
126         this.fetch_pos++;
127         this.updateProgress(this.fetch_pos); // min=0;
128         
129         
130         if (this.fetch_pos > this.fetch_files.size) {
131                          this.updateProgress(0);
132                      this.fetch_pos = 0;
133                      return;
134                         
135                 }
136          
137                 this.fetchResourceFrom ( src, target );
138                  
139
140          }
141          public void checkResources()
142          {
143                 bool needsload = false;
144                 string[] res = this.fetch_files;
145                         
146                 for (var i = 0; i <  this.fetch_files.length; i++ ) { 
147                         
148                         if (!FileUtils.test(
149                                 BuilderApplication.configDirectory() + "/resources/"  + this.fetch_files.get(i).target
150                                 FileTest.EXISTS
151                                 )) {
152                                 needsload = true;
153                         }
154                 }
155                 if (!needsload) {
156                         return;
157                 }
158                 this.fetchStart();
159          }
160                  
161         public void parseDirectory(string json, string target)
162         {
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) {
167                         return;
168                         //throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
169                 }
170                 
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");
176                          
177                         if (split.length > 1 && !name.has_suffix(split[1])) {
178                                 // not related..
179                                 continue;
180                         }
181                         if (this.files_has_target(split[0] + n)) {
182                                 continue;
183                         }
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);
186                         
187                 }
188         }
189         public bool files_has_target(string target)
190         {
191                 for (var i = 0; i <  this.fetch_files.length; i++ ) { 
192                         if (this.fetch_files.get(i).target == target) { 
193                                 return true;
194                         }
195                 }
196                 return false;
197                 
198         }
199         
200
201
202     
203     public void fetchResourceFrom(ResourcesItem item)
204     {
205                  
206                 // fetch...
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) => {
212                         
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 );
217                                 this.fetchNext();
218                                 return;
219                         }
220                         
221                         
222                         var tfn = BuilderApplication.configDirectory() + "/resources/" + target;
223                         
224                         
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 ();
229                         }
230                         
231                         
232                         
233                         
234                         // set data??? - if it's binary?
235             FileUtils.set_contents(  tfn, (string) message.response_body.data );
236             
237             switch (item.target) {
238                                 case "Gir.overides":
239                                         Palete.Gir.factory("Gtk").loadOverrides(true);
240                                         break;
241                                         
242                                 case "GtkUsage.txt":
243                                         Palete.factory("Gtk").load();
244                                         break;
245                                         
246                                 case "roodata.json":
247                                         Palete.factory("Roo").classes  = null;
248                                         Palete.factory("Roo").load();
249                                         break;
250                                         
251                                 default:
252                                         break;
253                         }
254             
255             
256             
257             
258             
259             
260             this.fetchNext();
261              
262         });
263                      
264
265     }
266 }