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