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