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 gchar *user_agent = NULL;
104 button_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
106 if (event->type == GDK_BUTTON_RELEASE)
108 switch (((GdkEventButton *)event)->button)
111 client_destroy(NULL, data);
119 client_destroy(GtkWidget *widget, gpointer data)
121 struct Client *c = (struct Client *)data;
124 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
125 changed_load_progress, c);
127 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
129 fprintf(stderr, __NAME__": Tab index was -1, bamboozled\n");
131 gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
136 quit_if_nothing_active();
140 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
143 WebKitWebContext *wc;
147 if (uri != NULL && cooperative_instances && !cooperative_alone)
149 f = ensure_uri_scheme(uri);
150 write(cooperative_pipe_fp, f, strlen(f));
151 write(cooperative_pipe_fp, "\n", 1);
156 c = calloc(1, sizeof(struct Client));
159 fprintf(stderr, __NAME__": fatal: calloc failed\n");
163 if (related_wv == NULL)
164 c->web_view = webkit_web_view_new();
166 c->web_view = webkit_web_view_new_with_related_view(related_wv);
167 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
169 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
170 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
171 G_CALLBACK(changed_title), c);
172 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
173 G_CALLBACK(changed_uri), c);
174 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
175 G_CALLBACK(changed_load_progress), c);
176 g_signal_connect(G_OBJECT(c->web_view), "create",
177 G_CALLBACK(client_new_request), NULL);
178 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
179 G_CALLBACK(menu_web_view), c);
180 g_signal_connect(G_OBJECT(c->web_view), "close",
181 G_CALLBACK(client_destroy), c);
182 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
183 G_CALLBACK(decide_policy), NULL);
184 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
185 G_CALLBACK(key_web_view), c);
186 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
187 G_CALLBACK(key_web_view), c);
188 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
189 G_CALLBACK(key_web_view), c);
190 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
191 G_CALLBACK(hover_web_view), c);
192 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
193 G_CALLBACK(crashed_web_view), c);
195 if (!initial_wc_setup_done)
197 if (accepted_language[0] != NULL)
198 webkit_web_context_set_preferred_languages(wc, accepted_language);
200 g_signal_connect(G_OBJECT(wc), "download-started",
201 G_CALLBACK(download_handle_start), NULL);
203 trust_user_certs(wc);
205 initial_wc_setup_done = TRUE;
208 if (user_agent != NULL)
209 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
210 "user-agent", user_agent, NULL);
212 if (enable_console_to_stdout)
213 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
215 webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
217 c->location = gtk_entry_new();
218 g_signal_connect(G_OBJECT(c->location), "key-press-event",
219 G_CALLBACK(key_location), c);
220 g_signal_connect(G_OBJECT(c->location), "icon-release",
221 G_CALLBACK(icon_location), c);
222 /* XXX This is a workaround. Setting this to NULL (which is done in
223 * grab_feeds_finished() if no feed has been detected) adds a little
224 * padding left of the text. Not sure why. The point of this call
225 * right here is to have that padding right from the start. This
226 * avoids a graphical artifact. */
227 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
228 GTK_ENTRY_ICON_PRIMARY,
231 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
232 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
233 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
235 c->tablabel = gtk_label_new(__NAME__);
236 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
237 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), 20);
239 evbox = gtk_event_box_new();
240 gtk_container_add(GTK_CONTAINER(evbox), c->tablabel);
241 g_signal_connect(G_OBJECT(evbox), "button-release-event",
242 G_CALLBACK(button_tablabel), c);
244 /* This only shows the event box and the label inside, nothing else.
245 * Needed because the evbox/label is "internal" to the notebook and
246 * not part of the normal "widget tree" (IIUC). */
247 gtk_widget_show_all(evbox);
249 gtk_notebook_append_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox);
250 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
253 show_web_view(NULL, c);
255 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
256 G_CALLBACK(show_web_view), c);
260 f = ensure_uri_scheme(uri);
261 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
267 return WEBKIT_WEB_VIEW(c->web_view);
271 client_new_request(WebKitWebView *web_view,
272 WebKitNavigationAction *navigation_action, gpointer data)
274 return client_new(NULL, web_view, FALSE);
278 cooperation_setup(void)
281 gchar *fifofilename, *fifopath;
283 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
284 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
285 g_free(fifofilename);
287 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
288 mkfifo(fifopath, 0600);
290 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
291 if (!cooperative_pipe_fp)
293 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
297 if (write(cooperative_pipe_fp, "", 0) == -1)
299 /* Could not do an empty write to the FIFO which means there's
300 * no one listening. */
301 close(cooperative_pipe_fp);
302 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
303 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
306 cooperative_alone = FALSE;
313 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
315 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
316 WebKitURIResponse *resp;
317 GtkToolItem *tb = GTK_TOOL_ITEM(data);
320 gchar *t, *filename, *base;
322 p = webkit_download_get_estimated_progress(download);
326 resp = webkit_download_get_response(download);
327 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
329 uri = webkit_download_get_destination(download);
330 filename = g_filename_from_uri(uri, NULL, NULL);
331 if (filename == NULL)
333 /* This really should not happen because WebKit uses that URI to
334 * write to a file... */
335 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
336 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
337 webkit_uri_response_get_uri(resp), p, size_mb);
341 base = g_path_get_basename(filename);
342 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
346 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
351 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
353 struct Client *c = (struct Client *)data;
356 "a = document.querySelectorAll('"
357 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
358 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
365 " for (i = 0; i < a.length; i++)"
367 " url = encodeURIComponent(a[i].href);"
368 " if ('title' in a[i] && a[i].title != '')"
369 " title = encodeURIComponent(a[i].title);"
372 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
377 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
382 /* The page has loaded fully. We now run the short JavaScript
383 * snippet above that operates on the DOM. It tries to grab all
384 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
386 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
388 grab_feeds_finished, c);
390 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
392 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
396 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
399 struct Client *c = (struct Client *)data;
401 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
402 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
404 u = u == NULL ? __NAME__ : u;
405 u = u[0] == 0 ? __NAME__ : u;
407 t = t == NULL ? u : t;
408 t = t[0] == 0 ? u : t;
410 gtk_label_set_text(GTK_LABEL(c->tablabel), t);
411 mainwindow_title(-1);
415 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
418 struct Client *c = (struct Client *)data;
421 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
423 /* When a web process crashes, we get a "notify::uri" signal, but we
424 * can no longer read a meaningful URI. It's just an empty string
425 * now. Not updating the location bar in this scenario is important,
426 * because we would override the "WEB PROCESS CRASHED" message. */
427 if (t != NULL && strlen(t) > 0)
429 gtk_entry_set_text(GTK_ENTRY(c->location), t);
431 if (history_file != NULL)
433 fp = fopen(history_file, "a");
436 fprintf(fp, "%s\n", t);
440 perror(__NAME__": Error opening history file");
446 crashed_web_view(WebKitWebView *web_view, gpointer data)
449 struct Client *c = (struct Client *)data;
451 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
452 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
453 gtk_entry_set_text(GTK_ENTRY(c->location), t);
460 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
461 WebKitPolicyDecisionType type, gpointer data)
463 WebKitResponsePolicyDecision *r;
467 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
468 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
469 if (!webkit_response_policy_decision_is_mime_type_supported(r))
470 webkit_policy_decision_download(decision);
472 webkit_policy_decision_use(decision);
475 /* Use whatever default there is. */
482 download_handle_finished(WebKitDownload *download, gpointer data)
488 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
491 g_signal_connect(G_OBJECT(download), "decide-destination",
492 G_CALLBACK(download_handle), data);
496 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
498 gchar *sug_clean, *path, *path2 = NULL, *uri;
503 sug_clean = g_strdup(suggested_filename);
504 for (i = 0; i < strlen(sug_clean); i++)
505 if (sug_clean[i] == G_DIR_SEPARATOR)
508 path = g_build_filename(download_dir, sug_clean, NULL);
509 path2 = g_strdup(path);
510 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
514 path2 = g_strdup_printf("%s.%d", path, suffix);
520 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
521 webkit_download_cancel(download);
525 uri = g_filename_to_uri(path2, NULL, NULL);
526 webkit_download_set_destination(download, uri);
529 tb = gtk_tool_button_new(NULL, NULL);
530 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
531 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
532 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
533 gtk_widget_show_all(dm.win);
535 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
536 G_CALLBACK(changed_download_progress), tb);
539 g_signal_connect(G_OBJECT(download), "finished",
540 G_CALLBACK(download_handle_finished), NULL);
542 g_object_ref(download);
543 g_signal_connect(G_OBJECT(tb), "clicked",
544 G_CALLBACK(downloadmanager_cancel), download);
551 /* Propagate -- to whom it may concern. */
556 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
558 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
560 webkit_download_cancel(download);
561 g_object_unref(download);
563 gtk_widget_destroy(GTK_WIDGET(tb));
567 downloadmanager_delete(GtkWidget *obj, gpointer data)
569 if (!quit_if_nothing_active())
570 gtk_widget_hide(dm.win);
576 downloadmanager_setup(void)
578 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
579 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
580 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
581 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
582 g_signal_connect(G_OBJECT(dm.win), "delete-event",
583 G_CALLBACK(downloadmanager_delete), NULL);
584 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
585 G_CALLBACK(key_downloadmanager), NULL);
587 dm.toolbar = gtk_toolbar_new();
588 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
589 GTK_ORIENTATION_VERTICAL);
590 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
591 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
593 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
594 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
595 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
596 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
598 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
602 ensure_uri_scheme(const gchar *t)
606 f = g_ascii_strdown(t, -1);
607 if (!g_str_has_prefix(f, "http:") &&
608 !g_str_has_prefix(f, "https:") &&
609 !g_str_has_prefix(f, "file:") &&
610 !g_str_has_prefix(f, "about:") &&
611 !g_str_has_prefix(f, "data:"))
614 fabs = realpath(t, NULL);
617 f = g_strdup_printf("file://%s", fabs);
621 f = g_strdup_printf("http://%s", t);
629 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
631 struct Client *c = (struct Client *)data;
632 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
639 argv[2] = c->external_handler_uri;
640 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
643 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
648 g_spawn_close_pid(pid);
652 grab_environment_configuration(void)
656 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
658 accepted_language[0] = g_strdup(e);
660 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
662 download_dir = g_strdup(e);
664 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
666 enable_console_to_stdout = TRUE;
668 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
670 fifo_suffix = g_strdup(e);
672 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
674 history_file = g_strdup(e);
676 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
678 home_uri = g_strdup(e);
680 e = g_getenv(__NAME_UPPERCASE__"_TAB_POS");
683 if (strcmp(e, "top") == 0)
684 tab_pos = GTK_POS_TOP;
685 if (strcmp(e, "right") == 0)
686 tab_pos = GTK_POS_RIGHT;
687 if (strcmp(e, "bottom") == 0)
688 tab_pos = GTK_POS_BOTTOM;
689 if (strcmp(e, "left") == 0)
690 tab_pos = GTK_POS_LEFT;
693 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
695 user_agent = g_strdup(e);
697 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
699 global_zoom = atof(e);
703 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
705 struct Client *c = (struct Client *)data;
706 WebKitJavascriptResult *js_result;
708 JSCException *exception;
712 g_free(c->feed_html);
715 /* This was taken almost verbatim from the example in WebKit's
718 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
720 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
724 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
729 value = webkit_javascript_result_get_js_value(js_result);
730 if (jsc_value_is_string(value))
732 str_value = jsc_value_to_string(value);
733 exception = jsc_context_get_exception(jsc_value_get_context(value));
734 if (exception != NULL)
736 fprintf(stderr, __NAME__": Error running javascript: %s\n",
737 jsc_exception_get_message(exception));
740 c->feed_html = str_value;
742 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
743 GTK_ENTRY_ICON_PRIMARY,
744 "application-rss+xml-symbolic");
745 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
746 GTK_ENTRY_ICON_PRIMARY,
751 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
752 GTK_ENTRY_ICON_PRIMARY,
756 webkit_javascript_result_unref(js_result);
760 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
763 struct Client *c = (struct Client *)data;
765 if (!gtk_widget_is_focus(c->location))
767 if (webkit_hit_test_result_context_is_link(ht))
769 gtk_entry_set_text(GTK_ENTRY(c->location),
770 webkit_hit_test_result_get_link_uri(ht));
772 if (c->hover_uri != NULL)
773 g_free(c->hover_uri);
774 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
778 gtk_entry_set_text(GTK_ENTRY(c->location),
779 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
781 if (c->hover_uri != NULL)
782 g_free(c->hover_uri);
789 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
792 struct Client *c = (struct Client *)data;
794 gchar *data_template =
799 " <meta charset=\"UTF-8\">"
800 " <title>Feeds</title>"
803 " <p>Feeds found on this page:</p>"
810 if (c->feed_html != NULL)
812 /* What we're actually trying to do is show a simple HTML page
813 * that lists all the feeds on the current page. The function
814 * webkit_web_view_load_html() looks like the proper way to do
815 * that. Sad thing is, it doesn't create a history entry, but
816 * instead simply replaces the content of the current page. This
817 * is not what we want.
819 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
820 * mechanism to show my custom HTML snippet *and* create a
823 * [0]: https://tools.ietf.org/html/rfc2397
824 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
825 d = g_strdup_printf(data_template, c->feed_html);
826 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
832 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
834 struct Client *c = (struct Client *)data;
835 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
838 if (event->type == GDK_KEY_PRESS)
840 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
842 switch (((GdkEventKey *)event)->keyval)
844 case GDK_KEY_q: /* close window (left hand) */
845 client_destroy(NULL, c);
847 case GDK_KEY_w: /* home (left hand) */
848 f = ensure_uri_scheme(home_uri);
849 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
852 case GDK_KEY_e: /* new tab (left hand) */
853 f = ensure_uri_scheme(home_uri);
854 client_new(f, NULL, TRUE);
857 case GDK_KEY_r: /* reload (left hand) */
858 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
861 case GDK_KEY_d: /* download manager (left hand) */
862 gtk_widget_show_all(dm.win);
864 case GDK_KEY_2: /* search forward (left hand) */
865 case GDK_KEY_n: /* search forward (maybe both hands) */
868 case GDK_KEY_3: /* search backward (left hand) */
871 case GDK_KEY_l: /* location (BOTH hands) */
872 gtk_widget_grab_focus(c->location);
874 case GDK_KEY_k: /* initiate search (BOTH hands) */
875 gtk_widget_grab_focus(c->location);
876 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
877 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
879 case GDK_KEY_c: /* reload trusted certs (left hand) */
880 trust_user_certs(wc);
882 case GDK_KEY_x: /* launch external handler (left hand) */
883 if (c->external_handler_uri != NULL)
884 g_free(c->external_handler_uri);
885 c->external_handler_uri = g_strdup(
886 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
887 external_handler_run(NULL, NULL, c);
889 case GDK_KEY_a: /* go one tab to the left (left hand) */
890 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
892 case GDK_KEY_s: /* go one tab to the right (left hand) */
893 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
897 /* navigate backward (left hand) */
898 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
900 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
903 /* navigate forward (left hand) */
904 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
906 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
915 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
917 if (event->type == GDK_KEY_PRESS)
919 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
921 switch (((GdkEventKey *)event)->keyval)
923 case GDK_KEY_d: /* close window (left hand) */
925 downloadmanager_delete(dm.win, NULL);
935 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
937 struct Client *c = (struct Client *)data;
941 if (key_common(widget, event, data))
944 if (event->type == GDK_KEY_PRESS)
946 switch (((GdkEventKey *)event)->keyval)
948 case GDK_KEY_KP_Enter:
950 gtk_widget_grab_focus(c->web_view);
951 t = gtk_entry_get_text(GTK_ENTRY(c->location));
952 if (t != NULL && t[0] == ':' && t[1] == '/')
954 if (search_text != NULL)
956 search_text = g_strdup(t + 2);
959 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
961 f = ensure_uri_scheme(t);
962 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
967 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
968 gtk_entry_set_text(GTK_ENTRY(c->location),
969 (t == NULL ? __NAME__ : t));
978 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
980 struct Client *c = (struct Client *)data;
984 if (key_common(widget, event, data))
987 if (event->type == GDK_KEY_PRESS)
989 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
991 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
992 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
995 else if (event->type == GDK_BUTTON_RELEASE)
997 switch (((GdkEventButton *)event)->button)
1000 if (c->hover_uri != NULL)
1002 client_new(c->hover_uri, NULL, TRUE);
1007 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1010 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
1014 else if (event->type == GDK_SCROLL)
1016 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
1017 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
1019 gdk_event_get_scroll_deltas(event, &dx, &dy);
1020 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1022 z = dx != 0 ? global_zoom : z;
1023 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1035 GIOChannel *channel = NULL;
1036 gchar *path = NULL, *buf = NULL;
1037 gchar **tokens = NULL;
1039 keywords = g_hash_table_new(g_str_hash, g_str_equal);
1041 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1043 channel = g_io_channel_new_file(path, "r", &err);
1044 if (channel != NULL)
1046 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1047 == G_IO_STATUS_NORMAL)
1052 tokens = g_strsplit(buf, " ", 2);
1053 if (tokens[0] != NULL && tokens[1] != NULL)
1054 g_hash_table_insert(keywords, g_strdup(tokens[0]),
1055 g_strdup(tokens[1]));
1060 g_io_channel_shutdown(channel, FALSE, NULL);
1066 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1068 gboolean ret = FALSE;
1069 gchar **tokens = NULL;
1070 gchar *val = NULL, *escaped = NULL, *uri = NULL;
1072 tokens = g_strsplit(t, " ", 2);
1073 if (tokens[0] != NULL && tokens[1] != NULL)
1075 val = g_hash_table_lookup(keywords, tokens[0]);
1078 escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1079 uri = g_strdup_printf((gchar *)val, escaped);
1080 webkit_web_view_load_uri(web_view, uri);
1092 mainwindow_setup(void)
1094 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1095 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1096 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1097 gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
1099 mw.notebook = gtk_notebook_new();
1100 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1101 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), tab_pos);
1102 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1103 g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
1104 G_CALLBACK(mainwindow_title_before), NULL);
1108 mainwindow_title_before(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
1110 mainwindow_title(idx);
1114 mainwindow_title(gint idx)
1116 GtkWidget *child, *evbox, *label;
1121 idx = gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook));
1126 child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
1130 evbox = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
1131 label = gtk_bin_get_child(GTK_BIN(evbox));
1132 text = gtk_label_get_text(GTK_LABEL(label));
1133 gtk_window_set_title(GTK_WINDOW(mw.win), text);
1137 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1138 WebKitHitTestResult *ht, gpointer data)
1140 struct Client *c = (struct Client *)data;
1141 GSimpleAction *action = NULL;
1142 WebKitContextMenuItem *mi = NULL;
1143 const gchar *uri = NULL;
1147 if (webkit_hit_test_result_context_is_link(ht))
1148 uri = webkit_hit_test_result_get_link_uri(ht);
1149 else if (webkit_hit_test_result_context_is_image(ht))
1150 uri = webkit_hit_test_result_get_image_uri(ht);
1151 else if (webkit_hit_test_result_context_is_media(ht))
1152 uri = webkit_hit_test_result_get_media_uri(ht);
1156 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1158 if (c->external_handler_uri != NULL)
1159 g_free(c->external_handler_uri);
1160 c->external_handler_uri = g_strdup(uri);
1161 action = g_simple_action_new("external_handler", NULL);
1162 g_signal_connect(G_OBJECT(action), "activate",
1163 G_CALLBACK(external_handler_run), data);
1164 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1165 "Open with external handler",
1167 webkit_context_menu_append(menu, mi);
1168 g_object_unref(action);
1171 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1176 quit_if_nothing_active(void)
1186 gtk_widget_show_all(dm.win);
1193 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1197 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1201 client_new(uri, NULL, TRUE);
1208 run_user_scripts(WebKitWebView *web_view)
1210 gchar *base = NULL, *path = NULL, *contents = NULL;
1211 const gchar *entry = NULL;
1212 GDir *scriptdir = NULL;
1214 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts", NULL);
1215 scriptdir = g_dir_open(base, 0, NULL);
1216 if (scriptdir != NULL)
1218 while ((entry = g_dir_read_name(scriptdir)) != NULL)
1220 path = g_build_filename(base, entry, NULL);
1221 if (g_str_has_suffix(path, ".js"))
1223 if (g_file_get_contents(path, &contents, NULL, NULL))
1225 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1231 g_dir_close(scriptdir);
1238 search(gpointer data, gint direction)
1240 struct Client *c = (struct Client *)data;
1241 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1242 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1244 if (search_text == NULL)
1250 webkit_find_controller_search(fc, search_text,
1251 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1252 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1256 webkit_find_controller_search_next(fc);
1259 webkit_find_controller_search_previous(fc);
1265 show_web_view(WebKitWebView *web_view, gpointer data)
1267 struct Client *c = (struct Client *)data;
1272 gtk_widget_show_all(mw.win);
1274 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1276 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
1278 gtk_widget_grab_focus(c->web_view);
1282 trust_user_certs(WebKitWebContext *wc)
1284 GTlsCertificate *cert;
1285 const gchar *basedir, *file, *absfile;
1288 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1289 dir = g_dir_open(basedir, 0, NULL);
1292 file = g_dir_read_name(dir);
1293 while (file != NULL)
1295 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1297 cert = g_tls_certificate_new_from_file(absfile, NULL);
1299 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1301 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1302 file = g_dir_read_name(dir);
1310 main(int argc, char **argv)
1315 gtk_init(&argc, &argv);
1316 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1317 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1319 grab_environment_configuration();
1321 while ((opt = getopt(argc, argv, "C")) != -1)
1326 cooperative_instances = FALSE;
1329 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1335 if (cooperative_instances)
1336 cooperation_setup();
1337 downloadmanager_setup();
1341 if (!cooperative_instances || cooperative_alone)
1343 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1345 webkit_web_context_set_web_extensions_directory(
1346 webkit_web_context_get_default(), c
1351 client_new(home_uri, NULL, TRUE);
1354 for (i = optind; i < argc; i++)
1355 client_new(argv[i], NULL, TRUE);
1358 if (!cooperative_instances || cooperative_alone)