Spider.bjs
authorAlan Knowles <alan@roojs.com>
Fri, 13 May 2016 06:34:49 +0000 (14:34 +0800)
committerAlan Knowles <alan@roojs.com>
Fri, 13 May 2016 06:34:49 +0000 (14:34 +0800)
BrowserWindow.bjs
BrowserWindow.vala
Spider.vala

Spider.bjs [new file with mode: 0644]
Spider.vala [new file with mode: 0644]

diff --git a/Spider.bjs b/Spider.bjs
new file mode 100644 (file)
index 0000000..bad5adb
--- /dev/null
@@ -0,0 +1,45 @@
+{
+ "name" : "Spider",
+ "parent" : "",
+ "title" : "",
+ "path" : "/home/alan/gitlive/app.webkitpdf/Spider.bjs",
+ "permname" : "",
+ "modOrder" : "",
+ "build_module" : "NEW GROUP",
+ "items" : [
+  {
+   "listeners" : {
+    "destroy" : "() => {\n    Gtk.main_quit();\n}",
+    "show" : "() => {\n\t\n\tprint(\"resizing window\\n\");\n\tthis.el.resize(1200,500);\n\tprint(\"loading url\\n\");\n\tthis.view.el.load_uri(webkitpdf.opt_url);\n\t\n\t// if we have not finished in 25 seconds\n\t// call printit...\n\t  GLib.Timeout.add_seconds(25, () => { \n\t\t\t_this.view.printit();\n\t\t\treturn true;\n\t});\n\t\n\n}"
+   },
+   "xtype" : "Window",
+   "$ xns" : "Gtk",
+   "Gtk.WindowType type" : "Gtk.WindowType.TOPLEVEL",
+   "items" : [
+    {
+     "id" : "scrolled_window",
+     "* init" : "{\n\tthis.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);\n}\n",
+     "* pack" : "add",
+     "xtype" : "ScrolledWindow",
+     "$ xns" : "Gtk",
+     "items" : [
+      {
+       "listeners" : {
+        "resource_request_starting" : "(p0, webres, netreq ) => {\n  print(\"resource req. started %s\\n\", netreq.uri);\n  \n  \n}",
+        "load_finished" : "( ) => {\n\t\t \n       this.printit();\n \n}"
+       },
+       "id" : "view",
+       "| void printit" : "() {\n   print(\"load_finsihed\\n\");\n   \n   if (this.print_started) {\n      print(\"load_finsihed - already printing..\\n\");\n      return;\n  }\n   \n    this.print_started = true; // flag to stop double call.\n    \n    \n\t// inject any javascript if needed..\n\tif (webkitpdf.opt_inject_js != null) {\n\t\tfor(var i =0; i < webkitpdf.opt_inject_js.length;i++) {\n\t\t\tstring str;\n\t\t\ttry {\n\t\t\t\tFileUtils.get_contents( webkitpdf.opt_inject_js[i], out str);\n\t\t\t} catch(FileError e) {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tthis.el.execute_script(str);\n\t\t}\n\t}\n\t\n\t\n\t\n\tprint(\"calling print in %d seconds\\n\", (int)webkitpdf.opt_delay);\n\tif (webkitpdf.opt_target_pdf != null) {\n\t\tGLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { \n\n\t\t\tthis.printpdf();\n\t\t\t return false;\n\t\t}, GLib.Priority.DEFAULT);\n\t}\n\t\t\n\t\t\n    GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { \n   \n\t\t\n\t\t// ----------------- JPEG \n\t\t// resize the window...\n\t\t// window.document.documentElement.scrollHeight\n\t\tvar scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;\n\t\tprint(\"Scroll height %d\\n\", scroll_height);\n\t\tif (scroll_height> 1024) {\n\t\t\t//_this.scrolled_window.el.set_size_request(1200, int.min(scroll_height, 6000));\n\t\t\t_this.scrolled_window.el.set_size_request(1200, int.min(scroll_height, 6000));\n\t\t\tprint(\"Resize to %d\\n\", scroll_height);\n\t\t    GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { \n\t\t\t\tthis.printpng();\n\t\t\t\treturn false;\n\t\t\t  }, GLib.Priority.DEFAULT);\n  \t\t    return false;\n\t\t}\n\t\tthis.printpng();\t\t\n\t\treturn false;\n\t  }, GLib.Priority.DEFAULT);\n\n\n    \n    \n}\n",
+       "* pack" : "add",
+       "xtype" : "WebView",
+       "| bool printpdf" : "() {\n\t \n\t\tvar  pe = new  Gtk.PrintOperation();\n\t\tpe.export_filename = webkitpdf.opt_target_pdf;\n\t\tprint(\"got callback for print\");\n\t\tpe.ref();\n\t\t/*pe.failed.connect(() => {\n\t\t\tprint(\"print failed\\n\");\n\t\t\t Gtk.main_quit();\n    \t\t\n\t\t})\n\t\t*/;\n\t\t pe.done.connect(() => {\n\t\t\tprint(\"print completed\\n\");\n\t\t\t Gtk.main_quit();\n    \t\t\n\t\t});\n\t\t\n\t\t//pe.run_dialog(_this.el);\n\t \n\t\t\n\t    var list = Gtk.PaperSize.get_paper_sizes(false);\n\t    var psetup = new Gtk.PageSetup();\n\t    for(var i = 0; i < list.length(); i++ ) {\n\t        var entry = list.nth_data(i).copy();\n\t        if (entry.get_name() == \"iso_a2\") {\n\t            psetup.set_paper_size(entry);\n\t        }\n\t    }\n\t        \n\n\n\t    pe.set_default_page_setup(psetup);\n\t    print(\"Calling Print?\\n\");\n\t    //pe.run_dialog(_this.el);\n\t    try {\n\t\t    this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);\n\t    } catch (Error e) {\n\t\t\t// print failed...\n\t\t}\n\t    \n\t    \n\t     \n\t\treturn false;\n}\n",
+       "| void printpng" : "() {\n    //var filename = \"/tmp/test.pdf\";\n    //print(\"load_changed %d ?= %d\\n\", le, LoadEvent.FINISHED);\n\n    //if (le != LoadEvent.FINISHED) {\n    //    return;\n   // }\n   \n// what size is the documet.\n\n     print(\"making screenshot\\n\");\n     \n    // fix vapi - get_snapshot add '?' to all null.\n    var pixmap = _this.view.el.get_snapshot( null );\n\n    \n    \n    int w,h;\n    \n    // add out to gdkpixmap.get_size\n\tpixmap.get_size( out  w , out   h);\n\tvar pixbuf  = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);\n\tprint(\"pixbuf size: %d x %d\\n \", w,h);\n\t \n    \n    \n     pixbuf.save(webkitpdf.opt_target_png, \"png\");\n     print(\"Saved to %s\\n\", webkitpdf.opt_target_png);\n    //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {\n    //    var sf = view.get_snapshot.end(res);\n\n    //    sf.write_to_png(Browser.opt_target_png);\n    //});\n    Gtk.main_quit();\n     \n   \n    \n    \n\n}\n",
+       "bool print_started" : false,
+       "$ xns" : "WebKit"
+      }
+     ]
+    }
+   ]
+  }
+ ]
+}
\ No newline at end of file
diff --git a/Spider.vala b/Spider.vala
new file mode 100644 (file)
index 0000000..74628ef
--- /dev/null
@@ -0,0 +1,272 @@
+static Spider  _Spider;
+
+public class Spider : Object
+{
+    public Gtk.Window el;
+    private Spider  _this;
+
+    public static Spider singleton()
+    {
+        if (_Spider == null) {
+            _Spider= new Spider();
+        }
+        return _Spider;
+    }
+    public Xcls_scrolled_window scrolled_window;
+    public Xcls_view view;
+
+        // my vars (def)
+
+    // ctor
+    public Spider()
+    {
+        _this = this;
+        this.el = new Gtk.Window( Gtk.WindowType.TOPLEVEL );
+
+        // my vars (dec)
+
+        // set gobject values
+        var child_0 = new Xcls_scrolled_window( _this );
+        child_0.ref();
+        this.el.add (  child_0.el  );
+
+        //listeners
+        this.el.destroy.connect( () => {
+            Gtk.main_quit();
+        });
+        this.el.show.connect( () => {
+               
+               print("resizing window\n");
+               this.el.resize(1200,500);
+               print("loading url\n");
+               this.view.el.load_uri(webkitpdf.opt_url);
+               
+               // if we have not finished in 25 seconds
+               // call printit...
+                 GLib.Timeout.add_seconds(25, () => { 
+                               _this.view.printit();
+                               return true;
+               });
+               
+        
+        });
+    }
+
+    // user defined functions
+    public class Xcls_scrolled_window : Object
+    {
+        public Gtk.ScrolledWindow el;
+        private Spider  _this;
+
+
+            // my vars (def)
+
+        // ctor
+        public Xcls_scrolled_window(Spider _owner )
+        {
+            _this = _owner;
+            _this.scrolled_window = this;
+            this.el = new Gtk.ScrolledWindow( null, null );
+
+            // my vars (dec)
+
+            // set gobject values
+            var child_0 = new Xcls_view( _this );
+            child_0.ref();
+            this.el.add (  child_0.el  );
+
+            // init method
+
+            {
+               this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
+            }
+        }
+
+        // user defined functions
+    }
+    public class Xcls_view : Object
+    {
+        public WebKit.WebView el;
+        private Spider  _this;
+
+
+            // my vars (def)
+        public bool print_started;
+
+        // ctor
+        public Xcls_view(Spider _owner )
+        {
+            _this = _owner;
+            _this.view = this;
+            this.el = new WebKit.WebView();
+
+            // my vars (dec)
+            this.print_started = false;
+
+            // set gobject values
+
+            //listeners
+            this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
+              print("resource req. started %s\n", netreq.uri);
+              
+              
+            });
+            this.el.load_finished.connect( ( ) => {
+                        
+                   this.printit();
+             
+            });
+        }
+
+        // user defined functions
+        public void printit () {
+           print("load_finsihed\n");
+           
+           if (this.print_started) {
+              print("load_finsihed - already printing..\n");
+              return;
+          }
+           
+            this.print_started = true; // flag to stop double call.
+            
+            
+               // inject any javascript if needed..
+               if (webkitpdf.opt_inject_js != null) {
+                       for(var i =0; i < webkitpdf.opt_inject_js.length;i++) {
+                               string str;
+                               try {
+                                       FileUtils.get_contents( webkitpdf.opt_inject_js[i], out str);
+                               } catch(FileError e) {
+                                       continue;
+                               }
+                               this.el.execute_script(str);
+                       }
+               }
+               
+               
+               
+               print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
+               if (webkitpdf.opt_target_pdf != null) {
+                       GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
+        
+                               this.printpdf();
+                                return false;
+                       }, GLib.Priority.DEFAULT);
+               }
+                       
+                       
+            GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
+           
+                       
+                       // ----------------- JPEG 
+                       // resize the window...
+                       // window.document.documentElement.scrollHeight
+                       var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
+                       print("Scroll height %d\n", scroll_height);
+                       if (scroll_height> 1024) {
+                               //_this.scrolled_window.el.set_size_request(1200, int.min(scroll_height, 6000));
+                               _this.scrolled_window.el.set_size_request(1200, int.min(scroll_height, 6000));
+                               print("Resize to %d\n", scroll_height);
+                           GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
+                                       this.printpng();
+                                       return false;
+                                 }, GLib.Priority.DEFAULT);
+                           return false;
+                       }
+                       this.printpng();                
+                       return false;
+                 }, GLib.Priority.DEFAULT);
+        
+        
+            
+            
+        }
+        public bool printpdf () {
+                
+                       var  pe = new  Gtk.PrintOperation();
+                       pe.export_filename = webkitpdf.opt_target_pdf;
+                       print("got callback for print");
+                       pe.ref();
+                       /*pe.failed.connect(() => {
+                               print("print failed\n");
+                                Gtk.main_quit();
+                       
+                       })
+                       */;
+                        pe.done.connect(() => {
+                               print("print completed\n");
+                                Gtk.main_quit();
+                       
+                       });
+                       
+                       //pe.run_dialog(_this.el);
+                
+                       
+                   var list = Gtk.PaperSize.get_paper_sizes(false);
+                   var psetup = new Gtk.PageSetup();
+                   for(var i = 0; i < list.length(); i++ ) {
+                       var entry = list.nth_data(i).copy();
+                       if (entry.get_name() == "iso_a2") {
+                           psetup.set_paper_size(entry);
+                       }
+                   }
+                       
+        
+        
+                   pe.set_default_page_setup(psetup);
+                   print("Calling Print?\n");
+                   //pe.run_dialog(_this.el);
+                   try {
+                           this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
+                   } catch (Error e) {
+                               // print failed...
+                       }
+                   
+                   
+                    
+                       return false;
+        }
+        public void printpng () {
+            //var filename = "/tmp/test.pdf";
+            //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
+        
+            //if (le != LoadEvent.FINISHED) {
+            //    return;
+           // }
+           
+        // what size is the documet.
+        
+             print("making screenshot\n");
+             
+            // fix vapi - get_snapshot add '?' to all null.
+            var pixmap = _this.view.el.get_snapshot( null );
+        
+            
+            
+            int w,h;
+            
+            // add out to gdkpixmap.get_size
+               pixmap.get_size( out  w , out   h);
+               var pixbuf  = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
+               print("pixbuf size: %d x %d\n ", w,h);
+                
+            
+            
+             pixbuf.save(webkitpdf.opt_target_png, "png");
+             print("Saved to %s\n", webkitpdf.opt_target_png);
+            //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
+            //    var sf = view.get_snapshot.end(res);
+        
+            //    sf.write_to_png(Browser.opt_target_png);
+            //});
+            Gtk.main_quit();
+             
+           
+            
+            
+        
+        }
+    }
+
+
+}