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 *);
59 gchar *external_handler_uri;
67 gboolean focus_new_tab;
76 struct DownloadManager
84 const gchar *accepted_language[2] = { NULL, NULL };
85 gint clients = 0, downloads = 0;
86 gboolean cooperative_alone = TRUE;
87 gboolean cooperative_instances = TRUE;
88 int cooperative_pipe_fp = 0;
89 gchar *download_dir = "/var/tmp";
90 gboolean enable_console_to_stdout = FALSE;
91 gchar *fifo_suffix = "main";
92 gdouble global_zoom = 1.0;
93 gchar *history_file = NULL;
94 gchar *home_uri = "about:blank";
95 gchar *search_text = NULL;
96 GtkPositionType tab_pos = GTK_POS_TOP;
97 gint tab_width_chars = 20;
98 gchar *user_agent = NULL;
102 client_destroy(GtkWidget *widget, gpointer data)
104 struct Client *c = (struct Client *)data;
107 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
108 changed_load_progress, c);
110 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
112 fprintf(stderr, __NAME__": Tab index was -1, bamboozled\n");
114 gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
119 quit_if_nothing_active();
123 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
128 GtkWidget *evbox, *tabbox;
130 if (uri != NULL && cooperative_instances && !cooperative_alone) {
131 f = ensure_uri_scheme(uri);
132 write(cooperative_pipe_fp, f, strlen(f));
133 write(cooperative_pipe_fp, "\n", 1);
138 c = calloc(1, sizeof(struct Client));
140 fprintf(stderr, __NAME__": fatal: calloc failed\n");
144 c->focus_new_tab = focus_tab;
146 if (related_wv == NULL)
147 c->web_view = webkit_web_view_new();
149 c->web_view = webkit_web_view_new_with_related_view(related_wv);
151 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
152 g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
153 G_CALLBACK(changed_favicon), c);
154 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
155 G_CALLBACK(changed_title), c);
156 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
157 G_CALLBACK(changed_uri), c);
158 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
159 G_CALLBACK(changed_load_progress), c);
160 g_signal_connect(G_OBJECT(c->web_view), "create",
161 G_CALLBACK(client_new_request), NULL);
162 g_signal_connect(G_OBJECT(c->web_view), "close",
163 G_CALLBACK(client_destroy), c);
164 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
165 G_CALLBACK(decide_policy), NULL);
166 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
167 G_CALLBACK(key_web_view), c);
168 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
169 G_CALLBACK(key_web_view), c);
170 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
171 G_CALLBACK(key_web_view), c);
172 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
173 G_CALLBACK(hover_web_view), c);
174 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
175 G_CALLBACK(crashed_web_view), c);
177 if (user_agent != NULL)
178 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
179 "user-agent", user_agent, NULL);
181 if (enable_console_to_stdout)
182 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
184 webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
186 c->location = gtk_entry_new();
187 g_signal_connect(G_OBJECT(c->location), "key-press-event",
188 G_CALLBACK(key_location), c);
189 g_signal_connect(G_OBJECT(c->location), "icon-release",
190 G_CALLBACK(icon_location), c);
191 /* XXX This is a workaround. Setting this to NULL (which is done in
192 * grab_feeds_finished() if no feed has been detected) adds a little
193 * padding left of the text. Not sure why. The point of this call
194 * right here is to have that padding right from the start. This
195 * avoids a graphical artifact. */
196 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
197 GTK_ENTRY_ICON_PRIMARY,
200 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
201 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
202 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
203 gtk_container_set_focus_child(GTK_CONTAINER(c->vbox), c->web_view);
205 c->tabicon = gtk_image_new_from_icon_name("text-html", GTK_ICON_SIZE_SMALL_TOOLBAR);
207 c->tablabel = gtk_label_new(__NAME__);
208 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
209 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), tab_width_chars);
210 gtk_widget_set_has_tooltip(c->tablabel, TRUE);
212 /* XXX I don't own a HiDPI screen, so I don't know if scale_factor
213 * does the right thing. */
214 tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
215 5 * gtk_widget_get_scale_factor(mw.win));
216 gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
217 gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
219 evbox = gtk_event_box_new();
220 gtk_container_add(GTK_CONTAINER(evbox), tabbox);
221 g_signal_connect(G_OBJECT(evbox), "button-release-event",
222 G_CALLBACK(key_tablabel), c);
224 gtk_widget_add_events(evbox, GDK_SCROLL_MASK);
225 g_signal_connect(G_OBJECT(evbox), "scroll-event",
226 G_CALLBACK(key_tablabel), c);
228 /* For easy access, store a reference to our label. */
229 g_object_set_data(G_OBJECT(evbox), "lariza-tab-label", c->tablabel);
231 /* This only shows the event box and the label inside, nothing else.
232 * Needed because the evbox/label is "internal" to the notebook and
233 * not part of the normal "widget tree" (IIUC). */
234 gtk_widget_show_all(evbox);
236 gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox,
237 gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1);
238 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
241 show_web_view(NULL, c);
243 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
244 G_CALLBACK(show_web_view), c);
247 f = ensure_uri_scheme(uri);
248 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
254 return WEBKIT_WEB_VIEW(c->web_view);
258 client_new_request(WebKitWebView *web_view,
259 WebKitNavigationAction *navigation_action, gpointer data)
261 return client_new(NULL, web_view, FALSE, FALSE);
265 cooperation_setup(void)
268 gchar *fifofilename, *fifopath;
270 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
271 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
272 g_free(fifofilename);
274 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
275 mkfifo(fifopath, 0600);
277 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
278 if (!cooperative_pipe_fp) {
279 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
281 if (write(cooperative_pipe_fp, "", 0) == -1) {
282 /* Could not do an empty write to the FIFO which means there's
283 * no one listening. */
284 close(cooperative_pipe_fp);
285 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
286 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
288 cooperative_alone = FALSE;
296 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
298 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
299 WebKitURIResponse *resp;
300 GtkToolItem *tb = GTK_TOOL_ITEM(data);
303 gchar *t, *filename, *base;
305 p = webkit_download_get_estimated_progress(download);
309 resp = webkit_download_get_response(download);
310 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
312 uri = webkit_download_get_destination(download);
313 filename = g_filename_from_uri(uri, NULL, NULL);
314 if (filename == NULL) {
315 /* This really should not happen because WebKit uses that URI to
316 * write to a file... */
317 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
318 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
319 webkit_uri_response_get_uri(resp), p, size_mb);
321 base = g_path_get_basename(filename);
322 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
326 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
331 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
333 struct Client *c = (struct Client *)data;
336 "a = document.querySelectorAll('"
337 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
338 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
345 " for (i = 0; i < a.length; i++)"
347 " url = encodeURIComponent(a[i].href);"
348 " if ('title' in a[i] && a[i].title != '')"
349 " title = encodeURIComponent(a[i].title);"
352 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
358 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
362 /* The page has loaded fully. We now run the short JavaScript
363 * snippet above that operates on the DOM. It tries to grab all
364 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
366 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
368 grab_feeds_finished, c);
370 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
372 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
376 changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
378 struct Client *c = (struct Client *)data;
380 int w, h, w_should, h_should;
381 GdkPixbuf *pb, *pb_scaled;
383 f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
385 gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html",
386 GTK_ICON_SIZE_SMALL_TOOLBAR);
388 w = cairo_image_surface_get_width(f);
389 h = cairo_image_surface_get_height(f);
390 pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
392 w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
393 h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
394 pb_scaled = gdk_pixbuf_scale_simple(pb, w_should, h_should,
395 GDK_INTERP_BILINEAR);
396 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon), pb_scaled);
398 g_object_unref(pb_scaled);
405 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
408 struct Client *c = (struct Client *)data;
410 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
411 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
413 u = u == NULL ? __NAME__ : u;
414 u = u[0] == 0 ? __NAME__ : u;
416 t = t == NULL ? u : t;
417 t = t[0] == 0 ? u : t;
419 gtk_label_set_text(GTK_LABEL(c->tablabel), t);
420 gtk_widget_set_tooltip_text(c->tablabel, t);
421 mainwindow_title(gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)));
425 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
428 struct Client *c = (struct Client *)data;
431 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
433 /* When a web process crashes, we get a "notify::uri" signal, but we
434 * can no longer read a meaningful URI. It's just an empty string
435 * now. Not updating the location bar in this scenario is important,
436 * because we would override the "WEB PROCESS CRASHED" message. */
437 if (t != NULL && strlen(t) > 0) {
438 gtk_entry_set_text(GTK_ENTRY(c->location), t);
440 if (history_file != NULL) {
441 fp = fopen(history_file, "a");
443 fprintf(fp, "%s\n", t);
446 perror(__NAME__": Error opening history file");
453 crashed_web_view(WebKitWebView *web_view, gpointer data)
456 struct Client *c = (struct Client *)data;
458 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
459 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
460 gtk_entry_set_text(GTK_ENTRY(c->location), t);
467 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
468 WebKitPolicyDecisionType type, gpointer data)
470 WebKitResponsePolicyDecision *r;
473 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
474 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
475 if (!webkit_response_policy_decision_is_mime_type_supported(r))
476 webkit_policy_decision_download(decision);
478 webkit_policy_decision_use(decision);
481 /* Use whatever default there is. */
488 download_handle_finished(WebKitDownload *download, gpointer data)
494 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
497 g_signal_connect(G_OBJECT(download), "decide-destination",
498 G_CALLBACK(download_handle), data);
502 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
504 gchar *sug_clean, *path, *path2 = NULL, *uri;
509 sug_clean = g_strdup(suggested_filename);
510 for (i = 0; i < strlen(sug_clean); i++)
511 if (sug_clean[i] == G_DIR_SEPARATOR)
514 path = g_build_filename(download_dir, sug_clean, NULL);
515 path2 = g_strdup(path);
516 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000) {
519 path2 = g_strdup_printf("%s.%d", path, suffix);
523 if (suffix == 1000) {
524 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
525 webkit_download_cancel(download);
527 uri = g_filename_to_uri(path2, NULL, NULL);
528 webkit_download_set_destination(download, uri);
531 tb = gtk_tool_button_new(NULL, NULL);
532 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
533 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
534 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
535 gtk_widget_show_all(dm.win);
537 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
538 G_CALLBACK(changed_download_progress), tb);
541 g_signal_connect(G_OBJECT(download), "finished",
542 G_CALLBACK(download_handle_finished), NULL);
544 g_object_ref(download);
545 g_signal_connect(G_OBJECT(tb), "clicked",
546 G_CALLBACK(downloadmanager_cancel), download);
553 /* Propagate -- to whom it may concern. */
558 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
560 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
562 webkit_download_cancel(download);
563 g_object_unref(download);
565 gtk_widget_destroy(GTK_WIDGET(tb));
569 downloadmanager_delete(GtkWidget *obj, gpointer data)
571 if (!quit_if_nothing_active())
572 gtk_widget_hide(dm.win);
578 downloadmanager_setup(void)
580 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
581 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
582 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
583 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
584 g_signal_connect(G_OBJECT(dm.win), "delete-event",
585 G_CALLBACK(downloadmanager_delete), NULL);
586 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
587 G_CALLBACK(key_downloadmanager), NULL);
589 dm.toolbar = gtk_toolbar_new();
590 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
591 GTK_ORIENTATION_VERTICAL);
592 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
593 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
595 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
596 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
597 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
598 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
600 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
604 ensure_uri_scheme(const gchar *t)
608 f = g_ascii_strdown(t, -1);
609 if (!g_str_has_prefix(f, "http:") &&
610 !g_str_has_prefix(f, "https:") &&
611 !g_str_has_prefix(f, "file:") &&
612 !g_str_has_prefix(f, "about:") &&
613 !g_str_has_prefix(f, "data:") &&
614 !g_str_has_prefix(f, "webkit:")) {
616 fabs = realpath(t, NULL);
618 f = g_strdup_printf("file://%s", fabs);
621 f = g_strdup_printf("http://%s", t);
629 grab_environment_configuration(void)
633 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
635 accepted_language[0] = g_strdup(e);
637 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
639 download_dir = g_strdup(e);
641 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
643 enable_console_to_stdout = TRUE;
645 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
647 fifo_suffix = g_strdup(e);
649 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
651 history_file = g_strdup(e);
653 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
655 home_uri = g_strdup(e);
657 e = g_getenv(__NAME_UPPERCASE__"_TAB_POS");
659 if (strcmp(e, "top") == 0)
660 tab_pos = GTK_POS_TOP;
661 if (strcmp(e, "right") == 0)
662 tab_pos = GTK_POS_RIGHT;
663 if (strcmp(e, "bottom") == 0)
664 tab_pos = GTK_POS_BOTTOM;
665 if (strcmp(e, "left") == 0)
666 tab_pos = GTK_POS_LEFT;
669 e = g_getenv(__NAME_UPPERCASE__"_TAB_WIDTH_CHARS");
671 tab_width_chars = atoi(e);
673 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
675 user_agent = g_strdup(e);
677 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
679 global_zoom = atof(e);
683 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
685 struct Client *c = (struct Client *)data;
686 WebKitJavascriptResult *js_result;
688 JSCException *exception;
692 g_free(c->feed_html);
695 /* This was taken almost verbatim from the example in WebKit's
698 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
700 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
703 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
708 value = webkit_javascript_result_get_js_value(js_result);
709 if (jsc_value_is_string(value)) {
710 str_value = jsc_value_to_string(value);
711 exception = jsc_context_get_exception(jsc_value_get_context(value));
712 if (exception != NULL) {
713 fprintf(stderr, __NAME__": Error running javascript: %s\n",
714 jsc_exception_get_message(exception));
716 c->feed_html = str_value;
719 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
720 GTK_ENTRY_ICON_PRIMARY,
721 "application-rss+xml-symbolic");
722 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
723 GTK_ENTRY_ICON_PRIMARY,
726 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
727 GTK_ENTRY_ICON_PRIMARY,
731 webkit_javascript_result_unref(js_result);
735 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
738 struct Client *c = (struct Client *)data;
741 g_free(c->hover_uri);
743 if (webkit_hit_test_result_context_is_link(ht)) {
744 to_show = webkit_hit_test_result_get_link_uri(ht);
745 c->hover_uri = g_strdup(to_show);
747 to_show = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
751 if (!gtk_widget_is_focus(c->location))
752 gtk_entry_set_text(GTK_ENTRY(c->location), to_show);
756 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
759 struct Client *c = (struct Client *)data;
761 gchar *data_template =
766 " <meta charset=\"UTF-8\">"
767 " <title>Feeds</title>"
770 " <p>Feeds found on this page:</p>"
777 if (c->feed_html != NULL)
779 /* What we're actually trying to do is show a simple HTML page
780 * that lists all the feeds on the current page. The function
781 * webkit_web_view_load_html() looks like the proper way to do
782 * that. Sad thing is, it doesn't create a history entry, but
783 * instead simply replaces the content of the current page. This
784 * is not what we want.
786 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
787 * mechanism to show my custom HTML snippet *and* create a
790 * [0]: https://tools.ietf.org/html/rfc2397
791 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
792 d = g_strdup_printf(data_template, c->feed_html);
793 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
799 init_default_web_context(void)
802 WebKitWebContext *wc;
803 WebKitCookieManager *cm;
805 wc = webkit_web_context_get_default();
807 p = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock", NULL);
808 webkit_web_context_set_sandbox_enabled(wc, TRUE);
809 webkit_web_context_add_path_to_sandbox(wc, p, TRUE);
812 webkit_web_context_set_process_model(wc,
813 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
815 p = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
817 webkit_web_context_set_web_extensions_directory(wc, p);
820 if (accepted_language[0] != NULL)
821 webkit_web_context_set_preferred_languages(wc, accepted_language);
823 g_signal_connect(G_OBJECT(wc), "download-started",
824 G_CALLBACK(download_handle_start), NULL);
826 trust_user_certs(wc);
828 cm = webkit_web_context_get_cookie_manager(wc);
829 webkit_cookie_manager_set_accept_policy(cm, WEBKIT_COOKIE_POLICY_ACCEPT_NO_THIRD_PARTY);
831 webkit_web_context_set_favicon_database_directory(wc, NULL);
832 webkit_cookie_manager_set_persistent_storage(cm,
833 g_build_filename("/",
834 g_get_user_cache_dir(),
838 WEBKIT_COOKIE_PERSISTENT_STORAGE_SQLITE);
842 search(gpointer data, gint direction)
844 struct Client *c = (struct Client *)data;
845 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
846 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
848 if (search_text == NULL)
853 webkit_find_controller_search(fc, search_text,
854 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
855 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
859 webkit_find_controller_search_next(fc);
862 webkit_find_controller_search_previous(fc);
865 webkit_find_controller_search_finish(fc);
871 search_init(struct Client *c, int direction)
873 gtk_widget_grab_focus(c->location);
874 const gchar *contents = gtk_entry_get_text(GTK_ENTRY(c->location));
875 if (strcspn(contents, "s/")) {
876 gtk_entry_set_text(GTK_ENTRY(c->location), "s/");
878 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
881 search(c, direction);
885 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
887 struct Client *c = (struct Client *)data;
891 if (event->type == GDK_KEY_PRESS) {
892 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK) {
893 switch (((GdkEventKey *)event)->keyval) {
894 case GDK_KEY_q: // close window
895 client_destroy(NULL, c);
897 case GDK_KEY_w: // new tab
898 f = ensure_uri_scheme(home_uri);
899 client_new(f, NULL, TRUE, TRUE);
902 case GDK_KEY_e: // reload
903 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(c->web_view));
905 case GDK_KEY_y: // download manager
906 gtk_widget_show_all(dm.win);
908 case GDK_KEY_d: // initiate web search
909 gtk_widget_grab_focus(c->location);
910 gtk_entry_set_text(GTK_ENTRY(c->location), "w/");
911 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
913 case GDK_KEY_s: // i-search forwards
916 case GDK_KEY_r: // i-search backwards
919 case GDK_KEY_t: // location
920 gtk_widget_grab_focus(c->location);
921 gtk_entry_set_text(GTK_ENTRY(c->location),
922 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
924 case GDK_KEY_u: // previous tab
925 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
927 case GDK_KEY_i: // next tab
928 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
930 case GDK_KEY_h: // back in history
931 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
933 case GDK_KEY_l: // forward in history
934 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
936 case GDK_KEY_j: // scroll down
937 for (int i = 0; i < 2; i++) {
938 event->key.keyval = GDK_KEY_Down;
939 gdk_event_put(event);
942 case GDK_KEY_k: // scroll up
943 for (int i = 0; i < 2; i++) {
944 event->key.keyval = GDK_KEY_Up;
945 gdk_event_put(event);
948 case GDK_KEY_f: // page down
949 event->key.keyval = GDK_KEY_Page_Down;
950 gdk_event_put(event);
952 case GDK_KEY_b: // page up
953 event->key.keyval = GDK_KEY_Page_Up;
954 gdk_event_put(event);
956 case GDK_KEY_o: // toggle JS
957 WebKitSettings *settings = webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view));
958 gboolean current = webkit_settings_get_enable_javascript(settings);
959 webkit_settings_set_enable_javascript(settings,
960 (current) ? FALSE : TRUE);
961 webkit_web_view_set_settings(WEBKIT_WEB_VIEW(c->web_view), settings);
963 case GDK_KEY_g: // go to bottom
965 gtk_widget_grab_focus(c->web_view);
966 gtk_entry_set_text(GTK_ENTRY(c->location),
967 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
968 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
969 "window.getSelection().removeAllRanges();"
970 "document.activeElement.blur();",
973 case GDK_KEY_equal: // zoom in
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 case GDK_KEY_minus: // zoom out
978 now = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
979 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), now - 0.1);
981 case GDK_KEY_0: // reset zoom
982 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), 1);
991 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
993 if (event->type == GDK_KEY_PRESS) {
994 if (((GdkEventKey *)event)->state & GDK_CONTROL_MASK) {
995 switch (((GdkEventKey *)event)->keyval) {
996 case GDK_KEY_y: /* close window (left hand) */
998 downloadmanager_delete(dm.win, NULL);
1008 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
1010 struct Client *c = (struct Client *)data;
1014 if (key_common(widget, event, data))
1017 if (event->type == GDK_KEY_PRESS) {
1018 switch (((GdkEventKey *)event)->keyval) {
1019 case GDK_KEY_KP_Enter:
1020 case GDK_KEY_Return:
1021 gtk_widget_grab_focus(c->web_view);
1022 t = gtk_entry_get_text(GTK_ENTRY(c->location));
1023 if (t != NULL && t[0] == 's' && t[1] == '/') {
1024 if (search_text != NULL)
1025 g_free(search_text);
1026 search_text = g_strdup(t + 2);
1028 } else if (t != NULL && t[0] == 'w' && t[1] == '/') {
1029 const char *engine = "https://duckduckgo.com?q=";
1030 int len = strlen(engine) + strlen(t) - 2;
1031 char *f = (char *) malloc(len);
1032 snprintf(f, len + 1, "%s%s", engine, t + 2);
1033 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
1036 f = ensure_uri_scheme(t);
1037 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
1041 case GDK_KEY_Escape:
1042 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
1043 gtk_entry_set_text(GTK_ENTRY(c->location),
1044 (t == NULL ? __NAME__ : t));
1053 key_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
1055 GdkScrollDirection direction;
1057 if (event->type == GDK_BUTTON_RELEASE) {
1058 switch (((GdkEventButton *)event)->button) {
1060 client_destroy(NULL, data);
1063 } else if (event->type == GDK_SCROLL) {
1064 gdk_event_get_scroll_direction(event, &direction);
1065 switch (direction) {
1067 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
1069 case GDK_SCROLL_DOWN:
1070 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
1081 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
1083 struct Client *c = (struct Client *)data;
1087 if (key_common(widget, event, data))
1090 if (event->type == GDK_KEY_PRESS) {
1091 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape) {
1092 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
1093 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
1095 } else if (event->type == GDK_BUTTON_RELEASE) {
1096 switch (((GdkEventButton *)event)->button) {
1098 if (c->hover_uri != NULL) {
1099 client_new(c->hover_uri, NULL, TRUE, FALSE);
1104 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1107 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
1110 } else if (event->type == GDK_SCROLL) {
1111 if (((GdkEventScroll *)event)->state & GDK_CONTROL_MASK ||
1112 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK) {
1113 gdk_event_get_scroll_deltas(event, &dx, &dy);
1114 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1116 z = dx != 0 ? global_zoom : z;
1117 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1126 mainwindow_setup(void)
1128 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1129 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1130 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1131 gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
1133 mw.notebook = gtk_notebook_new();
1134 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1135 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), tab_pos);
1136 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1137 g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
1138 G_CALLBACK(notebook_switch_page), NULL);
1142 mainwindow_title(gint idx)
1144 GtkWidget *child, *widg, *tablabel;
1147 child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
1151 widg = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
1152 tablabel = (GtkWidget *)g_object_get_data(G_OBJECT(widg), "lariza-tab-label");
1153 text = gtk_label_get_text(GTK_LABEL(tablabel));
1154 gtk_window_set_title(GTK_WINDOW(mw.win), text);
1158 notebook_switch_page(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
1160 mainwindow_title(idx);
1164 quit_if_nothing_active(void)
1167 if (downloads == 0) {
1171 gtk_widget_show_all(dm.win);
1179 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1183 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1186 client_new(uri, NULL, TRUE, TRUE);
1193 run_user_scripts(WebKitWebView *web_view)
1195 gchar *base = NULL, *path = NULL, *contents = NULL;
1196 const gchar *entry = NULL;
1197 GDir *scriptdir = NULL;
1199 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts",
1201 scriptdir = g_dir_open(base, 0, NULL);
1202 if (scriptdir != NULL) {
1203 while ((entry = g_dir_read_name(scriptdir)) != NULL) {
1204 path = g_build_filename(base, entry, NULL);
1205 char *jscmd = malloc(strlen(path) + 36);
1206 sprintf(jscmd, "console.log(\"Running userscript %s\");", path);
1207 if (g_str_has_suffix(path, ".js")) {
1208 if (g_file_get_contents(path, &contents, NULL, NULL)) {
1209 webkit_web_view_run_javascript(web_view, jscmd, NULL, NULL, NULL);
1210 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1217 g_dir_close(scriptdir);
1224 show_web_view(WebKitWebView *web_view, gpointer data)
1226 struct Client *c = (struct Client *)data;
1231 gtk_widget_show_all(mw.win);
1233 if (c->focus_new_tab) {
1234 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1236 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
1238 gtk_widget_grab_focus(c->web_view);
1243 trust_user_certs(WebKitWebContext *wc)
1245 GTlsCertificate *cert;
1246 const gchar *basedir, *file, *absfile;
1249 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1250 dir = g_dir_open(basedir, 0, NULL);
1252 file = g_dir_read_name(dir);
1253 while (file != NULL) {
1254 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1257 cert = g_tls_certificate_new_from_file(absfile, NULL);
1259 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1261 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1262 file = g_dir_read_name(dir);
1269 main(int argc, char **argv)
1273 gtk_init(&argc, &argv);
1274 grab_environment_configuration();
1276 while ((opt = getopt(argc, argv, "C")) != -1) {
1279 cooperative_instances = FALSE;
1282 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1287 if (cooperative_instances)
1288 cooperation_setup();
1290 if (!cooperative_instances || cooperative_alone)
1291 init_default_web_context();
1293 downloadmanager_setup();
1296 if (optind >= argc) {
1297 client_new(home_uri, NULL, TRUE, TRUE);
1299 for (i = optind; i < argc; i++)
1300 client_new(argv[i], NULL, TRUE, TRUE);
1303 if (!cooperative_instances || cooperative_alone)