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                                 // See Palete.Gir
81                                this.fetchNext();
82                     });
83                     break;
84
85             case 4: // The main gtk tree rules 
86                     this.fetchResourceFrom (
87                         "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/GtkUsage.txt",
88                         "GtkUsage.txt",
89                         (sess,msg) => {
90                                 // See Palete.Gtk
91                                this.fetchNext();
92                     });
93                     break;
94             case 5: // The main roo tree rules 
95                     this.fetchResourceFrom (
96                         "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/RooUsage.txt",
97                         "RooUsage.txt",
98                         (sess,msg) => {
99                                 // See Palete.Roo
100                                 this.fetchNext();
101                     });
102                     break;     
103           case 6: // The docs / types for Roojs - it's already in roojs if checked out..??
104                     // we might be better just checking if roojs is set up configured.
105                     
106                     this.fetchResourceFrom (
107                         "http://git.roojs.org/?p=roojs1;a=blob_plain;f=docs/json/roodata.json",
108                         "roodata.json",
109                         (sess,msg) => {
110                                 // See Palete.Roo
111                                 this.updateProgress(0);
112                                this.fetch_pos = 0;
113                     });
114                     break;  
115         }
116
117     
118     
119     
120    
121
122          }
123                         
124
125
126     
127     public void fetchResourceFrom(string src, string target, Soup.SessionCallback? callback)
128     {
129                  
130                 // fetch...
131                 print("downloading %s \nto : %s\n", src,res);
132                 var session = new Soup.Session ();
133                 session.user_agent = "App Builder ";
134             var message = new Soup.Message ("GET",  src );
135         session.queue_message (message, (sess, mess) => {
136
137             FileUtils.set_contents(
138                Application.configDirectory() + "/resources/" + res,
139                  (string) message.response_body.data
140             );
141                 
142             callback(sess,mess);
143         });
144                      
145
146         }