X-Git-Url: https://git.armaanb.net/?a=blobdiff_plain;f=src%2Fbrowser.c;h=3edab07a13fda9d52749ce92c4909ded8b48ea26;hb=HEAD;hp=995ee5ad8ce3e755471a521431a3694b54b16685;hpb=5a995e7e6b959ecfb1f25bf792e0c93fcc1bbb10;p=chorizo.git diff --git a/src/browser.c b/src/browser.c index 995ee5a..3edab07 100644 --- a/src/browser.c +++ b/src/browser.c @@ -53,23 +53,18 @@ struct Client { }; struct Configuration { + gdouble zoom_level; WebKitCookieAcceptPolicy cookie_policy; - const gchar *accepted_language[2]; gboolean cooperative_alone; gboolean cooperative_instances; - gboolean enable_console_to_stdout; - gboolean images_disabled; - gboolean javascript_disabled; + gboolean images_enabled; + gboolean javascript_enabled; gboolean private; - gboolean spellcheck_disabled; - gchar *download_dir; - gchar *fifo_suffix; - gchar *history_file; + gboolean spellcheck_enabled; + gchar *default_uri; gchar *home_uri; gchar *search_engine; gchar *spellcheck_language; - gchar *user_agent; - gdouble global_zoom; gint scroll_lines; gint tab_width_chars; } cfg; @@ -80,6 +75,8 @@ struct MainWindow { } mw; gint clients = 0; +struct Client **client_arr; + int cooperative_pipe_fp = 0; gchar *search_text; @@ -119,6 +116,12 @@ client_destroy(GtkWidget *widget, gpointer data) { quit_if_nothing_active(); } +void +set_uri(const char *uri, struct Client *c) { + if (!gtk_widget_is_focus(c->location) && uri != NULL) + gtk_entry_set_text(GTK_ENTRY(c->location), uri); +} + WebKitWebView * client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show, gboolean focus_tab) { @@ -142,18 +145,124 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show, c->focus_new_tab = focus_tab; - if (related_wv == NULL) - c->web_view = webkit_web_view_new(); - else + if (related_wv == NULL) { + WebKitUserContentManager *ucm = webkit_user_content_manager_new(); + WebKitUserScript *wkscript; + WebKitUserStyleSheet *wkstyle; + gchar *path = NULL, *source, *base; + const gchar *entry = NULL; + GDir *dir = NULL; + + base = g_build_filename(g_get_user_data_dir(), __NAME__, "user-scripts", + NULL); + dir = g_dir_open(base, 0, NULL); + if (dir != NULL) { + while ((entry = g_dir_read_name(dir)) != NULL) { + path = g_build_filename(base, entry, NULL); + if (g_str_has_suffix(path, ".js")) { + g_file_get_contents(path, &source, NULL, NULL); + wkscript = webkit_user_script_new( + source, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, + WEBKIT_USER_SCRIPT_INJECT_AT_DOCUMENT_START, NULL, + NULL); + webkit_user_content_manager_add_script(ucm, wkscript); + webkit_user_script_unref(wkscript); + } + g_free(path); + g_free(source); + } + g_dir_close(dir); + } + + base = g_build_filename(g_get_user_data_dir(), __NAME__, "user-styles", + NULL); + dir = g_dir_open(base, 0, NULL); + if (dir != NULL) { + while ((entry = g_dir_read_name(dir)) != NULL) { + path = g_build_filename(base, entry, NULL); + if (g_str_has_suffix(path, ".css")) { + g_file_get_contents(path, &source, NULL, NULL); + wkstyle = webkit_user_style_sheet_new( + source, WEBKIT_USER_CONTENT_INJECT_ALL_FRAMES, + WEBKIT_USER_STYLE_LEVEL_USER, NULL, NULL); + webkit_user_content_manager_add_style_sheet(ucm, wkstyle); + webkit_user_style_sheet_unref(wkstyle); + } + g_free(path); + g_free(source); + } + g_dir_close(dir); + } + + g_free(base); + + c->web_view = webkit_web_view_new_with_user_content_manager(ucm); + } else { c->web_view = webkit_web_view_new_with_related_view(related_wv); + } + // Get settings c->settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)); + webkit_settings_set_enable_javascript(c->settings, cfg.javascript_enabled); + webkit_settings_set_auto_load_images(c->settings, cfg.images_enabled); + + GKeyFile *config = get_ini(); + char *val; + val = g_key_file_get_string(config, "ui", "font_family_default", NULL); + if (val != NULL) + webkit_settings_set_default_font_family(c->settings, val); + + val = g_key_file_get_string(config, "ui", "font_family_default_monospace", + NULL); + if (val != NULL) + webkit_settings_set_monospace_font_family(c->settings, val); + + val = g_key_file_get_string(config, "ui", "font_family_default_sans_serif", + NULL); + if (val != NULL) + webkit_settings_set_sans_serif_font_family(c->settings, val); + + val = + g_key_file_get_string(config, "ui", "font_family_default_serif", NULL); + if (val != NULL) + webkit_settings_set_serif_font_family(c->settings, val); + + val = g_key_file_get_string(config, "browser", "user_agent", NULL); + if (val != NULL) + g_object_set(c->settings, "user-agent", val, NULL); + + int val2; + val2 = g_key_file_get_integer(config, "ui", "font_size_default", NULL); + if (val2) + webkit_settings_set_default_font_size(c->settings, val2); + + val2 = g_key_file_get_integer(config, "ui", "font_size_default_monospace", + NULL); + if (val2) + webkit_settings_set_default_monospace_font_size(c->settings, val2); + + gboolean val3; + val3 = g_key_file_get_boolean(config, "browser", "console_to_stdout", NULL); + if (val3) + webkit_settings_set_enable_write_console_messages_to_stdout(c->settings, + val3); + + val3 = g_key_file_get_boolean(config, "browser", "webgl_enable", NULL); + if (!val3) + webkit_settings_set_enable_webgl(c->settings, val3); + + val3 = g_key_file_get_boolean(config, "browser", + "javascript_can_access_clipboard", NULL); + if (val3) + webkit_settings_set_javascript_can_access_clipboard(c->settings, val3); + + double val4; + val4 = g_key_file_get_double(config, "ui", "zoom_level", NULL); + if (val4) + webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), val4); + + webkit_settings_set_enable_developer_extras(c->settings, TRUE); - webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), - cfg.global_zoom); - webkit_settings_set_enable_javascript(c->settings, - !cfg.javascript_disabled); - webkit_settings_set_auto_load_images(c->settings, !cfg.images_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", @@ -179,33 +288,23 @@ 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 (cfg.user_agent != NULL) { - g_object_set(c->settings, "user-agent", cfg.user_agent, NULL); - } - - if (cfg.enable_console_to_stdout) - webkit_settings_set_enable_write_console_messages_to_stdout(c->settings, - TRUE); - - webkit_settings_set_enable_developer_extras(c->settings, TRUE); - GtkWidget *locbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0); c->jsbutton = gtk_toggle_button_new_with_label("JS"); + gtk_widget_set_tooltip_text(c->jsbutton, "Toggle JavaScript execution"); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c->jsbutton), - !cfg.javascript_disabled); + cfg.javascript_enabled); g_signal_connect(G_OBJECT(c->jsbutton), "toggled", G_CALLBACK(togglejs), c); c->imgbutton = gtk_toggle_button_new_with_label("IMG"); + gtk_widget_set_tooltip_text(c->imgbutton, "Toggle image loading"); gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(c->imgbutton), - !cfg.images_disabled); + cfg.images_enabled); g_signal_connect(G_OBJECT(c->imgbutton), "toggled", G_CALLBACK(toggleimg), c); c->location = gtk_entry_new(); - gtk_box_pack_start(GTK_BOX(locbox), c->location, TRUE, TRUE, 5); - gtk_box_pack_end(GTK_BOX(locbox), c->jsbutton, FALSE, FALSE, 0); - gtk_box_pack_end(GTK_BOX(locbox), c->imgbutton, FALSE, FALSE, 0); + gtk_box_pack_start(GTK_BOX(locbox), c->location, TRUE, TRUE, 0); if (cfg.private) { GtkWidget *privindicator = gtk_label_new("Private mode"); @@ -215,6 +314,9 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show, gtk_box_pack_end(GTK_BOX(locbox), privindicator, FALSE, FALSE, 5); } + gtk_box_pack_start(GTK_BOX(locbox), c->jsbutton, FALSE, FALSE, 5); + gtk_box_pack_start(GTK_BOX(locbox), c->imgbutton, FALSE, FALSE, 0); + g_signal_connect(G_OBJECT(c->location), "key-press-event", G_CALLBACK(key_location), c); g_signal_connect(G_OBJECT(c->location), "icon-release", @@ -280,7 +382,11 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show, g_free(f); } + set_uri(uri, c); + clients++; + client_arr = realloc(client_arr, (clients + 1) * sizeof(client_arr[0])); + client_arr[clients] = c; return WEBKIT_WEB_VIEW(c->web_view); } @@ -297,8 +403,8 @@ cooperation_setup(void) { gchar *fifofilename, *fifopath; gchar *priv = (cfg.private) ? "-private" : ""; - fifofilename = - g_strdup_printf("%s%s%s-%s", __NAME__, priv, ".fifo", cfg.fifo_suffix); + fifofilename = g_strdup_printf("%s%s%s-%s", __NAME__, priv, ".fifo", + g_getenv(__NAME_UPPERCASE__ "_FIFO_SUFFIX")); fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL); g_free(fifofilename); @@ -310,8 +416,8 @@ cooperation_setup(void) { fprintf(stderr, __NAME__ ": Can't open FIFO at all.\n"); } else { if (write(cooperative_pipe_fp, "", 0) == -1) { - /* Could not do an empty write to the FIFO which means there's no - * one listening. */ + /* Could not do an empty write to the FIFO which means there's + * no one listening. */ close(cooperative_pipe_fp); towatch = g_io_channel_new_file(fifopath, "r+", NULL); g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL); @@ -361,8 +467,6 @@ changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data) { * references. */ webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view), grab_feeds, NULL, grab_feeds_finished, c); - - run_user_scripts(WEBKIT_WEB_VIEW(c->web_view)); } gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p); } @@ -413,9 +517,9 @@ changed_title(GObject *obj, GParamSpec *pspec, gpointer data) { gboolean mute = webkit_web_view_get_is_muted(WEBKIT_WEB_VIEW(c->web_view)); gchar *muted = (mute) ? "[m] " : ""; sprintf(name, "%s%s", muted, t); - gtk_label_set_text(GTK_LABEL(c->tablabel), name); g_free(name); + gtk_widget_set_tooltip_text(c->tablabel, t); mainwindow_title(gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook))); } @@ -433,10 +537,13 @@ changed_uri(GObject *obj, GParamSpec *pspec, gpointer data) { * now. Not updating the location bar in this scenario is important, * because we would override the "WEB PROCESS CRASHED" message. */ if (t != NULL && strlen(t) > 0) { - gtk_entry_set_text(GTK_ENTRY(c->location), t); + set_uri(t, c); + + gchar *history_file = + g_key_file_get_string(config, "browser", "history_file", NULL); - if (cfg.history_file != NULL && !cfg.private) { - fp = fopen(cfg.history_file, "a"); + if (history_file != NULL && !cfg.private) { + fp = fopen(history_file, "a"); if (fp != NULL) { fprintf(fp, "%s\n", t); fclose(fp); @@ -503,34 +610,23 @@ ensure_uri_scheme(const gchar *t) { void get_config(void) { - cfg.accepted_language[0] = NULL; - cfg.accepted_language[1] = NULL; cfg.cooperative_alone = TRUE; - cfg.cooperative_alone = TRUE; - cfg.fifo_suffix = "main"; - - const char *e = g_getenv(__NAME_UPPERCASE__ "_FIFO_SUFFIX"); - if (e != NULL) - cfg.fifo_suffix = g_strdup(e); - - char *xdg_down = getenv("XDG_DOWNLOAD_DIR"); - cfg.download_dir = (xdg_down) ? xdg_down : "/var/tmp"; + cfg.cooperative_instances = TRUE; config = get_ini(); - cfg.accepted_language[0] = - g_key_file_get_string(config, "browser", "accepted_language", NULL); - 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.home_uri = (cfg.home_uri) ? cfg.home_uri : "about:blank"; - cfg.enable_console_to_stdout = - g_key_file_get_boolean(config, "browser", "console_to_stdout", NULL); - 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); - cfg.images_disabled = - g_key_file_get_boolean(config, "browser", "images_disabled", NULL); + + cfg.javascript_enabled = + g_key_file_get_boolean(config, "browser", "javascript_enabled", NULL); + cfg.javascript_enabled = + (cfg.javascript_enabled) ? cfg.javascript_enabled : TRUE; + + cfg.images_enabled = + g_key_file_get_boolean(config, "browser", "images_enabled", NULL); + cfg.images_enabled = (cfg.images_enabled) ? cfg.images_enabled : TRUE; + char *input_cookie_policy = g_key_file_get_string(config, "browser", "cookie_policy", NULL); cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY; @@ -542,21 +638,28 @@ get_config(void) { } } + cfg.default_uri = g_key_file_get_string(config, "ui", "default_uri", NULL); + cfg.default_uri = (cfg.default_uri) ? cfg.default_uri : "https://"; + 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="; - cfg.spellcheck_disabled = - g_key_file_get_boolean(config, "ui", "spellcheck_disabled", NULL); + + cfg.spellcheck_enabled = + g_key_file_get_boolean(config, "ui", "spellcheck_enabled", NULL); + cfg.spellcheck_enabled = + (cfg.spellcheck_enabled) ? cfg.spellcheck_enabled : FALSE; + cfg.spellcheck_language = g_key_file_get_string(config, "ui", "spellcheck_language", NULL); cfg.spellcheck_language = (cfg.spellcheck_language) ? cfg.spellcheck_language : "en_US"; + cfg.scroll_lines = g_key_file_get_integer(config, "ui", "scroll_lines", NULL); cfg.scroll_lines = (cfg.scroll_lines) ? cfg.scroll_lines : 3; @@ -577,7 +680,8 @@ grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data) { /* This was taken almost verbatim from the example in WebKit's * documentation: * - * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html */ + * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html + */ js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object), result, &err); @@ -603,7 +707,7 @@ grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data) { GTK_ENTRY_ICON_SECONDARY, "application-rss+xml-symbolic"); gtk_entry_set_icon_activatable(GTK_ENTRY(c->location), - GTK_ENTRY_ICON_PRIMARY, TRUE); + GTK_ENTRY_ICON_SECONDARY, TRUE); } else { gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location), GTK_ENTRY_ICON_SECONDARY, NULL); @@ -629,7 +733,7 @@ hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, } if (!gtk_widget_is_focus(c->location)) - gtk_entry_set_text(GTK_ENTRY(c->location), to_show); + set_uri(to_show, c); } void @@ -690,16 +794,15 @@ init_default_web_context(void) { WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES; webkit_web_context_set_process_model(wc, model); - p = g_build_filename(g_get_user_data_dir(), __NAME__, "web_extensions", + p = g_build_filename(g_get_user_data_dir(), __NAME__, "web-extensions", NULL); webkit_web_context_set_web_extensions_directory(wc, p); g_free(p); - if (cfg.accepted_language[0] != NULL) - webkit_web_context_set_preferred_languages(wc, cfg.accepted_language); - + char *xdg_down = getenv("XDG_DOWNLOAD_DIR"); g_signal_connect(G_OBJECT(wc), "download-started", - G_CALLBACK(download_start), &cfg); + G_CALLBACK(download_start), + (xdg_down) ? xdg_down : "/var/tmp"); trust_user_certs(wc); @@ -714,12 +817,13 @@ init_default_web_context(void) { WebKitCookiePersistentStorage type = WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE; webkit_cookie_manager_set_persistent_storage(cm, fname, type); + g_free(fname); } const gchar *const languages[2] = {(const gchar *)cfg.spellcheck_language, NULL}; webkit_web_context_set_spell_checking_languages(wc, languages); - webkit_web_context_set_spell_checking_enabled(wc, !cfg.spellcheck_disabled); + webkit_web_context_set_spell_checking_enabled(wc, cfg.spellcheck_enabled); } void @@ -756,11 +860,12 @@ search_init(struct Client *c, int direction) { const gchar *contents = gtk_entry_get_text(GTK_ENTRY(c->location)); if (strcspn(contents, "s/")) { gtk_entry_set_text(GTK_ENTRY(c->location), "s/"); + gtk_editable_set_position(GTK_EDITABLE(c->location), -1); + } else { + search(c, 0); + search(c, -1); + search(c, direction); } - gtk_editable_set_position(GTK_EDITABLE(c->location), -1); - search(c, 0); - search(c, -1); - search(c, direction); } int @@ -777,6 +882,8 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data) { if (event->type == GDK_KEY_PRESS) { if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK) { + const char *uri = + webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)); int key = ((GdkEventKey *)event)->keyval; if (def_key("download_manager", GDK_KEY_y) == key) { downloadmanager_show(); @@ -789,9 +896,9 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data) { return TRUE; } else if (def_key("location", GDK_KEY_t) == key) { gtk_widget_grab_focus(c->location); - const char *uri = - webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)); - const char *goal = (uri) ? uri : "https://"; + const char *goal = (strcmp(cfg.home_uri, uri) == 0 || !uri) + ? cfg.default_uri + : uri; gtk_entry_set_text(GTK_ENTRY(c->location), goal); gtk_editable_set_position(GTK_EDITABLE(c->location), -1); return TRUE; @@ -805,8 +912,6 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data) { } else if (def_key("quit", GDK_KEY_g) == key) { search(c, 2); gtk_widget_grab_focus(c->web_view); - const gchar *uri = - webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)); gtk_entry_set_text(GTK_ENTRY(c->location), uri); gtk_editable_set_position(GTK_EDITABLE(c->location), -1); webkit_web_view_run_javascript( @@ -927,7 +1032,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), - cfg.global_zoom); + cfg.zoom_level); return TRUE; } } @@ -955,12 +1060,11 @@ 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 = cfg.search_engine; - int len = strlen(engine) + strlen(t) - 2; - char *f = (char *)malloc(len); - snprintf(f, len + 1, "%s%s", engine, t + 2); + int len = strlen(cfg.search_engine) + strlen(t) - 2; + gchar *f = malloc(len + 1); + snprintf(f, len + 1, "%s%s", cfg.search_engine, t + 2); webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f); - free(f); + g_free(f); } else { f = ensure_uri_scheme(t); webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f); @@ -1018,9 +1122,12 @@ key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data) { gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0); } } else if (event->type == GDK_BUTTON_RELEASE) { + GdkModifierType modifiers = gtk_accelerator_get_default_mod_mask(); switch (((GdkEventButton *)event)->button) { - case 2: - if (c->hover_uri != NULL) { + case 1: + if ((((GdkEventButton *)event)->state & modifiers) == + GDK_CONTROL_MASK && + c->hover_uri != NULL) { client_new(c->hover_uri, NULL, TRUE, FALSE); return TRUE; } @@ -1038,7 +1145,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 ? cfg.global_zoom : z; + z = dx != 0 ? cfg.zoom_level : z; webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z); return TRUE; } @@ -1054,9 +1161,10 @@ mainwindow_setup(void) { g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL); gchar *priv = (cfg.private) ? "-private" : ""; - gchar *title = malloc(strlen(priv) + strlen(__NAME__)); + gchar *title = malloc(strlen(priv) + strlen(__NAME__) + 1); sprintf(title, "%s%s", __NAME__, priv); gtk_window_set_title(GTK_WINDOW(mw.win), title); + g_free(title); mw.notebook = gtk_notebook_new(); gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE); @@ -1113,38 +1221,6 @@ remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data) { return TRUE; } -void -run_user_scripts(WebKitWebView *web_view) { - gchar *base = NULL, *path = NULL, *contents = NULL; - const gchar *entry = NULL; - GDir *scriptdir = NULL; - - base = - g_build_filename(g_get_user_data_dir(), __NAME__, "user-scripts", NULL); - scriptdir = g_dir_open(base, 0, NULL); - if (scriptdir != NULL) { - while ((entry = g_dir_read_name(scriptdir)) != NULL) { - path = g_build_filename(base, entry, NULL); - char *jscmd = malloc(strlen(path) + 36); - sprintf(jscmd, "console.log(\"Running userscript %s\");", path); - if (g_str_has_suffix(path, ".js")) { - if (g_file_get_contents(path, &contents, NULL, NULL)) { - webkit_web_view_run_javascript(web_view, jscmd, NULL, NULL, - NULL); - webkit_web_view_run_javascript(web_view, contents, NULL, - NULL, NULL); - g_free(contents); - g_free(jscmd); - } - } - g_free(path); - } - g_dir_close(scriptdir); - } - - g_free(base); -} - void show_web_view(WebKitWebView *web_view, gpointer data) { struct Client *c = (struct Client *)data; @@ -1166,17 +1242,20 @@ show_web_view(WebKitWebView *web_view, gpointer data) { void trust_user_certs(WebKitWebContext *wc) { GTlsCertificate *cert; - const gchar *basedir, *file, *absfile; - GDir *dir; + gchar *basedir, *absfile; + const gchar *file; + GDir *dir = NULL; basedir = g_build_filename(g_get_user_data_dir(), __NAME__, "certs", NULL); dir = g_dir_open(basedir, 0, NULL); + g_free(basedir); if (dir != NULL) { file = g_dir_read_name(dir); while (file != NULL) { absfile = g_build_filename(g_get_user_data_dir(), __NAME__, "certs", file, NULL); cert = g_tls_certificate_new_from_file(absfile, NULL); + g_free(absfile); if (cert == NULL) fprintf(stderr, __NAME__ ": Could not load trusted cert '%s'\n", file); @@ -1195,17 +1274,16 @@ get_ini(void) { config = g_key_file_new(); // Load user config - if (!g_key_file_load_from_file(config, - g_build_filename(g_get_user_config_dir(), - __NAME__, "chorizo.ini", - NULL), - flags, NULL)) { + gchar *fname = g_build_filename(g_get_user_config_dir(), __NAME__, + "chorizo.ini", NULL); + if (!g_key_file_load_from_file(config, fname, flags, NULL)) { // Load global config if (!g_key_file_load_from_file(config, "/etc/chorizo.ini", flags, NULL)) { fprintf(stderr, "Could not load chorizo.ini"); } } + g_free(fname); return config; } @@ -1231,6 +1309,11 @@ main(int argc, char **argv) { } gtk_init(&argc, &argv); + + // Keep clipboard contents after program closes + gtk_clipboard_store(gtk_clipboard_get_for_display(gdk_display_get_default(), + GDK_SELECTION_CLIPBOARD)); + get_config(); if (cfg.cooperative_instances) @@ -1242,6 +1325,7 @@ main(int argc, char **argv) { downloadmanager_setup(); mainwindow_setup(); + client_arr = malloc(sizeof(struct Client *)); if (optind >= argc) { client_new(cfg.home_uri, NULL, TRUE, TRUE); } else { @@ -1252,5 +1336,9 @@ main(int argc, char **argv) { if (!cfg.cooperative_instances || cfg.cooperative_alone) gtk_main(); + for (int i = 0; i < clients; i++) { + free(&(client_arr[i])); + } + exit(EXIT_SUCCESS); }