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