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