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