Builder/Provider/Palete/Gtk.js
[app.Builder.js] / Builder / Provider / Palete / Gtk.js
1 //<Script type="text/javascript">
2  
3 Gio = imports.gi.Gio;
4 GIRepository  = imports.gi.GIRepository;
5 xml     = imports.libxml;
6 console = imports.console;
7 XObject = imports.XObject.XObject;
8
9 Base = imports.Builder.Provider.Palete.Base.Base;
10 File = imports.File.File;
11 //----------------------- our roo verison
12
13
14
15
16 Gtk = XObject.define(
17     function(cfg) {
18         
19        
20         // various loader methods..
21           this.map = [];
22         this.load();
23         this.proplist = {};
24         this.comments = { }; 
25         // no parent...
26         
27        
28     },
29     Base,
30     {
31         load: function () {
32                 
33          
34         
35             var data = File.read(__script_path__ +'/../GtkUsage.txt');
36           // print(data);
37             data  = data.split(/\n/g);
38             var state = 0;
39             var cfg = [];
40             var left = [];
41             var right = [];
42             
43             data.forEach( function(d) {
44                 if (!d.length || d.match(/^\s+$/) || d.match(/^\s*\//)) { //empty
45                     return;
46                 }
47                 if (d.match(/^left:/)) { 
48                     state = 1;
49                     if (left.length ){
50                         
51                         cfg.push({
52                             left : left,
53                             right: right
54                         });
55                         }
56                     left = [];
57                     right = [];
58                     return;
59                 }
60                 if (d.match(/^right:/)) { 
61                     state = 2;
62                     return;
63                 }
64                 if (state == 1) {
65                     left.push(d.replace(/\s+/g, ''));
66                     return;
67                 }
68                 right.push(d.replace(/\s+/g, ''));
69                 //Seed.quit();
70                
71             }); 
72             if (left.length ){
73                         
74                 cfg.push({
75                     left : left,
76                     right: right
77                 });
78             }
79             this.map = cfg;
80              
81         },
82         
83         
84         commentLoad : function(ns)
85         {
86              
87             if (typeof(this.comments[ns]) != 'undefined') {
88                 return;
89             }
90
91             console.log("LOAD DOCS: " + ns);
92             var gi = GIRepository.IRepository.get_default();
93             var ver = gi.get_version(ns);
94             if (!ver) {
95                 this.comments[ns] = {};
96                 return;
97             }
98             var ret = { };
99
100             // no idea why this is broken on my build system.
101             function getAttribute(n, name){
102                 var properties = n.properties;
103                 while (properties){
104                     if (properties.name == name)
105                          return properties.children.content;
106                      properties = properties.next
107                 }
108                 return null;
109             }
110                  
111          
112             function walk(element, path) {
113                  
114                  
115                 if (!element) {
116                     return;
117                 }
118                 
119                 var n =  getAttribute(element, 'name') ;
120                 //console.log("WALK" + n);
121                 if (element.name == 'signal') {
122                     path += '.signal';
123                 } 
124                 if (n) {
125                     path += path.length ? '.' : '';
126                     path += n;
127                 }
128                 if (element.name == 'return-value') {
129                     path += '.return-value';
130                 }
131                 
132                 
133              
134                 var d =   getAttribute(element,'doc');
135                 if (d) {
136                  //   Seed.print(path + ':' + d);
137                     ret[path] = d;
138                 }
139                 
140                 var child = element.children;
141
142                 while (child){
143                     //console.log(child.tag);
144                     if (child.type == "element"){
145                         walk (child, path);
146                     }
147                     child = child.next;
148                 }
149             }
150             
151             var pth = GIRepository.IRepository.get_search_path ();
152             
153             
154             var gir_path = pth[0].replace(/lib\/girepository-1.0/, 'share\/gir-1.0');
155            
156             
157             //console.log(fn);
158             var  fn = gir_path + '/'+ ns + '-' + ver + '.gir';
159            // console.log(fn);
160             
161             if (!File.exists(fn)) {
162                 console.log('missing docc file ' + fn);
163                 this.comments[ns] = {};
164                 
165                 return;
166             }
167             var doc = xml.parseFile(fn);
168             //console.log("xmldoc?" + doc);
169             walk (doc.root, '');
170             //console.dump(ret);
171             this.comments[ns] = ret;
172             //console.dump(ret);
173
174         },
175        
176         
177         doc : function(what) {
178             var ns = what.split('.').shift();
179             this.commentLoad(ns);
180             return typeof(this.comments[ns][what]) == 'undefined' ?  '' : this.comments[ns][what];
181         },
182   
183         getPropertiesFor: function(ename, type)
184         {
185             print("Loading for " + ename);
186             
187             if (typeof(this.proplist[ename]) != 'undefined') {
188                     return this.proplist[ename][type];
189             }
190             // use introspection to get lists..
191             var gi = GIRepository.IRepository.get_default();
192             var es = ename.split('.');
193             var bi = gi.find_by_name(es[0], es[1]);
194             
195             if (!bi) {
196                 print("COULND NOT FIND BY NAME");
197                 return [];
198             }
199             var etype = GIRepository.base_info_get_type(bi);
200             var meth = etype == GIRepository.IInfoType.INTERFACE ?
201                 [ 
202                     'interface_info_get_n_properties',
203                     'interface_info_get_property',
204                     'interface_info_get_n_signals',
205                     'interface_info_get_signal'
206                 ] : [ 
207                     'object_info_get_n_properties',
208                     'object_info_get_property',
209                     'object_info_get_n_signals',
210                     'object_info_get_signal'
211                 ]; 
212             
213             
214             this.proplist[ename] = {}
215             this.proplist[ename]['props'] = [];
216             this.proplist[ename]['events'] = [];
217             this.proplist[ename]['inherits']= [];
218             var plist = this.proplist[ename]['props'] ;
219             var elist = this.proplist[ename]['events'];
220             var ilist = this.proplist[ename]['inherits'];
221              /*
222              we need...
223              p.name
224             p.type
225             p.desc
226             p.sig */
227             
228             // properties.. -- and parent ones...!!!
229             for (var i =0;i <  GIRepository[meth[0]](bi); i++) {
230                 var prop = GIRepository[meth[1]](bi, i);  
231                 var n_original =  GIRepository.base_info_get_name(prop);
232                 
233                 var flags =  GIRepository.property_info_get_flags(prop); // check for readonly..
234                 
235                 
236                 var ty = this.typeToName(GIRepository.property_info_get_type(prop));
237                 print (n_original +":"+ ty);
238                 if (ty === false) {
239                     continue;
240                 }
241                 var add = {
242                      name :  n_original.replace(/\-/g, '_'),
243                      type :  ty,
244                      desc : this.doc(ename + '.' + n_original),
245                      sig : ''
246                 }
247                 plist.push(add)
248             }
249            
250             // signals..
251             
252             for (var i =0;i <  GIRepository[meth[2]](bi); i++) {
253                 var prop = GIRepository[meth[3]](bi, i);  
254                 var n_original =  GIRepository.base_info_get_name(prop);
255                 // print ('signal: ' + n_original); 
256                 var add = {
257                     name :  n_original.replace(/\-/g, '_'),
258                     type : 'function', //???
259                     desc : this.doc(ename + '.signal.' + n_original),
260                     sig  : this.genSkel(prop) // fixme..
261                 }
262                 elist.push(add);
263             }
264             
265             if (etype == GIRepository.IInfoType.INTERFACE ) {
266                   return;
267             }
268             
269             // parent!!?!!?
270             var pi = GIRepository.object_info_get_parent(bi);
271             
272             if (pi) {
273                 
274                    
275                 var pname = GIRepository.base_info_get_namespace(pi) + '.' +
276                     GIRepository.base_info_get_name(pi);
277                 this.getPropertiesFor(pname, 'props');
278             
279                 elist.push.apply(elist,this.proplist[pname]['events']);
280                 plist.push.apply(plist,this.proplist[pname]['props']);
281                 ilist.push.apply(ilist,this.proplist[pname]['inherits']);
282             }
283             
284             // implements needs to be more carefull as it could add dupes..
285             // use the parent implements list to ensure no dupes..
286             for(var i =0; i < GIRepository.object_info_get_n_interfaces(bi); i++) {
287                  
288                 var prop = GIRepository.object_info_get_interface(bi,i);
289                 var iface = GIRepository.base_info_get_namespace(prop) +'.'+ 
290                     GIRepository.base_info_get_name(prop);
291                 if ( ilist.indexOf(iface) > -1) {
292                     continue;
293                 }
294                 this.getPropertiesFor(iface, 'props');
295                 ilist.push(iface);
296                 elist.push.apply(elist,this.proplist[iface]['events']);
297                 plist.push.apply(plist,this.proplist[iface]['props']);
298             }
299             
300             return this.proplist[ename][type];
301             
302         },
303         
304         genSkel: function(sig)
305         {
306             var args = ['self'];
307             var ret = "\n";
308             var ret_type = this.typeToName(GIRepository.callable_info_get_return_type(sig));
309             // might be a numbeR??
310             if (ret_type == 'boolean') {
311                 ret = "    return false;\n";
312             }
313             for(var a_i  =0; a_i   < GIRepository.callable_info_get_n_args(sig); a_i++) {
314                 var arg = GIRepository.callable_info_get_arg(sig, a_i);
315                 
316                 args.push(GIRepository.base_info_get_name(arg));
317             }
318             return 'function (' + args.join(', ') + ") {\n" + ret + "}"; 
319                 
320             
321             
322         },
323         typeToName  : function (type_info) // find type for properties or arguments.
324         {
325            var ty = GIRepository.type_tag_to_string( GIRepository.type_info_get_tag(type_info));
326            
327             if ((ty == 'void') && GIRepository.type_info_is_pointer(type_info)) {
328                 return false;
329             }
330             if (ty == 'array') {
331                 return false; // unspported   
332             }
333             if (ty != 'interface') {
334                 
335                 return ty;
336             }
337             // we can accept enum types here..
338             var interface_info = GIRepository.type_info_get_interface (type_info);        
339             var interface_type = GIRepository.base_info_get_type (interface_info);
340             if (interface_type != GIRepository.IInfoType.ENUM) {
341                 return false;
342             }
343             return GIRepository.base_info_get_namespace(interface_info) + '.' +
344                     GIRepository.base_info_get_name(interface_info);
345             
346         }
347         /**
348          * guess type..
349          * 
350          */
351         findType : function (data, prop, value)
352         {
353             // specials??
354             if ((prop == '|xns') || (prop == 'xtype'))  {
355                 return 'string';
356             }
357             
358             var qname = this.guessName(data);
359             var prs = this.getPropertiesFor(qname, 'props');
360             var ret = false;
361             prs.forEach(function(e) {
362                 if (ret !== false) {
363                     return; // got it.
364                 }
365                 if (e.name == prop) {
366                     ret = e;
367                 }
368                 
369             });
370             if (!ret) {
371                 return Base.prototype.findType(data, prop,value);
372             }
373              
374             // got the type
375             return e.type;
376         }
377         
378     }
379 );
380