move palete to a property of project - as vapis may differ
[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                         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                  *  == all static below here...
133                  * 
134                  */
135
136                 public static  Gee.HashMap<string,Gir> global_cache = null;
137                 
138                 public static Gir?  factory(Project.Project  project, string ns) 
139                 {
140                         if (global_cache == null) {
141                                 global_cache = new Gee.HashMap<string,Gir>();
142                                  
143                         }
144                         var cache = global_cache;
145                         if (project != null && project is Project.Gtk) {
146                                 cache = ((Project.Gtk)project).gir_cache;
147                         }
148                         
149                         var ret = cache.get(ns);
150                         
151                         
152                         if (ret == null && project != null) {
153
154                                 var a = new VapiParser( (Project.Gtk)project );
155                                 a.create_valac_tree();
156                                 ret = cache.get(ns);
157                         }
158                         
159                         if (ret != null && !ret.is_overlaid) {
160                                 ret.is_overlaid = true;
161                                 var iter = ret.classes.map_iterator();
162                                 while(iter.next()) {
163                                         iter.get_value().overlayParent(project);
164                                 }
165                                 // loop again and add the ctor properties.
166                                 iter = ret.classes.map_iterator();
167                                 while(iter.next()) {
168                                         iter.get_value().overlayCtorProperties();
169                                 }       
170                                 
171                                 
172                         }
173                          
174                         return ret;
175                         
176                 }
177                 
178                 
179                 
180                 
181                 public static GirObject?  factoryFqn(Project.Project project, string fqn)  
182                 {       
183                         var bits = fqn.split(".");
184                         if (bits.length < 1) {
185                                 return null;
186                         }
187                         
188                         var f = (GirObject)factory(project , bits[0]);
189
190                         if (bits.length == 1 || f ==null) {
191                                 return f;
192                         }
193                         return f.fetchByFqn(fqn.substring(bits[0].length+1)); // since classes are stored in fqn format...?
194                                             
195                         
196                 }
197
198                         
199                 /**
200                  * guess the fqn of a type == eg. gboolean or Widget etc...
201                  */
202                 public static string fqtypeLookup(Project.Project project, string type, string ns) {
203                         var g = factory(project, ns);
204                         if (g.classes.has_key(type)) {
205                                 return ns + "." + type;
206                         }
207                         // enums..
208                         if (g.consts.has_key(type)) {
209                                 return ns + "." + type;
210                         }
211                         
212                         
213                         // look at includes..
214                         var iter = g.includes.map_iterator();
215                         while(iter.next()) {
216                                 // skip empty namespaces on include..?
217                                 if ( iter.get_key() == "") {
218                                         continue;
219                                 }
220                                 var ret = fqtypeLookup(project, type, iter.get_key());
221                                 if (ret != type) {
222                                         return ret;
223                                 }
224                 }       
225                         return type;
226                 }
227                 
228
229
230                 
231                 // needed still - where's it called form..
232                 public static string guessDefaultValueForType(string type) {
233                         //print("guessDefaultValueForType: %s\n", type);
234                         if (type.length < 1 || type.contains(".")) {
235                                 return "null";
236                         }
237                         switch(type) {
238                                 case "gboolean":
239                                         return "true";
240                                 case "guint":
241                                         return "0";
242                                 case "utf8":
243                                         return "\"\"";
244                                 default:
245                                         return "?"+  type + "?";
246                         }
247
248                 }
249
250                 
251
252
253
254                  
255         }       
256
257         
258 }