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