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 Resources : Object
18 {
19
20      public signal void updateProgress(uint cur_pos);
21
22      static Resources singleton_val;
23      
24      string[] avail_files;
25      
26      public static Resources singleton()
27      {
28         if (singleton_val == null) {
29             singleton_val = new Resources();
30         }
31         return singleton_val;
32             
33      }
34          public Resources ()
35          {
36                  
37                    this.avail_files = { 
38                            "roodata.json",
39                                 "bootstrap.builder.html",
40                                 "roo.builder.html",
41                                 "roo.builder.js",
42                                 "Gir.overides",
43                                 "RooUsage.txt",
44                                 "GtkUsage.txt"
45                         };
46                         
47                  
48                  
49     
50      uint fetch_pos = 0;
51      public void fetchStart()
52      {
53             if (this.fetch_pos > 0) { // only fetch one at a time...
54                 return;
55             }
56             this.fetch_pos =0;
57             this.fetchNext();
58          
59      }
60      public void fetchNext()
61     {
62         var cur = this.fetch_pos;
63         this.fetch_pos++;
64         this.updateProgress(this.fetch_pos); // min=0;
65         
66         
67         if (cur > this.avail_files.length) {
68                         this.updateProgress(0);
69                      this.fetch_pos = 0;
70                      return;
71                         
72                 }
73         var target = this.avail_files[cur];
74         var src = "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/" + target;
75         if (target == "roodata.json") {
76                         src = "http://git.roojs.org/?p=roojs1;a=blob_plain;f=docs/json/roodata.json";
77                 }
78
79                 this.fetchResourceFrom (
80                         src,
81                         target,
82                         (sess,msg) => {
83                                 switch (target) {
84                                         case "Gir.overides":
85                                                 Palete.Gir.factory("Gtk").loadOverrides(true);
86                                                 break;
87                                                 
88                                         case "GtkUsage.txt":
89                                                 Palete.factory("Gtk").load();
90                                                 break;
91                                                 
92                                         case "roodata.json":
93                                                 Palete.factory("Roo").classes  = null;
94                                                 Palete.factory("Roo").load();
95                                         
96                                         default:
97                                                 break;
98                                 }
99                             this.fetchNext();
100                 });
101                  ;
102         
103         switch (cur) {
104                case 0: // html for rendering Bootstrap apps.
105                     this.fetchResourceFrom (
106                         "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/bootstrap.builder.html",
107                         "bootstrap.builder.html",
108                         (sess,msg) => {
109                                this.fetchNext();
110                     });
111                     break;
112               case 1:// html for rendering Roo apps.
113                      this.fetchResourceFrom (
114                         "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/roo.builder.html",
115                         "roo.builder.html",
116                         (sess,msg) => {
117                                this.fetchNext();
118                     });
119                     break;
120              case 2: // generic javascript
121                  this.fetchResourceFrom (
122                         "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/roo.builder.js",
123                         "roo.builder.js",
124                         (sess,msg) => {
125                             // should trigger a redraw on a the webkit if it's live...
126                                this.fetchNext();
127                     });
128                     break;
129
130             case 3: // Gir overrides - used to handle the fact we are not querying valadoc yet....and gir does
131                     // not map that well to vala...
132                     this.fetchResourceFrom (
133                         "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/Gir.overides",
134                         "Gir.overides",
135                         (sess,msg) => {
136                                  Palete.Gir.factory("Gtk").loadOverrides(true);
137                                 
138                                this.fetchNext();
139                     });
140                     break;
141
142             case 4: // The main gtk tree rules 
143                     this.fetchResourceFrom (
144                         "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/GtkUsage.txt",
145                         "GtkUsage.txt",
146                         (sess,msg) => {
147                                 Palete.factory("Gtk").load();
148                                this.fetchNext();
149                     });
150                     break;
151             case 5: // The main roo tree rules 
152                     this.fetchResourceFrom (
153                         "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/RooUsage.txt",
154                         "RooUsage.txt",
155                         (sess,msg) => {
156                                 // next step triggers the 
157                                 this.fetchNext();
158                     });
159                     break;     
160           case 6: // The docs / types for Roojs - it's already in roojs if checked out..??
161                     // we might be better just checking if roojs is set up configured.
162                     
163                     this.fetchResourceFrom (
164                         "http://git.roojs.org/?p=roojs1;a=blob_plain;f=docs/json/roodata.json",
165                         "roodata.json",
166                         (sess,msg) => {
167                                 // See Palete.Roo
168                             Palete.factory("Roo").classes  = null;
169                             Palete.factory("Roo").load();
170                             this.updateProgress(0);
171                            this.fetch_pos = 0;
172                     });
173                     break;  
174         }
175
176     
177     
178     
179    
180
181          }
182          public void checkResources()
183          {
184                     bool needsload = false;
185                     string[] res = { 
186                                 "bootstrap.builder.html",
187                                 "roo.builder.html",
188                                 "roo.builder.js",
189                                 "Gir.overides",
190                                 "RooUsage.txt",
191                                 "GtkUsage.txt"
192                         };
193                         
194                 for (var i = 0; i < res.length; i++ ) { 
195                         
196                         if (!FileUtils.test(
197                                 BuilderApplication.configDirectory() + "/resources/"  + res[i],
198                                 FileTest.EXISTS
199                                 )) {
200                                 needsload = true;
201                         }
202                 }
203                 if (!needsload) {
204                         return;
205                 }
206                 this.fetchStart();
207          }
208                  
209                         
210
211
212     
213     public void fetchResourceFrom(string src, string target, Soup.SessionCallback? callback)
214     {
215                  
216                 // fetch...
217                 print("downloading %s \nto : %s\n", src,target);
218                 var session = new Soup.Session ();
219                 session.user_agent = "App Builder ";
220             var message = new Soup.Message ("GET",  src );
221         session.queue_message (message, (sess, mess) => {
222
223             FileUtils.set_contents(
224                BuilderApplication.configDirectory() + "/resources/" + target,
225                  (string) message.response_body.data
226             );
227                 
228             callback(sess,mess);
229         });
230                      
231
232     }
233 }