fix #7988 - prnewsthailand fix
[app.webkitpdf] / webkitpdf.vala
1
2 /*
3 sudo valac   --vapidir=. --thread  webkitpdf.vala  BrowserWindow.vala  --vapidir=./vapi \
4     --pkg glib-2.0 --pkg webkit-1.0 --pkg gtk+-2.0 -o /usr/bin/webkitpdf --target-glib=2.32 
5   
6 sudo valac -D GTK3   webkitpdf.vala  BrowserWindow.vala  --vapidir=./vapi \
7     --pkg glib-2.0 --pkg webkit2gtk-4.0 --pkg gtk+-3.0 --pkg gio-2.0 -o /usr/bin/webkitpdf2 --target-glib=2.32 
8   
9 sudo valac -D GTK3   webkitpdf.vala  BrowserWindow.vala  --vapidir=./vapi \
10     --pkg glib-2.0 --pkg webkit2gtk-4.0 --pkg gtk+-3.0 --pkg gio-2.0 -o /usr/bin/webkitpdf2 --target-glib=2.32 
11    
12     
13 */
14 public class webkitpdf {
15  
16         public static bool opt_disable_javascript = false;
17     public static string? opt_url = null;
18         public static uint opt_delay = 0;
19         public static int opt_width = 0;
20         public static int opt_height = 0;
21         public static string? opt_target_png = null;
22         public static string? opt_target_pdf = null;    
23         [CCode (array_length = false, array_null_terminated = true)]
24         public static string[] opt_inject_js;
25         public static string? opt_cookies = null;
26         
27         const OptionEntry[] options = {
28                 
29                         { "width", 0, 0, OptionArg.INT, ref opt_width, "Width (default 1200)", null },                  
30                         { "height", 0, 0, OptionArg.INT, ref opt_height, "Height (PNG only) (default to expand to fit)", null },                                                
31                         { "url", 0, 0, OptionArg.STRING, ref opt_url, "Url to grab", null },
32                         { "delay", 0, 0, OptionArg.INT, ref opt_delay, "Delay before snapshot in seconds", null },
33                         { "png", 0, 0, OptionArg.STRING, ref opt_target_png, "File to write (PNG)", null },
34                         { "inject", 0, 0, OptionArg.STRING_ARRAY, ref opt_inject_js, "Inject Javascript file(s)", null },
35                         { "pdf", 0, 0, OptionArg.STRING, ref opt_target_pdf, "File to write (PDF)", null },
36                         { "cookies", 0, 0, OptionArg.STRING, ref opt_cookies, "Inject Cookie string", null },
37                         { "disable-javascript", 0, 0, OptionArg.NONE, ref opt_disable_javascript, "Disable Javascript..", null },
38                         { null }
39         }; 
40     public static int main(string[] args) 
41     {
42         Gtk.init(ref args);
43                 
44                 
45 #if GTK3                
46         // this is a horrific hack from https://bugs.webkit.org/show_bug.cgi?id=128674
47                 Environment.unset_variable("GNOME_DESKTOP_SESSION_ID");
48                 Environment.unset_variable("DESKTOP_SESSION");          
49                 // it should now fallb ack to http prox!?
50                 
51                 
52 #else
53                 unowned string proxy = Environment.get_variable ("http_proxy");
54                 if (proxy != null && proxy.length > 0) {
55                         var sess = WebKit.get_default_session();
56                         sess.proxy_uri = new Soup.URI(proxy);
57                 }
58 #endif          
59         var opt_context = new OptionContext ("webkitpdf");
60         
61                 
62                 try {
63                         opt_context.set_help_enabled (true);
64                         opt_context.add_main_entries (options, null);
65                         opt_context.parse (ref args);
66                         
67             if (webkitpdf.opt_url == null) {
68                 throw new OptionError.BAD_VALUE("missing url");
69             }
70                         if (webkitpdf.opt_target_png == null && webkitpdf.opt_target_pdf == null) {
71                 throw new OptionError.BAD_VALUE("missing pdf or png filename");
72             }
73             
74         
75             stdout.printf ( """Width: %d
76             Height: %d
77             URL: %s
78             Delay: %d
79             png: %s
80             inject: %s
81             PDF: %s
82             Cookie: %s
83             """,
84               opt_width,
85               opt_height,
86               opt_url,
87               (int) opt_delay,
88               opt_target_png == null ? "--empty--" : opt_target_png,
89               opt_inject_js.length < 1? "--empty--" : string.joinv(", ", opt_inject_js),
90               opt_target_pdf == null ? "--empty--" : opt_target_pdf,
91               opt_cookies == null ? "--empty--" : opt_cookies              
92               
93             
94             );
95             
96
97                 } catch (OptionError e) {
98                         stdout.printf ("error: %s\n", e.message);
99                         stdout.printf ("Run '%s --help' to see a full list of available command line options.\n %s", 
100                                          args[0], opt_context.get_help(true,null));
101                         return 0;
102                 }
103   
104         var browser = new BrowserWindow();
105         browser.el.show_all();
106  /*
107                 var ses = WebKit.get_default_session();
108                 ses.request_queued.connect((msg) => {
109                         print("queued request %s\n", msg.uri.to_string(false));
110                         msg.got_headers.connect(() => {
111                                 print("got conent type %s\n", msg.response_headers.get_content_type (null));
112                                 if (msg.response_headers.get_content_type (null) == "image/jpeg") {
113                                         ses.cancel_message(msg,Soup.KnownStatusCode.CANCELLED);
114                                 }
115                         });                     
116                 });
117  */
118  
119         Gtk.main();
120  
121         return 0;
122     }
123 }
124
125
126  
127