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