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