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 cookie_jar = new Soup.CookieJar();
61 var uri = new Soup.URI (blog);
63 string[] cookies = webkitpdf.opt_cookies.split ("=", 2);
65 if(cookies.length >=2 ) {
70 var context = WebKit.WebContext.get_default();
72 var cookie_manager = context.get_cookie_manager();
75 var sess = WebKit.get_default_session();
77 cookie_jar.set_accept_policy(Soup.CookieJarAcceptPolicy.ALWAYS);
79 cookie_jar.add_cookie(new Soup.Cookie (cookies[0], cookies[1], uri.get_host(), "/", -1));
81 sess.add_feature(cookie_jar);
85 print("loading url %s\n",blog);
86 this.view.el.load_uri(blog);
88 // if we have not finished in 25 seconds
91 GLib.Timeout.add_seconds(1, () => {
93 print("timeout 25 seconds :%d", cd);
95 print("calling printit - timed out");
107 // user defined functions
108 public class Xcls_scrolled_window : Object
110 public Gtk.ScrolledWindow el;
111 private BrowserWindow _this;
117 public Xcls_scrolled_window(BrowserWindow _owner )
120 _this.scrolled_window = this;
121 this.el = new Gtk.ScrolledWindow( null, null );
125 // set gobject values
126 var child_0 = new Xcls_view( _this );
128 this.el.add ( child_0.el );
133 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
137 // user defined functions
139 public class Xcls_view : Object
141 public WebKit.WebView el;
142 private BrowserWindow _this;
146 public bool print_started;
150 public Xcls_view(BrowserWindow _owner )
154 this.el = new WebKit.WebView();
157 this.print_started = false;
160 // set gobject values
168 this.el.load_changed.connect( (ev ) => {
169 if (ev != WebKit.LoadEvent.FINISHED) {
180 // this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
181 // print("resource req. started %s\n", netreq.uri);
185 this.el.load_finished.connect( ( ) => {
195 // user defined functions
196 public void printit () {
197 print("load_finsihed\n");
199 if (this.print_started) {
200 print("load_finsihed - already printing..%d %s\n", this.load_try, this.el.uri);
209 this.print_started = true; // flag to stop double call.
212 // inject any javascript if needed..
213 if (webkitpdf.opt_inject_js != null) {
215 print("injecting javascript files \n");
216 foreach(var inject_js in webkitpdf.opt_inject_js) {
217 print("inject? %s\n", inject_js);
220 FileUtils.get_contents( inject_js, out str);
221 } catch(FileError e) {
222 print("Failed to inject %s\n", inject_js);
226 GLib.MainLoop mainloop = new GLib.MainLoop();
227 this.el.run_javascript.begin(str, null, (obj,res) => {
233 this.el.execute_script(str);
238 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
239 if (webkitpdf.opt_target_pdf != null) {
240 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
241 print("delay done URL: %s\n", this.el.uri);
246 }, GLib.Priority.DEFAULT);
252 print("PNG not supported in webkit2\n");
256 GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => {
259 // ----------------- JPEG
260 // resize the window...
261 // window.document.documentElement.scrollHeight
262 var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
263 print("Scroll height %d\n", scroll_height);
264 if (scroll_height> 1024 || webkitpdf.opt_width > 0 || webkitpdf.opt_height > 0 ) {
265 var w = webkitpdf.opt_width > 0 ? webkitpdf.opt_width : 1200;
266 var h = int.min(scroll_height, 6000);
267 _this.scrolled_window.el.set_size_request( w, h);
268 _this.el.resize (w+50, h+50);
269 print("Resize to %d, %d\n", w,h);
270 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => {
273 }, GLib.Priority.DEFAULT);
278 }, GLib.Priority.DEFAULT);
284 public bool printpdf () {
288 var list = Gtk.PaperSize.get_paper_sizes(false);
289 var psetup = new Gtk.PageSetup();
290 for(var i = 0; i < list.length(); i++ ) {
291 var entry = list.nth_data(i).copy();
292 if (entry.get_name() == "iso_a2") {
293 psetup.set_paper_size(entry);
300 var pe = new WebKit.PrintOperation(this.el);
301 pe.set_page_setup(psetup);
302 pe.finished.connect( () => {
303 print("print completed\n");
308 var ps = new Gtk.PrintSettings();
309 ps.set_printer("Print to File");
310 ps.set("output-file-format", "pdf");
311 ps.set("output-uri", "file://" + webkitpdf.opt_target_pdf);
314 pe.set_print_settings(ps);
321 var pe = new Gtk.PrintOperation();
322 pe.export_filename = webkitpdf.opt_target_pdf;
323 print("got callback for print\n");
325 /*pe.failed.connect(() => {
326 print("print failed\n");
331 pe.done.connect(() => {
332 print("print completed\n");
337 //pe.run_dialog(_this.el);
343 pe.set_default_page_setup(psetup);
344 print("Calling Print?\n");
345 //pe.run_dialog(_this.el);
352 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
361 public void printpng () {
362 //var filename = "/tmp/test.pdf";
363 //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
365 //if (le != LoadEvent.FINISHED) {
369 // what size is the documet.
371 print("making screenshot\n");
375 // fix vapi - get_snapshot add '?' to all null.
376 var pixmap = _this.view.el.get_snapshot( null );
382 // add out to gdkpixmap.get_size
383 pixmap.get_size( out w , out h);
384 var pixbuf = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
385 print("pixbuf size: %d x %d\n ", w,h);
389 pixbuf.save(webkitpdf.opt_target_png, "png");
390 print("Saved to %s\n", webkitpdf.opt_target_png);
391 //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
392 // var sf = view.get_snapshot.end(res);
394 // sf.write_to_png(Browser.opt_target_png);