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