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 return ++this._version; // increased on every call? - bit of a kludge until we do real versioning
85 this._version = value;
90 public string permname;
91 public string language;
92 public string content_type;
93 public string modOrder;
95 public uint64 webkit_page_id; // set by webkit view - used to extract extension/etc..
96 public bool gen_extended = false; // nodetovala??
98 public Project.Project project;
102 public string build_module; // module to build if we compile (or are running tests...)
104 //Project : false, // link to container project!
106 public Node tree; // the tree of nodes.
108 //public GLib.List<JsRender> cn; // child files.. (used by project ... should move code here..)
110 public bool hasParent;
114 public Gee.HashMap<string,string> transStrings; // map of md5 -> string.
115 public Gee.HashMap<string,string> namedStrings;
116 public Gee.HashMap<string, GLib.ListStore> errorsByType;
120 public signal void changed (Node? node, string source);
123 public signal void compile_notice(string type, string file, int line, string message);
125 private GLib.Icon? _icon = null;
127 public GLib.Icon? icon {
130 if (this._icon != null) {
134 if (this.path == "") {
137 if (!GLib.FileUtils.test(this.path, GLib.FileTest.EXISTS)) {
140 this._icon = File.new_for_path(this.path).query_info("standard::icon",GLib.FileQueryInfoFlags.NONE).get_icon();
149 //public Xcls_Editor editor;
150 public GLib.ListStore childfiles; // used by directories..
153 //abstract JsRender(Project.Project project, string path);
155 protected JsRender(Project.Project project, string path)
158 //this.cn = new GLib.List<JsRender>();
159 GLib.debug("new jsrender %s", path);
161 this.project = project;
162 this.hasParent = false;
170 this.content_type = "";
171 this.build_module = "";
172 //this.loaded = false;
173 //print("JsRender.cto() - reset transStrings\n");
174 this.transStrings = new Gee.HashMap<string,string>();
175 this.namedStrings = new Gee.HashMap<string,string>();
176 // should use basename reallly...
178 var ar = this.path.split("/");
179 // name is in theory filename without .bjs (or .js eventually...)
181 Regex regex = new Regex ("\\.(bjs)$");
183 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
184 } catch (GLib.Error e) {
187 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
189 this.doubleStringProps = new Gee.ArrayList<string>();
190 this.childfiles = new GLib.ListStore(typeof(JsRender));
191 this.errorsByType = new Gee.HashMap<string, GLib.ListStore>();
198 public void renameTo(string name) throws Error
200 if (this.xtype == "PlainFile") {
203 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
204 if (FileUtils.test(bjs, FileTest.EXISTS)) {
205 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
207 GLib.FileUtils.remove(this.path);
209 // remove other files?
218 // not sure why xt is needed... -> project contains xtype..
220 public static JsRender factory(string xt, Project.Project project, string path) throws Error
225 return new Gtk(project, path);
228 return new Roo((Project.Roo) project, path);
230 // return new Flutter(project, path);
232 return new PlainFile(project, path);
234 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
240 public string nickType()
242 var ar = this.name.split(".");
244 for (var i =0; i < ar.length -1; i++) {
247 return string.joinv(".", ret);
250 public string nickName()
252 var ar = this.name.split(".");
253 return ar[ar.length-1];
257 public string nickNameSplit()
259 var n = this.nickName();
262 for (var i = 0; i < n.length; i++) {
263 if (i!=0 && n.get(i).isupper() && len > 10) {
267 ret += n.get(i).to_string();
277 Gdk.Pixbuf screenshot = null;
278 Gdk.Pixbuf screenshot92 = null;
279 Gdk.Pixbuf screenshot368 = null;
281 public Gdk.Pixbuf? getIcon(int size = 0) {
282 var fname = this.getIconFileName( );
283 if (!FileUtils.test(fname, FileTest.EXISTS)) {
284 GLib.debug("PIXBUF %s: %s does not exist?", this.name, fname);
290 if (this.screenshot == null) {
292 this.screenshot = new Gdk.Pixbuf.from_file(fname);
293 } catch (GLib.Error e) {}
295 return this.screenshot;
299 if (this.screenshot == null) {
301 if (this.screenshot == null) {
306 this.screenshot92 = this.screenshot.scale_simple(92, (int) (this.screenshot.height * 92.0 /this.screenshot.width * 1.0 )
307 , Gdk.InterpType.NEAREST) ;
308 return this.screenshot92;
311 if (this.screenshot == null) {
313 if (this.screenshot == null) {
318 this.screenshot368 = this.screenshot.scale_simple(368, (int) (this.screenshot.height * 368.0 /this.screenshot.width * 1.0 )
319 , Gdk.InterpType.NEAREST) ;
320 return this.screenshot368;
325 public void writeIcon(Gdk.Pixbuf pixbuf) {
327 this.screenshot92 = null;
328 this.screenshot368 = null;
329 this.screenshot = null;
331 GLib.debug("Wirte %s", this.getIconFileName( ));
332 pixbuf.save(this.getIconFileName( ),"png");
333 this.screenshot = pixbuf;
335 } catch (GLib.Error e) {
336 GLib.debug("failed to write pixbuf?");
344 public void widgetToIcon(global::Gtk.Widget widget) {
346 this.screenshot92 = null;
347 this.screenshot368 = null;
348 this.screenshot = null;
353 var filename = this.getIconFileName();
356 var p = new global::Gtk.WidgetPaintable(widget);
357 var s = new global::Gtk.Snapshot();
358 GLib.debug("Width %d, Height %d", widget.get_width(), widget.get_height());
359 p.snapshot(s, widget.get_width(), widget.get_height());
360 var n = s.free_to_node();
364 var r = new Gsk.CairoRenderer();
366 var t = r.render_texture(n,null);
367 GLib.debug("write to %s", filename);
368 t.save_to_png(filename);
372 } catch (GLib.Error e) {
373 GLib.debug("failed to write pixbuf?");
383 public string getIconFileName( )
386 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
388 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
390 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
391 File.new_for_path(dir).make_directory();
393 } catch (GLib.Error e) {
394 // eakk.. what to do here...
396 var fname = dir + "/" + m5 + ".png";
403 public string toJsonString()
405 if (this.xtype == "PlainFile") {
408 var node = new Json.Node(Json.NodeType.OBJECT);
409 node.set_object(this.toJsonObject());
410 var generator = new JsonGen(node);
411 generator.indent = 1;
412 generator.pretty = true;
415 return generator.to_data();
418 public void saveBJS()
420 // if (!this.loaded) {
421 /// GLib.debug("saveBJS - skip - not loaded?");
424 if (this.xtype == "PlainFile") {
430 GLib.debug("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
432 this.writeFile(this.path, this.toJsonString());
434 } catch(GLib.Error e) {
435 print("Save failed");
446 public string jsonHasOrEmpty(Json.Object obj, string key) {
447 return obj.has_member(key) ?
448 obj.get_string_member(key) : "";
452 public Json.Object toJsonObject ()
456 var ret = new Json.Object();
457 if (this.xtype == "PlainFile") {
461 //ret.set_string_member("id", this.id); // not relivant..
462 ret.set_string_member("name", this.name);
464 if (this.project.xtype == "Roo") {
465 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
466 ret.set_string_member("title", this.title == null ? "" : this.title);
467 //ret.set_string_member("path", this.path);
468 //ret.set_string_member("items", this.items);
469 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
470 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
472 if (this.project.xtype == "Gtk") {
474 ret.set_string_member("build_module", this.build_module );
476 ret.set_boolean_member("gen_extended", this.gen_extended);
478 if (this.transStrings.size > 0) {
479 var tr = new Json.Object();
480 var iter = this.transStrings.map_iterator();
481 while (iter.next()) {
482 tr.set_string_member(iter.get_value(), iter.get_key());
484 ret.set_object_member("strings", tr);
489 if (this.namedStrings.size > 0) {
490 var tr = new Json.Object();
491 var iter = this.namedStrings.map_iterator();
492 while (iter.next()) {
493 tr.set_string_member(iter.get_key(), iter.get_value());
495 ret.set_object_member("named_strings", tr);
498 var ar = new Json.Array();
499 // empty files do not have a tree.
500 if (this.tree != null) {
501 ar.add_object_element(this.tree.toJsonObject());
503 ret.set_array_member("items", ar);
510 public string getTitle ()
512 if (this.title == null) { // not sure why this happens..
515 if (this.title.length > 0) {
518 var a = this.path.split("/");
519 return a[a.length-1];
521 public string getTitleTip()
523 if (this.title.length > 0) {
524 return "<b>" + this.title + "</b> " + this.path;
531 this.cn.sort(function(a,b) {
532 return a.path > b.path;// ? 1 : -1;
536 // should be in palete provider really..
539 public Palete.Palete palete()
541 // error on plainfile?
542 return this.project.palete;
546 public string guessName(Node ar) // turns the object into full name.
548 // eg. xns: Roo, xtype: XXX -> Roo.xxx
549 if (!ar.hasXnsType()) {
553 return ar.get("xns") + "." + ar.get("xtype");
558 * non-atomic write (replacement for put contents, as it creates temporary files.
560 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
564 var f = GLib.File.new_for_path(path);
565 var data_out = new GLib.DataOutputStream(
566 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
568 data_out.put_string(contents, null);
569 data_out.close(null);
574 public Node? lineToNode(int line)
576 if (this.tree == null) {
579 return this.tree.lineToNode(line);
584 public GLib.ListStore toListStore()
586 var ret = new GLib.ListStore(typeof(Node));
587 ret.append(this.tree);
592 // used to handle list of files in project editor (really Gtk only)
593 public bool compile_group_selected {
595 var gproj = (Project.Gtk) this.project;
597 if (gproj.active_cg == null) {
600 if (this.xtype == "Dir") {
601 // show ticked if all ticked..
603 for(var i = 0; i < this.childfiles.n_items; i++ ) {
604 var f = (JsRender) this.childfiles.get_item(i);
605 if (!f.compile_group_selected) {
614 if (gproj.active_cg.sources == null) {
615 GLib.debug("compile_group_selected - sources is null? ");
619 return gproj.active_cg.sources.contains(this.relpath);
624 var gproj = (Project.Gtk) this.project;
626 if (gproj.active_cg == null) {
629 if (gproj.active_cg.loading_ui) {
633 if (this.xtype == "Dir") {
634 for(var i = 0; i < this.childfiles.n_items; i++ ) {
635 var f = (JsRender) this.childfiles.get_item(i);
636 f.compile_group_selected = value;
645 if (value == false) {
646 GLib.debug("REMOVE %s", this.relpath);
648 gproj.active_cg.sources.remove(this.relpath);
651 if (!gproj.active_cg.sources.contains(this.relpath)) {
652 GLib.debug("ADD %s", this.relpath);
653 gproj.active_cg.sources.add(this.relpath);
659 public bool compile_group_hidden {
661 var gproj = (Project.Gtk) this.project;
664 return gproj.hidden.contains(this.relpath);
669 var gproj = (Project.Gtk) this.project;
671 if (gproj.active_cg == null) {
674 if (gproj.active_cg.loading_ui) {
677 if (value == false) {
678 GLib.debug("REMOVE %s", this.relpath);
680 gproj.hidden.remove(this.relpath);
683 if (!gproj.hidden.contains(this.relpath)) {
684 gproj.hidden.add(this.relpath);
685 // hiding a project will auto clear it.
686 for(var i = 0; i < this.childfiles.n_items; i++ ) {
687 var f = (JsRender) this.childfiles.get_item(i);
688 f.compile_group_selected = false;
699 if (this.xtype == "Dir") {
702 // cleans up build (should really be in children..
703 this.removeFile(this.path);
704 if (this.path.has_suffix(".bjs") && this.project.xtype == "Roo") {
705 this.removeFile(this.path.substring(0, this.path.length-4) + ".js");
708 if (this.path.has_suffix(".bjs") && this.project.xtype == "Gtk") {
709 this.removeFile(this.path.substring(0, this.path.length-4) + ".vala");
710 this.removeFile(this.path.substring(0, this.path.length-4) + ".c");
711 this.removeFile(this.path.substring(0, this.path.length-4) + ".o");
713 if (this.path.has_suffix(".vala") && this.project.xtype == "Gtk") {
714 this.removeFile(this.path.substring(0, this.path.length-5) + ".c");
715 this.removeFile(this.path.substring(0, this.path.length-5) + ".o");
721 private void removeFile(string path)
724 if (GLib.FileUtils.test(path, GLib.FileTest.EXISTS)) {
725 GLib.FileUtils.unlink(path);
729 public string relTargetName()
731 return this.targetName().substring(this.project.path.length +1);
735 public string to_url()
737 return File.new_for_path (this.targetName()).get_uri ();
739 public Palete.LanguageClient? getLanguageServer()
742 return this.project.getLanguageServer(this.language_id());
745 public GLib.ListStore getErrors(string n)
747 var ls = this.errorsByType.get(n);
749 ls = new GLib.ListStore(typeof(Palete.CompileError));
750 this.errorsByType.set(n, ls );
756 public abstract string language_id();
757 public abstract void save();
758 public abstract void saveHTML(string html);
759 public abstract string toSource() ;
760 public abstract string toSourceCode() ; // used by commandline tester..
761 public abstract void setSource(string str);
762 public abstract string toSourcePreview() ;
763 public abstract void removeFiles() ;
764 public abstract void findTransStrings(Node? node );
765 public abstract string toGlade();
766 public abstract string targetName();
767 public abstract void loadItems() throws GLib.Error;