05c731e238558093cb77ddfa9e1e8ed51480d786
[roobuilder] / 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                         //GLib.debug("checkParamOverride :check %s", 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                         
91                         loadOverrides();
92                         var key = "%s.%s.%s".printf(cls,method,param);
93                          //print("Chekcing for key %s\n", key);
94                         if (!overrides.has_key(key)) {
95                                 return param;
96                         }
97                         return overrides.get(key);
98
99
100                 }
101                  
102                 public static void loadOverrides(bool force = false) 
103                 {
104                         if (overrides_loaded && ! force) {
105                                 return;
106                         }
107                         Json.Node node = null;
108                         var pa = new Json.Parser();
109                         try {
110                                 pa.load_from_file(BuilderApplication.configDirectory() + "/resources/Gir.overides");
111                                 node = pa.get_root();
112                                 if (node.get_node_type () != Json.NodeType.OBJECT) {
113                                         GLib.debug("Error loading gir.overides : Unexpected element type %s", node.type_name ());
114                                         
115                                         return;
116                                         //throw new GirError.INVALID_FORMAT ("Error loading gir.overides : Unexpected element type %s", node.type_name ());
117                                 }
118                         } catch (GLib.Error e) {
119                                 return;
120                         }
121                         
122                         overrides = new Gee.HashMap<string,string>();
123                 
124                 
125                         var obj = node.get_object ();
126                 
127                 
128                         obj.foreach_member((o , key, value) => {
129                                 //print(key+"\n");
130                                 var v = obj.get_string_member(key);
131                                 overrides.set(key, v);
132                         });
133         
134                         overrides_loaded = true;
135  
136
137                 }
138                 
139                 /**
140                  *  == all static below here...
141                  * 
142                  */
143
144         //      public static  Gee.HashMap<string,Gir> global_cache = null;
145                 
146                 public static GirObject?  factory(Project.Project?  project, string ns) 
147                 {
148                         
149                         if (project == null) {
150                                 return null;
151                         }
152                         if (project.gir_cache == null) {
153                                 project.gir_cache = new Gee.HashMap<string,GirObject>();
154                                  
155                         }
156                         var cache = project.gir_cache;
157                         if (project != null && project is Project.Gtk) {
158                                 var gproj = ((Project.Gtk)project);
159                                 if (!gproj.gir_cache_loaded) {
160                                         var a = new VapiParser( (Project.Gtk)project );
161                                         a.create_valac_tree();
162                                         gproj.gir_cache_loaded = true;
163                                 }
164                                 cache = gproj.gir_cache;
165                                 
166                                 
167                         }
168                         
169                         var ret = cache.get(ns);
170                         
171                          
172                         
173                         if (ret != null && !ret.is_overlaid) {
174                                 ret.is_overlaid = true;
175                                 var iter = ret.classes.map_iterator();
176                                 while(iter.next()) {
177                                         iter.get_value().overlayParent(project);
178                                 }
179                                 // loop again and add the ctor properties.
180                                 iter = ret.classes.map_iterator();
181                                 while(iter.next()) {
182                                         iter.get_value().overlayCtorProperties();
183                                 }       
184                                 
185                                 
186                         }
187                          
188                         return ret;
189                         
190                 }
191                 
192                 
193                 
194                 
195                 public static GirObject?  factoryFqn(Project.Project project, string in_fqn)  
196                 {       
197                         var fqn = in_fqn;
198                         // swap Gtk.Source* to GtkSource.
199                         
200                         GLib.debug("Gir.factoryFqn  search %s", fqn);
201                         var bits = fqn.split(".");
202                         if (bits.length < 1) {
203                                 GLib.debug("Gir.factoryFqn  fail - missing '.'");
204                                 return null;
205                         }
206                         
207                         var f = (GirObject)factory(project , bits[0]);
208
209                         if (bits.length == 1 || f ==null) {
210                                 GLib.debug("Gir.factoryFqn  fail - factory failed to load NS");
211                                 return f;
212                         }
213                         GLib.debug("Gir.factoryFqn  fetching child %s", fqn.substring(bits[0].length+1));
214                         return f.fetchByFqn(fqn.substring(bits[0].length+1)); // since classes are stored in fqn format...?
215                                             
216                         
217                 }
218                  
219                         
220                 /**
221                  * guess the fqn of a type == eg. gboolean or Widget etc...
222                  */
223                 public static string fqtypeLookup(Project.Project project, string type, string ns) {
224                         var g = factory(project, ns);
225                         if (g.classes.has_key(type)) {
226                                 return ns + "." + type;
227                         }
228                         // enums..
229                         if (g.consts.has_key(type)) {
230                                 return ns + "." + type;
231                         }
232                         
233                         
234                         // look at includes..
235                         var iter = g.includes.map_iterator();
236                         while(iter.next()) {
237                                 // skip empty namespaces on include..?
238                                 if ( iter.get_key() == "") {
239                                         continue;
240                                 }
241                                 var ret = fqtypeLookup(project, type, iter.get_key());
242                                 if (ret != type) {
243                                         return ret;
244                                 }
245                 }       
246                         return type;
247                 }
248                 
249
250
251                 
252                 // needed still - where's it called form..
253                 public static string guessDefaultValueForType(string type) {
254                         //print("guessDefaultValueForType: %s\n", type);
255                         if (type.length < 1 || type.contains(".")) {
256                                 return "null";
257                         }
258                         switch(type.down()) {
259                                 case "boolean":
260                                 case "bool":
261                                 case "gboolean":
262                                         return "true";
263                                 case "int":                                     
264                                 case "guint":
265                                         return "0";
266                                 case "gdouble":
267                                         return "0f";
268                                 case "utf8":
269                                 case "string":
270                                         return "\"\"";
271                                 default:
272                                         return "?"+  type + "?";
273                         }
274
275                 }
276
277                 
278
279
280
281                  
282         }       
283
284         
285 }