11 #include <gdk/gdkkeysyms.h>
13 #include <webkit2/webkit2.h>
14 #include <JavaScriptCore/JavaScript.h>
17 void client_destroy(GtkWidget *, gpointer);
18 WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean,
20 WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
22 void cooperation_setup(void);
23 void changed_download_progress(GObject *, GParamSpec *, gpointer);
24 void changed_load_progress(GObject *, GParamSpec *, gpointer);
25 void changed_favicon(GObject *, GParamSpec *, gpointer);
26 void changed_title(GObject *, GParamSpec *, gpointer);
27 void changed_uri(GObject *, GParamSpec *, gpointer);
28 gboolean crashed_web_view(WebKitWebView *, gpointer);
29 gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
30 WebKitPolicyDecisionType, gpointer);
31 gboolean download_handle(WebKitDownload *, gchar *, gpointer);
32 void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
33 void downloadmanager_cancel(GtkToolButton *, gpointer);
34 gboolean downloadmanager_delete(GtkWidget *, gpointer);
35 void downloadmanager_setup(void);
36 gchar *ensure_uri_scheme(const gchar *);
37 void grab_environment_configuration(void);
38 void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
39 void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
40 void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
41 void init_default_web_context(void);
42 gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
43 gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
44 gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
45 gboolean key_tablabel(GtkWidget *, GdkEvent *, gpointer);
46 gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
47 void mainwindow_setup(void);
48 void mainwindow_title(gint);
49 void notebook_switch_page(GtkNotebook *, GtkWidget *, guint, gpointer);
50 gboolean quit_if_nothing_active(void);
51 gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
52 void run_user_scripts(WebKitWebView *);
53 void search(gpointer, gint);
54 void show_web_view(WebKitWebView *, gpointer);
55 void trust_user_certs(WebKitWebContext *);
56 GKeyFile *get_ini(void);
61 gchar *external_handler_uri;
69 gboolean focus_new_tab;
78 struct DownloadManager
86 const gchar *accepted_language[2] = { NULL, NULL };
87 gint clients = 0, downloads = 0;
88 gboolean cooperative_alone = TRUE;
89 gboolean cooperative_instances = TRUE;
90 int cooperative_pipe_fp = 0;
91 gboolean enable_console_to_stdout;
92 gchar *fifo_suffix = "main";
100 gboolean javascript_disabled;
103 client_destroy(GtkWidget *widget, gpointer data)
105 struct Client *c = (struct Client *)data;
108 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
109 changed_load_progress, c);
111 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
113 fprintf(stderr, __NAME__": Tab index was -1, bamboozled\n");
115 gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
120 quit_if_nothing_active();
124 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
129 GtkWidget *evbox, *tabbox;
131 if (uri != NULL && cooperative_instances && !cooperative_alone) {
132 f = ensure_uri_scheme(uri);
133 write(cooperative_pipe_fp, f, strlen(f));
134 write(cooperative_pipe_fp, "\n", 1);
139 c = calloc(1, sizeof(struct Client));
141 fprintf(stderr, __NAME__": fatal: calloc failed\n");
145 c->focus_new_tab = focus_tab;
147 if (related_wv == NULL)
148 c->web_view = webkit_web_view_new();
150 c->web_view = webkit_web_view_new_with_related_view(related_wv);
152 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
153 WebKitSettings *settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
154 webkit_settings_set_enable_javascript(settings, !javascript_disabled);
155 g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
156 G_CALLBACK(changed_favicon), c);
157 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
158 G_CALLBACK(changed_title), c);
159 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
160 G_CALLBACK(changed_uri), c);
161 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
162 G_CALLBACK(changed_load_progress), c);
163 g_signal_connect(G_OBJECT(c->web_view), "create",
164 G_CALLBACK(client_new_request), NULL);
165 g_signal_connect(G_OBJECT(c->web_view), "close",
166 G_CALLBACK(client_destroy), c);
167 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
168 G_CALLBACK(decide_policy), NULL);
169 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
170 G_CALLBACK(key_web_view), c);
171 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
172 G_CALLBACK(key_web_view), c);
173 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
174 G_CALLBACK(key_web_view), c);
175 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
176 G_CALLBACK(hover_web_view), c);
177 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
178 G_CALLBACK(crashed_web_view), c);
180 if (user_agent != NULL)
181 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
182 "user-agent", user_agent, NULL);
184 if (enable_console_to_stdout)
185 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
187 webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
189 c->location = gtk_entry_new();
190 g_signal_connect(G_OBJECT(c->location), "key-press-event",
191 G_CALLBACK(key_location), c);
192 g_signal_connect(G_OBJECT(c->location), "icon-release",
193 G_CALLBACK(icon_location), c);
194 /* XXX This is a workaround. Setting this to NULL (which is done in
195 * grab_feeds_finished() if no feed has been detected) adds a little
196 * padding left of the text. Not sure why. The point of this call
197 * right here is to have that padding right from the start. This
198 * avoids a graphical artifact. */
199 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
200 GTK_ENTRY_ICON_PRIMARY,
203 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
204 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
205 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
206 gtk_container_set_focus_child(GTK_CONTAINER(c->vbox), c->web_view);
208 c->tabicon = gtk_image_new_from_icon_name("text-html", GTK_ICON_SIZE_SMALL_TOOLBAR);
210 c->tablabel = gtk_label_new(__NAME__);
211 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
212 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), tab_width_chars);
213 gtk_widget_set_has_tooltip(c->tablabel, TRUE);
215 /* XXX I don't own a HiDPI screen, so I don't know if scale_factor
216 * does the right thing. */
217 tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
218 5 * gtk_widget_get_scale_factor(mw.win));
219 gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
220 gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
222 evbox = gtk_event_box_new();
223 gtk_container_add(GTK_CONTAINER(evbox), tabbox);
224 g_signal_connect(G_OBJECT(evbox), "button-release-event",
225 G_CALLBACK(key_tablabel), c);
227 gtk_widget_add_events(evbox, GDK_SCROLL_MASK);
228 g_signal_connect(G_OBJECT(evbox), "scroll-event",
229 G_CALLBACK(key_tablabel), c);
231 /* For easy access, store a reference to our label. */
232 g_object_set_data(G_OBJECT(evbox), "lariza-tab-label", c->tablabel);
234 /* This only shows the event box and the label inside, nothing else.
235 * Needed because the evbox/label is "internal" to the notebook and
236 * not part of the normal "widget tree" (IIUC). */
237 gtk_widget_show_all(evbox);
239 gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox,
240 gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1);
241 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
244 show_web_view(NULL, c);
246 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
247 G_CALLBACK(show_web_view), c);
250 f = ensure_uri_scheme(uri);
251 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
257 return WEBKIT_WEB_VIEW(c->web_view);
261 client_new_request(WebKitWebView *web_view,
262 WebKitNavigationAction *navigation_action, gpointer data)
264 return client_new(NULL, web_view, FALSE, FALSE);
268 cooperation_setup(void)
271 gchar *fifofilename, *fifopath;
273 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
274 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
275 g_free(fifofilename);
277 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
278 mkfifo(fifopath, 0600);
280 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
281 if (!cooperative_pipe_fp) {
282 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
284 if (write(cooperative_pipe_fp, "", 0) == -1) {
285 /* Could not do an empty write to the FIFO which means there's
286 * no one listening. */
287 close(cooperative_pipe_fp);
288 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
289 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
291 cooperative_alone = FALSE;
299 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
301 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
302 WebKitURIResponse *resp;
303 GtkToolItem *tb = GTK_TOOL_ITEM(data);
306 gchar *t, *filename, *base;
308 p = webkit_download_get_estimated_progress(download);
312 resp = webkit_download_get_response(download);
313 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
315 uri = webkit_download_get_destination(download);
316 filename = g_filename_from_uri(uri, NULL, NULL);
317 if (filename == NULL) {
318 /* This really should not happen because WebKit uses that URI to
319 * write to a file... */
320 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
321 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
322 webkit_uri_response_get_uri(resp), p, size_mb);
324 base = g_path_get_basename(filename);
325 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
329 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
334 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
336 struct Client *c = (struct Client *)data;
339 "a = document.querySelectorAll('"
340 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
341 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
348 " for (i = 0; i < a.length; i++)"
350 " url = encodeURIComponent(a[i].href);"
351 " if ('title' in a[i] && a[i].title != '')"
352 " title = encodeURIComponent(a[i].title);"
355 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
361 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
365 /* The page has loaded fully. We now run the short JavaScript
366 * snippet above that operates on the DOM. It tries to grab all
367 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
369 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
371 grab_feeds_finished, c);
373 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
375 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
379 changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
381 struct Client *c = (struct Client *)data;
383 int w, h, w_should, h_should;
384 GdkPixbuf *pb, *pb_scaled;
386 f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
388 gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html",
389 GTK_ICON_SIZE_SMALL_TOOLBAR);
391 w = cairo_image_surface_get_width(f);
392 h = cairo_image_surface_get_height(f);
393 pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
395 w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
396 h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
397 pb_scaled = gdk_pixbuf_scale_simple(pb, w_should, h_should,
398 GDK_INTERP_BILINEAR);
399 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon), pb_scaled);
401 g_object_unref(pb_scaled);
408 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
411 struct Client *c = (struct Client *)data;
413 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
414 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
416 u = u == NULL ? __NAME__ : u;
417 u = u[0] == 0 ? __NAME__ : u;
419 t = t == NULL ? u : t;
420 t = t[0] == 0 ? u : t;
422 gtk_label_set_text(GTK_LABEL(c->tablabel), t);
423 gtk_widget_set_tooltip_text(c->tablabel, t);
424 mainwindow_title(gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)));
428 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
431 struct Client *c = (struct Client *)data;
434 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
436 /* When a web process crashes, we get a "notify::uri" signal, but we
437 * can no longer read a meaningful URI. It's just an empty string
438 * now. Not updating the location bar in this scenario is important,
439 * because we would override the "WEB PROCESS CRASHED" message. */
440 if (t != NULL && strlen(t) > 0) {
441 gtk_entry_set_text(GTK_ENTRY(c->location), t);
443 if (history_file != NULL) {
444 fp = fopen(history_file, "a");
446 fprintf(fp, "%s\n", t);
449 perror(__NAME__": Error opening history file");
456 crashed_web_view(WebKitWebView *web_view, gpointer data)
459 struct Client *c = (struct Client *)data;
461 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
462 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
463 gtk_entry_set_text(GTK_ENTRY(c->location), t);
470 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
471 WebKitPolicyDecisionType type, gpointer data)
473 WebKitResponsePolicyDecision *r;
476 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
477 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
478 if (!webkit_response_policy_decision_is_mime_type_supported(r))
479 webkit_policy_decision_download(decision);
481 webkit_policy_decision_use(decision);
484 /* Use whatever default there is. */
491 download_handle_finished(WebKitDownload *download, gpointer data)
497 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
500 g_signal_connect(G_OBJECT(download), "decide-destination",
501 G_CALLBACK(download_handle), data);
505 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
507 gchar *sug_clean, *path, *path2 = NULL, *uri;
512 sug_clean = g_strdup(suggested_filename);
513 for (i = 0; i < strlen(sug_clean); i++)
514 if (sug_clean[i] == G_DIR_SEPARATOR)
517 path = g_build_filename(download_dir, sug_clean, NULL);
518 path2 = g_strdup(path);
519 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000) {
522 path2 = g_strdup_printf("%s.%d", path, suffix);
526 if (suffix == 1000) {
527 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
528 webkit_download_cancel(download);
530 uri = g_filename_to_uri(path2, NULL, NULL);
531 webkit_download_set_destination(download, uri);
534 tb = gtk_tool_button_new(NULL, NULL);
535 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
536 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
537 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
538 gtk_widget_show_all(dm.win);
540 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
541 G_CALLBACK(changed_download_progress), tb);
544 g_signal_connect(G_OBJECT(download), "finished",
545 G_CALLBACK(download_handle_finished), NULL);
547 g_object_ref(download);
548 g_signal_connect(G_OBJECT(tb), "clicked",
549 G_CALLBACK(downloadmanager_cancel), download);
556 /* Propagate -- to whom it may concern. */
561 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
563 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
565 webkit_download_cancel(download);
566 g_object_unref(download);
568 gtk_widget_destroy(GTK_WIDGET(tb));
572 downloadmanager_delete(GtkWidget *obj, gpointer data)
574 if (!quit_if_nothing_active())
575 gtk_widget_hide(dm.win);
581 downloadmanager_setup(void)
583 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
584 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
585 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
586 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
587 g_signal_connect(G_OBJECT(dm.win), "delete-event",
588 G_CALLBACK(downloadmanager_delete), NULL);
589 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
590 G_CALLBACK(key_downloadmanager), NULL);
592 dm.toolbar = gtk_toolbar_new();
593 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
594 GTK_ORIENTATION_VERTICAL);
595 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
596 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
598 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
599 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
600 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
601 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
603 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
607 ensure_uri_scheme(const gchar *t)
611 f = g_ascii_strdown(t, -1);
612 if (!g_str_has_prefix(f, "http:") &&
613 !g_str_has_prefix(f, "https:") &&
614 !g_str_has_prefix(f, "file:") &&
615 !g_str_has_prefix(f, "about:") &&
616 !g_str_has_prefix(f, "data:") &&
617 !g_str_has_prefix(f, "webkit:")) {
619 fabs = realpath(t, NULL);
621 f = g_strdup_printf("file://%s", fabs);
624 f = g_strdup_printf("http://%s", t);
636 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
638 fifo_suffix = g_strdup(e);
640 char *xdg_down = getenv("XDG_DOWNLOAD_DIR");
641 download_dir = (xdg_down) ? xdg_down : "/var/tmp";
644 accepted_language[0] = g_key_file_get_string(config, "browser",
645 "accepted_language", NULL);
646 history_file = g_key_file_get_string(config, "browser", "history_file", NULL);
647 home_uri = g_key_file_get_string(config, "browser", "homepage", NULL);
648 enable_console_to_stdout = g_key_file_get_boolean(config, "browser",
649 "console_to_stdout", NULL);
650 user_agent = g_key_file_get_string(config, "browser", "user_agent", NULL);
651 javascript_disabled = g_key_file_get_boolean(config, "browser",
652 "javascript_disabled", NULL);
653 char *input_cookie_policy = g_key_file_get_string(config, "browser",
654 "cookie_policy", NULL);
655 cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY;
656 if (input_cookie_policy) {
657 if (strcmp(input_cookie_policy, "all") == 0) {
658 cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_ALWAYS;
659 } else if (strcmp(input_cookie_policy, "none") == 0) {
660 cookie_policy = WEBKIT_COOKIE_POLICY_ACCEPT_NEVER;
664 tab_width_chars = g_key_file_get_integer(config, "ui", "tab_width", NULL);
665 tab_width_chars = (tab_width_chars) ? tab_width_chars : 20;
666 global_zoom = g_key_file_get_double(config, "ui", "zoom_level", NULL);
667 global_zoom = (global_zoom) ? global_zoom : 1.0;
668 search_engine = g_key_file_get_string(config, "ui", "search_engine", NULL);
669 search_engine = (search_engine) ? search_engine : "https://duckduckgo.com?q=";
673 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
675 struct Client *c = (struct Client *)data;
676 WebKitJavascriptResult *js_result;
678 JSCException *exception;
682 g_free(c->feed_html);
685 /* This was taken almost verbatim from the example in WebKit's
688 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
690 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
693 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
698 value = webkit_javascript_result_get_js_value(js_result);
699 if (jsc_value_is_string(value)) {
700 str_value = jsc_value_to_string(value);
701 exception = jsc_context_get_exception(jsc_value_get_context(value));
702 if (exception != NULL) {
703 fprintf(stderr, __NAME__": Error running javascript: %s\n",
704 jsc_exception_get_message(exception));
706 c->feed_html = str_value;
709 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
710 GTK_ENTRY_ICON_PRIMARY,
711 "application-rss+xml-symbolic");
712 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
713 GTK_ENTRY_ICON_PRIMARY,
716 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
717 GTK_ENTRY_ICON_PRIMARY,
721 webkit_javascript_result_unref(js_result);
725 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
728 struct Client *c = (struct Client *)data;
731 g_free(c->hover_uri);
733 if (webkit_hit_test_result_context_is_link(ht)) {
734 to_show = webkit_hit_test_result_get_link_uri(ht);
735 c->hover_uri = g_strdup(to_show);
737 to_show = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
741 if (!gtk_widget_is_focus(c->location))
742 gtk_entry_set_text(GTK_ENTRY(c->location), to_show);
746 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
749 struct Client *c = (struct Client *)data;
751 gchar *data_template =
756 " <meta charset=\"UTF-8\">"
757 " <title>Feeds</title>"
760 " <p>Feeds found on this page:</p>"
767 if (c->feed_html != NULL)
769 /* What we're actually trying to do is show a simple HTML page
770 * that lists all the feeds on the current page. The function
771 * webkit_web_view_load_html() looks like the proper way to do
772 * that. Sad thing is, it doesn't create a history entry, but
773 * instead simply replaces the content of the current page. This
774 * is not what we want.
776 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
777 * mechanism to show my custom HTML snippet *and* create a
780 * [0]: https://tools.ietf.org/html/rfc2397
781 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
782 d = g_strdup_printf(data_template, c->feed_html);
783 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
789 init_default_web_context(void)
792 WebKitWebContext *wc;
793 WebKitCookieManager *cm;
795 wc = webkit_web_context_get_default();
797 p = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock", NULL);
798 webkit_web_context_set_sandbox_enabled(wc, TRUE);
799 webkit_web_context_add_path_to_sandbox(wc, p, TRUE);
802 webkit_web_context_set_process_model(wc,
803 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
805 p = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
807 webkit_web_context_set_web_extensions_directory(wc, p);
810 if (accepted_language[0] != NULL)
811 webkit_web_context_set_preferred_languages(wc, accepted_language);
813 g_signal_connect(G_OBJECT(wc), "download-started",
814 G_CALLBACK(download_handle_start), NULL);
816 trust_user_certs(wc);
818 cm = webkit_web_context_get_cookie_manager(wc);
819 webkit_cookie_manager_set_accept_policy(cm, cookie_policy);
821 webkit_web_context_set_favicon_database_directory(wc, NULL);
822 webkit_cookie_manager_set_persistent_storage(cm,
823 g_build_filename("/",
824 g_get_user_cache_dir(),
828 WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
832 search(gpointer data, gint direction)
834 struct Client *c = (struct Client *)data;
835 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
836 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
838 if (search_text == NULL)
843 webkit_find_controller_search(fc, search_text,
844 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
845 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
849 webkit_find_controller_search_next(fc);
852 webkit_find_controller_search_previous(fc);
855 webkit_find_controller_search_finish(fc);
861 search_init(struct Client *c, int direction)
863 gtk_widget_grab_focus(c->location);
864 const gchar *contents = gtk_entry_get_text(GTK_ENTRY(c->location));
865 if (strcspn(contents, "s/")) {
866 gtk_entry_set_text(GTK_ENTRY(c->location), "s/");
868 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
871 search(c, direction);
875 def_key(char *key, unsigned int def)
877 char *conf = g_key_file_get_string(config, "keybindings", key, NULL);
878 return (conf) ? gdk_keyval_from_name((conf) ? conf : NULL) : def;
882 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
884 struct Client *c = (struct Client *)data;
888 if (event->type == GDK_KEY_PRESS) {
889 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK) {
890 WebKitSettings *settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
891 gboolean js = webkit_settings_get_enable_javascript(settings);
892 int key = ((GdkEventKey *)event)->keyval;
893 if (def_key("close_tab", GDK_KEY_q) == key) {
894 client_destroy(NULL, c);
896 } else if (def_key("new_tab", GDK_KEY_w) == key) {
897 f = ensure_uri_scheme(home_uri);
898 client_new(f, NULL, TRUE, TRUE);
901 } else if (def_key("reload", GDK_KEY_e) == key) {
902 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(c->web_view));
904 } else if (def_key("download_manager", GDK_KEY_y) == key) {
905 gtk_widget_show_all(dm.win);
907 } else if (def_key("web_search", GDK_KEY_d) == key) {
908 gtk_widget_grab_focus(c->location);
909 gtk_entry_set_text(GTK_ENTRY(c->location), "w/");
910 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
912 } else if (def_key("search_forwards", GDK_KEY_s) == key) {
915 } else if (def_key("search_backwards", GDK_KEY_r) == key) {
918 } else if (def_key("location", GDK_KEY_t) == key) {
919 gtk_widget_grab_focus(c->location);
920 const char *uri = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
921 const char *goal = (uri) ? uri : "https://";
922 gtk_entry_set_text(GTK_ENTRY(c->location), goal);
923 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
925 } else if (def_key("previous_tab", GDK_KEY_u) == key) {
926 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
928 } else if (def_key("next_tab", GDK_KEY_i) == key) {
929 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
931 } else if (def_key("history_back", GDK_KEY_h) == key) {
932 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
934 } else if (def_key("history_forwards", GDK_KEY_l) == key) {
935 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
937 } else if (def_key("scroll_down", GDK_KEY_j) == key) {
938 for (int i = 0; i < 2; i++) {
939 event->key.keyval = GDK_KEY_Down;
940 gdk_event_put(event);
943 } else if (def_key("scroll_up", GDK_KEY_k) == key) {
944 event->key.keyval = GDK_KEY_Up;
945 gdk_event_put(event);
947 } else if (def_key("scroll_page_down", GDK_KEY_f) == key) {
948 event->key.keyval = GDK_KEY_Page_Down;
949 gdk_event_put(event);
951 } else if (def_key("scroll_page_up", GDK_KEY_b) == key) {
952 event->key.keyval = GDK_KEY_Page_Up;
953 gdk_event_put(event);
955 } else if (def_key("toggle_js", GDK_KEY_o) == key) {
956 webkit_settings_set_enable_javascript(settings, (js) ? FALSE : TRUE);
957 webkit_web_view_set_settings(WEBKIT_WEB_VIEW(c->web_view), settings);
959 } else if (def_key("quit", GDK_KEY_g) == key) {
961 gtk_widget_grab_focus(c->web_view);
962 gtk_entry_set_text(GTK_ENTRY(c->location),
963 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
964 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
965 "window.getSelection().removeAllRanges();"
966 "document.activeElement.blur();",
969 } else if (def_key("zoom_in", GDK_KEY_equal) == key) {
970 now = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
971 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), now + 0.1);
973 } else if (def_key("zoom_out", GDK_KEY_minus) == key) {
974 now = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
975 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), now - 0.1);
977 } else if (def_key("zoom_reset", GDK_KEY_0) == key) {
978 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view),
988 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
990 if (event->type == GDK_KEY_PRESS) {
991 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK) {
992 int key = ((GdkEventKey *)event)->keyval;
993 if ((def_key("close_tab", GDK_KEY_q) == key) ||
994 (def_key("download_manager", GDK_KEY_y) == key)) {
995 downloadmanager_delete(dm.win, NULL);
1005 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
1007 struct Client *c = (struct Client *)data;
1011 if (key_common(widget, event, data))
1014 if (event->type == GDK_KEY_PRESS) {
1015 int key = ((GdkEventKey *)event)->keyval;
1016 if ((GDK_KEY_KP_Enter == key) || (GDK_KEY_Return == key)) {
1017 gtk_widget_grab_focus(c->web_view);
1018 t = gtk_entry_get_text(GTK_ENTRY(c->location));
1019 if (t != NULL && t[0] == 's' && t[1] == '/') {
1020 if (search_text != NULL)
1021 g_free(search_text);
1022 search_text = g_strdup(t + 2);
1024 } else if (t != NULL && t[0] == 'w' && t[1] == '/') {
1025 const char *engine = search_engine;
1026 int len = strlen(engine) + strlen(t) - 2;
1027 char *f = (char *) malloc(len);
1028 snprintf(f, len + 1, "%s%s", engine, t + 2);
1029 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
1032 f = ensure_uri_scheme(t);
1033 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
1037 } else if ((GDK_KEY_Escape == key) ||
1038 (def_key("quit", GDK_KEY_g) == key)) {
1039 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
1040 gtk_entry_set_text(GTK_ENTRY(c->location),
1041 (t == NULL ? __NAME__ : t));
1049 key_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
1051 GdkScrollDirection direction;
1053 if (event->type == GDK_BUTTON_RELEASE) {
1054 switch (((GdkEventButton *)event)->button) {
1056 client_destroy(NULL, data);
1059 } else if (event->type == GDK_SCROLL) {
1060 gdk_event_get_scroll_direction(event, &direction);
1061 switch (direction) {
1063 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
1065 case GDK_SCROLL_DOWN:
1066 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
1077 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
1079 struct Client *c = (struct Client *)data;
1083 if (key_common(widget, event, data))
1086 if (event->type == GDK_KEY_PRESS) {
1087 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape) {
1088 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
1089 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
1091 } else if (event->type == GDK_BUTTON_RELEASE) {
1092 switch (((GdkEventButton *)event)->button) {
1094 if (c->hover_uri != NULL) {
1095 client_new(c->hover_uri, NULL, TRUE, FALSE);
1100 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1103 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
1106 } else if (event->type == GDK_SCROLL) {
1107 if (((GdkEventScroll *)event)->state & GDK_CONTROL_MASK ||
1108 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK) {
1109 gdk_event_get_scroll_deltas(event, &dx, &dy);
1110 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1112 z = dx != 0 ? global_zoom : z;
1113 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1122 mainwindow_setup(void)
1124 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1125 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1126 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1127 gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
1129 mw.notebook = gtk_notebook_new();
1130 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1131 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1132 g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
1133 G_CALLBACK(notebook_switch_page), NULL);
1137 mainwindow_title(gint idx)
1139 GtkWidget *child, *widg, *tablabel;
1142 child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
1146 widg = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
1147 tablabel = (GtkWidget *)g_object_get_data(G_OBJECT(widg), "lariza-tab-label");
1148 text = gtk_label_get_text(GTK_LABEL(tablabel));
1149 gtk_window_set_title(GTK_WINDOW(mw.win), text);
1153 notebook_switch_page(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
1155 mainwindow_title(idx);
1159 quit_if_nothing_active(void)
1162 if (downloads == 0) {
1166 gtk_widget_show_all(dm.win);
1174 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1178 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1181 client_new(uri, NULL, TRUE, TRUE);
1188 run_user_scripts(WebKitWebView *web_view)
1190 gchar *base = NULL, *path = NULL, *contents = NULL;
1191 const gchar *entry = NULL;
1192 GDir *scriptdir = NULL;
1194 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts",
1196 scriptdir = g_dir_open(base, 0, NULL);
1197 if (scriptdir != NULL) {
1198 while ((entry = g_dir_read_name(scriptdir)) != NULL) {
1199 path = g_build_filename(base, entry, NULL);
1200 char *jscmd = malloc(strlen(path) + 36);
1201 sprintf(jscmd, "console.log(\"Running userscript %s\");", path);
1202 if (g_str_has_suffix(path, ".js")) {
1203 if (g_file_get_contents(path, &contents, NULL, NULL)) {
1204 webkit_web_view_run_javascript(web_view, jscmd, NULL, NULL, NULL);
1205 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1212 g_dir_close(scriptdir);
1219 show_web_view(WebKitWebView *web_view, gpointer data)
1221 struct Client *c = (struct Client *)data;
1226 gtk_widget_show_all(mw.win);
1228 if (c->focus_new_tab) {
1229 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1231 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
1233 gtk_widget_grab_focus(c->web_view);
1238 trust_user_certs(WebKitWebContext *wc)
1240 GTlsCertificate *cert;
1241 const gchar *basedir, *file, *absfile;
1244 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1245 dir = g_dir_open(basedir, 0, NULL);
1247 file = g_dir_read_name(dir);
1248 while (file != NULL) {
1249 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1252 cert = g_tls_certificate_new_from_file(absfile, NULL);
1254 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1256 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1257 file = g_dir_read_name(dir);
1266 GKeyFileFlags flags = G_KEY_FILE_NONE;
1267 g_autoptr(GError) error = NULL;
1268 config = g_key_file_new();
1271 if (!g_key_file_load_from_file(config,
1272 g_build_filename(g_get_user_config_dir(),
1273 __NAME__, "lariza.ini",
1274 NULL), flags, &error)) {
1275 // Load global config
1276 if (!g_key_file_load_from_file(config, "/etc/lariza.ini", flags,
1278 fprintf(stderr, "Could not load lariza.ini: %s", error->message);
1285 main(int argc, char **argv)
1289 gtk_init(&argc, &argv);
1292 while ((opt = getopt(argc, argv, "C")) != -1) {
1295 cooperative_instances = FALSE;
1298 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1303 if (cooperative_instances)
1304 cooperation_setup();
1306 if (!cooperative_instances || cooperative_alone)
1307 init_default_web_context();
1309 downloadmanager_setup();
1312 if (optind >= argc) {
1313 client_new(home_uri, NULL, TRUE, TRUE);
1315 for (i = optind; i < argc; i++)
1316 client_new(argv[i], NULL, TRUE, TRUE);
1319 if (!cooperative_instances || cooperative_alone)