2 * This is the base class for representing the vala API
4 * it was originally based on parsing Gir files - but since then
5 * has evolved into using libvala
13 public errordomain GirError {
20 public class GirObject: Object {
23 public string propertyof;
25 public string nodetype; // eg. Signal / prop etc.
26 public string package;
27 public string direction; // used for vala in/out/ref...
29 public GirObject paramset = null;
30 public GirObject return_value = null;
31 public bool is_deprecated = false;
32 public bool is_instance;
34 public bool is_varargs;
35 public bool ctor_only; // specially added ctor properties..
36 public bool is_writable = true;
37 public bool is_readable = true;
38 public bool is_abstract = false;
43 public string sig; // signture (used to create event handlers)
45 public bool is_overlaid;
47 public GirObject gparent;
48 public Gee.ArrayList<GirObject> params;
49 public Gee.ArrayList<string> implements;
50 public Gee.ArrayList<string> implementations;
51 public Gee.ArrayList<string> inherits; // full list of all classes and interfaces...
52 public Gee.HashMap<string,GirObject> ctors;
53 public Gee.HashMap<string,GirObject> methods;
54 public Gee.HashMap<string,string> includes;
55 public Gee.HashMap<string,GirObject> classes;
56 public Gee.HashMap<string,GirObject> props;
57 public Gee.HashMap<string,GirObject> consts;
58 public Gee.HashMap<string,GirObject> signals;
60 public Gee.ArrayList<string> optvalues; // used by Roo only..
62 public Gee.ArrayList<string> can_drop_onto; // used by Roo only.. at present
63 public Gee.ArrayList<string> valid_cn; // used by Roo only.. at present
69 public GirObject(string nodetype, string n)
71 this.nodetype = nodetype;
77 this.is_array = false;
78 this.is_instance = false;
79 this.is_varargs = false;
80 this.ctor_only =false;
87 this.implements = new Gee.ArrayList<string>();
88 this.implementations = new Gee.ArrayList<string>();
89 this.inherits = new Gee.ArrayList<string>(); // list of all ancestors. (interfaces and parents)
90 this.includes = new Gee.HashMap<string,string>();
92 this.params = new Gee.ArrayList<GirObject>();
93 this.ctors = new Gee.HashMap<string,GirObject>();
94 this.methods =new Gee.HashMap<string,GirObject>();
96 this.classes = new Gee.HashMap<string,GirObject>();
97 this.props = new Gee.HashMap<string,GirObject>();
98 this.consts = new Gee.HashMap<string,GirObject>();
99 this.signals = new Gee.HashMap<string,GirObject>();
101 this.optvalues = new Gee.ArrayList<string>();
102 this.can_drop_onto = new Gee.ArrayList<string>();
103 this.valid_cn = new Gee.ArrayList<string>();
107 this.is_overlaid = false;
108 this.paramset = null;
111 public string[] inheritsToStringArray()
114 for(var i =0;i< this.inherits.size; i++) {
115 ret += this.inherits.get(i);
117 for(var i =0;i< this.implements.size; i++) {
118 ret += this.implements.get(i);
125 public void overlayParent(Project.Project project)
128 if (this.parent.length < 1 || this.is_overlaid) {
129 this.is_overlaid = true;
133 //print("Overlaying " +this.name + " with " + this.parent + "\n");
135 var pcls = this.clsToObject( project, this.parent);
138 //throw new GirError.INVALID_VALUE("Could not find class : " +
139 // this.parent + " of " + this.name + " in " + this.ns);
142 pcls.overlayParent( project );
143 this.copyFrom(pcls,false);
144 for(var i=0; i < this.implements.size; i++) {
145 var clsname = this.implements.get(i);
146 var picls = this.clsToObject(project, clsname);
147 this.copyFrom(picls,true);
149 this.is_overlaid = true;
153 public void overlayCtorProperties()
155 //print("Check overlay Ctor %s\n", this.name);
156 if (!this.ctors.has_key("new")) {
159 var ctor = this.ctors.get("new");
160 if (ctor.paramset == null || ctor.paramset.params.size < 1) {
163 //print("Found Ctor\n");
164 var iter = ctor.paramset.params.list_iterator();
165 while (iter.next()) {
166 var n = iter.get().name;
168 if (this.props.has_key(n)) {
174 //print("Adding prop %s\n", n);
177 var c = new GirObject("Prop",n);
180 c.propertyof = this.name;
181 c.type = iter.get().type;
183 this.props.set(n, c);
191 public string fqn() {
192 // not sure if fqn really is correct here...
194 return this.nodetype == "Class" || this.nodetype=="Interface"
195 ? this.name : (this.ns + this.name);
198 public void copyFrom(GirObject pcls, bool is_interface)
201 this.inherits.add(pcls.fqn());
203 var liter = pcls.inherits.list_iterator();
204 while(liter.next()) {
205 if (this.inherits.contains(liter.get())) {
208 this.inherits.add(liter.get());
212 var iter = pcls.methods.map_iterator();
214 if (null != this.methods.get(iter.get_key())) {
218 this.methods.set(iter.get_key(), iter.get_value());
221 iter = pcls.props.map_iterator();
223 if (null != this.props.get(iter.get_key())) {
227 this.props.set(iter.get_key(), iter.get_value());
230 iter = pcls.signals.map_iterator();
232 if (null != this.signals.get(iter.get_key())) {
236 this.signals.set(iter.get_key(), iter.get_value());
240 public Json.Object toJSON()
242 var r = new Json.Object();
243 r.set_string_member("nodetype", this.nodetype);
244 r.set_string_member("name", this.name);
245 if (this.propertyof.length > 0) {
246 r.set_string_member("of", this.propertyof);
248 if (this.type.length > 0) {
249 r.set_string_member("type", this.type);
251 if (this.parent != null && this.parent.length > 0) {
252 r.set_string_member("parent", this.parent);
254 if (this.sig.length > 0) {
255 r.set_string_member("sig", this.sig);
258 // is_arary / is_instance / is_varargs..
261 if (this.inherits.size > 0) {
262 r.set_array_member("inherits", this.toJSONArrayString(this.inherits));
265 if (this.implements.size > 0) {
266 r.set_array_member("implements", this.toJSONArrayString(this.implements));
269 if (this.params.size > 0) {
270 r.set_array_member("params", this.toJSONArrayObject(this.params));
272 if (this.ctors.size > 0) {
273 r.set_object_member("ctors", this.toJSONObject(this.ctors));
275 if (this.methods.size > 0) {
276 r.set_object_member("methods", this.toJSONObject(this.methods));
278 if (this.includes.size > 0) {
279 r.set_object_member("includes", this.toJSONObjectString(this.includes));
281 if (this.classes.size > 0) {
282 r.set_object_member("classes", this.toJSONObject(this.classes));
284 if (this.props.size > 0) {
285 r.set_object_member("props", this.toJSONObject(this.props));
287 if (this.consts.size > 0) {
288 r.set_object_member("consts", this.toJSONObject(this.consts));
290 if (this.signals.size > 0) {
291 r.set_object_member("signals", this.toJSONObject(this.signals));
293 if (this.paramset != null) {
294 r.set_object_member("paramset", this.paramset.toJSON());
296 if (this.return_value != null) {
297 r.set_object_member("return_value", this.return_value.toJSON());
301 public Json.Object toJSONObject(Gee.HashMap<string,GirObject> map)
303 var r = new Json.Object();
304 var iter = map.map_iterator();
306 r.set_object_member(iter.get_key(), iter.get_value().toJSON());
310 public Json.Object toJSONObjectString(Gee.HashMap<string,string> map)
312 var r = new Json.Object();
313 var iter = map.map_iterator();
315 r.set_string_member(iter.get_key(), iter.get_value());
319 public Json.Array toJSONArrayString(Gee.ArrayList<string> map)
321 var r = new Json.Array();
322 for(var i =0;i< map.size;i++) {
324 r.add_string_element(map.get(i));
328 public Json.Array toJSONArrayObject(Gee.ArrayList<GirObject> map)
330 var r = new Json.Array();
331 for(var i =0;i< map.size;i++) {
333 r.add_object_element(map.get(i).toJSON());
337 public string asJSONString()
339 var generator = new Json.Generator ();
340 generator.indent = 4;
341 generator.pretty = true;
342 var n = new Json.Node(Json.NodeType.OBJECT);
343 n.set_object(this.toJSON());
344 generator.set_root(n);
346 return generator.to_data(null);
350 public GirObject? fetchByFqn(string fqn) {
351 GLib.debug("Searching (%s)%s for %s\n", this.nodetype, this.name, fqn);
352 var bits = fqn.split(".");
354 var ret = this.classes.get(bits[0]);
356 if (bits.length < 2) {
359 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
362 ret = this.ctors.get(bits[0]);
364 if (bits.length < 2) {
367 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
370 ret = this.methods.get(bits[0]);
372 if (bits.length < 2) {
375 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
377 ret = this.props.get(bits[0]);
379 if (bits.length < 2) {
382 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
384 ret = this.consts.get(bits[0]);
386 if (bits.length < 2) {
389 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
392 ret = this.signals.get(bits[0]);
394 if (bits.length < 2) {
397 return ret.fetchByFqn(fqn.substring(bits[0].length+1));
399 if (this.paramset == null) {
402 var iter = this.paramset.params.list_iterator();
403 while (iter.next()) {
405 if (p.name != bits[0]) {
411 // fixme - other queires? - enums?
415 * -----------------------------------------------
416 * code relating to the structure loader ....
420 public GirObject clsToObject(Project.Project project , string in_pn)
425 var gir = Gir.factory (project, this.ns);
426 if (in_pn.contains(".")) {
427 gir = Gir.factory(project, in_pn.split(".")[0]);
428 pn = in_pn.split(".")[1];
432 return gir.classes.get(pn);
438 public JsRender.NodeProp toNodeProp()
441 if (this.nodetype.down() == "signal") { // gtk is Signal, roo is signal??
442 // when we add properties, they are actually listeners attached to signals
443 var r =new JsRender.NodeProp.listener(this.name, this.sig);
448 // does not handle Enums... - no need to handle anything else.
449 var def = this.type.contains(".") ? "" : Gir.guessDefaultValueForType(this.type);
452 return new JsRender.NodeProp.prop(this.name, this.type, def); // signature?
457 //public string fqtype() {
458 // return Gir.fqtypeLookup(this.type, this.ns);
460 /* return Gir.fqtypeLookup(this.type, this.ns); */