1 static BrowserWindow _BrowserWindow;
3 public class BrowserWindow : Object
6 private BrowserWindow _this;
7 public string cookie_file;
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( () => {
38 this.el.show.connect( () => {
40 print("resizing window\n");
41 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
42 var h = webkitpdf.opt_height > 0 ? webkitpdf.opt_height : 500;
47 // we have to do silly stuff with blogspot!!?!?
48 var blog = webkitpdf.opt_url;
51 /* Do not redirect if the domain is .com already */
52 if (/\.blogspot\.com/.match(blog)) {
53 var r = new Regex ("\\.blogspot\\..*?/");
54 blog = r.replace(blog, blog.length, 0, ".blogspot.com/ncr/");
57 if(webkitpdf.opt_cookies != null) {
58 print("setting cookie %s\n",webkitpdf.opt_cookies);
60 var uri = new Soup.URI (blog);
62 string[] cookies = webkitpdf.opt_cookies.split ("=", 2);
64 if(cookies.length > 1 ) {
67 var context = WebKit.WebContext.get_default();
68 var cookie_manager = context.get_cookie_manager();
69 cookie_manager.set_accept_policy(WebKit.CookieAcceptPolicy.NEVER);
70 string scheme = uri.get_scheme ();
73 FileIOStream iostream;
74 File file = File.new_tmp ("cookie-XXXXXX.txt", out iostream);
75 this.cookie_file = file.get_path ();
76 print("cookie tmp file name: %s\n", file.get_path ());
78 OutputStream ostream = iostream.output_stream;
79 DataOutputStream dostream = new DataOutputStream (ostream);
80 dostream.put_string ("%s\tTRUE\t/\t%s\t2147483647\t%s\t%s".printf(uri.get_host(),scheme == "http" ? "FALSE" : "TRUE",cookies[0],cookies[1]));
81 cookie_manager.set_persistent_storage(file.get_path(), WebKit.CookiePersistentStorage.TEXT);
83 stdout.printf ("Error: %s\n", e.message);
86 var sess = WebKit.get_default_session();
87 var cookie_jar = new Soup.CookieJar();
88 cookie_jar.set_accept_policy(Soup.CookieJarAcceptPolicy.NEVER);
89 cookie_jar.add_cookie(new Soup.Cookie (cookies[0], cookies[1], uri.get_host(), "/", -1));
90 sess.add_feature(cookie_jar);
98 print("loading url %s\n",blog);
99 this.view.el.load_uri(blog);
101 // if we have not finished in 25 seconds
104 GLib.Timeout.add_seconds(1, () => {
106 print("timeout 25 seconds :%d", cd);
108 print("calling printit - timed out");
109 _this.view.printit();
121 // user defined functions
122 public class Xcls_scrolled_window : Object
124 public Gtk.ScrolledWindow el;
125 private BrowserWindow _this;
131 public Xcls_scrolled_window(BrowserWindow _owner )
134 _this.scrolled_window = this;
135 this.el = new Gtk.ScrolledWindow( null, null );
139 // set gobject values
140 var child_0 = new Xcls_view( _this );
142 this.el.add ( child_0.el );
147 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
151 // user defined functions
153 public class Xcls_view : Object
155 public WebKit.WebView el;
156 private BrowserWindow _this;
160 public bool print_started;
164 public Xcls_view(BrowserWindow _owner )
168 this.el = new WebKit.WebView();
171 this.print_started = false;
174 // set gobject values
177 if(webkitpdf.opt_cookies != null) {
178 print("setting cookie %s\n",webkitpdf.opt_cookies);
180 var uri = new Soup.URI (blog);
182 string[] cookies = webkitpdf.opt_cookies.split ("=", 2);
184 if(cookies.length > 1 ) {
187 var context = WebKit.WebContext.get_default();
188 var cookie_manager = context.get_cookie_manager();
189 cookie_manager.set_accept_policy(WebKit.CookieAcceptPolicy.NEVER);
190 string scheme = uri.get_scheme ();
193 FileIOStream iostream;
194 File file = File.new_tmp ("cookie-XXXXXX.txt", out iostream);
195 this.cookie_file = file.get_path ();
196 print("cookie tmp file name: %s\n", file.get_path ());
198 OutputStream ostream = iostream.output_stream;
199 DataOutputStream dostream = new DataOutputStream (ostream);
200 dostream.put_string ("%s\tTRUE\t/\t%s\t2147483647\t%s\t%s".printf(uri.get_host(),scheme == "http" ? "FALSE" : "TRUE",cookies[0],cookies[1]));
201 cookie_manager.set_persistent_storage(file.get_path(), WebKit.CookiePersistentStorage.TEXT);
203 stdout.printf ("Error: %s\n", e.message);
206 var sess = WebKit.get_default_session();
207 var cookie_jar = new Soup.CookieJar();
208 cookie_jar.set_accept_policy(Soup.CookieJarAcceptPolicy.NEVER);
209 cookie_jar.add_cookie(new Soup.Cookie (cookies[0], cookies[1], uri.get_host(), "/", -1));
210 sess.add_feature(cookie_jar);
225 this.el.load_changed.connect( (ev ) => {
226 if (ev != WebKit.LoadEvent.FINISHED) {
237 // this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
238 // print("resource req. started %s\n", netreq.uri);
242 this.el.load_finished.connect( ( ) => {
252 // user defined functions
253 public void printit () {
254 print("load_finsihed\n");
256 if (this.print_started) {
257 print("load_finsihed - already printing..%d %s\n", this.load_try, this.el.uri);
266 this.print_started = true; // flag to stop double call.
269 // inject any javascript if needed..
270 if (webkitpdf.opt_inject_js != null) {
272 print("injecting javascript files \n");
273 foreach(var inject_js in webkitpdf.opt_inject_js) {
274 print("inject? %s\n", inject_js);
277 FileUtils.get_contents( inject_js, out str);
278 } catch(FileError e) {
279 print("Failed to inject %s\n", inject_js);
283 GLib.MainLoop mainloop = new GLib.MainLoop();
284 this.el.run_javascript.begin(str, null, (obj,res) => {
290 this.el.execute_script(str);
295 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
297 if (webkitpdf.opt_target_pdf != null) {
298 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
299 print("delay done URL: %s\n", this.el.uri);
304 }, GLib.Priority.DEFAULT);
310 print("PNG not supported in webkit2\n");
314 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
317 // ----------------- JPEG
318 // resize the window...
319 // window.document.documentElement.scrollHeight
320 var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
321 print("Scroll height %d\n", scroll_height);
322 if (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {
323 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
324 var h = int.min(scroll_height, 6000);
325 _this.scrolled_window.el.set_size_request( w, h);
326 _this.el.resize (w+50, h+50);
327 print("Resize to %d, %d\n", w,h);
328 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => {
331 }, GLib.Priority.DEFAULT);
336 }, GLib.Priority.DEFAULT);
342 public bool printpdf () {
346 var list = Gtk.PaperSize.get_paper_sizes(false);
347 var psetup = new Gtk.PageSetup();
348 for(var i = 0; i < list.length(); i++ ) {
349 var entry = list.nth_data(i).copy();
350 if (entry.get_name() == "iso_a2") {
351 psetup.set_paper_size(entry);
358 var pe = new WebKit.PrintOperation(this.el);
359 pe.set_page_setup(psetup);
360 pe.finished.connect( () => {
361 print("print completed\n");
362 this.delete_cookiefile();
367 var ps = new Gtk.PrintSettings();
368 ps.set_printer("Print to File");
369 ps.set("output-file-format", "pdf");
370 ps.set("output-uri", "file://" + webkitpdf.opt_target_pdf);
373 pe.set_print_settings(ps);
380 var pe = new Gtk.PrintOperation();
381 pe.export_filename = webkitpdf.opt_target_pdf;
382 print("got callback for print\n");
384 /*pe.failed.connect(() => {
385 print("print failed\n");
390 pe.done.connect(() => {
391 print("print completed\n");
392 this.delete_cookiefile();
397 //pe.run_dialog(_this.el);
403 pe.set_default_page_setup(psetup);
404 print("Calling Print?\n");
405 //pe.run_dialog(_this.el);
412 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
421 public void printpng () {
422 //var filename = "/tmp/test.pdf";
423 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
425 //if (le != LoadEvent.FINISHED) {
429 // what size is the documet.
431 print("making screenshot\n");
435 // fix vapi - get_snapshot add '?' to all null.
436 var pixmap = _this.view.el.get_snapshot( null );
442 // add out to gdkpixmap.get_size
443 pixmap.get_size( out w , out h);
444 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
445 print("pixbuf size: %d x %d\n ", w,h);
449 pixbuf.save(webkitpdf.opt_target_png, "png");
450 print("Saved to %s\n", webkitpdf.opt_target_png);
451 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
452 // var sf = view.get_snapshot.end(res);
454 // sf.write_to_png(Browser.opt_target_png);
464 public void delete_cookiefile () {
465 // _this -> owner BrowserWindow
466 // cookie_file is created by BrowserWindow
467 if(_this.cookie_file.length >0 ){
468 print("deleting tmp file %s\n",_this.cookie_file);
469 File file = File.new_for_path (_this.cookie_file);
471 if (file.query_exists () == true) {
475 stdout.printf ("Error: %s\n", e.message);