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 GirObject? factory(Project.Project? project, string ns)
149 if (project == null) {
152 if (project.gir_cache == null) {
153 project.gir_cache = new Gee.HashMap<string,GirObject>();
156 var cache = project.gir_cache;
157 if (project != null && project is Project.Gtk) {
158 var gproj = ((Project.Gtk)project);
159 if (!gproj.gir_cache_loaded) {
160 var a = new VapiParser( (Project.Gtk)project );
161 a.create_valac_tree();
162 gproj.gir_cache_loaded = true;
164 cache = gproj.gir_cache;
169 var ret = cache.get(ns);
173 if (ret != null && !ret.is_overlaid) {
174 ret.is_overlaid = true;
175 var iter = ret.classes.map_iterator();
177 iter.get_value().overlayParent(project);
179 // loop again and add the ctor properties.
180 iter = ret.classes.map_iterator();
182 iter.get_value().overlayCtorProperties();
195 public static GirObject? factoryFqn(Project.Project project, string in_fqn)
198 // swap Gtk.Source* to GtkSource.
200 GLib.debug("Gir.factoryFqn search %s", fqn);
201 var bits = fqn.split(".");
202 if (bits.length < 1) {
203 GLib.debug("Gir.factoryFqn fail - missing '.'");
207 var f = (GirObject)factory(project , bits[0]);
209 if (bits.length == 1 || f ==null) {
210 GLib.debug("Gir.factoryFqn fail - factory failed to load NS");
213 GLib.debug("Gir.factoryFqn fetching child %s", fqn.substring(bits[0].length+1));
214 return f.fetchByFqn(fqn.substring(bits[0].length+1)); // since classes are stored in fqn format...?
221 * guess the fqn of a type == eg. gboolean or Widget etc...
223 public static string fqtypeLookup(Project.Project project, string type, string ns) {
224 var g = factory(project, ns);
225 if (g.classes.has_key(type)) {
226 return ns + "." + type;
229 if (g.consts.has_key(type)) {
230 return ns + "." + type;
234 // look at includes..
235 var iter = g.includes.map_iterator();
237 // skip empty namespaces on include..?
238 if ( iter.get_key() == "") {
241 var ret = fqtypeLookup(project, type, iter.get_key());
252 // needed still - where's it called form..
253 public static string guessDefaultValueForType(string type) {
254 //print("guessDefaultValueForType: %s\n", type);
255 if (type.length < 1 || type.contains(".")) {
258 switch(type.down()) {
272 return "?"+ type + "?";