conf7819.file
[app.Builder.js] / src / Palete / Gtk.vala
1 namespace Palete {
2
3         
4         
5         
6         
7         public class Introspect.El : Object
8         {
9                 public enum eltype { 
10                             NS,
11                             CLASS,
12                             METHOD,
13                             PROP
14                 }
15                 
16             
17                 public eltype type;
18         }
19
20
21         public class Gtk : Palete {
22                 
23                 public Gee.ArrayList<string> package_cache;
24                 
25                 public Gtk()
26                 {
27
28
29                     
30                     base();
31                     this.name = "Gtk";
32                     var context = new Vala.CodeContext ();
33                          
34                     this.package_cache = this.loadPackages(Path.get_dirname (context.get_vapi_path("glib-2.0")));
35                                 //this.load();
36                     // various loader methods..
37                       //this.map = [];
38                     //this.load();
39                     //this.proplist = {};
40                     //this.comments = { }; 
41                     // no parent...
42                 }
43               
44                 public override void  load () {
45
46                         this.loadUsageFile(BuilderApplication.configDirectory() + "/resources/GtkUsage.txt");
47          
48                      
49                 }
50                 
51                 public string doc(string what) {
52                         var ns = what.split(".")[0];
53                         var gir =  Gir.factory(ns);
54                         return   gir.doc(what);
55                         
56                     //return typeof(this.comments[ns][what]) == 'undefined' ?  '' : this.comments[ns][what];
57                 }
58
59                         // does not handle implements...
60                 public override GirObject? getClass(string ename)
61                 {
62
63                         var es = ename.split(".");
64                         var gir = Gir.factory(es[0]);
65                 
66                         return gir.classes.get(es[1]);
67                 
68                 }
69
70                 public override Gee.HashMap<string,GirObject> getPropertiesFor(string ename, string type)
71                 {
72                         //print("Loading for " + ename);
73                     
74
75
76                                 // if (typeof(this.proplist[ename]) != 'undefined') {
77                         //print("using cache");
78                         //   return this.proplist[ename][type];
79                         //}
80                         // use introspection to get lists..
81          
82                         var es = ename.split(".");
83                         var gir = Gir.factory(es[0]);
84                 
85                         var cls = gir.classes.get(es[1]);
86                         if (cls == null) {
87                                 var ret = new Gee.HashMap<string,GirObject>();
88                                 return ret;
89                                 //throw new Error.INVALID_VALUE( "Could not find class: " + ename);
90                         
91                         }
92
93                         //cls.parseProps();
94                         //cls.parseSignals(); // ?? needed for add handler..
95                         //cls.parseMethods(); // ?? needed for ??..
96                         //cls.parseConstructors(); // ?? needed for ??..
97
98                         cls.overlayParent();
99
100                         switch  (type) {
101                                 case "props":
102                                         return cls.props;
103                                 case "signals":
104                                         return cls.signals;
105                                 case "methods":
106                                         return cls.methods;
107                                 case "ctors":
108                                         return cls.ctors;
109                                 default:
110                                         throw new Error.INVALID_VALUE( "getPropertiesFor called with: " + type);
111                                         //var ret = new Gee.HashMap<string,GirObject>();
112                                         //return ret;
113                                 
114                         }
115                                 
116                         
117                         //cls.overlayInterfaces(gir);
118                     
119                     
120                      
121                 }
122                 public string[] getInheritsFor(string ename)
123                 {
124                         string[] ret = {};
125                          
126                         var cls = Gir.factoryFqn(ename);
127                          
128                         if (cls == null || cls.nodetype != "Class") {
129                                 print("getInheritsFor:could not find cls: %s\n", ename);
130                                 return ret;
131                         }
132                         
133                         return cls.inheritsToStringArray();
134                         
135
136                 }
137          
138                 public override void fillPack(JsRender.Node node,JsRender.Node parent)
139                 {   
140                         
141                         string inherits =  string.joinv(" ", 
142                                       this.getInheritsFor (node.fqn())) + " ";
143                         inherits += node.fqn() + " ";
144                         //print ("fillPack:Inherits : %s\n", inherits);
145                         // parent.fqn() method ( node.fqn()
146                         var methods = this.getPropertiesFor (parent.fqn(), "methods");
147                         
148                         var res = new Gee.HashMap<string,string>();
149                         var map = methods.map_iterator();
150                         while (map.next()) {
151                                 
152                                 var n = map.get_key();
153                                 //print ("fillPack:checking method %s\n", n);
154                                 
155                                 var meth = map.get_value();
156                                 if (meth.paramset == null || meth.paramset.params.size < 1) {
157                                         print ("fillPack:c -- no params\n");
158                                 
159                                         continue;
160                                 }
161                                 var fp = meth.paramset.params.get(0);
162                                 
163                                 var type = Gir.fqtypeLookup(fp.type, meth.ns);
164                                 print ("fillPack:first param type is %s\n", type);
165
166                                 
167                                 if (!inherits.contains(" " + type + " ")) {
168                                         continue;
169                                 }
170                                 
171                                 
172                                 var pack = meth.name;
173                                 for(var i =1; i < meth.paramset.params.size; i++) {
174                                         var ty = Gir.fqtypeLookup(meth.paramset.params.get(i).type, meth.ns);
175                                         pack += "," + Gir.guessDefaultValueForType(ty);
176                                 }
177
178                                 print ("fillPack:add pack:  --          %s\n",pack );
179
180                                 res.set(meth.name, pack);
181                                 
182                                 
183
184                         }
185                         if (res.size < 1) {
186                                 return ;
187                         }
188                         if (res.has_key("pack_start")) {
189                                 node.props.set("* pack", res.get("pack_start"));
190                                 return;
191                         }
192                         if (res.has_key("add")) {
193                                 node.props.set("* pack", res.get("add"));
194                                 return;
195                         }
196                         var riter = res.map_iterator();
197                         while(riter.next()) {
198                                 node.props.set("* pack", riter.get_value());
199                                 return;
200                         }
201                         
202                         
203                 }
204                 public Gee.ArrayList<string> packages(Project.Gtk gproject)
205                 {
206                         var vapidirs = gproject.vapidirs();
207                         var ret =  new Gee.ArrayList<string>();
208                         ret.add_all(this.package_cache);
209                         for(var i = 0; i < vapidirs.length;i++) {
210                                 var add = this.loadPackages(vapidirs[i]);
211                                 for (var j=0; j < add.size; j++) {
212                                         if (ret.contains(add.get(j))) {
213                                                 continue;
214                                         }
215                                         ret.add(add.get(j));
216                                 }
217                                 
218                         }
219                         
220                         return ret;
221                 }
222                 
223                 public  Gee.ArrayList<string>  loadPackages(string dirname)
224                 {
225
226                         var ret = new  Gee.ArrayList<string>();
227                         //this.package_cache = new Gee.ArrayList<string>();
228                          
229                          
230                         var dir = File.new_for_path(dirname);
231                         try {
232                                 var file_enum = dir.enumerate_children(
233                                         GLib.FileAttribute.STANDARD_DISPLAY_NAME, 
234                                         GLib.FileQueryInfoFlags.NONE, 
235                                         null
236                                 );
237                         
238                          
239                                 FileInfo next_file; 
240                                 while ((next_file = file_enum.next_file(null)) != null) {
241                                         var fn = next_file.get_display_name();
242                                         if (!Regex.match_simple("\\.vapi$", fn)) {
243                                                 continue;
244                                         }
245                                         ret.add(Path.get_basename(fn).replace(".vapi", ""));
246                                 }       
247                         } catch(Error e) {
248                                 print("oops - something went wrong scanning the packages\n");
249                         }
250                         return ret;
251                         
252                          
253                 }
254  
255         
256     }
257 }
258