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, string new_sha) 
23         {
24                 this.target = target;
25                 this.src = src;
26                 this.new_sha = new_sha;
27                 this.cur_sha = "";
28                 this.update_cur_sha();
29                 print("New ResourcesItem %s (%s) => %s\n", target , this.cur_sha ,src);
30         }
31         public void update_cur_sha()
32         {
33                 if (this.target.contains("*")) {
34                         return;
35                 }
36                 var tfn = BuilderApplication.configDirectory() + "/resources/" + this.target;
37                 if (!GLib.FileUtils.test (tfn, FileTest.IS_REGULAR)) {
38                         return;
39                 }
40                 uint8[] data;
41                 GLib.FileUtils.get_data(tfn, out data);
42                 
43                 var  file = File.new_for_path (tfn);
44                  
45                 var info = file.query_info(
46                                  "standard::*",
47                                 FileQueryInfoFlags.NONE
48                 );
49                 var csdata = new GLib.ByteArray.take("blob %s\0".printf(info.get_size().to_string()).data);
50                 csdata.append(data);
51                  
52                 // git method... blob %d\0...string...
53                 this.cur_sha = GLib.Checksum.compute_for_data(GLib.ChecksumType.SHA1, csdata.data       );
54         }
55         
56 }
57
58
59 public class Resources : Object
60 {
61
62      public signal void updateProgress(uint cur_pos);
63
64      static Resources singleton_val;
65      
66       
67      Gee.ArrayList<ResourcesItem> fetch_files;
68      
69      public static Resources singleton()
70      {
71         if (singleton_val == null) {
72             singleton_val = new Resources();
73             singleton_val.ref();
74         }
75         return singleton_val;
76             
77      }
78          public Resources ()
79          {
80                  
81                 string[] avail_files = { 
82                         "roodata.json",
83                         "*",
84                         "Editors/*.js"
85                         
86                 };
87                 this.fetch_files = new Gee.ArrayList<ResourcesItem>();
88                 for (var i=0;i < avail_files.length; i++) {
89                         var target = avail_files[i];
90                         var src = "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + target;
91                          
92                         if (target == "roodata.json") {
93                                 src = "https://raw.githubusercontent.com/roojs/roojs1/master/docs/json/roodata.json";
94                                 //src = "http://git.roojs.org/?p=roojs1;a=blob_plain;f=docs/json/roodata.json";
95                         }
96                         if (target.contains("*")) {
97                                 var split = target.split("*");
98                                 src = "https://api.github.com/repos/roojs/app.Builder.js/contents/resources/" + split[0];
99                         }
100                         
101                         this.fetch_files.add(new ResourcesItem(src,target));
102                 }
103                 
104                 
105                 
106         }       
107                  
108                  
109     
110      int fetch_pos = 0;
111      public void fetchStart()
112      {
113             if (this.fetch_pos > 0) { // only fetch one at a time...
114                 return;
115             }
116             this.fetch_pos =0;
117             this.fetchNext();
118          
119      }
120      public void fetchNext()
121     {
122         var cur = this.fetch_pos;
123         this.fetch_pos++;
124         this.updateProgress(this.fetch_pos); // min=0;
125         
126         
127         if (this.fetch_pos > this.fetch_files.size) {
128                          this.updateProgress(0);
129                      this.fetch_pos = 0;
130                      return;
131                         
132                 }
133          
134                 this.fetchResourceFrom ( this.fetch_files.get(cur) );
135                  
136
137          }
138          public void checkResources()
139          {
140                 bool needsload = false;
141                  
142                         
143                 for (var i = 0; i <  this.fetch_files.size; i++ ) { 
144                         
145                         if (!FileUtils.test(
146                                 BuilderApplication.configDirectory() + "/resources/"  + this.fetch_files.get(i).target,
147                                 FileTest.EXISTS
148                                 )) {
149                                 needsload = true;
150                         }
151                 }
152                 if (!needsload) {
153                         return;
154                 }
155                 this.fetchStart();
156          }
157                  
158         public void parseDirectory(string json, string target)
159         {
160                 var pa = new Json.Parser();
161                 pa.load_from_data(json);
162                 var node = pa.get_root();
163                 if (node.get_node_type () != Json.NodeType.ARRAY) {
164                         return;
165                         //throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
166                 }
167                 
168                 var split = target.split("*");
169                 var ar = node.get_array ();
170                 for(var i = 0; i < ar.get_length(); i++) {
171                         var ob = ar.get_object_element(i);
172                         var n = ob.get_string_member("name");
173                         if (ob.get_string_member("type") == "dir") {
174                                 continue;
175                         }
176                         if (split.length > 1 && !n.has_suffix(split[1])) {
177                                 // not needed..
178                                 continue;
179                         }
180                         if (this.files_has_target(split[0] + n)) {
181                                 continue;
182                         }
183                         var src = "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + split[0] + n;
184                         var add = new ResourcesItem(src, split[0] + n, ob.get_string_member("sha") );
185                         add.new_sha = ob.get_string_member("sha");
186                         this.fetch_files.add(add);
187                         
188                 }
189         }
190         public bool files_has_target(string target)
191         {
192                 for (var i = 0; i <  this.fetch_files.size; i++ ) { 
193                         if (this.fetch_files.get(i).target == target) { 
194                                 return true;
195                         }
196                 }
197                 return false;
198                 
199         }
200         
201
202
203     
204     public void fetchResourceFrom(ResourcesItem item)
205     {
206                 if (item.new_sha != "" && item.new_sha == item.cur_sha) {
207                         this.fetchNext();
208                         return;
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 }