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