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
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             var gdir = GLib.dir_open(dir,0);
31             
32             while (true) {
33                 
34                 var fn = gdir.read_name ? gdir.read_name () : GLib.dir_read_name(gdir);
35            //      console.log('trying ' +  fn);
36                 if (!fn) {
37                     gdir.close ? gdir.close() : GLib.dir_close(gdir);
38                     return;;
39                 }
40                 if (!fn.match(/.typelib$/)) {
41                     continue;
42                 }
43                 var par = fn.split('-').shift();
44                  //console.log('trying ' +  par);
45                 if (ret.indexOf(par) > -1) {
46                      continue;
47                 }
48                 ret.push(par);
49                 
50                 
51             } 
52         }
53         var gi = GI.Repository.get_default();
54         var pth = GI.Repository.get_search_path ();
55         
56         scanGir(pth[0]);
57         ret.sort();
58         console.dump(ret);
59         return ret;
60         
61     },
62         
63         
64     ns:  function(ns) {
65         var gi = GI.Repository.get_default();
66         ret = {
67             titleType: 'Namespace',
68             ns: ns,
69             name : ns,
70             alias : ns,
71             objects : [],
72             functions : [],
73             enums : [],
74             structs: [],
75             constants : [],
76             unions : [],
77             
78             // so ns looks like class..
79           
80             extendsClasses : [], // what it extends...
81             childClasses : [], // what uses it..
82             properties : [],
83             constructors : [],
84             methods : [],
85             values : [], /// really constants.
86             signals : [],
87             interfaces: [],
88         };
89      
90         for (var i=0; i <  GI.Repository.get_n_infos (gi,ns); i++ ) {
91             var info = GI.Repository.get_info (gi,ns, i);
92             
93             var info_type = GI.base_info_get_type (info);
94             switch(info_type) {
95                 case  GI.InfoType.OBJECT:
96                     ret.objects.push(info.get_name());
97                     this.clsGatherInterfaces(ns , info.get_name());
98                     continue;
99                  case  GI.InfoType.INTERFACE:
100                     ret.interfaces.push(info.get_name());
101                     continue;
102                 case  GI.InfoType.FUNCTION:
103                     new imports.Method.Method(info, ret, 'functions', []);    
104                     continue;
105                 
106                 case  GI.InfoType.CALLBACK:
107                    // new Introspect.Callback(info, ret, 'callbacks', []);
108                     continue;
109                 
110                 case  GI.InfoType.ENUM:
111                 case  GI.InfoType.FLAGS:
112                     ret.enums.push(info.get_name());
113                     continue;
114                 case  GI.InfoType.STRUCT:
115                     if (GI.struct_info_is_gtype_struct (info)) {
116                         continue;
117                     }
118
119                     ret.structs.push(info.get_name());
120                     continue;
121                 case  GI.InfoType.UNION:
122                     ret.unions.push(info.get_name());
123                     continue;
124                 case  GI.InfoType.CONSTANT:
125                     new imports.Constant.Constant(info, ret, 'values', []);
126                     
127                     continue;
128                 
129                 
130                 default:
131                     continue;
132             }
133         }
134         
135                 
136         var gi = GI.Repository.get_default();
137         var ver = gi.get_version(ns);
138         var pth = GI.Repository.get_search_path ();
139         var gir_path = pth[0].replace(/lib\/girepository-1.0/, 'share\/gir-1.0');
140        //console.log(fn);
141         ret.gir_file = gir_path + '/'+ ns + '-' + ver + '.gir';
142         ret.gir_filename = ns + '-' + ver + '.gir';
143         
144         //console.dump(this.ifaceList);
145         return ret;
146
147     },
148  
149     
150   
151     // store all the interfaces, so we can show a list of them later...
152     // called when you list the namespace
153     clsGatherInterfaces : function(ns, cls)
154     {
155        // print("clsGatherInterfaces: " + ns + ", " + cls);
156         var gi = GI.Repository.get_default();
157         var bb = GI.Repository.find_by_name(gi,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 =  prop.get_namespace() +'.' + prop.get_name();
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.Repository.get_default();
201         var ver = GI.Repository.get_version(gi,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