resources/RooUsage.txt
[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                         // we can do this for pack methods..
69                         if (method == null) {
70                                 print("skip parent.parent is null\n");
71                                 return;
72                         }
73                         var cls = method.gparent;
74                         if (cls == null || cls.nodetype != "Class") {
75                                 //print("skip parent.parent.parent not Class\n");
76                                 return;
77                         }
78
79                          
80                         
81                         c.name =  fetchOverride( cls.name, method.name, c.name);
82                 }
83                 public static bool overrides_loaded = false;
84                 public static Gee.HashMap<string,string> overrides;
85         
86                 public static string fetchOverride(  string cls, string method, string param)
87                 {
88                         // overrides should be in a file Gir.overides
89                         // in that "Gtk.Label.new.str" : "label"
90                         loadOverrides();
91                         var key = "%s.%s.%s".printf(cls,method,param);
92                          //print("Chekcing for key %s\n", key);
93                         if (!overrides.has_key(key)) {
94                                 return param;
95                         }
96                         return overrides.get(key);
97
98
99                 }
100                  
101                 public static void loadOverrides(bool force = false)
102                 {
103                         if (overrides_loaded && ! force) {
104                                 return;
105                         }
106                 
107                         var pa = new Json.Parser();
108                         pa.load_from_file(BuilderApplication.configDirectory() + "/resources/Gir.overides");
109                         var node = pa.get_root();
110                     
111                         if (node.get_node_type () != Json.NodeType.OBJECT) {
112                                 throw new GirError.INVALID_FORMAT ("Error loading gir.overides : Unexpected element type %s", node.type_name ());
113                         }
114                         overrides = new Gee.HashMap<string,string>();
115                 
116                 
117                         var obj = node.get_object ();
118                 
119                 
120                         obj.foreach_member((o , key, value) => {
121                                 //print(key+"\n");
122                                 var v = obj.get_string_member(key);
123                                 overrides.set(key, v);
124                         });
125         
126                         overrides_loaded = true;
127
128                 
129
130                 }
131                 
132                 /**
133                  *  == all static below here...
134                  * 
135                  */
136                 public static  Gee.HashMap<string,Gir> cache = null;
137
138                 
139                 public static Gir?  factory(string ns) 
140                 {
141                         if (cache == null) {
142                                 cache = new Gee.HashMap<string,Gir>();
143                                 var a = new VapiParser( );
144                                 a.create_valac_tree();
145                                   
146                         }
147                         var ret = cache.get(ns);
148                         
149                         
150                         /*
151                         if (ret == null) {
152
153                                 var add = new Gir(ns);
154                                 
155                                 cache.set(ns, add);
156                         
157                                 var iter = add.classes.map_iterator();
158                                 while(iter.next()) {
159                                         iter.get_value().overlayParent();
160                                 }
161                                 // loop again and add the ctor properties.
162                                 iter = add.classes.map_iterator();
163                                 while(iter.next()) {
164                                         iter.get_value().overlayCtorProperties();
165                                 }       
166
167                                 
168                                 ret = cache.get(ns);
169                         }
170                         */
171                         if (ret != null && !ret.is_overlaid) {
172                                 ret.is_overlaid = true;
173                                 var iter = ret.classes.map_iterator();
174                                 while(iter.next()) {
175                                         iter.get_value().overlayParent();
176                                 }
177                                 // loop again and add the ctor properties.
178                                 iter = ret.classes.map_iterator();
179                                 while(iter.next()) {
180                                         iter.get_value().overlayCtorProperties();
181                                 }       
182                                 
183                                 
184                         }
185                         
186                         
187                          
188
189                         return ret;
190                         
191                 }
192                 public static GirObject?  factoryFqn(string fqn)  
193                 {       
194                         var bits = fqn.split(".");
195                         if (bits.length < 1) {
196                                 return null;
197                         }
198                         
199                         var f = (GirObject)factory(bits[0]);
200
201                         if (bits.length == 1 || f ==null) {
202                                 return f;
203                         }
204                         return f.fetchByFqn(fqn.substring(bits[0].length+1)); // since classes are stored in fqn format...?
205                                             
206                         
207                 }
208
209                         
210                 /**
211                  * guess the fqn of a type == eg. gboolean or Widget etc...
212                  */
213                 public static string fqtypeLookup(string type, string ns) {
214                         var g = factory(ns);
215                         if (g.classes.has_key(type)) {
216                                 return ns + "." + type;
217                         }
218                         // enums..
219                         if (g.consts.has_key(type)) {
220                                 return ns + "." + type;
221                         }
222                         
223                         
224                         // look at includes..
225                         var iter = g.includes.map_iterator();
226                         while(iter.next()) {
227                                 // skip empty namespaces on include..?
228                                 if ( iter.get_key() == "") {
229                                         continue;
230                                 }
231                                 var ret = fqtypeLookup(type, iter.get_key());
232                                 if (ret != type) {
233                                         return ret;
234                                 }
235                 }       
236                         return type;
237                 }
238                 
239
240
241                 
242                 // needed still - where's it called form..
243                 public static string guessDefaultValueForType(string type) {
244                         //print("guessDefaultValueForType: %s\n", type);
245                         if (type.length < 1 || type.contains(".")) {
246                                 return "null";
247                         }
248                         switch(type) {
249                                 case "gboolean":
250                                         return "true";
251                                 case "guint":
252                                         return "0";
253                                 case "utf8":
254                                         return "\"\"";
255                                 default:
256                                         return "?"+  type + "?";
257                         }
258
259                 }
260
261                 
262
263
264
265                  
266         }       
267
268         
269 }