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