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