11 #include <gdk/gdkkeysyms.h>
13 #include <webkit2/webkit2.h>
14 #include <JavaScriptCore/JavaScript.h>
17 static void client_destroy(GtkWidget *, gpointer);
18 static gboolean client_destroy_request(WebKitWebView *, gpointer);
19 static WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean);
20 static WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
22 static void cooperation_setup(void);
23 static void changed_download_progress(GObject *, GParamSpec *, gpointer);
24 static void changed_load_progress(GObject *, GParamSpec *, gpointer);
25 static void changed_title(GObject *, GParamSpec *, gpointer);
26 static void changed_uri(GObject *, GParamSpec *, gpointer);
27 static gboolean crashed_web_view(WebKitWebView *, 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);
33 static gboolean downloadmanager_delete(GtkWidget *, gpointer);
34 static void downloadmanager_setup(void);
35 static gchar *ensure_uri_scheme(const gchar *);
36 static void external_handler_run(GSimpleAction *, GVariant *, gpointer);
37 static void grab_environment_configuration(void);
38 static void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
39 static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
40 static void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
41 static gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
42 static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
43 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
44 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
45 static void keywords_load(void);
46 static gboolean keywords_try_search(WebKitWebView *, const gchar *);
47 static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
48 WebKitHitTestResult *, gpointer);
49 static gboolean quit_if_nothing_active(void);
50 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
51 static void search(gpointer, gint);
52 static void show_web_view(WebKitWebView *, gpointer);
53 static Window tabbed_launch(void);
54 static void trust_user_certs(WebKitWebContext *);
59 gchar *external_handler_uri;
68 struct DownloadManager
76 static const gchar *accepted_language[2] = { NULL, NULL };
77 static gint clients = 0, downloads = 0;
78 static gboolean cooperative_alone = TRUE;
79 static gboolean cooperative_instances = TRUE;
80 static int cooperative_pipe_fp = 0;
81 static gchar *download_dir = "/var/tmp";
82 static gboolean enable_webgl = FALSE;
83 static gboolean enable_console_to_stdout = FALSE;
84 static Window embed = 0;
85 static gchar *fifo_suffix = "main";
86 static gdouble global_zoom = 1.0;
87 static gchar *history_file = NULL;
88 static gchar *home_uri = "about:blank";
89 static gboolean initial_wc_setup_done = FALSE;
90 static GHashTable *keywords = NULL;
91 static gchar *search_text = NULL;
92 static gboolean tabbed_automagic = TRUE;
93 static gchar *user_agent = NULL;
95 static gchar *feed_html_header =
99 " <meta charset=\"UTF-8\">"
100 " <title>Feeds</title>"
103 " <p>Feeds found on this page:</p>"
107 static gchar *feed_html_footer =
113 static gchar *grab_feeds =
114 "a = document.querySelectorAll('"
115 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
116 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
123 " for (i = 0; i < a.length; i++)"
125 " url = encodeURIComponent(a[i].href);"
126 " if ('title' in a[i] && a[i].title != '')"
127 " title = encodeURIComponent(a[i].title);"
130 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
138 client_destroy(GtkWidget *widget, gpointer data)
140 struct Client *c = (struct Client *)data;
142 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
143 changed_load_progress, c);
148 quit_if_nothing_active();
152 client_destroy_request(WebKitWebView *web_view, gpointer data)
154 struct Client *c = (struct Client *)data;
156 gtk_widget_destroy(c->win);
162 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
165 WebKitWebContext *wc;
168 if (uri != NULL && cooperative_instances && !cooperative_alone)
170 f = ensure_uri_scheme(uri);
171 write(cooperative_pipe_fp, f, strlen(f));
172 write(cooperative_pipe_fp, "\n", 1);
177 c = calloc(1, sizeof(struct Client));
180 fprintf(stderr, __NAME__": fatal: calloc failed\n");
186 c->win = gtk_plug_new(embed);
187 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
189 fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
190 gtk_widget_destroy(c->win);
197 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
199 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
201 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
202 gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
204 if (related_wv == NULL)
205 c->web_view = webkit_web_view_new();
207 c->web_view = webkit_web_view_new_with_related_view(related_wv);
208 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
210 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
211 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
212 G_CALLBACK(changed_title), c);
213 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
214 G_CALLBACK(changed_uri), c);
215 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
216 G_CALLBACK(changed_load_progress), c);
217 g_signal_connect(G_OBJECT(c->web_view), "create",
218 G_CALLBACK(client_new_request), NULL);
219 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
220 G_CALLBACK(menu_web_view), c);
221 g_signal_connect(G_OBJECT(c->web_view), "close",
222 G_CALLBACK(client_destroy_request), c);
223 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
224 G_CALLBACK(decide_policy), NULL);
225 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
226 G_CALLBACK(key_web_view), c);
227 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
228 G_CALLBACK(key_web_view), c);
229 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
230 G_CALLBACK(key_web_view), c);
231 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
232 G_CALLBACK(hover_web_view), c);
233 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
234 G_CALLBACK(crashed_web_view), c);
236 if (!initial_wc_setup_done)
238 if (accepted_language[0] != NULL)
239 webkit_web_context_set_preferred_languages(wc, accepted_language);
241 g_signal_connect(G_OBJECT(wc), "download-started",
242 G_CALLBACK(download_handle_start), NULL);
244 trust_user_certs(wc);
246 initial_wc_setup_done = TRUE;
249 if (user_agent != NULL)
250 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
251 "user-agent", user_agent, NULL);
253 if (enable_console_to_stdout)
254 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
257 webkit_settings_set_enable_webgl(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
259 c->location = gtk_entry_new();
260 g_signal_connect(G_OBJECT(c->location), "key-press-event",
261 G_CALLBACK(key_location), c);
262 g_signal_connect(G_OBJECT(c->location), "icon-release",
263 G_CALLBACK(icon_location), c);
264 /* XXX This is a workaround. Setting this to NULL (which is done in
265 * grab_feeds_finished() if no feed has been detected) adds a little
266 * padding left of the text. Not sure why. The point of this call
267 * right here is to have that padding right from the start. This
268 * avoids a graphical artifact. */
269 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
270 GTK_ENTRY_ICON_PRIMARY,
273 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
274 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
275 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
277 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
280 show_web_view(NULL, c);
282 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
283 G_CALLBACK(show_web_view), c);
287 f = ensure_uri_scheme(uri);
288 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
294 return WEBKIT_WEB_VIEW(c->web_view);
298 client_new_request(WebKitWebView *web_view,
299 WebKitNavigationAction *navigation_action, gpointer data)
301 return client_new(NULL, web_view, FALSE);
305 cooperation_setup(void)
308 gchar *fifofilename, *fifopath;
310 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
311 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
312 g_free(fifofilename);
314 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
315 mkfifo(fifopath, 0600);
317 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
318 if (!cooperative_pipe_fp)
320 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
324 if (write(cooperative_pipe_fp, "", 0) == -1)
326 /* Could not do an empty write to the FIFO which means there's
327 * no one listening. */
328 close(cooperative_pipe_fp);
329 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
330 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
333 cooperative_alone = FALSE;
340 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
342 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
343 WebKitURIResponse *resp;
344 GtkToolItem *tb = GTK_TOOL_ITEM(data);
347 gchar *t, *filename, *base;
349 p = webkit_download_get_estimated_progress(download);
353 resp = webkit_download_get_response(download);
354 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
356 uri = webkit_download_get_destination(download);
357 filename = g_filename_from_uri(uri, NULL, NULL);
358 if (filename == NULL)
360 /* This really should not happen because WebKit uses that URI to
361 * write to a file... */
362 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
363 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
364 webkit_uri_response_get_uri(resp), p, size_mb);
368 base = g_path_get_basename(filename);
369 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
373 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
378 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
380 struct Client *c = (struct Client *)data;
383 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
388 /* The page has loaded fully. We now run a short JavaScript
389 * snippet that operates on the DOM. It tries to grab all
390 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
392 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
394 grab_feeds_finished, c);
396 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
400 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
403 struct Client *c = (struct Client *)data;
405 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
406 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
408 u = u == NULL ? __NAME__ : u;
409 u = u[0] == 0 ? __NAME__ : u;
411 t = t == NULL ? u : t;
412 t = t[0] == 0 ? u : t;
414 gtk_window_set_title(GTK_WINDOW(c->win), t);
418 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
421 struct Client *c = (struct Client *)data;
424 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
426 /* When a web process crashes, we get a "notify::uri" signal, but we
427 * can no longer read a meaningful URI. It's just an empty string
428 * now. Not updating the location bar in this scenario is important,
429 * because we would override the "WEB PROCESS CRASHED" message. */
430 if (t != NULL && strlen(t) > 0)
432 gtk_entry_set_text(GTK_ENTRY(c->location), t);
434 if (history_file != NULL)
436 fp = fopen(history_file, "a");
439 fprintf(fp, "%s\n", t);
443 perror(__NAME__": Error opening history file");
449 crashed_web_view(WebKitWebView *web_view, gpointer data)
452 struct Client *c = (struct Client *)data;
454 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
455 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
456 gtk_entry_set_text(GTK_ENTRY(c->location), t);
463 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
464 WebKitPolicyDecisionType type, gpointer data)
466 WebKitResponsePolicyDecision *r;
470 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
471 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
472 if (!webkit_response_policy_decision_is_mime_type_supported(r))
473 webkit_policy_decision_download(decision);
475 webkit_policy_decision_use(decision);
478 /* Use whatever default there is. */
485 download_handle_finished(WebKitDownload *download, gpointer data)
491 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
494 g_signal_connect(G_OBJECT(download), "decide-destination",
495 G_CALLBACK(download_handle), data);
499 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
501 gchar *sug_clean, *path, *path2 = NULL, *uri;
506 sug_clean = g_strdup(suggested_filename);
507 for (i = 0; i < strlen(sug_clean); i++)
508 if (sug_clean[i] == G_DIR_SEPARATOR)
511 path = g_build_filename(download_dir, sug_clean, NULL);
512 path2 = g_strdup(path);
513 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
517 path2 = g_strdup_printf("%s.%d", path, suffix);
523 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
524 webkit_download_cancel(download);
528 uri = g_filename_to_uri(path2, NULL, NULL);
529 webkit_download_set_destination(download, uri);
532 tb = gtk_tool_button_new(NULL, NULL);
533 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
534 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
535 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
536 gtk_widget_show_all(dm.win);
538 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
539 G_CALLBACK(changed_download_progress), tb);
542 g_signal_connect(G_OBJECT(download), "finished",
543 G_CALLBACK(download_handle_finished), NULL);
545 g_object_ref(download);
546 g_signal_connect(G_OBJECT(tb), "clicked",
547 G_CALLBACK(downloadmanager_cancel), download);
554 /* Propagate -- to whom it may concern. */
559 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
561 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
563 webkit_download_cancel(download);
564 g_object_unref(download);
566 gtk_widget_destroy(GTK_WIDGET(tb));
570 downloadmanager_delete(GtkWidget *obj, gpointer data)
572 if (!quit_if_nothing_active())
573 gtk_widget_hide(dm.win);
579 downloadmanager_setup(void)
581 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
582 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
583 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
584 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
585 g_signal_connect(G_OBJECT(dm.win), "delete-event",
586 G_CALLBACK(downloadmanager_delete), NULL);
587 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
588 G_CALLBACK(key_downloadmanager), NULL);
590 dm.toolbar = gtk_toolbar_new();
591 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
592 GTK_ORIENTATION_VERTICAL);
593 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
594 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
596 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
597 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
598 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
599 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
601 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
605 ensure_uri_scheme(const gchar *t)
609 f = g_ascii_strdown(t, -1);
610 if (!g_str_has_prefix(f, "http:") &&
611 !g_str_has_prefix(f, "https:") &&
612 !g_str_has_prefix(f, "file:") &&
613 !g_str_has_prefix(f, "about:") &&
614 !g_str_has_prefix(f, "data:"))
617 fabs = realpath(t, NULL);
620 f = g_strdup_printf("file://%s", fabs);
624 f = g_strdup_printf("http://%s", t);
632 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
634 struct Client *c = (struct Client *)data;
635 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
642 argv[2] = c->external_handler_uri;
643 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
646 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
651 g_spawn_close_pid(pid);
655 grab_environment_configuration(void)
659 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
661 accepted_language[0] = g_strdup(e);
663 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
665 download_dir = g_strdup(e);
667 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
669 enable_console_to_stdout = TRUE;
671 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_EXPERIMENTAL_WEBGL");
675 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
677 fifo_suffix = g_strdup(e);
679 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
681 history_file = g_strdup(e);
683 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
685 home_uri = g_strdup(e);
687 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
689 user_agent = g_strdup(e);
691 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
693 global_zoom = atof(e);
697 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
699 struct Client *c = (struct Client *)data;
700 WebKitJavascriptResult *js_result;
702 JSGlobalContextRef context;
704 JSStringRef js_str_value;
707 g_free(c->feed_html);
710 /* This was taken almost verbatim from the example in WebKit's
713 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
717 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
722 context = webkit_javascript_result_get_global_context(js_result);
723 value = webkit_javascript_result_get_value(js_result);
725 if (JSValueIsString(context, value))
727 js_str_value = JSValueToStringCopy(context, value, NULL);
728 str_length = JSStringGetMaximumUTF8CStringSize(js_str_value);
729 c->feed_html = (gchar *)g_malloc(str_length);
730 JSStringGetUTF8CString(js_str_value, c->feed_html, str_length);
731 JSStringRelease(js_str_value);
733 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
734 GTK_ENTRY_ICON_PRIMARY,
735 "application-rss+xml-symbolic");
736 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
737 GTK_ENTRY_ICON_PRIMARY,
742 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
743 GTK_ENTRY_ICON_PRIMARY,
747 webkit_javascript_result_unref(js_result);
751 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
754 struct Client *c = (struct Client *)data;
756 if (!gtk_widget_is_focus(c->location))
758 if (webkit_hit_test_result_context_is_link(ht))
760 gtk_entry_set_text(GTK_ENTRY(c->location),
761 webkit_hit_test_result_get_link_uri(ht));
763 if (c->hover_uri != NULL)
764 g_free(c->hover_uri);
765 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
769 gtk_entry_set_text(GTK_ENTRY(c->location),
770 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
772 if (c->hover_uri != NULL)
773 g_free(c->hover_uri);
780 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event, gpointer data)
782 struct Client *c = (struct Client *)data;
785 if (c->feed_html != NULL)
787 /* What we're actually trying to do is show a simple HTML page
788 * that lists all the feeds on the current page. The function
789 * webkit_web_view_load_html() looks like the proper way to do
790 * that. Sad thing is, it doesn't create a history entry, but
791 * instead simply replaces the content of the current page. This
792 * is not what we want.
794 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
795 * mechanism to show my custom HTML snippet *and* create a
798 * [0]: https://tools.ietf.org/html/rfc2397
799 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
800 d = g_strdup_printf("data:text/html,%s%s%s",
804 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
810 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
812 struct Client *c = (struct Client *)data;
813 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
816 if (event->type == GDK_KEY_PRESS)
818 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
820 switch (((GdkEventKey *)event)->keyval)
822 case GDK_KEY_q: /* close window (left hand) */
823 gtk_widget_destroy(c->win);
825 case GDK_KEY_w: /* home (left hand) */
826 f = ensure_uri_scheme(home_uri);
827 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
830 case GDK_KEY_e: /* new tab (left hand) */
831 f = ensure_uri_scheme(home_uri);
832 client_new(f, NULL, TRUE);
835 case GDK_KEY_r: /* reload (left hand) */
836 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
839 case GDK_KEY_d: /* download manager (left hand) */
840 gtk_widget_show_all(dm.win);
842 case GDK_KEY_2: /* search forward (left hand) */
843 case GDK_KEY_n: /* search forward (maybe both hands) */
846 case GDK_KEY_3: /* search backward (left hand) */
849 case GDK_KEY_l: /* location (BOTH hands) */
850 gtk_widget_grab_focus(c->location);
852 case GDK_KEY_k: /* initiate search (BOTH hands) */
853 gtk_widget_grab_focus(c->location);
854 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
855 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
857 case GDK_KEY_c: /* reload trusted certs (left hand) */
858 trust_user_certs(wc);
860 case GDK_KEY_x: /* launch external handler (left hand) */
861 if (c->external_handler_uri != NULL)
862 g_free(c->external_handler_uri);
863 c->external_handler_uri = g_strdup(
864 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
865 external_handler_run(NULL, NULL, c);
869 /* navigate backward (left hand) */
870 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
872 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
875 /* navigate forward (left hand) */
876 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
878 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
887 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
889 if (event->type == GDK_KEY_PRESS)
891 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
893 switch (((GdkEventKey *)event)->keyval)
895 case GDK_KEY_d: /* close window (left hand) */
897 downloadmanager_delete(dm.win, NULL);
907 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
909 struct Client *c = (struct Client *)data;
913 if (key_common(widget, event, data))
916 if (event->type == GDK_KEY_PRESS)
918 switch (((GdkEventKey *)event)->keyval)
920 case GDK_KEY_KP_Enter:
922 gtk_widget_grab_focus(c->web_view);
923 t = gtk_entry_get_text(GTK_ENTRY(c->location));
924 if (t != NULL && t[0] == ':' && t[1] == '/')
926 if (search_text != NULL)
928 search_text = g_strdup(t + 2);
931 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
933 f = ensure_uri_scheme(t);
934 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
939 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
940 gtk_entry_set_text(GTK_ENTRY(c->location),
941 (t == NULL ? __NAME__ : t));
950 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
952 struct Client *c = (struct Client *)data;
956 if (key_common(widget, event, data))
959 if (event->type == GDK_KEY_PRESS)
961 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
963 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
964 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
967 else if (event->type == GDK_BUTTON_RELEASE)
969 switch (((GdkEventButton *)event)->button)
972 if (c->hover_uri != NULL)
974 client_new(c->hover_uri, NULL, TRUE);
979 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
982 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
986 else if (event->type == GDK_SCROLL)
988 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
989 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
991 gdk_event_get_scroll_deltas(event, &dx, &dy);
992 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
994 z = dx != 0 ? global_zoom : z;
995 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1007 GIOChannel *channel = NULL;
1008 gchar *path = NULL, *buf = NULL;
1009 gchar **tokens = NULL;
1011 keywords = g_hash_table_new(g_str_hash, g_str_equal);
1013 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1015 channel = g_io_channel_new_file(path, "r", &err);
1016 if (channel != NULL)
1018 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1019 == G_IO_STATUS_NORMAL)
1024 tokens = g_strsplit(buf, " ", 2);
1025 if (tokens[0] != NULL && tokens[1] != NULL)
1026 g_hash_table_insert(keywords, g_strdup(tokens[0]),
1027 g_strdup(tokens[1]));
1032 g_io_channel_shutdown(channel, FALSE, NULL);
1038 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1040 gboolean ret = FALSE;
1041 gchar **tokens = NULL;
1042 gchar *val = NULL, *escaped = NULL, *uri = NULL;
1044 tokens = g_strsplit(t, " ", 2);
1045 if (tokens[0] != NULL && tokens[1] != NULL)
1047 val = g_hash_table_lookup(keywords, tokens[0]);
1050 escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1051 uri = g_strdup_printf((gchar *)val, escaped);
1052 webkit_web_view_load_uri(web_view, uri);
1064 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1065 WebKitHitTestResult *ht, gpointer data)
1067 struct Client *c = (struct Client *)data;
1068 GSimpleAction *action = NULL;
1069 WebKitContextMenuItem *mi = NULL;
1070 const gchar *uri = NULL;
1074 if (webkit_hit_test_result_context_is_link(ht))
1075 uri = webkit_hit_test_result_get_link_uri(ht);
1076 else if (webkit_hit_test_result_context_is_image(ht))
1077 uri = webkit_hit_test_result_get_image_uri(ht);
1078 else if (webkit_hit_test_result_context_is_media(ht))
1079 uri = webkit_hit_test_result_get_media_uri(ht);
1083 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1085 if (c->external_handler_uri != NULL)
1086 g_free(c->external_handler_uri);
1087 c->external_handler_uri = g_strdup(uri);
1088 action = g_simple_action_new("external_handler", NULL);
1089 g_signal_connect(G_OBJECT(action), "activate",
1090 G_CALLBACK(external_handler_run), data);
1091 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1092 "Open with external handler",
1094 webkit_context_menu_append(menu, mi);
1095 g_object_unref(action);
1098 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1103 quit_if_nothing_active(void)
1113 gtk_widget_show_all(dm.win);
1120 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1124 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1128 client_new(uri, NULL, TRUE);
1135 search(gpointer data, gint direction)
1137 struct Client *c = (struct Client *)data;
1138 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1139 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1141 if (search_text == NULL)
1147 webkit_find_controller_search(fc, search_text,
1148 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1149 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1153 webkit_find_controller_search_next(fc);
1156 webkit_find_controller_search_previous(fc);
1162 show_web_view(WebKitWebView *web_view, gpointer data)
1164 struct Client *c = (struct Client *)data;
1168 gtk_widget_grab_focus(c->web_view);
1169 gtk_widget_show_all(c->win);
1176 GIOChannel *tabbed_stdout_channel;
1178 gchar *output = NULL;
1179 char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
1182 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
1183 NULL, NULL, NULL, &tabbed_stdout, NULL,
1186 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
1191 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
1192 if (tabbed_stdout_channel == NULL)
1194 fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
1197 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
1198 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
1201 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
1205 plug_into = strtol(output, NULL, 16);
1208 fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
1213 trust_user_certs(WebKitWebContext *wc)
1215 GTlsCertificate *cert;
1216 const gchar *basedir, *file, *absfile;
1219 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1220 dir = g_dir_open(basedir, 0, NULL);
1223 file = g_dir_read_name(dir);
1224 while (file != NULL)
1226 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1228 cert = g_tls_certificate_new_from_file(absfile, NULL);
1230 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1232 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1233 file = g_dir_read_name(dir);
1241 main(int argc, char **argv)
1246 gtk_init(&argc, &argv);
1247 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1248 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1250 grab_environment_configuration();
1252 while ((opt = getopt(argc, argv, "e:CT")) != -1)
1257 embed = atol(optarg);
1258 tabbed_automagic = FALSE;
1261 cooperative_instances = FALSE;
1264 tabbed_automagic = FALSE;
1267 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1273 if (cooperative_instances)
1274 cooperation_setup();
1275 downloadmanager_setup();
1277 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1278 embed = tabbed_launch();
1280 if (!cooperative_instances || cooperative_alone)
1282 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1284 webkit_web_context_set_web_extensions_directory(
1285 webkit_web_context_get_default(), c
1290 client_new(home_uri, NULL, TRUE);
1293 for (i = optind; i < argc; i++)
1294 client_new(argv[i], NULL, TRUE);
1297 if (!cooperative_instances || cooperative_alone)