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 != null) {
57 print("setting cookie %s\n",webkitpdf.opt_cookies);
59 var uri = new Soup.URI (blog);
61 string[] cookies = webkitpdf.opt_cookies.split ("=", 2);
63 if(cookies.length >=2 ) {
65 var context = WebKit.WebContext.get_default();
66 var cookie_manager = context.get_cookie_manager();
67 cookie_manager.set_accept_policy(WebKit.CookieAcceptPolicy.ALWAYS);
68 string scheme = uri.get_scheme ();
70 var sess = WebKit.get_default_session();
71 var cookie_jar = new Soup.CookieJar();
72 cookie_jar.set_accept_policy(Soup.CookieJarAcceptPolicy.ALWAYS);
73 cookie_jar.add_cookie(new Soup.Cookie (cookies[0], cookies[1], uri.get_host(), "/", -1));
74 sess.add_feature(cookie_jar);
81 print("loading url %s\n",blog);
82 this.view.el.load_uri(blog);
84 // if we have not finished in 25 seconds
87 GLib.Timeout.add_seconds(1, () => {
89 print("timeout 25 seconds :%d", cd);
91 print("calling printit - timed out");
103 // user defined functions
104 public class Xcls_scrolled_window : Object
106 public Gtk.ScrolledWindow el;
107 private BrowserWindow _this;
113 public Xcls_scrolled_window(BrowserWindow _owner )
116 _this.scrolled_window = this;
117 this.el = new Gtk.ScrolledWindow( null, null );
121 // set gobject values
122 var child_0 = new Xcls_view( _this );
124 this.el.add ( child_0.el );
129 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
133 // user defined functions
135 public class Xcls_view : Object
137 public WebKit.WebView el;
138 private BrowserWindow _this;
142 public bool print_started;
146 public Xcls_view(BrowserWindow _owner )
150 this.el = new WebKit.WebView();
153 this.print_started = false;
156 // set gobject values
164 this.el.load_changed.connect( (ev ) => {
165 if (ev != WebKit.LoadEvent.FINISHED) {
176 // this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
177 // print("resource req. started %s\n", netreq.uri);
181 this.el.load_finished.connect( ( ) => {
191 // user defined functions
192 public void printit () {
193 print("load_finsihed\n");
195 if (this.print_started) {
196 print("load_finsihed - already printing..%d %s\n", this.load_try, this.el.uri);
205 this.print_started = true; // flag to stop double call.
208 // inject any javascript if needed..
209 if (webkitpdf.opt_inject_js != null) {
211 print("injecting javascript files \n");
212 foreach(var inject_js in webkitpdf.opt_inject_js) {
213 print("inject? %s\n", inject_js);
216 FileUtils.get_contents( inject_js, out str);
217 } catch(FileError e) {
218 print("Failed to inject %s\n", inject_js);
222 GLib.MainLoop mainloop = new GLib.MainLoop();
223 this.el.run_javascript.begin(str, null, (obj,res) => {
229 this.el.execute_script(str);
234 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
235 if (webkitpdf.opt_target_pdf != null) {
236 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
237 print("delay done URL: %s\n", this.el.uri);
242 }, GLib.Priority.DEFAULT);
248 print("PNG not supported in webkit2\n");
252 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
255 // ----------------- JPEG
256 // resize the window...
257 // window.document.documentElement.scrollHeight
258 var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
259 print("Scroll height %d\n", scroll_height);
260 if (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {
261 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
262 var h = int.min(scroll_height, 6000);
263 _this.scrolled_window.el.set_size_request( w, h);
264 _this.el.resize (w+50, h+50);
265 print("Resize to %d, %d\n", w,h);
266 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => {
269 }, GLib.Priority.DEFAULT);
274 }, GLib.Priority.DEFAULT);
280 public bool printpdf () {
284 var list = Gtk.PaperSize.get_paper_sizes(false);
285 var psetup = new Gtk.PageSetup();
286 for(var i = 0; i < list.length(); i++ ) {
287 var entry = list.nth_data(i).copy();
288 if (entry.get_name() == "iso_a2") {
289 psetup.set_paper_size(entry);
296 var pe = new WebKit.PrintOperation(this.el);
297 pe.set_page_setup(psetup);
298 pe.finished.connect( () => {
299 print("print completed\n");
304 var ps = new Gtk.PrintSettings();
305 ps.set_printer("Print to File");
306 ps.set("output-file-format", "pdf");
307 ps.set("output-uri", "file://" + webkitpdf.opt_target_pdf);
310 pe.set_print_settings(ps);
317 var pe = new Gtk.PrintOperation();
318 pe.export_filename = webkitpdf.opt_target_pdf;
319 print("got callback for print\n");
321 /*pe.failed.connect(() => {
322 print("print failed\n");
327 pe.done.connect(() => {
328 print("print completed\n");
333 //pe.run_dialog(_this.el);
339 pe.set_default_page_setup(psetup);
340 print("Calling Print?\n");
341 //pe.run_dialog(_this.el);
348 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
357 public void printpng () {
358 //var filename = "/tmp/test.pdf";
359 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
361 //if (le != LoadEvent.FINISHED) {
365 // what size is the documet.
367 print("making screenshot\n");
371 // fix vapi - get_snapshot add '?' to all null.
372 var pixmap = _this.view.el.get_snapshot( null );
378 // add out to gdkpixmap.get_size
379 pixmap.get_size( out w , out h);
380 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
381 print("pixbuf size: %d x %d\n ", w,h);
385 pixbuf.save(webkitpdf.opt_target_png, "png");
386 print("Saved to %s\n", webkitpdf.opt_target_png);
387 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
388 // var sf = view.get_snapshot.end(res);
390 // sf.write_to_png(Browser.opt_target_png);