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