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 feed_icon(gpointer, gchar *);
38 static void grab_environment_configuration(void);
39 static void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
40 static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
41 static void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
42 static gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
43 static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
44 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
45 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
46 static void keywords_load(void);
47 static gboolean keywords_try_search(WebKitWebView *, const gchar *);
48 static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
49 WebKitHitTestResult *, gpointer);
50 static gboolean quit_if_nothing_active(void);
51 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
52 static void search(gpointer, gint);
53 static void show_web_view(WebKitWebView *, gpointer);
54 static Window tabbed_launch(void);
55 static void trust_user_certs(WebKitWebContext *);
60 gchar *external_handler_uri;
69 struct DownloadManager
77 static const gchar *accepted_language[2] = { NULL, NULL };
78 static gint clients = 0, downloads = 0;
79 static gboolean cooperative_alone = TRUE;
80 static gboolean cooperative_instances = TRUE;
81 static int cooperative_pipe_fp = 0;
82 static gchar *download_dir = "/var/tmp";
83 static gboolean enable_webgl = FALSE;
84 static gboolean enable_console_to_stdout = FALSE;
85 static Window embed = 0;
86 static gchar *fifo_suffix = "main";
87 static gdouble global_zoom = 1.0;
88 static gchar *history_file = NULL;
89 static gchar *home_uri = "about:blank";
90 static gboolean initial_wc_setup_done = FALSE;
91 static GHashTable *keywords = NULL;
92 static gchar *search_text = NULL;
93 static gboolean tabbed_automagic = TRUE;
94 static gchar *user_agent = NULL;
96 static gchar *feed_html_header =
100 " <meta charset=\"UTF-8\">"
101 " <title>Feeds</title>"
104 " <p>Feeds found on this page:</p>"
108 static gchar *feed_html_footer =
114 static gchar *grab_feeds =
115 "a = document.querySelectorAll('"
116 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
117 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
124 " for (i = 0; i < a.length; i++)"
126 " url = encodeURIComponent(a[i].href);"
127 " if ('title' in a[i] && a[i].title != '')"
128 " title = encodeURIComponent(a[i].title);"
131 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
139 client_destroy(GtkWidget *widget, gpointer data)
141 struct Client *c = (struct Client *)data;
143 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
144 changed_load_progress, c);
149 quit_if_nothing_active();
153 client_destroy_request(WebKitWebView *web_view, gpointer data)
155 struct Client *c = (struct Client *)data;
157 gtk_widget_destroy(c->win);
163 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
166 WebKitWebContext *wc;
169 if (uri != NULL && cooperative_instances && !cooperative_alone)
171 f = ensure_uri_scheme(uri);
172 write(cooperative_pipe_fp, f, strlen(f));
173 write(cooperative_pipe_fp, "\n", 1);
178 c = calloc(1, sizeof(struct Client));
181 fprintf(stderr, __NAME__": fatal: calloc failed\n");
187 c->win = gtk_plug_new(embed);
188 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
190 fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
191 gtk_widget_destroy(c->win);
198 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
200 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
202 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
203 gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
205 if (related_wv == NULL)
206 c->web_view = webkit_web_view_new();
208 c->web_view = webkit_web_view_new_with_related_view(related_wv);
209 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
211 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
212 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
213 G_CALLBACK(changed_title), c);
214 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
215 G_CALLBACK(changed_uri), c);
216 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
217 G_CALLBACK(changed_load_progress), c);
218 g_signal_connect(G_OBJECT(c->web_view), "create",
219 G_CALLBACK(client_new_request), NULL);
220 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
221 G_CALLBACK(menu_web_view), c);
222 g_signal_connect(G_OBJECT(c->web_view), "close",
223 G_CALLBACK(client_destroy_request), c);
224 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
225 G_CALLBACK(decide_policy), NULL);
226 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
227 G_CALLBACK(key_web_view), c);
228 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
229 G_CALLBACK(key_web_view), c);
230 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
231 G_CALLBACK(key_web_view), c);
232 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
233 G_CALLBACK(hover_web_view), c);
234 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
235 G_CALLBACK(crashed_web_view), c);
237 if (!initial_wc_setup_done)
239 if (accepted_language[0] != NULL)
240 webkit_web_context_set_preferred_languages(wc, accepted_language);
242 g_signal_connect(G_OBJECT(wc), "download-started",
243 G_CALLBACK(download_handle_start), NULL);
245 trust_user_certs(wc);
247 initial_wc_setup_done = TRUE;
250 if (user_agent != NULL)
251 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
252 "user-agent", user_agent, NULL);
254 if (enable_console_to_stdout)
255 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
258 webkit_settings_set_enable_webgl(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
260 c->location = gtk_entry_new();
261 g_signal_connect(G_OBJECT(c->location), "key-press-event",
262 G_CALLBACK(key_location), c);
263 g_signal_connect(G_OBJECT(c->location), "icon-release",
264 G_CALLBACK(icon_location), c);
265 /* XXX This is a workaround. Setting this to NULL (which is done in
266 * feed_icon() if no feed has been detected) adds a little padding
267 * left of the text. Not sure why. The point of this call right
268 * here is to have that padding right from the start. This avoids a
269 * graphical artifact. */
270 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
271 GTK_ENTRY_ICON_PRIMARY,
274 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
275 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
276 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
278 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
281 show_web_view(NULL, c);
283 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
284 G_CALLBACK(show_web_view), c);
288 f = ensure_uri_scheme(uri);
289 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
295 return WEBKIT_WEB_VIEW(c->web_view);
299 client_new_request(WebKitWebView *web_view,
300 WebKitNavigationAction *navigation_action, gpointer data)
302 return client_new(NULL, web_view, FALSE);
306 cooperation_setup(void)
309 gchar *fifofilename, *fifopath;
311 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
312 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
313 g_free(fifofilename);
315 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
316 mkfifo(fifopath, 0600);
318 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
319 if (!cooperative_pipe_fp)
321 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
325 if (write(cooperative_pipe_fp, "", 0) == -1)
327 /* Could not do an empty write to the FIFO which means there's
328 * no one listening. */
329 close(cooperative_pipe_fp);
330 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
331 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
334 cooperative_alone = FALSE;
341 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
343 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
344 WebKitURIResponse *resp;
345 GtkToolItem *tb = GTK_TOOL_ITEM(data);
348 gchar *t, *filename, *base;
350 p = webkit_download_get_estimated_progress(download);
354 resp = webkit_download_get_response(download);
355 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
357 uri = webkit_download_get_destination(download);
358 filename = g_filename_from_uri(uri, NULL, NULL);
359 if (filename == NULL)
361 /* This really should not happen because WebKit uses that URI to
362 * write to a file... */
363 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
364 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
365 webkit_uri_response_get_uri(resp), p, size_mb);
369 base = g_path_get_basename(filename);
370 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
374 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
379 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
381 struct Client *c = (struct Client *)data;
384 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
389 /* The page has loaded fully. We now run a short JavaScript
390 * snippet that operates on the DOM. It tries to grab all
391 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
393 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
395 grab_feeds_finished, c);
397 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
401 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
404 struct Client *c = (struct Client *)data;
406 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
407 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
409 u = u == NULL ? __NAME__ : u;
410 u = u[0] == 0 ? __NAME__ : u;
412 t = t == NULL ? u : t;
413 t = t[0] == 0 ? u : t;
415 gtk_window_set_title(GTK_WINDOW(c->win), t);
419 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
422 struct Client *c = (struct Client *)data;
425 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
427 /* When a web process crashes, we get a "notify::uri" signal, but we
428 * can no longer read a meaningful URI. It's just an empty string
429 * now. Not updating the location bar in this scenario is important,
430 * because we would override the "WEB PROCESS CRASHED" message. */
431 if (t != NULL && strlen(t) > 0)
433 gtk_entry_set_text(GTK_ENTRY(c->location), t);
435 if (history_file != NULL)
437 fp = fopen(history_file, "a");
440 fprintf(fp, "%s\n", t);
444 perror(__NAME__": Error opening history file");
450 crashed_web_view(WebKitWebView *web_view, gpointer data)
453 struct Client *c = (struct Client *)data;
455 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
456 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
457 gtk_entry_set_text(GTK_ENTRY(c->location), t);
464 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
465 WebKitPolicyDecisionType type, gpointer data)
467 WebKitResponsePolicyDecision *r;
471 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
472 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
473 if (!webkit_response_policy_decision_is_mime_type_supported(r))
474 webkit_policy_decision_download(decision);
476 webkit_policy_decision_use(decision);
479 /* Use whatever default there is. */
486 download_handle_finished(WebKitDownload *download, gpointer data)
492 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
495 g_signal_connect(G_OBJECT(download), "decide-destination",
496 G_CALLBACK(download_handle), data);
500 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
502 gchar *sug_clean, *path, *path2 = NULL, *uri;
507 sug_clean = g_strdup(suggested_filename);
508 for (i = 0; i < strlen(sug_clean); i++)
509 if (sug_clean[i] == G_DIR_SEPARATOR)
512 path = g_build_filename(download_dir, sug_clean, NULL);
513 path2 = g_strdup(path);
514 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
518 path2 = g_strdup_printf("%s.%d", path, suffix);
524 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
525 webkit_download_cancel(download);
529 uri = g_filename_to_uri(path2, NULL, NULL);
530 webkit_download_set_destination(download, uri);
533 tb = gtk_tool_button_new(NULL, NULL);
534 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
535 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
536 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
537 gtk_widget_show_all(dm.win);
539 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
540 G_CALLBACK(changed_download_progress), tb);
543 g_signal_connect(G_OBJECT(download), "finished",
544 G_CALLBACK(download_handle_finished), NULL);
546 g_object_ref(download);
547 g_signal_connect(G_OBJECT(tb), "clicked",
548 G_CALLBACK(downloadmanager_cancel), download);
555 /* Propagate -- to whom it may concern. */
560 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
562 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
564 webkit_download_cancel(download);
565 g_object_unref(download);
567 gtk_widget_destroy(GTK_WIDGET(tb));
571 downloadmanager_delete(GtkWidget *obj, gpointer data)
573 if (!quit_if_nothing_active())
574 gtk_widget_hide(dm.win);
580 downloadmanager_setup(void)
582 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
583 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
584 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
585 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
586 g_signal_connect(G_OBJECT(dm.win), "delete-event",
587 G_CALLBACK(downloadmanager_delete), NULL);
588 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
589 G_CALLBACK(key_downloadmanager), NULL);
591 dm.toolbar = gtk_toolbar_new();
592 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
593 GTK_ORIENTATION_VERTICAL);
594 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
595 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
597 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
598 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
599 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
600 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
602 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
606 ensure_uri_scheme(const gchar *t)
610 f = g_ascii_strdown(t, -1);
611 if (!g_str_has_prefix(f, "http:") &&
612 !g_str_has_prefix(f, "https:") &&
613 !g_str_has_prefix(f, "file:") &&
614 !g_str_has_prefix(f, "about:") &&
615 !g_str_has_prefix(f, "data:"))
618 fabs = realpath(t, NULL);
621 f = g_strdup_printf("file://%s", fabs);
625 f = g_strdup_printf("http://%s", t);
633 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
635 struct Client *c = (struct Client *)data;
636 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
643 argv[2] = c->external_handler_uri;
644 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
647 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
652 g_spawn_close_pid(pid);
655 void feed_icon(gpointer user_data, gchar *feed_html)
657 struct Client *c = (struct Client *)user_data;
659 if (feed_html != NULL)
661 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
662 GTK_ENTRY_ICON_PRIMARY,
663 "application-rss+xml-symbolic");
664 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
665 GTK_ENTRY_ICON_PRIMARY,
668 if (c->feed_html != NULL)
669 g_free(c->feed_html);
670 c->feed_html = g_strdup(feed_html);
674 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
675 GTK_ENTRY_ICON_PRIMARY,
681 grab_environment_configuration(void)
685 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
687 accepted_language[0] = g_strdup(e);
689 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
691 download_dir = g_strdup(e);
693 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
695 enable_console_to_stdout = TRUE;
697 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_EXPERIMENTAL_WEBGL");
701 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
703 fifo_suffix = g_strdup(e);
705 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
707 history_file = g_strdup(e);
709 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
711 home_uri = g_strdup(e);
713 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
715 user_agent = g_strdup(e);
717 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
719 global_zoom = atof(e);
723 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
725 WebKitJavascriptResult *js_result;
727 JSGlobalContextRef context;
729 JSStringRef js_str_value;
733 /* This was taken almost verbatim from the example in WebKit's
736 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
740 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
745 context = webkit_javascript_result_get_global_context(js_result);
746 value = webkit_javascript_result_get_value(js_result);
748 if (JSValueIsString(context, value))
750 js_str_value = JSValueToStringCopy(context, value, NULL);
751 str_length = JSStringGetMaximumUTF8CStringSize(js_str_value);
752 str_value = (gchar *)g_malloc(str_length);
753 JSStringGetUTF8CString(js_str_value, str_value, str_length);
754 JSStringRelease(js_str_value);
755 feed_icon(data, str_value);
759 feed_icon(data, NULL);
761 webkit_javascript_result_unref(js_result);
765 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
768 struct Client *c = (struct Client *)data;
770 if (!gtk_widget_is_focus(c->location))
772 if (webkit_hit_test_result_context_is_link(ht))
774 gtk_entry_set_text(GTK_ENTRY(c->location),
775 webkit_hit_test_result_get_link_uri(ht));
777 if (c->hover_uri != NULL)
778 g_free(c->hover_uri);
779 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
783 gtk_entry_set_text(GTK_ENTRY(c->location),
784 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
786 if (c->hover_uri != NULL)
787 g_free(c->hover_uri);
794 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event, gpointer data)
796 struct Client *c = (struct Client *)data;
799 if (c->feed_html != NULL)
801 /* What we're actually trying to do is show a simple HTML page
802 * that lists all the feeds on the current page. The function
803 * webkit_web_view_load_html() looks like the proper way to do
804 * that. Sad thing is, it doesn't create a history entry, but
805 * instead simply replaces the content of the current page. This
806 * is not what we want.
808 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
809 * mechanism to show my custom HTML snippet *and* create a
812 * [0]: https://tools.ietf.org/html/rfc2397
813 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
814 d = g_strdup_printf("data:text/html,%s%s%s",
818 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
824 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
826 struct Client *c = (struct Client *)data;
827 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
830 if (event->type == GDK_KEY_PRESS)
832 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
834 switch (((GdkEventKey *)event)->keyval)
836 case GDK_KEY_q: /* close window (left hand) */
837 gtk_widget_destroy(c->win);
839 case GDK_KEY_w: /* home (left hand) */
840 f = ensure_uri_scheme(home_uri);
841 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
844 case GDK_KEY_e: /* new tab (left hand) */
845 f = ensure_uri_scheme(home_uri);
846 client_new(f, NULL, TRUE);
849 case GDK_KEY_r: /* reload (left hand) */
850 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
853 case GDK_KEY_d: /* download manager (left hand) */
854 gtk_widget_show_all(dm.win);
856 case GDK_KEY_2: /* search forward (left hand) */
857 case GDK_KEY_n: /* search forward (maybe both hands) */
860 case GDK_KEY_3: /* search backward (left hand) */
863 case GDK_KEY_l: /* location (BOTH hands) */
864 gtk_widget_grab_focus(c->location);
866 case GDK_KEY_k: /* initiate search (BOTH hands) */
867 gtk_widget_grab_focus(c->location);
868 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
869 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
871 case GDK_KEY_c: /* reload trusted certs (left hand) */
872 trust_user_certs(wc);
874 case GDK_KEY_x: /* launch external handler (left hand) */
875 if (c->external_handler_uri != NULL)
876 g_free(c->external_handler_uri);
877 c->external_handler_uri = g_strdup(
878 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
879 external_handler_run(NULL, NULL, c);
883 /* navigate backward (left hand) */
884 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
886 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
889 /* navigate forward (left hand) */
890 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
892 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
901 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
903 if (event->type == GDK_KEY_PRESS)
905 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
907 switch (((GdkEventKey *)event)->keyval)
909 case GDK_KEY_d: /* close window (left hand) */
911 downloadmanager_delete(dm.win, NULL);
921 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
923 struct Client *c = (struct Client *)data;
927 if (key_common(widget, event, data))
930 if (event->type == GDK_KEY_PRESS)
932 switch (((GdkEventKey *)event)->keyval)
934 case GDK_KEY_KP_Enter:
936 gtk_widget_grab_focus(c->web_view);
937 t = gtk_entry_get_text(GTK_ENTRY(c->location));
938 if (t != NULL && t[0] == ':' && t[1] == '/')
940 if (search_text != NULL)
942 search_text = g_strdup(t + 2);
945 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
947 f = ensure_uri_scheme(t);
948 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
953 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
954 gtk_entry_set_text(GTK_ENTRY(c->location),
955 (t == NULL ? __NAME__ : t));
964 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
966 struct Client *c = (struct Client *)data;
970 if (key_common(widget, event, data))
973 if (event->type == GDK_KEY_PRESS)
975 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
977 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
978 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
981 else if (event->type == GDK_BUTTON_RELEASE)
983 switch (((GdkEventButton *)event)->button)
986 if (c->hover_uri != NULL)
988 client_new(c->hover_uri, NULL, TRUE);
993 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
996 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
1000 else if (event->type == GDK_SCROLL)
1002 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
1003 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
1005 gdk_event_get_scroll_deltas(event, &dx, &dy);
1006 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1008 z = dx != 0 ? global_zoom : z;
1009 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1021 GIOChannel *channel = NULL;
1022 gchar *path = NULL, *buf = NULL;
1023 gchar **tokens = NULL;
1025 keywords = g_hash_table_new(g_str_hash, g_str_equal);
1027 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1029 channel = g_io_channel_new_file(path, "r", &err);
1030 if (channel != NULL)
1032 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1033 == G_IO_STATUS_NORMAL)
1038 tokens = g_strsplit(buf, " ", 2);
1039 if (tokens[0] != NULL && tokens[1] != NULL)
1040 g_hash_table_insert(keywords, g_strdup(tokens[0]),
1041 g_strdup(tokens[1]));
1046 g_io_channel_shutdown(channel, FALSE, NULL);
1052 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1054 gboolean ret = FALSE;
1055 gchar **tokens = NULL;
1056 gchar *val = NULL, *escaped = NULL, *uri = NULL;
1058 tokens = g_strsplit(t, " ", 2);
1059 if (tokens[0] != NULL && tokens[1] != NULL)
1061 val = g_hash_table_lookup(keywords, tokens[0]);
1064 escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1065 uri = g_strdup_printf((gchar *)val, escaped);
1066 webkit_web_view_load_uri(web_view, uri);
1078 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1079 WebKitHitTestResult *ht, gpointer data)
1081 struct Client *c = (struct Client *)data;
1082 GSimpleAction *action = NULL;
1083 WebKitContextMenuItem *mi = NULL;
1084 const gchar *uri = NULL;
1088 if (webkit_hit_test_result_context_is_link(ht))
1089 uri = webkit_hit_test_result_get_link_uri(ht);
1090 else if (webkit_hit_test_result_context_is_image(ht))
1091 uri = webkit_hit_test_result_get_image_uri(ht);
1092 else if (webkit_hit_test_result_context_is_media(ht))
1093 uri = webkit_hit_test_result_get_media_uri(ht);
1097 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1099 if (c->external_handler_uri != NULL)
1100 g_free(c->external_handler_uri);
1101 c->external_handler_uri = g_strdup(uri);
1102 action = g_simple_action_new("external_handler", NULL);
1103 g_signal_connect(G_OBJECT(action), "activate",
1104 G_CALLBACK(external_handler_run), data);
1105 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1106 "Open with external handler",
1108 webkit_context_menu_append(menu, mi);
1109 g_object_unref(action);
1112 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1117 quit_if_nothing_active(void)
1127 gtk_widget_show_all(dm.win);
1134 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1138 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1142 client_new(uri, NULL, TRUE);
1149 search(gpointer data, gint direction)
1151 struct Client *c = (struct Client *)data;
1152 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1153 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1155 if (search_text == NULL)
1161 webkit_find_controller_search(fc, search_text,
1162 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1163 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1167 webkit_find_controller_search_next(fc);
1170 webkit_find_controller_search_previous(fc);
1176 show_web_view(WebKitWebView *web_view, gpointer data)
1178 struct Client *c = (struct Client *)data;
1182 gtk_widget_grab_focus(c->web_view);
1183 gtk_widget_show_all(c->win);
1190 GIOChannel *tabbed_stdout_channel;
1192 gchar *output = NULL;
1193 char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
1196 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
1197 NULL, NULL, NULL, &tabbed_stdout, NULL,
1200 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
1205 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
1206 if (tabbed_stdout_channel == NULL)
1208 fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
1211 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
1212 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
1215 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
1219 plug_into = strtol(output, NULL, 16);
1222 fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
1227 trust_user_certs(WebKitWebContext *wc)
1229 GTlsCertificate *cert;
1230 const gchar *basedir, *file, *absfile;
1233 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1234 dir = g_dir_open(basedir, 0, NULL);
1237 file = g_dir_read_name(dir);
1238 while (file != NULL)
1240 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1242 cert = g_tls_certificate_new_from_file(absfile, NULL);
1244 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1246 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1247 file = g_dir_read_name(dir);
1255 main(int argc, char **argv)
1260 gtk_init(&argc, &argv);
1261 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1262 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1264 grab_environment_configuration();
1266 while ((opt = getopt(argc, argv, "e:CT")) != -1)
1271 embed = atol(optarg);
1272 tabbed_automagic = FALSE;
1275 cooperative_instances = FALSE;
1278 tabbed_automagic = FALSE;
1281 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1287 if (cooperative_instances)
1288 cooperation_setup();
1289 downloadmanager_setup();
1291 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1292 embed = tabbed_launch();
1294 if (!cooperative_instances || cooperative_alone)
1296 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1298 webkit_web_context_set_web_extensions_directory(
1299 webkit_web_context_get_default(), c
1304 client_new(home_uri, NULL, TRUE);
1307 for (i = optind; i < argc; i++)
1308 client_new(argv[i], NULL, TRUE);
1311 if (!cooperative_instances || cooperative_alone)