move introspect
[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
7
8 XObject = imports.XObject.XObject;
9
10 console = imports.console.console;
11
12  
13
14 NameSpace = {
15     
16    
17     references : { }, 
18     
19     namespaces : function(ns) 
20     {
21         // this should be a class of it's own...
22         this.references[ns] = []; // technically not needed - but fills in files..
23         // get this from GI... (it's the path..)
24         var ret = [];
25        
26         function scanGir(dir) 
27         {
28             if (!GLib.file_test(dir, GLib.FileTest.EXISTS)) {
29                 return;
30             }
31             var gdir = GLib.dir_open(dir,0);
32             
33             while (true) {
34                 
35                 var fn = gdir.read_name ? gdir.read_name () : GLib.dir_read_name(gdir);
36            //      console.log('trying ' +  fn);
37                 if (!fn) {
38                     gdir.close ? gdir.close() : GLib.dir_close(gdir);
39                     return;;
40                 }
41                 if (!fn.match(/.typelib$/)) {
42                     continue;
43                 }
44                 var par = fn.split('-').shift();
45                  //console.log('trying ' +  par);
46                 if (ret.indexOf(par) > -1) {
47                      continue;
48                 }
49                 ret.push(par);
50                 
51                 
52             } 
53         }
54         var gi = GI.IRepository.get_default();
55         var pth = GI.IRepository.get_search_path ();
56         
57         scanGir(pth[0]);
58         ret.sort();
59         console.dump(ret);
60         return ret;
61         
62     },
63         
64         
65     ns:  function(ns) {
66         var gi = GI.IRepository.get_default();
67         ret = {
68             titleType: 'Namespace',
69             ns: ns,
70             name : ns,
71             alias : ns,
72             objects : [],
73             functions : [],
74             enums : [],
75             structs: [],
76             constants : [],
77             unions : [],
78             
79             // so ns looks like class..
80           
81             extendsClasses : [], // what it extends...
82             childClasses : [], // what uses it..
83             properties : [],
84             constructors : [],
85             methods : [],
86             values : [], /// really constants.
87             signals : [],
88             interfaces: [],
89         };
90      
91         for (var i=0; i <  gi.get_n_infos (ns); i++ ) {
92             var info = gi.get_info (ns, i);
93             
94             var info_type = GI.base_info_get_type (info);
95             switch(info_type) {
96                 case  GI.IInfoType.OBJECT:
97                     ret.objects.push(GI.base_info_get_name(info));
98                     this.clsGatherInterfaces(ns , GI.base_info_get_name(info));
99                     continue;
100                  case  GI.IInfoType.INTERFACE:
101                     ret.interfaces.push(GI.base_info_get_name(info));
102                     continue;
103                 case  GI.IInfoType.FUNCTION:
104                     new imports.Method.Method(info, ret, 'functions', []);    
105                     continue;
106                 
107                 case  GI.IInfoType.CALLBACK:
108                    // new Introspect.Callback(info, ret, 'callbacks', []);
109                     continue;
110                 
111                 case  GI.IInfoType.ENUM:
112                 case  GI.IInfoType.FLAGS:
113                     ret.enums.push(GI.base_info_get_name(info));
114                     continue;
115                 case  GI.IInfoType.STRUCT:
116                     if (GI.struct_info_is_gtype_struct (info)) {
117                         continue;
118                     }
119
120                     ret.structs.push(GI.base_info_get_name(info));
121                     continue;
122                 case  GI.IInfoType.UNION:
123                     ret.unions.push(GI.base_info_get_name(info));
124                     continue;
125                 case  GI.IInfoType.CONSTANT:
126                     new imports.Constant.Constant(info, ret, 'values', []);
127                     
128                     continue;
129                 
130                 
131                 default:
132                     continue;
133             }
134         }
135         
136                 
137         var gi = GI.IRepository.get_default();
138         var ver = gi.get_version(ns);
139         var pth = GI.IRepository.get_search_path ();
140         var gir_path = pth[0].replace(/lib\/girepository-1.0/, 'share\/gir-1.0');
141        //console.log(fn);
142         ret.gir_file = gir_path + '/'+ ns + '-' + ver + '.gir';
143         ret.gir_filename = ns + '-' + ver + '.gir';
144         
145         //console.dump(this.ifaceList);
146         return ret;
147
148     },
149  
150     
151   
152     // store all the interfaces, so we can show a list of them later...
153     // called when you list the namespace
154     clsGatherInterfaces : function(ns, cls)
155     {
156         var gi = GI.IRepository.get_default();
157         var bb = gi.find_by_name(ns, cls);
158         var fullname = ns+'.'+cls;
159         this.ifaceList = this.ifaceList || { };
160          
161         
162         for(var i =0; i < GI.object_info_get_n_interfaces(bb); i++) {
163            
164             var prop = GI.object_info_get_interface(bb,i);
165            
166             var add =  GI.base_info_get_namespace(prop) +'.' + GI.base_info_get_name(prop);
167             this.ifaceList[add] = this.ifaceList[add] || [];
168             if (this.ifaceList[add].indexOf(fullname) < 0) {
169                 this.ifaceList[add].push(fullname);
170             }
171              
172         }
173         
174        
175         
176     },
177     
178            
179         
180    
181     doc : function(what) {
182         var ns = what.split('.').shift();
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.IRepository.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.IRepository.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