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 File file = File.new_for_path(GLib.Environment.get_user_cache_dir() + '/cookie.txt' );
71 FileOutputStream ostream = file.replace (null, false, FileCreateFlags.NONE);
72 DataOutputStream dostream = new DataOutputStream (ostream);
73 dostream.put_string (uri.get_host() + "\tTRUE\t/\t2147483647\t" + cookies[0] + "\t" + cookies[1] );
75 stdout.printf ("Error: %s\n", e.message);
78 var sess = WebKit.get_default_session();
79 var cookie_jar = new Soup.CookieJar();
80 cookie_jar.set_accept_policy(Soup.CookieJarAcceptPolicy.ALWAYS);
81 cookie_jar.add_cookie(new Soup.Cookie (cookies[0], cookies[1], uri.get_host(), "/", -1));
82 sess.add_feature(cookie_jar);
89 print("loading url %s\n",blog);
90 this.view.el.load_uri(blog);
92 // if we have not finished in 25 seconds
95 GLib.Timeout.add_seconds(1, () => {
97 print("timeout 25 seconds :%d", cd);
99 print("calling printit - timed out");
100 _this.view.printit();
111 // user defined functions
112 public class Xcls_scrolled_window : Object
114 public Gtk.ScrolledWindow el;
115 private BrowserWindow _this;
121 public Xcls_scrolled_window(BrowserWindow _owner )
124 _this.scrolled_window = this;
125 this.el = new Gtk.ScrolledWindow( null, null );
129 // set gobject values
130 var child_0 = new Xcls_view( _this );
132 this.el.add ( child_0.el );
137 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
141 // user defined functions
143 public class Xcls_view : Object
145 public WebKit.WebView el;
146 private BrowserWindow _this;
150 public bool print_started;
154 public Xcls_view(BrowserWindow _owner )
158 this.el = new WebKit.WebView();
161 this.print_started = false;
164 // set gobject values
172 this.el.load_changed.connect( (ev ) => {
173 if (ev != WebKit.LoadEvent.FINISHED) {
184 // this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
185 // print("resource req. started %s\n", netreq.uri);
189 this.el.load_finished.connect( ( ) => {
199 // user defined functions
200 public void printit () {
201 print("load_finsihed\n");
203 if (this.print_started) {
204 print("load_finsihed - already printing..%d %s\n", this.load_try, this.el.uri);
213 this.print_started = true; // flag to stop double call.
216 // inject any javascript if needed..
217 if (webkitpdf.opt_inject_js != null) {
219 print("injecting javascript files \n");
220 foreach(var inject_js in webkitpdf.opt_inject_js) {
221 print("inject? %s\n", inject_js);
224 FileUtils.get_contents( inject_js, out str);
225 } catch(FileError e) {
226 print("Failed to inject %s\n", inject_js);
230 GLib.MainLoop mainloop = new GLib.MainLoop();
231 this.el.run_javascript.begin(str, null, (obj,res) => {
237 this.el.execute_script(str);
242 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
243 if (webkitpdf.opt_target_pdf != null) {
244 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
245 print("delay done URL: %s\n", this.el.uri);
250 }, GLib.Priority.DEFAULT);
256 print("PNG not supported in webkit2\n");
260 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
263 // ----------------- JPEG
264 // resize the window...
265 // window.document.documentElement.scrollHeight
266 var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
267 print("Scroll height %d\n", scroll_height);
268 if (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {
269 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
270 var h = int.min(scroll_height, 6000);
271 _this.scrolled_window.el.set_size_request( w, h);
272 _this.el.resize (w+50, h+50);
273 print("Resize to %d, %d\n", w,h);
274 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => {
277 }, GLib.Priority.DEFAULT);
282 }, GLib.Priority.DEFAULT);
288 public bool printpdf () {
292 var list = Gtk.PaperSize.get_paper_sizes(false);
293 var psetup = new Gtk.PageSetup();
294 for(var i = 0; i < list.length(); i++ ) {
295 var entry = list.nth_data(i).copy();
296 if (entry.get_name() == "iso_a2") {
297 psetup.set_paper_size(entry);
304 var pe = new WebKit.PrintOperation(this.el);
305 pe.set_page_setup(psetup);
306 pe.finished.connect( () => {
307 print("print completed\n");
312 var ps = new Gtk.PrintSettings();
313 ps.set_printer("Print to File");
314 ps.set("output-file-format", "pdf");
315 ps.set("output-uri", "file://" + webkitpdf.opt_target_pdf);
318 pe.set_print_settings(ps);
325 var pe = new Gtk.PrintOperation();
326 pe.export_filename = webkitpdf.opt_target_pdf;
327 print("got callback for print\n");
329 /*pe.failed.connect(() => {
330 print("print failed\n");
335 pe.done.connect(() => {
336 print("print completed\n");
341 //pe.run_dialog(_this.el);
347 pe.set_default_page_setup(psetup);
348 print("Calling Print?\n");
349 //pe.run_dialog(_this.el);
356 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
365 public void printpng () {
366 //var filename = "/tmp/test.pdf";
367 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
369 //if (le != LoadEvent.FINISHED) {
373 // what size is the documet.
375 print("making screenshot\n");
379 // fix vapi - get_snapshot add '?' to all null.
380 var pixmap = _this.view.el.get_snapshot( null );
386 // add out to gdkpixmap.get_size
387 pixmap.get_size( out w , out h);
388 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
389 print("pixbuf size: %d x %d\n ", w,h);
393 pixbuf.save(webkitpdf.opt_target_png, "png");
394 print("Saved to %s\n", webkitpdf.opt_target_png);
395 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
396 // var sf = view.get_snapshot.end(res);
398 // sf.write_to_png(Browser.opt_target_png);