Introspect/Class.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            // print("NAME: " + info.get_name());
93             //continue;
94             var info_type = GI.base_info_get_type (info);
95            // print("Type: " + info_type);
96             switch(info_type) {
97                 case  GI.InfoType.OBJECT:
98                     ret.objects.push(info.get_name());
99                     this.clsGatherInterfaces(ns , info.get_name());
100                     continue;
101                  case  GI.InfoType.INTERFACE:
102                     ret.interfaces.push(info.get_name());
103                     continue;
104                 case  GI.InfoType.FUNCTION:
105                     new imports.Method.Method(info, ret, 'functions', []);    
106                     continue;
107                 
108                 case  GI.InfoType.CALLBACK:
109                    // new Introspect.Callback(info, ret, 'callbacks', []);
110                     continue;
111                 
112                 case  GI.InfoType.ENUM:
113                 case  GI.InfoType.FLAGS:
114                     ret.enums.push(info.get_name());
115                     continue;
116                 case  GI.InfoType.STRUCT:
117                     if (GI.struct_info_is_gtype_struct (info)) {
118                         continue;
119                     }
120
121                     ret.structs.push(info.get_name());
122                     continue;
123                 case  GI.InfoType.UNION:
124                     ret.unions.push(info.get_name());
125                     continue;
126                 case  GI.InfoType.CONSTANT:
127                     new imports.Constant.Constant(info, ret, 'values', []);
128                     
129                     continue;
130                 
131                 
132                 default:
133                     continue;
134             }
135         }
136         print ("SCAN NAMESPACE ALL DONE");
137                 
138         var gi = GI.Repository.get_default();
139         var ver = GI.Repository.get_version(gi,ns);
140         var pth = GI.Repository.get_search_path ();
141         var gir_path = pth[0].replace(/lib\/girepository-1.0/, 'share\/gir-1.0');
142        //console.log(fn);
143         ret.gir_file = gir_path + '/'+ ns + '-' + ver + '.gir';
144         ret.gir_filename = ns + '-' + ver + '.gir';
145         
146         //console.dump(this.ifaceList);
147         return ret;
148
149     },
150  
151     
152   
153     // store all the interfaces, so we can show a list of them later...
154     // called when you list the namespace
155     clsGatherInterfaces : function(ns, cls)
156     {
157        // print("clsGatherInterfaces: " + ns + ", " + cls);
158         var gi = GI.Repository.get_default();
159         var bb = GI.Repository.find_by_name(gi,ns, cls);
160         var fullname = ns+'.'+cls;
161         this.ifaceList = this.ifaceList || { };
162          
163         
164         for(var i =0; i < GI.object_info_get_n_interfaces(bb); i++) {
165            
166             var prop = GI.object_info_get_interface(bb,i);
167            
168             var add =  prop.get_namespace() +'.' + prop.get_name();
169             this.ifaceList[add] = this.ifaceList[add] || [];
170             if (this.ifaceList[add].indexOf(fullname) < 0) {
171                 this.ifaceList[add].push(fullname);
172             }
173              
174         }
175         
176        
177         
178     },
179     
180            
181         
182    
183     doc : function(what) {
184         print ("DOC: + " +what);
185         var ns = what.split('.').shift();
186         return '';
187         this.commentLoad(ns);
188         return typeof(this.comments[ns][what]) == 'undefined' ?  '' : this.comments[ns][what];
189         
190     },
191     
192     
193     
194     comments : {},
195     
196     commentLoad : function(ns)
197     {
198         
199         if (typeof(this.comments[ns]) != 'undefined') {
200             return;
201         }
202         
203         console.log("LOAD DOCS: " + ns);
204         var gi = GI.Repository.get_default();
205         var ver = GI.Repository.get_version(gi,ns);
206         if (!ver) {
207             this.comments[ns] = {};
208             return;
209         }
210         var ret = { };
211         
212         // no idea why this is broken on my build system.
213         var  getAttribute = function(n, name){
214             var properties = n.properties;
215             while (properties){
216                 if (properties.name == name)
217                     return properties.children.content;
218                 properties = properties.next
219             }
220             return null;
221         }
222                 
223         
224         function walk (element, path) {
225             
226             
227             if (!element) {
228                 return;
229             }
230             
231             var n =  getAttribute(element, 'name') ;
232             //console.log("WALK" + n);
233             if (element.name == 'signal') {
234                 path += '.signal';
235             }
236             
237             if (n) {
238                 path += path.length ? '.' : '';
239                 path += n;
240             }
241             if (element.name == 'return-value') {
242                 path += '.return-value';
243             }
244             
245             var d =   getAttribute(element,'doc');
246             if (d) {
247              //   Seed.print(path + ':' + d);
248                 ret[path] = d;
249             }
250             
251             var child = element.children;
252
253             while (child){
254                 //console.log(child.tag);
255                 if (child.type == "element"){
256                     walk (child, path);
257                 }
258                 child = child.next;
259             }
260         }
261         
262         var pth = GI.Repository.get_search_path ();
263         
264         
265         var gir_path = pth[0].replace(/lib\/girepository-1.0/, 'share\/gir-1.0');
266        
267         
268         //console.log(fn);
269         var  fn = gir_path + '/'+ ns + '-' + ver + '.gir';
270        // console.log(fn);
271         
272         if (!GLib.file_test(fn, GLib.FileTest.EXISTS)) {
273             console.log('missing docc file ' + fn);
274             this.comments[ns] = {};
275             
276             return;
277         }
278         var doc = xml.parseFile(fn);
279         //console.log("xmldoc?" + doc);
280         walk (doc.root, '');
281         //console.dump(ret);
282         this.comments[ns] = ret;
283
284     },
285     registry : { },
286     factory : function(type, ns, name) {
287         if (typeof (this.registry[ns +'.' + name]) == 'undefined') {
288             this.registry[ns +'.' + name] = new imports[type][type](ns,name);
289             this.registry[ns +'.' + name].load();
290         }
291         
292         return this.registry[ns +'.' + name];
293     }
294         
295 };
296
297
298