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