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                 print("deleting cookie tmp file name: %s\n", filename);
120                 
121
122         
123         });
124     }
125
126     // user defined functions
127     public class Xcls_scrolled_window : Object
128     {
129         public Gtk.ScrolledWindow el;
130         private BrowserWindow  _this;
131
132
133             // my vars (def)
134
135         // ctor
136         public Xcls_scrolled_window(BrowserWindow _owner )
137         {
138             _this = _owner;
139             _this.scrolled_window = this;
140             this.el = new Gtk.ScrolledWindow( null, null );
141
142             // my vars (dec)
143
144             // set gobject values
145             var child_0 = new Xcls_view( _this );
146             child_0.ref();
147             this.el.add (  child_0.el  );
148
149             // init method
150
151             {
152                this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
153             }
154         }
155
156         // user defined functions
157     }
158     public class Xcls_view : Object
159     {
160         public WebKit.WebView el;
161         private BrowserWindow  _this;
162
163
164             // my vars (def)
165         public bool print_started;
166         public int load_try;
167
168         // ctor
169         public Xcls_view(BrowserWindow _owner )
170         {
171             _this = _owner;
172             _this.view = this;
173             this.el = new WebKit.WebView();
174
175             // my vars (dec)
176             this.print_started = false;
177             this.load_try = 0;
178
179             // set gobject values
180
181             // init method
182
183             {
184                   this.load_try = 0;
185             
186             #if GTK3
187                    this.el.load_changed.connect( (ev ) => {
188                      if (ev != WebKit.LoadEvent.FINISHED) {
189                            return;       
190                        }
191                                   
192                            this.printit(); 
193                           
194                          
195                         });
196             
197             #else
198              //listeners
199                       //  this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
200                         //  print("resource req. started %s\n", netreq.uri);
201                              
202                       //  });
203                         
204                         this.el.load_finished.connect( ( ) => {
205                                 
206                                 
207                                this.printit(); 
208                          
209                         });
210             #endif
211             }
212         }
213
214         // user defined functions
215         public void printit () {
216            print("load_finsihed\n");
217            
218            if (this.print_started) {
219                print("load_finsihed - already printing..%d %s\n", this.load_try, this.el.uri);
220            
221               
222               
223               
224               return;
225           }
226            
227          
228             this.print_started = true; // flag to stop double call.
229             
230             
231          // inject any javascript if needed..
232          if (webkitpdf.opt_inject_js != null) {
233                 
234             print("injecting javascript files \n");
235             foreach(var inject_js in  webkitpdf.opt_inject_js) {
236             print("inject? %s\n", inject_js);
237                string str;
238                try {
239                   FileUtils.get_contents( inject_js, out str);
240                } catch(FileError e) {
241                   print("Failed to inject %s\n",  inject_js);
242                   continue;
243                }
244         #if GTK3
245                GLib.MainLoop mainloop = new GLib.MainLoop();
246                this.el.run_javascript.begin(str, null, (obj,res) => {
247                   mainloop.quit();
248                });
249                mainloop.run();
250                                 
251         #else                   
252                this.el.execute_script(str);
253         #endif
254             }
255          }
256                   
257          print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
258          if (webkitpdf.opt_target_pdf != null) {
259             GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
260                print("delay done URL: %s\n", this.el.uri);
261                         
262                                  
263                this.printpdf();
264                 return false;
265             }, GLib.Priority.DEFAULT);
266             return;
267          }
268                         
269         
270         #if GTK3
271          print("PNG not supported in webkit2\n");
272          return;
273         #else
274                         
275             GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
276            
277                         
278             // ----------------- JPEG 
279             // resize the window...
280             // window.document.documentElement.scrollHeight
281             var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
282             print("Scroll height %d\n", scroll_height);
283             if (scroll_height> 1024 || webkitpdf.opt_width > 0  || webkitpdf.opt_height > 0  ) {
284                var w = webkitpdf.opt_width > 0 ?  webkitpdf.opt_width  : 1200;
285                var h = int.min(scroll_height, 6000);
286                _this.scrolled_window.el.set_size_request( w, h); 
287                _this.el.resize (w+50, h+50);
288                print("Resize to %d, %d\n", w,h);
289                 GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => { 
290                   this.printpng();
291                   return false;
292                  }, GLib.Priority.DEFAULT);
293                    return false;
294             }
295             this.printpng();            
296             return false;
297            }, GLib.Priority.DEFAULT);
298         
299         
300         #endif    
301             
302         }
303         public bool printpdf () {
304         
305         
306         
307             var list = Gtk.PaperSize.get_paper_sizes(false);
308              var psetup = new Gtk.PageSetup();
309              for(var i = 0; i < list.length(); i++ ) {
310                  var entry = list.nth_data(i).copy();
311                  if (entry.get_name() == "iso_a2") {
312                      psetup.set_paper_size(entry);
313                  }
314              }
315                     
316         
317         #if GTK3         
318         
319             var  pe = new  WebKit.PrintOperation(this.el);
320             pe.set_page_setup(psetup);
321             pe.finished.connect( () => {
322                print("print completed\n");
323                 Gtk.main_quit();
324                         
325             });
326                         
327             var ps = new Gtk.PrintSettings();
328              ps.set_printer("Print to File");
329              ps.set("output-file-format", "pdf");
330              ps.set("output-uri", "file://" + webkitpdf.opt_target_pdf);
331         
332                         
333             pe.set_print_settings(ps);
334                         
335         
336             pe.print();
337                                   
338         
339         #else       
340             var  pe = new  Gtk.PrintOperation();
341             pe.export_filename = webkitpdf.opt_target_pdf;
342             print("got callback for print\n");
343             pe.ref();
344             /*pe.failed.connect(() => {
345                print("print failed\n");
346                 Gtk.main_quit();
347                         
348             })
349             */;
350              pe.done.connect(() => {
351                print("print completed\n");
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
424
425 }