11 #include <gdk/gdkkeysyms.h>
13 #include <webkit2/webkit2.h>
14 #include <JavaScriptCore/JavaScript.h>
17 static gboolean button_tablabel(GtkWidget *, GdkEvent *, gpointer);
18 static void client_destroy(GtkWidget *, 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 void mainwindow_setup(void);
48 static void mainwindow_title(gint);
49 static void mainwindow_title_before(GtkNotebook *, GtkWidget *, guint, gpointer);
50 static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
51 WebKitHitTestResult *, gpointer);
52 static gboolean quit_if_nothing_active(void);
53 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
54 static void run_user_scripts(WebKitWebView *);
55 static void search(gpointer, gint);
56 static void show_web_view(WebKitWebView *, gpointer);
57 static void trust_user_certs(WebKitWebContext *);
62 gchar *external_handler_uri;
77 struct DownloadManager
85 static const gchar *accepted_language[2] = { NULL, NULL };
86 static gint clients = 0, downloads = 0;
87 static gboolean cooperative_alone = TRUE;
88 static gboolean cooperative_instances = TRUE;
89 static int cooperative_pipe_fp = 0;
90 static gchar *download_dir = "/var/tmp";
91 static gboolean enable_console_to_stdout = FALSE;
92 static gchar *fifo_suffix = "main";
93 static gdouble global_zoom = 1.0;
94 static gchar *history_file = NULL;
95 static gchar *home_uri = "about:blank";
96 static gboolean initial_wc_setup_done = FALSE;
97 static GHashTable *keywords = NULL;
98 static gchar *search_text = NULL;
99 static GtkPositionType tab_pos = GTK_POS_TOP;
100 static gint tab_width_chars = 20;
101 static gchar *user_agent = NULL;
105 button_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
107 if (event->type == GDK_BUTTON_RELEASE)
109 switch (((GdkEventButton *)event)->button)
112 client_destroy(NULL, data);
120 client_destroy(GtkWidget *widget, gpointer data)
122 struct Client *c = (struct Client *)data;
125 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
126 changed_load_progress, c);
128 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
130 fprintf(stderr, __NAME__": Tab index was -1, bamboozled\n");
132 gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
137 quit_if_nothing_active();
141 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
144 WebKitWebContext *wc;
148 if (uri != NULL && cooperative_instances && !cooperative_alone)
150 f = ensure_uri_scheme(uri);
151 write(cooperative_pipe_fp, f, strlen(f));
152 write(cooperative_pipe_fp, "\n", 1);
157 c = calloc(1, sizeof(struct Client));
160 fprintf(stderr, __NAME__": fatal: calloc failed\n");
164 if (related_wv == NULL)
165 c->web_view = webkit_web_view_new();
167 c->web_view = webkit_web_view_new_with_related_view(related_wv);
168 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
170 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
171 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
172 G_CALLBACK(changed_title), c);
173 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
174 G_CALLBACK(changed_uri), c);
175 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
176 G_CALLBACK(changed_load_progress), c);
177 g_signal_connect(G_OBJECT(c->web_view), "create",
178 G_CALLBACK(client_new_request), NULL);
179 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
180 G_CALLBACK(menu_web_view), c);
181 g_signal_connect(G_OBJECT(c->web_view), "close",
182 G_CALLBACK(client_destroy), c);
183 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
184 G_CALLBACK(decide_policy), NULL);
185 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
186 G_CALLBACK(key_web_view), c);
187 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
188 G_CALLBACK(key_web_view), c);
189 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
190 G_CALLBACK(key_web_view), c);
191 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
192 G_CALLBACK(hover_web_view), c);
193 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
194 G_CALLBACK(crashed_web_view), c);
196 if (!initial_wc_setup_done)
198 if (accepted_language[0] != NULL)
199 webkit_web_context_set_preferred_languages(wc, accepted_language);
201 g_signal_connect(G_OBJECT(wc), "download-started",
202 G_CALLBACK(download_handle_start), NULL);
204 trust_user_certs(wc);
206 initial_wc_setup_done = TRUE;
209 if (user_agent != NULL)
210 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
211 "user-agent", user_agent, NULL);
213 if (enable_console_to_stdout)
214 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
216 webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
218 c->location = gtk_entry_new();
219 g_signal_connect(G_OBJECT(c->location), "key-press-event",
220 G_CALLBACK(key_location), c);
221 g_signal_connect(G_OBJECT(c->location), "icon-release",
222 G_CALLBACK(icon_location), c);
223 /* XXX This is a workaround. Setting this to NULL (which is done in
224 * grab_feeds_finished() if no feed has been detected) adds a little
225 * padding left of the text. Not sure why. The point of this call
226 * right here is to have that padding right from the start. This
227 * avoids a graphical artifact. */
228 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
229 GTK_ENTRY_ICON_PRIMARY,
232 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
233 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
234 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
236 c->tablabel = gtk_label_new(__NAME__);
237 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
238 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), tab_width_chars);
240 evbox = gtk_event_box_new();
241 gtk_container_add(GTK_CONTAINER(evbox), c->tablabel);
242 g_signal_connect(G_OBJECT(evbox), "button-release-event",
243 G_CALLBACK(button_tablabel), c);
245 /* This only shows the event box and the label inside, nothing else.
246 * Needed because the evbox/label is "internal" to the notebook and
247 * not part of the normal "widget tree" (IIUC). */
248 gtk_widget_show_all(evbox);
250 gtk_notebook_append_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox);
251 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
254 show_web_view(NULL, c);
256 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
257 G_CALLBACK(show_web_view), c);
261 f = ensure_uri_scheme(uri);
262 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
268 return WEBKIT_WEB_VIEW(c->web_view);
272 client_new_request(WebKitWebView *web_view,
273 WebKitNavigationAction *navigation_action, gpointer data)
275 return client_new(NULL, web_view, FALSE);
279 cooperation_setup(void)
282 gchar *fifofilename, *fifopath;
284 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
285 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
286 g_free(fifofilename);
288 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
289 mkfifo(fifopath, 0600);
291 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
292 if (!cooperative_pipe_fp)
294 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
298 if (write(cooperative_pipe_fp, "", 0) == -1)
300 /* Could not do an empty write to the FIFO which means there's
301 * no one listening. */
302 close(cooperative_pipe_fp);
303 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
304 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
307 cooperative_alone = FALSE;
314 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
316 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
317 WebKitURIResponse *resp;
318 GtkToolItem *tb = GTK_TOOL_ITEM(data);
321 gchar *t, *filename, *base;
323 p = webkit_download_get_estimated_progress(download);
327 resp = webkit_download_get_response(download);
328 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
330 uri = webkit_download_get_destination(download);
331 filename = g_filename_from_uri(uri, NULL, NULL);
332 if (filename == NULL)
334 /* This really should not happen because WebKit uses that URI to
335 * write to a file... */
336 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
337 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
338 webkit_uri_response_get_uri(resp), p, size_mb);
342 base = g_path_get_basename(filename);
343 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
347 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
352 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
354 struct Client *c = (struct Client *)data;
357 "a = document.querySelectorAll('"
358 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
359 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
366 " for (i = 0; i < a.length; i++)"
368 " url = encodeURIComponent(a[i].href);"
369 " if ('title' in a[i] && a[i].title != '')"
370 " title = encodeURIComponent(a[i].title);"
373 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
378 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
383 /* The page has loaded fully. We now run the short JavaScript
384 * snippet above that operates on the DOM. It tries to grab all
385 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
387 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
389 grab_feeds_finished, c);
391 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
393 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
397 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
400 struct Client *c = (struct Client *)data;
402 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
403 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
405 u = u == NULL ? __NAME__ : u;
406 u = u[0] == 0 ? __NAME__ : u;
408 t = t == NULL ? u : t;
409 t = t[0] == 0 ? u : t;
411 gtk_label_set_text(GTK_LABEL(c->tablabel), t);
412 mainwindow_title(-1);
416 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
419 struct Client *c = (struct Client *)data;
422 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
424 /* When a web process crashes, we get a "notify::uri" signal, but we
425 * can no longer read a meaningful URI. It's just an empty string
426 * now. Not updating the location bar in this scenario is important,
427 * because we would override the "WEB PROCESS CRASHED" message. */
428 if (t != NULL && strlen(t) > 0)
430 gtk_entry_set_text(GTK_ENTRY(c->location), t);
432 if (history_file != NULL)
434 fp = fopen(history_file, "a");
437 fprintf(fp, "%s\n", t);
441 perror(__NAME__": Error opening history file");
447 crashed_web_view(WebKitWebView *web_view, gpointer data)
450 struct Client *c = (struct Client *)data;
452 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
453 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
454 gtk_entry_set_text(GTK_ENTRY(c->location), t);
461 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
462 WebKitPolicyDecisionType type, gpointer data)
464 WebKitResponsePolicyDecision *r;
468 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
469 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
470 if (!webkit_response_policy_decision_is_mime_type_supported(r))
471 webkit_policy_decision_download(decision);
473 webkit_policy_decision_use(decision);
476 /* Use whatever default there is. */
483 download_handle_finished(WebKitDownload *download, gpointer data)
489 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
492 g_signal_connect(G_OBJECT(download), "decide-destination",
493 G_CALLBACK(download_handle), data);
497 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
499 gchar *sug_clean, *path, *path2 = NULL, *uri;
504 sug_clean = g_strdup(suggested_filename);
505 for (i = 0; i < strlen(sug_clean); i++)
506 if (sug_clean[i] == G_DIR_SEPARATOR)
509 path = g_build_filename(download_dir, sug_clean, NULL);
510 path2 = g_strdup(path);
511 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
515 path2 = g_strdup_printf("%s.%d", path, suffix);
521 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
522 webkit_download_cancel(download);
526 uri = g_filename_to_uri(path2, NULL, NULL);
527 webkit_download_set_destination(download, uri);
530 tb = gtk_tool_button_new(NULL, NULL);
531 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
532 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
533 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
534 gtk_widget_show_all(dm.win);
536 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
537 G_CALLBACK(changed_download_progress), tb);
540 g_signal_connect(G_OBJECT(download), "finished",
541 G_CALLBACK(download_handle_finished), NULL);
543 g_object_ref(download);
544 g_signal_connect(G_OBJECT(tb), "clicked",
545 G_CALLBACK(downloadmanager_cancel), download);
552 /* Propagate -- to whom it may concern. */
557 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
559 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
561 webkit_download_cancel(download);
562 g_object_unref(download);
564 gtk_widget_destroy(GTK_WIDGET(tb));
568 downloadmanager_delete(GtkWidget *obj, gpointer data)
570 if (!quit_if_nothing_active())
571 gtk_widget_hide(dm.win);
577 downloadmanager_setup(void)
579 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
580 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
581 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
582 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
583 g_signal_connect(G_OBJECT(dm.win), "delete-event",
584 G_CALLBACK(downloadmanager_delete), NULL);
585 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
586 G_CALLBACK(key_downloadmanager), NULL);
588 dm.toolbar = gtk_toolbar_new();
589 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
590 GTK_ORIENTATION_VERTICAL);
591 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
592 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
594 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
595 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
596 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
597 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
599 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
603 ensure_uri_scheme(const gchar *t)
607 f = g_ascii_strdown(t, -1);
608 if (!g_str_has_prefix(f, "http:") &&
609 !g_str_has_prefix(f, "https:") &&
610 !g_str_has_prefix(f, "file:") &&
611 !g_str_has_prefix(f, "about:") &&
612 !g_str_has_prefix(f, "data:"))
615 fabs = realpath(t, NULL);
618 f = g_strdup_printf("file://%s", fabs);
622 f = g_strdup_printf("http://%s", t);
630 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
632 struct Client *c = (struct Client *)data;
633 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
640 argv[2] = c->external_handler_uri;
641 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
644 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
649 g_spawn_close_pid(pid);
653 grab_environment_configuration(void)
657 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
659 accepted_language[0] = g_strdup(e);
661 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
663 download_dir = g_strdup(e);
665 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
667 enable_console_to_stdout = TRUE;
669 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
671 fifo_suffix = g_strdup(e);
673 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
675 history_file = g_strdup(e);
677 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
679 home_uri = g_strdup(e);
681 e = g_getenv(__NAME_UPPERCASE__"_TAB_POS");
684 if (strcmp(e, "top") == 0)
685 tab_pos = GTK_POS_TOP;
686 if (strcmp(e, "right") == 0)
687 tab_pos = GTK_POS_RIGHT;
688 if (strcmp(e, "bottom") == 0)
689 tab_pos = GTK_POS_BOTTOM;
690 if (strcmp(e, "left") == 0)
691 tab_pos = GTK_POS_LEFT;
694 e = g_getenv(__NAME_UPPERCASE__"_TAB_WIDTH_CHARS");
696 tab_width_chars = atoi(e);
698 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
700 user_agent = g_strdup(e);
702 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
704 global_zoom = atof(e);
708 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
710 struct Client *c = (struct Client *)data;
711 WebKitJavascriptResult *js_result;
713 JSCException *exception;
717 g_free(c->feed_html);
720 /* This was taken almost verbatim from the example in WebKit's
723 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
725 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
729 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
734 value = webkit_javascript_result_get_js_value(js_result);
735 if (jsc_value_is_string(value))
737 str_value = jsc_value_to_string(value);
738 exception = jsc_context_get_exception(jsc_value_get_context(value));
739 if (exception != NULL)
741 fprintf(stderr, __NAME__": Error running javascript: %s\n",
742 jsc_exception_get_message(exception));
745 c->feed_html = str_value;
747 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
748 GTK_ENTRY_ICON_PRIMARY,
749 "application-rss+xml-symbolic");
750 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
751 GTK_ENTRY_ICON_PRIMARY,
756 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
757 GTK_ENTRY_ICON_PRIMARY,
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,
797 struct Client *c = (struct Client *)data;
799 gchar *data_template =
804 " <meta charset=\"UTF-8\">"
805 " <title>Feeds</title>"
808 " <p>Feeds found on this page:</p>"
815 if (c->feed_html != NULL)
817 /* What we're actually trying to do is show a simple HTML page
818 * that lists all the feeds on the current page. The function
819 * webkit_web_view_load_html() looks like the proper way to do
820 * that. Sad thing is, it doesn't create a history entry, but
821 * instead simply replaces the content of the current page. This
822 * is not what we want.
824 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
825 * mechanism to show my custom HTML snippet *and* create a
828 * [0]: https://tools.ietf.org/html/rfc2397
829 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
830 d = g_strdup_printf(data_template, c->feed_html);
831 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
837 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
839 struct Client *c = (struct Client *)data;
840 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
843 if (event->type == GDK_KEY_PRESS)
845 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
847 switch (((GdkEventKey *)event)->keyval)
849 case GDK_KEY_q: /* close window (left hand) */
850 client_destroy(NULL, c);
852 case GDK_KEY_w: /* home (left hand) */
853 f = ensure_uri_scheme(home_uri);
854 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
857 case GDK_KEY_e: /* new tab (left hand) */
858 f = ensure_uri_scheme(home_uri);
859 client_new(f, NULL, TRUE);
862 case GDK_KEY_r: /* reload (left hand) */
863 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
866 case GDK_KEY_d: /* download manager (left hand) */
867 gtk_widget_show_all(dm.win);
869 case GDK_KEY_2: /* search forward (left hand) */
870 case GDK_KEY_n: /* search forward (maybe both hands) */
873 case GDK_KEY_3: /* search backward (left hand) */
876 case GDK_KEY_l: /* location (BOTH hands) */
877 gtk_widget_grab_focus(c->location);
879 case GDK_KEY_k: /* initiate search (BOTH hands) */
880 gtk_widget_grab_focus(c->location);
881 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
882 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
884 case GDK_KEY_c: /* reload trusted certs (left hand) */
885 trust_user_certs(wc);
887 case GDK_KEY_x: /* launch external handler (left hand) */
888 if (c->external_handler_uri != NULL)
889 g_free(c->external_handler_uri);
890 c->external_handler_uri = g_strdup(
891 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
892 external_handler_run(NULL, NULL, c);
894 case GDK_KEY_a: /* go one tab to the left (left hand) */
895 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
897 case GDK_KEY_s: /* go one tab to the right (left hand) */
898 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
902 /* navigate backward (left hand) */
903 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
905 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
908 /* navigate forward (left hand) */
909 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
911 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
920 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
922 if (event->type == GDK_KEY_PRESS)
924 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
926 switch (((GdkEventKey *)event)->keyval)
928 case GDK_KEY_d: /* close window (left hand) */
930 downloadmanager_delete(dm.win, NULL);
940 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
942 struct Client *c = (struct Client *)data;
946 if (key_common(widget, event, data))
949 if (event->type == GDK_KEY_PRESS)
951 switch (((GdkEventKey *)event)->keyval)
953 case GDK_KEY_KP_Enter:
955 gtk_widget_grab_focus(c->web_view);
956 t = gtk_entry_get_text(GTK_ENTRY(c->location));
957 if (t != NULL && t[0] == ':' && t[1] == '/')
959 if (search_text != NULL)
961 search_text = g_strdup(t + 2);
964 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
966 f = ensure_uri_scheme(t);
967 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
972 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
973 gtk_entry_set_text(GTK_ENTRY(c->location),
974 (t == NULL ? __NAME__ : t));
983 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
985 struct Client *c = (struct Client *)data;
989 if (key_common(widget, event, data))
992 if (event->type == GDK_KEY_PRESS)
994 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
996 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
997 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
1000 else if (event->type == GDK_BUTTON_RELEASE)
1002 switch (((GdkEventButton *)event)->button)
1005 if (c->hover_uri != NULL)
1007 client_new(c->hover_uri, NULL, TRUE);
1012 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1015 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
1019 else if (event->type == GDK_SCROLL)
1021 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
1022 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
1024 gdk_event_get_scroll_deltas(event, &dx, &dy);
1025 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1027 z = dx != 0 ? global_zoom : z;
1028 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1040 GIOChannel *channel = NULL;
1041 gchar *path = NULL, *buf = NULL;
1042 gchar **tokens = NULL;
1044 keywords = g_hash_table_new(g_str_hash, g_str_equal);
1046 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1048 channel = g_io_channel_new_file(path, "r", &err);
1049 if (channel != NULL)
1051 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1052 == G_IO_STATUS_NORMAL)
1057 tokens = g_strsplit(buf, " ", 2);
1058 if (tokens[0] != NULL && tokens[1] != NULL)
1059 g_hash_table_insert(keywords, g_strdup(tokens[0]),
1060 g_strdup(tokens[1]));
1065 g_io_channel_shutdown(channel, FALSE, NULL);
1071 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1073 gboolean ret = FALSE;
1074 gchar **tokens = NULL;
1075 gchar *val = NULL, *escaped = NULL, *uri = NULL;
1077 tokens = g_strsplit(t, " ", 2);
1078 if (tokens[0] != NULL && tokens[1] != NULL)
1080 val = g_hash_table_lookup(keywords, tokens[0]);
1083 escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1084 uri = g_strdup_printf((gchar *)val, escaped);
1085 webkit_web_view_load_uri(web_view, uri);
1097 mainwindow_setup(void)
1099 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1100 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1101 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1102 gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
1104 mw.notebook = gtk_notebook_new();
1105 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1106 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), tab_pos);
1107 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1108 g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
1109 G_CALLBACK(mainwindow_title_before), NULL);
1113 mainwindow_title_before(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
1115 mainwindow_title(idx);
1119 mainwindow_title(gint idx)
1121 GtkWidget *child, *evbox, *label;
1126 idx = gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook));
1131 child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
1135 evbox = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
1136 label = gtk_bin_get_child(GTK_BIN(evbox));
1137 text = gtk_label_get_text(GTK_LABEL(label));
1138 gtk_window_set_title(GTK_WINDOW(mw.win), text);
1142 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1143 WebKitHitTestResult *ht, gpointer data)
1145 struct Client *c = (struct Client *)data;
1146 GSimpleAction *action = NULL;
1147 WebKitContextMenuItem *mi = NULL;
1148 const gchar *uri = NULL;
1152 if (webkit_hit_test_result_context_is_link(ht))
1153 uri = webkit_hit_test_result_get_link_uri(ht);
1154 else if (webkit_hit_test_result_context_is_image(ht))
1155 uri = webkit_hit_test_result_get_image_uri(ht);
1156 else if (webkit_hit_test_result_context_is_media(ht))
1157 uri = webkit_hit_test_result_get_media_uri(ht);
1161 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1163 if (c->external_handler_uri != NULL)
1164 g_free(c->external_handler_uri);
1165 c->external_handler_uri = g_strdup(uri);
1166 action = g_simple_action_new("external_handler", NULL);
1167 g_signal_connect(G_OBJECT(action), "activate",
1168 G_CALLBACK(external_handler_run), data);
1169 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1170 "Open with external handler",
1172 webkit_context_menu_append(menu, mi);
1173 g_object_unref(action);
1176 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1181 quit_if_nothing_active(void)
1191 gtk_widget_show_all(dm.win);
1198 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1202 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1206 client_new(uri, NULL, TRUE);
1213 run_user_scripts(WebKitWebView *web_view)
1215 gchar *base = NULL, *path = NULL, *contents = NULL;
1216 const gchar *entry = NULL;
1217 GDir *scriptdir = NULL;
1219 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts", NULL);
1220 scriptdir = g_dir_open(base, 0, NULL);
1221 if (scriptdir != NULL)
1223 while ((entry = g_dir_read_name(scriptdir)) != NULL)
1225 path = g_build_filename(base, entry, NULL);
1226 if (g_str_has_suffix(path, ".js"))
1228 if (g_file_get_contents(path, &contents, NULL, NULL))
1230 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1236 g_dir_close(scriptdir);
1243 search(gpointer data, gint direction)
1245 struct Client *c = (struct Client *)data;
1246 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1247 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1249 if (search_text == NULL)
1255 webkit_find_controller_search(fc, search_text,
1256 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1257 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1261 webkit_find_controller_search_next(fc);
1264 webkit_find_controller_search_previous(fc);
1270 show_web_view(WebKitWebView *web_view, gpointer data)
1272 struct Client *c = (struct Client *)data;
1277 gtk_widget_show_all(mw.win);
1279 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1281 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
1283 gtk_widget_grab_focus(c->web_view);
1287 trust_user_certs(WebKitWebContext *wc)
1289 GTlsCertificate *cert;
1290 const gchar *basedir, *file, *absfile;
1293 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1294 dir = g_dir_open(basedir, 0, NULL);
1297 file = g_dir_read_name(dir);
1298 while (file != NULL)
1300 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1302 cert = g_tls_certificate_new_from_file(absfile, NULL);
1304 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1306 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1307 file = g_dir_read_name(dir);
1315 main(int argc, char **argv)
1320 gtk_init(&argc, &argv);
1321 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1322 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1324 grab_environment_configuration();
1326 while ((opt = getopt(argc, argv, "C")) != -1)
1331 cooperative_instances = FALSE;
1334 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1340 if (cooperative_instances)
1341 cooperation_setup();
1342 downloadmanager_setup();
1346 if (!cooperative_instances || cooperative_alone)
1348 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1350 webkit_web_context_set_web_extensions_directory(
1351 webkit_web_context_get_default(), c
1356 client_new(home_uri, NULL, TRUE);
1359 for (i = optind; i < argc; i++)
1360 client_new(argv[i], NULL, TRUE);
1363 if (!cooperative_instances || cooperative_alone)