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