]> git.armaanb.net Git - chorizo.git/commitdiff
User configuration using environment variables
authorPeter Hofmann <scm@uninformativ.de>
Sun, 15 Jun 2014 13:36:21 +0000 (15:36 +0200)
committerPeter Hofmann <scm@uninformativ.de>
Sun, 15 Jun 2014 13:41:21 +0000 (15:41 +0200)
I think of these options (language, download dir, zoom) as user
settings. The user does not change them all the time. He sets them once
and for all in his shell rc.

"-e" and friends, however, are considered runtime settings. "THIS TIME,
I want my browser to debug all requests!"

browser.c

index 582806125a5b8d50b9b30bf848709bb9f4687f6e..81ddf995fd511843efb067b0666bcecbf22c4b5a 100644 (file)
--- a/browser.c
+++ b/browser.c
 #include <webkit/webkit.h>
 
 
-#define DOWNLOAD_DIR "/tmp/tmp"
-#define LANGUAGE "en-US"
-
-
 static void adblock(WebKitWebView *, WebKitWebFrame *, WebKitWebResource *,
                     WebKitNetworkRequest *, WebKitNetworkResponse *, gpointer);
 static void adblock_load(void);
@@ -35,6 +31,7 @@ static gboolean download_request(WebKitWebView *, WebKitWebFrame *,
                                  WebKitWebPolicyDecision *, gpointer);
 static gboolean download_wget(WebKitWebView *, WebKitDownload *, gpointer);
 static gchar *ensure_url_scheme(const gchar *);
+static void grab_environment_configuration(void);
 static void hover_web_view(WebKitWebView *, gchar *, gchar *, gpointer);
 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
@@ -56,11 +53,13 @@ struct Client
 };
 
 
+static gchar *accepted_language = "en-US";
 static GSList *adblock_patterns = NULL;
 static gint clients = 0;
 static gboolean cooperative_alone = TRUE;
 static gboolean cooperative_instances = TRUE;
 static int cooperative_pipe_fp = 0;
+static gchar *download_dir = "/tmp";
 static Window embed = 0;
 static gchar *first_uri = NULL;
 static gdouble global_zoom = 1.0;
@@ -237,8 +236,8 @@ client_new(const gchar *uri)
 
        if (!language_is_set)
        {
-               g_object_set(webkit_get_default_session(), "accept-language", LANGUAGE,
-                            NULL);
+               g_object_set(webkit_get_default_session(), "accept-language",
+                            accepted_language, NULL);
                language_is_set = TRUE;
        }
 
@@ -397,7 +396,7 @@ download_wget(WebKitWebView *web_view, WebKitDownload *download, gpointer data)
        uri = webkit_download_get_uri(download);
        if (fork() == 0)
        {
-               chdir(DOWNLOAD_DIR);
+               chdir(download_dir);
                if (embed == 0)
                        ret = execlp("xterm", "xterm", "-hold", "-e", "wget", uri, NULL);
                else
@@ -441,6 +440,41 @@ ensure_url_scheme(const gchar *t)
                return g_strdup(t);
 }
 
+void
+grab_environment_configuration(void)
+{
+       gchar *u, *v;
+       const gchar *e;
+
+       u = g_ascii_strup(__NAME__, -1);
+
+       v = g_strdup_printf("%s_ACCEPTED_LANGUAGE", u);
+       e = g_getenv(v);
+       if (e != NULL)
+       {
+               accepted_language = g_strdup(e);
+       }
+       g_free(v);
+
+       v = g_strdup_printf("%s_DOWNLOAD_DIR", u);
+       e = g_getenv(v);
+       if (e != NULL)
+       {
+               download_dir = g_strdup(e);
+       }
+       g_free(v);
+
+       v = g_strdup_printf("%s_ZOOM", u);
+       e = g_getenv(v);
+       if (e != NULL)
+       {
+               global_zoom = atof(e);
+       }
+       g_free(v);
+
+       g_free(u);
+}
+
 void
 hover_web_view(WebKitWebView *web_view, gchar *title, gchar *uri,
                    gpointer data)
@@ -684,13 +718,12 @@ main(int argc, char **argv)
 
        gtk_init(&argc, &argv);
 
-       while ((opt = getopt(argc, argv, "z:e:rCT")) != -1)
+       grab_environment_configuration();
+
+       while ((opt = getopt(argc, argv, "e:rCT")) != -1)
        {
                switch (opt)
                {
-                       case 'z':
-                               global_zoom = atof(optarg);
-                               break;
                        case 'e':
                                embed = atol(optarg);
                                tabbed_automagic = FALSE;