From: Armaan Bhojwani Date: Sun, 6 Jun 2021 14:05:54 +0000 (-0400) Subject: Put configuration options into a struct X-Git-Tag: v1.0.0~48 X-Git-Url: https://git.armaanb.net/?p=chorizo.git;a=commitdiff_plain;h=25379bffdc9f0f48f92c379012105e7d98636cc2 Put configuration options into a struct --- diff --git a/browser.c b/browser.c index 5731056..9486b9d 100644 --- a/browser.c +++ b/browser.c @@ -1,18 +1,17 @@ +#include #include #include #include -#include -#include -#include #include +#include +#include -#include -#include +#include #include #include +#include +#include #include -#include - void client_destroy(GtkWidget *, gpointer); WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean, @@ -59,8 +58,8 @@ GKeyFile *config; struct Client { gchar *external_handler_uri; - gchar *hover_uri; gchar *feed_html; + gchar *hover_uri; GtkWidget *location; GtkWidget *tabicon; GtkWidget *tablabel; @@ -82,22 +81,27 @@ struct DownloadManager GtkWidget *win; } dm; +struct Configuration +{ + WebKitCookieAcceptPolicy cookie_policy; + const gchar *accepted_language[2]; + gboolean cooperative_alone; + gboolean cooperative_instances; + gboolean enable_console_to_stdout; + gboolean javascript_disabled; + gchar *download_dir; + gchar *fifo_suffix; + gchar *history_file; + gchar *home_uri; + gchar *search_engine; + gchar *user_agent; + gdouble global_zoom; + gint tab_width_chars; +} cfg; -const gchar *accepted_language[2] = { NULL, NULL }; gint clients = 0, downloads = 0; -gboolean cooperative_alone = TRUE; -gboolean cooperative_instances = TRUE; int cooperative_pipe_fp = 0; -gboolean enable_console_to_stdout; -gchar *fifo_suffix = "main"; -gdouble global_zoom; -gchar *download_dir; -gchar *history_file; -gchar *home_uri; gchar *search_text; -gint tab_width_chars; -gchar *user_agent; -gboolean javascript_disabled; void client_destroy(GtkWidget *widget, gpointer data) @@ -128,7 +132,7 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show, gchar *f; GtkWidget *evbox, *tabbox; - if (uri != NULL && cooperative_instances && !cooperative_alone) { + if (uri != NULL && cfg.cooperative_instances && !cfg.cooperative_alone) { f = ensure_uri_scheme(uri); write(cooperative_pipe_fp, f, strlen(f)); write(cooperative_pipe_fp, "\n", 1); @@ -149,9 +153,9 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show, else c->web_view = webkit_web_view_new_with_related_view(related_wv); - webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom); + webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), cfg.global_zoom); WebKitSettings *settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)); - webkit_settings_set_enable_javascript(settings, !javascript_disabled); + webkit_settings_set_enable_javascript(settings, !cfg.javascript_disabled); g_signal_connect(G_OBJECT(c->web_view), "notify::favicon", G_CALLBACK(changed_favicon), c); g_signal_connect(G_OBJECT(c->web_view), "notify::title", @@ -177,11 +181,11 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show, g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed", G_CALLBACK(crashed_web_view), c); - if (user_agent != NULL) + if (cfg.user_agent != NULL) g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))), - "user-agent", user_agent, NULL); + "user-agent", cfg.user_agent, NULL); - if (enable_console_to_stdout) + if (cfg.enable_console_to_stdout) webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE); webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE); @@ -209,7 +213,7 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show, c->tablabel = gtk_label_new(__NAME__); gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END); - gtk_label_set_width_chars(GTK_LABEL(c->tablabel), tab_width_chars); + gtk_label_set_width_chars(GTK_LABEL(c->tablabel), cfg.tab_width_chars); gtk_widget_set_has_tooltip(c->tablabel, TRUE); /* XXX I don't own a HiDPI screen, so I don't know if scale_factor @@ -270,7 +274,7 @@ cooperation_setup(void) GIOChannel *towatch; gchar *fifofilename, *fifopath; - fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix); + fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", cfg.fifo_suffix); fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL); g_free(fifofilename); @@ -288,7 +292,7 @@ cooperation_setup(void) towatch = g_io_channel_new_file(fifopath, "r+", NULL); g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL); } else { - cooperative_alone = FALSE; + cfg.cooperative_alone = FALSE; } } @@ -440,8 +444,8 @@ changed_uri(GObject *obj, GParamSpec *pspec, gpointer data) if (t != NULL && strlen(t) > 0) { gtk_entry_set_text(GTK_ENTRY(c->location), t); - if (history_file != NULL) { - fp = fopen(history_file, "a"); + if (cfg.history_file != NULL) { + fp = fopen(cfg.history_file, "a"); if (fp != NULL) { fprintf(fp, "%s\n", t); fclose(fp); @@ -514,7 +518,7 @@ download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer da if (sug_clean[i] == G_DIR_SEPARATOR) sug_clean[i] = '_'; - path = g_build_filename(download_dir, sug_clean, NULL); + path = g_build_filename(cfg.download_dir, sug_clean, NULL); path2 = g_strdup(path); while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000) { g_free(path2); @@ -631,42 +635,48 @@ ensure_uri_scheme(const gchar *t) void get_config(void) { - const gchar *e; + cfg.accepted_language[0] = NULL; + cfg.accepted_language[1] = NULL; + cfg.cooperative_alone = TRUE; + cfg.cooperative_alone = TRUE; + cfg.fifo_suffix = "main"; - e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX"); + const char *e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX"); if (e != NULL) - fifo_suffix = g_strdup(e); + cfg.fifo_suffix = g_strdup(e); char *xdg_down = getenv("XDG_DOWNLOAD_DIR"); - download_dir = (xdg_down) ? xdg_down : "/var/tmp"; + cfg.download_dir = (xdg_down) ? xdg_down : "/var/tmp"; config = get_ini(); - accepted_language[0] = g_key_file_get_string(config, "browser", + cfg.accepted_language[0] = g_key_file_get_string(config, "browser", "accepted_language", NULL); - history_file = g_key_file_get_string(config, "browser", "history_file", NULL); - home_uri = g_key_file_get_string(config, "browser", "homepage", NULL); - enable_console_to_stdout = g_key_file_get_boolean(config, "browser", + cfg.history_file = g_key_file_get_string(config, "browser", "history_file", + NULL); + cfg.home_uri = g_key_file_get_string(config, "browser", "homepage", NULL); + cfg.enable_console_to_stdout = g_key_file_get_boolean(config, "browser", "console_to_stdout", NULL); - user_agent = g_key_file_get_string(config, "browser", "user_agent", NULL); - javascript_disabled = g_key_file_get_boolean(config, "browser", + cfg.user_agent = g_key_file_get_string(config, "browser", "user_agent", NULL); + cfg.javascript_disabled = g_key_file_get_boolean(config, "browser", "javascript_disabled", NULL); char *input_cookie_policy = g_key_file_get_string(config, "browser", "cookie_policy", NULL); - cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY; + cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY; if (input_cookie_policy) { if (strcmp(input_cookie_policy, "all") == 0) { - cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS; + cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS; } else if (strcmp(input_cookie_policy, "none") == 0) { - cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NEVER; + cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NEVER; } } - tab_width_chars = g_key_file_get_integer(config, "ui", "tab_width", NULL); - tab_width_chars = (tab_width_chars) ? tab_width_chars : 20; - global_zoom = g_key_file_get_double(config, "ui", "zoom_level", NULL); - global_zoom = (global_zoom) ? global_zoom : 1.0; - search_engine = g_key_file_get_string(config, "ui", "search_engine", NULL); - search_engine = (search_engine) ? search_engine : "https://duckduckgo.com?q="; + cfg.tab_width_chars = g_key_file_get_integer(config, "ui", "tab_width", NULL); + cfg.tab_width_chars = (cfg.tab_width_chars) ? cfg.tab_width_chars : 20; + cfg.global_zoom = g_key_file_get_double(config, "ui", "zoom_level", NULL); + cfg.global_zoom = (cfg.global_zoom) ? cfg.global_zoom : 1.0; + cfg.search_engine = g_key_file_get_string(config, "ui", "search_engine", NULL); + cfg.search_engine = (cfg.search_engine) ? cfg.search_engine : + "https://duckduckgo.com?q="; } void @@ -807,8 +817,8 @@ init_default_web_context(void) webkit_web_context_set_web_extensions_directory(wc, p); g_free(p); - if (accepted_language[0] != NULL) - webkit_web_context_set_preferred_languages(wc, accepted_language); + if (cfg.accepted_language[0] != NULL) + webkit_web_context_set_preferred_languages(wc, cfg.accepted_language); g_signal_connect(G_OBJECT(wc), "download-started", G_CALLBACK(download_handle_start), NULL); @@ -816,7 +826,7 @@ init_default_web_context(void) trust_user_certs(wc); cm = webkit_web_context_get_cookie_manager(wc); - webkit_cookie_manager_set_accept_policy(cm, cookie_policy); + webkit_cookie_manager_set_accept_policy(cm, cfg.cookie_policy); webkit_web_context_set_favicon_database_directory(wc, NULL); webkit_cookie_manager_set_persistent_storage(cm, @@ -894,7 +904,7 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data) client_destroy(NULL, c); return TRUE; } else if (def_key("new_tab", GDK_KEY_w) == key) { - f = ensure_uri_scheme(home_uri); + f = ensure_uri_scheme(cfg.home_uri); client_new(f, NULL, TRUE, TRUE); g_free(f); return TRUE; @@ -976,7 +986,7 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data) return TRUE; } else if (def_key("zoom_reset", GDK_KEY_0) == key) { webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), - global_zoom); + cfg.global_zoom); return TRUE; } } @@ -1022,7 +1032,7 @@ key_location(GtkWidget *widget, GdkEvent *event, gpointer data) search_text = g_strdup(t + 2); search(c, 0); } else if (t != NULL && t[0] == 'w' && t[1] == '/') { - const char *engine = search_engine; + const char *engine = cfg.search_engine; int len = strlen(engine) + strlen(t) - 2; char *f = (char *) malloc(len); snprintf(f, len + 1, "%s%s", engine, t + 2); @@ -1109,7 +1119,7 @@ key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data) gdk_event_get_scroll_deltas(event, &dx, &dy); z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view)); z += -dy * 0.1; - z = dx != 0 ? global_zoom : z; + z = dx != 0 ? cfg.global_zoom : z; webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z); return TRUE; } @@ -1292,7 +1302,7 @@ main(int argc, char **argv) while ((opt = getopt(argc, argv, "C")) != -1) { switch (opt) { case 'C': - cooperative_instances = FALSE; + cfg.cooperative_instances = FALSE; break; default: fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n"); @@ -1300,23 +1310,23 @@ main(int argc, char **argv) } } - if (cooperative_instances) + if (cfg.cooperative_instances) cooperation_setup(); - if (!cooperative_instances || cooperative_alone) + if (!cfg.cooperative_instances || cfg.cooperative_alone) init_default_web_context(); downloadmanager_setup(); mainwindow_setup(); if (optind >= argc) { - client_new(home_uri, NULL, TRUE, TRUE); + client_new(cfg.home_uri, NULL, TRUE, TRUE); } else { for (i = optind; i < argc; i++) client_new(argv[i], NULL, TRUE, TRUE); } - if (!cooperative_instances || cooperative_alone) + if (!cfg.cooperative_instances || cfg.cooperative_alone) gtk_main(); exit(EXIT_SUCCESS);