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