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