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      var fetch_pos = 0;
21      public void fetchStart()
22      {
23           this.fetch_pos =0;
24          this.fetchNext();
25          
26      }
27      public void fetchNext()
28     {
29         var cur = this.fetch_pos;
30         this.fetch_pos++;
31         switch (cur) {
32                case 0: // html for rendering Bootstrap apps.
33                     this.fetchResourceFrom (
34                         "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/bootstrap.builder.html",
35                         "bootstrap.builder.html",
36                         (sess,msg) => {
37                                this.fetchNext();
38                     });
39                     break;
40               case 1:
41                      this.fetchResourceFrom (
42                         "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/roo.builder.html",
43                         "roo.builder.html",
44                         (sess,msg) => {
45                                this.fetchNext();
46                     });
47                     break;
48                  this.fetchResourceFrom (
49                         "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/roo.builder.html",
50                         "roo.builder.html",
51                         (sess,msg) => {
52                                this.fetchNext();
53                     });
54                     break;
55      )
56
57     
58     
59     
60      string[] res = { 
61                                 "bootstrap.builder.html",
62                                 "roo.builder.html",
63                                 "roo.builder.js",
64                                 "Gir.overides",
65                                 "RooUsage.txt",
66                                 "GtkUsage.txt"
67                         };
68                         for (var i = 0; i < res.length; i++ ) { 
69                                 this.fetchResource(res[i], force);
70                         }
71                         
72                         this.fetchResourceFrom (
73                                 "http://git.roojs.org/?p=roojs1;a=blob_plain;f=docs/json/roodata.json",
74                                 "roodata.json",
75                                 force
76                         );
77                         
78
79                 }
80                 public void fetchResource(string res, bool force) {
81                         if (!force && FileUtils.test(configDirectory() + "/resources/" + res, FileTest.EXISTS)) {
82                                 return;
83                         }
84                         this.fetchResourceFrom(
85                                "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/" + res,
86                                res,
87                                force
88                        );
89                         
90
91
92     
93     public void fetchResourceFrom(string src, string target, Soup.SessionCallback? callback)
94     {
95                  
96                 // fetch...
97                 print("downloading %s \nto : %s\n", src,res);
98                 var session = new Soup.Session ();
99                 session.user_agent = "App Builder ";
100             var message = new Soup.Message ("GET",  src );
101         session.queue_message (message, (sess, mess) => {
102
103             FileUtils.set_contents(
104                Application.configDirectory() + "/resources/" + res,
105                  (string) message.response_body.data
106             );
107                 
108             callback(sess,mess);
109         });
110                      
111
112         }