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