1 static BrowserWindow _BrowserWindow;
3 public class BrowserWindow : Object
6 private BrowserWindow _this;
8 public static BrowserWindow singleton()
10 if (_BrowserWindow == null) {
11 _BrowserWindow= new BrowserWindow();
13 return _BrowserWindow;
15 public Xcls_scrolled_window scrolled_window;
16 public Xcls_view view;
21 public BrowserWindow()
24 this.el = new Gtk.Window( Gtk.WindowType.TOPLEVEL );
29 var child_0 = new Xcls_scrolled_window( _this );
31 this.el.add ( child_0.el );
34 this.el.destroy.connect( () => {
37 this.el.show.connect( () => {
39 print("resizing window\n");
40 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
41 var h = webkitpdf.opt_height > 0 ? webkitpdf.opt_height : 500;
46 // we have to do silly stuff with blogspot!!?!?
47 var blog = webkitpdf.opt_url;
50 /* Do not redirect if the domain is .com already */
51 if (/\.blogspot\.com/.match(blog)) {
52 var r = new Regex ("\\.blogspot\\..*?/");
53 blog = r.replace(blog, blog.length, 0, ".blogspot.com/ncr/");
56 if(webkitpdf.opt_cookies.length > 0) {
57 print("setting cookie %s\n",webkitpdf.opt_cookies);
58 var ses = WebKit.get_default_session();
60 var cookie_jar = new Soup.CookieJar();
62 cookie_jar.set_accept_policy(Soup.CookieJarAcceptPolicy.ALWAYS);
63 cookie_jar.add_cookie();
64 sess.add_feature(cookie_jar);
65 //this.view.el.set_cookie(webkitpdf.opt_cookies);
68 print("loading url %s\n",blog);
69 this.view.el.load_uri(blog);
71 // if we have not finished in 25 seconds
74 GLib.Timeout.add_seconds(1, () => {
76 print("timeout 25 seconds :%d", cd);
78 print("calling printit - timed out");
90 // user defined functions
91 public class Xcls_scrolled_window : Object
93 public Gtk.ScrolledWindow el;
94 private BrowserWindow _this;
100 public Xcls_scrolled_window(BrowserWindow _owner )
103 _this.scrolled_window = this;
104 this.el = new Gtk.ScrolledWindow( null, null );
108 // set gobject values
109 var child_0 = new Xcls_view( _this );
111 this.el.add ( child_0.el );
116 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
120 // user defined functions
122 public class Xcls_view : Object
124 public WebKit.WebView el;
125 private BrowserWindow _this;
129 public bool print_started;
133 public Xcls_view(BrowserWindow _owner )
137 this.el = new WebKit.WebView();
140 this.print_started = false;
143 // set gobject values
151 this.el.load_changed.connect( (ev ) => {
152 if (ev != WebKit.LoadEvent.FINISHED) {
163 // this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
164 // print("resource req. started %s\n", netreq.uri);
168 this.el.load_finished.connect( ( ) => {
178 // user defined functions
179 public void printit () {
180 print("load_finsihed\n");
182 if (this.print_started) {
183 print("load_finsihed - already printing..%d %s\n", this.load_try, this.el.uri);
192 this.print_started = true; // flag to stop double call.
195 // inject any javascript if needed..
196 if (webkitpdf.opt_inject_js != null) {
198 print("injecting javascript files \n");
199 foreach(var inject_js in webkitpdf.opt_inject_js) {
200 print("inject? %s\n", inject_js);
203 FileUtils.get_contents( inject_js, out str);
204 } catch(FileError e) {
205 print("Failed to inject %s\n", inject_js);
209 GLib.MainLoop mainloop = new GLib.MainLoop();
210 this.el.run_javascript.begin(str, null, (obj,res) => {
216 this.el.execute_script(str);
221 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
222 if (webkitpdf.opt_target_pdf != null) {
223 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
224 print("delay done URL: %s\n", this.el.uri);
229 }, GLib.Priority.DEFAULT);
235 print("PNG not supported in webkit2\n");
239 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
242 // ----------------- JPEG
243 // resize the window...
244 // window.document.documentElement.scrollHeight
245 var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
246 print("Scroll height %d\n", scroll_height);
247 if (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {
248 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
249 var h = int.min(scroll_height, 6000);
250 _this.scrolled_window.el.set_size_request( w, h);
251 _this.el.resize (w+50, h+50);
252 print("Resize to %d, %d\n", w,h);
253 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => {
256 }, GLib.Priority.DEFAULT);
261 }, GLib.Priority.DEFAULT);
267 public bool printpdf () {
271 var list = Gtk.PaperSize.get_paper_sizes(false);
272 var psetup = new Gtk.PageSetup();
273 for(var i = 0; i < list.length(); i++ ) {
274 var entry = list.nth_data(i).copy();
275 if (entry.get_name() == "iso_a2") {
276 psetup.set_paper_size(entry);
283 var pe = new WebKit.PrintOperation(this.el);
284 pe.set_page_setup(psetup);
285 pe.finished.connect( () => {
286 print("print completed\n");
291 var ps = new Gtk.PrintSettings();
292 ps.set_printer("Print to File");
293 ps.set("output-file-format", "pdf");
294 ps.set("output-uri", "file://" + webkitpdf.opt_target_pdf);
297 pe.set_print_settings(ps);
304 var pe = new Gtk.PrintOperation();
305 pe.export_filename = webkitpdf.opt_target_pdf;
306 print("got callback for print\n");
308 /*pe.failed.connect(() => {
309 print("print failed\n");
314 pe.done.connect(() => {
315 print("print completed\n");
320 //pe.run_dialog(_this.el);
326 pe.set_default_page_setup(psetup);
327 print("Calling Print?\n");
328 //pe.run_dialog(_this.el);
335 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
344 public void printpng () {
345 //var filename = "/tmp/test.pdf";
346 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
348 //if (le != LoadEvent.FINISHED) {
352 // what size is the documet.
354 print("making screenshot\n");
358 // fix vapi - get_snapshot add '?' to all null.
359 var pixmap = _this.view.el.get_snapshot( null );
365 // add out to gdkpixmap.get_size
366 pixmap.get_size( out w , out h);
367 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
368 print("pixbuf size: %d x %d\n ", w,h);
372 pixbuf.save(webkitpdf.opt_target_png, "png");
373 print("Saved to %s\n", webkitpdf.opt_target_png);
374 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
375 // var sf = view.get_snapshot.end(res);
377 // sf.write_to_png(Browser.opt_target_png);