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