Test.GtkWriter.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
12     static Gee.HashMap<string,Palete>? cache = null;
13     
14         public static Palete factory(string xtype)
15     {
16         if (cache == null) {
17             cache = new Gee.HashMap<string,Palete>();
18         }
19         if (cache.get(xtype) != null) {
20             return cache.get(xtype);
21         }
22         switch(xtype) {
23             case "Gtk":
24                 cache.set(xtype, new Gtk());
25                 break;
26             //case "Roo":
27             //    cache.set(xtype, new Roo());
28                 break;
29             default:
30                 throw new Error.INVALID_TYPE("invalid argument to Palete factory");
31         }
32         return cache.get(xtype);
33     }
34        
35
36     public class Palete : Object 
37     {
38         
39        
40         public string name;
41         
42         public Palete()
43         {
44             // nothing?
45         }
46         
47         
48         //map : false, // array of mappings   left: [] , right : []
49         
50         //items : false, // the tree of nodes.
51         
52         
53
54
55         string  guessName(JsRender.Node ar) throws Error // turns the object into full name.
56         {
57             throw new Error.NEED_IMPLEMENTING("xxx. guessName needs implimenting");
58         }
59             
60
61             
62         /**
63          * gather a  list of potentional objects that can be added..
64          * 
65          */
66         /*
67         gatherList: function (existing) {
68             existing = existing || [];
69            // existing.push('*top'); // always have top
70             var ret  = []; 
71             console.log("GATHER LIST? " + this.map.length);
72             
73             
74             function addRight(right) {
75                 right.forEach(function(r) {
76                     if (ret.indexOf(r) > -1) {
77                         return;
78                     }
79                     ret.push(r);
80                 });
81             }
82             
83             this.map.forEach(function(m) {
84                 var done = false
85                 m.left.forEach( function(left) {
86                     if (done) return; 
87                     
88                     var l = left.replace(/:.*$/, '');
89                    // print("chk:" + l + " in " + existing.join(',')); 
90                     if (existing.indexOf(l) > -1) {
91                         addRight(m.right);
92                         done =true;
93                         //return true; // no more needed..
94                     }
95                 });
96                 
97             });
98             ret.sort();
99             
100            // console.dump(ret);
101             return ret;
102             
103             
104             
105         },
106         
107         getDropList : function(rval)
108         {
109             
110             var ret = [];
111             this.map.forEach( function(m) {
112                 if (m.right.indexOf(rval) > -1) {
113                     m.left.forEach(function(l) {
114                         if (ret.indexOf(l) > -1) {
115                             return;
116                         }
117                         ret.push(l)
118                     });
119                 }
120                 
121             });
122             console.log("DROP LIST:");
123             console.dump(ret);
124             return ret;
125             
126         },
127         /**
128          * basic guess type.. 
129          * 
130          * /
131         findType : function (data, prop, value)
132         {
133             if (prop[0] == '|') {
134                 return 'function';
135             }
136             return typeof(value);
137         },
138         
139         
140         findOptions : function(ename)
141         {
142             switch(ename.toLowerCase()) {
143                 case 'boolean': 
144                     return [ 'true', 'false' ];
145                 // everything else does not have options.
146                 case 'string': 
147                 case 'utf8': 
148                 case 'int': 
149                 case 'uint': 
150                 case 'function': 
151                     return false;
152                 default: 
153                     console.log("OOPS: = unknown type: " + ename);
154                     return false;
155             }
156         },
157         confirmCanAdd: function(parent, child) {
158             // confirms that one obj can be added to another.
159             // returns true, for items, or list of properties that can hold it..
160             return true;
161             
162         },
163         getDefaultPack: function(pname, cname) {
164             return 'add';
165         },
166         */
167         public void saveTemplate (string name, JsRender.Node data)
168         {
169
170                         var gn = data.fqn();
171             // store it in user's directory..
172             var appdir =  GLib.Environment.get_home_dir() + "/.Builder"; 
173
174                         
175             if (!GLib.FileUtils.test(appdir+ "/" + gn, GLib.FileTest.IS_DIR)) {
176                                 GLib.File.new_for_path (appdir+ "/" + gn).make_directory ();
177                                 
178             }
179             GLib.FileUtils.set_contents(appdir+ "/" + gn + "/" +  name + ".json", data.toJsonString());
180             
181         }
182         
183         /**
184          * list templates - in home directory (and app dir in future...)
185          * @param {String} name  - eg. Gtk.Window..
186          * @return {Array} list of templates available..
187          */
188           
189         public  GLib.List<string> listTemplates (JsRender.Node node)
190         {
191             
192             var gn = node.fqn();
193             
194             var ret = new GLib.List<string>();
195             var dir= GLib.Environment.get_home_dir() + "/.Builder/" + gn;
196                         if (!GLib.FileUtils.test(dir, GLib.FileTest.IS_DIR)) {
197                         return ret;
198                         }
199                         
200
201
202                                     
203                         var f = File.new_for_path(dir);
204         
205             var file_enum = f.enumerate_children(GLib.FileAttribute.STANDARD_DISPLAY_NAME, GLib.FileQueryInfoFlags.NONE, null);
206              
207             FileInfo next_file; 
208             while ((next_file = file_enum.next_file(null)) != null) {
209                 var n = next_file.get_display_name();
210                         if (!Regex.match_simple ("\\.json$", n)) {
211                                         continue;
212                                 }
213                                 ret.append( dir + "/" + n);
214             }
215             return ret;
216             
217         }
218  
219         public JsRender.Node? loadTemplate(string path)
220         {
221
222                     var pa = new Json.Parser();
223             pa.load_from_file(path);
224             var node = pa.get_root();
225             
226             if (node.get_node_type () != Json.NodeType.OBJECT) {
227                         return null;
228                 }
229             var obj = node.get_object ();
230              
231             var ret = new JsRender.Node();
232             ret.loadFromJson(obj);
233             return ret;
234         }
235             
236         
237     }
238
239 }
240
241
242