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         if (cur > this.avail_files.length) {
68                         this.updateProgress(0);
69                      this.fetch_pos = 0;
70                      return;
71                         
72                 }
73         var target = this.avail_files[cur];
74         var src = "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/" + target;
75         if (target == "roodata.json") {
76                         src = "http://git.roojs.org/?p=roojs1;a=blob_plain;f=docs/json/roodata.json";
77                 }
78
79                 this.fetchResourceFrom (
80                         src,
81                         target,
82                         (sess,msg) => {
83                                 switch (target) {
84                                         case "Gir.overides":
85                                                 Palete.Gir.factory("Gtk").loadOverrides(true);
86                                                 break;
87                                                 
88                                         case "GtkUsage.txt":
89                                                 Palete.factory("Gtk").load();
90                                                 break;
91                                                 
92                                         case "roodata.json":
93                                                 Palete.factory("Roo").classes  = null;
94                                                 Palete.factory("Roo").load();
95                                         
96                                         default:
97                                                 break;
98                                 }
99                             this.fetchNext();
100                 });
101                  ;
102          
103     
104     
105     
106    
107
108          }
109          public void checkResources()
110          {
111                     bool needsload = false;
112                     string[] res = { 
113                                 "bootstrap.builder.html",
114                                 "roo.builder.html",
115                                 "roo.builder.js",
116                                 "Gir.overides",
117                                 "RooUsage.txt",
118                                 "GtkUsage.txt"
119                         };
120                         
121                 for (var i = 0; i < res.length; i++ ) { 
122                         
123                         if (!FileUtils.test(
124                                 BuilderApplication.configDirectory() + "/resources/"  + res[i],
125                                 FileTest.EXISTS
126                                 )) {
127                                 needsload = true;
128                         }
129                 }
130                 if (!needsload) {
131                         return;
132                 }
133                 this.fetchStart();
134          }
135                  
136                         
137
138
139     
140     public void fetchResourceFrom(string src, string target, Soup.SessionCallback? callback)
141     {
142                  
143                 // fetch...
144                 print("downloading %s \nto : %s\n", src,target);
145                 var session = new Soup.Session ();
146                 session.user_agent = "App Builder ";
147             var message = new Soup.Message ("GET",  src );
148         session.queue_message (message, (sess, mess) => {
149
150             FileUtils.set_contents(
151                BuilderApplication.configDirectory() + "/resources/" + target,
152                  (string) message.response_body.data
153             );
154                 
155             callback(sess,mess);
156         });
157                      
158
159     }
160 }