1 //<Script type="text/javascript">
6 public errordomain Error {
11 public abstract class JsRender : Object {
13 * @cfg {Array} doubleStringProps list of properties that can be double quoted.
15 public Gee.ArrayList<string> doubleStringProps;
17 public string id = "";
18 public string name { get; set; default = ""; } // is the JS name of the file.
19 public string fullname = "";
20 public string path = ""; // is the full path to the file.
22 public string relpath {
24 return this.project.path == this.path ? "" : this.path.substring(this.project.path.length+1);
28 public string reldir {
30 return this.project.path == this.dir ? "" : this. dir.substring(this.project.path.length+1);
37 return GLib.Path.get_dirname(this.path);
43 public string file_namespace {
45 if (!this.name.contains(".")) {
48 var bits = this.name.split(".");
53 public string file_without_namespace {
55 if (!this.name.contains(".")) {
58 var bits = this.name.split(".");
59 return this.name.substring(bits[0].length +1);
65 public string parent = ""; // JS parent.
66 public string region = ""; // RooJS - insert region.
68 public string title = ""; // a title.. ?? nickname.. ??? -
72 public string permname;
73 public string language;
74 public string content_type;
75 public string modOrder;
77 public uint64 webkit_page_id; // set by webkit view - used to extract extension/etc..
78 public bool gen_extended = false; // nodetovala??
80 public Project.Project project;
84 public string build_module; // module to build if we compile (or are running tests...)
86 //Project : false, // link to container project!
88 public Node tree; // the tree of nodes.
90 //public GLib.List<JsRender> cn; // child files.. (used by project ... should move code here..)
92 public bool hasParent;
96 public Gee.HashMap<string,string> transStrings; // map of md5 -> string.
97 public Gee.HashMap<string,string> namedStrings;
99 public signal void changed (Node? node, string source);
102 public signal void compile_notice(string type, string file, int line, string message);
107 //public Xcls_Editor editor;
108 public GLib.ListStore childfiles; // used by directories..
111 //abstract JsRender(Project.Project project, string path);
113 public void aconstruct(Project.Project project, string path)
116 //this.cn = new GLib.List<JsRender>();
118 this.project = project;
119 this.hasParent = false;
127 this.content_type = "";
128 this.build_module = "";
129 //this.loaded = false;
130 //print("JsRender.cto() - reset transStrings\n");
131 this.transStrings = new Gee.HashMap<string,string>();
132 this.namedStrings = new Gee.HashMap<string,string>();
133 // should use basename reallly...
135 var ar = this.path.split("/");
136 // name is in theory filename without .bjs (or .js eventually...)
138 Regex regex = new Regex ("\\.(bjs)$");
140 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
141 } catch (GLib.Error e) {
144 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
146 this.doubleStringProps = new Gee.ArrayList<string>();
147 this.childfiles = new GLib.ListStore(typeof(JsRender));
151 public void renameTo(string name) throws Error
153 if (this.xtype == "PlainFile") {
156 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
157 if (FileUtils.test(bjs, FileTest.EXISTS)) {
158 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
160 GLib.FileUtils.remove(this.path);
162 // remove other files?
171 // not sure why xt is needed... -> project contains xtype..
173 public static JsRender factory(string xt, Project.Project project, string path) throws Error
178 return new Gtk(project, path);
181 return new Roo((Project.Roo) project, path);
183 // return new Flutter(project, path);
185 return new PlainFile(project, path);
187 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
193 public string nickType()
195 var ar = this.name.split(".");
197 for (var i =0; i < ar.length -1; i++) {
200 return string.joinv(".", ret);
203 public string nickName()
205 var ar = this.name.split(".");
206 return ar[ar.length-1];
210 public string nickNameSplit()
212 var n = this.nickName();
215 for (var i = 0; i < n.length; i++) {
216 if (i!=0 && n.get(i).isupper() && len > 10) {
220 ret += n.get(i).to_string();
230 Gdk.Pixbuf screenshot = null;
231 Gdk.Pixbuf screenshot92 = null;
232 Gdk.Pixbuf screenshot368 = null;
234 public Gdk.Pixbuf? getIcon(int size = 0) {
235 var fname = this.getIconFileName( );
236 if (!FileUtils.test(fname, FileTest.EXISTS)) {
237 GLib.debug("PIXBUF %s: %s does not exist?", this.name, fname);
243 if (this.screenshot == null) {
245 this.screenshot = new Gdk.Pixbuf.from_file(fname);
246 } catch (GLib.Error e) {}
248 return this.screenshot;
252 if (this.screenshot == null) {
254 if (this.screenshot == null) {
259 this.screenshot92 = this.screenshot.scale_simple(92, (int) (this.screenshot.height * 92.0 /this.screenshot.width * 1.0 )
260 , Gdk.InterpType.NEAREST) ;
261 return this.screenshot92;
264 if (this.screenshot == null) {
266 if (this.screenshot == null) {
271 this.screenshot368 = this.screenshot.scale_simple(368, (int) (this.screenshot.height * 368.0 /this.screenshot.width * 1.0 )
272 , Gdk.InterpType.NEAREST) ;
273 return this.screenshot368;
278 public void writeIcon(Gdk.Pixbuf pixbuf) {
280 this.screenshot92 = null;
281 this.screenshot368 = null;
282 this.screenshot = null;
284 GLib.debug("Wirte %s", this.getIconFileName( ));
285 pixbuf.save(this.getIconFileName( ),"png");
286 this.screenshot = pixbuf;
288 } catch (GLib.Error e) {
289 GLib.debug("failed to write pixbuf?");
297 public void widgetToIcon(global::Gtk.Widget widget) {
299 this.screenshot92 = null;
300 this.screenshot368 = null;
301 this.screenshot = null;
306 var filename = this.getIconFileName();
309 var p = new global::Gtk.WidgetPaintable(widget);
310 var s = new global::Gtk.Snapshot();
311 GLib.debug("Width %d, Height %d", widget.get_width(), widget.get_height());
312 p.snapshot(s, widget.get_width(), widget.get_height());
313 var n = s.free_to_node();
314 var r = new Gsk.CairoRenderer();
316 var t = r.render_texture(n,null);
317 GLib.debug("write to %s", filename);
318 t.save_to_png(filename);
322 } catch (GLib.Error e) {
323 GLib.debug("failed to write pixbuf?");
333 public string getIconFileName( )
336 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
338 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
340 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
341 File.new_for_path(dir).make_directory();
343 } catch (GLib.Error e) {
344 // eakk.. what to do here...
346 var fname = dir + "/" + m5 + ".png";
353 public string toJsonString()
355 if (this.xtype == "PlainFile") {
358 var node = new Json.Node(Json.NodeType.OBJECT);
359 node.set_object(this.toJsonObject());
360 var generator = new JsonGen(node);
361 generator.indent = 1;
362 generator.pretty = true;
365 return generator.to_data();
368 public void saveBJS()
370 // if (!this.loaded) {
371 /// GLib.debug("saveBJS - skip - not loaded?");
374 if (this.xtype == "PlainFile") {
380 GLib.debug("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
382 this.writeFile(this.path, this.toJsonString());
384 } catch(GLib.Error e) {
385 print("Save failed");
396 public string jsonHasOrEmpty(Json.Object obj, string key) {
397 return obj.has_member(key) ?
398 obj.get_string_member(key) : "";
402 public Json.Object toJsonObject ()
406 var ret = new Json.Object();
407 if (this.xtype == "PlainFile") {
411 //ret.set_string_member("id", this.id); // not relivant..
412 ret.set_string_member("name", this.name);
414 if (this.project.xtype == "Roo") {
415 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
416 ret.set_string_member("title", this.title == null ? "" : this.title);
417 //ret.set_string_member("path", this.path);
418 //ret.set_string_member("items", this.items);
419 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
420 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
422 if (this.project.xtype == "Gtk") {
424 ret.set_string_member("build_module", this.build_module );
426 ret.set_boolean_member("gen_extended", this.gen_extended);
428 if (this.transStrings.size > 0) {
429 var tr = new Json.Object();
430 var iter = this.transStrings.map_iterator();
431 while (iter.next()) {
432 tr.set_string_member(iter.get_value(), iter.get_key());
434 ret.set_object_member("strings", tr);
439 if (this.namedStrings.size > 0) {
440 var tr = new Json.Object();
441 var iter = this.namedStrings.map_iterator();
442 while (iter.next()) {
443 tr.set_string_member(iter.get_key(), iter.get_value());
445 ret.set_object_member("named_strings", tr);
448 var ar = new Json.Array();
449 // empty files do not have a tree.
450 if (this.tree != null) {
451 ar.add_object_element(this.tree.toJsonObject());
453 ret.set_array_member("items", ar);
460 public string getTitle ()
462 if (this.title == null) { // not sure why this happens..
465 if (this.title.length > 0) {
468 var a = this.path.split("/");
469 return a[a.length-1];
471 public string getTitleTip()
473 if (this.title.length > 0) {
474 return "<b>" + this.title + "</b> " + this.path;
481 this.cn.sort(function(a,b) {
482 return a.path > b.path;// ? 1 : -1;
486 // should be in palete provider really..
489 public Palete.Palete palete()
491 // error on plainfile?
492 return this.project.palete;
496 public string guessName(Node ar) // turns the object into full name.
498 // eg. xns: Roo, xtype: XXX -> Roo.xxx
499 if (!ar.hasXnsType()) {
503 return ar.get("xns") + "." + ar.get("xtype");
508 * non-atomic write (replacement for put contents, as it creates temporary files.
510 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
514 var f = GLib.File.new_for_path(path);
515 var data_out = new GLib.DataOutputStream(
516 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
518 data_out.put_string(contents, null);
519 data_out.close(null);
524 public Node? lineToNode(int line)
526 if (this.tree == null) {
529 return this.tree.lineToNode(line);
534 public GLib.ListStore toListStore()
536 var ret = new GLib.ListStore(typeof(Node));
537 ret.append(this.tree);
542 // used to handle list of files in project editor (really Gtk only)
543 public bool compile_group_selected {
545 var gproj = (Project.Gtk) this.project;
547 if (gproj.active_cg == null) {
550 if (this.xtype == "Dir") {
551 // show ticked if all ticked..
553 for(var i = 0; i < this.childfiles.n_items; i++ ) {
554 var f = (JsRender) this.childfiles.get_item(i);
555 if (!f.compile_group_selected) {
564 if (gproj.active_cg.sources == null) {
565 GLib.debug("compile_group_selected - sources is null? ");
569 return gproj.active_cg.sources.contains(this.relpath);
574 var gproj = (Project.Gtk) this.project;
576 if (gproj.active_cg == null) {
579 if (gproj.active_cg.loading_ui) {
583 if (this.xtype == "Dir") {
584 for(var i = 0; i < this.childfiles.n_items; i++ ) {
585 var f = (JsRender) this.childfiles.get_item(i);
586 f.compile_group_selected = value;
595 if (value == false) {
596 GLib.debug("REMOVE %s", this.relpath);
598 gproj.active_cg.sources.remove(this.relpath);
601 if (!gproj.active_cg.sources.contains(this.relpath)) {
602 GLib.debug("ADD %s", this.relpath);
603 gproj.active_cg.sources.add(this.relpath);
609 public bool compile_group_hidden {
611 var gproj = (Project.Gtk) this.project;
614 return gproj.hidden.contains(this.relpath);
619 var gproj = (Project.Gtk) this.project;
621 if (gproj.active_cg == null) {
624 if (gproj.active_cg.loading_ui) {
627 if (value == false) {
628 GLib.debug("REMOVE %s", this.relpath);
630 gproj.hidden.remove(this.relpath);
633 if (!gproj.hidden.contains(this.relpath)) {
634 gproj.hidden.add(this.relpath);
635 // hiding a project will auto clear it.
636 for(var i = 0; i < this.childfiles.n_items; i++ ) {
637 var f = (JsRender) this.childfiles.get_item(i);
638 f.compile_group_selected = false;
649 if (this.xtype == "Dir") {
652 // cleans up build (should really be in children..
653 this.removeFile(this.path);
654 if (this.path.has_suffix(".bjs") && this.project.xtype == "Roo") {
655 this.removeFile(this.path.substring(0, this.path.length-4) + ".js");
658 if (this.path.has_suffix(".bjs") && this.project.xtype == "Gtk") {
659 this.removeFile(this.path.substring(0, this.path.length-4) + ".vala");
660 this.removeFile(this.path.substring(0, this.path.length-4) + ".c");
661 this.removeFile(this.path.substring(0, this.path.length-4) + ".o");
663 if (this.path.has_suffix(".vala") && this.project.xtype == "Gtk") {
664 this.removeFile(this.path.substring(0, this.path.length-5) + ".c");
665 this.removeFile(this.path.substring(0, this.path.length-5) + ".o");
671 private void removeFile(string path)
674 if (GLib.FileUtils.test(path, GLib.FileTest.EXISTS)) {
675 GLib.FileUtils.unlink(path);
681 public abstract void save();
682 public abstract void saveHTML(string html);
683 public abstract string toSource() ;
684 public abstract string toSourceCode() ; // used by commandline tester..
685 public abstract void setSource(string str);
686 public abstract string toSourcePreview() ;
687 public abstract void removeFiles() ;
688 public abstract void findTransStrings(Node? node );
689 public abstract string toGlade();
690 public abstract string targetName();
691 public abstract void loadItems() throws GLib.Error;