src/Palete/VapiParser.vala
[app.Builder.js] / src / Palete / Gir.vala
1
2 // valac -g  --pkg gee-1.0 --pkg libxml-2.0 --pkg gobject-introspection-1.0 --pkg json-glib-1.0  Palete/Gir.vala -o /tmp/Gir
3 /* 
4 public static int main (string[] args) {
5     
6     var g = Palete.Gir.factory("Gtk");
7         var test = g.classes.get("ToolButton");
8         
9         
10     var generator = new Json.Generator ();
11     var n = new Json.Node(Json.NodeType.OBJECT);
12     n.set_object(test.toJSON());
13     generator.set_root(n);
14     generator.indent = 4;
15     generator.pretty = true;
16     
17     print(generator.to_data(null));
18     return 0;
19 }
20  */
21 namespace Palete {
22  
23          
24     
25     
26     // Gir - is the libvala based version - 
27     
28     
29         public class Gir : GirObject {
30     
31                 //Gee.Hashmap<string,what> nodes;
32                 
33                 public Gir (string ns)
34                 {
35                         base("Package",ns);
36                          
37                 }
38                  
39                 public string doc(string what)
40                 {
41                         var ar = what.split(".");
42                         var cls = this.classes.get(ar[1]);
43                         if (ar.length == 2) {
44                                 return cls.doctxt != null ? cls.doctxt : "";
45                         }
46                         // return the property.. by default..
47                         var pr = cls.props.get(ar[2]);
48                         return pr.doctxt != null ? pr.doctxt : "";
49
50                 }
51                 /**
52                  * since constructors from the API from gir or vala do not map
53                  * correctly to properties, we have an Gir.overides file in resources
54                  * that changes the ctor's for some elements.
55                  * 
56                  */
57                  
58                 public static void checkParamOverride(GirObject c)
59                 {
60                         
61                         //print("checkParamOverride :check %s\n", c.name);
62                         var parset = c.gparent;
63                         if (parset == null || parset.nodetype != "Paramset") {
64                                 //print("skip parent not Paramset\n");
65                                 return;
66                         }
67                         var method = parset.gparent;
68                         if (method == null || method.nodetype != "Ctor") {
69                                 //print("skip parent.parent not Ctor\n");
70                                 return;
71                         }
72                         var cls = method.gparent;
73                         if (cls == null || cls.nodetype != "Class") {
74                                 //print("skip parent.parent.parent not Class\n");
75                                 return;
76                         }
77
78                          
79                         
80                         c.name =  fetchOverride( cls.name, method.name, c.name);
81                 }
82                 public static bool overrides_loaded = false;
83                 public static Gee.HashMap<string,string> overrides;
84         
85                 public static string fetchOverride(  string cls, string method, string param)
86                 {
87                         // overrides should be in a file Gir.overides
88                         // in that "Gtk.Label.new.str" : "label"
89                         loadOverrides();
90                         var key = "%s.%s.%s".printf(cls,method,param);
91                          //print("Chekcing for key %s\n", key);
92                         if (!overrides.has_key(key)) {
93                                 return param;
94                         }
95                         return overrides.get(key);
96
97
98                 }
99                  
100                 public static void loadOverrides(bool force = false)
101                 {
102                         if (overrides_loaded && ! force) {
103                                 return;
104                         }
105                 
106                         var pa = new Json.Parser();
107                         pa.load_from_file(BuilderApplication.configDirectory() + "/resources/Gir.overides");
108                         var node = pa.get_root();
109                     
110                         if (node.get_node_type () != Json.NodeType.OBJECT) {
111                                 throw new GirError.INVALID_FORMAT ("Error loading gir.overides : Unexpected element type %s", node.type_name ());
112                         }
113                         overrides = new Gee.HashMap<string,string>();
114                 
115                 
116                         var obj = node.get_object ();
117                 
118                 
119                         obj.foreach_member((o , key, value) => {
120                                 //print(key+"\n");
121                                 var v = obj.get_string_member(key);
122                                 overrides.set(key, v);
123                         });
124         
125                         overrides_loaded = true;
126
127                 
128
129                 }
130                 
131                 /**
132                  *  == all static below here...
133                  * 
134                  */
135                 public static  Gee.HashMap<string,Gir> cache = null;
136
137                 
138                 public static Gir?  factory(string ns) 
139                 {
140                         if (cache == null) {
141                                 cache = new Gee.HashMap<string,Gir>();
142                                 var a = new VapiParser( );
143                                 a.create_valac_tree();
144                                   
145                         }
146                         var ret = cache.get(ns);
147                         
148                         
149                         /*
150                         if (ret == null) {
151
152                                 var add = new Gir(ns);
153                                 
154                                 cache.set(ns, add);
155                         
156                                 var iter = add.classes.map_iterator();
157                                 while(iter.next()) {
158                                         iter.get_value().overlayParent();
159                                 }
160                                 // loop again and add the ctor properties.
161                                 iter = add.classes.map_iterator();
162                                 while(iter.next()) {
163                                         iter.get_value().overlayCtorProperties();
164                                 }       
165
166                                 
167                                 ret = cache.get(ns);
168                         }
169                         */
170                         if (ret != null && !ret.is_overlaid) {
171                                 ret.is_overlaid = true;
172                                 var iter = ret.classes.map_iterator();
173                                 while(iter.next()) {
174                                         iter.get_value().overlayParent();
175                                 }
176                                 // loop again and add the ctor properties.
177                                 iter = ret.classes.map_iterator();
178                                 while(iter.next()) {
179                                         iter.get_value().overlayCtorProperties();
180                                 }       
181                                 
182                                 
183                         }
184                         
185                         
186                          
187
188                         return ret;
189                         
190                 }
191                 public static GirObject?  factoryFqn(string fqn)  
192                 {       
193                         var bits = fqn.split(".");
194                         if (bits.length < 1) {
195                                 return null;
196                         }
197                         
198                         var f = (GirObject)factory(bits[0]);
199
200                         if (bits.length == 1 || f ==null) {
201                                 return f;
202                         }
203                         return f.fetchByFqn(fqn.substring(bits[0].length+1)); // since classes are stored in fqn format...?
204                                             
205                         
206                 }
207
208                         
209                 /**
210                  * guess the fqn of a type == eg. gboolean or Widget etc...
211                  */
212                 public static string fqtypeLookup(string type, string ns) {
213                         var g = factory(ns);
214                         if (g.classes.has_key(type)) {
215                                 return ns + "." + type;
216                         }
217                         // enums..
218                         if (g.consts.has_key(type)) {
219                                 return ns + "." + type;
220                         }
221                         
222                         
223                         // look at includes..
224                         var iter = g.includes.map_iterator();
225                         while(iter.next()) {
226                                 // skip empty namespaces on include..?
227                                 if ( iter.get_key() == "") {
228                                         continue;
229                                 }
230                                 var ret = fqtypeLookup(type, iter.get_key());
231                                 if (ret != type) {
232                                         return ret;
233                                 }
234                 }       
235                         return type;
236                 }
237                 
238
239
240                 
241                 // needed still - where's it called form..
242                 public static string guessDefaultValueForType(string type) {
243                         //print("guessDefaultValueForType: %s\n", type);
244                         if (type.length < 1 || type.contains(".")) {
245                                 return "null";
246                         }
247                         switch(type) {
248                                 case "gboolean":
249                                         return "true";
250                                 case "guint":
251                                         return "0";
252                                 case "utf8":
253                                         return "\"\"";
254                                 default:
255                                         return "?"+  type + "?";
256                         }
257
258                 }
259
260                 
261
262
263
264                  
265         }       
266
267         
268 }