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