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            function typeToName (type_info) {
228                var ty = GIRepository.type_tag_to_string( GIRepository.type_info_get_tag(type_info));
229                
230                 if ((ty == 'void') && GIRepository.type_info_is_pointer(type_info)) {
231                     return false;
232                 }
233                 if (ty == 'array') {
234                     return false; // unspported   
235                 }
236                 if (ty != 'interface') {
237                     
238                     return ty;
239                 }
240                 // we can accept enum types here..
241                 var interface_info = GIRepository.type_info_get_interface (type_info);        
242                 var interface_type = GIRepository.base_info_get_type (interface_info);
243                 if (interface_type != GIRepository.IInfoType.ENUM) {
244                     return false;
245                 }
246                 return GIRepository.base_info_get_namespace(interface_info) + '.' +
247                         GIRepository.base_info_get_name(interface_info);
248                 
249             }
250             // properties.. -- and parent ones...!!!
251             for (var i =0;i <  GIRepository[meth[0]](bi); i++) {
252                 var prop = GIRepository[meth[1]](bi, i);  
253                 var n_original =  GIRepository.base_info_get_name(prop);
254                 
255                 var flags =  GIRepository.property_info_get_flags(prop); // check for readonly..
256                 
257                 
258                 var ty = typeToName(GIRepository.property_info_get_type(prop));
259                 print (n_original +":"+ ty);
260                 if (ty === false) {
261                     continue;
262                 }
263                 var add = {
264                      name :  n_original.replace(/\-/g, '_'),
265                      type :  ty,
266                      desc : this.doc(ename + '.' + n_original),
267                      sig : ''
268                 }
269                 plist.push(add)
270             }
271            
272             // signals..
273             
274             for (var i =0;i <  GIRepository[meth[2]](bi); i++) {
275                 var prop = GIRepository[meth[3]](bi, i);  
276                 var n_original =  GIRepository.base_info_get_name(prop);
277                  print ('signal: ' + n_original); 
278                 var add = {
279                     name :  n_original.replace(/\-/g, '_'),
280                     type : 'function', //???
281                     desc : this.doc(ename + '.signal.' + n_original),
282                     sig  : '' // fixme..
283                 }
284                 elist.push(add);
285             }
286             
287             if (etype == GIRepository.IInfoType.INTERFACE ) {
288                   return;
289             }
290             
291             // parent!!?!!?
292             var pi = GIRepository.object_info_get_parent(bi);
293             
294             if (pi) {
295                 
296                    
297                 var pname = GIRepository.base_info_get_namespace(pi) + '.' +
298                     GIRepository.base_info_get_name(pi);
299                 this.getPropertiesFor(pname, 'props');
300             
301                 elist.push.apply(elist,this.proplist[pname]['events']);
302                 plist.push.apply(plist,this.proplist[pname]['props']);
303                 ilist.push.apply(ilist,this.proplist[pname]['inherits']);
304             }
305             
306             // implements needs to be more carefull as it could add dupes..
307             // use the parent implements list to ensure no dupes..
308             for(var i =0; i < GIRepository.object_info_get_n_interfaces(bi); i++) {
309                  
310                 var prop = GIRepository.object_info_get_interface(bi,i);
311                 var iface = GIRepository.base_info_get_namespace(prop) +'.'+ 
312                     GIRepository.base_info_get_name(prop);
313                 if ( ilist.indexOf(iface) > -1) {
314                     continue;
315                 }
316                 this.getPropertiesFor(iface, 'props');
317                 ilist.push(iface);
318                 elist.push.apply(elist,this.proplist[iface]['events']);
319                 plist.push.apply(plist,this.proplist[iface]['props']);
320             }
321             
322             return this.proplist[ename][type];
323             
324         }
325         
326         
327         
328     }
329 );
330