1 //<Script type="text/javascript">
4 GIRepository = imports.gi.GIRepository;
6 console = imports.console;
7 XObject = imports.XObject.XObject;
9 Base = imports.Palete.Base.Base;
10 File = imports.File.File;
11 //----------------------- our roo verison
20 // various loader methods..
37 var data = File.read(__script_path__ +'/GtkUsage.txt');
39 data = data.split(/\n/g);
45 data.forEach( function(d) {
46 if (!d.length || d.match(/^\s+$/) || d.match(/^\s*\//)) { //empty
49 if (d.match(/^left:/)) {
62 if (d.match(/^right:/)) {
67 left.push(d.replace(/\s+/g, ''));
70 right.push(d.replace(/\s+/g, ''));
86 commentLoad : function(ns)
89 if (typeof(this.comments[ns]) != 'undefined') {
93 console.log("LOAD DOCS: " + ns);
94 var gi = GIRepository.Repository.get_default();
95 var ver = gi.get_version(ns);
97 this.comments[ns] = {};
102 // no idea why this is broken on my build system.
103 function getAttribute(n, name){
104 var properties = n.properties;
106 if (properties.name == name)
107 return properties.children.content;
108 properties = properties.next
114 function walk(element, path) {
121 var n = getAttribute(element, 'name') ;
122 //console.log("WALK" + n);
123 if (element.name == 'signal') {
127 path += path.length ? '.' : '';
130 if (element.name == 'return-value') {
131 path += '.return-value';
136 var d = getAttribute(element,'doc');
138 // Seed.print(path + ':' + d);
142 var child = element.children;
145 //console.log(child.tag);
146 if (child.type == "element"){
153 var pth = GIRepository.Repository.get_search_path ();
156 var gir_path = pth[0].replace(/lib\/girepository-1.0/, 'share\/gir-1.0');
160 var fn = gir_path + '/'+ ns + '-' + ver + '.gir';
163 if (!File.exists(fn)) {
164 console.log('missing docc file ' + fn);
165 this.comments[ns] = {};
169 var doc = xml.parseFile(fn);
170 //console.log("xmldoc?" + doc);
173 this.comments[ns] = ret;
179 doc : function(what) {
180 var ns = what.split('.').shift();
181 this.commentLoad(ns);
182 return typeof(this.comments[ns][what]) == 'undefined' ? '' : this.comments[ns][what];
185 getPropertiesFor: function(ename, type)
187 print("Loading for " + ename);
189 if (typeof(this.proplist[ename]) != 'undefined') {
190 print("using cache");
191 return this.proplist[ename][type];
193 // use introspection to get lists..
194 var gi = GIRepository.Repository.get_default();
195 var es = ename.split('.');
198 var bi = gi.find_by_name(es[0], es[1]);
201 print("COULND NOT FIND BY NAME");
204 var etype = bi.get_type();;
205 var meth = etype == GIRepository.InfoType.INTERFACE ?
207 'interface_info_get_n_properties',
208 'interface_info_get_property',
209 'interface_info_get_n_signals',
210 'interface_info_get_signal',
211 'interface_info_get_n_methods',
212 'interface_info_get_method'
214 'object_info_get_n_properties',
215 'object_info_get_property',
216 'object_info_get_n_signals',
217 'object_info_get_signal',
218 'object_info_get_n_methods',
219 'object_info_get_method'
223 this.proplist[ename] = {}
224 this.proplist[ename]['props'] = [];
225 this.proplist[ename]['events'] = [];
226 this.proplist[ename]['methods'] = [];
227 this.proplist[ename]['inherits']= [];
229 var plist = this.proplist[ename]['props'] ;
230 var elist = this.proplist[ename]['events'];
231 var mlist = this.proplist[ename]['methods'];
232 var ilist = this.proplist[ename]['inherits'];
240 // properties.. -- and parent ones...!!!
241 for (var i =0;i < GIRepository[meth[0]](bi); i++) {
242 var prop = GIRepository[meth[1]](bi,i);
243 var n_original = prop.get_name();
245 var flags = GIRepository.property_info_get_flags(prop); // check for readonly..
248 var ty = this.typeToName(GIRepository.property_info_get_type(prop));
249 print (n_original +":"+ ty);
254 name : n_original.replace(/\-/g, '_'),
256 desc : this.doc(ename + '.' + n_original),
268 for (var i =0;i < GIRepository[meth[2]](bi); i++) {
269 var prop =GIRepository[meth[3]](bi,i);
270 var n_original = prop.get_name();
271 // print ('signal: ' + n_original);
273 name : n_original.replace(/\-/g, '_'),
274 type : 'function', //???
275 desc : this.doc(ename + '.signal.' + n_original),
276 sig : this.genSkel(prop) // fixme..
282 for (var i =0;i < GIRepository[meth[4]](bi); i++) {
283 var prop = GIRepository[meth[5]](bi,i);
284 var n_original = prop.get_name();
285 print(ename +": ADD : " + n_original );
286 var flags = GIRepository.property_info_get_flags(prop);
287 if (flags & GIRepository.FunctionInfoFlags.IS_CONSTRUCTOR) {
290 if (!(flags & GIRepository.FunctionInfoFlags.IS_METHOD)) {
293 // print ('signal: ' + n_original);
295 name : n_original.replace(/\-/g, '_'),
296 type : 'function', //???
297 desc : this.doc(ename + '.signal.' + n_original)
299 this.genParams(prop,add);
308 if (etype == GIRepository.InfoType.INTERFACE ) {
309 // print("SKIPPING PARENT - it's an interface?!?!");
314 var pi = GIRepository.object_info_get_parent(bi);
319 var pname = pi.get_namespace() + '.' + pi.get_name();
320 this.getPropertiesFor(pname, 'props');
323 elist.push.apply(elist,this.proplist[pname]['events']);
324 plist.push.apply(plist,this.proplist[pname]['props']);
326 ilist.push.apply(ilist,this.proplist[pname]['inherits']);
328 this.overrides(mlist, this.proplist[pname]['methods']);
332 print("NO PARENT FOUND?");
335 // implements needs to be more carefull as it could add dupes..
336 // use the parent implements list to ensure no dupes..
337 for(var i =0; i < GIRepository.object_info_get_n_interfaces(bi); i++) {
339 var prop = GIRepository.object_info_get_interface(bi,i);
340 var iface = prop.get_namespace() +'.'+ prop.get_name();
341 if ( ilist.indexOf(iface) > -1) {
344 this.getPropertiesFor(iface, 'props'); // just load one of them..
347 elist.push.apply(elist,this.proplist[iface]['events']);
348 plist.push.apply(plist,this.proplist[iface]['props']);
349 this.overrides(mlist, this.proplist[pname]['methods']);
351 function sfunc(a,b) {
352 if (a.name == b.name) return 0;
353 return a.name > b.name ? 1 : -1
359 return this.proplist[ename][type];
362 genParams: function(sig, meth)
366 meth.ret_type = this.typeToName(GIRepository.callable_info_get_return_type(sig));
367 // might be a numbeR??
369 for(var a_i =0; a_i < GIRepository.callable_info_get_n_args(sig); a_i++) {
370 var arg = GIRepository.callable_info_get_arg(sig, a_i);
373 name : arg.get_name(),
374 type : this.typeToName(arg.get_type(), true)
382 genSkel: function(sig) // should really use genParams...
386 var ret_type = this.typeToName(GIRepository.callable_info_get_return_type(sig));
387 // might be a numbeR??
388 if (ret_type == 'boolean') {
389 ret = " return false;\n";
391 for(var a_i =0; a_i < GIRepository.callable_info_get_n_args(sig); a_i++) {
392 var arg = GIRepository.callable_info_get_arg(sig, a_i);
394 args.push(arg.get_name());
396 return 'function (' + args.join(', ') + ") {\n" + ret + "}";
401 typeToName : function (type_info, allow_iface) // find type for properties or arguments.
403 var ty = GIRepository.type_tag_to_string( GIRepository.type_info_get_tag(type_info));
405 if ((ty == 'void') && GIRepository.type_info_is_pointer(type_info)) {
409 return false; // unspported
411 if (ty != 'interface') {
414 // we can accept enum types here..
415 var interface_info = GIRepository.type_info_get_interface(type_info);
416 var interface_type = interface_info.get_type();
418 if (!allow_iface && interface_type != GIRepository.InfoType.ENUM) {
421 return interface_info.get_namespace() + '.' + interface_info.get_name();
425 * merge two proprety arrays' ignoring what's overrriden.
430 overrides : function (top, bottom)
434 return !top.every(function(t) {
435 if (t.name == b.name) {
441 bottom.forEach(function(e) {
453 findType : function (data, prop, value)
456 if ((prop == '|xns') || (prop == 'xtype')) {
460 var qname = this.guessName(data);
461 if (prop[0] == '|') {
462 prop= prop.substring(1);
465 var prs = this.getPropertiesFor(qname, 'props');
467 prs.forEach(function(e) {
471 if (e.name == prop) {
477 return Base.prototype.findType(data, prop,value);
484 findOptions : function(ename)
487 var es = ename.split('.');
489 return Base.prototype.findOptions(ename);
491 var gi = GIRepository.Repository.get_default();
492 var bi = gi.find_by_name(es[0], es[1]);
493 var etype = GIRepository.object_info_get_type(bi);
494 if (etype != GIRepository.InfoType.ENUM) {
495 console.log("Options not handled yet!!!");
499 // got an enum.. let's return the values..
500 for(var i =0; i < bi.get_n_values(); i++) {
502 var prop = bi.get_value(i);
505 ret.push( ename + '.' + prop.get_name().toUpperCase() )
510 * determine the packing types..
512 getDefaultPack: function(pname, cname) {
513 var list = this.getPackingList(pname,cname);
514 // console.dump(list);
517 if (!list.every(function(e) { return e.name != 'add'; })) {
518 return 'add'; // add is in our list..?? what about args..?!?
522 e.params.forEach(function(p,i) {
523 if (ret === false) { return; } // skip broken..
524 if (i==0) { return; } // skip first..
525 if (p.type == 'boolean') {
529 if (p.type == 'number') {
533 if (p.type == 'uint') {
537 ret = false; // invalid!
539 return ret === false ? false : ret.join(',');
542 list.every(function(e) {
545 //print("DEFAULT PACK TEST : " + e.name + " : " +packret);
546 if (packret !== false) {
549 return true; // continue
551 //print("DEFAULT PACK: " + packret);
552 // should we do best match..?
558 getPackingList :function (pname, cname)
560 var funcs = this.getPropertiesFor(pname,'methods');
561 //print("getPackingList : ALL FUNCS");
562 //console.dump(funcs);
565 // let's assume top down...
566 var inherits = [ cname ];
567 inherits.push.apply(inherits, this.getPropertiesFor(cname,'inherits'));
568 funcs.forEach(function(m) {
569 if (m.params.length && (typeof(m.params[0].type) == 'string') &&
570 inherits.indexOf(m.params[0].type) > -1) {