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