Introspect/NameSpace.js
[gnome.introspection-doc-generator] / Introspect / NameSpace.js
1 //<script type="text/javascript">
2 //Gtk = imports.gi.Gtk;
3 GI      = imports.gi.GIRepository;
4 GLib    = imports.gi.GLib;
5 xml     = imports.libxml;
6 File    = imports.File.File;
7
8 XObject = imports.XObject.XObject;
9
10 console = imports.console.console;
11
12  
13
14 NameSpace = {
15    
16     references : { }, 
17     
18     namespaces : function(ns) 
19     {
20         // this should be a class of it's own...
21         this.references[ns] = []; // technically not needed - but fills in files..
22         // get this from GI... (it's the path..)
23         var ret = [];
24        
25         function scanGir(dir) 
26         {
27             if (!GLib.file_test(dir, GLib.FileTest.EXISTS)) {
28                 return;
29             }
30             File.list(dir).forEach(function(fn) 
31             {
32                 if (!fn.match(/\.typelib$/)) {
33                     return;
34                 }
35                 var par = fn.split('-').shift();
36                  //console.log('trying ' +  par);
37                 if (ret.indexOf(par) > -1) {
38                      return;
39                 }
40                 ret.push(par);
41             }); 
42         }
43         
44         var gi = GI.Repository.get_default();
45         var pth = GI.Repository.get_search_path();
46         
47         scanGir(pth[0]);
48         ret.sort();
49         console.dump(ret);
50         return ret;
51         
52     },
53         
54         
55     ns:  function(ns) {
56         var gi = GI.Repository.get_default();
57         ret = {
58             titleType: 'Namespace',
59             ns: ns,
60             name : ns,
61             alias : ns,
62             objects : [],
63             functions : [],
64             enums : [],
65             structs: [],
66             constants : [],
67             unions : [],
68             
69             // so ns looks like class..
70           
71             extendsClasses : [], // what it extends...
72             childClasses : [], // what uses it..
73             properties : [],
74             constructors : [],
75             methods : [],
76             values : [], /// really constants.
77             signals : [],
78             interfaces: [],
79         };
80      
81         for (var i=0; i <  gi.get_n_infos (ns); i++ ) {
82             var info = gi.get_info (ns, i);
83            // print("NAME: " + info.get_name());
84             //continue;
85             var info_type = GI.base_info_get_type (info);
86            // print("Type: " + info_type);
87             switch(info_type) {
88                 case  GI.InfoType.OBJECT:
89                     ret.objects.push(info.get_name());
90                     this.clsGatherInterfaces(ns , info.get_name());
91                     continue;
92                  case  GI.InfoType.INTERFACE:
93                     ret.interfaces.push(info.get_name());
94                     continue;
95                 case  GI.InfoType.FUNCTION:
96                     new imports.Method.Method(info, ret, 'functions', []);    
97                     continue;
98                 
99                 case  GI.InfoType.CALLBACK:
100                    // new Introspect.Callback(info, ret, 'callbacks', []);
101                     continue;
102                 
103                 case  GI.InfoType.ENUM:
104                 case  GI.InfoType.FLAGS:
105                     ret.enums.push(info.get_name());
106                     continue;
107                 case  GI.InfoType.STRUCT:
108                     if (GI.struct_info_is_gtype_struct (info)) {
109                         continue;
110                     }
111
112                     ret.structs.push(info.get_name());
113                     continue;
114                 case  GI.InfoType.UNION:
115                     ret.unions.push(info.get_name());
116                     continue;
117                 case  GI.InfoType.CONSTANT:
118                     new imports.Constant.Constant(info, ret, 'values', []);
119                     
120                     continue;
121                 
122                 
123                 default:
124                     continue;
125             }
126         }
127         print ("SCAN NAMESPACE ALL DONE");
128                 
129         var gi = GI.Repository.get_default();
130         var ver = GI.Repository.get_version(gi,ns);
131         var pth = GI.Repository.get_search_path ();
132         var gir_path = pth[0].replace(/lib\/girepository-1.0/, 'share\/gir-1.0');
133        //console.log(fn);
134         ret.gir_file = gir_path + '/'+ ns + '-' + ver + '.gir';
135         ret.gir_filename = ns + '-' + ver + '.gir';
136         
137         //console.dump(this.ifaceList);
138         return ret;
139
140     },
141  
142     
143
144     // store all the interfaces, so we can show a list of them later...
145     // called when you list the namespace
146     clsGatherInterfaces : function(ns, cls)
147     {
148        // print("clsGatherInterfaces: " + ns + ", " + cls);
149         var gi = GI.Repository.get_default();
150         var bb = gi.find_by_name(ns, cls);
151         var fullname = ns+'.'+cls;
152         this.ifaceList = this.ifaceList || { };
153          
154         
155         for(var i =0; i < GI.object_info_get_n_interfaces(bb); i++) {
156            
157             var prop = GI.object_info_get_interface(bb,i);
158            
159             var add =  prop.get_namespace() +'.' + prop.get_name();
160             this.ifaceList[add] = this.ifaceList[add] || [];
161             if (this.ifaceList[add].indexOf(fullname) < 0) {
162                 this.ifaceList[add].push(fullname);
163             }
164              
165         }
166         
167        
168         
169     },
170     
171            
172         
173    
174     doc : function(what) {
175         print ("DOC: + " +what);
176         var ns = what.split('.').shift();
177         return '';
178         this.commentLoad(ns);
179         return typeof(this.comments[ns][what]) == 'undefined' ?  '' : this.comments[ns][what];
180         
181     },
182     
183     
184     
185     comments : {},
186     
187     commentLoad : function(ns)
188     {
189         
190         if (typeof(this.comments[ns]) != 'undefined') {
191             return;
192         }
193         
194         console.log("LOAD DOCS: " + ns);
195         var gi = GI.Repository.get_default();
196         var ver = gi.get_version(ns);
197         if (!ver) {
198             this.comments[ns] = {};
199             return;
200         }
201         var ret = { };
202         
203         // no idea why this is broken on my build system.
204         var  getAttribute = function(n, name){
205             var properties = n.properties;
206             while (properties){
207                 if (properties.name == name)
208                     return properties.children.content;
209                 properties = properties.next
210             }
211             return null;
212         }
213                 
214         
215         function walk (element, path) {
216             
217             
218             if (!element) {
219                 return;
220             }
221             
222             var n =  getAttribute(element, 'name') ;
223             //console.log("WALK" + n);
224             if (element.name == 'signal') {
225                 path += '.signal';
226             }
227             
228             if (n) {
229                 path += path.length ? '.' : '';
230                 path += n;
231             }
232             if (element.name == 'return-value') {
233                 path += '.return-value';
234             }
235             
236             var d =   getAttribute(element,'doc');
237             if (d) {
238              //   Seed.print(path + ':' + d);
239                 ret[path] = d;
240             }
241             
242             var child = element.children;
243
244             while (child){
245                 //console.log(child.tag);
246                 if (child.type == "element"){
247                     walk (child, path);
248                 }
249                 child = child.next;
250             }
251         }
252         
253         var pth = GI.Repository.get_search_path ();
254         
255         
256         var gir_path = pth[0].replace(/lib\/girepository-1.0/, 'share\/gir-1.0');
257        
258         
259         //console.log(fn);
260         var  fn = gir_path + '/'+ ns + '-' + ver + '.gir';
261        // console.log(fn);
262         
263         if (!GLib.file_test(fn, GLib.FileTest.EXISTS)) {
264             console.log('missing docc file ' + fn);
265             this.comments[ns] = {};
266             
267             return;
268         }
269         var doc = xml.parseFile(fn);
270         //console.log("xmldoc?" + doc);
271         walk (doc.root, '');
272         //console.dump(ret);
273         this.comments[ns] = ret;
274
275     },
276     registry : { },
277     factory : function(type, ns, name) {
278         if (typeof (this.registry[ns +'.' + name]) == 'undefined') {
279             this.registry[ns +'.' + name] = new imports[type][type](ns,name);
280             this.registry[ns +'.' + name].load();
281         }
282         
283         return this.registry[ns +'.' + name];
284     }
285         
286 };
287
288
289