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