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 *);
60 gchar *external_handler_uri;
68 gboolean focus_new_tab;
77 struct DownloadManager
85 const gchar *accepted_language[2] = { NULL, NULL };
86 gint clients = 0, downloads = 0;
87 gboolean cooperative_alone = TRUE;
88 gboolean cooperative_instances = TRUE;
89 int cooperative_pipe_fp = 0;
90 gchar *download_dir = "/var/tmp";
91 gboolean enable_console_to_stdout = FALSE;
92 gchar *fifo_suffix = "main";
93 gdouble global_zoom = 1.0;
94 gchar *history_file = NULL;
95 gchar *home_uri = "about:blank";
96 gchar *search_text = NULL;
97 GtkPositionType tab_pos = GTK_POS_TOP;
98 gint tab_width_chars = 20;
99 gchar *user_agent = NULL;
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)
133 f = ensure_uri_scheme(uri);
134 write(cooperative_pipe_fp, f, strlen(f));
135 write(cooperative_pipe_fp, "\n", 1);
140 c = calloc(1, sizeof(struct Client));
143 fprintf(stderr, __NAME__": fatal: calloc failed\n");
147 c->focus_new_tab = focus_tab;
149 if (related_wv == NULL)
150 c->web_view = webkit_web_view_new();
152 c->web_view = webkit_web_view_new_with_related_view(related_wv);
154 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
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);
207 c->tabicon = gtk_image_new_from_icon_name("text-html", GTK_ICON_SIZE_SMALL_TOOLBAR);
209 c->tablabel = gtk_label_new(__NAME__);
210 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
211 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), tab_width_chars);
212 gtk_widget_set_has_tooltip(c->tablabel, TRUE);
214 /* XXX I don't own a HiDPI screen, so I don't know if scale_factor
215 * does the right thing. */
216 tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
217 5 * gtk_widget_get_scale_factor(mw.win));
218 gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
219 gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
221 evbox = gtk_event_box_new();
222 gtk_container_add(GTK_CONTAINER(evbox), tabbox);
223 g_signal_connect(G_OBJECT(evbox), "button-release-event",
224 G_CALLBACK(key_tablabel), c);
226 gtk_widget_add_events(evbox, GDK_SCROLL_MASK);
227 g_signal_connect(G_OBJECT(evbox), "scroll-event",
228 G_CALLBACK(key_tablabel), c);
230 /* For easy access, store a reference to our label. */
231 g_object_set_data(G_OBJECT(evbox), "lariza-tab-label", c->tablabel);
233 /* This only shows the event box and the label inside, nothing else.
234 * Needed because the evbox/label is "internal" to the notebook and
235 * not part of the normal "widget tree" (IIUC). */
236 gtk_widget_show_all(evbox);
238 gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox,
239 gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1);
240 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
243 show_web_view(NULL, c);
245 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
246 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)
283 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
287 if (write(cooperative_pipe_fp, "", 0) == -1)
289 /* Could not do an empty write to the FIFO which means there's
290 * no one listening. */
291 close(cooperative_pipe_fp);
292 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
293 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
296 cooperative_alone = FALSE;
303 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
305 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
306 WebKitURIResponse *resp;
307 GtkToolItem *tb = GTK_TOOL_ITEM(data);
310 gchar *t, *filename, *base;
312 p = webkit_download_get_estimated_progress(download);
316 resp = webkit_download_get_response(download);
317 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
319 uri = webkit_download_get_destination(download);
320 filename = g_filename_from_uri(uri, NULL, NULL);
321 if (filename == NULL)
323 /* This really should not happen because WebKit uses that URI to
324 * write to a file... */
325 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
326 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
327 webkit_uri_response_get_uri(resp), p, size_mb);
331 base = g_path_get_basename(filename);
332 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
336 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
341 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
343 struct Client *c = (struct Client *)data;
346 "a = document.querySelectorAll('"
347 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
348 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
355 " for (i = 0; i < a.length; i++)"
357 " url = encodeURIComponent(a[i].href);"
358 " if ('title' in a[i] && a[i].title != '')"
359 " title = encodeURIComponent(a[i].title);"
362 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
367 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
372 /* The page has loaded fully. We now run the short JavaScript
373 * snippet above that operates on the DOM. It tries to grab all
374 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
376 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
378 grab_feeds_finished, c);
380 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
382 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
386 changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
388 struct Client *c = (struct Client *)data;
390 int w, h, w_should, h_should;
391 GdkPixbuf *pb, *pb_scaled;
393 f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
396 gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html",
397 GTK_ICON_SIZE_SMALL_TOOLBAR);
401 w = cairo_image_surface_get_width(f);
402 h = cairo_image_surface_get_height(f);
403 pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
406 w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
407 h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
408 pb_scaled = gdk_pixbuf_scale_simple(pb, w_should, h_should,
409 GDK_INTERP_BILINEAR);
410 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon), pb_scaled);
412 g_object_unref(pb_scaled);
419 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
422 struct Client *c = (struct Client *)data;
424 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
425 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
427 u = u == NULL ? __NAME__ : u;
428 u = u[0] == 0 ? __NAME__ : u;
430 t = t == NULL ? u : t;
431 t = t[0] == 0 ? u : t;
433 gtk_label_set_text(GTK_LABEL(c->tablabel), t);
434 gtk_widget_set_tooltip_text(c->tablabel, t);
435 mainwindow_title(gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)));
439 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
442 struct Client *c = (struct Client *)data;
445 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
447 /* When a web process crashes, we get a "notify::uri" signal, but we
448 * can no longer read a meaningful URI. It's just an empty string
449 * now. Not updating the location bar in this scenario is important,
450 * because we would override the "WEB PROCESS CRASHED" message. */
451 if (t != NULL && strlen(t) > 0)
453 gtk_entry_set_text(GTK_ENTRY(c->location), t);
455 if (history_file != NULL)
457 fp = fopen(history_file, "a");
460 fprintf(fp, "%s\n", t);
464 perror(__NAME__": Error opening history file");
470 crashed_web_view(WebKitWebView *web_view, gpointer data)
473 struct Client *c = (struct Client *)data;
475 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
476 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
477 gtk_entry_set_text(GTK_ENTRY(c->location), t);
484 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
485 WebKitPolicyDecisionType type, gpointer data)
487 WebKitResponsePolicyDecision *r;
491 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
492 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
493 if (!webkit_response_policy_decision_is_mime_type_supported(r))
494 webkit_policy_decision_download(decision);
496 webkit_policy_decision_use(decision);
499 /* Use whatever default there is. */
506 download_handle_finished(WebKitDownload *download, gpointer data)
512 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
515 g_signal_connect(G_OBJECT(download), "decide-destination",
516 G_CALLBACK(download_handle), data);
520 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
522 gchar *sug_clean, *path, *path2 = NULL, *uri;
527 sug_clean = g_strdup(suggested_filename);
528 for (i = 0; i < strlen(sug_clean); i++)
529 if (sug_clean[i] == G_DIR_SEPARATOR)
532 path = g_build_filename(download_dir, sug_clean, NULL);
533 path2 = g_strdup(path);
534 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
538 path2 = g_strdup_printf("%s.%d", path, suffix);
544 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
545 webkit_download_cancel(download);
549 uri = g_filename_to_uri(path2, NULL, NULL);
550 webkit_download_set_destination(download, uri);
553 tb = gtk_tool_button_new(NULL, NULL);
554 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
555 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
556 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
557 gtk_widget_show_all(dm.win);
559 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
560 G_CALLBACK(changed_download_progress), tb);
563 g_signal_connect(G_OBJECT(download), "finished",
564 G_CALLBACK(download_handle_finished), NULL);
566 g_object_ref(download);
567 g_signal_connect(G_OBJECT(tb), "clicked",
568 G_CALLBACK(downloadmanager_cancel), download);
575 /* Propagate -- to whom it may concern. */
580 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
582 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
584 webkit_download_cancel(download);
585 g_object_unref(download);
587 gtk_widget_destroy(GTK_WIDGET(tb));
591 downloadmanager_delete(GtkWidget *obj, gpointer data)
593 if (!quit_if_nothing_active())
594 gtk_widget_hide(dm.win);
600 downloadmanager_setup(void)
602 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
603 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
604 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
605 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
606 g_signal_connect(G_OBJECT(dm.win), "delete-event",
607 G_CALLBACK(downloadmanager_delete), NULL);
608 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
609 G_CALLBACK(key_downloadmanager), NULL);
611 dm.toolbar = gtk_toolbar_new();
612 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
613 GTK_ORIENTATION_VERTICAL);
614 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
615 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
617 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
618 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
619 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
620 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
622 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
626 ensure_uri_scheme(const gchar *t)
630 f = g_ascii_strdown(t, -1);
631 if (!g_str_has_prefix(f, "http:") &&
632 !g_str_has_prefix(f, "https:") &&
633 !g_str_has_prefix(f, "file:") &&
634 !g_str_has_prefix(f, "about:") &&
635 !g_str_has_prefix(f, "data:") &&
636 !g_str_has_prefix(f, "webkit:"))
639 fabs = realpath(t, NULL);
642 f = g_strdup_printf("file://%s", fabs);
646 f = g_strdup_printf("http://%s", t);
654 grab_environment_configuration(void)
658 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
660 accepted_language[0] = g_strdup(e);
662 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
664 download_dir = g_strdup(e);
666 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
668 enable_console_to_stdout = TRUE;
670 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
672 fifo_suffix = g_strdup(e);
674 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
676 history_file = g_strdup(e);
678 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
680 home_uri = g_strdup(e);
682 e = g_getenv(__NAME_UPPERCASE__"_TAB_POS");
685 if (strcmp(e, "top") == 0)
686 tab_pos = GTK_POS_TOP;
687 if (strcmp(e, "right") == 0)
688 tab_pos = GTK_POS_RIGHT;
689 if (strcmp(e, "bottom") == 0)
690 tab_pos = GTK_POS_BOTTOM;
691 if (strcmp(e, "left") == 0)
692 tab_pos = GTK_POS_LEFT;
695 e = g_getenv(__NAME_UPPERCASE__"_TAB_WIDTH_CHARS");
697 tab_width_chars = atoi(e);
699 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
701 user_agent = g_strdup(e);
703 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
705 global_zoom = atof(e);
709 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
711 struct Client *c = (struct Client *)data;
712 WebKitJavascriptResult *js_result;
714 JSCException *exception;
718 g_free(c->feed_html);
721 /* This was taken almost verbatim from the example in WebKit's
724 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
726 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
730 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
735 value = webkit_javascript_result_get_js_value(js_result);
736 if (jsc_value_is_string(value))
738 str_value = jsc_value_to_string(value);
739 exception = jsc_context_get_exception(jsc_value_get_context(value));
740 if (exception != NULL)
742 fprintf(stderr, __NAME__": Error running javascript: %s\n",
743 jsc_exception_get_message(exception));
746 c->feed_html = str_value;
748 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
749 GTK_ENTRY_ICON_PRIMARY,
750 "application-rss+xml-symbolic");
751 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
752 GTK_ENTRY_ICON_PRIMARY,
757 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
758 GTK_ENTRY_ICON_PRIMARY,
762 webkit_javascript_result_unref(js_result);
766 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
769 struct Client *c = (struct Client *)data;
771 if (!gtk_widget_is_focus(c->location))
773 if (webkit_hit_test_result_context_is_link(ht))
775 gtk_entry_set_text(GTK_ENTRY(c->location),
776 webkit_hit_test_result_get_link_uri(ht));
778 if (c->hover_uri != NULL)
779 g_free(c->hover_uri);
780 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
784 gtk_entry_set_text(GTK_ENTRY(c->location),
785 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
787 if (c->hover_uri != NULL)
788 g_free(c->hover_uri);
795 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
798 struct Client *c = (struct Client *)data;
800 gchar *data_template =
805 " <meta charset=\"UTF-8\">"
806 " <title>Feeds</title>"
809 " <p>Feeds found on this page:</p>"
816 if (c->feed_html != NULL)
818 /* What we're actually trying to do is show a simple HTML page
819 * that lists all the feeds on the current page. The function
820 * webkit_web_view_load_html() looks like the proper way to do
821 * that. Sad thing is, it doesn't create a history entry, but
822 * instead simply replaces the content of the current page. This
823 * is not what we want.
825 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
826 * mechanism to show my custom HTML snippet *and* create a
829 * [0]: https://tools.ietf.org/html/rfc2397
830 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
831 d = g_strdup_printf(data_template, c->feed_html);
832 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
838 init_default_web_context(void)
841 WebKitWebContext *wc;
843 wc = webkit_web_context_get_default();
845 p = g_build_filename(g_get_user_config_dir(), __NAME__, "adblock.black", NULL);
846 webkit_web_context_set_sandbox_enabled(wc, TRUE);
847 webkit_web_context_add_path_to_sandbox(wc, p, TRUE);
850 webkit_web_context_set_process_model(wc,
851 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
853 p = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
855 webkit_web_context_set_web_extensions_directory(wc, p);
858 if (accepted_language[0] != NULL)
859 webkit_web_context_set_preferred_languages(wc, accepted_language);
861 g_signal_connect(G_OBJECT(wc), "download-started",
862 G_CALLBACK(download_handle_start), NULL);
864 trust_user_certs(wc);
866 webkit_web_context_set_favicon_database_directory(wc, NULL);
870 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
872 struct Client *c = (struct Client *)data;
873 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
876 if (event->type == GDK_KEY_PRESS)
878 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
880 switch (((GdkEventKey *)event)->keyval)
882 case GDK_KEY_q: /* close window (left hand) */
883 client_destroy(NULL, c);
885 case GDK_KEY_w: /* home (left hand) */
886 f = ensure_uri_scheme(home_uri);
887 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
890 case GDK_KEY_e: /* new tab (left hand) */
891 f = ensure_uri_scheme(home_uri);
892 client_new(f, NULL, TRUE, TRUE);
895 case GDK_KEY_r: /* reload (left hand) */
896 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
899 case GDK_KEY_d: /* download manager (left hand) */
900 gtk_widget_show_all(dm.win);
902 case GDK_KEY_2: /* search forward (left hand) */
903 case GDK_KEY_n: /* search forward (maybe both hands) */
906 case GDK_KEY_3: /* search backward (left hand) */
909 case GDK_KEY_l: /* location (BOTH hands) */
910 gtk_widget_grab_focus(c->location);
912 case GDK_KEY_k: /* initiate search (BOTH hands) */
913 gtk_widget_grab_focus(c->location);
914 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
915 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
917 case GDK_KEY_c: /* reload trusted certs (left hand) */
918 trust_user_certs(wc);
920 case GDK_KEY_a: /* go one tab to the left (left hand) */
921 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
923 case GDK_KEY_s: /* go one tab to the right (left hand) */
924 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
928 /* navigate backward (left hand) */
929 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
931 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
934 /* navigate forward (left hand) */
935 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
937 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
946 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
948 if (event->type == GDK_KEY_PRESS)
950 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
952 switch (((GdkEventKey *)event)->keyval)
954 case GDK_KEY_d: /* close window (left hand) */
956 downloadmanager_delete(dm.win, NULL);
966 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
968 struct Client *c = (struct Client *)data;
972 if (key_common(widget, event, data))
975 if (event->type == GDK_KEY_PRESS)
977 switch (((GdkEventKey *)event)->keyval)
979 case GDK_KEY_KP_Enter:
981 gtk_widget_grab_focus(c->web_view);
982 t = gtk_entry_get_text(GTK_ENTRY(c->location));
983 if (t != NULL && t[0] == ':' && t[1] == '/')
985 if (search_text != NULL)
987 search_text = g_strdup(t + 2);
992 f = ensure_uri_scheme(t);
993 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
998 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
999 gtk_entry_set_text(GTK_ENTRY(c->location),
1000 (t == NULL ? __NAME__ : t));
1009 key_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
1011 GdkScrollDirection direction;
1013 if (event->type == GDK_BUTTON_RELEASE)
1015 switch (((GdkEventButton *)event)->button)
1018 client_destroy(NULL, data);
1022 else if (event->type == GDK_SCROLL)
1024 gdk_event_get_scroll_direction(event, &direction);
1028 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
1030 case GDK_SCROLL_DOWN:
1031 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
1042 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
1044 struct Client *c = (struct Client *)data;
1048 if (key_common(widget, event, data))
1051 if (event->type == GDK_KEY_PRESS)
1053 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
1055 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
1056 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
1059 else if (event->type == GDK_BUTTON_RELEASE)
1061 switch (((GdkEventButton *)event)->button)
1064 if (c->hover_uri != NULL)
1066 client_new(c->hover_uri, NULL, TRUE, FALSE);
1071 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1074 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
1078 else if (event->type == GDK_SCROLL)
1080 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
1081 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
1083 gdk_event_get_scroll_deltas(event, &dx, &dy);
1084 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1086 z = dx != 0 ? global_zoom : z;
1087 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1096 mainwindow_setup(void)
1098 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1099 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1100 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1101 gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
1103 mw.notebook = gtk_notebook_new();
1104 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1105 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), tab_pos);
1106 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1107 g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
1108 G_CALLBACK(notebook_switch_page), NULL);
1112 mainwindow_title(gint idx)
1114 GtkWidget *child, *widg, *tablabel;
1117 child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
1121 widg = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
1122 tablabel = (GtkWidget *)g_object_get_data(G_OBJECT(widg), "lariza-tab-label");
1123 text = gtk_label_get_text(GTK_LABEL(tablabel));
1124 gtk_window_set_title(GTK_WINDOW(mw.win), text);
1128 notebook_switch_page(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
1130 mainwindow_title(idx);
1134 quit_if_nothing_active(void)
1144 gtk_widget_show_all(dm.win);
1151 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1155 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1159 client_new(uri, NULL, TRUE, TRUE);
1166 run_user_scripts(WebKitWebView *web_view)
1168 gchar *base = NULL, *path = NULL, *contents = NULL;
1169 const gchar *entry = NULL;
1170 GDir *scriptdir = NULL;
1172 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts", NULL);
1173 scriptdir = g_dir_open(base, 0, NULL);
1174 if (scriptdir != NULL)
1176 while ((entry = g_dir_read_name(scriptdir)) != NULL)
1178 path = g_build_filename(base, entry, NULL);
1179 if (g_str_has_suffix(path, ".js"))
1181 if (g_file_get_contents(path, &contents, NULL, NULL))
1183 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1189 g_dir_close(scriptdir);
1196 search(gpointer data, gint direction)
1198 struct Client *c = (struct Client *)data;
1199 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1200 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1202 if (search_text == NULL)
1208 webkit_find_controller_search(fc, search_text,
1209 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1210 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1214 webkit_find_controller_search_next(fc);
1217 webkit_find_controller_search_previous(fc);
1223 show_web_view(WebKitWebView *web_view, gpointer data)
1225 struct Client *c = (struct Client *)data;
1230 gtk_widget_show_all(mw.win);
1232 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);
1253 file = g_dir_read_name(dir);
1254 while (file != NULL)
1256 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1258 cert = g_tls_certificate_new_from_file(absfile, NULL);
1260 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1262 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1263 file = g_dir_read_name(dir);
1271 main(int argc, char **argv)
1275 gtk_init(&argc, &argv);
1276 grab_environment_configuration();
1278 while ((opt = getopt(argc, argv, "C")) != -1)
1283 cooperative_instances = FALSE;
1286 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1291 if (cooperative_instances)
1292 cooperation_setup();
1294 if (!cooperative_instances || cooperative_alone)
1295 init_default_web_context();
1297 downloadmanager_setup();
1301 client_new(home_uri, NULL, TRUE, TRUE);
1304 for (i = optind; i < argc; i++)
1305 client_new(argv[i], NULL, TRUE, TRUE);
1308 if (!cooperative_instances || cooperative_alone)