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