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 page load
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      delegate void updateProgress(uint cur_pos);
21
22     static Resources singleton_val;
23     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:
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                                this.fetchNext();
71                     });
72                     break;
73
74             case 3: // Gir overrides - used to handle the fact we are not querying valadoc yet....and gir does
75                     // not map that well to vala...
76                     this.fetchResourceFrom (
77                         "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/Gir.overides",
78                         "Gir.overides",
79                         (sess,msg) => {
80                                 (new Palete.GirObject()).loadOverides(true);
81                                 
82                                this.fetchNext();
83                     });
84                     break;
85
86             case 4: // The main gtk tree rules 
87                     this.fetchResourceFrom (
88                         "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/GtkUsage.txt",
89                         "GtkUsage.txt",
90                         (sess,msg) => {
91                                 Palete factory("Gtk").load();
92                                this.fetchNext();
93                     });
94                     break;
95             case 5: // The main roo tree rules 
96                     this.fetchResourceFrom (
97                         "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/RooUsage.txt",
98                         "RooUsage.txt",
99                         (sess,msg) => {
100                                 // next step triggers the 
101                                 this.fetchNext();
102                     });
103                     break;     
104           case 6: // The docs / types for Roojs - it's already in roojs if checked out..??
105                     // we might be better just checking if roojs is set up configured.
106                     
107                     this.fetchResourceFrom (
108                         "http://git.roojs.org/?p=roojs1;a=blob_plain;f=docs/json/roodata.json",
109                         "roodata.json",
110                         (sess,msg) => {
111                                 // See Palete.Roo
112                             Palete factory("Roo").classes. = null;
113                             Palete factory("Roo").load();
114                                 this.updateProgress(0);
115                                this.fetch_pos = 0;
116                     });
117                     break;  
118         }
119
120     
121     
122     
123    
124
125          }
126                         
127
128
129     
130     public void fetchResourceFrom(string src, string target, Soup.SessionCallback? callback)
131     {
132                  
133                 // fetch...
134                 print("downloading %s \nto : %s\n", src,res);
135                 var session = new Soup.Session ();
136                 session.user_agent = "App Builder ";
137             var message = new Soup.Message ("GET",  src );
138         session.queue_message (message, (sess, mess) => {
139
140             FileUtils.set_contents(
141                Application.configDirectory() + "/resources/" + res,
142                  (string) message.response_body.data
143             );
144                 
145             callback(sess,mess);
146         });
147                      
148
149         }