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);
64 public string file_ext {
66 if (!this.path.contains(".")) {
69 var bits = this.name.split(".");
70 return bits[bits.length-1];
74 public string parent = ""; // JS parent.
75 public string region = ""; // RooJS - insert region.
77 public string title = ""; // a title.. ?? nickname.. ??? -
79 private int _version = 1; // should we increment this based on the node..?
82 if (this.tree != null) {
83 return this.tree.updated_count;
85 return ++this._version; // increased on every call? - bit of a kludge until we do real versioning
88 this._version = value;
93 public string permname;
94 public string language;
95 public string content_type;
96 public string modOrder;
98 public uint64 webkit_page_id; // set by webkit view - used to extract extension/etc..
99 public bool gen_extended = false; // nodetovala??
101 public Project.Project project;
105 public string build_module; // module to build if we compile (or are running tests...)
107 //Project : false, // link to container project!
109 public Node tree; // the tree of nodes.
111 //public GLib.List<JsRender> cn; // child files.. (used by project ... should move code here..)
113 public bool hasParent;
117 public Gee.HashMap<string,string> transStrings; // map of md5 -> string.
118 public Gee.HashMap<string,string> namedStrings;
119 public Gee.HashMap<string, GLib.ListStore> errorsByType;
123 public signal void changed (Node? node, string source);
126 public signal void compile_notice(string type, string file, int line, string message);
128 private GLib.Icon? _icon = null;
130 public GLib.Icon? icon {
133 if (this._icon != null) {
137 if (this.path == "") {
140 if (!GLib.FileUtils.test(this.path, GLib.FileTest.EXISTS)) {
143 this._icon = File.new_for_path(this.path).query_info("standard::icon",GLib.FileQueryInfoFlags.NONE).get_icon();
152 //public Xcls_Editor editor;
153 public GLib.ListStore childfiles; // used by directories..
156 //abstract JsRender(Project.Project project, string path);
158 protected JsRender(Project.Project project, string path)
161 //this.cn = new GLib.List<JsRender>();
162 GLib.debug("new jsrender %s", path);
164 this.project = project;
165 this.hasParent = false;
173 this.content_type = "";
174 this.build_module = "";
175 //this.loaded = false;
176 //print("JsRender.cto() - reset transStrings\n");
177 this.transStrings = new Gee.HashMap<string,string>();
178 this.namedStrings = new Gee.HashMap<string,string>();
179 // should use basename reallly...
181 var ar = this.path.split("/");
182 // name is in theory filename without .bjs (or .js eventually...)
184 Regex regex = new Regex ("\\.(bjs)$");
186 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
187 } catch (GLib.Error e) {
190 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
192 this.doubleStringProps = new Gee.ArrayList<string>();
193 this.childfiles = new GLib.ListStore(typeof(JsRender));
194 this.errorsByType = new Gee.HashMap<string, GLib.ListStore>();
201 public void renameTo(string name) throws Error
203 if (this.xtype == "PlainFile") {
206 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
207 if (FileUtils.test(bjs, FileTest.EXISTS)) {
208 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
210 GLib.FileUtils.remove(this.path);
212 // remove other files?
221 // not sure why xt is needed... -> project contains xtype..
223 public static JsRender factory(string xt, Project.Project project, string path) throws Error
228 return new Gtk(project, path);
231 return new Roo((Project.Roo) project, path);
233 // return new Flutter(project, path);
235 return new PlainFile(project, path);
237 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
243 public string nickType()
245 var ar = this.name.split(".");
247 for (var i =0; i < ar.length -1; i++) {
250 return string.joinv(".", ret);
253 public string nickName()
255 var ar = this.name.split(".");
256 return ar[ar.length-1];
260 public string nickNameSplit()
262 var n = this.nickName();
265 for (var i = 0; i < n.length; i++) {
266 if (i!=0 && n.get(i).isupper() && len > 10) {
270 ret += n.get(i).to_string();
280 Gdk.Pixbuf screenshot = null;
281 Gdk.Pixbuf screenshot92 = null;
282 Gdk.Pixbuf screenshot368 = null;
284 public Gdk.Pixbuf? getIcon(int size = 0) {
285 var fname = this.getIconFileName( );
286 if (!FileUtils.test(fname, FileTest.EXISTS)) {
287 GLib.debug("PIXBUF %s: %s does not exist?", this.name, fname);
293 if (this.screenshot == null) {
295 this.screenshot = new Gdk.Pixbuf.from_file(fname);
296 } catch (GLib.Error e) {}
298 return this.screenshot;
302 if (this.screenshot == null) {
304 if (this.screenshot == null) {
309 this.screenshot92 = this.screenshot.scale_simple(92, (int) (this.screenshot.height * 92.0 /this.screenshot.width * 1.0 )
310 , Gdk.InterpType.NEAREST) ;
311 return this.screenshot92;
314 if (this.screenshot == null) {
316 if (this.screenshot == null) {
321 this.screenshot368 = this.screenshot.scale_simple(368, (int) (this.screenshot.height * 368.0 /this.screenshot.width * 1.0 )
322 , Gdk.InterpType.NEAREST) ;
323 return this.screenshot368;
328 public void writeIcon(Gdk.Pixbuf pixbuf) {
330 this.screenshot92 = null;
331 this.screenshot368 = null;
332 this.screenshot = null;
334 GLib.debug("Wirte %s", this.getIconFileName( ));
335 pixbuf.save(this.getIconFileName( ),"png");
336 this.screenshot = pixbuf;
338 } catch (GLib.Error e) {
339 GLib.debug("failed to write pixbuf?");
347 public void widgetToIcon(global::Gtk.Widget widget) {
349 this.screenshot92 = null;
350 this.screenshot368 = null;
351 this.screenshot = null;
356 var filename = this.getIconFileName();
359 var p = new global::Gtk.WidgetPaintable(widget);
360 var s = new global::Gtk.Snapshot();
361 GLib.debug("Width %d, Height %d", widget.get_width(), widget.get_height());
362 p.snapshot(s, widget.get_width(), widget.get_height());
363 var n = s.free_to_node();
367 var r = new Gsk.CairoRenderer();
369 var t = r.render_texture(n,null);
370 GLib.debug("write to %s", filename);
371 t.save_to_png(filename);
375 } catch (GLib.Error e) {
376 GLib.debug("failed to write pixbuf?");
386 public string getIconFileName( )
389 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
391 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
393 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
394 File.new_for_path(dir).make_directory();
396 } catch (GLib.Error e) {
397 // eakk.. what to do here...
399 var fname = dir + "/" + m5 + ".png";
406 public string toJsonString()
408 if (this.xtype == "PlainFile") {
411 var node = new Json.Node(Json.NodeType.OBJECT);
412 node.set_object(this.toJsonObject());
413 var generator = new JsonGen(node);
414 generator.indent = 1;
415 generator.pretty = true;
418 return generator.to_data();
421 public void saveBJS()
423 // if (!this.loaded) {
424 /// GLib.debug("saveBJS - skip - not loaded?");
427 if (this.xtype == "PlainFile") {
433 GLib.debug("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
435 this.writeFile(this.path, this.toJsonString());
437 } catch(GLib.Error e) {
438 print("Save failed");
449 public string jsonHasOrEmpty(Json.Object obj, string key) {
450 return obj.has_member(key) ?
451 obj.get_string_member(key) : "";
455 public Json.Object toJsonObject ()
459 var ret = new Json.Object();
460 if (this.xtype == "PlainFile") {
464 //ret.set_string_member("id", this.id); // not relivant..
465 ret.set_string_member("name", this.name);
467 if (this.project.xtype == "Roo") {
468 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
469 ret.set_string_member("title", this.title == null ? "" : this.title);
470 //ret.set_string_member("path", this.path);
471 //ret.set_string_member("items", this.items);
472 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
473 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
475 if (this.project.xtype == "Gtk") {
477 ret.set_string_member("build_module", this.build_module );
479 ret.set_boolean_member("gen_extended", this.gen_extended);
481 if (this.transStrings.size > 0) {
482 var tr = new Json.Object();
483 var iter = this.transStrings.map_iterator();
484 while (iter.next()) {
485 tr.set_string_member(iter.get_value(), iter.get_key());
487 ret.set_object_member("strings", tr);
492 if (this.namedStrings.size > 0) {
493 var tr = new Json.Object();
494 var iter = this.namedStrings.map_iterator();
495 while (iter.next()) {
496 tr.set_string_member(iter.get_key(), iter.get_value());
498 ret.set_object_member("named_strings", tr);
501 var ar = new Json.Array();
502 // empty files do not have a tree.
503 if (this.tree != null) {
504 ar.add_object_element(this.tree.toJsonObject());
506 ret.set_array_member("items", ar);
513 public string getTitle ()
515 if (this.title == null) { // not sure why this happens..
518 if (this.title.length > 0) {
521 var a = this.path.split("/");
522 return a[a.length-1];
524 public string getTitleTip()
526 if (this.title.length > 0) {
527 return "<b>" + this.title + "</b> " + this.path;
534 this.cn.sort(function(a,b) {
535 return a.path > b.path;// ? 1 : -1;
539 // should be in palete provider really..
542 public Palete.Palete palete()
544 // error on plainfile?
545 return this.project.palete;
549 public string guessName(Node ar) // turns the object into full name.
551 // eg. xns: Roo, xtype: XXX -> Roo.xxx
552 if (!ar.hasXnsType()) {
556 return ar.get("xns") + "." + ar.get("xtype");
561 * non-atomic write (replacement for put contents, as it creates temporary files.
563 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
567 var f = GLib.File.new_for_path(path);
568 var data_out = new GLib.DataOutputStream(
569 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
571 data_out.put_string(contents, null);
572 data_out.close(null);
577 public Node? lineToNode(int line)
579 if (this.tree == null) {
582 return this.tree.lineToNode(line);
587 public GLib.ListStore toListStore()
589 var ret = new GLib.ListStore(typeof(Node));
590 ret.append(this.tree);
595 // used to handle list of files in project editor (really Gtk only)
596 public bool compile_group_selected {
598 var gproj = (Project.Gtk) this.project;
600 if (gproj.active_cg == null) {
603 if (this.xtype == "Dir") {
604 // show ticked if all ticked..
606 for(var i = 0; i < this.childfiles.n_items; i++ ) {
607 var f = (JsRender) this.childfiles.get_item(i);
608 if (!f.compile_group_selected) {
617 if (gproj.active_cg.sources == null) {
618 GLib.debug("compile_group_selected - sources is null? ");
622 return gproj.active_cg.sources.contains(this.relpath);
627 var gproj = (Project.Gtk) this.project;
629 if (gproj.active_cg == null) {
632 if (gproj.active_cg.loading_ui) {
636 if (this.xtype == "Dir") {
637 for(var i = 0; i < this.childfiles.n_items; i++ ) {
638 var f = (JsRender) this.childfiles.get_item(i);
639 f.compile_group_selected = value;
648 if (value == false) {
649 GLib.debug("REMOVE %s", this.relpath);
651 gproj.active_cg.sources.remove(this.relpath);
654 if (!gproj.active_cg.sources.contains(this.relpath)) {
655 GLib.debug("ADD %s", this.relpath);
656 gproj.active_cg.sources.add(this.relpath);
662 public bool compile_group_hidden {
664 var gproj = (Project.Gtk) this.project;
667 return gproj.hidden.contains(this.relpath);
672 var gproj = (Project.Gtk) this.project;
674 if (gproj.active_cg == null) {
677 if (gproj.active_cg.loading_ui) {
680 if (value == false) {
681 GLib.debug("REMOVE %s", this.relpath);
683 gproj.hidden.remove(this.relpath);
686 if (!gproj.hidden.contains(this.relpath)) {
687 gproj.hidden.add(this.relpath);
688 // hiding a project will auto clear it.
689 for(var i = 0; i < this.childfiles.n_items; i++ ) {
690 var f = (JsRender) this.childfiles.get_item(i);
691 f.compile_group_selected = false;
702 if (this.xtype == "Dir") {
705 // cleans up build (should really be in children..
706 this.removeFile(this.path);
707 if (this.path.has_suffix(".bjs") && this.project.xtype == "Roo") {
708 this.removeFile(this.path.substring(0, this.path.length-4) + ".js");
711 if (this.path.has_suffix(".bjs") && this.project.xtype == "Gtk") {
712 this.removeFile(this.path.substring(0, this.path.length-4) + ".vala");
713 this.removeFile(this.path.substring(0, this.path.length-4) + ".c");
714 this.removeFile(this.path.substring(0, this.path.length-4) + ".o");
716 if (this.path.has_suffix(".vala") && this.project.xtype == "Gtk") {
717 this.removeFile(this.path.substring(0, this.path.length-5) + ".c");
718 this.removeFile(this.path.substring(0, this.path.length-5) + ".o");
724 private void removeFile(string path)
727 if (GLib.FileUtils.test(path, GLib.FileTest.EXISTS)) {
728 GLib.FileUtils.unlink(path);
732 public string relTargetName()
734 return this.targetName().substring(this.project.path.length +1);
738 public string to_url()
740 return File.new_for_path (this.targetName()).get_uri ();
742 public Palete.LanguageClient? getLanguageServer()
745 return this.project.getLanguageServer(this.language_id());
748 public GLib.ListStore getErrors(string n)
750 var ls = this.errorsByType.get(n);
752 ls = new GLib.ListStore(typeof(Palete.CompileError));
753 this.errorsByType.set(n, ls );
759 public abstract string language_id();
760 public abstract void save();
761 public abstract void saveHTML(string html);
762 public abstract string toSource() ;
763 public abstract string toSourceCode() ; // used by commandline tester..
764 public abstract void setSource(string str);
765 public abstract string toSourcePreview() ;
766 public abstract void removeFiles() ;
767 public abstract void findTransStrings(Node? node );
768 public abstract string toGlade();
769 public abstract string targetName();
770 public abstract void loadItems() throws GLib.Error;