X-Git-Url: https://git.armaanb.net/?p=chorizo.git;a=blobdiff_plain;f=browser.c;h=8df0e330777700e743a953d9cd7123db95e30109;hp=e943f4812562c6e4b4386420ddbc50102b968140;hb=8720c4df27e449f88ebecfb086cd6ae79a363420;hpb=bd5749a1eaf3ce19687f1b68116343860e213055 diff --git a/browser.c b/browser.c index e943f48..8df0e33 100644 --- a/browser.c +++ b/browser.c @@ -14,44 +14,45 @@ #include -static void client_destroy(GtkWidget *, gpointer); -static gboolean client_destroy_request(WebKitWebView *, gpointer); -static WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean); -static WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *, - gpointer); -static void cooperation_setup(void); -static void changed_download_progress(GObject *, GParamSpec *, gpointer); -static void changed_load_progress(GObject *, GParamSpec *, gpointer); -static void changed_title(GObject *, GParamSpec *, gpointer); -static void changed_uri(GObject *, GParamSpec *, gpointer); -static gboolean crashed_web_view(WebKitWebView *, gpointer); -static gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *, - WebKitPolicyDecisionType, gpointer); -static gboolean download_handle(WebKitDownload *, gchar *, gpointer); -static void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer); -static void downloadmanager_cancel(GtkToolButton *, gpointer); -static gboolean downloadmanager_delete(GtkWidget *, gpointer); -static void downloadmanager_setup(void); -static gchar *ensure_uri_scheme(const gchar *); -static void external_handler_run(GSimpleAction *, GVariant *, gpointer); -static void grab_environment_configuration(void); -static void grab_feeds_finished(GObject *, GAsyncResult *, gpointer); -static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer); -static void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer); -static gboolean key_common(GtkWidget *, GdkEvent *, gpointer); -static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer); -static gboolean key_location(GtkWidget *, GdkEvent *, gpointer); -static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer); -static void keywords_load(void); -static gboolean keywords_try_search(WebKitWebView *, const gchar *); -static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *, - WebKitHitTestResult *, gpointer); -static gboolean quit_if_nothing_active(void); -static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer); -static void search(gpointer, gint); -static void show_web_view(WebKitWebView *, gpointer); -static Window tabbed_launch(void); -static void trust_user_certs(WebKitWebContext *); +void client_destroy(GtkWidget *, gpointer); +WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean, + gboolean); +WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *, + gpointer); +void cooperation_setup(void); +void changed_download_progress(GObject *, GParamSpec *, gpointer); +void changed_load_progress(GObject *, GParamSpec *, gpointer); +void changed_favicon(GObject *, GParamSpec *, gpointer); +void changed_title(GObject *, GParamSpec *, gpointer); +void changed_uri(GObject *, GParamSpec *, gpointer); +gboolean crashed_web_view(WebKitWebView *, gpointer); +gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *, + WebKitPolicyDecisionType, gpointer); +gboolean download_handle(WebKitDownload *, gchar *, gpointer); +void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer); +void downloadmanager_cancel(GtkToolButton *, gpointer); +gboolean downloadmanager_delete(GtkWidget *, gpointer); +void downloadmanager_setup(void); +gchar *ensure_uri_scheme(const gchar *); +void grab_environment_configuration(void); +void grab_feeds_finished(GObject *, GAsyncResult *, gpointer); +void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer); +void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer); +void init_default_web_context(void); +gboolean key_common(GtkWidget *, GdkEvent *, gpointer); +gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer); +gboolean key_location(GtkWidget *, GdkEvent *, gpointer); +gboolean key_tablabel(GtkWidget *, GdkEvent *, gpointer); +gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer); +void mainwindow_setup(void); +void mainwindow_title(gint); +void notebook_switch_page(GtkNotebook *, GtkWidget *, guint, gpointer); +gboolean quit_if_nothing_active(void); +gboolean remote_msg(GIOChannel *, GIOCondition, gpointer); +void run_user_scripts(WebKitWebView *); +void search(gpointer, gint); +void show_web_view(WebKitWebView *, gpointer); +void trust_user_certs(WebKitWebContext *); struct Client @@ -60,11 +61,19 @@ struct Client gchar *hover_uri; gchar *feed_html; GtkWidget *location; + GtkWidget *tabicon; + GtkWidget *tablabel; GtkWidget *vbox; GtkWidget *web_view; - GtkWidget *win; + gboolean focus_new_tab; }; +struct MainWindow +{ + GtkWidget *win; + GtkWidget *notebook; +} mw; + struct DownloadManager { GtkWidget *scroll; @@ -73,56 +82,51 @@ struct DownloadManager } dm; -static const gchar *accepted_language[2] = { NULL, NULL }; -static gint clients = 0, downloads = 0; -static gboolean cooperative_alone = TRUE; -static gboolean cooperative_instances = TRUE; -static int cooperative_pipe_fp = 0; -static gchar *download_dir = "/var/tmp"; -static gboolean enable_webgl = FALSE; -static gboolean enable_console_to_stdout = FALSE; -static Window embed = 0; -static gchar *fifo_suffix = "main"; -static gdouble global_zoom = 1.0; -static gchar *history_file = NULL; -static gchar *home_uri = "about:blank"; -static gboolean initial_wc_setup_done = FALSE; -static GHashTable *keywords = NULL; -static gchar *search_text = NULL; -static gboolean tabbed_automagic = TRUE; -static gchar *user_agent = NULL; +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; +gchar *download_dir = "/var/tmp"; +gboolean enable_console_to_stdout = FALSE; +gchar *fifo_suffix = "main"; +gdouble global_zoom = 1.0; +gchar *history_file = NULL; +gchar *home_uri = "about:blank"; +gchar *search_text = NULL; +GtkPositionType tab_pos = GTK_POS_TOP; +gint tab_width_chars = 20; +gchar *user_agent = NULL; void client_destroy(GtkWidget *widget, gpointer data) { struct Client *c = (struct Client *)data; + gint idx; g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view), changed_load_progress, c); + idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox); + if (idx == -1) + fprintf(stderr, __NAME__": Tab index was -1, bamboozled\n"); + else + gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx); + free(c); clients--; quit_if_nothing_active(); } -gboolean -client_destroy_request(WebKitWebView *web_view, gpointer data) -{ - struct Client *c = (struct Client *)data; - - gtk_widget_destroy(c->win); - - return TRUE; -} - WebKitWebView * -client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show) +client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show, + gboolean focus_tab) { struct Client *c; - WebKitWebContext *wc; gchar *f; + GtkWidget *evbox, *tabbox; if (uri != NULL && cooperative_instances && !cooperative_alone) { @@ -140,33 +144,16 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show) exit(EXIT_FAILURE); } - if (embed != 0) - { - c->win = gtk_plug_new(embed); - if (!gtk_plug_get_embedded(GTK_PLUG(c->win))) - { - fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed); - gtk_widget_destroy(c->win); - c->win = NULL; - embed = 0; - } - } - - if (c->win == NULL) - c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL); - - gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600); - - g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c); - gtk_window_set_title(GTK_WINDOW(c->win), __NAME__); + c->focus_new_tab = focus_tab; if (related_wv == NULL) c->web_view = webkit_web_view_new(); else c->web_view = webkit_web_view_new_with_related_view(related_wv); - wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view)); webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom); + 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", G_CALLBACK(changed_title), c); g_signal_connect(G_OBJECT(c->web_view), "notify::uri", @@ -175,10 +162,8 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show) G_CALLBACK(changed_load_progress), c); g_signal_connect(G_OBJECT(c->web_view), "create", G_CALLBACK(client_new_request), NULL); - g_signal_connect(G_OBJECT(c->web_view), "context-menu", - G_CALLBACK(menu_web_view), c); g_signal_connect(G_OBJECT(c->web_view), "close", - G_CALLBACK(client_destroy_request), c); + G_CALLBACK(client_destroy), c); g_signal_connect(G_OBJECT(c->web_view), "decide-policy", G_CALLBACK(decide_policy), NULL); g_signal_connect(G_OBJECT(c->web_view), "key-press-event", @@ -192,19 +177,6 @@ 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 (!initial_wc_setup_done) - { - if (accepted_language[0] != NULL) - webkit_web_context_set_preferred_languages(wc, accepted_language); - - g_signal_connect(G_OBJECT(wc), "download-started", - G_CALLBACK(download_handle_start), NULL); - - trust_user_certs(wc); - - initial_wc_setup_done = TRUE; - } - if (user_agent != NULL) g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))), "user-agent", user_agent, NULL); @@ -212,8 +184,7 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show) if (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); - if (enable_webgl) - webkit_settings_set_enable_webgl(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); c->location = gtk_entry_new(); g_signal_connect(G_OBJECT(c->location), "key-press-event", @@ -233,7 +204,40 @@ client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show) gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0); gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0); - gtk_container_add(GTK_CONTAINER(c->win), c->vbox); + c->tabicon = gtk_image_new_from_icon_name("text-html", GTK_ICON_SIZE_SMALL_TOOLBAR); + + 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_widget_set_has_tooltip(c->tablabel, TRUE); + + /* XXX I don't own a HiDPI screen, so I don't know if scale_factor + * does the right thing. */ + tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, + 5 * gtk_widget_get_scale_factor(mw.win)); + gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0); + gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0); + + evbox = gtk_event_box_new(); + gtk_container_add(GTK_CONTAINER(evbox), tabbox); + g_signal_connect(G_OBJECT(evbox), "button-release-event", + G_CALLBACK(key_tablabel), c); + + gtk_widget_add_events(evbox, GDK_SCROLL_MASK); + g_signal_connect(G_OBJECT(evbox), "scroll-event", + G_CALLBACK(key_tablabel), c); + + /* For easy access, store a reference to our label. */ + g_object_set_data(G_OBJECT(evbox), "lariza-tab-label", c->tablabel); + + /* This only shows the event box and the label inside, nothing else. + * Needed because the evbox/label is "internal" to the notebook and + * not part of the normal "widget tree" (IIUC). */ + gtk_widget_show_all(evbox); + + gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox, + gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1); + gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE); if (show) show_web_view(NULL, c); @@ -257,7 +261,7 @@ WebKitWebView * client_new_request(WebKitWebView *web_view, WebKitNavigationAction *navigation_action, gpointer data) { - return client_new(NULL, web_view, FALSE); + return client_new(NULL, web_view, FALSE, FALSE); } void @@ -365,17 +369,52 @@ changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data) { p = 0; - /* The page has loaded fully. We now run a the short JavaScript + /* The page has loaded fully. We now run the short JavaScript * snippet above that operates on the DOM. It tries to grab all * occurences of , i.e. RSS/Atom feed * 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); } +void +changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data) +{ + struct Client *c = (struct Client *)data; + cairo_surface_t *f; + int w, h, w_should, h_should; + GdkPixbuf *pb, *pb_scaled; + + f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view)); + if (f == NULL) + { + gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html", + GTK_ICON_SIZE_SMALL_TOOLBAR); + } + else + { + w = cairo_image_surface_get_width(f); + h = cairo_image_surface_get_height(f); + pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h); + if (pb != NULL) + { + w_should = 16 * gtk_widget_get_scale_factor(c->tabicon); + h_should = 16 * gtk_widget_get_scale_factor(c->tabicon); + pb_scaled = gdk_pixbuf_scale_simple(pb, w_should, h_should, + GDK_INTERP_BILINEAR); + gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon), pb_scaled); + + g_object_unref(pb_scaled); + g_object_unref(pb); + } + } +} + void changed_title(GObject *obj, GParamSpec *pspec, gpointer data) { @@ -391,7 +430,9 @@ changed_title(GObject *obj, GParamSpec *pspec, gpointer data) t = t == NULL ? u : t; t = t[0] == 0 ? u : t; - gtk_window_set_title(GTK_WINDOW(c->win), t); + gtk_label_set_text(GTK_LABEL(c->tablabel), t); + gtk_widget_set_tooltip_text(c->tablabel, t); + mainwindow_title(gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook))); } void @@ -591,7 +632,8 @@ ensure_uri_scheme(const gchar *t) !g_str_has_prefix(f, "https:") && !g_str_has_prefix(f, "file:") && !g_str_has_prefix(f, "about:") && - !g_str_has_prefix(f, "data:")) + !g_str_has_prefix(f, "data:") && + !g_str_has_prefix(f, "webkit:")) { g_free(f); fabs = realpath(t, NULL); @@ -608,29 +650,6 @@ ensure_uri_scheme(const gchar *t) return g_strdup(t); } -void -external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data) -{ - struct Client *c = (struct Client *)data; - gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL }; - GPid pid; - GError *err = NULL; - - (void)simple; - (void)param; - - argv[2] = c->external_handler_uri; - if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, - &pid, &err)) - { - fprintf(stderr, __NAME__": Could not launch key handler: %s\n", - err->message); - g_error_free(err); - } - else - g_spawn_close_pid(pid); -} - void grab_environment_configuration(void) { @@ -648,10 +667,6 @@ grab_environment_configuration(void) if (e != NULL) enable_console_to_stdout = TRUE; - e = g_getenv(__NAME_UPPERCASE__"_ENABLE_EXPERIMENTAL_WEBGL"); - if (e != NULL) - enable_webgl = TRUE; - e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX"); if (e != NULL) fifo_suffix = g_strdup(e); @@ -664,6 +679,23 @@ grab_environment_configuration(void) if (e != NULL) home_uri = g_strdup(e); + e = g_getenv(__NAME_UPPERCASE__"_TAB_POS"); + if (e != NULL) + { + if (strcmp(e, "top") == 0) + tab_pos = GTK_POS_TOP; + if (strcmp(e, "right") == 0) + tab_pos = GTK_POS_RIGHT; + if (strcmp(e, "bottom") == 0) + tab_pos = GTK_POS_BOTTOM; + if (strcmp(e, "left") == 0) + tab_pos = GTK_POS_LEFT; + } + + e = g_getenv(__NAME_UPPERCASE__"_TAB_WIDTH_CHARS"); + if (e != NULL) + tab_width_chars = atoi(e); + e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT"); if (e != NULL) user_agent = g_strdup(e); @@ -678,17 +710,18 @@ grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data) { struct Client *c = (struct Client *)data; WebKitJavascriptResult *js_result; - JSValueRef value; - JSGlobalContextRef context; + JSCValue *value; + JSCException *exception; GError *err = NULL; - JSStringRef js_str_value; - gsize str_length; + gchar *str_value; g_free(c->feed_html); c->feed_html = NULL; /* This was taken almost verbatim from the example in WebKit's - * documentation. */ + * documentation: + * + * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */ js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object), result, &err); @@ -699,16 +732,18 @@ grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data) return; } - context = webkit_javascript_result_get_global_context(js_result); - value = webkit_javascript_result_get_value(js_result); - - if (JSValueIsString(context, value)) + value = webkit_javascript_result_get_js_value(js_result); + if (jsc_value_is_string(value)) { - js_str_value = JSValueToStringCopy(context, value, NULL); - str_length = JSStringGetMaximumUTF8CStringSize(js_str_value); - c->feed_html = (gchar *)g_malloc(str_length); - JSStringGetUTF8CString(js_str_value, c->feed_html, str_length); - JSStringRelease(js_str_value); + str_value = jsc_value_to_string(value); + exception = jsc_context_get_exception(jsc_value_get_context(value)); + if (exception != NULL) + { + fprintf(stderr, __NAME__": Error running javascript: %s\n", + jsc_exception_get_message(exception)); + } + else + c->feed_html = str_value; gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location), GTK_ENTRY_ICON_PRIMARY, @@ -799,6 +834,38 @@ icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event, } } +void +init_default_web_context(void) +{ + gchar *p; + WebKitWebContext *wc; + + wc = webkit_web_context_get_default(); + + p = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock.black", NULL); + webkit_web_context_set_sandbox_enabled(wc, TRUE); + webkit_web_context_add_path_to_sandbox(wc, p, TRUE); + g_free(p); + + webkit_web_context_set_process_model(wc, + WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES); + + p = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions", + NULL); + 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); + + g_signal_connect(G_OBJECT(wc), "download-started", + G_CALLBACK(download_handle_start), NULL); + + trust_user_certs(wc); + + webkit_web_context_set_favicon_database_directory(wc, NULL); +} + gboolean key_common(GtkWidget *widget, GdkEvent *event, gpointer data) { @@ -813,7 +880,7 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data) switch (((GdkEventKey *)event)->keyval) { case GDK_KEY_q: /* close window (left hand) */ - gtk_widget_destroy(c->win); + client_destroy(NULL, c); return TRUE; case GDK_KEY_w: /* home (left hand) */ f = ensure_uri_scheme(home_uri); @@ -822,7 +889,7 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data) return TRUE; case GDK_KEY_e: /* new tab (left hand) */ f = ensure_uri_scheme(home_uri); - client_new(f, NULL, TRUE); + client_new(f, NULL, TRUE, TRUE); g_free(f); return TRUE; case GDK_KEY_r: /* reload (left hand) */ @@ -850,12 +917,11 @@ key_common(GtkWidget *widget, GdkEvent *event, gpointer data) case GDK_KEY_c: /* reload trusted certs (left hand) */ trust_user_certs(wc); return TRUE; - case GDK_KEY_x: /* launch external handler (left hand) */ - if (c->external_handler_uri != NULL) - g_free(c->external_handler_uri); - c->external_handler_uri = g_strdup( - webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view))); - external_handler_run(NULL, NULL, c); + case GDK_KEY_a: /* go one tab to the left (left hand) */ + gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook)); + return TRUE; + case GDK_KEY_s: /* go one tab to the right (left hand) */ + gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook)); return TRUE; } } @@ -921,7 +987,7 @@ key_location(GtkWidget *widget, GdkEvent *event, gpointer data) search_text = g_strdup(t + 2); search(c, 0); } - else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t)) + else { f = ensure_uri_scheme(t); webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f); @@ -939,6 +1005,39 @@ key_location(GtkWidget *widget, GdkEvent *event, gpointer data) return FALSE; } +gboolean +key_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data) +{ + GdkScrollDirection direction; + + if (event->type == GDK_BUTTON_RELEASE) + { + switch (((GdkEventButton *)event)->button) + { + case 2: + client_destroy(NULL, data); + return TRUE; + } + } + else if (event->type == GDK_SCROLL) + { + gdk_event_get_scroll_direction(event, &direction); + switch (direction) + { + case GDK_SCROLL_UP: + gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook)); + break; + case GDK_SCROLL_DOWN: + gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook)); + break; + default: + break; + } + return TRUE; + } + return FALSE; +} + gboolean key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data) { @@ -964,7 +1063,7 @@ key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data) case 2: if (c->hover_uri != NULL) { - client_new(c->hover_uri, NULL, TRUE); + client_new(c->hover_uri, NULL, TRUE, FALSE); return TRUE; } break; @@ -994,102 +1093,41 @@ key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data) } void -keywords_load(void) +mainwindow_setup(void) { - GError *err = NULL; - GIOChannel *channel = NULL; - gchar *path = NULL, *buf = NULL; - gchar **tokens = NULL; - - keywords = g_hash_table_new(g_str_hash, g_str_equal); - - path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch", - 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); - if (buf[0] != '#') - { - tokens = g_strsplit(buf, " ", 2); - if (tokens[0] != NULL && tokens[1] != NULL) - g_hash_table_insert(keywords, g_strdup(tokens[0]), - g_strdup(tokens[1])); - g_strfreev(tokens); - } - g_free(buf); - } - g_io_channel_shutdown(channel, FALSE, NULL); - } - g_free(path); + mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL); + gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600); + g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL); + gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__); + + mw.notebook = gtk_notebook_new(); + gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE); + gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), tab_pos); + gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook); + g_signal_connect(G_OBJECT(mw.notebook), "switch-page", + G_CALLBACK(notebook_switch_page), NULL); } -gboolean -keywords_try_search(WebKitWebView *web_view, const gchar *t) +void +mainwindow_title(gint idx) { - gboolean ret = FALSE; - gchar **tokens = NULL; - gchar *val = NULL, *escaped = NULL, *uri = NULL; + GtkWidget *child, *widg, *tablabel; + const gchar *text; - tokens = g_strsplit(t, " ", 2); - if (tokens[0] != NULL && tokens[1] != NULL) - { - val = g_hash_table_lookup(keywords, tokens[0]); - if (val != NULL) - { - escaped = g_uri_escape_string(tokens[1], NULL, TRUE); - uri = g_strdup_printf((gchar *)val, escaped); - webkit_web_view_load_uri(web_view, uri); - g_free(uri); - g_free(escaped); - ret = TRUE; - } - } - g_strfreev(tokens); + child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx); + if (child == NULL) + return; - return ret; + widg = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child); + tablabel = (GtkWidget *)g_object_get_data(G_OBJECT(widg), "lariza-tab-label"); + text = gtk_label_get_text(GTK_LABEL(tablabel)); + gtk_window_set_title(GTK_WINDOW(mw.win), text); } -gboolean -menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev, - WebKitHitTestResult *ht, gpointer data) +void +notebook_switch_page(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data) { - struct Client *c = (struct Client *)data; - GSimpleAction *action = NULL; - WebKitContextMenuItem *mi = NULL; - const gchar *uri = NULL; - - (void)ev; - - if (webkit_hit_test_result_context_is_link(ht)) - uri = webkit_hit_test_result_get_link_uri(ht); - else if (webkit_hit_test_result_context_is_image(ht)) - uri = webkit_hit_test_result_get_image_uri(ht); - else if (webkit_hit_test_result_context_is_media(ht)) - uri = webkit_hit_test_result_get_media_uri(ht); - - if (uri != NULL) - { - webkit_context_menu_append(menu, webkit_context_menu_item_new_separator()); - - if (c->external_handler_uri != NULL) - g_free(c->external_handler_uri); - c->external_handler_uri = g_strdup(uri); - action = g_simple_action_new("external_handler", NULL); - g_signal_connect(G_OBJECT(action), "activate", - G_CALLBACK(external_handler_run), data); - mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action), - "Open with external handler", - NULL); - webkit_context_menu_append(menu, mi); - g_object_unref(action); - } - - /* FALSE = Show the menu. (TRUE = Don't ever show it.) */ - return FALSE; + mainwindow_title(idx); } gboolean @@ -1118,12 +1156,42 @@ remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data) if (uri) { g_strstrip(uri); - client_new(uri, NULL, TRUE); + client_new(uri, NULL, TRUE, TRUE); g_free(uri); } 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_config_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); + if (g_str_has_suffix(path, ".js")) + { + if (g_file_get_contents(path, &contents, NULL, NULL)) + { + webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL); + g_free(contents); + } + } + g_free(path); + } + g_dir_close(scriptdir); + } + + g_free(base); +} + void search(gpointer data, gint direction) { @@ -1155,51 +1223,20 @@ void show_web_view(WebKitWebView *web_view, gpointer data) { struct Client *c = (struct Client *)data; + gint idx; (void)web_view; - gtk_widget_grab_focus(c->web_view); - gtk_widget_show_all(c->win); -} - -Window -tabbed_launch(void) -{ - gint tabbed_stdout; - GIOChannel *tabbed_stdout_channel; - GError *err = NULL; - gchar *output = NULL; - char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL }; - Window plug_into; + gtk_widget_show_all(mw.win); - if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, - NULL, NULL, NULL, &tabbed_stdout, NULL, - &err)) + if (c->focus_new_tab) { - fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message); - g_error_free(err); - return 0; - } + idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox); + if (idx != -1) + gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx); - tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout); - if (tabbed_stdout_channel == NULL) - { - fprintf(stderr, __NAME__": Could open tabbed's stdout\n"); - return 0; + gtk_widget_grab_focus(c->web_view); } - g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL); - g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL); - if (output == NULL) - { - fprintf(stderr, __NAME__": Could not read XID from tabbed\n"); - return 0; - } - g_strstrip(output); - plug_into = strtol(output, NULL, 16); - g_free(output); - if (plug_into == 0) - fprintf(stderr, __NAME__": The XID from tabbed is 0\n"); - return plug_into; } void @@ -1233,61 +1270,43 @@ trust_user_certs(WebKitWebContext *wc) int main(int argc, char **argv) { - gchar *c; int opt, i; gtk_init(&argc, &argv); - webkit_web_context_set_process_model(webkit_web_context_get_default(), - WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES); - grab_environment_configuration(); - while ((opt = getopt(argc, argv, "e:CT")) != -1) + while ((opt = getopt(argc, argv, "C")) != -1) { switch (opt) { - case 'e': - embed = atol(optarg); - tabbed_automagic = FALSE; - break; case 'C': cooperative_instances = FALSE; break; - case 'T': - tabbed_automagic = FALSE; - break; default: fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n"); exit(EXIT_FAILURE); } } - keywords_load(); if (cooperative_instances) cooperation_setup(); - downloadmanager_setup(); - - if (tabbed_automagic && !(cooperative_instances && !cooperative_alone)) - embed = tabbed_launch(); if (!cooperative_instances || cooperative_alone) - { - c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions", - NULL); - webkit_web_context_set_web_extensions_directory( - webkit_web_context_get_default(), c - ); - } + init_default_web_context(); + + downloadmanager_setup(); + mainwindow_setup(); if (optind >= argc) - client_new(home_uri, NULL, TRUE); + client_new(home_uri, NULL, TRUE, TRUE); else { for (i = optind; i < argc; i++) - client_new(argv[i], NULL, TRUE); + client_new(argv[i], NULL, TRUE, TRUE); } if (!cooperative_instances || cooperative_alone) gtk_main(); + exit(EXIT_SUCCESS); }