BrowserWindow.vala
[app.webkitpdf] / BrowserWindow.vala
1 static BrowserWindow  _BrowserWindow;
2
3 public class BrowserWindow : Object
4 {
5     public Gtk.Window el;
6     private BrowserWindow  _this;
7
8     public static BrowserWindow singleton()
9     {
10         if (_BrowserWindow == null) {
11             _BrowserWindow= new BrowserWindow();
12         }
13         return _BrowserWindow;
14     }
15     public Xcls_scrolled_window scrolled_window;
16     public Xcls_view view;
17
18         // my vars (def)
19
20     // ctor
21     public BrowserWindow()
22     {
23         _this = this;
24         this.el = new Gtk.Window( Gtk.WindowType.TOPLEVEL );
25
26         // my vars (dec)
27
28         // set gobject values
29         var child_0 = new Xcls_scrolled_window( _this );
30         child_0.ref();
31         this.el.add (  child_0.el  );
32
33         //listeners
34         this.el.destroy.connect( () => {
35             Gtk.main_quit();
36         });
37         this.el.show.connect( () => {
38                 
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;
42                                 
43                 
44          this.el.resize(w,h);
45         
46          // we have to do silly stuff with blogspot!!?!?
47          var blog = webkitpdf.opt_url;
48         
49         
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/");
54           }
55           
56          if(webkitpdf.opt_cookies.length > 0) {
57              print("setting cookie %s\n",webkitpdf.opt_cookies);
58              var ses = WebKit.get_default_session();
59              
60              var cookie_jar = new Soup.CookieJar();
61                             var Soup.URI uri = new Soup.URI (blog);
62                             var domain = uri.get_host();
63                             
64                             string[] lines = webkitpdf.opt_cookies.split ("=", 2);
65                             
66                                  cookie_jar.set_accept_policy(Soup.CookieJarAcceptPolicy.ALWAYS);
67                             cookie_jar.add_cookie();
68                                  sess.add_feature(cookie_jar);
69                       
70          } 
71                 
72          print("loading url %s\n",blog);        
73          this.view.el.load_uri(blog);
74                 
75          // if we have not finished in 25 seconds
76          // call printit...
77          var cd = 25;
78            GLib.Timeout.add_seconds(1, () => { 
79             cd--;
80             print("timeout 25 seconds :%d", cd);
81             if (cd < 1) {
82                print("calling printit - timed out");
83                _this.view.printit();
84                return false;                    
85             }
86             return true;
87         
88          });
89                 
90         
91         });
92     }
93
94     // user defined functions
95     public class Xcls_scrolled_window : Object
96     {
97         public Gtk.ScrolledWindow el;
98         private BrowserWindow  _this;
99
100
101             // my vars (def)
102
103         // ctor
104         public Xcls_scrolled_window(BrowserWindow _owner )
105         {
106             _this = _owner;
107             _this.scrolled_window = this;
108             this.el = new Gtk.ScrolledWindow( null, null );
109
110             // my vars (dec)
111
112             // set gobject values
113             var child_0 = new Xcls_view( _this );
114             child_0.ref();
115             this.el.add (  child_0.el  );
116
117             // init method
118
119             {
120                this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
121             }
122         }
123
124         // user defined functions
125     }
126     public class Xcls_view : Object
127     {
128         public WebKit.WebView el;
129         private BrowserWindow  _this;
130
131
132             // my vars (def)
133         public bool print_started;
134         public int load_try;
135
136         // ctor
137         public Xcls_view(BrowserWindow _owner )
138         {
139             _this = _owner;
140             _this.view = this;
141             this.el = new WebKit.WebView();
142
143             // my vars (dec)
144             this.print_started = false;
145             this.load_try = 0;
146
147             // set gobject values
148
149             // init method
150
151             {
152                   this.load_try = 0;
153             
154             #if GTK3
155                    this.el.load_changed.connect( (ev ) => {
156                      if (ev != WebKit.LoadEvent.FINISHED) {
157                            return;       
158                        }
159                                   
160                            this.printit(); 
161                           
162                          
163                         });
164             
165             #else
166              //listeners
167                       //  this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
168                         //  print("resource req. started %s\n", netreq.uri);
169                              
170                       //  });
171                         
172                         this.el.load_finished.connect( ( ) => {
173                                 
174                                 
175                                this.printit(); 
176                          
177                         });
178             #endif
179             }
180         }
181
182         // user defined functions
183         public void printit () {
184            print("load_finsihed\n");
185            
186            if (this.print_started) {
187                print("load_finsihed - already printing..%d %s\n", this.load_try, this.el.uri);
188            
189               
190               
191               
192               return;
193           }
194            
195          
196             this.print_started = true; // flag to stop double call.
197             
198             
199          // inject any javascript if needed..
200          if (webkitpdf.opt_inject_js != null) {
201                 
202             print("injecting javascript files \n");
203             foreach(var inject_js in  webkitpdf.opt_inject_js) {
204             print("inject? %s\n", inject_js);
205                string str;
206                try {
207                   FileUtils.get_contents( inject_js, out str);
208                } catch(FileError e) {
209                   print("Failed to inject %s\n",  inject_js);
210                   continue;
211                }
212         #if GTK3
213                GLib.MainLoop mainloop = new GLib.MainLoop();
214                this.el.run_javascript.begin(str, null, (obj,res) => {
215                   mainloop.quit();
216                });
217                mainloop.run();
218                                 
219         #else                   
220                this.el.execute_script(str);
221         #endif
222             }
223          }
224                   
225          print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
226          if (webkitpdf.opt_target_pdf != null) {
227             GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
228                print("delay done URL: %s\n", this.el.uri);
229                         
230                                  
231                this.printpdf();
232                 return false;
233             }, GLib.Priority.DEFAULT);
234             return;
235          }
236                         
237         
238         #if GTK3
239          print("PNG not supported in webkit2\n");
240          return;
241         #else
242                         
243             GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
244            
245                         
246             // ----------------- JPEG 
247             // resize the window...
248             // window.document.documentElement.scrollHeight
249             var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
250             print("Scroll height %d\n", scroll_height);
251             if (scroll_height> 1024 || webkitpdf.opt_width > 0  || webkitpdf.opt_height > 0  ) {
252                var w = webkitpdf.opt_width > 0 ?  webkitpdf.opt_width  : 1200;
253                var h = int.min(scroll_height, 6000);
254                _this.scrolled_window.el.set_size_request( w, h); 
255                _this.el.resize (w+50, h+50);
256                print("Resize to %d, %d\n", w,h);
257                 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => { 
258                   this.printpng();
259                   return false;
260                  }, GLib.Priority.DEFAULT);
261                    return false;
262             }
263             this.printpng();            
264             return false;
265            }, GLib.Priority.DEFAULT);
266         
267         
268         #endif    
269             
270         }
271         public bool printpdf () {
272         
273         
274         
275             var list = Gtk.PaperSize.get_paper_sizes(false);
276              var psetup = new Gtk.PageSetup();
277              for(var i = 0; i < list.length(); i++ ) {
278                  var entry = list.nth_data(i).copy();
279                  if (entry.get_name() == "iso_a2") {
280                      psetup.set_paper_size(entry);
281                  }
282              }
283                     
284         
285         #if GTK3         
286         
287             var  pe = new  WebKit.PrintOperation(this.el);
288             pe.set_page_setup(psetup);
289             pe.finished.connect( () => {
290                print("print completed\n");
291                 Gtk.main_quit();
292                         
293             });
294                         
295             var ps = new Gtk.PrintSettings();
296              ps.set_printer("Print to File");
297              ps.set("output-file-format", "pdf");
298              ps.set("output-uri", "file://" + webkitpdf.opt_target_pdf);
299         
300                         
301             pe.set_print_settings(ps);
302                         
303         
304             pe.print();
305                                   
306         
307         #else       
308             var  pe = new  Gtk.PrintOperation();
309             pe.export_filename = webkitpdf.opt_target_pdf;
310             print("got callback for print\n");
311             pe.ref();
312             /*pe.failed.connect(() => {
313                print("print failed\n");
314                 Gtk.main_quit();
315                         
316             })
317             */;
318              pe.done.connect(() => {
319                print("print completed\n");
320                 Gtk.main_quit();
321                         
322             });
323                         
324             //pe.run_dialog(_this.el);
325                  
326                         
327                         
328         
329         
330              pe.set_default_page_setup(psetup);
331              print("Calling Print?\n");
332              //pe.run_dialog(_this.el);
333                     
334                     
335         
336         
337         
338              try {
339                 this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
340              } catch (Error e) {
341                // print failed...
342             }
343         #endif      
344                     
345                      
346             return false;
347         }
348         public void printpng () {
349             //var filename = "/tmp/test.pdf";
350             //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
351         
352             //if (le != LoadEvent.FINISHED) {
353             //    return;
354            // }
355            
356         // what size is the documet.
357         
358              print("making screenshot\n");
359         #if GTK3
360         
361         #else     
362             // fix vapi - get_snapshot add '?' to all null.
363             var pixmap = _this.view.el.get_snapshot( null );
364         
365             
366             
367             int w,h;
368             
369             // add out to gdkpixmap.get_size
370          pixmap.get_size( out  w , out   h);
371          var pixbuf  = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
372          print("pixbuf size: %d x %d\n ", w,h);
373                  
374             
375             
376              pixbuf.save(webkitpdf.opt_target_png, "png");
377              print("Saved to %s\n", webkitpdf.opt_target_png);
378             //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
379             //    var sf = view.get_snapshot.end(res);
380         
381             //    sf.write_to_png(Browser.opt_target_png);
382             //});
383             Gtk.main_quit();
384              
385         #endif   
386             
387             
388         
389         }
390     }
391
392
393 }