10 #include <gdk/gdkkeysyms.h>
12 #include <webkit2/webkit2.h>
15 static void client_destroy(GtkWidget *, gpointer);
16 static gboolean client_destroy_request(WebKitWebView *, gpointer);
17 static WebKitWebView *client_new(const gchar *);
18 static WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
20 static void cooperation_setup(void);
21 static void changed_download_progress(GObject *, GParamSpec *, gpointer);
22 static void changed_load_progress(GObject *, GParamSpec *, gpointer);
23 static void changed_title(GObject *, GParamSpec *, gpointer);
24 static void changed_uri(GObject *, GParamSpec *, gpointer);
25 static gboolean crashed_web_view(WebKitWebView *, gpointer);
26 static gboolean crashed_web_view_reload(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 void downloadmanager_setup(void);
33 static gchar *ensure_uri_scheme(const gchar *);
34 static void external_handler_run(GtkAction *, gpointer);
35 static void grab_environment_configuration(void);
36 static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
37 static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
38 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
39 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
40 static void keywords_load(void);
41 static gboolean keywords_try_search(WebKitWebView *, const gchar *);
42 static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
43 WebKitHitTestResult *, gpointer);
44 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
45 static void search(gpointer, gint);
46 static Window tabbed_launch(void);
47 static void trust_user_certs(WebKitWebContext *);
52 gchar *external_handler_uri;
62 struct DownloadManager
70 static const gchar *accepted_language[2] = { NULL, NULL };
71 static gint clients = 0;
72 static gboolean cooperative_alone = TRUE;
73 static gboolean cooperative_instances = TRUE;
74 static int cooperative_pipe_fp = 0;
75 static int crash_autoreload_delay = 2;
76 static gchar *download_dir = "/var/tmp";
77 static Window embed = 0;
78 static gchar *fifo_suffix = "main";
79 static gdouble global_zoom = 1.0;
80 static gchar *home_uri = "about:blank";
81 static gboolean initial_wc_setup_done = FALSE;
82 static GHashTable *keywords = NULL;
83 static gchar *search_text = NULL;
84 static gboolean tabbed_automagic = TRUE;
85 static gchar *user_agent = NULL;
89 client_destroy(GtkWidget *obj, gpointer data)
91 struct Client *c = (struct Client *)data;
93 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
94 changed_load_progress, c);
104 client_destroy_request(WebKitWebView *web_view, gpointer data)
106 struct Client *c = (struct Client *)data;
108 gtk_widget_destroy(c->win);
114 client_new(const gchar *uri)
117 WebKitWebContext *wc;
120 if (uri != NULL && cooperative_instances && !cooperative_alone)
122 write(cooperative_pipe_fp, uri, strlen(uri));
123 write(cooperative_pipe_fp, "\n", 1);
127 c = malloc(sizeof(struct Client));
130 fprintf(stderr, __NAME__": fatal: malloc failed\n");
134 c->external_handler_uri = NULL;
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);
222 gtk_widget_grab_focus(c->web_view);
223 gtk_widget_show_all(c->win);
227 f = ensure_uri_scheme(uri);
228 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
234 return WEBKIT_WEB_VIEW(c->web_view);
238 client_new_request(WebKitWebView *web_view,
239 WebKitNavigationAction *navigation_action, gpointer data)
241 return client_new(NULL);
245 cooperation_setup(void)
248 gchar *fifofilename, *fifopath;
250 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
251 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
252 g_free(fifofilename);
254 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
255 mkfifo(fifopath, 0600);
257 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
258 if (!cooperative_pipe_fp)
260 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
264 if (write(cooperative_pipe_fp, "", 0) == -1)
266 /* Could not do an empty write to the FIFO which means there's
267 * no one listening. */
268 close(cooperative_pipe_fp);
269 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
270 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
273 cooperative_alone = FALSE;
280 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
282 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
283 WebKitURIResponse *resp;
284 GtkToolItem *tb = GTK_TOOL_ITEM(data);
287 gchar *t, *filename, *base;
289 p = webkit_download_get_estimated_progress(download);
293 resp = webkit_download_get_response(download);
294 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
296 uri = webkit_download_get_destination(download);
297 filename = g_filename_from_uri(uri, NULL, NULL);
298 if (filename == NULL)
300 /* This really should not happen because WebKit uses that URI to
301 * write to a file... */
302 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
303 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
304 webkit_uri_response_get_uri(resp), p, size_mb);
308 base = g_path_get_basename(filename);
309 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
313 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
318 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
320 struct Client *c = (struct Client *)data;
323 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
324 gtk_level_bar_set_value(GTK_LEVEL_BAR(c->progress), p);
328 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
331 struct Client *c = (struct Client *)data;
333 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
334 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
336 u = u == NULL ? __NAME__ : u;
337 u = u[0] == 0 ? __NAME__ : u;
339 t = t == NULL ? u : t;
340 t = t[0] == 0 ? u : t;
342 gtk_window_set_title(GTK_WINDOW(c->win), t);
346 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
349 struct Client *c = (struct Client *)data;
351 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
352 gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? __NAME__ : t));
356 crashed_web_view(WebKitWebView *web_view, gpointer data)
358 fprintf(stderr, __NAME__": WebView crashed!\n");
359 if (crash_autoreload_delay >= 1)
361 fprintf(stderr, __NAME__": Reloading WebView in %d seconds.\n",
362 crash_autoreload_delay);
363 g_timeout_add_seconds(crash_autoreload_delay, crashed_web_view_reload,
371 crashed_web_view_reload(gpointer data)
373 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(data));
375 return G_SOURCE_REMOVE;
379 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
380 WebKitPolicyDecisionType type, gpointer data)
382 WebKitResponsePolicyDecision *r;
386 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
387 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
388 if (!webkit_response_policy_decision_is_mime_type_supported(r))
389 webkit_policy_decision_download(decision);
391 webkit_policy_decision_use(decision);
394 /* Use whatever default there is. */
401 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
404 g_signal_connect(G_OBJECT(download), "decide-destination",
405 G_CALLBACK(download_handle), data);
409 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
411 gchar *sug_clean, *path, *path2 = NULL, *uri;
416 sug_clean = g_strdup(suggested_filename);
417 for (i = 0; i < strlen(sug_clean); i++)
418 if (sug_clean[i] == G_DIR_SEPARATOR)
421 path = g_build_filename(download_dir, sug_clean, NULL);
422 path2 = g_strdup(path);
423 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
427 path2 = g_strdup_printf("%s.%d", path, suffix);
433 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
434 webkit_download_cancel(download);
438 uri = g_filename_to_uri(path2, NULL, NULL);
439 webkit_download_set_destination(download, uri);
442 tb = gtk_tool_button_new(NULL, NULL);
443 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
444 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
445 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
446 gtk_widget_show_all(dm.win);
448 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
449 G_CALLBACK(changed_download_progress), tb);
451 g_object_ref(download);
452 g_signal_connect(G_OBJECT(tb), "clicked",
453 G_CALLBACK(downloadmanager_cancel), download);
460 /* Propagate -- to whom it may concern. */
465 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
467 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
469 webkit_download_cancel(download);
470 g_object_unref(download);
472 gtk_widget_destroy(GTK_WIDGET(tb));
476 downloadmanager_setup(void)
478 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
479 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
480 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
481 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
482 g_signal_connect(G_OBJECT(dm.win), "delete-event",
483 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
484 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
485 G_CALLBACK(key_downloadmanager), NULL);
487 dm.toolbar = gtk_toolbar_new();
488 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
489 GTK_ORIENTATION_VERTICAL);
490 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
491 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
493 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
494 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
495 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
496 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
498 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
502 ensure_uri_scheme(const gchar *t)
506 f = g_ascii_strdown(t, -1);
507 if (!g_str_has_prefix(f, "http:") &&
508 !g_str_has_prefix(f, "https:") &&
509 !g_str_has_prefix(f, "file:") &&
510 !g_str_has_prefix(f, "about:"))
513 f = g_strdup_printf("http://%s", t);
521 external_handler_run(GtkAction *action, gpointer data)
523 struct Client *c = (struct Client *)data;
524 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
530 argv[2] = c->external_handler_uri;
531 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
534 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
539 g_spawn_close_pid(pid);
543 grab_environment_configuration(void)
547 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
549 accepted_language[0] = g_strdup(e);
551 e = g_getenv(__NAME_UPPERCASE__"_CRASH_AUTORELOAD_DELAY");
553 crash_autoreload_delay = atoi(e);
555 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
557 download_dir = g_strdup(e);
559 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
561 fifo_suffix = g_strdup(e);
563 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
565 home_uri = g_strdup(e);
567 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
569 user_agent = g_strdup(e);
571 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
573 global_zoom = atof(e);
577 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
580 struct Client *c = (struct Client *)data;
582 if (!gtk_widget_is_focus(c->location))
584 if (webkit_hit_test_result_context_is_link(ht))
586 gtk_entry_set_text(GTK_ENTRY(c->location),
587 webkit_hit_test_result_get_link_uri(ht));
589 if (c->hover_uri != NULL)
590 g_free(c->hover_uri);
591 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
595 gtk_entry_set_text(GTK_ENTRY(c->location),
596 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
598 if (c->hover_uri != NULL)
599 g_free(c->hover_uri);
606 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
608 if (event->type == GDK_KEY_PRESS)
610 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
612 switch (((GdkEventKey *)event)->keyval)
614 case GDK_KEY_d: /* close window (left hand) */
615 gtk_widget_hide(dm.win);
625 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
627 struct Client *c = (struct Client *)data;
630 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
632 if (event->type == GDK_KEY_PRESS)
634 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
636 switch (((GdkEventKey *)event)->keyval)
638 case GDK_KEY_q: /* close window (left hand) */
639 gtk_widget_destroy(c->win);
641 case GDK_KEY_d: /* download manager (left hand) */
642 gtk_widget_show_all(dm.win);
644 case GDK_KEY_r: /* reload (left hand) */
645 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
648 case GDK_KEY_k: /* initiate search (BOTH hands) */
649 gtk_entry_set_text(GTK_ENTRY(c->location), "/");
650 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
652 case GDK_KEY_c: /* reload trusted certs (left hand) */
653 trust_user_certs(wc);
655 case GDK_KEY_x: /* launch external handler (left hand) */
656 if (c->external_handler_uri != NULL)
657 g_free(c->external_handler_uri);
658 c->external_handler_uri = g_strdup(
659 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
660 external_handler_run(NULL, c);
666 switch (((GdkEventKey *)event)->keyval)
668 case GDK_KEY_KP_Enter:
670 gtk_widget_grab_focus(c->web_view);
671 t = gtk_entry_get_text(GTK_ENTRY(c->location));
672 if (t != NULL && t[0] == '/')
674 if (search_text != NULL)
676 search_text = g_strdup(t + 1); /* XXX whacky */
679 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
681 f = ensure_uri_scheme(t);
682 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
687 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
688 gtk_entry_set_text(GTK_ENTRY(c->location),
689 (t == NULL ? __NAME__ : t));
699 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
701 struct Client *c = (struct Client *)data;
705 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
707 if (event->type == GDK_KEY_PRESS)
709 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
711 switch (((GdkEventKey *)event)->keyval)
713 case GDK_KEY_q: /* close window (left hand) */
714 gtk_widget_destroy(c->win);
716 case GDK_KEY_w: /* home (left hand) */
717 f = ensure_uri_scheme(home_uri);
718 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
721 case GDK_KEY_e: /* new tab (left hand) */
722 f = ensure_uri_scheme(home_uri);
726 case GDK_KEY_r: /* reload (left hand) */
727 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
730 case GDK_KEY_d: /* download manager (left hand) */
731 gtk_widget_show_all(dm.win);
733 case GDK_KEY_2: /* search forward (left hand) */
734 case GDK_KEY_n: /* search forward (maybe both hands) */
737 case GDK_KEY_3: /* search backward (left hand) */
740 case GDK_KEY_l: /* location (BOTH hands) */
741 gtk_widget_grab_focus(c->location);
743 case GDK_KEY_k: /* initiate search (BOTH hands) */
744 gtk_widget_grab_focus(c->location);
745 gtk_entry_set_text(GTK_ENTRY(c->location), "/");
746 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
748 case GDK_KEY_c: /* reload trusted certs (left hand) */
749 trust_user_certs(wc);
751 case GDK_KEY_x: /* launch external handler (left hand) */
752 if (c->external_handler_uri != NULL)
753 g_free(c->external_handler_uri);
754 c->external_handler_uri = g_strdup(
755 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
756 external_handler_run(NULL, c);
760 /* navigate backward (left hand) */
761 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
763 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
766 /* navigate forward (left hand) */
767 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
769 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
772 else if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
774 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
775 gtk_level_bar_set_value(GTK_LEVEL_BAR(c->progress), 0);
778 else if (event->type == GDK_BUTTON_PRESS)
780 switch (((GdkEventButton *)event)->button)
783 if (c->hover_uri != NULL)
785 client_new(c->hover_uri);
790 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
793 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
797 else if (event->type == GDK_SCROLL)
799 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
800 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
802 gdk_event_get_scroll_deltas(event, &dx, &dy);
803 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
805 z = dx != 0 ? global_zoom : z;
806 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
818 GIOChannel *channel = NULL;
819 gchar *path = NULL, *buf = NULL;
820 gchar **tokens = NULL;
822 keywords = g_hash_table_new(g_str_hash, g_str_equal);
824 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
826 channel = g_io_channel_new_file(path, "r", &err);
829 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
830 == G_IO_STATUS_NORMAL)
835 tokens = g_strsplit(buf, " ", 2);
836 if (tokens[0] != NULL && tokens[1] != NULL)
837 g_hash_table_insert(keywords, g_strdup(tokens[0]),
838 g_strdup(tokens[1]));
843 g_io_channel_shutdown(channel, FALSE, NULL);
849 keywords_try_search(WebKitWebView *web_view, const gchar *t)
851 gboolean ret = FALSE;
852 gchar **tokens = NULL;
853 gchar *val = NULL, *uri = NULL;
855 tokens = g_strsplit(t, " ", 2);
856 if (tokens[0] != NULL && tokens[1] != NULL)
858 val = g_hash_table_lookup(keywords, tokens[0]);
861 uri = g_strdup_printf((gchar *)val, tokens[1]);
862 webkit_web_view_load_uri(web_view, uri);
873 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
874 WebKitHitTestResult *ht, gpointer data)
876 struct Client *c = (struct Client *)data;
877 GtkAction *action = NULL;
878 WebKitContextMenuItem *mi = NULL;
879 const gchar *uri = NULL;
883 if (webkit_hit_test_result_context_is_link(ht))
884 uri = webkit_hit_test_result_get_link_uri(ht);
885 else if (webkit_hit_test_result_context_is_image(ht))
886 uri = webkit_hit_test_result_get_image_uri(ht);
887 else if (webkit_hit_test_result_context_is_media(ht))
888 uri = webkit_hit_test_result_get_media_uri(ht);
892 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
894 if (c->external_handler_uri != NULL)
895 g_free(c->external_handler_uri);
896 c->external_handler_uri = g_strdup(uri);
897 action = gtk_action_new("external_handler", "Open with external handler",
899 g_signal_connect(G_OBJECT(action), "activate",
900 G_CALLBACK(external_handler_run), data);
901 mi = webkit_context_menu_item_new(action);
902 webkit_context_menu_append(menu, mi);
905 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
910 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
914 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
925 search(gpointer data, gint direction)
927 struct Client *c = (struct Client *)data;
928 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
929 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
931 if (search_text == NULL)
937 webkit_find_controller_search(fc, search_text,
938 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
939 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
943 webkit_find_controller_search_next(fc);
946 webkit_find_controller_search_previous(fc);
955 GIOChannel *tabbed_stdout_channel;
957 gchar *output = NULL;
958 char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
961 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
962 NULL, NULL, NULL, &tabbed_stdout, NULL,
965 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
970 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
971 if (tabbed_stdout_channel == NULL)
973 fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
976 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
977 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
980 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
984 plug_into = strtol(output, NULL, 16);
987 fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
992 trust_user_certs(WebKitWebContext *wc)
994 GTlsCertificate *cert;
995 const gchar *basedir, *file, *absfile;
998 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
999 dir = g_dir_open(basedir, 0, NULL);
1002 file = g_dir_read_name(dir);
1003 while (file != NULL)
1005 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1007 cert = g_tls_certificate_new_from_file(absfile, NULL);
1009 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1011 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1012 file = g_dir_read_name(dir);
1020 main(int argc, char **argv)
1025 gtk_init(&argc, &argv);
1027 grab_environment_configuration();
1029 while ((opt = getopt(argc, argv, "e:CT")) != -1)
1034 embed = atol(optarg);
1035 tabbed_automagic = FALSE;
1038 cooperative_instances = FALSE;
1041 tabbed_automagic = FALSE;
1044 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1050 if (cooperative_instances)
1051 cooperation_setup();
1052 downloadmanager_setup();
1054 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1055 embed = tabbed_launch();
1057 if (!cooperative_instances || cooperative_alone)
1059 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1061 webkit_web_context_set_web_extensions_directory(
1062 webkit_web_context_get_default(), c
1067 client_new(home_uri);
1070 for (i = optind; i < argc; i++)
1071 client_new(argv[i]);
1074 if (!cooperative_instances || cooperative_alone)