BrowserWindow.bjs
[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                 print("loading url\n");
46                 this.view.el.load_uri(webkitpdf.opt_url);
47                 
48                 // if we have not finished in 25 seconds
49                 // call printit...
50                   GLib.Timeout.add_seconds(25, () => { 
51                                 _this.view.printit();
52                                 return true;
53                 });
54                 
55         
56         });
57     }
58
59     // user defined functions
60     public class Xcls_scrolled_window : Object
61     {
62         public Gtk.ScrolledWindow el;
63         private BrowserWindow  _this;
64
65
66             // my vars (def)
67
68         // ctor
69         public Xcls_scrolled_window(BrowserWindow _owner )
70         {
71             _this = _owner;
72             _this.scrolled_window = this;
73             this.el = new Gtk.ScrolledWindow( null, null );
74
75             // my vars (dec)
76
77             // set gobject values
78             var child_0 = new Xcls_view( _this );
79             child_0.ref();
80             this.el.add (  child_0.el  );
81
82             // init method
83
84             {
85                 this.el.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC);
86             }
87         }
88
89         // user defined functions
90     }
91     public class Xcls_view : Object
92     {
93         public WebKit.WebView el;
94         private BrowserWindow  _this;
95
96
97             // my vars (def)
98         public bool print_started;
99
100         // ctor
101         public Xcls_view(BrowserWindow _owner )
102         {
103             _this = _owner;
104             _this.view = this;
105             this.el = new WebKit.WebView();
106
107             // my vars (dec)
108             this.print_started = false;
109
110             // set gobject values
111
112             //listeners
113             this.el.mime_type_policy_decision_requested.connect( (p0, netreq, mimetype, pol ) => {
114              //  print("resource response  %s, %s\n",    webres.get_mime_type(), netres.uri);
115                 print("mimetype:  %s", mimetype);
116              // netres.message.got_headers.connect(() => { 
117              //   print("GOT HEADERS?  [%s] : %s\n", 
118                 //  netres.message.request_headers.get_content_type(null),netres.uri);
119                 //  });
120                 
121                 return true; // default behaviour..
122               
123                
124             });
125             this.el.resource_request_starting.connect( (p0, webres, netreq ) => {
126               print("resource req. started %s\n", netreq.uri);
127                  
128             });
129             this.el.load_finished.connect( ( ) => {
130                          
131                    this.printit();
132              
133             });
134         }
135
136         // user defined functions
137         public void printit () {
138            print("load_finsihed\n");
139            
140            if (this.print_started) {
141               print("load_finsihed - already printing..\n");
142               return;
143           }
144            
145             this.print_started = true; // flag to stop double call.
146             
147             
148                 // inject any javascript if needed..
149                 if (webkitpdf.opt_inject_js != null) {
150                 
151                         print("injecting javascript files \n");
152                         foreach(var inject_js in  webkitpdf.opt_inject_js) {
153                         print("inject? %s\n", inject_js);
154                                 string str;
155                                 try {
156                                         FileUtils.get_contents( inject_js, out str);
157                                 } catch(FileError e) {
158                                         print("Failed to inject %s\n",  inject_js);
159                                         continue;
160                                 }
161                                 this.el.execute_script(str);
162                         }
163                 }
164                 
165                 
166                 
167                 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
168                 if (webkitpdf.opt_target_pdf != null) {
169                         GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
170         
171                                 this.printpdf();
172                                  return false;
173                         }, GLib.Priority.DEFAULT);
174                         return;
175                 }
176                         
177                         
178             GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
179            
180                         
181                         // ----------------- JPEG 
182                         // resize the window...
183                         // window.document.documentElement.scrollHeight
184                         var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
185                         print("Scroll height %d\n", scroll_height);
186                         if (scroll_height> 1024 || webkitpdf.opt_width > 0  || webkitpdf.opt_height > 0  ) {
187                                 var w = webkitpdf.opt_width > 0 ?  webkitpdf.opt_width  : 1200;
188                                 var h = int.min(scroll_height, 6000);
189                                 _this.scrolled_window.el.set_size_request( w, h); 
190                                 _this.el.resize (w+50, h+50);
191                                 print("Resize to %d, %d\n", w,h);
192                             GLib.Timeout.add_seconds(webkitpdf.opt_delay > 0 ? webkitpdf.opt_delay : 1 , () => { 
193                                         this.printpng();
194                                         return false;
195                                   }, GLib.Priority.DEFAULT);
196                             return false;
197                         }
198                         this.printpng();                
199                         return false;
200                   }, GLib.Priority.DEFAULT);
201         
202         
203             
204             
205         }
206         public bool printpdf () {
207                  
208                         var  pe = new  Gtk.PrintOperation();
209                         pe.export_filename = webkitpdf.opt_target_pdf;
210                         print("got callback for print");
211                         pe.ref();
212                         /*pe.failed.connect(() => {
213                                 print("print failed\n");
214                                  Gtk.main_quit();
215                         
216                         })
217                         */;
218                          pe.done.connect(() => {
219                                 print("print completed\n");
220                                  Gtk.main_quit();
221                         
222                         });
223                         
224                         //pe.run_dialog(_this.el);
225                  
226                         
227                     var list = Gtk.PaperSize.get_paper_sizes(false);
228                     var psetup = new Gtk.PageSetup();
229                     for(var i = 0; i < list.length(); i++ ) {
230                         var entry = list.nth_data(i).copy();
231                         if (entry.get_name() == "iso_a2") {
232                             psetup.set_paper_size(entry);
233                         }
234                     }
235                         
236         
237         
238                     pe.set_default_page_setup(psetup);
239                     print("Calling Print?\n");
240                     //pe.run_dialog(_this.el);
241                     try {
242                             this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
243                     } catch (Error e) {
244                                 // print failed...
245                         }
246                     
247                     
248                      
249                         return false;
250         }
251         public void printpng () {
252             //var filename = "/tmp/test.pdf";
253             //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
254         
255             //if (le != LoadEvent.FINISHED) {
256             //    return;
257            // }
258            
259         // what size is the documet.
260         
261              print("making screenshot\n");
262              
263             // fix vapi - get_snapshot add '?' to all null.
264             var pixmap = _this.view.el.get_snapshot( null );
265         
266             
267             
268             int w,h;
269             
270             // add out to gdkpixmap.get_size
271                 pixmap.get_size( out  w , out   h);
272                 var pixbuf  = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
273                 print("pixbuf size: %d x %d\n ", w,h);
274                  
275             
276             
277              pixbuf.save(webkitpdf.opt_target_png, "png");
278              print("Saved to %s\n", webkitpdf.opt_target_png);
279             //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
280             //    var sf = view.get_snapshot.end(res);
281         
282             //    sf.write_to_png(Browser.opt_target_png);
283             //});
284             Gtk.main_quit();
285              
286            
287             
288             
289         
290         }
291     }
292
293
294 }