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                         }
74                         var ret = cache.get(ns);
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
96                         return ret;
97                         
98                 }
99                 public static GirObject?  factoryFqn(string fqn)  
100                 {       
101                         var bits = fqn.split(".");
102                         if (bits.length < 1) {
103                                 return null;
104                         }
105                         
106                         var f = (GirObject)factory(bits[0]);
107
108                         if (bits.length == 1 || f ==null) {
109                                 return f;
110                         }
111                         return f.fetchByFqn(fqn.substring(bits[0].length+1)); // since classes are stored in fqn format...?
112                                             
113                         
114                 }
115
116                         
117                 /**
118                  * guess the fqn of a type == eg. gboolean or Widget etc...
119                  */
120                 public static string fqtypeLookup(string type, string ns) {
121                         var g = factory(ns);
122                         if (g.classes.has_key(type)) {
123                                 return ns + "." + type;
124                         }
125                         // enums..
126                         if (g.consts.has_key(type)) {
127                                 return ns + "." + type;
128                         }
129                         
130                         
131                         // look at includes..
132                         var iter = g.includes.map_iterator();
133                         while(iter.next()) {
134                                 // skip empty namespaces on include..?
135                                 if ( iter.get_key() == "") {
136                                         continue;
137                                 }
138                                 var ret = fqtypeLookup(type, iter.get_key());
139                                 if (ret != type) {
140                                         return ret;
141                                 }
142                 }       
143                         return type;
144                 }
145                 
146
147
148                 
149                 // needed still - where's it called form..
150                 public static string guessDefaultValueForType(string type) {
151                         //print("guessDefaultValueForType: %s\n", type);
152                         if (type.length < 1 || type.contains(".")) {
153                                 return "null";
154                         }
155                         switch(type) {
156                                 case "gboolean":
157                                         return "true";
158                                 case "guint":
159                                         return "0";
160                                 case "utf8":
161                                         return "\"\"";
162                                 default:
163                                         return "?"+  type + "?";
164                         }
165
166                 }
167
168
169                 
170
171
172
173                  
174         }       
175
176         
177 }