9 #include <JavaScriptCore/JavaScript.h>
10 #include <gdk/gdkkeysyms.h>
14 #include <webkit2/webkit2.h>
16 void client_destroy(GtkWidget *, gpointer);
17 WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean,
19 WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
21 void cooperation_setup(void);
22 void changed_download_progress(GObject *, GParamSpec *, gpointer);
23 void changed_load_progress(GObject *, GParamSpec *, gpointer);
24 void changed_favicon(GObject *, GParamSpec *, gpointer);
25 void changed_title(GObject *, GParamSpec *, gpointer);
26 void changed_uri(GObject *, GParamSpec *, gpointer);
27 gboolean crashed_web_view(WebKitWebView *, gpointer);
28 gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
29 WebKitPolicyDecisionType, gpointer);
30 gboolean download_handle(WebKitDownload *, gchar *, gpointer);
31 void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
32 void downloadmanager_cancel(GtkToolButton *, gpointer);
33 gboolean downloadmanager_delete(GtkWidget *, gpointer);
34 void downloadmanager_setup(void);
35 gchar *ensure_uri_scheme(const gchar *);
36 void grab_environment_configuration(void);
37 void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
38 void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
39 void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
40 void init_default_web_context(void);
41 gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
42 gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
43 gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
44 gboolean key_tablabel(GtkWidget *, GdkEvent *, gpointer);
45 gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
46 void mainwindow_setup(void);
47 void mainwindow_title(gint);
48 void notebook_switch_page(GtkNotebook *, GtkWidget *, guint, gpointer);
49 gboolean quit_if_nothing_active(void);
50 gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
51 void run_user_scripts(WebKitWebView *);
52 void search(gpointer, gint);
53 void show_web_view(WebKitWebView *, gpointer);
54 void trust_user_certs(WebKitWebContext *);
55 GKeyFile *get_ini(void);
60 gchar *external_handler_uri;
68 gboolean focus_new_tab;
77 struct DownloadManager
86 WebKitCookieAcceptPolicy cookie_policy;
87 const gchar *accepted_language[2];
88 gboolean cooperative_alone;
89 gboolean cooperative_instances;
90 gboolean enable_console_to_stdout;
91 gboolean javascript_disabled;
102 gint clients = 0, downloads = 0;
103 int cooperative_pipe_fp = 0;
107 client_destroy(GtkWidget *widget, gpointer data)
109 struct Client *c = (struct Client *)data;
112 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
113 changed_load_progress, c);
115 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
117 fprintf(stderr, __NAME__": Tab index was -1, bamboozled\n");
119 gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
124 quit_if_nothing_active();
128 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
133 GtkWidget *evbox, *tabbox;
135 if (uri != NULL && cfg.cooperative_instances && !cfg.cooperative_alone) {
136 f = ensure_uri_scheme(uri);
137 write(cooperative_pipe_fp, f, strlen(f));
138 write(cooperative_pipe_fp, "\n", 1);
143 c = calloc(1, sizeof(struct Client));
145 fprintf(stderr, __NAME__": fatal: calloc failed\n");
149 c->focus_new_tab = focus_tab;
151 if (related_wv == NULL)
152 c->web_view = webkit_web_view_new();
154 c->web_view = webkit_web_view_new_with_related_view(related_wv);
156 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), cfg.global_zoom);
157 WebKitSettings *settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
158 webkit_settings_set_enable_javascript(settings, !cfg.javascript_disabled);
159 g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
160 G_CALLBACK(changed_favicon), c);
161 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
162 G_CALLBACK(changed_title), c);
163 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
164 G_CALLBACK(changed_uri), c);
165 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
166 G_CALLBACK(changed_load_progress), c);
167 g_signal_connect(G_OBJECT(c->web_view), "create",
168 G_CALLBACK(client_new_request), NULL);
169 g_signal_connect(G_OBJECT(c->web_view), "close",
170 G_CALLBACK(client_destroy), c);
171 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
172 G_CALLBACK(decide_policy), NULL);
173 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
174 G_CALLBACK(key_web_view), c);
175 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
176 G_CALLBACK(key_web_view), c);
177 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
178 G_CALLBACK(key_web_view), c);
179 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
180 G_CALLBACK(hover_web_view), c);
181 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
182 G_CALLBACK(crashed_web_view), c);
184 if (cfg.user_agent != NULL)
185 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
186 "user-agent", cfg.user_agent, NULL);
188 if (cfg.enable_console_to_stdout)
189 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
191 webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
193 c->location = gtk_entry_new();
194 g_signal_connect(G_OBJECT(c->location), "key-press-event",
195 G_CALLBACK(key_location), c);
196 g_signal_connect(G_OBJECT(c->location), "icon-release",
197 G_CALLBACK(icon_location), c);
198 /* XXX This is a workaround. Setting this to NULL (which is done in
199 * grab_feeds_finished() if no feed has been detected) adds a little
200 * padding left of the text. Not sure why. The point of this call
201 * right here is to have that padding right from the start. This
202 * avoids a graphical artifact. */
203 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
204 GTK_ENTRY_ICON_PRIMARY,
207 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
208 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
209 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
210 gtk_container_set_focus_child(GTK_CONTAINER(c->vbox), c->web_view);
212 c->tabicon = gtk_image_new_from_icon_name("text-html", GTK_ICON_SIZE_SMALL_TOOLBAR);
214 c->tablabel = gtk_label_new(__NAME__);
215 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
216 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), cfg.tab_width_chars);
217 gtk_widget_set_has_tooltip(c->tablabel, TRUE);
219 /* XXX I don't own a HiDPI screen, so I don't know if scale_factor
220 * does the right thing. */
221 tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
222 5 * gtk_widget_get_scale_factor(mw.win));
223 gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
224 gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
226 evbox = gtk_event_box_new();
227 gtk_container_add(GTK_CONTAINER(evbox), tabbox);
228 g_signal_connect(G_OBJECT(evbox), "button-release-event",
229 G_CALLBACK(key_tablabel), c);
231 gtk_widget_add_events(evbox, GDK_SCROLL_MASK);
232 g_signal_connect(G_OBJECT(evbox), "scroll-event",
233 G_CALLBACK(key_tablabel), c);
235 /* For easy access, store a reference to our label. */
236 g_object_set_data(G_OBJECT(evbox), "lariza-tab-label", c->tablabel);
238 /* This only shows the event box and the label inside, nothing else.
239 * Needed because the evbox/label is "internal" to the notebook and
240 * not part of the normal "widget tree" (IIUC). */
241 gtk_widget_show_all(evbox);
243 gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox,
244 gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1);
245 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
248 show_web_view(NULL, c);
250 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
251 G_CALLBACK(show_web_view), c);
254 f = ensure_uri_scheme(uri);
255 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
261 return WEBKIT_WEB_VIEW(c->web_view);
265 client_new_request(WebKitWebView *web_view,
266 WebKitNavigationAction *navigation_action, gpointer data)
268 return client_new(NULL, web_view, FALSE, FALSE);
272 cooperation_setup(void)
275 gchar *fifofilename, *fifopath;
277 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", cfg.fifo_suffix);
278 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
279 g_free(fifofilename);
281 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
282 mkfifo(fifopath, 0600);
284 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
285 if (!cooperative_pipe_fp) {
286 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
288 if (write(cooperative_pipe_fp, "", 0) == -1) {
289 /* Could not do an empty write to the FIFO which means there's
290 * no one listening. */
291 close(cooperative_pipe_fp);
292 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
293 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
295 cfg.cooperative_alone = FALSE;
303 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
305 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
306 WebKitURIResponse *resp;
307 GtkToolItem *tb = GTK_TOOL_ITEM(data);
310 gchar *t, *filename, *base;
312 p = webkit_download_get_estimated_progress(download);
316 resp = webkit_download_get_response(download);
317 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
319 uri = webkit_download_get_destination(download);
320 filename = g_filename_from_uri(uri, NULL, NULL);
321 if (filename == NULL) {
322 /* This really should not happen because WebKit uses that URI to
323 * write to a file... */
324 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
325 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
326 webkit_uri_response_get_uri(resp), p, size_mb);
328 base = g_path_get_basename(filename);
329 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
333 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
338 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
340 struct Client *c = (struct Client *)data;
343 "a = document.querySelectorAll('"
344 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
345 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
352 " for (i = 0; i < a.length; i++)"
354 " url = encodeURIComponent(a[i].href);"
355 " if ('title' in a[i] && a[i].title != '')"
356 " title = encodeURIComponent(a[i].title);"
359 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
365 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
369 /* The page has loaded fully. We now run the short JavaScript
370 * snippet above that operates on the DOM. It tries to grab all
371 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
373 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
375 grab_feeds_finished, c);
377 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
379 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
383 changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
385 struct Client *c = (struct Client *)data;
387 int w, h, w_should, h_should;
388 GdkPixbuf *pb, *pb_scaled;
390 f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
392 gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html",
393 GTK_ICON_SIZE_SMALL_TOOLBAR);
395 w = cairo_image_surface_get_width(f);
396 h = cairo_image_surface_get_height(f);
397 pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
399 w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
400 h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
401 pb_scaled = gdk_pixbuf_scale_simple(pb, w_should, h_should,
402 GDK_INTERP_BILINEAR);
403 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon), pb_scaled);
405 g_object_unref(pb_scaled);
412 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
415 struct Client *c = (struct Client *)data;
417 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
418 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
420 u = u == NULL ? __NAME__ : u;
421 u = u[0] == 0 ? __NAME__ : u;
423 t = t == NULL ? u : t;
424 t = t[0] == 0 ? u : t;
426 gtk_label_set_text(GTK_LABEL(c->tablabel), t);
427 gtk_widget_set_tooltip_text(c->tablabel, t);
428 mainwindow_title(gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)));
432 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
435 struct Client *c = (struct Client *)data;
438 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
440 /* When a web process crashes, we get a "notify::uri" signal, but we
441 * can no longer read a meaningful URI. It's just an empty string
442 * now. Not updating the location bar in this scenario is important,
443 * because we would override the "WEB PROCESS CRASHED" message. */
444 if (t != NULL && strlen(t) > 0) {
445 gtk_entry_set_text(GTK_ENTRY(c->location), t);
447 if (cfg.history_file != NULL) {
448 fp = fopen(cfg.history_file, "a");
450 fprintf(fp, "%s\n", t);
453 perror(__NAME__": Error opening history file");
460 crashed_web_view(WebKitWebView *web_view, gpointer data)
463 struct Client *c = (struct Client *)data;
465 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
466 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
467 gtk_entry_set_text(GTK_ENTRY(c->location), t);
474 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
475 WebKitPolicyDecisionType type, gpointer data)
477 WebKitResponsePolicyDecision *r;
480 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
481 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
482 if (!webkit_response_policy_decision_is_mime_type_supported(r))
483 webkit_policy_decision_download(decision);
485 webkit_policy_decision_use(decision);
488 /* Use whatever default there is. */
495 download_handle_finished(WebKitDownload *download, gpointer data)
501 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
504 g_signal_connect(G_OBJECT(download), "decide-destination",
505 G_CALLBACK(download_handle), data);
509 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
511 gchar *sug_clean, *path, *path2 = NULL, *uri;
516 sug_clean = g_strdup(suggested_filename);
517 for (i = 0; i < strlen(sug_clean); i++)
518 if (sug_clean[i] == G_DIR_SEPARATOR)
521 path = g_build_filename(cfg.download_dir, sug_clean, NULL);
522 path2 = g_strdup(path);
523 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000) {
526 path2 = g_strdup_printf("%s.%d", path, suffix);
530 if (suffix == 1000) {
531 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
532 webkit_download_cancel(download);
534 uri = g_filename_to_uri(path2, NULL, NULL);
535 webkit_download_set_destination(download, uri);
538 tb = gtk_tool_button_new(NULL, NULL);
539 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
540 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
541 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
542 gtk_widget_show_all(dm.win);
544 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
545 G_CALLBACK(changed_download_progress), tb);
548 g_signal_connect(G_OBJECT(download), "finished",
549 G_CALLBACK(download_handle_finished), NULL);
551 g_object_ref(download);
552 g_signal_connect(G_OBJECT(tb), "clicked",
553 G_CALLBACK(downloadmanager_cancel), download);
560 /* Propagate -- to whom it may concern. */
565 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
567 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
569 webkit_download_cancel(download);
570 g_object_unref(download);
572 gtk_widget_destroy(GTK_WIDGET(tb));
576 downloadmanager_delete(GtkWidget *obj, gpointer data)
578 if (!quit_if_nothing_active())
579 gtk_widget_hide(dm.win);
585 downloadmanager_setup(void)
587 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
588 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
589 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
590 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
591 g_signal_connect(G_OBJECT(dm.win), "delete-event",
592 G_CALLBACK(downloadmanager_delete), NULL);
593 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
594 G_CALLBACK(key_downloadmanager), NULL);
596 dm.toolbar = gtk_toolbar_new();
597 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
598 GTK_ORIENTATION_VERTICAL);
599 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
600 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
602 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
603 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
604 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
605 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
607 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
611 ensure_uri_scheme(const gchar *t)
615 f = g_ascii_strdown(t, -1);
616 if (!g_str_has_prefix(f, "http:") &&
617 !g_str_has_prefix(f, "https:") &&
618 !g_str_has_prefix(f, "file:") &&
619 !g_str_has_prefix(f, "about:") &&
620 !g_str_has_prefix(f, "data:") &&
621 !g_str_has_prefix(f, "webkit:")) {
623 fabs = realpath(t, NULL);
625 f = g_strdup_printf("file://%s", fabs);
628 f = g_strdup_printf("http://%s", t);
638 cfg.accepted_language[0] = NULL;
639 cfg.accepted_language[1] = NULL;
640 cfg.cooperative_alone = TRUE;
641 cfg.cooperative_alone = TRUE;
642 cfg.fifo_suffix = "main";
644 const char *e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
646 cfg.fifo_suffix = g_strdup(e);
648 char *xdg_down = getenv("XDG_DOWNLOAD_DIR");
649 cfg.download_dir = (xdg_down) ? xdg_down : "/var/tmp";
652 cfg.accepted_language[0] = g_key_file_get_string(config, "browser",
653 "accepted_language", NULL);
654 cfg.history_file = g_key_file_get_string(config, "browser", "history_file",
656 cfg.home_uri = g_key_file_get_string(config, "browser", "homepage", NULL);
657 cfg.home_uri = (cfg.home_uri) ? cfg.home_uri : "about:blank";
658 cfg.enable_console_to_stdout = g_key_file_get_boolean(config, "browser",
661 cfg.user_agent = g_key_file_get_string(config, "browser", "user_agent", NULL);
662 cfg.javascript_disabled = g_key_file_get_boolean(config, "browser",
663 "javascript_disabled", NULL);
664 char *input_cookie_policy = g_key_file_get_string(config, "browser",
665 "cookie_policy", NULL);
666 cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
667 if (input_cookie_policy) {
668 if (strcmp(input_cookie_policy, "all") == 0) {
669 cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
670 } else if (strcmp(input_cookie_policy, "none") == 0) {
671 cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
675 cfg.tab_width_chars = g_key_file_get_integer(config, "ui", "tab_width", NULL);
676 cfg.tab_width_chars = (cfg.tab_width_chars) ? cfg.tab_width_chars : 20;
677 cfg.global_zoom = g_key_file_get_double(config, "ui", "zoom_level", NULL);
678 cfg.global_zoom = (cfg.global_zoom) ? cfg.global_zoom : 1.0;
679 cfg.search_engine = g_key_file_get_string(config, "ui", "search_engine", NULL);
680 cfg.search_engine = (cfg.search_engine) ? cfg.search_engine :
681 "https://duckduckgo.com?q=";
685 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
687 struct Client *c = (struct Client *)data;
688 WebKitJavascriptResult *js_result;
690 JSCException *exception;
694 g_free(c->feed_html);
697 /* This was taken almost verbatim from the example in WebKit's
700 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
702 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
705 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
710 value = webkit_javascript_result_get_js_value(js_result);
711 if (jsc_value_is_string(value)) {
712 str_value = jsc_value_to_string(value);
713 exception = jsc_context_get_exception(jsc_value_get_context(value));
714 if (exception != NULL) {
715 fprintf(stderr, __NAME__": Error running javascript: %s\n",
716 jsc_exception_get_message(exception));
718 c->feed_html = str_value;
721 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
722 GTK_ENTRY_ICON_PRIMARY,
723 "application-rss+xml-symbolic");
724 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
725 GTK_ENTRY_ICON_PRIMARY,
728 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
729 GTK_ENTRY_ICON_PRIMARY,
733 webkit_javascript_result_unref(js_result);
737 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
740 struct Client *c = (struct Client *)data;
743 g_free(c->hover_uri);
745 if (webkit_hit_test_result_context_is_link(ht)) {
746 to_show = webkit_hit_test_result_get_link_uri(ht);
747 c->hover_uri = g_strdup(to_show);
749 to_show = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
753 if (!gtk_widget_is_focus(c->location))
754 gtk_entry_set_text(GTK_ENTRY(c->location), to_show);
758 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
761 struct Client *c = (struct Client *)data;
763 gchar *data_template =
768 " <meta charset=\"UTF-8\">"
769 " <title>Feeds</title>"
772 " <p>Feeds found on this page:</p>"
779 if (c->feed_html != NULL)
781 /* What we're actually trying to do is show a simple HTML page
782 * that lists all the feeds on the current page. The function
783 * webkit_web_view_load_html() looks like the proper way to do
784 * that. Sad thing is, it doesn't create a history entry, but
785 * instead simply replaces the content of the current page. This
786 * is not what we want.
788 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
789 * mechanism to show my custom HTML snippet *and* create a
792 * [0]: https://tools.ietf.org/html/rfc2397
793 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
794 d = g_strdup_printf(data_template, c->feed_html);
795 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
801 init_default_web_context(void)
804 WebKitWebContext *wc;
805 WebKitCookieManager *cm;
807 wc = webkit_web_context_get_default();
809 p = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock", NULL);
810 webkit_web_context_set_sandbox_enabled(wc, TRUE);
811 webkit_web_context_add_path_to_sandbox(wc, p, TRUE);
814 webkit_web_context_set_process_model(wc,
815 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
817 p = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
819 webkit_web_context_set_web_extensions_directory(wc, p);
822 if (cfg.accepted_language[0] != NULL)
823 webkit_web_context_set_preferred_languages(wc, cfg.accepted_language);
825 g_signal_connect(G_OBJECT(wc), "download-started",
826 G_CALLBACK(download_handle_start), NULL);
828 trust_user_certs(wc);
830 cm = webkit_web_context_get_cookie_manager(wc);
831 webkit_cookie_manager_set_accept_policy(cm, cfg.cookie_policy);
833 webkit_web_context_set_favicon_database_directory(wc, NULL);
834 webkit_cookie_manager_set_persistent_storage(cm,
835 g_build_filename("/",
836 g_get_user_cache_dir(),
840 WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
844 search(gpointer data, gint direction)
846 struct Client *c = (struct Client *)data;
847 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
848 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
850 if (search_text == NULL)
855 webkit_find_controller_search(fc, search_text,
856 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
857 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
861 webkit_find_controller_search_next(fc);
864 webkit_find_controller_search_previous(fc);
867 webkit_find_controller_search_finish(fc);
873 search_init(struct Client *c, int direction)
875 gtk_widget_grab_focus(c->location);
876 const gchar *contents = gtk_entry_get_text(GTK_ENTRY(c->location));
877 if (strcspn(contents, "s/")) {
878 gtk_entry_set_text(GTK_ENTRY(c->location), "s/");
880 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
883 search(c, direction);
887 def_key(char *key, unsigned int def)
889 char *conf = g_key_file_get_string(config, "keybindings", key, NULL);
890 return (conf) ? gdk_keyval_from_name((conf) ? conf : NULL) : def;
894 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
896 struct Client *c = (struct Client *)data;
900 if (event->type == GDK_KEY_PRESS) {
901 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK) {
902 WebKitSettings *settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
903 gboolean js = webkit_settings_get_enable_javascript(settings);
904 int key = ((GdkEventKey *)event)->keyval;
905 if (def_key("download_manager", GDK_KEY_y) == key) {
906 gtk_widget_show_all(dm.win);
908 } else if (def_key("history_back", GDK_KEY_h) == key) {
909 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
911 } else if (def_key("history_forwards", GDK_KEY_l) == key) {
912 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
914 } else if (def_key("location", GDK_KEY_t) == key) {
915 gtk_widget_grab_focus(c->location);
916 const char *uri = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
917 const char *goal = (uri) ? uri : "https://";
918 gtk_entry_set_text(GTK_ENTRY(c->location), goal);
919 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
921 } else if (def_key("quit", GDK_KEY_g) == key) {
923 gtk_widget_grab_focus(c->web_view);
924 gtk_entry_set_text(GTK_ENTRY(c->location),
925 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
926 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
927 "window.getSelection().removeAllRanges();"
928 "document.activeElement.blur();",
931 } else if (def_key("reload", GDK_KEY_e) == key) {
932 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(c->web_view));
934 } else if (def_key("scroll_line_down", GDK_KEY_j) == key) {
935 for (int i = 0; i < 2; i++) {
936 event->key.keyval = GDK_KEY_Down;
937 gdk_event_put(event);
940 } else if (def_key("scroll_line_up", GDK_KEY_k) == key) {
941 event->key.keyval = GDK_KEY_Up;
942 gdk_event_put(event);
944 } else if (def_key("scroll_page_down", GDK_KEY_f) == key) {
945 event->key.keyval = GDK_KEY_Page_Down;
946 gdk_event_put(event);
948 } else if (def_key("scroll_page_up", GDK_KEY_b) == key) {
949 event->key.keyval = GDK_KEY_Page_Up;
950 gdk_event_put(event);
952 } else if (def_key("search_forwards", GDK_KEY_s) == key) {
955 } else if (def_key("search_backwards", GDK_KEY_r) == key) {
958 } else if (def_key("tab_close", GDK_KEY_q) == key) {
959 client_destroy(NULL, c);
961 } else if (def_key("tab_previous", GDK_KEY_u) == key) {
962 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
964 } else if (def_key("tab_new", GDK_KEY_w) == key) {
965 f = ensure_uri_scheme(cfg.home_uri);
966 client_new(f, NULL, TRUE, TRUE);
969 } else if (def_key("tab_next", GDK_KEY_i) == key) {
970 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
972 } else if (def_key("toggle_js", GDK_KEY_o) == key) {
973 webkit_settings_set_enable_javascript(settings, (js) ? FALSE : TRUE);
974 webkit_web_view_set_settings(WEBKIT_WEB_VIEW(c->web_view), settings);
976 } else if (def_key("web_search", GDK_KEY_d) == key) {
977 gtk_widget_grab_focus(c->location);
978 gtk_entry_set_text(GTK_ENTRY(c->location), "w/");
979 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
981 } else if (def_key("zoom_in", GDK_KEY_equal) == key) {
982 now = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
983 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), now + 0.1);
985 } else if (def_key("zoom_out", GDK_KEY_minus) == key) {
986 now = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
987 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), now - 0.1);
989 } else if (def_key("zoom_reset", GDK_KEY_0) == key) {
990 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view),
1000 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
1002 if (event->type == GDK_KEY_PRESS) {
1003 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK) {
1004 int key = ((GdkEventKey *)event)->keyval;
1005 if ((def_key("close_tab", GDK_KEY_q) == key) ||
1006 (def_key("download_manager", GDK_KEY_y) == key)) {
1007 downloadmanager_delete(dm.win, NULL);
1017 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
1019 struct Client *c = (struct Client *)data;
1023 if (key_common(widget, event, data))
1026 if (event->type == GDK_KEY_PRESS) {
1027 int key = ((GdkEventKey *)event)->keyval;
1028 if ((GDK_KEY_KP_Enter == key) || (GDK_KEY_Return == key)) {
1029 gtk_widget_grab_focus(c->web_view);
1030 t = gtk_entry_get_text(GTK_ENTRY(c->location));
1031 if (t != NULL && t[0] == 's' && t[1] == '/') {
1032 if (search_text != NULL)
1033 g_free(search_text);
1034 search_text = g_strdup(t + 2);
1036 } else if (t != NULL && t[0] == 'w' && t[1] == '/') {
1037 const char *engine = cfg.search_engine;
1038 int len = strlen(engine) + strlen(t) - 2;
1039 char *f = (char *) malloc(len);
1040 snprintf(f, len + 1, "%s%s", engine, t + 2);
1041 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
1044 f = ensure_uri_scheme(t);
1045 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
1049 } else if ((GDK_KEY_Escape == key) ||
1050 (def_key("quit", GDK_KEY_g) == key)) {
1051 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
1052 gtk_entry_set_text(GTK_ENTRY(c->location),
1053 (t == NULL ? __NAME__ : t));
1061 key_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
1063 GdkScrollDirection direction;
1065 if (event->type == GDK_BUTTON_RELEASE) {
1066 switch (((GdkEventButton *)event)->button) {
1068 client_destroy(NULL, data);
1071 } else if (event->type == GDK_SCROLL) {
1072 gdk_event_get_scroll_direction(event, &direction);
1073 switch (direction) {
1075 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
1077 case GDK_SCROLL_DOWN:
1078 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
1089 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
1091 struct Client *c = (struct Client *)data;
1095 if (key_common(widget, event, data))
1098 if (event->type == GDK_KEY_PRESS) {
1099 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape) {
1100 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
1101 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
1103 } else if (event->type == GDK_BUTTON_RELEASE) {
1104 switch (((GdkEventButton *)event)->button) {
1106 if (c->hover_uri != NULL) {
1107 client_new(c->hover_uri, NULL, TRUE, FALSE);
1112 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1115 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
1118 } else if (event->type == GDK_SCROLL) {
1119 if (((GdkEventScroll *)event)->state & GDK_CONTROL_MASK ||
1120 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK) {
1121 gdk_event_get_scroll_deltas(event, &dx, &dy);
1122 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1124 z = dx != 0 ? cfg.global_zoom : z;
1125 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1134 mainwindow_setup(void)
1136 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1137 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1138 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1139 gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
1141 mw.notebook = gtk_notebook_new();
1142 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1143 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1144 g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
1145 G_CALLBACK(notebook_switch_page), NULL);
1149 mainwindow_title(gint idx)
1151 GtkWidget *child, *widg, *tablabel;
1154 child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
1158 widg = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
1159 tablabel = (GtkWidget *)g_object_get_data(G_OBJECT(widg), "lariza-tab-label");
1160 text = gtk_label_get_text(GTK_LABEL(tablabel));
1161 gtk_window_set_title(GTK_WINDOW(mw.win), text);
1165 notebook_switch_page(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
1167 mainwindow_title(idx);
1171 quit_if_nothing_active(void)
1174 if (downloads == 0) {
1178 gtk_widget_show_all(dm.win);
1186 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1190 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1193 client_new(uri, NULL, TRUE, TRUE);
1200 run_user_scripts(WebKitWebView *web_view)
1202 gchar *base = NULL, *path = NULL, *contents = NULL;
1203 const gchar *entry = NULL;
1204 GDir *scriptdir = NULL;
1206 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts",
1208 scriptdir = g_dir_open(base, 0, NULL);
1209 if (scriptdir != NULL) {
1210 while ((entry = g_dir_read_name(scriptdir)) != NULL) {
1211 path = g_build_filename(base, entry, NULL);
1212 char *jscmd = malloc(strlen(path) + 36);
1213 sprintf(jscmd, "console.log(\"Running userscript %s\");", path);
1214 if (g_str_has_suffix(path, ".js")) {
1215 if (g_file_get_contents(path, &contents, NULL, NULL)) {
1216 webkit_web_view_run_javascript(web_view, jscmd, NULL, NULL, NULL);
1217 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1224 g_dir_close(scriptdir);
1231 show_web_view(WebKitWebView *web_view, gpointer data)
1233 struct Client *c = (struct Client *)data;
1238 gtk_widget_show_all(mw.win);
1240 if (c->focus_new_tab) {
1241 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1243 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
1245 gtk_widget_grab_focus(c->web_view);
1250 trust_user_certs(WebKitWebContext *wc)
1252 GTlsCertificate *cert;
1253 const gchar *basedir, *file, *absfile;
1256 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1257 dir = g_dir_open(basedir, 0, NULL);
1259 file = g_dir_read_name(dir);
1260 while (file != NULL) {
1261 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1264 cert = g_tls_certificate_new_from_file(absfile, NULL);
1266 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1268 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1269 file = g_dir_read_name(dir);
1278 GKeyFileFlags flags = G_KEY_FILE_NONE;
1279 g_autoptr(GError) error = NULL;
1280 config = g_key_file_new();
1283 if (!g_key_file_load_from_file(config,
1284 g_build_filename(g_get_user_config_dir(),
1285 __NAME__, "lariza.ini",
1286 NULL), flags, &error)) {
1287 // Load global config
1288 if (!g_key_file_load_from_file(config, "/etc/lariza.ini", flags,
1290 fprintf(stderr, "Could not load lariza.ini: %s", error->message);
1297 main(int argc, char **argv)
1301 gtk_init(&argc, &argv);
1304 while ((opt = getopt(argc, argv, "C")) != -1) {
1307 cfg.cooperative_instances = FALSE;
1310 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1315 if (cfg.cooperative_instances)
1316 cooperation_setup();
1318 if (!cfg.cooperative_instances || cfg.cooperative_alone)
1319 init_default_web_context();
1321 downloadmanager_setup();
1324 if (optind >= argc) {
1325 client_new(cfg.home_uri, NULL, TRUE, TRUE);
1327 for (i = optind; i < argc; i++)
1328 client_new(argv[i], NULL, TRUE, TRUE);
1331 if (!cfg.cooperative_instances || cfg.cooperative_alone)