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