webkitpdf.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                 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                 
147                         print("injecting javascript files \n");
148                         foreach(var inject_js in  webkitpdf.opt_inject_js) {
149                         print("inject? %s\n", inject_js);
150                                 string str;
151                                 try {
152                                         FileUtils.get_contents( inject_js, out str);
153                                 } catch(FileError e) {
154                                         print("Failed to inject %s\n",  inject_js);
155                                         continue;
156                                 }
157                                 this.el.execute_script(str);
158                         }
159                 }
160                 
161                 
162                 
163                 print("calling print in %d seconds\n", (int)webkitpdf.opt_delay);
164                 if (webkitpdf.opt_target_pdf != null) {
165                         GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
166         
167                                 this.printpdf();
168                                  return false;
169                         }, GLib.Priority.DEFAULT);
170                 }
171                         
172                         
173             GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
174            
175                         
176                         // ----------------- JPEG 
177                         // resize the window...
178                         // window.document.documentElement.scrollHeight
179                         var scroll_height = (int) _this.view.el.get_dom_document().document_element.scroll_height;
180                         print("Scroll height %d\n", scroll_height);
181                         if (scroll_height> 1024) {
182                                 //_this.scrolled_window.el.set_size_request(1200, int.min(scroll_height, 6000));
183                                 _this.scrolled_window.el.set_size_request(1200, int.min(scroll_height, 6000));
184                                 print("Resize to %d\n", scroll_height);
185                             GLib.Timeout.add_seconds(webkitpdf.opt_delay, () => { 
186                                         this.printpng();
187                                         return false;
188                                   }, GLib.Priority.DEFAULT);
189                             return false;
190                         }
191                         this.printpng();                
192                         return false;
193                   }, GLib.Priority.DEFAULT);
194         
195         
196             
197             
198         }
199         public bool printpdf () {
200                  
201                         var  pe = new  Gtk.PrintOperation();
202                         pe.export_filename = webkitpdf.opt_target_pdf;
203                         print("got callback for print");
204                         pe.ref();
205                         /*pe.failed.connect(() => {
206                                 print("print failed\n");
207                                  Gtk.main_quit();
208                         
209                         })
210                         */;
211                          pe.done.connect(() => {
212                                 print("print completed\n");
213                                  Gtk.main_quit();
214                         
215                         });
216                         
217                         //pe.run_dialog(_this.el);
218                  
219                         
220                     var list = Gtk.PaperSize.get_paper_sizes(false);
221                     var psetup = new Gtk.PageSetup();
222                     for(var i = 0; i < list.length(); i++ ) {
223                         var entry = list.nth_data(i).copy();
224                         if (entry.get_name() == "iso_a2") {
225                             psetup.set_paper_size(entry);
226                         }
227                     }
228                         
229         
230         
231                     pe.set_default_page_setup(psetup);
232                     print("Calling Print?\n");
233                     //pe.run_dialog(_this.el);
234                     try {
235                             this.el.get_main_frame().print_full(pe,Gtk.PrintOperationAction.EXPORT);
236                     } catch (Error e) {
237                                 // print failed...
238                         }
239                     
240                     
241                      
242                         return false;
243         }
244         public void printpng () {
245             //var filename = "/tmp/test.pdf";
246             //print("load_changed %d ?= %d\n", le, LoadEvent.FINISHED);
247         
248             //if (le != LoadEvent.FINISHED) {
249             //    return;
250            // }
251            
252         // what size is the documet.
253         
254              print("making screenshot\n");
255              
256             // fix vapi - get_snapshot add '?' to all null.
257             var pixmap = _this.view.el.get_snapshot( null );
258         
259             
260             
261             int w,h;
262             
263             // add out to gdkpixmap.get_size
264                 pixmap.get_size( out  w , out   h);
265                 var pixbuf  = Gdk.pixbuf_get_from_drawable(null, pixmap, null , 0, 0, 0, 0, w, h);
266                 print("pixbuf size: %d x %d\n ", w,h);
267                  
268             
269             
270              pixbuf.save(webkitpdf.opt_target_png, "png");
271              print("Saved to %s\n", webkitpdf.opt_target_png);
272             //view.get_snapshot.begin(WebKit.SnapshotRegion.FULL_DOCUMENT, WebKit.SnapshotOptions.NONE, null, (obj, res) => {
273             //    var sf = view.get_snapshot.end(res);
274         
275             //    sf.write_to_png(Browser.opt_target_png);
276             //});
277             Gtk.main_quit();
278              
279            
280             
281             
282         
283         }
284     }
285
286
287 }