11 #include <gdk/gdkkeysyms.h>
13 #include <webkit2/webkit2.h>
14 #include <JavaScriptCore/JavaScript.h>
17 static gboolean button_tablabel(GtkWidget *, GdkEvent *, gpointer);
18 static void client_destroy(GtkWidget *, gpointer);
19 static WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean);
20 static WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
22 static void cooperation_setup(void);
23 static void changed_download_progress(GObject *, GParamSpec *, gpointer);
24 static void changed_load_progress(GObject *, GParamSpec *, gpointer);
25 static void changed_favicon(GObject *, GParamSpec *, gpointer);
26 static void changed_title(GObject *, GParamSpec *, gpointer);
27 static void changed_uri(GObject *, GParamSpec *, gpointer);
28 static gboolean crashed_web_view(WebKitWebView *, gpointer);
29 static gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
30 WebKitPolicyDecisionType, gpointer);
31 static gboolean download_handle(WebKitDownload *, gchar *, gpointer);
32 static void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
33 static void downloadmanager_cancel(GtkToolButton *, gpointer);
34 static gboolean downloadmanager_delete(GtkWidget *, gpointer);
35 static void downloadmanager_setup(void);
36 static gchar *ensure_uri_scheme(const gchar *);
37 static void external_handler_run(GSimpleAction *, GVariant *, gpointer);
38 static void grab_environment_configuration(void);
39 static void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
40 static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
41 static void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
42 static gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
43 static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
44 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
45 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
46 static void keywords_load(void);
47 static gboolean keywords_try_search(WebKitWebView *, const gchar *);
48 static void mainwindow_setup(void);
49 static void mainwindow_title(gint);
50 static void mainwindow_title_before(GtkNotebook *, GtkWidget *, guint, gpointer);
51 static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
52 WebKitHitTestResult *, gpointer);
53 static gboolean quit_if_nothing_active(void);
54 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
55 static void run_user_scripts(WebKitWebView *);
56 static void search(gpointer, gint);
57 static void show_web_view(WebKitWebView *, gpointer);
58 static void trust_user_certs(WebKitWebContext *);
63 gchar *external_handler_uri;
79 struct DownloadManager
87 static const gchar *accepted_language[2] = { NULL, NULL };
88 static gint clients = 0, downloads = 0;
89 static gboolean cooperative_alone = TRUE;
90 static gboolean cooperative_instances = TRUE;
91 static int cooperative_pipe_fp = 0;
92 static gchar *download_dir = "/var/tmp";
93 static gboolean enable_console_to_stdout = FALSE;
94 static gchar *fifo_suffix = "main";
95 static gdouble global_zoom = 1.0;
96 static gchar *history_file = NULL;
97 static gchar *home_uri = "about:blank";
98 static gboolean initial_wc_setup_done = FALSE;
99 static GHashTable *keywords = NULL;
100 static gchar *search_text = NULL;
101 static GtkPositionType tab_pos = GTK_POS_TOP;
102 static gint tab_width_chars = 20;
103 static gchar *user_agent = NULL;
107 button_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
109 if (event->type == GDK_BUTTON_RELEASE)
111 switch (((GdkEventButton *)event)->button)
114 client_destroy(NULL, data);
122 client_destroy(GtkWidget *widget, gpointer data)
124 struct Client *c = (struct Client *)data;
127 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
128 changed_load_progress, c);
130 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
132 fprintf(stderr, __NAME__": Tab index was -1, bamboozled\n");
134 gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
139 quit_if_nothing_active();
143 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
146 WebKitWebContext *wc;
148 GtkWidget *evbox, *tabbox;
150 if (uri != NULL && cooperative_instances && !cooperative_alone)
152 f = ensure_uri_scheme(uri);
153 write(cooperative_pipe_fp, f, strlen(f));
154 write(cooperative_pipe_fp, "\n", 1);
159 c = calloc(1, sizeof(struct Client));
162 fprintf(stderr, __NAME__": fatal: calloc failed\n");
166 if (related_wv == NULL)
167 c->web_view = webkit_web_view_new();
169 c->web_view = webkit_web_view_new_with_related_view(related_wv);
170 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
172 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
173 g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
174 G_CALLBACK(changed_favicon), c);
175 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
176 G_CALLBACK(changed_title), c);
177 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
178 G_CALLBACK(changed_uri), c);
179 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
180 G_CALLBACK(changed_load_progress), c);
181 g_signal_connect(G_OBJECT(c->web_view), "create",
182 G_CALLBACK(client_new_request), NULL);
183 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
184 G_CALLBACK(menu_web_view), c);
185 g_signal_connect(G_OBJECT(c->web_view), "close",
186 G_CALLBACK(client_destroy), c);
187 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
188 G_CALLBACK(decide_policy), NULL);
189 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
190 G_CALLBACK(key_web_view), c);
191 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
192 G_CALLBACK(key_web_view), c);
193 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
194 G_CALLBACK(key_web_view), c);
195 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
196 G_CALLBACK(hover_web_view), c);
197 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
198 G_CALLBACK(crashed_web_view), c);
200 if (!initial_wc_setup_done)
202 if (accepted_language[0] != NULL)
203 webkit_web_context_set_preferred_languages(wc, accepted_language);
205 g_signal_connect(G_OBJECT(wc), "download-started",
206 G_CALLBACK(download_handle_start), NULL);
208 trust_user_certs(wc);
210 webkit_web_context_set_favicon_database_directory(wc, NULL);
212 initial_wc_setup_done = TRUE;
215 if (user_agent != NULL)
216 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
217 "user-agent", user_agent, NULL);
219 if (enable_console_to_stdout)
220 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
222 webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
224 c->location = gtk_entry_new();
225 g_signal_connect(G_OBJECT(c->location), "key-press-event",
226 G_CALLBACK(key_location), c);
227 g_signal_connect(G_OBJECT(c->location), "icon-release",
228 G_CALLBACK(icon_location), c);
229 /* XXX This is a workaround. Setting this to NULL (which is done in
230 * grab_feeds_finished() if no feed has been detected) adds a little
231 * padding left of the text. Not sure why. The point of this call
232 * right here is to have that padding right from the start. This
233 * avoids a graphical artifact. */
234 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
235 GTK_ENTRY_ICON_PRIMARY,
238 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
239 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
240 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
242 c->tabicon = gtk_image_new_from_icon_name("text-html", GTK_ICON_SIZE_LARGE_TOOLBAR);
244 c->tablabel = gtk_label_new(__NAME__);
245 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
246 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), tab_width_chars);
248 tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
249 gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
250 gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
252 evbox = gtk_event_box_new();
253 gtk_container_add(GTK_CONTAINER(evbox), tabbox);
254 g_signal_connect(G_OBJECT(evbox), "button-release-event",
255 G_CALLBACK(button_tablabel), c);
257 /* This only shows the event box and the label inside, nothing else.
258 * Needed because the evbox/label is "internal" to the notebook and
259 * not part of the normal "widget tree" (IIUC). */
260 gtk_widget_show_all(evbox);
262 gtk_notebook_append_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox);
263 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
266 show_web_view(NULL, c);
268 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
269 G_CALLBACK(show_web_view), c);
273 f = ensure_uri_scheme(uri);
274 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
280 return WEBKIT_WEB_VIEW(c->web_view);
284 client_new_request(WebKitWebView *web_view,
285 WebKitNavigationAction *navigation_action, gpointer data)
287 return client_new(NULL, web_view, FALSE);
291 cooperation_setup(void)
294 gchar *fifofilename, *fifopath;
296 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
297 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
298 g_free(fifofilename);
300 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
301 mkfifo(fifopath, 0600);
303 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
304 if (!cooperative_pipe_fp)
306 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
310 if (write(cooperative_pipe_fp, "", 0) == -1)
312 /* Could not do an empty write to the FIFO which means there's
313 * no one listening. */
314 close(cooperative_pipe_fp);
315 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
316 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
319 cooperative_alone = FALSE;
326 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
328 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
329 WebKitURIResponse *resp;
330 GtkToolItem *tb = GTK_TOOL_ITEM(data);
333 gchar *t, *filename, *base;
335 p = webkit_download_get_estimated_progress(download);
339 resp = webkit_download_get_response(download);
340 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
342 uri = webkit_download_get_destination(download);
343 filename = g_filename_from_uri(uri, NULL, NULL);
344 if (filename == NULL)
346 /* This really should not happen because WebKit uses that URI to
347 * write to a file... */
348 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
349 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
350 webkit_uri_response_get_uri(resp), p, size_mb);
354 base = g_path_get_basename(filename);
355 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
359 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
364 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
366 struct Client *c = (struct Client *)data;
369 "a = document.querySelectorAll('"
370 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
371 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
378 " for (i = 0; i < a.length; i++)"
380 " url = encodeURIComponent(a[i].href);"
381 " if ('title' in a[i] && a[i].title != '')"
382 " title = encodeURIComponent(a[i].title);"
385 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
390 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
395 /* The page has loaded fully. We now run the short JavaScript
396 * snippet above that operates on the DOM. It tries to grab all
397 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
399 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
401 grab_feeds_finished, c);
403 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
405 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
409 changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
411 struct Client *c = (struct Client *)data;
413 int w, h, w_scaled, h_scaled;
414 GdkPixbuf *pb, *pb_scaled;
416 f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
419 w = cairo_image_surface_get_width(f);
420 h = cairo_image_surface_get_height(f);
421 pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
424 if (w > 16 || h > 16)
426 w_scaled = 16 * gtk_widget_get_scale_factor(c->tabicon);
427 h_scaled = 16 * gtk_widget_get_scale_factor(c->tabicon);
428 pb_scaled = gdk_pixbuf_scale_simple(pb, w_scaled, h_scaled,
429 GDK_INTERP_BILINEAR);
430 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon), pb_scaled);
431 g_object_unref(pb_scaled);
434 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon), pb);
442 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
445 struct Client *c = (struct Client *)data;
447 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
448 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
450 u = u == NULL ? __NAME__ : u;
451 u = u[0] == 0 ? __NAME__ : u;
453 t = t == NULL ? u : t;
454 t = t[0] == 0 ? u : t;
456 gtk_label_set_text(GTK_LABEL(c->tablabel), t);
457 mainwindow_title(-1);
461 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
464 struct Client *c = (struct Client *)data;
467 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
469 /* When a web process crashes, we get a "notify::uri" signal, but we
470 * can no longer read a meaningful URI. It's just an empty string
471 * now. Not updating the location bar in this scenario is important,
472 * because we would override the "WEB PROCESS CRASHED" message. */
473 if (t != NULL && strlen(t) > 0)
475 gtk_entry_set_text(GTK_ENTRY(c->location), t);
477 if (history_file != NULL)
479 fp = fopen(history_file, "a");
482 fprintf(fp, "%s\n", t);
486 perror(__NAME__": Error opening history file");
492 crashed_web_view(WebKitWebView *web_view, gpointer data)
495 struct Client *c = (struct Client *)data;
497 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
498 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
499 gtk_entry_set_text(GTK_ENTRY(c->location), t);
506 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
507 WebKitPolicyDecisionType type, gpointer data)
509 WebKitResponsePolicyDecision *r;
513 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
514 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
515 if (!webkit_response_policy_decision_is_mime_type_supported(r))
516 webkit_policy_decision_download(decision);
518 webkit_policy_decision_use(decision);
521 /* Use whatever default there is. */
528 download_handle_finished(WebKitDownload *download, gpointer data)
534 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
537 g_signal_connect(G_OBJECT(download), "decide-destination",
538 G_CALLBACK(download_handle), data);
542 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
544 gchar *sug_clean, *path, *path2 = NULL, *uri;
549 sug_clean = g_strdup(suggested_filename);
550 for (i = 0; i < strlen(sug_clean); i++)
551 if (sug_clean[i] == G_DIR_SEPARATOR)
554 path = g_build_filename(download_dir, sug_clean, NULL);
555 path2 = g_strdup(path);
556 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
560 path2 = g_strdup_printf("%s.%d", path, suffix);
566 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
567 webkit_download_cancel(download);
571 uri = g_filename_to_uri(path2, NULL, NULL);
572 webkit_download_set_destination(download, uri);
575 tb = gtk_tool_button_new(NULL, NULL);
576 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
577 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
578 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
579 gtk_widget_show_all(dm.win);
581 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
582 G_CALLBACK(changed_download_progress), tb);
585 g_signal_connect(G_OBJECT(download), "finished",
586 G_CALLBACK(download_handle_finished), NULL);
588 g_object_ref(download);
589 g_signal_connect(G_OBJECT(tb), "clicked",
590 G_CALLBACK(downloadmanager_cancel), download);
597 /* Propagate -- to whom it may concern. */
602 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
604 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
606 webkit_download_cancel(download);
607 g_object_unref(download);
609 gtk_widget_destroy(GTK_WIDGET(tb));
613 downloadmanager_delete(GtkWidget *obj, gpointer data)
615 if (!quit_if_nothing_active())
616 gtk_widget_hide(dm.win);
622 downloadmanager_setup(void)
624 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
625 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
626 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
627 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
628 g_signal_connect(G_OBJECT(dm.win), "delete-event",
629 G_CALLBACK(downloadmanager_delete), NULL);
630 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
631 G_CALLBACK(key_downloadmanager), NULL);
633 dm.toolbar = gtk_toolbar_new();
634 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
635 GTK_ORIENTATION_VERTICAL);
636 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
637 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
639 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
640 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
641 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
642 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
644 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
648 ensure_uri_scheme(const gchar *t)
652 f = g_ascii_strdown(t, -1);
653 if (!g_str_has_prefix(f, "http:") &&
654 !g_str_has_prefix(f, "https:") &&
655 !g_str_has_prefix(f, "file:") &&
656 !g_str_has_prefix(f, "about:") &&
657 !g_str_has_prefix(f, "data:"))
660 fabs = realpath(t, NULL);
663 f = g_strdup_printf("file://%s", fabs);
667 f = g_strdup_printf("http://%s", t);
675 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
677 struct Client *c = (struct Client *)data;
678 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
685 argv[2] = c->external_handler_uri;
686 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
689 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
694 g_spawn_close_pid(pid);
698 grab_environment_configuration(void)
702 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
704 accepted_language[0] = g_strdup(e);
706 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
708 download_dir = g_strdup(e);
710 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
712 enable_console_to_stdout = TRUE;
714 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
716 fifo_suffix = g_strdup(e);
718 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
720 history_file = g_strdup(e);
722 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
724 home_uri = g_strdup(e);
726 e = g_getenv(__NAME_UPPERCASE__"_TAB_POS");
729 if (strcmp(e, "top") == 0)
730 tab_pos = GTK_POS_TOP;
731 if (strcmp(e, "right") == 0)
732 tab_pos = GTK_POS_RIGHT;
733 if (strcmp(e, "bottom") == 0)
734 tab_pos = GTK_POS_BOTTOM;
735 if (strcmp(e, "left") == 0)
736 tab_pos = GTK_POS_LEFT;
739 e = g_getenv(__NAME_UPPERCASE__"_TAB_WIDTH_CHARS");
741 tab_width_chars = atoi(e);
743 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
745 user_agent = g_strdup(e);
747 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
749 global_zoom = atof(e);
753 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
755 struct Client *c = (struct Client *)data;
756 WebKitJavascriptResult *js_result;
758 JSCException *exception;
762 g_free(c->feed_html);
765 /* This was taken almost verbatim from the example in WebKit's
768 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
770 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
774 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
779 value = webkit_javascript_result_get_js_value(js_result);
780 if (jsc_value_is_string(value))
782 str_value = jsc_value_to_string(value);
783 exception = jsc_context_get_exception(jsc_value_get_context(value));
784 if (exception != NULL)
786 fprintf(stderr, __NAME__": Error running javascript: %s\n",
787 jsc_exception_get_message(exception));
790 c->feed_html = str_value;
792 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
793 GTK_ENTRY_ICON_PRIMARY,
794 "application-rss+xml-symbolic");
795 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
796 GTK_ENTRY_ICON_PRIMARY,
801 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
802 GTK_ENTRY_ICON_PRIMARY,
806 webkit_javascript_result_unref(js_result);
810 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
813 struct Client *c = (struct Client *)data;
815 if (!gtk_widget_is_focus(c->location))
817 if (webkit_hit_test_result_context_is_link(ht))
819 gtk_entry_set_text(GTK_ENTRY(c->location),
820 webkit_hit_test_result_get_link_uri(ht));
822 if (c->hover_uri != NULL)
823 g_free(c->hover_uri);
824 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
828 gtk_entry_set_text(GTK_ENTRY(c->location),
829 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
831 if (c->hover_uri != NULL)
832 g_free(c->hover_uri);
839 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
842 struct Client *c = (struct Client *)data;
844 gchar *data_template =
849 " <meta charset=\"UTF-8\">"
850 " <title>Feeds</title>"
853 " <p>Feeds found on this page:</p>"
860 if (c->feed_html != NULL)
862 /* What we're actually trying to do is show a simple HTML page
863 * that lists all the feeds on the current page. The function
864 * webkit_web_view_load_html() looks like the proper way to do
865 * that. Sad thing is, it doesn't create a history entry, but
866 * instead simply replaces the content of the current page. This
867 * is not what we want.
869 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
870 * mechanism to show my custom HTML snippet *and* create a
873 * [0]: https://tools.ietf.org/html/rfc2397
874 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
875 d = g_strdup_printf(data_template, c->feed_html);
876 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
882 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
884 struct Client *c = (struct Client *)data;
885 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
888 if (event->type == GDK_KEY_PRESS)
890 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
892 switch (((GdkEventKey *)event)->keyval)
894 case GDK_KEY_q: /* close window (left hand) */
895 client_destroy(NULL, c);
897 case GDK_KEY_w: /* home (left hand) */
898 f = ensure_uri_scheme(home_uri);
899 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
902 case GDK_KEY_e: /* new tab (left hand) */
903 f = ensure_uri_scheme(home_uri);
904 client_new(f, NULL, TRUE);
907 case GDK_KEY_r: /* reload (left hand) */
908 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
911 case GDK_KEY_d: /* download manager (left hand) */
912 gtk_widget_show_all(dm.win);
914 case GDK_KEY_2: /* search forward (left hand) */
915 case GDK_KEY_n: /* search forward (maybe both hands) */
918 case GDK_KEY_3: /* search backward (left hand) */
921 case GDK_KEY_l: /* location (BOTH hands) */
922 gtk_widget_grab_focus(c->location);
924 case GDK_KEY_k: /* initiate search (BOTH hands) */
925 gtk_widget_grab_focus(c->location);
926 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
927 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
929 case GDK_KEY_c: /* reload trusted certs (left hand) */
930 trust_user_certs(wc);
932 case GDK_KEY_x: /* launch external handler (left hand) */
933 if (c->external_handler_uri != NULL)
934 g_free(c->external_handler_uri);
935 c->external_handler_uri = g_strdup(
936 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
937 external_handler_run(NULL, NULL, c);
939 case GDK_KEY_a: /* go one tab to the left (left hand) */
940 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
942 case GDK_KEY_s: /* go one tab to the right (left hand) */
943 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
947 /* navigate backward (left hand) */
948 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
950 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
953 /* navigate forward (left hand) */
954 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
956 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
965 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
967 if (event->type == GDK_KEY_PRESS)
969 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
971 switch (((GdkEventKey *)event)->keyval)
973 case GDK_KEY_d: /* close window (left hand) */
975 downloadmanager_delete(dm.win, NULL);
985 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
987 struct Client *c = (struct Client *)data;
991 if (key_common(widget, event, data))
994 if (event->type == GDK_KEY_PRESS)
996 switch (((GdkEventKey *)event)->keyval)
998 case GDK_KEY_KP_Enter:
1000 gtk_widget_grab_focus(c->web_view);
1001 t = gtk_entry_get_text(GTK_ENTRY(c->location));
1002 if (t != NULL && t[0] == ':' && t[1] == '/')
1004 if (search_text != NULL)
1005 g_free(search_text);
1006 search_text = g_strdup(t + 2);
1009 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
1011 f = ensure_uri_scheme(t);
1012 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
1016 case GDK_KEY_Escape:
1017 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
1018 gtk_entry_set_text(GTK_ENTRY(c->location),
1019 (t == NULL ? __NAME__ : t));
1028 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
1030 struct Client *c = (struct Client *)data;
1034 if (key_common(widget, event, data))
1037 if (event->type == GDK_KEY_PRESS)
1039 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
1041 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
1042 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
1045 else if (event->type == GDK_BUTTON_RELEASE)
1047 switch (((GdkEventButton *)event)->button)
1050 if (c->hover_uri != NULL)
1052 client_new(c->hover_uri, NULL, TRUE);
1057 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1060 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
1064 else if (event->type == GDK_SCROLL)
1066 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
1067 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
1069 gdk_event_get_scroll_deltas(event, &dx, &dy);
1070 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1072 z = dx != 0 ? global_zoom : z;
1073 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1085 GIOChannel *channel = NULL;
1086 gchar *path = NULL, *buf = NULL;
1087 gchar **tokens = NULL;
1089 keywords = g_hash_table_new(g_str_hash, g_str_equal);
1091 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1093 channel = g_io_channel_new_file(path, "r", &err);
1094 if (channel != NULL)
1096 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1097 == G_IO_STATUS_NORMAL)
1102 tokens = g_strsplit(buf, " ", 2);
1103 if (tokens[0] != NULL && tokens[1] != NULL)
1104 g_hash_table_insert(keywords, g_strdup(tokens[0]),
1105 g_strdup(tokens[1]));
1110 g_io_channel_shutdown(channel, FALSE, NULL);
1116 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1118 gboolean ret = FALSE;
1119 gchar **tokens = NULL;
1120 gchar *val = NULL, *escaped = NULL, *uri = NULL;
1122 tokens = g_strsplit(t, " ", 2);
1123 if (tokens[0] != NULL && tokens[1] != NULL)
1125 val = g_hash_table_lookup(keywords, tokens[0]);
1128 escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1129 uri = g_strdup_printf((gchar *)val, escaped);
1130 webkit_web_view_load_uri(web_view, uri);
1142 mainwindow_setup(void)
1144 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1145 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1146 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1147 gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
1149 mw.notebook = gtk_notebook_new();
1150 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1151 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), tab_pos);
1152 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1153 g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
1154 G_CALLBACK(mainwindow_title_before), NULL);
1158 mainwindow_title_before(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
1160 mainwindow_title(idx);
1164 mainwindow_title(gint idx)
1166 GtkWidget *child, *tabbox, *evbox, *label;
1167 GList *tabbox_children, *last;
1172 idx = gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook));
1177 child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
1181 evbox = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
1182 tabbox = gtk_bin_get_child(GTK_BIN(evbox));
1183 tabbox_children = gtk_container_get_children(GTK_CONTAINER(tabbox));
1184 last = g_list_last(tabbox_children);
1186 text = gtk_label_get_text(GTK_LABEL(label));
1187 gtk_window_set_title(GTK_WINDOW(mw.win), text);
1188 g_list_free(tabbox_children);
1192 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1193 WebKitHitTestResult *ht, gpointer data)
1195 struct Client *c = (struct Client *)data;
1196 GSimpleAction *action = NULL;
1197 WebKitContextMenuItem *mi = NULL;
1198 const gchar *uri = NULL;
1202 if (webkit_hit_test_result_context_is_link(ht))
1203 uri = webkit_hit_test_result_get_link_uri(ht);
1204 else if (webkit_hit_test_result_context_is_image(ht))
1205 uri = webkit_hit_test_result_get_image_uri(ht);
1206 else if (webkit_hit_test_result_context_is_media(ht))
1207 uri = webkit_hit_test_result_get_media_uri(ht);
1211 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1213 if (c->external_handler_uri != NULL)
1214 g_free(c->external_handler_uri);
1215 c->external_handler_uri = g_strdup(uri);
1216 action = g_simple_action_new("external_handler", NULL);
1217 g_signal_connect(G_OBJECT(action), "activate",
1218 G_CALLBACK(external_handler_run), data);
1219 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1220 "Open with external handler",
1222 webkit_context_menu_append(menu, mi);
1223 g_object_unref(action);
1226 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1231 quit_if_nothing_active(void)
1241 gtk_widget_show_all(dm.win);
1248 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1252 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1256 client_new(uri, NULL, TRUE);
1263 run_user_scripts(WebKitWebView *web_view)
1265 gchar *base = NULL, *path = NULL, *contents = NULL;
1266 const gchar *entry = NULL;
1267 GDir *scriptdir = NULL;
1269 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts", NULL);
1270 scriptdir = g_dir_open(base, 0, NULL);
1271 if (scriptdir != NULL)
1273 while ((entry = g_dir_read_name(scriptdir)) != NULL)
1275 path = g_build_filename(base, entry, NULL);
1276 if (g_str_has_suffix(path, ".js"))
1278 if (g_file_get_contents(path, &contents, NULL, NULL))
1280 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1286 g_dir_close(scriptdir);
1293 search(gpointer data, gint direction)
1295 struct Client *c = (struct Client *)data;
1296 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1297 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1299 if (search_text == NULL)
1305 webkit_find_controller_search(fc, search_text,
1306 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1307 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1311 webkit_find_controller_search_next(fc);
1314 webkit_find_controller_search_previous(fc);
1320 show_web_view(WebKitWebView *web_view, gpointer data)
1322 struct Client *c = (struct Client *)data;
1327 gtk_widget_show_all(mw.win);
1329 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1331 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
1333 gtk_widget_grab_focus(c->web_view);
1337 trust_user_certs(WebKitWebContext *wc)
1339 GTlsCertificate *cert;
1340 const gchar *basedir, *file, *absfile;
1343 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1344 dir = g_dir_open(basedir, 0, NULL);
1347 file = g_dir_read_name(dir);
1348 while (file != NULL)
1350 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1352 cert = g_tls_certificate_new_from_file(absfile, NULL);
1354 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1356 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1357 file = g_dir_read_name(dir);
1365 main(int argc, char **argv)
1370 gtk_init(&argc, &argv);
1371 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1372 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1374 grab_environment_configuration();
1376 while ((opt = getopt(argc, argv, "C")) != -1)
1381 cooperative_instances = FALSE;
1384 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1390 if (cooperative_instances)
1391 cooperation_setup();
1392 downloadmanager_setup();
1396 if (!cooperative_instances || cooperative_alone)
1398 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1400 webkit_web_context_set_web_extensions_directory(
1401 webkit_web_context_get_default(), c
1406 client_new(home_uri, NULL, TRUE);
1409 for (i = optind; i < argc; i++)
1410 client_new(argv[i], NULL, TRUE);
1413 if (!cooperative_instances || cooperative_alone)