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