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