11 #include <gdk/gdkkeysyms.h>
13 #include <webkit2/webkit2.h>
16 static void client_destroy(GtkWidget *, gpointer);
17 static gboolean client_destroy_request(WebKitWebView *, gpointer);
18 static WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean);
19 static WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
21 static void cooperation_setup(void);
22 static void changed_download_progress(GObject *, GParamSpec *, gpointer);
23 static void changed_load_progress(GObject *, GParamSpec *, gpointer);
24 static void changed_title(GObject *, GParamSpec *, gpointer);
25 static void changed_uri(GObject *, GParamSpec *, gpointer);
26 static gboolean crashed_web_view(WebKitWebView *, gpointer);
27 static gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
28 WebKitPolicyDecisionType, gpointer);
29 static gboolean download_handle(WebKitDownload *, gchar *, gpointer);
30 static void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
31 static void downloadmanager_cancel(GtkToolButton *, gpointer data);
32 static gboolean downloadmanager_delete(GtkWidget *, gpointer);
33 static void downloadmanager_setup(void);
34 static gchar *ensure_uri_scheme(const gchar *);
35 static void external_handler_run(gpointer);
36 static void grab_environment_configuration(void);
37 static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
38 static gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
39 static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
40 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
41 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
42 static void keywords_load(void);
43 static gboolean keywords_try_search(WebKitWebView *, const gchar *);
44 static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
45 WebKitHitTestResult *, gpointer);
46 static gboolean quit_if_nothing_active(void);
47 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
48 static void search(gpointer, gint);
49 static void show_web_view(WebKitWebView *, gpointer);
50 static Window tabbed_launch(void);
51 static void trust_user_certs(WebKitWebContext *);
56 gchar *external_handler_uri;
64 struct DownloadManager
72 static const gchar *accepted_language[2] = { NULL, NULL };
73 static gint clients = 0, downloads = 0;
74 static gboolean cooperative_alone = TRUE;
75 static gboolean cooperative_instances = TRUE;
76 static int cooperative_pipe_fp = 0;
77 static gchar *download_dir = "/var/tmp";
78 static gboolean enable_webgl = FALSE;
79 static gboolean enable_console_to_stdout = FALSE;
80 static Window embed = 0;
81 static gchar *fifo_suffix = "main";
82 static gdouble global_zoom = 1.0;
83 static gchar *history_file = NULL;
84 static gchar *home_uri = "about:blank";
85 static gboolean initial_wc_setup_done = FALSE;
86 static GHashTable *keywords = NULL;
87 static gchar *search_text = NULL;
88 static gboolean tabbed_automagic = TRUE;
89 static gchar *user_agent = NULL;
93 client_destroy(GtkWidget *widget, gpointer data)
95 struct Client *c = (struct Client *)data;
97 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
98 changed_load_progress, c);
103 quit_if_nothing_active();
107 client_destroy_request(WebKitWebView *web_view, gpointer data)
109 struct Client *c = (struct Client *)data;
111 gtk_widget_destroy(c->win);
117 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
120 WebKitWebContext *wc;
123 if (uri != NULL && cooperative_instances && !cooperative_alone)
125 f = ensure_uri_scheme(uri);
126 write(cooperative_pipe_fp, f, strlen(f));
127 write(cooperative_pipe_fp, "\n", 1);
132 c = calloc(1, sizeof(struct Client));
135 fprintf(stderr, __NAME__": fatal: calloc failed\n");
141 c->win = gtk_plug_new(embed);
142 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
144 fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
145 gtk_widget_destroy(c->win);
152 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
154 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
156 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
157 gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
159 if (related_wv == NULL)
160 c->web_view = webkit_web_view_new();
162 c->web_view = webkit_web_view_new_with_related_view(related_wv);
163 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
165 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
166 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
167 G_CALLBACK(changed_title), c);
168 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
169 G_CALLBACK(changed_uri), c);
170 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
171 G_CALLBACK(changed_load_progress), c);
172 g_signal_connect(G_OBJECT(c->web_view), "create",
173 G_CALLBACK(client_new_request), NULL);
174 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
175 G_CALLBACK(menu_web_view), c);
176 g_signal_connect(G_OBJECT(c->web_view), "close",
177 G_CALLBACK(client_destroy_request), c);
178 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
179 G_CALLBACK(decide_policy), NULL);
180 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
181 G_CALLBACK(key_web_view), c);
182 g_signal_connect(G_OBJECT(c->web_view), "button-press-event",
183 G_CALLBACK(key_web_view), c);
184 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
185 G_CALLBACK(key_web_view), c);
186 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
187 G_CALLBACK(hover_web_view), c);
188 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
189 G_CALLBACK(crashed_web_view), c);
191 if (!initial_wc_setup_done)
193 if (accepted_language[0] != NULL)
194 webkit_web_context_set_preferred_languages(wc, accepted_language);
196 g_signal_connect(G_OBJECT(wc), "download-started",
197 G_CALLBACK(download_handle_start), NULL);
199 trust_user_certs(wc);
201 initial_wc_setup_done = TRUE;
204 if (user_agent != NULL)
205 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
206 "user-agent", user_agent, NULL);
208 if (enable_console_to_stdout)
209 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
212 webkit_settings_set_enable_webgl(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
214 c->location = gtk_entry_new();
215 g_signal_connect(G_OBJECT(c->location), "key-press-event",
216 G_CALLBACK(key_location), c);
218 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
219 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
220 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
222 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
225 show_web_view(NULL, c);
227 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
228 G_CALLBACK(show_web_view), c);
232 f = ensure_uri_scheme(uri);
233 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
239 return WEBKIT_WEB_VIEW(c->web_view);
243 client_new_request(WebKitWebView *web_view,
244 WebKitNavigationAction *navigation_action, gpointer data)
246 return client_new(NULL, web_view, FALSE);
250 cooperation_setup(void)
253 gchar *fifofilename, *fifopath;
255 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
256 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
257 g_free(fifofilename);
259 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
260 mkfifo(fifopath, 0600);
262 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
263 if (!cooperative_pipe_fp)
265 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
269 if (write(cooperative_pipe_fp, "", 0) == -1)
271 /* Could not do an empty write to the FIFO which means there's
272 * no one listening. */
273 close(cooperative_pipe_fp);
274 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
275 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
278 cooperative_alone = FALSE;
285 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
287 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
288 WebKitURIResponse *resp;
289 GtkToolItem *tb = GTK_TOOL_ITEM(data);
292 gchar *t, *filename, *base;
294 p = webkit_download_get_estimated_progress(download);
298 resp = webkit_download_get_response(download);
299 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
301 uri = webkit_download_get_destination(download);
302 filename = g_filename_from_uri(uri, NULL, NULL);
303 if (filename == NULL)
305 /* This really should not happen because WebKit uses that URI to
306 * write to a file... */
307 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
308 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
309 webkit_uri_response_get_uri(resp), p, size_mb);
313 base = g_path_get_basename(filename);
314 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
318 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
323 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
325 struct Client *c = (struct Client *)data;
328 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
331 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
335 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
338 struct Client *c = (struct Client *)data;
340 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
341 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
343 u = u == NULL ? __NAME__ : u;
344 u = u[0] == 0 ? __NAME__ : u;
346 t = t == NULL ? u : t;
347 t = t[0] == 0 ? u : t;
349 gtk_window_set_title(GTK_WINDOW(c->win), t);
353 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
356 struct Client *c = (struct Client *)data;
359 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
361 /* When a web process crashes, we get a "notify::uri" signal, but we
362 * can no longer read a meaningful URI. It's just an empty string
363 * now. Not updating the location bar in this scenario is important,
364 * because we would override the "WEB PROCESS CRASHED" message. */
365 if (t != NULL && strlen(t) > 0)
367 gtk_entry_set_text(GTK_ENTRY(c->location), t);
369 if (history_file != NULL)
371 fp = fopen(history_file, "a");
374 fprintf(fp, "%s\n", t);
378 perror(__NAME__": Error opening history file");
384 crashed_web_view(WebKitWebView *web_view, gpointer data)
387 struct Client *c = (struct Client *)data;
389 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
390 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
391 gtk_entry_set_text(GTK_ENTRY(c->location), t);
398 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
399 WebKitPolicyDecisionType type, gpointer data)
401 WebKitResponsePolicyDecision *r;
405 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
406 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
407 if (!webkit_response_policy_decision_is_mime_type_supported(r))
408 webkit_policy_decision_download(decision);
410 webkit_policy_decision_use(decision);
413 /* Use whatever default there is. */
420 download_handle_finished(WebKitDownload *download, gpointer data)
426 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
429 g_signal_connect(G_OBJECT(download), "decide-destination",
430 G_CALLBACK(download_handle), data);
434 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
436 gchar *sug_clean, *path, *path2 = NULL, *uri;
441 sug_clean = g_strdup(suggested_filename);
442 for (i = 0; i < strlen(sug_clean); i++)
443 if (sug_clean[i] == G_DIR_SEPARATOR)
446 path = g_build_filename(download_dir, sug_clean, NULL);
447 path2 = g_strdup(path);
448 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
452 path2 = g_strdup_printf("%s.%d", path, suffix);
458 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
459 webkit_download_cancel(download);
463 uri = g_filename_to_uri(path2, NULL, NULL);
464 webkit_download_set_destination(download, uri);
467 tb = gtk_tool_button_new(NULL, NULL);
468 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
469 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
470 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
471 gtk_widget_show_all(dm.win);
473 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
474 G_CALLBACK(changed_download_progress), tb);
477 g_signal_connect(G_OBJECT(download), "finished",
478 G_CALLBACK(download_handle_finished), NULL);
480 g_object_ref(download);
481 g_signal_connect(G_OBJECT(tb), "clicked",
482 G_CALLBACK(downloadmanager_cancel), download);
489 /* Propagate -- to whom it may concern. */
494 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
496 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
498 webkit_download_cancel(download);
499 g_object_unref(download);
501 gtk_widget_destroy(GTK_WIDGET(tb));
505 downloadmanager_delete(GtkWidget *obj, gpointer data)
507 if (!quit_if_nothing_active())
508 gtk_widget_hide(dm.win);
514 downloadmanager_setup(void)
516 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
517 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
518 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
519 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
520 g_signal_connect(G_OBJECT(dm.win), "delete-event",
521 G_CALLBACK(downloadmanager_delete), NULL);
522 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
523 G_CALLBACK(key_downloadmanager), NULL);
525 dm.toolbar = gtk_toolbar_new();
526 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
527 GTK_ORIENTATION_VERTICAL);
528 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
529 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
531 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
532 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
533 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
534 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
536 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
540 ensure_uri_scheme(const gchar *t)
544 f = g_ascii_strdown(t, -1);
545 if (!g_str_has_prefix(f, "http:") &&
546 !g_str_has_prefix(f, "https:") &&
547 !g_str_has_prefix(f, "file:") &&
548 !g_str_has_prefix(f, "about:"))
551 fabs = realpath(t, NULL);
554 f = g_strdup_printf("file://%s", fabs);
558 f = g_strdup_printf("http://%s", t);
566 external_handler_run(gpointer data)
568 struct Client *c = (struct Client *)data;
569 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
573 argv[2] = c->external_handler_uri;
574 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
577 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
582 g_spawn_close_pid(pid);
586 grab_environment_configuration(void)
590 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
592 accepted_language[0] = g_strdup(e);
594 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
596 download_dir = g_strdup(e);
598 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
600 enable_console_to_stdout = TRUE;
602 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_EXPERIMENTAL_WEBGL");
606 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
608 fifo_suffix = g_strdup(e);
610 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
612 history_file = g_strdup(e);
614 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
616 home_uri = g_strdup(e);
618 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
620 user_agent = g_strdup(e);
622 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
624 global_zoom = atof(e);
628 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
631 struct Client *c = (struct Client *)data;
633 if (!gtk_widget_is_focus(c->location))
635 if (webkit_hit_test_result_context_is_link(ht))
637 gtk_entry_set_text(GTK_ENTRY(c->location),
638 webkit_hit_test_result_get_link_uri(ht));
640 if (c->hover_uri != NULL)
641 g_free(c->hover_uri);
642 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
646 gtk_entry_set_text(GTK_ENTRY(c->location),
647 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
649 if (c->hover_uri != NULL)
650 g_free(c->hover_uri);
657 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
659 struct Client *c = (struct Client *)data;
660 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
663 if (event->type == GDK_KEY_PRESS)
665 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
667 switch (((GdkEventKey *)event)->keyval)
669 case GDK_KEY_q: /* close window (left hand) */
670 gtk_widget_destroy(c->win);
672 case GDK_KEY_w: /* home (left hand) */
673 f = ensure_uri_scheme(home_uri);
674 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
677 case GDK_KEY_e: /* new tab (left hand) */
678 f = ensure_uri_scheme(home_uri);
679 client_new(f, NULL, TRUE);
682 case GDK_KEY_r: /* reload (left hand) */
683 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
686 case GDK_KEY_d: /* download manager (left hand) */
687 gtk_widget_show_all(dm.win);
689 case GDK_KEY_2: /* search forward (left hand) */
690 case GDK_KEY_n: /* search forward (maybe both hands) */
693 case GDK_KEY_3: /* search backward (left hand) */
696 case GDK_KEY_l: /* location (BOTH hands) */
697 gtk_widget_grab_focus(c->location);
699 case GDK_KEY_k: /* initiate search (BOTH hands) */
700 gtk_widget_grab_focus(c->location);
701 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
702 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
704 case GDK_KEY_c: /* reload trusted certs (left hand) */
705 trust_user_certs(wc);
707 case GDK_KEY_x: /* launch external handler (left hand) */
708 if (c->external_handler_uri != NULL)
709 g_free(c->external_handler_uri);
710 c->external_handler_uri = g_strdup(
711 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
712 external_handler_run(c);
716 /* navigate backward (left hand) */
717 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
719 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
722 /* navigate forward (left hand) */
723 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
725 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
734 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
736 if (event->type == GDK_KEY_PRESS)
738 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
740 switch (((GdkEventKey *)event)->keyval)
742 case GDK_KEY_d: /* close window (left hand) */
744 downloadmanager_delete(dm.win, NULL);
754 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
756 struct Client *c = (struct Client *)data;
760 if (key_common(widget, event, data))
763 if (event->type == GDK_KEY_PRESS)
765 switch (((GdkEventKey *)event)->keyval)
767 case GDK_KEY_KP_Enter:
769 gtk_widget_grab_focus(c->web_view);
770 t = gtk_entry_get_text(GTK_ENTRY(c->location));
771 if (t != NULL && t[0] == ':' && t[1] == '/')
773 if (search_text != NULL)
775 search_text = g_strdup(t + 2); /* XXX whacky */
778 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
780 f = ensure_uri_scheme(t);
781 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
786 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
787 gtk_entry_set_text(GTK_ENTRY(c->location),
788 (t == NULL ? __NAME__ : t));
797 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
799 struct Client *c = (struct Client *)data;
803 if (key_common(widget, event, data))
806 if (event->type == GDK_KEY_PRESS)
808 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
810 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
811 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
814 else if (event->type == GDK_BUTTON_PRESS)
816 switch (((GdkEventButton *)event)->button)
819 if (c->hover_uri != NULL)
821 client_new(c->hover_uri, NULL, TRUE);
826 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
829 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
833 else if (event->type == GDK_SCROLL)
835 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
836 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
838 gdk_event_get_scroll_deltas(event, &dx, &dy);
839 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
841 z = dx != 0 ? global_zoom : z;
842 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
854 GIOChannel *channel = NULL;
855 gchar *path = NULL, *buf = NULL;
856 gchar **tokens = NULL;
858 keywords = g_hash_table_new(g_str_hash, g_str_equal);
860 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
862 channel = g_io_channel_new_file(path, "r", &err);
865 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
866 == G_IO_STATUS_NORMAL)
871 tokens = g_strsplit(buf, " ", 2);
872 if (tokens[0] != NULL && tokens[1] != NULL)
873 g_hash_table_insert(keywords, g_strdup(tokens[0]),
874 g_strdup(tokens[1]));
879 g_io_channel_shutdown(channel, FALSE, NULL);
885 keywords_try_search(WebKitWebView *web_view, const gchar *t)
887 gboolean ret = FALSE;
888 gchar **tokens = NULL;
889 gchar *val = NULL, *escaped = NULL, *uri = NULL;
891 tokens = g_strsplit(t, " ", 2);
892 if (tokens[0] != NULL && tokens[1] != NULL)
894 val = g_hash_table_lookup(keywords, tokens[0]);
897 escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
898 uri = g_strdup_printf((gchar *)val, escaped);
899 webkit_web_view_load_uri(web_view, uri);
911 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
912 WebKitHitTestResult *ht, gpointer data)
914 struct Client *c = (struct Client *)data;
915 GSimpleAction *action = NULL;
916 WebKitContextMenuItem *mi = NULL;
917 const gchar *uri = NULL;
921 if (webkit_hit_test_result_context_is_link(ht))
922 uri = webkit_hit_test_result_get_link_uri(ht);
923 else if (webkit_hit_test_result_context_is_image(ht))
924 uri = webkit_hit_test_result_get_image_uri(ht);
925 else if (webkit_hit_test_result_context_is_media(ht))
926 uri = webkit_hit_test_result_get_media_uri(ht);
930 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
932 if (c->external_handler_uri != NULL)
933 g_free(c->external_handler_uri);
934 c->external_handler_uri = g_strdup(uri);
935 action = g_simple_action_new("external_handler", NULL);
936 g_signal_connect_swapped(G_OBJECT(action), "activate",
937 G_CALLBACK(external_handler_run), data);
938 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
939 "Open with external handler",
941 webkit_context_menu_append(menu, mi);
942 g_object_unref(action);
945 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
950 quit_if_nothing_active(void)
960 gtk_widget_show_all(dm.win);
967 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
971 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
975 client_new(uri, NULL, TRUE);
982 search(gpointer data, gint direction)
984 struct Client *c = (struct Client *)data;
985 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
986 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
988 if (search_text == NULL)
994 webkit_find_controller_search(fc, search_text,
995 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
996 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1000 webkit_find_controller_search_next(fc);
1003 webkit_find_controller_search_previous(fc);
1009 show_web_view(WebKitWebView *web_view, gpointer data)
1011 struct Client *c = (struct Client *)data;
1015 gtk_widget_grab_focus(c->web_view);
1016 gtk_widget_show_all(c->win);
1023 GIOChannel *tabbed_stdout_channel;
1025 gchar *output = NULL;
1026 char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
1029 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
1030 NULL, NULL, NULL, &tabbed_stdout, NULL,
1033 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
1038 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
1039 if (tabbed_stdout_channel == NULL)
1041 fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
1044 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
1045 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
1048 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
1052 plug_into = strtol(output, NULL, 16);
1055 fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
1060 trust_user_certs(WebKitWebContext *wc)
1062 GTlsCertificate *cert;
1063 const gchar *basedir, *file, *absfile;
1066 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1067 dir = g_dir_open(basedir, 0, NULL);
1070 file = g_dir_read_name(dir);
1071 while (file != NULL)
1073 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1075 cert = g_tls_certificate_new_from_file(absfile, NULL);
1077 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1079 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1080 file = g_dir_read_name(dir);
1088 main(int argc, char **argv)
1093 gtk_init(&argc, &argv);
1094 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1095 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1097 grab_environment_configuration();
1099 while ((opt = getopt(argc, argv, "e:CT")) != -1)
1104 embed = atol(optarg);
1105 tabbed_automagic = FALSE;
1108 cooperative_instances = FALSE;
1111 tabbed_automagic = FALSE;
1114 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1120 if (cooperative_instances)
1121 cooperation_setup();
1122 downloadmanager_setup();
1124 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1125 embed = tabbed_launch();
1127 if (!cooperative_instances || cooperative_alone)
1129 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1131 webkit_web_context_set_web_extensions_directory(
1132 webkit_web_context_get_default(), c
1137 client_new(home_uri, NULL, TRUE);
1140 for (i = optind; i < argc; i++)
1141 client_new(argv[i], NULL, TRUE);
1144 if (!cooperative_instances || cooperative_alone)