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