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