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