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             singleton_val.ref();
31         }
32         return singleton_val;
33             
34      }
35          public Resources ()
36          {
37                  
38            this.avail_files = { 
39                    "roodata.json",
40                         "bootstrap.builder.html",
41                         "roo.builder.html",
42                         "roo.builder.js",
43                         "Gir.overides",
44                         "RooUsage.txt",
45                         "GtkUsage.txt",
46                         "Editors/Edit.Roo.grid.Grid.js"
47                 };
48         }       
49                  
50                  
51     
52      uint fetch_pos = 0;
53      public void fetchStart()
54      {
55             if (this.fetch_pos > 0) { // only fetch one at a time...
56                 return;
57             }
58             this.fetch_pos =0;
59             this.fetchNext();
60          
61      }
62      public void fetchNext()
63     {
64         var cur = this.fetch_pos;
65         this.fetch_pos++;
66         this.updateProgress(this.fetch_pos); // min=0;
67         
68         
69         if (cur > this.avail_files.length) {
70                         this.updateProgress(0);
71                      this.fetch_pos = 0;
72                      return;
73                         
74                 }
75         var target = this.avail_files[cur];
76         var src = "http://git.roojs.org/?p=app.Builder.js;a=blob_plain;f=resources/" + target;
77         if (target == "roodata.json") {
78                         src = "http://git.roojs.org/?p=roojs1;a=blob_plain;f=docs/json/roodata.json";
79                 }
80
81                 this.fetchResourceFrom (
82                         src,
83                         target,
84                         (sess,msg) => {
85                                 switch (target) {
86                                         case "Gir.overides":
87                                                 Palete.Gir.factory("Gtk").loadOverrides(true);
88                                                 break;
89                                                 
90                                         case "GtkUsage.txt":
91                                                 Palete.factory("Gtk").load();
92                                                 break;
93                                                 
94                                         case "roodata.json":
95                                                 Palete.factory("Roo").classes  = null;
96                                                 Palete.factory("Roo").load();
97                                                 break;
98                                                 
99                                         default:
100                                                 break;
101                                 }
102                             this.fetchNext();
103                 });
104                  
105
106          }
107          public void checkResources()
108          {
109                 bool needsload = false;
110                 string[] res = this.avail_files;
111                         
112                 for (var i = 0; i < res.length; i++ ) { 
113                         
114                         if (!FileUtils.test(
115                                 BuilderApplication.configDirectory() + "/resources/"  + res[i],
116                                 FileTest.EXISTS
117                                 )) {
118                                 needsload = true;
119                         }
120                 }
121                 if (!needsload) {
122                         return;
123                 }
124                 this.fetchStart();
125          }
126                  
127                         
128
129
130     
131     public void fetchResourceFrom(string src, string target)
132     {
133                  
134                 // fetch...
135                 print("downloading %s \nto : %s\n", src,target);
136                 var session = new Soup.Session ();
137                 session.user_agent = "App Builder ";
138             var message = new Soup.Message ("GET",  src );
139         session.queue_message (message, (sess, mess) => {
140                         
141                         
142                         var tfn = BuilderApplication.configDirectory() + "/resources/" + target;
143                         // create parent directory if needed
144                         if (!GLib.FileUtils.test (GLib.Path.get_dirname(tfn), FileTest.IS_DIR)) {
145                                 var f =  GLib.File.new_for_path(GLib.Path.get_dirname(tfn));
146                                 f.make_directory_with_parents ();
147                         }
148                         
149                         
150                         // set data??? - if it's binary?
151             FileUtils.set_contents(  tfn, (string) message.response_body.data );
152             
153             switch (target) {
154                                 case "Gir.overides":
155                                         Palete.Gir.factory("Gtk").loadOverrides(true);
156                                         break;
157                                         
158                                 case "GtkUsage.txt":
159                                         Palete.factory("Gtk").load();
160                                         break;
161                                         
162                                 case "roodata.json":
163                                         Palete.factory("Roo").classes  = null;
164                                         Palete.factory("Roo").load();
165                                         break;
166                                         
167                                 default:
168                                         break;
169                         }
170             
171             this.fetchNext();
172              
173         });
174                      
175
176     }
177 }