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