Fix #7394 - more warning fixes
[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 Gir?  factory(Project.Project?  project, string ns) 
147                 {
148                         if (global_cache == null) {
149                                 global_cache = new Gee.HashMap<string,Gir>();
150                                  
151                         }
152                         var cache = global_cache;
153                         if (project != null && project is Project.Gtk) {
154                                 var gproj = ((Project.Gtk)project);
155                                 if (!gproj.gir_cache_loaded) {
156                                         var a = new VapiParser( (Project.Gtk)project );
157                                         a.create_valac_tree();
158                                         gproj.gir_cache_loaded = true;
159                                 }
160                                 cache = gproj.gir_cache;
161                                 
162                                 
163                         }
164                         
165                         var ret = cache.get(ns);
166                         
167                          
168                         
169                         if (ret != null && !ret.is_overlaid) {
170                                 ret.is_overlaid = true;
171                                 var iter = ret.classes.map_iterator();
172                                 while(iter.next()) {
173                                         iter.get_value().overlayParent(project);
174                                 }
175                                 // loop again and add the ctor properties.
176                                 iter = ret.classes.map_iterator();
177                                 while(iter.next()) {
178                                         iter.get_value().overlayCtorProperties();
179                                 }       
180                                 
181                                 
182                         }
183                          
184                         return ret;
185                         
186                 }
187                 
188                 
189                 
190                 
191                 public static GirObject?  factoryFqn(Project.Project project, string in_fqn)  
192                 {       
193                         var fqn = in_fqn;
194                         // swap Gtk.Source* to GtkSource.
195                         
196                         GLib.debug("Gir.factoryFqn  search %s", fqn);
197                         var bits = fqn.split(".");
198                         if (bits.length < 1) {
199                                 GLib.debug("Gir.factoryFqn  fail - missing '.'");
200                                 return null;
201                         }
202                         
203                         var f = (GirObject)factory(project , bits[0]);
204
205                         if (bits.length == 1 || f ==null) {
206                                 GLib.debug("Gir.factoryFqn  fail - factory failed to load NS");
207                                 return f;
208                         }
209                         GLib.debug("Gir.factoryFqn  fetching child %s", fqn.substring(bits[0].length+1));
210                         return f.fetchByFqn(fqn.substring(bits[0].length+1)); // since classes are stored in fqn format...?
211                                             
212                         
213                 }
214                  
215                         
216                 /**
217                  * guess the fqn of a type == eg. gboolean or Widget etc...
218                  */
219                 public static string fqtypeLookup(Project.Project project, string type, string ns) {
220                         var g = factory(project, ns);
221                         if (g.classes.has_key(type)) {
222                                 return ns + "." + type;
223                         }
224                         // enums..
225                         if (g.consts.has_key(type)) {
226                                 return ns + "." + type;
227                         }
228                         
229                         
230                         // look at includes..
231                         var iter = g.includes.map_iterator();
232                         while(iter.next()) {
233                                 // skip empty namespaces on include..?
234                                 if ( iter.get_key() == "") {
235                                         continue;
236                                 }
237                                 var ret = fqtypeLookup(project, type, iter.get_key());
238                                 if (ret != type) {
239                                         return ret;
240                                 }
241                 }       
242                         return type;
243                 }
244                 
245
246
247                 
248                 // needed still - where's it called form..
249                 public static string guessDefaultValueForType(string type) {
250                         //print("guessDefaultValueForType: %s\n", type);
251                         if (type.length < 1 || type.contains(".")) {
252                                 return "null";
253                         }
254                         switch(type.down()) {
255                                 case "boolean":
256                                 case "bool":
257                                 case "gboolean":
258                                         return "true";
259                                 case "int":                                     
260                                 case "guint":
261                                         return "0";
262                                 case "gdouble":
263                                         return "0f";
264                                 case "utf8":
265                                 case "string":
266                                         return "\"\"";
267                                 default:
268                                         return "?"+  type + "?";
269                         }
270
271                 }
272
273                 
274
275
276
277                  
278         }       
279
280         
281 }