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