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