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