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