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                                  
79                                 var v = obj.get_string_member(key);
80                         
81                         
82                                 overrides.set(key, v);
83
84                         });
85         
86                         overrides_loaded = true;
87
88                 
89
90                 }
91                 
92                 /**
93                  *  == all static below here...
94                  * 
95                  */
96                 public static  Gee.HashMap<string,Gir> cache = null;
97
98                 
99                 public static Gir?  factory(string ns) 
100                 {
101                         if (cache == null) {
102                                 cache = new Gee.HashMap<string,Gir>();
103                                 var a = new VapiParser( );
104                                 a.create_valac_tree();
105                                   
106                         }
107                         var ret = cache.get(ns);
108                         
109                         
110                         /*
111                         if (ret == null) {
112
113                                 var add = new Gir(ns);
114                                 
115                                 cache.set(ns, add);
116                         
117                                 var iter = add.classes.map_iterator();
118                                 while(iter.next()) {
119                                         iter.get_value().overlayParent();
120                                 }
121                                 // loop again and add the ctor properties.
122                                 iter = add.classes.map_iterator();
123                                 while(iter.next()) {
124                                         iter.get_value().overlayCtorProperties();
125                                 }       
126
127                                 
128                                 ret = cache.get(ns);
129                         }
130                         */
131                         if (ret != null && !ret.is_overlaid) {
132                                 ret.is_overlaid = true;
133                                 var iter = ret.classes.map_iterator();
134                                 while(iter.next()) {
135                                         iter.get_value().overlayParent();
136                                 }
137                                 // loop again and add the ctor properties.
138                                 iter = ret.classes.map_iterator();
139                                 while(iter.next()) {
140                                         iter.get_value().overlayCtorProperties();
141                                 }       
142                                 
143                                 
144                         }
145                         
146                         
147                          
148
149                         return ret;
150                         
151                 }
152                 public static GirObject?  factoryFqn(string fqn)  
153                 {       
154                         var bits = fqn.split(".");
155                         if (bits.length < 1) {
156                                 return null;
157                         }
158                         
159                         var f = (GirObject)factory(bits[0]);
160
161                         if (bits.length == 1 || f ==null) {
162                                 return f;
163                         }
164                         return f.fetchByFqn(fqn.substring(bits[0].length+1)); // since classes are stored in fqn format...?
165                                             
166                         
167                 }
168
169                         
170                 /**
171                  * guess the fqn of a type == eg. gboolean or Widget etc...
172                  */
173                 public static string fqtypeLookup(string type, string ns) {
174                         var g = factory(ns);
175                         if (g.classes.has_key(type)) {
176                                 return ns + "." + type;
177                         }
178                         // enums..
179                         if (g.consts.has_key(type)) {
180                                 return ns + "." + type;
181                         }
182                         
183                         
184                         // look at includes..
185                         var iter = g.includes.map_iterator();
186                         while(iter.next()) {
187                                 // skip empty namespaces on include..?
188                                 if ( iter.get_key() == "") {
189                                         continue;
190                                 }
191                                 var ret = fqtypeLookup(type, iter.get_key());
192                                 if (ret != type) {
193                                         return ret;
194                                 }
195                 }       
196                         return type;
197                 }
198                 
199
200
201                 
202                 // needed still - where's it called form..
203                 public static string guessDefaultValueForType(string type) {
204                         //print("guessDefaultValueForType: %s\n", type);
205                         if (type.length < 1 || type.contains(".")) {
206                                 return "null";
207                         }
208                         switch(type) {
209                                 case "gboolean":
210                                         return "true";
211                                 case "guint":
212                                         return "0";
213                                 case "utf8":
214                                         return "\"\"";
215                                 default:
216                                         return "?"+  type + "?";
217                         }
218
219                 }
220
221                 
222
223
224
225                  
226         }       
227
228         
229 }