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 >=2 ) {
66 var context = WebKit.WebContext.get_default();
67 var cookie_manager = context.get_cookie_manager();
68 cookie_manager.set_accept_policy(WebKit.CookieAcceptPolicy.NEVER);
69 string scheme = uri.get_scheme ();
71 if (scheme=="https") {
75 FileIOStream iostream;
76 File file = File.new_tmp ("cookie-XXXXXX.txt", out iostream);
77 this.cookie_file = file.get_path ();
78 print("cookie tmp file name: %s\n", file.get_path ());
80 OutputStream ostream = iostream.output_stream;
81 DataOutputStream dostream = new DataOutputStream (ostream);
83 dostream.put_string (uri.get_host() + "\tTRUE\t/\t"+ssl+"\t2147483647\t" + cookies[0] + "\t" + cookies[1] );
84 cookie_manager.set_persistent_storage(file.get_path(), WebKit.CookiePersistentStorage.TEXT);
86 stdout.printf ("Error: %s\n", e.message);
89 var sess = WebKit.get_default_session();
90 var cookie_jar = new Soup.CookieJar();
91 cookie_jar.set_accept_policy(Soup.CookieJarAcceptPolicy.NEVER);
92 cookie_jar.add_cookie(new Soup.Cookie (cookies[0], cookies[1], uri.get_host(), "/", -1));
93 sess.add_feature(cookie_jar);
100 print("loading url %s\n",blog);
101 this.view.el.load_uri(blog);
103 // if we have not finished in 25 seconds
106 GLib.Timeout.add_seconds(1, () => {
108 print("timeout 25 seconds :%d", cd);
110 print("calling printit - timed out");
111 _this.view.printit();
123 // user defined functions
124 public class Xcls_scrolled_window : Object
126 public Gtk.ScrolledWindow el;
127 private BrowserWindow _this;
133 public Xcls_scrolled_window(BrowserWindow _owner )
136 _this.scrolled_window = this;
137 this.el = new Gtk.ScrolledWindow( null, null );
141 // set gobject values
142 var child_0 = new Xcls_view( _this );
144 this.el.add ( child_0.el );
149 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
153 // user defined functions
155 public class Xcls_view : Object
157 public WebKit.WebView el;
158 private BrowserWindow _this;
162 public bool print_started;
166 public Xcls_view(BrowserWindow _owner )
170 this.el = new WebKit.WebView();
173 this.print_started = false;
176 // set gobject values
184 this.el.load_changed.connect( (ev ) => {
185 if (ev != WebKit.LoadEvent.FINISHED) {
196 // this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
197 // print("resource req. started %s\n", netreq.uri);
201 this.el.load_finished.connect( ( ) => {
211 // user defined functions
212 public void printit () {
213 print("load_finsihed\n");
215 if (this.print_started) {
216 print("load_finsihed - already printing..%d %s\n", this.load_try, this.el.uri);
225 this.print_started = true; // flag to stop double call.
228 // inject any javascript if needed..
229 if (webkitpdf.opt_inject_js != null) {
231 print("injecting javascript files \n");
232 foreach(var inject_js in webkitpdf.opt_inject_js) {
233 print("inject? %s\n", inject_js);
236 FileUtils.get_contents( inject_js, out str);
237 } catch(FileError e) {
238 print("Failed to inject %s\n", inject_js);
242 GLib.MainLoop mainloop = new GLib.MainLoop();
243 this.el.run_javascript.begin(str, null, (obj,res) => {
249 this.el.execute_script(str);
254 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
255 if (webkitpdf.opt_target_pdf != null) {
256 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
257 print("delay done URL: %s\n", this.el.uri);
262 }, GLib.Priority.DEFAULT);
268 print("PNG not supported in webkit2\n");
272 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
275 // ----------------- JPEG
276 // resize the window...
277 // window.document.documentElement.scrollHeight
278 var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
279 print("Scroll height %d\n", scroll_height);
280 if (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {
281 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
282 var h = int.min(scroll_height, 6000);
283 _this.scrolled_window.el.set_size_request( w, h);
284 _this.el.resize (w+50, h+50);
285 print("Resize to %d, %d\n", w,h);
286 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => {
289 }, GLib.Priority.DEFAULT);
294 }, GLib.Priority.DEFAULT);
300 public bool printpdf () {
304 var list = Gtk.PaperSize.get_paper_sizes(false);
305 var psetup = new Gtk.PageSetup();
306 for(var i = 0; i < list.length(); i++ ) {
307 var entry = list.nth_data(i).copy();
308 if (entry.get_name() == "iso_a2") {
309 psetup.set_paper_size(entry);
316 var pe = new WebKit.PrintOperation(this.el);
317 pe.set_page_setup(psetup);
318 pe.finished.connect( () => {
319 print("print completed\n");
325 var ps = new Gtk.PrintSettings();
326 ps.set_printer("Print to File");
327 ps.set("output-file-format", "pdf");
328 ps.set("output-uri", "file://" + webkitpdf.opt_target_pdf);
331 pe.set_print_settings(ps);
338 var pe = new Gtk.PrintOperation();
339 pe.export_filename = webkitpdf.opt_target_pdf;
340 print("got callback for print\n");
342 /*pe.failed.connect(() => {
343 print("print failed\n");
348 pe.done.connect(() => {
349 print("print completed\n");
354 //pe.run_dialog(_this.el);
360 pe.set_default_page_setup(psetup);
361 print("Calling Print?\n");
362 //pe.run_dialog(_this.el);
369 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
378 public void printpng () {
379 //var filename = "/tmp/test.pdf";
380 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
382 //if (le != LoadEvent.FINISHED) {
386 // what size is the documet.
388 print("making screenshot\n");
392 // fix vapi - get_snapshot add '?' to all null.
393 var pixmap = _this.view.el.get_snapshot( null );
399 // add out to gdkpixmap.get_size
400 pixmap.get_size( out w , out h);
401 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
402 print("pixbuf size: %d x %d\n ", w,h);
406 pixbuf.save(webkitpdf.opt_target_png, "png");
407 print("Saved to %s\n", webkitpdf.opt_target_png);
408 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
409 // var sf = view.get_snapshot.end(res);
411 // sf.write_to_png(Browser.opt_target_png);