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