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