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