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