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                 public void loadOverrides(bool force = false)
52                 {
53                         // BC..
54                 }
55                 
56                 /**
57                  *  == all static below here...
58                  * 
59                  */
60                 public static  Gee.HashMap<string,Gir> cache = null;
61
62                 
63                 public static Gir?  factory(string ns) 
64                 {
65                         if (cache == null) {
66                                 cache = new Gee.HashMap<string,Gir>();
67                                 var a = new VapiParser( );
68                                 a.create_valac_tree();
69                                   
70                         }
71                         var ret = cache.get(ns);
72                         
73                         
74                         /*
75                         if (ret == null) {
76
77                                 var add = new Gir(ns);
78                                 
79                                 cache.set(ns, add);
80                         
81                                 var iter = add.classes.map_iterator();
82                                 while(iter.next()) {
83                                         iter.get_value().overlayParent();
84                                 }
85                                 // loop again and add the ctor properties.
86                                 iter = add.classes.map_iterator();
87                                 while(iter.next()) {
88                                         iter.get_value().overlayCtorProperties();
89                                 }       
90
91                                 
92                                 ret = cache.get(ns);
93                         }
94                         */
95                         if (ret != null && !ret.is_overlaid) {
96                                 var iter = ret.classes.map_iterator();
97                                 while(iter.next()) {
98                                         iter.get_value().overlayParent();
99                                 }
100                                 // loop again and add the ctor properties.
101                                 iter = ret.classes.map_iterator();
102                                 while(iter.next()) {
103                                         iter.get_value().overlayCtorProperties();
104                                 }       
105                                 ret.is_overlaid = true;
106                                 
107                         }
108                         
109                         
110                          
111
112                         return ret;
113                         
114                 }
115                 public static GirObject?  factoryFqn(string fqn)  
116                 {       
117                         var bits = fqn.split(".");
118                         if (bits.length < 1) {
119                                 return null;
120                         }
121                         
122                         var f = (GirObject)factory(bits[0]);
123
124                         if (bits.length == 1 || f ==null) {
125                                 return f;
126                         }
127                         return f.fetchByFqn(fqn.substring(bits[0].length+1)); // since classes are stored in fqn format...?
128                                             
129                         
130                 }
131
132                         
133                 /**
134                  * guess the fqn of a type == eg. gboolean or Widget etc...
135                  */
136                 public static string fqtypeLookup(string type, string ns) {
137                         var g = factory(ns);
138                         if (g.classes.has_key(type)) {
139                                 return ns + "." + type;
140                         }
141                         // enums..
142                         if (g.consts.has_key(type)) {
143                                 return ns + "." + type;
144                         }
145                         
146                         
147                         // look at includes..
148                         var iter = g.includes.map_iterator();
149                         while(iter.next()) {
150                                 // skip empty namespaces on include..?
151                                 if ( iter.get_key() == "") {
152                                         continue;
153                                 }
154                                 var ret = fqtypeLookup(type, iter.get_key());
155                                 if (ret != type) {
156                                         return ret;
157                                 }
158                 }       
159                         return type;
160                 }
161                 
162
163
164                 
165                 // needed still - where's it called form..
166                 public static string guessDefaultValueForType(string type) {
167                         //print("guessDefaultValueForType: %s\n", type);
168                         if (type.length < 1 || type.contains(".")) {
169                                 return "null";
170                         }
171                         switch(type) {
172                                 case "gboolean":
173                                         return "true";
174                                 case "guint":
175                                         return "0";
176                                 case "utf8":
177                                         return "\"\"";
178                                 default:
179                                         return "?"+  type + "?";
180                         }
181
182                 }
183
184                 
185
186
187
188                  
189         }       
190
191         
192 }