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
4 public static int main (string[] args) {
6 var g = Palete.Gir.factory("Gtk");
7 var test = g.classes.get("ToolButton");
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);
15 generator.pretty = true;
17 print(generator.to_data(null));
26 // Gir - is the libvala based version -
29 public class Gir : GirObject {
31 //Gee.Hashmap<string,what> nodes;
33 public Gir (string ns)
39 public string doc(string what)
41 var ar = what.split(".");
42 var cls = this.classes.get(ar[1]);
44 return cls.doctxt != null ? cls.doctxt : "";
46 // return the property.. by default..
47 var pr = cls.props.get(ar[2]);
48 return pr.doctxt != null ? pr.doctxt : "";
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.
58 public static void checkParamOverride(GirObject c)
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");
67 var method = parset.gparent;
68 // we can do this for pack methods..
70 print("skip parent.parent is null\n");
73 var cls = method.gparent;
74 if (cls == null || cls.nodetype != "Class") {
75 //print("skip parent.parent.parent not Class\n");
81 c.name = fetchOverride( cls.name, method.name, c.name);
83 public static bool overrides_loaded = false;
84 public static Gee.HashMap<string,string> overrides;
86 public static string fetchOverride( string cls, string method, string param)
88 // overrides should be in a file Gir.overides
89 // in that "Gtk.Label.new.str" : "label"
92 var key = "%s.%s.%s".printf(cls,method,param);
93 //print("Chekcing for key %s\n", key);
94 if (!overrides.has_key(key)) {
97 return overrides.get(key);
102 public static void loadOverrides(bool force = false)
104 if (overrides_loaded && ! force) {
107 Json.Node node = null;
108 var pa = new Json.Parser();
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 ());
116 //throw new GirError.INVALID_FORMAT ("Error loading gir.overides : Unexpected element type %s", node.type_name ());
118 } catch (GLib.Error e) {
122 overrides = new Gee.HashMap<string,string>();
125 var obj = node.get_object ();
128 obj.foreach_member((o , key, value) => {
130 var v = obj.get_string_member(key);
131 overrides.set(key, v);
134 overrides_loaded = true;
140 * == all static below here...
144 public static Gee.HashMap<string,Gir> global_cache = null;
146 public static Gir? factory(Project.Project? project, string ns)
148 if (global_cache == null) {
149 global_cache = new Gee.HashMap<string,Gir>();
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;
160 cache = gproj.gir_cache;
165 var ret = cache.get(ns);
169 if (ret != null && !ret.is_overlaid) {
170 ret.is_overlaid = true;
171 var iter = ret.classes.map_iterator();
173 iter.get_value().overlayParent(project);
175 // loop again and add the ctor properties.
176 iter = ret.classes.map_iterator();
178 iter.get_value().overlayCtorProperties();
191 public static GirObject? factoryFqn(Project.Project project, string in_fqn)
194 // swap Gtk.Source* to GtkSource.
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 '.'");
203 var f = (GirObject)factory(project , bits[0]);
205 if (bits.length == 1 || f ==null) {
206 GLib.debug("Gir.factoryFqn fail - factory failed to load NS");
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...?
217 * guess the fqn of a type == eg. gboolean or Widget etc...
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;
225 if (g.consts.has_key(type)) {
226 return ns + "." + type;
230 // look at includes..
231 var iter = g.includes.map_iterator();
233 // skip empty namespaces on include..?
234 if ( iter.get_key() == "") {
237 var ret = fqtypeLookup(project, type, iter.get_key());
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(".")) {
254 switch(type.down()) {
268 return "?"+ type + "?";