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         string target;
19         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                         "resources/bootstrap.builder.html",
53                         "resources/roo.builder.html",
54                         "resources/roo.builder.js",
55                         "resources/Gir.overides",
56                         "resources/RooUsage.txt",
57                         "resources/GtkUsage.txt",
58                         "resources/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/" + 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/" + 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.avail_files.length) {
102                         this.updateProgress(0);
103                      this.fetch_pos = 0;
104                      return;
105                         
106                 }
107         var target = this.fetch_files.keys[cur]avail_files[cur];
108         
109         var src = "https://raw.githubusercontent.com/roojs/app.Builder.js/master/resources/" + target;
110         //var src = "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/" + target;
111         if (target == "roodata.json") {
112                         src = "https://raw.githubusercontent.com/roojs/roojs1/master/docs/json/roodata.json";
113                         //src = "http://git.roojs.org/?p=roojs1;a=blob_plain;f=docs/json/roodata.json";
114                 }
115
116                 if (target.contains('*')) {
117                         var split = target.split('*');
118                         src = "https://api.github.com/repos/roojs/app.Builder.js/contents/resources/Editors
119                         
120
121                 this.fetchResourceFrom ( src, target );
122                  
123
124          }
125          public void checkResources()
126          {
127                 bool needsload = false;
128                 string[] res = this.avail_files;
129                         
130                 for (var i = 0; i < res.length; i++ ) { 
131                         
132                         if (!FileUtils.test(
133                                 BuilderApplication.configDirectory() + "/resources/"  + res[i],
134                                 FileTest.EXISTS
135                                 )) {
136                                 needsload = true;
137                         }
138                 }
139                 if (!needsload) {
140                         return;
141                 }
142                 this.fetchStart();
143          }
144                  
145                         
146
147
148     
149     public void fetchResourceFrom(string src, string target)
150     {
151                  
152                 // fetch...
153                 print("downloading %s \nto : %s\n", src,target);
154                 var session = new Soup.Session ();
155                 session.user_agent = "App Builder ";
156             var message = new Soup.Message ("GET",  src );
157         session.queue_message (message, (sess, mess) => {
158                         
159                         
160                         var tfn = BuilderApplication.configDirectory() + "/resources/" + target;
161                         // create parent directory if needed
162                         if (!GLib.FileUtils.test (GLib.Path.get_dirname(tfn), FileTest.IS_DIR)) {
163                                 var f =  GLib.File.new_for_path(GLib.Path.get_dirname(tfn));
164                                 f.make_directory_with_parents ();
165                         }
166                         
167                         
168                         // set data??? - if it's binary?
169             FileUtils.set_contents(  tfn, (string) message.response_body.data );
170             
171             switch (target) {
172                                 case "Gir.overides":
173                                         Palete.Gir.factory("Gtk").loadOverrides(true);
174                                         break;
175                                         
176                                 case "GtkUsage.txt":
177                                         Palete.factory("Gtk").load();
178                                         break;
179                                         
180                                 case "roodata.json":
181                                         Palete.factory("Roo").classes  = null;
182                                         Palete.factory("Roo").load();
183                                         break;
184                                         
185                                 default:
186                                         break;
187                         }
188             
189             this.fetchNext();
190              
191         });
192                      
193
194     }
195 }