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 *, 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 crashed_web_view_reload(gpointer);
28 static gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
29 WebKitPolicyDecisionType, gpointer);
30 static gboolean download_handle(WebKitDownload *, gchar *, gpointer);
31 static void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
32 static void downloadmanager_cancel(GtkToolButton *, gpointer data);
33 static void downloadmanager_setup(void);
34 static gchar *ensure_uri_scheme(const gchar *);
35 static void external_handler_run(GtkAction *, 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 remote_msg(GIOChannel *, GIOCondition, gpointer);
47 static void search(gpointer, gint);
48 static void show_web_view(WebKitWebView *, gpointer);
49 static Window tabbed_launch(void);
50 static void trust_user_certs(WebKitWebContext *);
55 gchar *external_handler_uri;
65 struct DownloadManager
73 static const gchar *accepted_language[2] = { NULL, NULL };
74 static gint clients = 0;
75 static gboolean cooperative_alone = TRUE;
76 static gboolean cooperative_instances = TRUE;
77 static int cooperative_pipe_fp = 0;
78 static int crash_autoreload_delay = 2;
79 static gchar *download_dir = "/var/tmp";
80 static Window embed = 0;
81 static gchar *fifo_suffix = "main";
82 static gdouble global_zoom = 1.0;
83 static gchar *home_uri = "about:blank";
84 static gboolean initial_wc_setup_done = FALSE;
85 static GHashTable *keywords = NULL;
86 static gchar *search_text = NULL;
87 static gboolean tabbed_automagic = TRUE;
88 static gchar *user_agent = NULL;
92 client_destroy(GtkWidget *obj, gpointer data)
94 struct Client *c = (struct Client *)data;
96 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
97 changed_load_progress, c);
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, gboolean show)
120 WebKitWebContext *wc;
123 if (uri != NULL && cooperative_instances && !cooperative_alone)
125 write(cooperative_pipe_fp, uri, strlen(uri));
126 write(cooperative_pipe_fp, "\n", 1);
130 c = calloc(1, sizeof(struct Client));
133 fprintf(stderr, __NAME__": fatal: calloc failed\n");
139 c->win = gtk_plug_new(embed);
140 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
142 fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
143 gtk_widget_destroy(c->win);
150 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
152 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
154 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
155 gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
157 c->web_view = webkit_web_view_new();
158 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
160 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
161 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
162 G_CALLBACK(changed_title), c);
163 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
164 G_CALLBACK(changed_uri), c);
165 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
166 G_CALLBACK(changed_load_progress), c);
167 g_signal_connect(G_OBJECT(c->web_view), "create",
168 G_CALLBACK(client_new_request), NULL);
169 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
170 G_CALLBACK(menu_web_view), c);
171 g_signal_connect(G_OBJECT(c->web_view), "close",
172 G_CALLBACK(client_destroy_request), c);
173 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
174 G_CALLBACK(decide_policy), NULL);
175 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
176 G_CALLBACK(key_web_view), c);
177 g_signal_connect(G_OBJECT(c->web_view), "button-press-event",
178 G_CALLBACK(key_web_view), c);
179 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
180 G_CALLBACK(key_web_view), c);
181 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
182 G_CALLBACK(hover_web_view), c);
183 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
184 G_CALLBACK(crashed_web_view), c);
186 if (!initial_wc_setup_done)
188 if (accepted_language[0] != NULL)
189 webkit_web_context_set_preferred_languages(wc, accepted_language);
191 g_signal_connect(G_OBJECT(wc), "download-started",
192 G_CALLBACK(download_handle_start), NULL);
194 trust_user_certs(wc);
196 initial_wc_setup_done = TRUE;
199 if (user_agent != NULL)
200 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
201 "user-agent", user_agent, NULL);
203 c->location = gtk_entry_new();
204 g_signal_connect(G_OBJECT(c->location), "key-press-event",
205 G_CALLBACK(key_location), c);
207 /* XXX Progress bars don't work/look as intended anymore. Level bars
208 * are a dirty workaround (kind of). */
209 c->progress = gtk_level_bar_new();
210 gtk_level_bar_set_value(GTK_LEVEL_BAR(c->progress), 1);
212 c->top_box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
213 gtk_box_pack_start(GTK_BOX(c->top_box), c->location, TRUE, TRUE, 0);
214 gtk_box_pack_start(GTK_BOX(c->top_box), c->progress, FALSE, FALSE, 0);
216 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
217 gtk_box_pack_start(GTK_BOX(c->vbox), c->top_box, FALSE, FALSE, 0);
218 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
220 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
223 show_web_view(NULL, c);
225 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
226 G_CALLBACK(show_web_view), c);
230 f = ensure_uri_scheme(uri);
231 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
237 return WEBKIT_WEB_VIEW(c->web_view);
241 client_new_request(WebKitWebView *web_view,
242 WebKitNavigationAction *navigation_action, gpointer data)
244 return client_new(NULL, FALSE);
248 cooperation_setup(void)
251 gchar *fifofilename, *fifopath;
253 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
254 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
255 g_free(fifofilename);
257 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
258 mkfifo(fifopath, 0600);
260 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
261 if (!cooperative_pipe_fp)
263 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
267 if (write(cooperative_pipe_fp, "", 0) == -1)
269 /* Could not do an empty write to the FIFO which means there's
270 * no one listening. */
271 close(cooperative_pipe_fp);
272 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
273 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
276 cooperative_alone = FALSE;
283 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
285 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
286 WebKitURIResponse *resp;
287 GtkToolItem *tb = GTK_TOOL_ITEM(data);
290 gchar *t, *filename, *base;
292 p = webkit_download_get_estimated_progress(download);
296 resp = webkit_download_get_response(download);
297 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
299 uri = webkit_download_get_destination(download);
300 filename = g_filename_from_uri(uri, NULL, NULL);
301 if (filename == NULL)
303 /* This really should not happen because WebKit uses that URI to
304 * write to a file... */
305 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
306 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
307 webkit_uri_response_get_uri(resp), p, size_mb);
311 base = g_path_get_basename(filename);
312 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
316 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
321 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
323 struct Client *c = (struct Client *)data;
326 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
327 gtk_level_bar_set_value(GTK_LEVEL_BAR(c->progress), p);
331 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
334 struct Client *c = (struct Client *)data;
336 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
337 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
339 u = u == NULL ? __NAME__ : u;
340 u = u[0] == 0 ? __NAME__ : u;
342 t = t == NULL ? u : t;
343 t = t[0] == 0 ? u : t;
345 gtk_window_set_title(GTK_WINDOW(c->win), t);
349 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
352 struct Client *c = (struct Client *)data;
354 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
355 gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? __NAME__ : t));
359 crashed_web_view(WebKitWebView *web_view, gpointer data)
361 fprintf(stderr, __NAME__": WebView crashed!\n");
362 if (crash_autoreload_delay >= 1)
364 fprintf(stderr, __NAME__": Reloading WebView in %d seconds.\n",
365 crash_autoreload_delay);
366 g_timeout_add_seconds(crash_autoreload_delay, crashed_web_view_reload,
374 crashed_web_view_reload(gpointer data)
376 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(data));
378 return G_SOURCE_REMOVE;
382 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
383 WebKitPolicyDecisionType type, gpointer data)
385 WebKitResponsePolicyDecision *r;
389 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
390 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
391 if (!webkit_response_policy_decision_is_mime_type_supported(r))
392 webkit_policy_decision_download(decision);
394 webkit_policy_decision_use(decision);
397 /* Use whatever default there is. */
404 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
407 g_signal_connect(G_OBJECT(download), "decide-destination",
408 G_CALLBACK(download_handle), data);
412 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
414 gchar *sug_clean, *path, *path2 = NULL, *uri;
419 sug_clean = g_strdup(suggested_filename);
420 for (i = 0; i < strlen(sug_clean); i++)
421 if (sug_clean[i] == G_DIR_SEPARATOR)
424 path = g_build_filename(download_dir, sug_clean, NULL);
425 path2 = g_strdup(path);
426 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
430 path2 = g_strdup_printf("%s.%d", path, suffix);
436 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
437 webkit_download_cancel(download);
441 uri = g_filename_to_uri(path2, NULL, NULL);
442 webkit_download_set_destination(download, uri);
445 tb = gtk_tool_button_new(NULL, NULL);
446 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
447 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
448 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
449 gtk_widget_show_all(dm.win);
451 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
452 G_CALLBACK(changed_download_progress), tb);
454 g_object_ref(download);
455 g_signal_connect(G_OBJECT(tb), "clicked",
456 G_CALLBACK(downloadmanager_cancel), download);
463 /* Propagate -- to whom it may concern. */
468 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
470 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
472 webkit_download_cancel(download);
473 g_object_unref(download);
475 gtk_widget_destroy(GTK_WIDGET(tb));
479 downloadmanager_setup(void)
481 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
482 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
483 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
484 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
485 g_signal_connect(G_OBJECT(dm.win), "delete-event",
486 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
487 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
488 G_CALLBACK(key_downloadmanager), NULL);
490 dm.toolbar = gtk_toolbar_new();
491 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
492 GTK_ORIENTATION_VERTICAL);
493 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
494 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
496 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
497 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
498 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
499 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
501 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
505 ensure_uri_scheme(const gchar *t)
509 f = g_ascii_strdown(t, -1);
510 if (!g_str_has_prefix(f, "http:") &&
511 !g_str_has_prefix(f, "https:") &&
512 !g_str_has_prefix(f, "file:") &&
513 !g_str_has_prefix(f, "about:"))
516 fabs = realpath(t, NULL);
519 f = g_strdup_printf("file://%s", fabs);
523 f = g_strdup_printf("http://%s", t);
531 external_handler_run(GtkAction *action, gpointer data)
533 struct Client *c = (struct Client *)data;
534 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
540 argv[2] = c->external_handler_uri;
541 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
544 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
549 g_spawn_close_pid(pid);
553 grab_environment_configuration(void)
557 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
559 accepted_language[0] = g_strdup(e);
561 e = g_getenv(__NAME_UPPERCASE__"_CRASH_AUTORELOAD_DELAY");
563 crash_autoreload_delay = atoi(e);
565 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
567 download_dir = g_strdup(e);
569 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
571 fifo_suffix = g_strdup(e);
573 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
575 home_uri = g_strdup(e);
577 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
579 user_agent = g_strdup(e);
581 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
583 global_zoom = atof(e);
587 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
590 struct Client *c = (struct Client *)data;
592 if (!gtk_widget_is_focus(c->location))
594 if (webkit_hit_test_result_context_is_link(ht))
596 gtk_entry_set_text(GTK_ENTRY(c->location),
597 webkit_hit_test_result_get_link_uri(ht));
599 if (c->hover_uri != NULL)
600 g_free(c->hover_uri);
601 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
605 gtk_entry_set_text(GTK_ENTRY(c->location),
606 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
608 if (c->hover_uri != NULL)
609 g_free(c->hover_uri);
616 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
618 struct Client *c = (struct Client *)data;
619 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
622 if (event->type == GDK_KEY_PRESS)
624 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
626 switch (((GdkEventKey *)event)->keyval)
628 case GDK_KEY_q: /* close window (left hand) */
629 gtk_widget_destroy(c->win);
631 case GDK_KEY_w: /* home (left hand) */
632 f = ensure_uri_scheme(home_uri);
633 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
636 case GDK_KEY_e: /* new tab (left hand) */
637 f = ensure_uri_scheme(home_uri);
641 case GDK_KEY_r: /* reload (left hand) */
642 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
645 case GDK_KEY_d: /* download manager (left hand) */
646 gtk_widget_show_all(dm.win);
648 case GDK_KEY_2: /* search forward (left hand) */
649 case GDK_KEY_n: /* search forward (maybe both hands) */
652 case GDK_KEY_3: /* search backward (left hand) */
655 case GDK_KEY_l: /* location (BOTH hands) */
656 gtk_widget_grab_focus(c->location);
658 case GDK_KEY_k: /* initiate search (BOTH hands) */
659 gtk_widget_grab_focus(c->location);
660 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
661 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
663 case GDK_KEY_c: /* reload trusted certs (left hand) */
664 trust_user_certs(wc);
666 case GDK_KEY_x: /* launch external handler (left hand) */
667 if (c->external_handler_uri != NULL)
668 g_free(c->external_handler_uri);
669 c->external_handler_uri = g_strdup(
670 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
671 external_handler_run(NULL, c);
675 /* navigate backward (left hand) */
676 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
678 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
681 /* navigate forward (left hand) */
682 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
684 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
693 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
695 if (event->type == GDK_KEY_PRESS)
697 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
699 switch (((GdkEventKey *)event)->keyval)
701 case GDK_KEY_d: /* close window (left hand) */
702 gtk_widget_hide(dm.win);
712 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
714 struct Client *c = (struct Client *)data;
718 if (key_common(widget, event, data))
721 if (event->type == GDK_KEY_PRESS)
723 switch (((GdkEventKey *)event)->keyval)
725 case GDK_KEY_KP_Enter:
727 gtk_widget_grab_focus(c->web_view);
728 t = gtk_entry_get_text(GTK_ENTRY(c->location));
729 if (t != NULL && t[0] == ':' && t[1] == '/')
731 if (search_text != NULL)
733 search_text = g_strdup(t + 2); /* XXX whacky */
736 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
738 f = ensure_uri_scheme(t);
739 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
744 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
745 gtk_entry_set_text(GTK_ENTRY(c->location),
746 (t == NULL ? __NAME__ : t));
755 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
757 struct Client *c = (struct Client *)data;
761 if (key_common(widget, event, data))
764 if (event->type == GDK_KEY_PRESS)
766 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
768 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
769 gtk_level_bar_set_value(GTK_LEVEL_BAR(c->progress), 0);
772 else if (event->type == GDK_BUTTON_PRESS)
774 switch (((GdkEventButton *)event)->button)
777 if (c->hover_uri != NULL)
779 client_new(c->hover_uri, TRUE);
784 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
787 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
791 else if (event->type == GDK_SCROLL)
793 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
794 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
796 gdk_event_get_scroll_deltas(event, &dx, &dy);
797 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
799 z = dx != 0 ? global_zoom : z;
800 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
812 GIOChannel *channel = NULL;
813 gchar *path = NULL, *buf = NULL;
814 gchar **tokens = NULL;
816 keywords = g_hash_table_new(g_str_hash, g_str_equal);
818 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
820 channel = g_io_channel_new_file(path, "r", &err);
823 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
824 == G_IO_STATUS_NORMAL)
829 tokens = g_strsplit(buf, " ", 2);
830 if (tokens[0] != NULL && tokens[1] != NULL)
831 g_hash_table_insert(keywords, g_strdup(tokens[0]),
832 g_strdup(tokens[1]));
837 g_io_channel_shutdown(channel, FALSE, NULL);
843 keywords_try_search(WebKitWebView *web_view, const gchar *t)
845 gboolean ret = FALSE;
846 gchar **tokens = NULL;
847 gchar *val = NULL, *uri = NULL;
849 tokens = g_strsplit(t, " ", 2);
850 if (tokens[0] != NULL && tokens[1] != NULL)
852 val = g_hash_table_lookup(keywords, tokens[0]);
855 uri = g_strdup_printf((gchar *)val, tokens[1]);
856 webkit_web_view_load_uri(web_view, uri);
867 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
868 WebKitHitTestResult *ht, gpointer data)
870 struct Client *c = (struct Client *)data;
871 GtkAction *action = NULL;
872 WebKitContextMenuItem *mi = NULL;
873 const gchar *uri = NULL;
877 if (webkit_hit_test_result_context_is_link(ht))
878 uri = webkit_hit_test_result_get_link_uri(ht);
879 else if (webkit_hit_test_result_context_is_image(ht))
880 uri = webkit_hit_test_result_get_image_uri(ht);
881 else if (webkit_hit_test_result_context_is_media(ht))
882 uri = webkit_hit_test_result_get_media_uri(ht);
886 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
888 if (c->external_handler_uri != NULL)
889 g_free(c->external_handler_uri);
890 c->external_handler_uri = g_strdup(uri);
891 action = gtk_action_new("external_handler", "Open with external handler",
893 g_signal_connect(G_OBJECT(action), "activate",
894 G_CALLBACK(external_handler_run), data);
895 mi = webkit_context_menu_item_new(action);
896 webkit_context_menu_append(menu, mi);
899 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
904 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
908 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
912 client_new(uri, TRUE);
919 search(gpointer data, gint direction)
921 struct Client *c = (struct Client *)data;
922 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
923 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
925 if (search_text == NULL)
931 webkit_find_controller_search(fc, search_text,
932 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
933 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
937 webkit_find_controller_search_next(fc);
940 webkit_find_controller_search_previous(fc);
946 show_web_view(WebKitWebView *web_view, gpointer data)
948 struct Client *c = (struct Client *)data;
952 gtk_widget_grab_focus(c->web_view);
953 gtk_widget_show_all(c->win);
960 GIOChannel *tabbed_stdout_channel;
962 gchar *output = NULL;
963 char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
966 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
967 NULL, NULL, NULL, &tabbed_stdout, NULL,
970 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
975 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
976 if (tabbed_stdout_channel == NULL)
978 fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
981 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
982 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
985 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
989 plug_into = strtol(output, NULL, 16);
992 fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
997 trust_user_certs(WebKitWebContext *wc)
999 GTlsCertificate *cert;
1000 const gchar *basedir, *file, *absfile;
1003 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1004 dir = g_dir_open(basedir, 0, NULL);
1007 file = g_dir_read_name(dir);
1008 while (file != NULL)
1010 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1012 cert = g_tls_certificate_new_from_file(absfile, NULL);
1014 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1016 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1017 file = g_dir_read_name(dir);
1025 main(int argc, char **argv)
1030 gtk_init(&argc, &argv);
1032 grab_environment_configuration();
1034 while ((opt = getopt(argc, argv, "e:CT")) != -1)
1039 embed = atol(optarg);
1040 tabbed_automagic = FALSE;
1043 cooperative_instances = FALSE;
1046 tabbed_automagic = FALSE;
1049 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1055 if (cooperative_instances)
1056 cooperation_setup();
1057 downloadmanager_setup();
1059 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1060 embed = tabbed_launch();
1062 if (!cooperative_instances || cooperative_alone)
1064 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1066 webkit_web_context_set_web_extensions_directory(
1067 webkit_web_context_get_default(), c
1072 client_new(home_uri, TRUE);
1075 for (i = optind; i < argc; i++)
1076 client_new(argv[i], TRUE);
1079 if (!cooperative_instances || cooperative_alone)