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