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