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