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