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;
92 gboolean spellcheck_disabled;
98 gchar *spellcheck_language;
101 gint tab_width_chars;
104 gint clients = 0, downloads = 0;
105 int cooperative_pipe_fp = 0;
109 client_destroy(GtkWidget *widget, gpointer data)
111 struct Client *c = (struct Client *)data;
114 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
115 changed_load_progress, c);
117 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
119 fprintf(stderr, __NAME__": Tab index was -1, bamboozled\n");
121 gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
126 quit_if_nothing_active();
130 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
135 GtkWidget *evbox, *tabbox;
137 if (uri != NULL && cfg.cooperative_instances && !cfg.cooperative_alone) {
138 f = ensure_uri_scheme(uri);
139 write(cooperative_pipe_fp, f, strlen(f));
140 write(cooperative_pipe_fp, "\n", 1);
145 c = calloc(1, sizeof(struct Client));
147 fprintf(stderr, __NAME__": fatal: calloc failed\n");
151 c->focus_new_tab = focus_tab;
153 if (related_wv == NULL)
154 c->web_view = webkit_web_view_new();
156 c->web_view = webkit_web_view_new_with_related_view(related_wv);
158 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), cfg.global_zoom);
159 WebKitSettings *settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
160 webkit_settings_set_enable_javascript(settings, !cfg.javascript_disabled);
161 g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
162 G_CALLBACK(changed_favicon), c);
163 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
164 G_CALLBACK(changed_title), c);
165 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
166 G_CALLBACK(changed_uri), c);
167 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
168 G_CALLBACK(changed_load_progress), c);
169 g_signal_connect(G_OBJECT(c->web_view), "create",
170 G_CALLBACK(client_new_request), NULL);
171 g_signal_connect(G_OBJECT(c->web_view), "close",
172 G_CALLBACK(client_destroy), c);
173 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
174 G_CALLBACK(decide_policy), NULL);
175 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
176 G_CALLBACK(key_web_view), c);
177 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
178 G_CALLBACK(key_web_view), c);
179 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
180 G_CALLBACK(key_web_view), c);
181 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
182 G_CALLBACK(hover_web_view), c);
183 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
184 G_CALLBACK(crashed_web_view), c);
186 if (cfg.user_agent != NULL)
187 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
188 "user-agent", cfg.user_agent, NULL);
190 if (cfg.enable_console_to_stdout)
191 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
193 webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
195 c->location = gtk_entry_new();
196 g_signal_connect(G_OBJECT(c->location), "key-press-event",
197 G_CALLBACK(key_location), c);
198 g_signal_connect(G_OBJECT(c->location), "icon-release",
199 G_CALLBACK(icon_location), c);
200 /* XXX This is a workaround. Setting this to NULL (which is done in
201 * grab_feeds_finished() if no feed has been detected) adds a little
202 * padding left of the text. Not sure why. The point of this call
203 * right here is to have that padding right from the start. This
204 * avoids a graphical artifact. */
205 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
206 GTK_ENTRY_ICON_PRIMARY,
209 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
210 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
211 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
212 gtk_container_set_focus_child(GTK_CONTAINER(c->vbox), c->web_view);
214 c->tabicon = gtk_image_new_from_icon_name("text-html", GTK_ICON_SIZE_SMALL_TOOLBAR);
216 c->tablabel = gtk_label_new(__NAME__);
217 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
218 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), cfg.tab_width_chars);
219 gtk_widget_set_has_tooltip(c->tablabel, TRUE);
221 /* XXX I don't own a HiDPI screen, so I don't know if scale_factor
222 * does the right thing. */
223 tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
224 5 * gtk_widget_get_scale_factor(mw.win));
225 gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
226 gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
228 evbox = gtk_event_box_new();
229 gtk_container_add(GTK_CONTAINER(evbox), tabbox);
230 g_signal_connect(G_OBJECT(evbox), "button-release-event",
231 G_CALLBACK(key_tablabel), c);
233 gtk_widget_add_events(evbox, GDK_SCROLL_MASK);
234 g_signal_connect(G_OBJECT(evbox), "scroll-event",
235 G_CALLBACK(key_tablabel), c);
237 /* For easy access, store a reference to our label. */
238 g_object_set_data(G_OBJECT(evbox), "lariza-tab-label", c->tablabel);
240 /* This only shows the event box and the label inside, nothing else.
241 * Needed because the evbox/label is "internal" to the notebook and
242 * not part of the normal "widget tree" (IIUC). */
243 gtk_widget_show_all(evbox);
245 gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox,
246 gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1);
247 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
250 show_web_view(NULL, c);
252 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
253 G_CALLBACK(show_web_view), c);
256 f = ensure_uri_scheme(uri);
257 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
263 return WEBKIT_WEB_VIEW(c->web_view);
267 client_new_request(WebKitWebView *web_view,
268 WebKitNavigationAction *navigation_action, gpointer data)
270 return client_new(NULL, web_view, FALSE, FALSE);
274 cooperation_setup(void)
277 gchar *fifofilename, *fifopath;
279 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", cfg.fifo_suffix);
280 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
281 g_free(fifofilename);
283 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
284 mkfifo(fifopath, 0600);
286 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
287 if (!cooperative_pipe_fp) {
288 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
290 if (write(cooperative_pipe_fp, "", 0) == -1) {
291 /* Could not do an empty write to the FIFO which means there's
292 * no one listening. */
293 close(cooperative_pipe_fp);
294 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
295 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
297 cfg.cooperative_alone = FALSE;
305 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
307 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
308 WebKitURIResponse *resp;
309 GtkToolItem *tb = GTK_TOOL_ITEM(data);
312 gchar *t, *filename, *base;
314 p = webkit_download_get_estimated_progress(download);
318 resp = webkit_download_get_response(download);
319 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
321 uri = webkit_download_get_destination(download);
322 filename = g_filename_from_uri(uri, NULL, NULL);
323 if (filename == NULL) {
324 /* This really should not happen because WebKit uses that URI to
325 * write to a file... */
326 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
327 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
328 webkit_uri_response_get_uri(resp), p, size_mb);
330 base = g_path_get_basename(filename);
331 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
335 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
340 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
342 struct Client *c = (struct Client *)data;
345 "a = document.querySelectorAll('"
346 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
347 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
354 " for (i = 0; i < a.length; i++)"
356 " url = encodeURIComponent(a[i].href);"
357 " if ('title' in a[i] && a[i].title != '')"
358 " title = encodeURIComponent(a[i].title);"
361 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
367 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
371 /* The page has loaded fully. We now run the short JavaScript
372 * snippet above that operates on the DOM. It tries to grab all
373 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
375 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
377 grab_feeds_finished, c);
379 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
381 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
385 changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
387 struct Client *c = (struct Client *)data;
389 int w, h, w_should, h_should;
390 GdkPixbuf *pb, *pb_scaled;
392 f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
394 gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html",
395 GTK_ICON_SIZE_SMALL_TOOLBAR);
397 w = cairo_image_surface_get_width(f);
398 h = cairo_image_surface_get_height(f);
399 pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
401 w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
402 h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
403 pb_scaled = gdk_pixbuf_scale_simple(pb, w_should, h_should,
404 GDK_INTERP_BILINEAR);
405 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon), pb_scaled);
407 g_object_unref(pb_scaled);
414 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
417 struct Client *c = (struct Client *)data;
419 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
420 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
422 u = u == NULL ? __NAME__ : u;
423 u = u[0] == 0 ? __NAME__ : u;
425 t = t == NULL ? u : t;
426 t = t[0] == 0 ? u : t;
428 gtk_label_set_text(GTK_LABEL(c->tablabel), t);
429 gtk_widget_set_tooltip_text(c->tablabel, t);
430 mainwindow_title(gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)));
434 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
437 struct Client *c = (struct Client *)data;
440 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
442 /* When a web process crashes, we get a "notify::uri" signal, but we
443 * can no longer read a meaningful URI. It's just an empty string
444 * now. Not updating the location bar in this scenario is important,
445 * because we would override the "WEB PROCESS CRASHED" message. */
446 if (t != NULL && strlen(t) > 0) {
447 gtk_entry_set_text(GTK_ENTRY(c->location), t);
449 if (cfg.history_file != NULL) {
450 fp = fopen(cfg.history_file, "a");
452 fprintf(fp, "%s\n", t);
455 perror(__NAME__": Error opening history file");
462 crashed_web_view(WebKitWebView *web_view, gpointer data)
465 struct Client *c = (struct Client *)data;
467 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
468 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
469 gtk_entry_set_text(GTK_ENTRY(c->location), t);
476 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
477 WebKitPolicyDecisionType type, gpointer data)
479 WebKitResponsePolicyDecision *r;
482 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
483 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
484 if (!webkit_response_policy_decision_is_mime_type_supported(r))
485 webkit_policy_decision_download(decision);
487 webkit_policy_decision_use(decision);
490 /* Use whatever default there is. */
497 download_handle_finished(WebKitDownload *download, gpointer data)
503 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
506 g_signal_connect(G_OBJECT(download), "decide-destination",
507 G_CALLBACK(download_handle), data);
511 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
513 gchar *sug_clean, *path, *path2 = NULL, *uri;
518 sug_clean = g_strdup(suggested_filename);
519 for (i = 0; i < strlen(sug_clean); i++)
520 if (sug_clean[i] == G_DIR_SEPARATOR)
523 path = g_build_filename(cfg.download_dir, sug_clean, NULL);
524 path2 = g_strdup(path);
525 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000) {
528 path2 = g_strdup_printf("%s.%d", path, suffix);
532 if (suffix == 1000) {
533 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
534 webkit_download_cancel(download);
536 uri = g_filename_to_uri(path2, NULL, NULL);
537 webkit_download_set_destination(download, uri);
540 tb = gtk_tool_button_new(NULL, NULL);
541 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
542 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
543 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
544 gtk_widget_show_all(dm.win);
546 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
547 G_CALLBACK(changed_download_progress), tb);
550 g_signal_connect(G_OBJECT(download), "finished",
551 G_CALLBACK(download_handle_finished), NULL);
553 g_object_ref(download);
554 g_signal_connect(G_OBJECT(tb), "clicked",
555 G_CALLBACK(downloadmanager_cancel), download);
562 /* Propagate -- to whom it may concern. */
567 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
569 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
571 webkit_download_cancel(download);
572 g_object_unref(download);
574 gtk_widget_destroy(GTK_WIDGET(tb));
578 downloadmanager_delete(GtkWidget *obj, gpointer data)
580 if (!quit_if_nothing_active())
581 gtk_widget_hide(dm.win);
587 downloadmanager_setup(void)
589 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
590 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
591 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
592 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
593 g_signal_connect(G_OBJECT(dm.win), "delete-event",
594 G_CALLBACK(downloadmanager_delete), NULL);
595 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
596 G_CALLBACK(key_downloadmanager), NULL);
598 dm.toolbar = gtk_toolbar_new();
599 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
600 GTK_ORIENTATION_VERTICAL);
601 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
602 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
604 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
605 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
606 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
607 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
609 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
613 ensure_uri_scheme(const gchar *t)
617 f = g_ascii_strdown(t, -1);
618 if (!g_str_has_prefix(f, "http:") &&
619 !g_str_has_prefix(f, "https:") &&
620 !g_str_has_prefix(f, "file:") &&
621 !g_str_has_prefix(f, "about:") &&
622 !g_str_has_prefix(f, "data:") &&
623 !g_str_has_prefix(f, "webkit:")) {
625 fabs = realpath(t, NULL);
627 f = g_strdup_printf("file://%s", fabs);
630 f = g_strdup_printf("http://%s", t);
640 cfg.accepted_language[0] = NULL;
641 cfg.accepted_language[1] = NULL;
642 cfg.cooperative_alone = TRUE;
643 cfg.cooperative_alone = TRUE;
644 cfg.fifo_suffix = "main";
646 const char *e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
648 cfg.fifo_suffix = g_strdup(e);
650 char *xdg_down = getenv("XDG_DOWNLOAD_DIR");
651 cfg.download_dir = (xdg_down) ? xdg_down : "/var/tmp";
654 cfg.accepted_language[0] = g_key_file_get_string(config, "browser",
655 "accepted_language", NULL);
656 cfg.history_file = g_key_file_get_string(config, "browser", "history_file",
658 cfg.home_uri = g_key_file_get_string(config, "browser", "homepage", NULL);
659 cfg.home_uri = (cfg.home_uri) ? cfg.home_uri : "about:blank";
660 cfg.enable_console_to_stdout = g_key_file_get_boolean(config, "browser",
663 cfg.user_agent = g_key_file_get_string(config, "browser", "user_agent", NULL);
664 cfg.javascript_disabled = g_key_file_get_boolean(config, "browser",
665 "javascript_disabled", NULL);
666 char *input_cookie_policy = g_key_file_get_string(config, "browser",
667 "cookie_policy", NULL);
668 cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
669 if (input_cookie_policy) {
670 if (strcmp(input_cookie_policy, "all") == 0) {
671 cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
672 } else if (strcmp(input_cookie_policy, "none") == 0) {
673 cfg.cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
677 cfg.tab_width_chars = g_key_file_get_integer(config, "ui", "tab_width", NULL);
678 cfg.tab_width_chars = (cfg.tab_width_chars) ? cfg.tab_width_chars : 20;
679 cfg.global_zoom = g_key_file_get_double(config, "ui", "zoom_level", NULL);
680 cfg.global_zoom = (cfg.global_zoom) ? cfg.global_zoom : 1.0;
681 cfg.search_engine = g_key_file_get_string(config, "ui", "search_engine",
683 cfg.search_engine = (cfg.search_engine) ? cfg.search_engine :
684 "https://duckduckgo.com?q=";
685 cfg.spellcheck_disabled = g_key_file_get_boolean(config, "ui",
686 "spellcheck_disabled", NULL);
687 cfg.spellcheck_language = g_key_file_get_string(config, "ui",
688 "spellcheck_language",
690 cfg.spellcheck_language = (cfg.spellcheck_language) ? cfg.spellcheck_language
695 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
697 struct Client *c = (struct Client *)data;
698 WebKitJavascriptResult *js_result;
700 JSCException *exception;
704 g_free(c->feed_html);
707 /* This was taken almost verbatim from the example in WebKit's
710 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
712 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
715 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
720 value = webkit_javascript_result_get_js_value(js_result);
721 if (jsc_value_is_string(value)) {
722 str_value = jsc_value_to_string(value);
723 exception = jsc_context_get_exception(jsc_value_get_context(value));
724 if (exception != NULL) {
725 fprintf(stderr, __NAME__": Error running javascript: %s\n",
726 jsc_exception_get_message(exception));
728 c->feed_html = str_value;
731 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
732 GTK_ENTRY_ICON_PRIMARY,
733 "application-rss+xml-symbolic");
734 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
735 GTK_ENTRY_ICON_PRIMARY,
738 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
739 GTK_ENTRY_ICON_PRIMARY,
743 webkit_javascript_result_unref(js_result);
747 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
750 struct Client *c = (struct Client *)data;
753 g_free(c->hover_uri);
755 if (webkit_hit_test_result_context_is_link(ht)) {
756 to_show = webkit_hit_test_result_get_link_uri(ht);
757 c->hover_uri = g_strdup(to_show);
759 to_show = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
763 if (!gtk_widget_is_focus(c->location))
764 gtk_entry_set_text(GTK_ENTRY(c->location), to_show);
768 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
771 struct Client *c = (struct Client *)data;
773 gchar *data_template =
778 " <meta charset=\"UTF-8\">"
779 " <title>Feeds</title>"
782 " <p>Feeds found on this page:</p>"
789 if (c->feed_html != NULL)
791 /* What we're actually trying to do is show a simple HTML page
792 * that lists all the feeds on the current page. The function
793 * webkit_web_view_load_html() looks like the proper way to do
794 * that. Sad thing is, it doesn't create a history entry, but
795 * instead simply replaces the content of the current page. This
796 * is not what we want.
798 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
799 * mechanism to show my custom HTML snippet *and* create a
802 * [0]: https://tools.ietf.org/html/rfc2397
803 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
804 d = g_strdup_printf(data_template, c->feed_html);
805 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
811 init_default_web_context(void)
814 WebKitWebContext *wc;
815 WebKitCookieManager *cm;
817 wc = webkit_web_context_get_default();
819 p = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock", NULL);
820 webkit_web_context_set_sandbox_enabled(wc, TRUE);
821 webkit_web_context_add_path_to_sandbox(wc, p, TRUE);
824 webkit_web_context_set_process_model(wc,
825 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
827 p = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
829 webkit_web_context_set_web_extensions_directory(wc, p);
832 if (cfg.accepted_language[0] != NULL)
833 webkit_web_context_set_preferred_languages(wc, cfg.accepted_language);
835 g_signal_connect(G_OBJECT(wc), "download-started",
836 G_CALLBACK(download_handle_start), NULL);
838 trust_user_certs(wc);
840 cm = webkit_web_context_get_cookie_manager(wc);
841 webkit_cookie_manager_set_accept_policy(cm, cfg.cookie_policy);
843 webkit_web_context_set_favicon_database_directory(wc, NULL);
844 webkit_cookie_manager_set_persistent_storage(cm,
845 g_build_filename("/",
846 g_get_user_cache_dir(),
850 WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
852 const gchar * const languages[2] = {(const gchar *)cfg.spellcheck_language,
854 webkit_web_context_set_spell_checking_languages(wc, languages);
855 webkit_web_context_set_spell_checking_enabled(wc, !cfg.spellcheck_disabled);
859 search(gpointer data, gint direction)
861 struct Client *c = (struct Client *)data;
862 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
863 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
865 if (search_text == NULL)
870 webkit_find_controller_search(fc, search_text,
871 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
872 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
876 webkit_find_controller_search_next(fc);
879 webkit_find_controller_search_previous(fc);
882 webkit_find_controller_search_finish(fc);
888 search_init(struct Client *c, int direction)
890 gtk_widget_grab_focus(c->location);
891 const gchar *contents = gtk_entry_get_text(GTK_ENTRY(c->location));
892 if (strcspn(contents, "s/")) {
893 gtk_entry_set_text(GTK_ENTRY(c->location), "s/");
895 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
898 search(c, direction);
902 def_key(char *key, unsigned int def)
904 char *conf = g_key_file_get_string(config, "keybindings", key, NULL);
905 return (conf) ? gdk_keyval_from_name((conf) ? conf : NULL) : def;
909 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
911 struct Client *c = (struct Client *)data;
915 if (event->type == GDK_KEY_PRESS) {
916 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK) {
917 WebKitSettings *settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
918 gboolean js = webkit_settings_get_enable_javascript(settings);
919 int key = ((GdkEventKey *)event)->keyval;
920 if (def_key("download_manager", GDK_KEY_y) == key) {
921 gtk_widget_show_all(dm.win);
923 } else if (def_key("history_back", GDK_KEY_h) == key) {
924 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
926 } else if (def_key("history_forwards", GDK_KEY_l) == key) {
927 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
929 } else if (def_key("location", GDK_KEY_t) == key) {
930 gtk_widget_grab_focus(c->location);
931 const char *uri = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
932 const char *goal = (uri) ? uri : "https://";
933 gtk_entry_set_text(GTK_ENTRY(c->location), goal);
934 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
936 } else if (def_key("print", GDK_KEY_Print) == key) {
937 webkit_print_operation_run_dialog(webkit_print_operation_new(WEBKIT_WEB_VIEW(c->web_view)),
938 GTK_WINDOW(gtk_widget_get_toplevel(mw.win)));
940 } else if (def_key("quit", GDK_KEY_g) == key) {
942 gtk_widget_grab_focus(c->web_view);
943 gtk_entry_set_text(GTK_ENTRY(c->location),
944 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
945 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
946 "window.getSelection().removeAllRanges();"
947 "document.activeElement.blur();",
950 } else if (def_key("reload", GDK_KEY_e) == key) {
951 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(c->web_view));
953 } else if (def_key("scroll_line_down", GDK_KEY_j) == key) {
954 for (int i = 0; i < 2; i++) {
955 event->key.keyval = GDK_KEY_Down;
956 gdk_event_put(event);
959 } else if (def_key("scroll_line_up", GDK_KEY_k) == key) {
960 event->key.keyval = GDK_KEY_Up;
961 gdk_event_put(event);
963 } else if (def_key("scroll_page_down", GDK_KEY_f) == key) {
964 event->key.keyval = GDK_KEY_Page_Down;
965 gdk_event_put(event);
967 } else if (def_key("scroll_page_up", GDK_KEY_b) == key) {
968 event->key.keyval = GDK_KEY_Page_Up;
969 gdk_event_put(event);
971 } else if (def_key("search_forwards", GDK_KEY_s) == key) {
974 } else if (def_key("search_backwards", GDK_KEY_r) == key) {
977 } else if (def_key("tab_close", GDK_KEY_q) == key) {
978 client_destroy(NULL, c);
980 } else if (def_key("tab_switch_1", GDK_KEY_1) == key) {
981 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 0);
983 } else if (def_key("tab_switch_2", GDK_KEY_2) == key) {
984 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 1);
986 } else if (def_key("tab_switch_3", GDK_KEY_3) == key) {
987 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 2);
989 } else if (def_key("tab_switch_4", GDK_KEY_4) == key) {
990 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 3);
992 } else if (def_key("tab_switch_5", GDK_KEY_5) == key) {
993 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 4);
995 } else if (def_key("tab_switch_6", GDK_KEY_6) == key) {
996 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 5);
998 } else if (def_key("tab_switch_7", GDK_KEY_7) == key) {
999 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 6);
1001 } else if (def_key("tab_switch_8", GDK_KEY_8) == key) {
1002 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 7);
1004 } else if (def_key("tab_switch_9", GDK_KEY_9) == key) {
1005 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), 8);
1007 } else if (def_key("tab_previous", GDK_KEY_u) == key) {
1008 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
1010 } else if (def_key("tab_new", GDK_KEY_w) == key) {
1011 f = ensure_uri_scheme(cfg.home_uri);
1012 client_new(f, NULL, TRUE, TRUE);
1015 } else if (def_key("tab_next", GDK_KEY_i) == key) {
1016 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
1018 } else if (def_key("toggle_js", GDK_KEY_o) == key) {
1019 webkit_settings_set_enable_javascript(settings, (js) ? FALSE : TRUE);
1020 webkit_web_view_set_settings(WEBKIT_WEB_VIEW(c->web_view), settings);
1022 } else if (def_key("web_search", GDK_KEY_d) == key) {
1023 gtk_widget_grab_focus(c->location);
1024 gtk_entry_set_text(GTK_ENTRY(c->location), "w/");
1025 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
1027 } else if (def_key("zoom_in", GDK_KEY_equal) == key) {
1028 now = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1029 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), now + 0.1);
1031 } else if (def_key("zoom_out", GDK_KEY_minus) == key) {
1032 now = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1033 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), now - 0.1);
1035 } else if (def_key("zoom_reset", GDK_KEY_0) == key) {
1036 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view),
1046 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
1048 if (event->type == GDK_KEY_PRESS) {
1049 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK) {
1050 int key = ((GdkEventKey *)event)->keyval;
1051 if ((def_key("close_tab", GDK_KEY_q) == key) ||
1052 (def_key("download_manager", GDK_KEY_y) == key)) {
1053 downloadmanager_delete(dm.win, NULL);
1063 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
1065 struct Client *c = (struct Client *)data;
1069 if (key_common(widget, event, data))
1072 if (event->type == GDK_KEY_PRESS) {
1073 int key = ((GdkEventKey *)event)->keyval;
1074 if ((GDK_KEY_KP_Enter == key) || (GDK_KEY_Return == key)) {
1075 gtk_widget_grab_focus(c->web_view);
1076 t = gtk_entry_get_text(GTK_ENTRY(c->location));
1077 if (t != NULL && t[0] == 's' && t[1] == '/') {
1078 if (search_text != NULL)
1079 g_free(search_text);
1080 search_text = g_strdup(t + 2);
1082 } else if (t != NULL && t[0] == 'w' && t[1] == '/') {
1083 const char *engine = cfg.search_engine;
1084 int len = strlen(engine) + strlen(t) - 2;
1085 char *f = (char *) malloc(len);
1086 snprintf(f, len + 1, "%s%s", engine, t + 2);
1087 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
1090 f = ensure_uri_scheme(t);
1091 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
1095 } else if ((GDK_KEY_Escape == key) ||
1096 (def_key("quit", GDK_KEY_g) == key)) {
1097 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
1098 gtk_entry_set_text(GTK_ENTRY(c->location),
1099 (t == NULL ? __NAME__ : t));
1107 key_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
1109 GdkScrollDirection direction;
1111 if (event->type == GDK_BUTTON_RELEASE) {
1112 switch (((GdkEventButton *)event)->button) {
1114 client_destroy(NULL, data);
1117 } else if (event->type == GDK_SCROLL) {
1118 gdk_event_get_scroll_direction(event, &direction);
1119 switch (direction) {
1121 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
1123 case GDK_SCROLL_DOWN:
1124 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
1135 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
1137 struct Client *c = (struct Client *)data;
1141 if (key_common(widget, event, data))
1144 if (event->type == GDK_KEY_PRESS) {
1145 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape) {
1146 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
1147 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
1149 } else if (event->type == GDK_BUTTON_RELEASE) {
1150 switch (((GdkEventButton *)event)->button) {
1152 if (c->hover_uri != NULL) {
1153 client_new(c->hover_uri, NULL, TRUE, FALSE);
1158 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1161 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
1164 } else if (event->type == GDK_SCROLL) {
1165 if (((GdkEventScroll *)event)->state & GDK_CONTROL_MASK ||
1166 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK) {
1167 gdk_event_get_scroll_deltas(event, &dx, &dy);
1168 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1170 z = dx != 0 ? cfg.global_zoom : z;
1171 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1180 mainwindow_setup(void)
1182 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1183 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1184 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1185 gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
1187 mw.notebook = gtk_notebook_new();
1188 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1189 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1190 g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
1191 G_CALLBACK(notebook_switch_page), NULL);
1195 mainwindow_title(gint idx)
1197 GtkWidget *child, *widg, *tablabel;
1200 child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
1204 widg = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
1205 tablabel = (GtkWidget *)g_object_get_data(G_OBJECT(widg), "lariza-tab-label");
1206 text = gtk_label_get_text(GTK_LABEL(tablabel));
1207 gtk_window_set_title(GTK_WINDOW(mw.win), text);
1211 notebook_switch_page(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
1213 mainwindow_title(idx);
1217 quit_if_nothing_active(void)
1220 if (downloads == 0) {
1224 gtk_widget_show_all(dm.win);
1232 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1236 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1239 client_new(uri, NULL, TRUE, TRUE);
1246 run_user_scripts(WebKitWebView *web_view)
1248 gchar *base = NULL, *path = NULL, *contents = NULL;
1249 const gchar *entry = NULL;
1250 GDir *scriptdir = NULL;
1252 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts",
1254 scriptdir = g_dir_open(base, 0, NULL);
1255 if (scriptdir != NULL) {
1256 while ((entry = g_dir_read_name(scriptdir)) != NULL) {
1257 path = g_build_filename(base, entry, NULL);
1258 char *jscmd = malloc(strlen(path) + 36);
1259 sprintf(jscmd, "console.log(\"Running userscript %s\");", path);
1260 if (g_str_has_suffix(path, ".js")) {
1261 if (g_file_get_contents(path, &contents, NULL, NULL)) {
1262 webkit_web_view_run_javascript(web_view, jscmd, NULL, NULL, NULL);
1263 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1270 g_dir_close(scriptdir);
1277 show_web_view(WebKitWebView *web_view, gpointer data)
1279 struct Client *c = (struct Client *)data;
1284 gtk_widget_show_all(mw.win);
1286 if (c->focus_new_tab) {
1287 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1289 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
1291 gtk_widget_grab_focus(c->web_view);
1296 trust_user_certs(WebKitWebContext *wc)
1298 GTlsCertificate *cert;
1299 const gchar *basedir, *file, *absfile;
1302 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1303 dir = g_dir_open(basedir, 0, NULL);
1305 file = g_dir_read_name(dir);
1306 while (file != NULL) {
1307 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1310 cert = g_tls_certificate_new_from_file(absfile, NULL);
1312 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1314 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1315 file = g_dir_read_name(dir);
1324 GKeyFileFlags flags = G_KEY_FILE_NONE;
1325 g_autoptr(GError) error = NULL;
1326 config = g_key_file_new();
1329 if (!g_key_file_load_from_file(config,
1330 g_build_filename(g_get_user_config_dir(),
1331 __NAME__, "lariza.ini",
1332 NULL), flags, &error)) {
1333 // Load global config
1334 if (!g_key_file_load_from_file(config, "/etc/lariza.ini", flags,
1336 fprintf(stderr, "Could not load lariza.ini: %s", error->message);
1343 main(int argc, char **argv)
1347 gtk_init(&argc, &argv);
1350 while ((opt = getopt(argc, argv, "C")) != -1) {
1353 cfg.cooperative_instances = FALSE;
1356 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1361 if (cfg.cooperative_instances)
1362 cooperation_setup();
1364 if (!cfg.cooperative_instances || cfg.cooperative_alone)
1365 init_default_web_context();
1367 downloadmanager_setup();
1370 if (optind >= argc) {
1371 client_new(cfg.home_uri, NULL, TRUE, TRUE);
1373 for (i = optind; i < argc; i++)
1374 client_new(argv[i], NULL, TRUE, TRUE);
1377 if (!cfg.cooperative_instances || cfg.cooperative_alone)