JsRender/Gtk.vala.c
[app.Builder.js] / Palete / Palete.vala
1
2 namespace Palete 
3 {
4         public errordomain Error {
5                 INVALID_TYPE,
6                 NEED_IMPLEMENTING,
7                 MISSING_FILE,
8                 INVALID_VALUE
9         }
10
11         public class Usage : Object 
12         {
13                 public Gee.ArrayList<string> left;
14                 public Gee.ArrayList<string> right;
15                 public Usage(Gee.ArrayList<string> ileft, Gee.ArrayList<string> iright)
16                 {
17                         this.left = ileft;
18                         this.right=  iright;
19                 }
20                 public bool leftHas(string s)
21                 {
22                         for(var i = 0 ; i < this.left.size; i++) {
23                                 var m = this.left.get(i);
24                                 if (s == m) {
25                                         return true;
26                                 }
27                                 if (!m.contains(":")) {
28                                         continue;
29                                 }
30                                 var ar = m.split(":");
31                                 if (ar[0] == s) {
32                                         return true;
33                                 }
34                         }
35                         return false;
36                                 
37                 }
38
39                 
40         }
41
42         
43
44     static Gee.HashMap<string,Palete>? cache = null;
45     
46     public static Palete factory(string xtype)
47     {
48         if (cache == null) {
49             cache = new Gee.HashMap<string,Palete>();
50         }
51         if (cache.get(xtype) != null) {
52             return cache.get(xtype);
53         }
54         switch(xtype) {
55             case "Gtk":
56                 cache.set(xtype, new Gtk());
57                 break;
58             case "Roo":
59                 cache.set(xtype, new Roo());
60                 break;
61             default:
62                 throw new Error.INVALID_TYPE("invalid argument to Palete factory");
63         }
64         return cache.get(xtype);
65     }
66        
67
68     public abstract class Palete : Object 
69     {
70         
71        
72         public string name;
73
74         public Gee.ArrayList<Usage> map;
75                 
76         public Palete()
77         {
78             // nothing?
79                 this.map = null;
80         }
81         
82         
83         //map : false, // array of mappings   left: [] , right : []
84         
85         //items : false, // the tree of nodes.
86         
87         
88
89
90         string  guessName(JsRender.Node ar) throws Error // turns the object into full name.
91         {
92             throw new Error.NEED_IMPLEMENTING("xxx. guessName needs implimenting");
93         }
94             
95
96             
97         /**
98          * gather a  list of potentional objects that can be added..
99          * 
100          */
101         /*
102         listChildren: function (existing) {
103             existing = existing || [];
104            // existing.push('*top'); // always have top
105             var ret  = []; 
106             console.log("GATHER LIST? " + this.map.length);
107             
108             
109             function addRight(right) {
110                 right.forEach(function(r) {
111                     if (ret.indexOf(r) > -1) {
112                         return;
113                     }
114                     ret.push(r);
115                 });
116             }
117             
118             this.map.forEach(function(m) {
119                 var done = false
120                 m.left.forEach( function(left) {
121                     if (done) return; 
122                     
123                     var l = left.replace(/:.*$/, '');
124                    // print("chk:" + l + " in " + existing.join(',')); 
125                     if (existing.indexOf(l) > -1) {
126                         addRight(m.right);
127                         done =true;
128                         //return true; // no more needed..
129                     }
130                 });
131                 
132             });
133             ret.sort();
134             
135            // console.dump(ret);
136             return ret;
137             
138             
139             
140         },
141         */
142         
143
144             
145         public string[] getChildList(string in_rval)
146         {
147
148                 if (this.map == null) {
149                         this.load();
150                 }
151                 // empty == *top
152                 
153                 var rval = in_rval == "" ? "*top" : in_rval; 
154                                 
155                                 // should be a bit more than this..
156                         // -> it should look for all elements that inherit 
157                 string[] ret = {};
158                 var rt = new Gee.ArrayList<string>();
159                 for (var i = 0; i < this.map.size; i++) {
160                         var m = this.map.get(i);
161                                 
162                         if (!m.leftHas(rval)) {
163                                 continue;
164                         }
165                         print("found LEFT, adding right\n");
166                 
167                         for(var ii =0; ii < m.right.size; ii++) {
168                                 var l = m.right.get(ii);
169                                         
170                                 if (rt.index_of(l) > -1) {
171                                     continue;
172                                 }
173                                 //print("ADD " + string.joinv(", ", ret) + "\n");
174                                 ret += l;
175                                 rt.add(l);
176                         }
177                         
178                 
179                 }
180                 print ("drop list for %s is:\n%s\n", rval, string.joinv("\n", ret));
181                 //console.log("DROP LIST:");
182                 //console.dump(ret);
183                 return ret;
184             
185         }
186
187             
188         public string[] getDropList(string rval)
189         {
190
191                 if (this.map == null) {
192                         this.load();
193                 }
194
195                                 
196                                 // should be a bit more than this..
197                         // -> it should look for all elements that inherit 
198                 string[] ret = {};
199                 var rt = new Gee.ArrayList<string>();
200                 for (var i = 0; i < this.map.size; i++) {
201                         var m = this.map.get(i);
202                                 
203                         if (m.right.index_of(rval) < 0) {
204                                 continue;
205                         }
206                         //print("found RIGHT, adding left\n");
207                 
208                         for(var ii =0; ii < m.left.size; ii++) {
209                                 var l = m.left.get(ii);
210                                         
211                                 if (rt.index_of(l) > -1) {
212                                     continue;
213                                 }
214                                 //print("ADD " + string.joinv(", ", ret) + "\n");
215                                 ret += l;
216                                 rt.add(l);
217                         }
218                         
219                 
220                 }
221                  print ("drop list for %s is:\n%s\n", rval, string.joinv("\n", ret));
222                 //console.log("DROP LIST:");
223                 //console.dump(ret);
224                 return ret;
225             
226         }
227         /**
228          * basic guess type.. 
229          * 
230          * /
231         findType : function (data, prop, value)
232         {
233             if (prop[0] == '|') {
234                 return 'function';
235             }
236             return typeof(value);
237         },
238         
239         
240         findOptions : function(ename)
241         {
242             switch(ename.toLowerCase()) {
243                 case 'boolean': 
244                     return [ 'true', 'false' ];
245                 // everything else does not have options.
246                 case 'string': 
247                 case 'utf8': 
248                 case 'int': 
249                 case 'uint': 
250                 case 'function': 
251                     return false;
252                 default: 
253                     console.log("OOPS: = unknown type: " + ename);
254                     return false;
255             }
256         },
257         confirmCanAdd: function(parent, child) {
258             // confirms that one obj can be added to another.
259             // returns true, for items, or list of properties that can hold it..
260             return true;
261             
262         },
263         getDefaultPack: function(pname, cname) {
264             return 'add';
265         },
266         */
267         public void saveTemplate (string name, JsRender.Node data)
268         {
269
270                         var gn = data.fqn();
271             // store it in user's directory..
272             var appdir =  GLib.Environment.get_home_dir() + "/.Builder"; 
273
274                         
275             if (!GLib.FileUtils.test(appdir+ "/" + gn, GLib.FileTest.IS_DIR)) {
276                                 GLib.File.new_for_path (appdir+ "/" + gn).make_directory ();
277                                 
278             }
279             GLib.FileUtils.set_contents(appdir+ "/" + gn + "/" +  name + ".json", data.toJsonString());
280             
281         }
282         
283         /**
284          * list templates - in home directory (and app dir in future...)
285          * @param {String} name  - eg. Gtk.Window..
286          * @return {Array} list of templates available..
287          */
288           
289         public  GLib.List<string> listTemplates (JsRender.Node node)
290         {
291             
292                 var gn = node.fqn();
293             
294                 var ret = new GLib.List<string>();
295                 var dir= GLib.Environment.get_home_dir() + "/.Builder/" + gn;
296                 if (!GLib.FileUtils.test(dir, GLib.FileTest.IS_DIR)) {
297                         return ret;
298                 }
299                         
300
301
302                                     
303                 var f = File.new_for_path(dir);
304         
305             var file_enum = f.enumerate_children(GLib.FileAttribute.STANDARD_DISPLAY_NAME, GLib.FileQueryInfoFlags.NONE, null);
306              
307             FileInfo next_file; 
308             while ((next_file = file_enum.next_file(null)) != null) {
309                 var n = next_file.get_display_name();
310                         if (!Regex.match_simple ("\\.json$", n)) {
311                                         continue;
312                                 }
313                                 ret.append( dir + "/" + n);
314             }
315             return ret;
316             
317         }
318  
319         public JsRender.Node? loadTemplate(string path)
320         {
321
322                 var pa = new Json.Parser();
323                 pa.load_from_file(path);
324                 var node = pa.get_root();
325
326                 if (node.get_node_type () != Json.NodeType.OBJECT) {
327                         return null;
328                 }
329                 var obj = node.get_object ();
330
331                 var ret = new JsRender.Node();
332
333
334                 ret.loadFromJson(obj, 1);
335                 ret.ref(); // not sure if needed -- but we had a case where ret became uninitialized?
336                 
337                 return ret;
338          }
339           public   void  loadUsageFile (string fname) {
340
341
342
343                         
344                 print("Palete Load called\n");
345                 string raw;
346                 if (!FileUtils.test (fname, FileTest.EXISTS)) {
347                         throw new Error.MISSING_FILE(fname + " not found");
348                 }
349         
350                 FileUtils.get_contents(fname, out raw);
351               // print(data);
352                 var data  = raw.split("\n");
353                 var state = 0;
354                 var cfg = new Gee.ArrayList<Usage>();
355                 var left = new Gee.ArrayList<string>();
356                 var right = new Gee.ArrayList<string>();
357         
358                 for (var i = 0; i < data.length; i++) {
359                         var d = data[i].strip();
360                         //print("READ " + d + "\n");
361                         if (
362                                 d.length < 1
363                             ||
364                              Regex.match_simple ("^\\s+$", d)
365                             ||
366                                 Regex.match_simple ("^\\s*/", d)
367                      ){
368                             continue;
369                         }
370                                 
371                         if (Regex.match_simple ("^left:", d)) { 
372                             state = 1;
373                             if (left.size > 0 ){
374                                 cfg.add(new Usage( left, right));
375                                                 }
376                             left = new Gee.ArrayList<string>();
377                                         right = new Gee.ArrayList<string>();
378                             continue;
379                         }
380                          if (Regex.match_simple ("^right:", d)) { 
381                             state = 2;
382                             continue;
383                         }
384                         if (state == 1) {
385                                                 //print("add left: " + d + "\n");
386                             left.add(d);
387                             continue;
388                         }
389                                         //print("add Right: " + d + "\n");
390                         right.add(d);
391                         //Seed.quit();
392                        
393                 }
394                 if (left.size > 0 ){
395                         cfg.add(new Usage( left, right));
396                 }
397                 this.map = cfg;
398
399         }
400         public string validateCode(string code, string language) 
401         {   
402                 if (language == "javascript") {
403                         return "";
404                 }
405                 return "";
406
407         }
408
409             
410           
411         public abstract void fillPack(JsRender.Node node,JsRender.Node parent);
412         public abstract void load();
413         public abstract Gee.HashMap<string,GirObject> getPropertiesFor(string ename, string type);
414         public abstract GirObject? getClass(string ename);
415     }
416
417
418 }
419
420
421