src/Palete/Gir.vala
[app.Builder.js] / 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         public abstract class GirBase : GirObject { 
24                 
25                 public abstract void load();
26                 
27                 public string doc(string what)
28                 {
29                         var ar = what.split(".");
30                         var cls = this.classes.get(ar[1]);
31                         if (ar.length == 2) {
32                                 return cls.doctxt != null ? cls.doctxt : "";
33                         }
34                         // return the property.. by default..
35                         var pr = cls.props.get(ar[2]);
36                         return pr.doctxt != null ? pr.doctxt : "";
37
38                 }
39         }
40     
41     
42     // Gir - is the libvala based version - 
43     
44     
45         public class Gir : GirBase {
46     
47                 //Gee.Hashmap<string,what> nodes;
48                 
49                 public Gir (string ns)
50                 {
51                         base("Package",ns);
52                          
53                 }
54                  
55                 
56                 
57                 /**
58                  *  == all static below here...
59                  * 
60                  */
61                 public static  Gee.HashMap<string,Gir> cache = null;
62
63                 
64                 public static GirBase?  factory(string ns) 
65                 {
66                         if (cache == null) {
67                                 cache = new Gee.HashMap<string,Gir>();
68                                 var a = new Palete.VapiParser( );
69                                 a.create_valac_tree();
70                                  
71                                 
72                         }
73                         var ret = cache.get(ns);
74                         if (ret == null) {
75
76                                 var add = new Gir(ns);
77                                 
78                                 cache.set(ns, add);
79                         
80                                 var iter = add.classes.map_iterator();
81                                 while(iter.next()) {
82                                         iter.get_value().overlayParent();
83                                 }
84                                 // loop again and add the ctor properties.
85                                 iter = add.classes.map_iterator();
86                                 while(iter.next()) {
87                                         iter.get_value().overlayCtorProperties();
88                                 }       
89
90                                 
91                                 ret = cache.get(ns);
92                         }
93                          
94
95                         return ret;
96                         
97                 }
98                 public static GirObject?  factoryFqn(string fqn)  
99                 {       
100                         var bits = fqn.split(".");
101                         if (bits.length < 1) {
102                                 return null;
103                         }
104                         
105                         var f = (GirObject)factory(bits[0]);
106
107                         if (bits.length == 1 || f ==null) {
108                                 return f;
109                         }
110                         return f.fetchByFqn(fqn.substring(bits[0].length+1)); // since classes are stored in fqn format...?
111                                             
112                         
113                 }
114
115                         
116                 /**
117                  * guess the fqn of a type == eg. gboolean or Widget etc...
118                  */
119                 public static string fqtypeLookup(string type, string ns) {
120                         var g = factory(ns);
121                         if (g.classes.has_key(type)) {
122                                 return ns + "." + type;
123                         }
124                         // enums..
125                         if (g.consts.has_key(type)) {
126                                 return ns + "." + type;
127                         }
128                         
129                         
130                         // look at includes..
131                         var iter = g.includes.map_iterator();
132                         while(iter.next()) {
133                                 // skip empty namespaces on include..?
134                                 if ( iter.get_key() == "") {
135                                         continue;
136                                 }
137                                 var ret = fqtypeLookup(type, iter.get_key());
138                                 if (ret != type) {
139                                         return ret;
140                                 }
141                 }       
142                         return type;
143                 }
144                 
145
146
147                 
148                 // needed still - where's it called form..
149                 public static string guessDefaultValueForType(string type) {
150                         //print("guessDefaultValueForType: %s\n", type);
151                         if (type.length < 1 || type.contains(".")) {
152                                 return "null";
153                         }
154                         switch(type) {
155                                 case "gboolean":
156                                         return "true";
157                                 case "guint":
158                                         return "0";
159                                 case "utf8":
160                                         return "\"\"";
161                                 default:
162                                         return "?"+  type + "?";
163                         }
164
165                 }
166
167
168                 
169
170
171
172                  
173         }       
174
175         
176 }