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