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