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(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 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 Window embed = 0;
80 static gchar *fifo_suffix = "main";
81 static gdouble global_zoom = 1.0;
82 static gchar *history_file = NULL;
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 *widget, 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);
102 quit_if_nothing_active();
106 client_destroy_request(WebKitWebView *web_view, gpointer data)
108 struct Client *c = (struct Client *)data;
110 gtk_widget_destroy(c->win);
116 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
119 WebKitWebContext *wc;
122 if (uri != NULL && cooperative_instances && !cooperative_alone)
124 f = ensure_uri_scheme(uri);
125 write(cooperative_pipe_fp, f, strlen(f));
126 write(cooperative_pipe_fp, "\n", 1);
131 c = calloc(1, sizeof(struct Client));
134 fprintf(stderr, __NAME__": fatal: calloc failed\n");
140 c->win = gtk_plug_new(embed);
141 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
143 fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
144 gtk_widget_destroy(c->win);
151 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
153 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
155 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
156 gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
158 if (related_wv == NULL)
159 c->web_view = webkit_web_view_new();
161 c->web_view = webkit_web_view_new_with_related_view(related_wv);
162 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
164 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
165 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
166 G_CALLBACK(changed_title), c);
167 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
168 G_CALLBACK(changed_uri), c);
169 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
170 G_CALLBACK(changed_load_progress), c);
171 g_signal_connect(G_OBJECT(c->web_view), "create",
172 G_CALLBACK(client_new_request), NULL);
173 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
174 G_CALLBACK(menu_web_view), c);
175 g_signal_connect(G_OBJECT(c->web_view), "close",
176 G_CALLBACK(client_destroy_request), c);
177 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
178 G_CALLBACK(decide_policy), NULL);
179 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
180 G_CALLBACK(key_web_view), c);
181 g_signal_connect(G_OBJECT(c->web_view), "button-press-event",
182 G_CALLBACK(key_web_view), c);
183 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
184 G_CALLBACK(key_web_view), c);
185 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
186 G_CALLBACK(hover_web_view), c);
187 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
188 G_CALLBACK(crashed_web_view), c);
190 if (!initial_wc_setup_done)
192 if (accepted_language[0] != NULL)
193 webkit_web_context_set_preferred_languages(wc, accepted_language);
195 g_signal_connect(G_OBJECT(wc), "download-started",
196 G_CALLBACK(download_handle_start), NULL);
198 trust_user_certs(wc);
200 initial_wc_setup_done = TRUE;
203 if (user_agent != NULL)
204 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
205 "user-agent", user_agent, NULL);
208 webkit_settings_set_enable_webgl(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
210 c->location = gtk_entry_new();
211 g_signal_connect(G_OBJECT(c->location), "key-press-event",
212 G_CALLBACK(key_location), c);
214 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
215 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
216 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
218 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
221 show_web_view(NULL, c);
223 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
224 G_CALLBACK(show_web_view), c);
228 f = ensure_uri_scheme(uri);
229 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
235 return WEBKIT_WEB_VIEW(c->web_view);
239 client_new_request(WebKitWebView *web_view,
240 WebKitNavigationAction *navigation_action, gpointer data)
242 return client_new(NULL, web_view, FALSE);
246 cooperation_setup(void)
249 gchar *fifofilename, *fifopath;
251 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
252 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
253 g_free(fifofilename);
255 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
256 mkfifo(fifopath, 0600);
258 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
259 if (!cooperative_pipe_fp)
261 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
265 if (write(cooperative_pipe_fp, "", 0) == -1)
267 /* Could not do an empty write to the FIFO which means there's
268 * no one listening. */
269 close(cooperative_pipe_fp);
270 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
271 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
274 cooperative_alone = FALSE;
281 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
283 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
284 WebKitURIResponse *resp;
285 GtkToolItem *tb = GTK_TOOL_ITEM(data);
288 gchar *t, *filename, *base;
290 p = webkit_download_get_estimated_progress(download);
294 resp = webkit_download_get_response(download);
295 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
297 uri = webkit_download_get_destination(download);
298 filename = g_filename_from_uri(uri, NULL, NULL);
299 if (filename == NULL)
301 /* This really should not happen because WebKit uses that URI to
302 * write to a file... */
303 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
304 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
305 webkit_uri_response_get_uri(resp), p, size_mb);
309 base = g_path_get_basename(filename);
310 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
314 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
319 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
321 struct Client *c = (struct Client *)data;
324 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
326 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
330 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
333 struct Client *c = (struct Client *)data;
335 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
336 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
338 u = u == NULL ? __NAME__ : u;
339 u = u[0] == 0 ? __NAME__ : u;
341 t = t == NULL ? u : t;
342 t = t[0] == 0 ? u : t;
344 gtk_window_set_title(GTK_WINDOW(c->win), t);
348 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
351 struct Client *c = (struct Client *)data;
354 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
356 /* When a web process crashes, we get a "notify::uri" signal, but we
357 * can no longer read a meaningful URI. It's just an empty string
358 * now. Not updating the location bar in this scenario is important,
359 * because we would override the "WEB PROCESS CRASHED" message. */
360 if (t != NULL && strlen(t) > 0)
362 gtk_entry_set_text(GTK_ENTRY(c->location), t);
364 if (history_file != NULL)
366 fp = fopen(history_file, "a");
369 fprintf(fp, "%s\n", t);
373 perror(__NAME__": Error opening history file");
379 crashed_web_view(WebKitWebView *web_view, gpointer data)
382 struct Client *c = (struct Client *)data;
384 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
385 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
386 gtk_entry_set_text(GTK_ENTRY(c->location), t);
393 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
394 WebKitPolicyDecisionType type, gpointer data)
396 WebKitResponsePolicyDecision *r;
400 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
401 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
402 if (!webkit_response_policy_decision_is_mime_type_supported(r))
403 webkit_policy_decision_download(decision);
405 webkit_policy_decision_use(decision);
408 /* Use whatever default there is. */
415 download_handle_finished(WebKitDownload *download, gpointer data)
421 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
424 g_signal_connect(G_OBJECT(download), "decide-destination",
425 G_CALLBACK(download_handle), data);
429 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
431 gchar *sug_clean, *path, *path2 = NULL, *uri;
436 sug_clean = g_strdup(suggested_filename);
437 for (i = 0; i < strlen(sug_clean); i++)
438 if (sug_clean[i] == G_DIR_SEPARATOR)
441 path = g_build_filename(download_dir, sug_clean, NULL);
442 path2 = g_strdup(path);
443 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
447 path2 = g_strdup_printf("%s.%d", path, suffix);
453 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
454 webkit_download_cancel(download);
458 uri = g_filename_to_uri(path2, NULL, NULL);
459 webkit_download_set_destination(download, uri);
462 tb = gtk_tool_button_new(NULL, NULL);
463 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
464 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
465 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
466 gtk_widget_show_all(dm.win);
468 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
469 G_CALLBACK(changed_download_progress), tb);
472 g_signal_connect(G_OBJECT(download), "finished",
473 G_CALLBACK(download_handle_finished), NULL);
475 g_object_ref(download);
476 g_signal_connect(G_OBJECT(tb), "clicked",
477 G_CALLBACK(downloadmanager_cancel), download);
484 /* Propagate -- to whom it may concern. */
489 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
491 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
493 webkit_download_cancel(download);
494 g_object_unref(download);
496 gtk_widget_destroy(GTK_WIDGET(tb));
500 downloadmanager_delete(GtkWidget *obj, gpointer data)
502 if (!quit_if_nothing_active())
503 gtk_widget_hide(dm.win);
509 downloadmanager_setup(void)
511 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
512 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
513 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
514 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
515 g_signal_connect(G_OBJECT(dm.win), "delete-event",
516 G_CALLBACK(downloadmanager_delete), NULL);
517 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
518 G_CALLBACK(key_downloadmanager), NULL);
520 dm.toolbar = gtk_toolbar_new();
521 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
522 GTK_ORIENTATION_VERTICAL);
523 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
524 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
526 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
527 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
528 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
529 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
531 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
535 ensure_uri_scheme(const gchar *t)
539 f = g_ascii_strdown(t, -1);
540 if (!g_str_has_prefix(f, "http:") &&
541 !g_str_has_prefix(f, "https:") &&
542 !g_str_has_prefix(f, "file:") &&
543 !g_str_has_prefix(f, "about:"))
546 fabs = realpath(t, NULL);
549 f = g_strdup_printf("file://%s", fabs);
553 f = g_strdup_printf("http://%s", t);
561 external_handler_run(GtkAction *action, gpointer data)
563 struct Client *c = (struct Client *)data;
564 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
570 argv[2] = c->external_handler_uri;
571 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
574 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
579 g_spawn_close_pid(pid);
583 grab_environment_configuration(void)
587 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
589 accepted_language[0] = g_strdup(e);
591 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
593 download_dir = g_strdup(e);
595 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_EXPERIMENTAL_WEBGL");
599 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
601 fifo_suffix = g_strdup(e);
603 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
605 history_file = g_strdup(e);
607 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
609 home_uri = g_strdup(e);
611 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
613 user_agent = g_strdup(e);
615 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
617 global_zoom = atof(e);
621 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
624 struct Client *c = (struct Client *)data;
626 if (!gtk_widget_is_focus(c->location))
628 if (webkit_hit_test_result_context_is_link(ht))
630 gtk_entry_set_text(GTK_ENTRY(c->location),
631 webkit_hit_test_result_get_link_uri(ht));
633 if (c->hover_uri != NULL)
634 g_free(c->hover_uri);
635 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
639 gtk_entry_set_text(GTK_ENTRY(c->location),
640 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
642 if (c->hover_uri != NULL)
643 g_free(c->hover_uri);
650 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
652 struct Client *c = (struct Client *)data;
653 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
656 if (event->type == GDK_KEY_PRESS)
658 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
660 switch (((GdkEventKey *)event)->keyval)
662 case GDK_KEY_q: /* close window (left hand) */
663 gtk_widget_destroy(c->win);
665 case GDK_KEY_w: /* home (left hand) */
666 f = ensure_uri_scheme(home_uri);
667 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
670 case GDK_KEY_e: /* new tab (left hand) */
671 f = ensure_uri_scheme(home_uri);
672 client_new(f, NULL, TRUE);
675 case GDK_KEY_r: /* reload (left hand) */
676 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
679 case GDK_KEY_d: /* download manager (left hand) */
680 gtk_widget_show_all(dm.win);
682 case GDK_KEY_2: /* search forward (left hand) */
683 case GDK_KEY_n: /* search forward (maybe both hands) */
686 case GDK_KEY_3: /* search backward (left hand) */
689 case GDK_KEY_l: /* location (BOTH hands) */
690 gtk_widget_grab_focus(c->location);
692 case GDK_KEY_k: /* initiate search (BOTH hands) */
693 gtk_widget_grab_focus(c->location);
694 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
695 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
697 case GDK_KEY_c: /* reload trusted certs (left hand) */
698 trust_user_certs(wc);
700 case GDK_KEY_x: /* launch external handler (left hand) */
701 if (c->external_handler_uri != NULL)
702 g_free(c->external_handler_uri);
703 c->external_handler_uri = g_strdup(
704 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
705 external_handler_run(NULL, c);
709 /* navigate backward (left hand) */
710 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
712 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
715 /* navigate forward (left hand) */
716 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
718 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
727 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
729 if (event->type == GDK_KEY_PRESS)
731 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
733 switch (((GdkEventKey *)event)->keyval)
735 case GDK_KEY_d: /* close window (left hand) */
737 downloadmanager_delete(dm.win, NULL);
747 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
749 struct Client *c = (struct Client *)data;
753 if (key_common(widget, event, data))
756 if (event->type == GDK_KEY_PRESS)
758 switch (((GdkEventKey *)event)->keyval)
760 case GDK_KEY_KP_Enter:
762 gtk_widget_grab_focus(c->web_view);
763 t = gtk_entry_get_text(GTK_ENTRY(c->location));
764 if (t != NULL && t[0] == ':' && t[1] == '/')
766 if (search_text != NULL)
768 search_text = g_strdup(t + 2); /* XXX whacky */
771 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
773 f = ensure_uri_scheme(t);
774 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
779 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
780 gtk_entry_set_text(GTK_ENTRY(c->location),
781 (t == NULL ? __NAME__ : t));
790 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
792 struct Client *c = (struct Client *)data;
796 if (key_common(widget, event, data))
799 if (event->type == GDK_KEY_PRESS)
801 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
803 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
804 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
807 else if (event->type == GDK_BUTTON_PRESS)
809 switch (((GdkEventButton *)event)->button)
812 if (c->hover_uri != NULL)
814 client_new(c->hover_uri, NULL, TRUE);
819 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
822 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
826 else if (event->type == GDK_SCROLL)
828 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
829 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
831 gdk_event_get_scroll_deltas(event, &dx, &dy);
832 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
834 z = dx != 0 ? global_zoom : z;
835 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
847 GIOChannel *channel = NULL;
848 gchar *path = NULL, *buf = NULL;
849 gchar **tokens = NULL;
851 keywords = g_hash_table_new(g_str_hash, g_str_equal);
853 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
855 channel = g_io_channel_new_file(path, "r", &err);
858 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
859 == G_IO_STATUS_NORMAL)
864 tokens = g_strsplit(buf, " ", 2);
865 if (tokens[0] != NULL && tokens[1] != NULL)
866 g_hash_table_insert(keywords, g_strdup(tokens[0]),
867 g_strdup(tokens[1]));
872 g_io_channel_shutdown(channel, FALSE, NULL);
878 keywords_try_search(WebKitWebView *web_view, const gchar *t)
880 gboolean ret = FALSE;
881 gchar **tokens = NULL;
882 gchar *val = NULL, *uri = NULL;
884 tokens = g_strsplit(t, " ", 2);
885 if (tokens[0] != NULL && tokens[1] != NULL)
887 val = g_hash_table_lookup(keywords, tokens[0]);
890 uri = g_strdup_printf((gchar *)val, tokens[1]);
891 webkit_web_view_load_uri(web_view, uri);
902 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
903 WebKitHitTestResult *ht, gpointer data)
905 struct Client *c = (struct Client *)data;
906 GtkAction *action = NULL;
907 WebKitContextMenuItem *mi = NULL;
908 const gchar *uri = NULL;
912 if (webkit_hit_test_result_context_is_link(ht))
913 uri = webkit_hit_test_result_get_link_uri(ht);
914 else if (webkit_hit_test_result_context_is_image(ht))
915 uri = webkit_hit_test_result_get_image_uri(ht);
916 else if (webkit_hit_test_result_context_is_media(ht))
917 uri = webkit_hit_test_result_get_media_uri(ht);
921 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
923 if (c->external_handler_uri != NULL)
924 g_free(c->external_handler_uri);
925 c->external_handler_uri = g_strdup(uri);
926 action = gtk_action_new("external_handler", "Open with external handler",
928 g_signal_connect(G_OBJECT(action), "activate",
929 G_CALLBACK(external_handler_run), data);
930 mi = webkit_context_menu_item_new(action);
931 webkit_context_menu_append(menu, mi);
934 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
939 quit_if_nothing_active(void)
949 gtk_widget_show_all(dm.win);
956 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
960 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
964 client_new(uri, NULL, TRUE);
971 search(gpointer data, gint direction)
973 struct Client *c = (struct Client *)data;
974 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
975 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
977 if (search_text == NULL)
983 webkit_find_controller_search(fc, search_text,
984 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
985 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
989 webkit_find_controller_search_next(fc);
992 webkit_find_controller_search_previous(fc);
998 show_web_view(WebKitWebView *web_view, gpointer data)
1000 struct Client *c = (struct Client *)data;
1004 gtk_widget_grab_focus(c->web_view);
1005 gtk_widget_show_all(c->win);
1012 GIOChannel *tabbed_stdout_channel;
1014 gchar *output = NULL;
1015 char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
1018 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
1019 NULL, NULL, NULL, &tabbed_stdout, NULL,
1022 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
1027 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
1028 if (tabbed_stdout_channel == NULL)
1030 fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
1033 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
1034 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
1037 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
1041 plug_into = strtol(output, NULL, 16);
1044 fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
1049 trust_user_certs(WebKitWebContext *wc)
1051 GTlsCertificate *cert;
1052 const gchar *basedir, *file, *absfile;
1055 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1056 dir = g_dir_open(basedir, 0, NULL);
1059 file = g_dir_read_name(dir);
1060 while (file != NULL)
1062 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1064 cert = g_tls_certificate_new_from_file(absfile, NULL);
1066 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1068 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1069 file = g_dir_read_name(dir);
1077 main(int argc, char **argv)
1082 gtk_init(&argc, &argv);
1083 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1084 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1086 grab_environment_configuration();
1088 while ((opt = getopt(argc, argv, "e:CT")) != -1)
1093 embed = atol(optarg);
1094 tabbed_automagic = FALSE;
1097 cooperative_instances = FALSE;
1100 tabbed_automagic = FALSE;
1103 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1109 if (cooperative_instances)
1110 cooperation_setup();
1111 downloadmanager_setup();
1113 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1114 embed = tabbed_launch();
1116 if (!cooperative_instances || cooperative_alone)
1118 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1120 webkit_web_context_set_web_extensions_directory(
1121 webkit_web_context_get_default(), c
1126 client_new(home_uri, NULL, TRUE);
1129 for (i = optind; i < argc; i++)
1130 client_new(argv[i], NULL, TRUE);
1133 if (!cooperative_instances || cooperative_alone)