webkitpdf.vala
[app.webkitpdf] / webkitpdf.vala
1
2 // valac   --vapidir=. --thread  webkitpdf.vala  BrowserWindow.vala  --vapi=./vapi
3 //    --pkg glib-2.0 --pkg webkit-1.0 --pkg gtk+-2.0 -o /usr/bin/webkitpdf --target-glib=2.32 
4  
5 public class webkitpdf {
6  
7     public static string? opt_url = null;
8         public static uint opt_delay = 0;
9         public static string? opt_target_png = null;
10         public static string? opt_target_pdf = null;    
11         public static string[]? opt_inject_js = null;           
12         const OptionEntry[] options = {
13                 
14                         
15                         { "url", 0, 0, OptionArg.STRING, ref opt_url, "Url to grab", null },
16                         { "delay", 0, 0, OptionArg.INT, ref opt_delay, "Delay before snapshot in seconds", null },
17                         { "png", 0, 0, OptionArg.STRING, ref opt_target_png, "File to write (PNG)", null },
18                         { "inject", 0, 0, OptionArg.STRING_ARRAY, ref opt_inject_js, "Inject Javascript file(s)", null },
19                         { "pdf", 0, 0, OptionArg.STRING, ref opt_target_pdf, "File to write (PDF)", null },
20                         { null }
21         };
22     public static int main(string[] args) 
23     {
24         Gtk.init(ref args);
25                  
26                 unowned string proxy = Environment.get_variable ("http_proxy");
27                 if (proxy != null && proxy.length > 0) {
28                         var sess = WebKit.get_default_session();
29                         sess.proxy_uri = new Soup.URI(proxy);
30                 }
31                 
32         var opt_context = new OptionContext ("webkitpdf");
33                 
34                 
35                 try {
36                         opt_context.set_help_enabled (true);
37                         opt_context.add_main_entries (options, null);
38                         opt_context.parse (ref args);
39                         
40             if (webkitpdf.opt_url == null) {
41                 throw new OptionError.BAD_VALUE("missing url");
42             }
43                         if (webkitpdf.opt_target_png == null && webkitpdf.opt_target_pdf == null) {
44                 throw new OptionError.BAD_VALUE("missing pdf or png filename");
45             }
46             
47
48                 } catch (OptionError e) {
49                         stdout.printf ("error: %s\n", e.message);
50                         stdout.printf ("Run '%s --help' to see a full list of available command line options.\n %s", 
51                                          args[0], opt_context.get_help(true,null));
52                         return 0;
53                 }
54   
55         var browser = new BrowserWindow();
56         browser.el.show_all();
57  /*
58                 var ses = WebKit.get_default_session();
59                 ses.request_queued.connect((msg) => {
60                         print("queued request %s\n", msg.uri.to_string(false));
61                         msg.got_headers.connect(() => {
62                                 print("got conent type %s\n", msg.response_headers.get_content_type (null));
63                                 if (msg.response_headers.get_content_type (null) == "image/jpeg") {
64                                         ses.cancel_message(msg,Soup.KnownStatusCode.CANCELLED);
65                                 }
66                         });                     
67                 });
68  */
69  
70         Gtk.main();
71  
72         return 0;
73     }
74 }
75
76
77