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