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