01425164711cbff060dbc167077ec7993c0c2e4a
[roobuilder] / src / JsRender / Gtk.vala
1 /**
2  * 
3  *  this is the code represents a File when using the Gtk view..
4  *   
5  *  It ues NodeToGtk
6  * 
7  * 
8  */
9
10 namespace JsRender {
11
12
13  
14         int gid = 1;
15
16   
17         public  class Gtk : JsRender
18         {
19            
20
21             public Gtk(Project.Project project, string path) {
22             
23                 aconstruct( project, path);
24                 this.xtype = "Gtk";
25                 this.language = "vala";
26                 
27                 
28                 //this.items = false;
29                 //if (cfg.json) {
30                 //    var jstr =  JSON.parse(cfg.json);
31                 //    this.items = [ jstr ];
32                 //    //console.log(cfg.items.length);
33                 //    delete cfg.json; // not needed!
34                 // }
35                  
36                 
37                 
38                 // super?!?!
39                 this.id = "file-gtk-%d".printf(gid++);
40                 //console.dump(this);
41                 // various loader methods..
42
43                 // Class = list of arguments ... and which property to use as a value.
44            
45
46                 
47                 
48             }
49               
50
51             /*
52             setNSID : function(id)
53             {
54                 
55                 this.items[0]['*class'] = id;
56                 
57                 
58             },
59             getType: function() {
60                 return 'Gtk';
61             },
62             */
63
64                 public   override void   removeFiles() {
65                         var js = GLib.Path.get_dirname(this.path) +"/" +  name + ".js";
66                         if (FileUtils.test(js, FileTest.EXISTS)) {
67                                 GLib.FileUtils.remove(js);
68                         }
69                         var vala = GLib.Path.get_dirname(this.path) +"/" + name + ".vala";
70                         if (FileUtils.test(vala, FileTest.EXISTS)) {
71                                 GLib.FileUtils.remove(vala);
72                         }
73                 }
74             
75                 public   override void  loadItems() throws GLib.Error // : function(cb, sync) == original was async.
76                 {
77                   
78                         print("load Items!\n");
79                         if (this.tree != null) {
80                                 this.loaded = true;
81                         
82                                 return;
83                         }
84                         /*
85                         print("load: %s\n" , this.path);
86                         if (!GLib.FileUtils.test(this.path, GLib.FileTest.EXISTS)) {
87                                 // new file?!?
88                                 this.tree = null;
89                                 this.loaded = true;
90                                 return;
91                         }
92                         */
93
94                         var pa = new Json.Parser();
95                         pa.load_from_file(this.path);
96                         var node = pa.get_root();
97                 
98                         if (node.get_node_type () != Json.NodeType.OBJECT) {
99                                 throw new Error.INVALID_FORMAT ("Unexpected element type %s", node.type_name ());
100                         }
101                         var obj = node.get_object ();
102                 
103                         this.name = obj.get_string_member("name");
104                         this.parent = obj.get_string_member("parent");
105                         this.title = obj.get_string_member("title");
106                 
107                         if (obj.has_member("build_module")) { // should check type really..
108                                 this.build_module = obj.get_string_member("build_module");
109                         }
110                          
111                         // load items[0] ??? into tree...
112                         var bjs_version_str = this.jsonHasOrEmpty(obj, "bjs-version");
113                         bjs_version_str = bjs_version_str == "" ? "1" : bjs_version_str;
114
115                         if (obj.has_member("items") 
116                                 && 
117                                 obj.get_member("items").get_node_type() == Json.NodeType.ARRAY
118                                 &&
119                                 obj.get_array_member("items").get_length() > 0
120                         ) {
121                                 var ar = obj.get_array_member("items");
122                                 var tree_base = ar.get_object_element(0);
123                                 this.tree = new Node();
124                                 this.tree.loadFromJson(tree_base, int.parse(bjs_version_str));
125
126                         }
127                         NodeToVala.mungeFile(this); // force line numbering..
128                         this.loaded = true;
129                 
130                 }
131              
132             
133         
134             public override string toSourcePreview()
135             {
136                         return "";
137                 }
138             public override void setSource(string str) {}
139             
140             public override string toSourceCode() // no seed support currently.
141             {
142                     return  NodeToVala.mungeFile(this);
143             }
144             
145             // this is only used by dumping code...
146             public override string toSource() // no seed support currently.
147             {
148                  
149                          return  NodeToVala.mungeFile(this);
150                 
151                 
152             }
153         
154             public override void save() {
155                 this.saveBJS();
156                 // this.saveJS(); - disabled at present.. project settings will probably enable this later..
157         
158                 this.saveVala();
159             }
160                 // ignore these calls.
161             public override void saveHTML ( string html ) {}
162                 
163                         
164             /** 
165              *  saveJS
166              * 
167              * save as a javascript file. - not used currently
168              * why is this not save...???
169              * 
170              
171               
172             void saveJS()
173             {
174                  
175                 var fn = GLib.Path.get_dirname(this.path) + "/" + this.name + ".js";
176                 print("WRITE :%s\n " , fn);
177                 this.writeFile(fn, this.toSource());
178                 
179             }
180             */
181             
182             public override string targetName()
183             {
184                 return GLib.Path.get_dirname(this.path) + "/" + this.name + ".vala";
185         }
186             
187             void  saveVala()
188             {
189                         if (this.tree == null) {
190                                 return;
191                         }
192                         var fn = this.targetName();
193                         GLib.debug("WRITE :%s\n " , fn);
194                         try {
195                                 this.writeFile(fn,  NodeToVala.mungeFile(this));
196                 } catch (GLib.Error e) {}
197                 
198                 
199             }
200                 /*
201             valaCompileCmd : function()
202             {
203                 
204                 var fn = '/tmp/' + this.name + '.vala';
205                 print("WRITE : " + fn);
206                 File.write(fn, this.toVala(true));
207                 
208                 
209                 
210                 return ["valac",
211                        "--pkg",  "gio-2.0",
212                        "--pkg" , "posix" ,
213                        "--pkg" , "gtk+-3.0",
214                        "--pkg",  "libnotify",
215                        "--pkg",  "gtksourceview-3.0",
216                        "--pkg", "libwnck-3.0",
217                        fn ,   "-o", "/tmp/" + this.name];
218                 
219                
220                  
221                 
222             },
223             */
224             
225    
226              
227             public override void  findTransStrings(Node? node )
228                 {
229                         // not yet..
230                 }
231
232                 public override string toGlade() 
233                 {
234                         return  NodeToGlade.mungeFile(this);
235                 }
236           
237
238         
239
240         }
241 }
242
243
244