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