diff options
author | Peter Hofmann <scm@uninformativ.de> | 2014-06-15 09:49:47 +0200 |
---|---|---|
committer | Peter Hofmann <scm@uninformativ.de> | 2014-06-15 09:54:09 +0200 |
commit | b004657a0e40a79c85385267cfd4d127082ebd98 (patch) | |
tree | cdd7780a6ec3d85c2003aefaf2a779c5ca189d31 /browser.c | |
parent | f3156e3d82d5a3698fcaa314b0a7492b671bb352 (diff) | |
download | lariza-b004657a0e40a79c85385267cfd4d127082ebd98.tar.gz |
Refactoring: Group and unify identifiers
Diffstat (limited to 'browser.c')
-rw-r--r-- | browser.c | 535 |
1 files changed, 268 insertions, 267 deletions
@@ -18,44 +18,30 @@ static void adblock(WebKitWebView *, WebKitWebFrame *, WebKitWebResource *, WebKitNetworkRequest *, WebKitNetworkResponse *, gpointer); -static void destroy_client(GtkWidget *, gpointer); -static gboolean do_download(WebKitWebView *, WebKitDownload *, gpointer); -static gboolean download_request(WebKitWebView *, WebKitWebFrame *, - WebKitNetworkRequest *, gchar *, - WebKitWebPolicyDecision *, gpointer); -static Window launch_tabbed(void); -static void load_adblock(void); -static void load_status_changed(GObject *obj, GParamSpec *pspec, - gpointer data); -static gboolean location_key(GtkWidget *, GdkEvent *, gpointer); -static void new_client(const gchar *uri); -static gboolean new_client_request(WebKitWebView *, WebKitWebFrame *, +static void adblock_load(void); +static void client_destroy(GtkWidget *, gpointer); +static void client_new(const gchar *uri); +static gboolean client_new_request(WebKitWebView *, WebKitWebFrame *, WebKitNetworkRequest *, WebKitWebNavigationAction *, WebKitWebPolicyDecision *, gpointer); +static void cooperation_setup(void); +static void changed_load_status(GObject *obj, GParamSpec *pspec, + gpointer data); +static void changed_title(GObject *, GParamSpec *, gpointer); +static void changed_uri(GObject *, GParamSpec *, gpointer); +static gboolean download_request(WebKitWebView *, WebKitWebFrame *, + WebKitNetworkRequest *, gchar *, + WebKitWebPolicyDecision *, gpointer); +static gboolean download_wget(WebKitWebView *, WebKitDownload *, gpointer); +static void hover_web_view(WebKitWebView *, gchar *, gchar *, gpointer); +static gboolean key_location(GtkWidget *, GdkEvent *, gpointer); +static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer); static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer); static void search(gpointer, gint); -static void setup_cooperation(void); static void scroll(GtkAdjustment *, gint, gdouble); -static void title_changed(GObject *, GParamSpec *, gpointer); -static void uri_changed(GObject *, GParamSpec *, gpointer); +static Window tabbed_launch(void); static void usage(void); -static void web_view_hover(WebKitWebView *, gchar *, gchar *, gpointer); -static gboolean web_view_key(GtkWidget *, GdkEvent *, gpointer); - - -static Window embed = 0; -static gint clients = 0; -static gdouble global_zoom = 1.0; -static gchar *search_text = NULL; -static gchar *first_uri = NULL; -static gboolean show_all_requests = FALSE; -static GSList *adblock_patterns = NULL; -static gboolean cooperative_instances = TRUE; -static int cooperative_pipe_fp = 0; -static gboolean alone = TRUE; -static gboolean automagic_tabbed = TRUE; -static gboolean language_set = FALSE; struct Client @@ -69,6 +55,20 @@ struct Client }; +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 Window embed = 0; +static gchar *first_uri = NULL; +static gdouble global_zoom = 1.0; +static gboolean language_is_set = FALSE; +static gchar *search_text = NULL; +static gboolean show_all_requests = FALSE; +static gboolean tabbed_automagic = TRUE; + + void adblock(WebKitWebView *web_view, WebKitWebFrame *frame, WebKitWebResource *resource, WebKitNetworkRequest *request, @@ -101,7 +101,42 @@ adblock(WebKitWebView *web_view, WebKitWebFrame *frame, } void -destroy_client(GtkWidget *obj, gpointer data) +adblock_load(void) +{ + GRegex *re = NULL; + GError *err = NULL; + GIOChannel *channel = NULL; + gchar *path = NULL; + gchar *buf = NULL; + + path = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock.black", + NULL); + channel = g_io_channel_new_file(path, "r", &err); + if (channel != NULL) + { + while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL) + == G_IO_STATUS_NORMAL) + { + g_strstrip(buf); + re = g_regex_new(buf, + G_REGEX_CASELESS | G_REGEX_OPTIMIZE, + G_REGEX_MATCH_PARTIAL, &err); + if (err != NULL) + { + fprintf(stderr, __NAME__": Could not compile regex: %s\n", buf); + g_error_free(err); + err = NULL; + } + adblock_patterns = g_slist_append(adblock_patterns, re); + + g_free(buf); + } + } + g_free(path); +} + +void +client_destroy(GtkWidget *obj, gpointer data) { struct Client *c = (struct Client *)data; @@ -121,95 +156,10 @@ destroy_client(GtkWidget *obj, gpointer data) gtk_main_quit(); } -gboolean -do_download(WebKitWebView *web_view, WebKitDownload *download, gpointer data) -{ - const gchar *uri; - char id[16] = ""; - gint ret; - - (void)web_view; - (void)data; - - uri = webkit_download_get_uri(download); - if (fork() == 0) - { - chdir(DOWNLOAD_DIR); - if (embed == 0) - ret = execlp("xterm", "xterm", "-hold", "-e", "wget", uri, NULL); - else - { - if (snprintf(id, 16, "%ld", embed) >= 16) - { - fprintf(stderr, __NAME__": id for xterm embed truncated!\n"); - exit(EXIT_FAILURE); - } - ret = execlp("xterm", "xterm", "-hold", "-into", id, "-e", "wget", - uri, NULL); - } - - if (ret == -1) - { - fprintf(stderr, __NAME__": exec'ing xterm for download"); - perror(" failed"); - exit(EXIT_FAILURE); - } - } - - return FALSE; -} - -gboolean -download_request(WebKitWebView *web_view, WebKitWebFrame *frame, - WebKitNetworkRequest *request, gchar *mime_type, - WebKitWebPolicyDecision *policy_decision, gpointer data) -{ - (void)frame; - (void)request; - (void)data; - - if (!webkit_web_view_can_show_mime_type(web_view, mime_type)) - { - webkit_web_policy_decision_download(policy_decision); - return TRUE; - } - return FALSE; -} - -gboolean -location_key(GtkWidget *widget, GdkEvent *event, gpointer data) -{ - struct Client *c = (struct Client *)data; - const gchar *t; - - (void)widget; - - if (event->type == GDK_KEY_PRESS) - { - if (((GdkEventKey *)event)->keyval == GDK_KEY_Return) - { - gtk_widget_grab_focus(c->web_view); - t = gtk_entry_get_text(GTK_ENTRY(c->location)); - if (t != NULL && t[0] == '/') - { - if (search_text != NULL) - g_free(search_text); - search_text = g_strdup(t + 1); /* XXX whacky */ - search(c, 1); - } - else - webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), t); - return TRUE; - } - } - - return FALSE; -} - void -new_client(const gchar *uri) +client_new(const gchar *uri) { - if (cooperative_instances && !alone) + if (cooperative_instances && !cooperative_alone) { write(cooperative_pipe_fp, uri, strlen(uri)); write(cooperative_pipe_fp, "\n", 1); @@ -245,38 +195,38 @@ new_client(const gchar *uri) gtk_window_set_default_size(GTK_WINDOW(c->win), 1024, 768); g_signal_connect(G_OBJECT(c->win), "destroy", - G_CALLBACK(destroy_client), c); + G_CALLBACK(client_destroy), c); gtk_window_set_title(GTK_WINDOW(c->win), __NAME__); c->web_view = webkit_web_view_new(); webkit_web_view_set_full_content_zoom(WEBKIT_WEB_VIEW(c->web_view), TRUE); webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom); g_signal_connect(G_OBJECT(c->web_view), "notify::title", - G_CALLBACK(title_changed), c); + G_CALLBACK(changed_title), c); g_signal_connect(G_OBJECT(c->web_view), "notify::uri", - G_CALLBACK(uri_changed), c); + G_CALLBACK(changed_uri), c); g_signal_connect(G_OBJECT(c->web_view), "notify::load-status", - G_CALLBACK(load_status_changed), c); + G_CALLBACK(changed_load_status), c); g_signal_connect(G_OBJECT(c->web_view), "new-window-policy-decision-requested", - G_CALLBACK(new_client_request), NULL); + G_CALLBACK(client_new_request), NULL); g_signal_connect(G_OBJECT(c->web_view), "mime-type-policy-decision-requested", G_CALLBACK(download_request), NULL); g_signal_connect(G_OBJECT(c->web_view), "download-requested", - G_CALLBACK(do_download), NULL); + G_CALLBACK(download_wget), NULL); g_signal_connect(G_OBJECT(c->web_view), "key-press-event", - G_CALLBACK(web_view_key), c); + G_CALLBACK(key_web_view), c); g_signal_connect(G_OBJECT(c->web_view), "hovering-over-link", - G_CALLBACK(web_view_hover), c); + G_CALLBACK(hover_web_view), c); g_signal_connect(G_OBJECT(c->web_view), "resource-request-starting", G_CALLBACK(adblock), NULL); - if (!language_set) + if (!language_is_set) { g_object_set(webkit_get_default_session(), "accept-language", LANGUAGE, NULL); - language_set = TRUE; + language_is_set = TRUE; } c->scroll = gtk_scrolled_window_new(NULL, NULL); @@ -285,7 +235,7 @@ new_client(const gchar *uri) c->location = gtk_entry_new(); g_signal_connect(G_OBJECT(c->location), "key-press-event", - G_CALLBACK(location_key), c); + G_CALLBACK(key_location), c); c->status = gtk_statusbar_new(); gtk_statusbar_set_has_resize_grip(GTK_STATUSBAR(c->status), FALSE); @@ -306,7 +256,7 @@ new_client(const gchar *uri) } gboolean -new_client_request(WebKitWebView *web_view, WebKitWebFrame *frame, +client_new_request(WebKitWebView *web_view, WebKitWebFrame *frame, WebKitNetworkRequest *request, WebKitWebNavigationAction *navigation_action, WebKitWebPolicyDecision *policy_decision, gpointer user_data) @@ -317,43 +267,13 @@ new_client_request(WebKitWebView *web_view, WebKitWebFrame *frame, (void)user_data; webkit_web_policy_decision_ignore(policy_decision); - new_client(webkit_network_request_get_uri(request)); + client_new(webkit_network_request_get_uri(request)); return TRUE; } -gboolean -remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data) -{ - gchar *uri = NULL; - - (void)condition; - (void)data; - - g_io_channel_read_line(channel, &uri, NULL, NULL, NULL); - if (uri) - { - g_strstrip(uri); - new_client(uri); - g_free(uri); - } - return TRUE; -} - -void -search(gpointer data, gint direction) -{ - struct Client *c = (struct Client *)data; - - if (search_text == NULL) - return; - - webkit_web_view_search_text(WEBKIT_WEB_VIEW(c->web_view), search_text, - FALSE, direction == 1, TRUE); -} - void -setup_cooperation(void) +cooperation_setup(void) { GIOChannel *towatch; gchar *fifopath; @@ -379,30 +299,35 @@ setup_cooperation(void) g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL); } else - alone = FALSE; + cooperative_alone = FALSE; } g_free(fifopath); } void -scroll(GtkAdjustment *a, gint step_type, gdouble factor) +changed_load_status(GObject *obj, GParamSpec *pspec, gpointer data) { - gdouble new, lower, upper, step; - lower = gtk_adjustment_get_lower(a); - upper = gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a) + lower; - if (step_type == 0) - step = gtk_adjustment_get_step_increment(a); + struct Client *c = (struct Client *)data; + + (void)obj; + (void)pspec; + + if (webkit_web_view_get_load_status(WEBKIT_WEB_VIEW(c->web_view)) + == WEBKIT_LOAD_FINISHED) + { + gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1); + gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Finished."); + } else - step = gtk_adjustment_get_page_increment(a); - new = gtk_adjustment_get_value(a) + factor * step; - new = new < lower ? lower : new; - new = new > upper ? upper : new; - gtk_adjustment_set_value(a, new); + { + gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1); + gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Loading..."); + } } void -title_changed(GObject *obj, GParamSpec *pspec, gpointer data) +changed_title(GObject *obj, GParamSpec *pspec, gpointer data) { const gchar *t; struct Client *c = (struct Client *)data; @@ -415,7 +340,7 @@ title_changed(GObject *obj, GParamSpec *pspec, gpointer data) } void -uri_changed(GObject *obj, GParamSpec *pspec, gpointer data) +changed_uri(GObject *obj, GParamSpec *pspec, gpointer data) { const gchar *t; struct Client *c = (struct Client *)data; @@ -427,106 +352,63 @@ uri_changed(GObject *obj, GParamSpec *pspec, gpointer data) gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? __NAME__ : t)); } -void -usage(void) -{ - fprintf(stderr, "Usage: "__NAME__" [OPTION]... <URI>...\n"); - exit(EXIT_FAILURE); -} - -Window -launch_tabbed(void) +gboolean +download_request(WebKitWebView *web_view, WebKitWebFrame *frame, + WebKitNetworkRequest *request, gchar *mime_type, + WebKitWebPolicyDecision *policy_decision, gpointer data) { - gint tabbed_stdout; - GIOChannel *tabbed_stdout_channel; - GError *err = NULL; - gchar *output = NULL; - char *argv[] = { "tabbed", "-c", "-d", NULL }; - Window plug_into; - - if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, - NULL, NULL, NULL, &tabbed_stdout, NULL, - &err)) - { - fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message); - g_error_free(err); - return 0; - } + (void)frame; + (void)request; + (void)data; - tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout); - g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL); - if (output == NULL) + if (!webkit_web_view_can_show_mime_type(web_view, mime_type)) { - fprintf(stderr, __NAME__": Could not read XID from tabbed\n"); - return 0; + webkit_web_policy_decision_download(policy_decision); + return TRUE; } - - g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL); - - g_strstrip(output); - plug_into = strtol(output, NULL, 16); - g_free(output); - return plug_into; + return FALSE; } -void -load_adblock(void) +gboolean +download_wget(WebKitWebView *web_view, WebKitDownload *download, gpointer data) { - GRegex *re = NULL; - GError *err = NULL; - GIOChannel *channel = NULL; - gchar *path = NULL; - gchar *buf = NULL; + const gchar *uri; + char id[16] = ""; + gint ret; - path = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock.black", - NULL); - channel = g_io_channel_new_file(path, "r", &err); - if (channel != NULL) + (void)web_view; + (void)data; + + uri = webkit_download_get_uri(download); + if (fork() == 0) { - while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL) - == G_IO_STATUS_NORMAL) + chdir(DOWNLOAD_DIR); + if (embed == 0) + ret = execlp("xterm", "xterm", "-hold", "-e", "wget", uri, NULL); + else { - g_strstrip(buf); - re = g_regex_new(buf, - G_REGEX_CASELESS | G_REGEX_OPTIMIZE, - G_REGEX_MATCH_PARTIAL, &err); - if (err != NULL) + if (snprintf(id, 16, "%ld", embed) >= 16) { - fprintf(stderr, __NAME__": Could not compile regex: %s\n", buf); - g_error_free(err); - err = NULL; + fprintf(stderr, __NAME__": id for xterm embed truncated!\n"); + exit(EXIT_FAILURE); } - adblock_patterns = g_slist_append(adblock_patterns, re); + ret = execlp("xterm", "xterm", "-hold", "-into", id, "-e", "wget", + uri, NULL); + } - g_free(buf); + if (ret == -1) + { + fprintf(stderr, __NAME__": exec'ing xterm for download"); + perror(" failed"); + exit(EXIT_FAILURE); } } - g_free(path); -} - -void -load_status_changed(GObject *obj, GParamSpec *pspec, gpointer data) -{ - struct Client *c = (struct Client *)data; - (void)obj; - (void)pspec; - - if (webkit_web_view_get_load_status(WEBKIT_WEB_VIEW(c->web_view)) - == WEBKIT_LOAD_FINISHED) - { - gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1); - gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Finished."); - } - else - { - gtk_statusbar_pop(GTK_STATUSBAR(c->status), 1); - gtk_statusbar_push(GTK_STATUSBAR(c->status), 1, "Loading..."); - } + return FALSE; } void -web_view_hover(WebKitWebView *web_view, gchar *title, gchar *uri, +hover_web_view(WebKitWebView *web_view, gchar *title, gchar *uri, gpointer data) { struct Client *c = (struct Client *)data; @@ -540,7 +422,37 @@ web_view_hover(WebKitWebView *web_view, gchar *title, gchar *uri, } gboolean -web_view_key(GtkWidget *widget, GdkEvent *event, gpointer data) +key_location(GtkWidget *widget, GdkEvent *event, gpointer data) +{ + struct Client *c = (struct Client *)data; + const gchar *t; + + (void)widget; + + if (event->type == GDK_KEY_PRESS) + { + if (((GdkEventKey *)event)->keyval == GDK_KEY_Return) + { + gtk_widget_grab_focus(c->web_view); + t = gtk_entry_get_text(GTK_ENTRY(c->location)); + if (t != NULL && t[0] == '/') + { + if (search_text != NULL) + g_free(search_text); + search_text = g_strdup(t + 1); /* XXX whacky */ + search(c, 1); + } + else + webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), t); + return TRUE; + } + } + + return FALSE; +} + +gboolean +key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data) { struct Client *c = (struct Client *)data; @@ -619,6 +531,95 @@ web_view_key(GtkWidget *widget, GdkEvent *event, gpointer data) return FALSE; } +gboolean +remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data) +{ + gchar *uri = NULL; + + (void)condition; + (void)data; + + g_io_channel_read_line(channel, &uri, NULL, NULL, NULL); + if (uri) + { + g_strstrip(uri); + client_new(uri); + g_free(uri); + } + return TRUE; +} + +void +search(gpointer data, gint direction) +{ + struct Client *c = (struct Client *)data; + + if (search_text == NULL) + return; + + webkit_web_view_search_text(WEBKIT_WEB_VIEW(c->web_view), search_text, + FALSE, direction == 1, TRUE); +} + +void +scroll(GtkAdjustment *a, gint step_type, gdouble factor) +{ + gdouble new, lower, upper, step; + lower = gtk_adjustment_get_lower(a); + upper = gtk_adjustment_get_upper(a) - gtk_adjustment_get_page_size(a) + lower; + if (step_type == 0) + step = gtk_adjustment_get_step_increment(a); + else + step = gtk_adjustment_get_page_increment(a); + new = gtk_adjustment_get_value(a) + factor * step; + new = new < lower ? lower : new; + new = new > upper ? upper : new; + gtk_adjustment_set_value(a, new); +} + +Window +tabbed_launch(void) +{ + gint tabbed_stdout; + GIOChannel *tabbed_stdout_channel; + GError *err = NULL; + gchar *output = NULL; + char *argv[] = { "tabbed", "-c", "-d", NULL }; + Window plug_into; + + if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, + NULL, NULL, NULL, &tabbed_stdout, NULL, + &err)) + { + fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message); + g_error_free(err); + return 0; + } + + tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout); + g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL); + if (output == NULL) + { + fprintf(stderr, __NAME__": Could not read XID from tabbed\n"); + return 0; + } + + g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL); + + g_strstrip(output); + plug_into = strtol(output, NULL, 16); + g_free(output); + return plug_into; +} + +void +usage(void) +{ + fprintf(stderr, "Usage: "__NAME__" [OPTION]... <URI>...\n"); + exit(EXIT_FAILURE); +} + + int main(int argc, char **argv) { @@ -635,7 +636,7 @@ main(int argc, char **argv) break; case 'e': embed = atol(optarg); - automagic_tabbed = FALSE; + tabbed_automagic = FALSE; break; case 'r': show_all_requests = TRUE; @@ -644,7 +645,7 @@ main(int argc, char **argv) cooperative_instances = FALSE; break; case 'T': - automagic_tabbed = FALSE; + tabbed_automagic = FALSE; break; default: usage(); @@ -654,16 +655,16 @@ main(int argc, char **argv) if (optind >= argc) usage(); - load_adblock(); - setup_cooperation(); + adblock_load(); + cooperation_setup(); - if (automagic_tabbed && !(cooperative_instances && !alone)) - embed = launch_tabbed(); + if (tabbed_automagic && !(cooperative_instances && !cooperative_alone)) + embed = tabbed_launch(); first_uri = g_strdup(argv[optind]); for (i = optind; i < argc; i++) - new_client(argv[i]); - if (!cooperative_instances || alone) + client_new(argv[i]); + if (!cooperative_instances || cooperative_alone) gtk_main(); exit(EXIT_SUCCESS); } |