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