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