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.. ??? -
81 public string permname;
82 public string language;
83 public string content_type;
84 public string modOrder;
86 public uint64 webkit_page_id; // set by webkit view - used to extract extension/etc..
87 public bool gen_extended = false; // nodetovala??
89 public Project.Project project;
93 public string build_module; // module to build if we compile (or are running tests...)
95 //Project : false, // link to container project!
97 public Node tree; // the tree of nodes.
99 //public GLib.List<JsRender> cn; // child files.. (used by project ... should move code here..)
101 public bool hasParent;
105 public Gee.HashMap<string,string> transStrings; // map of md5 -> string.
106 public Gee.HashMap<string,string> namedStrings;
108 public signal void changed (Node? node, string source);
111 public signal void compile_notice(string type, string file, int line, string message);
116 //public Xcls_Editor editor;
117 public GLib.ListStore childfiles; // used by directories..
120 //abstract JsRender(Project.Project project, string path);
122 public void aconstruct(Project.Project project, string path)
125 //this.cn = new GLib.List<JsRender>();
127 this.project = project;
128 this.hasParent = false;
136 this.content_type = "";
137 this.build_module = "";
138 //this.loaded = false;
139 //print("JsRender.cto() - reset transStrings\n");
140 this.transStrings = new Gee.HashMap<string,string>();
141 this.namedStrings = new Gee.HashMap<string,string>();
142 // should use basename reallly...
144 var ar = this.path.split("/");
145 // name is in theory filename without .bjs (or .js eventually...)
147 Regex regex = new Regex ("\\.(bjs)$");
149 this.name = ar.length > 0 ? regex.replace(ar[ar.length-1],ar[ar.length-1].length, 0 , "") : "";
150 } catch (GLib.Error e) {
153 this.fullname = (this.parent.length > 0 ? (this.parent + ".") : "" ) + this.name;
155 this.doubleStringProps = new Gee.ArrayList<string>();
156 this.childfiles = new GLib.ListStore(typeof(JsRender));
160 public void renameTo(string name) throws Error
162 if (this.xtype == "PlainFile") {
165 var bjs = GLib.Path.get_dirname(this.path) +"/" + name + ".bjs";
166 if (FileUtils.test(bjs, FileTest.EXISTS)) {
167 throw new Error.RENAME_FILE_EXISTS("File exists %s\n",name);
169 GLib.FileUtils.remove(this.path);
171 // remove other files?
180 // not sure why xt is needed... -> project contains xtype..
182 public static JsRender factory(string xt, Project.Project project, string path) throws Error
187 return new Gtk(project, path);
190 return new Roo((Project.Roo) project, path);
192 // return new Flutter(project, path);
194 return new PlainFile(project, path);
196 throw new Error.INVALID_FORMAT("JsRender Factory called with xtype=%s", xt);
202 public string nickType()
204 var ar = this.name.split(".");
206 for (var i =0; i < ar.length -1; i++) {
209 return string.joinv(".", ret);
212 public string nickName()
214 var ar = this.name.split(".");
215 return ar[ar.length-1];
219 public string nickNameSplit()
221 var n = this.nickName();
224 for (var i = 0; i < n.length; i++) {
225 if (i!=0 && n.get(i).isupper() && len > 10) {
229 ret += n.get(i).to_string();
239 Gdk.Pixbuf screenshot = null;
240 Gdk.Pixbuf screenshot92 = null;
241 Gdk.Pixbuf screenshot368 = null;
243 public Gdk.Pixbuf? getIcon(int size = 0) {
244 var fname = this.getIconFileName( );
245 if (!FileUtils.test(fname, FileTest.EXISTS)) {
246 GLib.debug("PIXBUF %s: %s does not exist?", this.name, fname);
252 if (this.screenshot == null) {
254 this.screenshot = new Gdk.Pixbuf.from_file(fname);
255 } catch (GLib.Error e) {}
257 return this.screenshot;
261 if (this.screenshot == null) {
263 if (this.screenshot == null) {
268 this.screenshot92 = this.screenshot.scale_simple(92, (int) (this.screenshot.height * 92.0 /this.screenshot.width * 1.0 )
269 , Gdk.InterpType.NEAREST) ;
270 return this.screenshot92;
273 if (this.screenshot == null) {
275 if (this.screenshot == null) {
280 this.screenshot368 = this.screenshot.scale_simple(368, (int) (this.screenshot.height * 368.0 /this.screenshot.width * 1.0 )
281 , Gdk.InterpType.NEAREST) ;
282 return this.screenshot368;
287 public void writeIcon(Gdk.Pixbuf pixbuf) {
289 this.screenshot92 = null;
290 this.screenshot368 = null;
291 this.screenshot = null;
293 GLib.debug("Wirte %s", this.getIconFileName( ));
294 pixbuf.save(this.getIconFileName( ),"png");
295 this.screenshot = pixbuf;
297 } catch (GLib.Error e) {
298 GLib.debug("failed to write pixbuf?");
306 public void widgetToIcon(global::Gtk.Widget widget) {
308 this.screenshot92 = null;
309 this.screenshot368 = null;
310 this.screenshot = null;
315 var filename = this.getIconFileName();
318 var p = new global::Gtk.WidgetPaintable(widget);
319 var s = new global::Gtk.Snapshot();
320 GLib.debug("Width %d, Height %d", widget.get_width(), widget.get_height());
321 p.snapshot(s, widget.get_width(), widget.get_height());
322 var n = s.free_to_node();
323 var r = new Gsk.CairoRenderer();
325 var t = r.render_texture(n,null);
326 GLib.debug("write to %s", filename);
327 t.save_to_png(filename);
331 } catch (GLib.Error e) {
332 GLib.debug("failed to write pixbuf?");
342 public string getIconFileName( )
345 var m5 = GLib.Checksum.compute_for_string(GLib.ChecksumType.MD5,this.path);
347 var dir = GLib.Environment.get_home_dir() + "/.Builder/icons";
349 if (!FileUtils.test(dir, FileTest.IS_DIR)) {
350 File.new_for_path(dir).make_directory();
352 } catch (GLib.Error e) {
353 // eakk.. what to do here...
355 var fname = dir + "/" + m5 + ".png";
362 public string toJsonString()
364 if (this.xtype == "PlainFile") {
367 var node = new Json.Node(Json.NodeType.OBJECT);
368 node.set_object(this.toJsonObject());
369 var generator = new JsonGen(node);
370 generator.indent = 1;
371 generator.pretty = true;
374 return generator.to_data();
377 public void saveBJS()
379 // if (!this.loaded) {
380 /// GLib.debug("saveBJS - skip - not loaded?");
383 if (this.xtype == "PlainFile") {
389 GLib.debug("WRITE :%s\n " , this.path);// + "\n" + JSON.stringify(write));
391 this.writeFile(this.path, this.toJsonString());
393 } catch(GLib.Error e) {
394 print("Save failed");
405 public string jsonHasOrEmpty(Json.Object obj, string key) {
406 return obj.has_member(key) ?
407 obj.get_string_member(key) : "";
411 public Json.Object toJsonObject ()
415 var ret = new Json.Object();
416 if (this.xtype == "PlainFile") {
420 //ret.set_string_member("id", this.id); // not relivant..
421 ret.set_string_member("name", this.name);
423 if (this.project.xtype == "Roo") {
424 ret.set_string_member("parent", this.parent == null ? "" : this.parent);
425 ret.set_string_member("title", this.title == null ? "" : this.title);
426 //ret.set_string_member("path", this.path);
427 //ret.set_string_member("items", this.items);
428 ret.set_string_member("permname", this.permname == null ? "" : this.permname);
429 ret.set_string_member("modOrder", this.modOrder == null ? "" : this.modOrder);
431 if (this.project.xtype == "Gtk") {
433 ret.set_string_member("build_module", this.build_module );
435 ret.set_boolean_member("gen_extended", this.gen_extended);
437 if (this.transStrings.size > 0) {
438 var tr = new Json.Object();
439 var iter = this.transStrings.map_iterator();
440 while (iter.next()) {
441 tr.set_string_member(iter.get_value(), iter.get_key());
443 ret.set_object_member("strings", tr);
448 if (this.namedStrings.size > 0) {
449 var tr = new Json.Object();
450 var iter = this.namedStrings.map_iterator();
451 while (iter.next()) {
452 tr.set_string_member(iter.get_key(), iter.get_value());
454 ret.set_object_member("named_strings", tr);
457 var ar = new Json.Array();
458 // empty files do not have a tree.
459 if (this.tree != null) {
460 ar.add_object_element(this.tree.toJsonObject());
462 ret.set_array_member("items", ar);
469 public string getTitle ()
471 if (this.title == null) { // not sure why this happens..
474 if (this.title.length > 0) {
477 var a = this.path.split("/");
478 return a[a.length-1];
480 public string getTitleTip()
482 if (this.title.length > 0) {
483 return "<b>" + this.title + "</b> " + this.path;
490 this.cn.sort(function(a,b) {
491 return a.path > b.path;// ? 1 : -1;
495 // should be in palete provider really..
498 public Palete.Palete palete()
500 // error on plainfile?
501 return this.project.palete;
505 public string guessName(Node ar) // turns the object into full name.
507 // eg. xns: Roo, xtype: XXX -> Roo.xxx
508 if (!ar.hasXnsType()) {
512 return ar.get("xns") + "." + ar.get("xtype");
517 * non-atomic write (replacement for put contents, as it creates temporary files.
519 public void writeFile(string path, string contents) throws GLib.IOError, GLib.Error
523 var f = GLib.File.new_for_path(path);
524 var data_out = new GLib.DataOutputStream(
525 f.replace(null, false, GLib.FileCreateFlags.NONE, null)
527 data_out.put_string(contents, null);
528 data_out.close(null);
533 public Node? lineToNode(int line)
535 if (this.tree == null) {
538 return this.tree.lineToNode(line);
543 public GLib.ListStore toListStore()
545 var ret = new GLib.ListStore(typeof(Node));
546 ret.append(this.tree);
551 // used to handle list of files in project editor (really Gtk only)
552 public bool compile_group_selected {
554 var gproj = (Project.Gtk) this.project;
556 if (gproj.active_cg == null) {
559 if (this.xtype == "Dir") {
560 // show ticked if all ticked..
562 for(var i = 0; i < this.childfiles.n_items; i++ ) {
563 var f = (JsRender) this.childfiles.get_item(i);
564 if (!f.compile_group_selected) {
573 if (gproj.active_cg.sources == null) {
574 GLib.debug("compile_group_selected - sources is null? ");
578 return gproj.active_cg.sources.contains(this.relpath);
583 var gproj = (Project.Gtk) this.project;
585 if (gproj.active_cg == null) {
588 if (gproj.active_cg.loading_ui) {
592 if (this.xtype == "Dir") {
593 for(var i = 0; i < this.childfiles.n_items; i++ ) {
594 var f = (JsRender) this.childfiles.get_item(i);
595 f.compile_group_selected = value;
604 if (value == false) {
605 GLib.debug("REMOVE %s", this.relpath);
607 gproj.active_cg.sources.remove(this.relpath);
610 if (!gproj.active_cg.sources.contains(this.relpath)) {
611 GLib.debug("ADD %s", this.relpath);
612 gproj.active_cg.sources.add(this.relpath);
618 public bool compile_group_hidden {
620 var gproj = (Project.Gtk) this.project;
623 return gproj.hidden.contains(this.relpath);
628 var gproj = (Project.Gtk) this.project;
630 if (gproj.active_cg == null) {
633 if (gproj.active_cg.loading_ui) {
636 if (value == false) {
637 GLib.debug("REMOVE %s", this.relpath);
639 gproj.hidden.remove(this.relpath);
642 if (!gproj.hidden.contains(this.relpath)) {
643 gproj.hidden.add(this.relpath);
644 // hiding a project will auto clear it.
645 for(var i = 0; i < this.childfiles.n_items; i++ ) {
646 var f = (JsRender) this.childfiles.get_item(i);
647 f.compile_group_selected = false;
658 if (this.xtype == "Dir") {
661 // cleans up build (should really be in children..
662 this.removeFile(this.path);
663 if (this.path.has_suffix(".bjs") && this.project.xtype == "Roo") {
664 this.removeFile(this.path.substring(0, this.path.length-4) + ".js");
667 if (this.path.has_suffix(".bjs") && this.project.xtype == "Gtk") {
668 this.removeFile(this.path.substring(0, this.path.length-4) + ".vala");
669 this.removeFile(this.path.substring(0, this.path.length-4) + ".c");
670 this.removeFile(this.path.substring(0, this.path.length-4) + ".o");
672 if (this.path.has_suffix(".vala") && this.project.xtype == "Gtk") {
673 this.removeFile(this.path.substring(0, this.path.length-5) + ".c");
674 this.removeFile(this.path.substring(0, this.path.length-5) + ".o");
680 private void removeFile(string path)
683 if (GLib.FileUtils.test(path, GLib.FileTest.EXISTS)) {
684 GLib.FileUtils.unlink(path);
688 public string relTargetName()
690 return this.targetName().substring(this.project.path.length +1);
694 public abstract void save();
695 public abstract void saveHTML(string html);
696 public abstract string toSource() ;
697 public abstract string toSourceCode() ; // used by commandline tester..
698 public abstract void setSource(string str);
699 public abstract string toSourcePreview() ;
700 public abstract void removeFiles() ;
701 public abstract void findTransStrings(Node? node );
702 public abstract string toGlade();
703 public abstract string targetName();
704 public abstract void loadItems() throws GLib.Error;