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          
24     
25     
26     // Gir - is the libvala based version - 
27     
28     
29         public class Gir : GirObject {
30     
31                 //Gee.Hashmap<string,what> nodes;
32                 
33                 public Gir (string ns)
34                 {
35                         base("Package",ns);
36                          
37                 }
38                  
39                 public string doc(string what)
40                 {
41                         var ar = what.split(".");
42                         var cls = this.classes.get(ar[1]);
43                         if (ar.length == 2) {
44                                 return cls.doctxt != null ? cls.doctxt : "";
45                         }
46                         // return the property.. by default..
47                         var pr = cls.props.get(ar[2]);
48                         return pr.doctxt != null ? pr.doctxt : "";
49
50                 }
51                 /**
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.
55                  * 
56                  */
57                  public void loadOverrides(bool force = false)
58                 {
59                         if (overrides_loaded && ! force) {
60                                 return;
61                         }
62                 
63                         var pa = new Json.Parser();
64                         pa.load_from_file(BuilderApplication.configDirectory() + "/resources/Gir.overides");
65                         var node = pa.get_root();
66                     
67                         if (node.get_node_type () != Json.NodeType.OBJECT) {
68                                 throw new GirError.INVALID_FORMAT ("Error loading gir.overides : Unexpected element type %s", node.type_name ());
69                         }
70                         overrides = new Gee.HashMap<string,string>();
71                 
72                 
73                         var obj = node.get_object ();
74                 
75                 
76                         obj.foreach_member((o , key, value) => {
77                                 //print(key+"\n");
78                                 var v = obj.get_string_member(key);
79                                 overrides.set(key, v);
80                         });
81         
82                         overrides_loaded = true;
83
84                 
85
86                 }
87                 
88                 /**
89                  *  == all static below here...
90                  * 
91                  */
92                 public static  Gee.HashMap<string,Gir> cache = null;
93
94                 
95                 public static Gir?  factory(string ns) 
96                 {
97                         if (cache == null) {
98                                 cache = new Gee.HashMap<string,Gir>();
99                                 var a = new VapiParser( );
100                                 a.create_valac_tree();
101                                   
102                         }
103                         var ret = cache.get(ns);
104                         
105                         
106                         /*
107                         if (ret == null) {
108
109                                 var add = new Gir(ns);
110                                 
111                                 cache.set(ns, add);
112                         
113                                 var iter = add.classes.map_iterator();
114                                 while(iter.next()) {
115                                         iter.get_value().overlayParent();
116                                 }
117                                 // loop again and add the ctor properties.
118                                 iter = add.classes.map_iterator();
119                                 while(iter.next()) {
120                                         iter.get_value().overlayCtorProperties();
121                                 }       
122
123                                 
124                                 ret = cache.get(ns);
125                         }
126                         */
127                         if (ret != null && !ret.is_overlaid) {
128                                 ret.is_overlaid = true;
129                                 var iter = ret.classes.map_iterator();
130                                 while(iter.next()) {
131                                         iter.get_value().overlayParent();
132                                 }
133                                 // loop again and add the ctor properties.
134                                 iter = ret.classes.map_iterator();
135                                 while(iter.next()) {
136                                         iter.get_value().overlayCtorProperties();
137                                 }       
138                                 
139                                 
140                         }
141                         
142                         
143                          
144
145                         return ret;
146                         
147                 }
148                 public static GirObject?  factoryFqn(string fqn)  
149                 {       
150                         var bits = fqn.split(".");
151                         if (bits.length < 1) {
152                                 return null;
153                         }
154                         
155                         var f = (GirObject)factory(bits[0]);
156
157                         if (bits.length == 1 || f ==null) {
158                                 return f;
159                         }
160                         return f.fetchByFqn(fqn.substring(bits[0].length+1)); // since classes are stored in fqn format...?
161                                             
162                         
163                 }
164
165                         
166                 /**
167                  * guess the fqn of a type == eg. gboolean or Widget etc...
168                  */
169                 public static string fqtypeLookup(string type, string ns) {
170                         var g = factory(ns);
171                         if (g.classes.has_key(type)) {
172                                 return ns + "." + type;
173                         }
174                         // enums..
175                         if (g.consts.has_key(type)) {
176                                 return ns + "." + type;
177                         }
178                         
179                         
180                         // look at includes..
181                         var iter = g.includes.map_iterator();
182                         while(iter.next()) {
183                                 // skip empty namespaces on include..?
184                                 if ( iter.get_key() == "") {
185                                         continue;
186                                 }
187                                 var ret = fqtypeLookup(type, iter.get_key());
188                                 if (ret != type) {
189                                         return ret;
190                                 }
191                 }       
192                         return type;
193                 }
194                 
195
196
197                 
198                 // needed still - where's it called form..
199                 public static string guessDefaultValueForType(string type) {
200                         //print("guessDefaultValueForType: %s\n", type);
201                         if (type.length < 1 || type.contains(".")) {
202                                 return "null";
203                         }
204                         switch(type) {
205                                 case "gboolean":
206                                         return "true";
207                                 case "guint":
208                                         return "0";
209                                 case "utf8":
210                                         return "\"\"";
211                                 default:
212                                         return "?"+  type + "?";
213                         }
214
215                 }
216
217                 
218
219
220
221                  
222         }       
223
224         
225 }