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