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\n", 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"
91 var key = "%s.%s.%s".printf(cls,method,param);
92 //print("Chekcing for key %s\n", key);
93 if (!overrides.has_key(key)) {
96 return overrides.get(key);
101 public static void loadOverrides(bool force = false)
103 if (overrides_loaded && ! force) {
107 var pa = new Json.Parser();
108 pa.load_from_file(BuilderApplication.configDirectory() + "/resources/Gir.overides");
109 var node = pa.get_root();
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 ());
114 overrides = new Gee.HashMap<string,string>();
117 var obj = node.get_object ();
120 obj.foreach_member((o , key, value) => {
122 var v = obj.get_string_member(key);
123 overrides.set(key, v);
126 overrides_loaded = true;
132 * == all static below here...
136 public static Gee.HashMap<string,Gir> global_cache = null;
138 public static Gir? factory(Project.Project project, string ns)
140 if (global_cache == null) {
141 global_cache = new Gee.HashMap<string,Gir>();
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;
152 cache = gproj.gir_cache;
157 var ret = cache.get(ns);
161 if (ret != null && !ret.is_overlaid) {
162 ret.is_overlaid = true;
163 var iter = ret.classes.map_iterator();
165 iter.get_value().overlayParent(project);
167 // loop again and add the ctor properties.
168 iter = ret.classes.map_iterator();
170 iter.get_value().overlayCtorProperties();
183 public static GirObject? factoryFqn(Project.Project project, string in_fqn)
186 // swap Gtk.Source* to GtkSource.
188 GLib.debug("Gir.facotryFqn search %s", fqn);
189 var bits = fqn.split(".");
190 if (bits.length < 1) {
194 var f = (GirObject)factory(project , bits[0]);
196 if (bits.length == 1 || f ==null) {
199 return f.fetchByFqn(fqn.substring(bits[0].length+1)); // since classes are stored in fqn format...?
206 * guess the fqn of a type == eg. gboolean or Widget etc...
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;
214 if (g.consts.has_key(type)) {
215 return ns + "." + type;
219 // look at includes..
220 var iter = g.includes.map_iterator();
222 // skip empty namespaces on include..?
223 if ( iter.get_key() == "") {
226 var ret = fqtypeLookup(project, type, iter.get_key());
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(".")) {
251 return "?"+ type + "?";