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,
21 static WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
23 static void cooperation_setup(void);
24 static void changed_download_progress(GObject *, GParamSpec *, gpointer);
25 static void changed_load_progress(GObject *, GParamSpec *, gpointer);
26 static void changed_favicon(GObject *, GParamSpec *, gpointer);
27 static void changed_title(GObject *, GParamSpec *, gpointer);
28 static void changed_uri(GObject *, GParamSpec *, gpointer);
29 static gboolean crashed_web_view(WebKitWebView *, gpointer);
30 static gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
31 WebKitPolicyDecisionType, gpointer);
32 static gboolean download_handle(WebKitDownload *, gchar *, gpointer);
33 static void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
34 static void downloadmanager_cancel(GtkToolButton *, gpointer);
35 static gboolean downloadmanager_delete(GtkWidget *, gpointer);
36 static void downloadmanager_setup(void);
37 static gchar *ensure_uri_scheme(const gchar *);
38 static void external_handler_run(GSimpleAction *, GVariant *, gpointer);
39 static void grab_environment_configuration(void);
40 static void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
41 static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
42 static void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
43 static gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
44 static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
45 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
46 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
47 static void keywords_load(void);
48 static gboolean keywords_try_search(WebKitWebView *, const gchar *);
49 static void mainwindow_setup(void);
50 static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
51 WebKitHitTestResult *, gpointer);
52 static gboolean quit_if_nothing_active(void);
53 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
54 static void run_user_scripts(WebKitWebView *);
55 static void search(gpointer, gint);
56 static void show_web_view(WebKitWebView *, gpointer);
57 static void trust_user_certs(WebKitWebContext *);
62 gchar *external_handler_uri;
70 gboolean focus_new_tab;
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,
147 WebKitWebContext *wc;
149 GtkWidget *evbox, *tabbox;
151 if (uri != NULL && cooperative_instances && !cooperative_alone)
153 f = ensure_uri_scheme(uri);
154 write(cooperative_pipe_fp, f, strlen(f));
155 write(cooperative_pipe_fp, "\n", 1);
160 c = calloc(1, sizeof(struct Client));
163 fprintf(stderr, __NAME__": fatal: calloc failed\n");
167 c->focus_new_tab = focus_tab;
169 if (related_wv == NULL)
170 c->web_view = webkit_web_view_new();
172 c->web_view = webkit_web_view_new_with_related_view(related_wv);
173 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
175 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
176 g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
177 G_CALLBACK(changed_favicon), c);
178 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
179 G_CALLBACK(changed_title), c);
180 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
181 G_CALLBACK(changed_uri), c);
182 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
183 G_CALLBACK(changed_load_progress), c);
184 g_signal_connect(G_OBJECT(c->web_view), "create",
185 G_CALLBACK(client_new_request), NULL);
186 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
187 G_CALLBACK(menu_web_view), c);
188 g_signal_connect(G_OBJECT(c->web_view), "close",
189 G_CALLBACK(client_destroy), c);
190 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
191 G_CALLBACK(decide_policy), NULL);
192 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
193 G_CALLBACK(key_web_view), c);
194 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
195 G_CALLBACK(key_web_view), c);
196 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
197 G_CALLBACK(key_web_view), c);
198 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
199 G_CALLBACK(hover_web_view), c);
200 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
201 G_CALLBACK(crashed_web_view), c);
203 if (!initial_wc_setup_done)
205 if (accepted_language[0] != NULL)
206 webkit_web_context_set_preferred_languages(wc, accepted_language);
208 g_signal_connect(G_OBJECT(wc), "download-started",
209 G_CALLBACK(download_handle_start), NULL);
211 trust_user_certs(wc);
213 webkit_web_context_set_favicon_database_directory(wc, NULL);
215 initial_wc_setup_done = TRUE;
218 if (user_agent != NULL)
219 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
220 "user-agent", user_agent, NULL);
222 if (enable_console_to_stdout)
223 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
225 webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
227 c->location = gtk_entry_new();
228 g_signal_connect(G_OBJECT(c->location), "key-press-event",
229 G_CALLBACK(key_location), c);
230 g_signal_connect(G_OBJECT(c->location), "icon-release",
231 G_CALLBACK(icon_location), c);
232 /* XXX This is a workaround. Setting this to NULL (which is done in
233 * grab_feeds_finished() if no feed has been detected) adds a little
234 * padding left of the text. Not sure why. The point of this call
235 * right here is to have that padding right from the start. This
236 * avoids a graphical artifact. */
237 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
238 GTK_ENTRY_ICON_PRIMARY,
241 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
242 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
243 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
245 c->tabicon = gtk_image_new_from_icon_name("text-html", GTK_ICON_SIZE_SMALL_TOOLBAR);
247 c->tablabel = gtk_label_new(__NAME__);
248 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
249 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), tab_width_chars);
250 gtk_widget_set_has_tooltip(c->tablabel, TRUE);
252 /* XXX I don't own a HiDPI screen, so I don't know if scale_factor
253 * does the right thing. */
254 tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
255 5 * gtk_widget_get_scale_factor(mw.win));
256 gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
257 gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
259 evbox = gtk_event_box_new();
260 gtk_container_add(GTK_CONTAINER(evbox), tabbox);
261 g_signal_connect(G_OBJECT(evbox), "button-release-event",
262 G_CALLBACK(button_tablabel), c);
264 /* This only shows the event box and the label inside, nothing else.
265 * Needed because the evbox/label is "internal" to the notebook and
266 * not part of the normal "widget tree" (IIUC). */
267 gtk_widget_show_all(evbox);
269 gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox,
270 gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1);
271 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
274 show_web_view(NULL, c);
276 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
277 G_CALLBACK(show_web_view), c);
281 f = ensure_uri_scheme(uri);
282 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
288 return WEBKIT_WEB_VIEW(c->web_view);
292 client_new_request(WebKitWebView *web_view,
293 WebKitNavigationAction *navigation_action, gpointer data)
295 return client_new(NULL, web_view, FALSE, FALSE);
299 cooperation_setup(void)
302 gchar *fifofilename, *fifopath;
304 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
305 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
306 g_free(fifofilename);
308 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
309 mkfifo(fifopath, 0600);
311 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
312 if (!cooperative_pipe_fp)
314 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
318 if (write(cooperative_pipe_fp, "", 0) == -1)
320 /* Could not do an empty write to the FIFO which means there's
321 * no one listening. */
322 close(cooperative_pipe_fp);
323 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
324 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
327 cooperative_alone = FALSE;
334 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
336 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
337 WebKitURIResponse *resp;
338 GtkToolItem *tb = GTK_TOOL_ITEM(data);
341 gchar *t, *filename, *base;
343 p = webkit_download_get_estimated_progress(download);
347 resp = webkit_download_get_response(download);
348 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
350 uri = webkit_download_get_destination(download);
351 filename = g_filename_from_uri(uri, NULL, NULL);
352 if (filename == NULL)
354 /* This really should not happen because WebKit uses that URI to
355 * write to a file... */
356 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
357 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
358 webkit_uri_response_get_uri(resp), p, size_mb);
362 base = g_path_get_basename(filename);
363 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
367 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
372 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
374 struct Client *c = (struct Client *)data;
377 "a = document.querySelectorAll('"
378 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
379 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
386 " for (i = 0; i < a.length; i++)"
388 " url = encodeURIComponent(a[i].href);"
389 " if ('title' in a[i] && a[i].title != '')"
390 " title = encodeURIComponent(a[i].title);"
393 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
398 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
403 /* The page has loaded fully. We now run the short JavaScript
404 * snippet above that operates on the DOM. It tries to grab all
405 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
407 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
409 grab_feeds_finished, c);
411 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
413 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
417 changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
419 struct Client *c = (struct Client *)data;
421 int w, h, w_should, h_should;
422 GdkPixbuf *pb, *pb_scaled;
424 f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
427 gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html",
428 GTK_ICON_SIZE_SMALL_TOOLBAR);
432 w = cairo_image_surface_get_width(f);
433 h = cairo_image_surface_get_height(f);
434 pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
437 w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
438 h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
439 pb_scaled = gdk_pixbuf_scale_simple(pb, w_should, h_should,
440 GDK_INTERP_BILINEAR);
441 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon), pb_scaled);
443 g_object_unref(pb_scaled);
450 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
453 struct Client *c = (struct Client *)data;
455 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
456 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
458 u = u == NULL ? __NAME__ : u;
459 u = u[0] == 0 ? __NAME__ : u;
461 t = t == NULL ? u : t;
462 t = t[0] == 0 ? u : t;
464 gtk_label_set_text(GTK_LABEL(c->tablabel), t);
465 gtk_widget_set_tooltip_text(c->tablabel, t);
469 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
472 struct Client *c = (struct Client *)data;
475 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
477 /* When a web process crashes, we get a "notify::uri" signal, but we
478 * can no longer read a meaningful URI. It's just an empty string
479 * now. Not updating the location bar in this scenario is important,
480 * because we would override the "WEB PROCESS CRASHED" message. */
481 if (t != NULL && strlen(t) > 0)
483 gtk_entry_set_text(GTK_ENTRY(c->location), t);
485 if (history_file != NULL)
487 fp = fopen(history_file, "a");
490 fprintf(fp, "%s\n", t);
494 perror(__NAME__": Error opening history file");
500 crashed_web_view(WebKitWebView *web_view, gpointer data)
503 struct Client *c = (struct Client *)data;
505 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
506 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
507 gtk_entry_set_text(GTK_ENTRY(c->location), t);
514 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
515 WebKitPolicyDecisionType type, gpointer data)
517 WebKitResponsePolicyDecision *r;
521 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
522 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
523 if (!webkit_response_policy_decision_is_mime_type_supported(r))
524 webkit_policy_decision_download(decision);
526 webkit_policy_decision_use(decision);
529 /* Use whatever default there is. */
536 download_handle_finished(WebKitDownload *download, gpointer data)
542 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
545 g_signal_connect(G_OBJECT(download), "decide-destination",
546 G_CALLBACK(download_handle), data);
550 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
552 gchar *sug_clean, *path, *path2 = NULL, *uri;
557 sug_clean = g_strdup(suggested_filename);
558 for (i = 0; i < strlen(sug_clean); i++)
559 if (sug_clean[i] == G_DIR_SEPARATOR)
562 path = g_build_filename(download_dir, sug_clean, NULL);
563 path2 = g_strdup(path);
564 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
568 path2 = g_strdup_printf("%s.%d", path, suffix);
574 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
575 webkit_download_cancel(download);
579 uri = g_filename_to_uri(path2, NULL, NULL);
580 webkit_download_set_destination(download, uri);
583 tb = gtk_tool_button_new(NULL, NULL);
584 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
585 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
586 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
587 gtk_widget_show_all(dm.win);
589 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
590 G_CALLBACK(changed_download_progress), tb);
593 g_signal_connect(G_OBJECT(download), "finished",
594 G_CALLBACK(download_handle_finished), NULL);
596 g_object_ref(download);
597 g_signal_connect(G_OBJECT(tb), "clicked",
598 G_CALLBACK(downloadmanager_cancel), download);
605 /* Propagate -- to whom it may concern. */
610 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
612 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
614 webkit_download_cancel(download);
615 g_object_unref(download);
617 gtk_widget_destroy(GTK_WIDGET(tb));
621 downloadmanager_delete(GtkWidget *obj, gpointer data)
623 if (!quit_if_nothing_active())
624 gtk_widget_hide(dm.win);
630 downloadmanager_setup(void)
632 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
633 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
634 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
635 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
636 g_signal_connect(G_OBJECT(dm.win), "delete-event",
637 G_CALLBACK(downloadmanager_delete), NULL);
638 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
639 G_CALLBACK(key_downloadmanager), NULL);
641 dm.toolbar = gtk_toolbar_new();
642 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
643 GTK_ORIENTATION_VERTICAL);
644 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
645 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
647 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
648 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
649 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
650 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
652 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
656 ensure_uri_scheme(const gchar *t)
660 f = g_ascii_strdown(t, -1);
661 if (!g_str_has_prefix(f, "http:") &&
662 !g_str_has_prefix(f, "https:") &&
663 !g_str_has_prefix(f, "file:") &&
664 !g_str_has_prefix(f, "about:") &&
665 !g_str_has_prefix(f, "data:"))
668 fabs = realpath(t, NULL);
671 f = g_strdup_printf("file://%s", fabs);
675 f = g_strdup_printf("http://%s", t);
683 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
685 struct Client *c = (struct Client *)data;
686 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
693 argv[2] = c->external_handler_uri;
694 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
697 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
702 g_spawn_close_pid(pid);
706 grab_environment_configuration(void)
710 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
712 accepted_language[0] = g_strdup(e);
714 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
716 download_dir = g_strdup(e);
718 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
720 enable_console_to_stdout = TRUE;
722 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
724 fifo_suffix = g_strdup(e);
726 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
728 history_file = g_strdup(e);
730 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
732 home_uri = g_strdup(e);
734 e = g_getenv(__NAME_UPPERCASE__"_TAB_POS");
737 if (strcmp(e, "top") == 0)
738 tab_pos = GTK_POS_TOP;
739 if (strcmp(e, "right") == 0)
740 tab_pos = GTK_POS_RIGHT;
741 if (strcmp(e, "bottom") == 0)
742 tab_pos = GTK_POS_BOTTOM;
743 if (strcmp(e, "left") == 0)
744 tab_pos = GTK_POS_LEFT;
747 e = g_getenv(__NAME_UPPERCASE__"_TAB_WIDTH_CHARS");
749 tab_width_chars = atoi(e);
751 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
753 user_agent = g_strdup(e);
755 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
757 global_zoom = atof(e);
761 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
763 struct Client *c = (struct Client *)data;
764 WebKitJavascriptResult *js_result;
766 JSCException *exception;
770 g_free(c->feed_html);
773 /* This was taken almost verbatim from the example in WebKit's
776 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
778 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
782 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
787 value = webkit_javascript_result_get_js_value(js_result);
788 if (jsc_value_is_string(value))
790 str_value = jsc_value_to_string(value);
791 exception = jsc_context_get_exception(jsc_value_get_context(value));
792 if (exception != NULL)
794 fprintf(stderr, __NAME__": Error running javascript: %s\n",
795 jsc_exception_get_message(exception));
798 c->feed_html = str_value;
800 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
801 GTK_ENTRY_ICON_PRIMARY,
802 "application-rss+xml-symbolic");
803 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
804 GTK_ENTRY_ICON_PRIMARY,
809 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
810 GTK_ENTRY_ICON_PRIMARY,
814 webkit_javascript_result_unref(js_result);
818 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
821 struct Client *c = (struct Client *)data;
823 if (!gtk_widget_is_focus(c->location))
825 if (webkit_hit_test_result_context_is_link(ht))
827 gtk_entry_set_text(GTK_ENTRY(c->location),
828 webkit_hit_test_result_get_link_uri(ht));
830 if (c->hover_uri != NULL)
831 g_free(c->hover_uri);
832 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
836 gtk_entry_set_text(GTK_ENTRY(c->location),
837 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
839 if (c->hover_uri != NULL)
840 g_free(c->hover_uri);
847 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
850 struct Client *c = (struct Client *)data;
852 gchar *data_template =
857 " <meta charset=\"UTF-8\">"
858 " <title>Feeds</title>"
861 " <p>Feeds found on this page:</p>"
868 if (c->feed_html != NULL)
870 /* What we're actually trying to do is show a simple HTML page
871 * that lists all the feeds on the current page. The function
872 * webkit_web_view_load_html() looks like the proper way to do
873 * that. Sad thing is, it doesn't create a history entry, but
874 * instead simply replaces the content of the current page. This
875 * is not what we want.
877 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
878 * mechanism to show my custom HTML snippet *and* create a
881 * [0]: https://tools.ietf.org/html/rfc2397
882 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
883 d = g_strdup_printf(data_template, c->feed_html);
884 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
890 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
892 struct Client *c = (struct Client *)data;
893 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
896 if (event->type == GDK_KEY_PRESS)
898 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
900 switch (((GdkEventKey *)event)->keyval)
902 case GDK_KEY_q: /* close window (left hand) */
903 client_destroy(NULL, c);
905 case GDK_KEY_w: /* home (left hand) */
906 f = ensure_uri_scheme(home_uri);
907 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
910 case GDK_KEY_e: /* new tab (left hand) */
911 f = ensure_uri_scheme(home_uri);
912 client_new(f, NULL, TRUE, TRUE);
915 case GDK_KEY_r: /* reload (left hand) */
916 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
919 case GDK_KEY_d: /* download manager (left hand) */
920 gtk_widget_show_all(dm.win);
922 case GDK_KEY_2: /* search forward (left hand) */
923 case GDK_KEY_n: /* search forward (maybe both hands) */
926 case GDK_KEY_3: /* search backward (left hand) */
929 case GDK_KEY_l: /* location (BOTH hands) */
930 gtk_widget_grab_focus(c->location);
932 case GDK_KEY_k: /* initiate search (BOTH hands) */
933 gtk_widget_grab_focus(c->location);
934 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
935 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
937 case GDK_KEY_c: /* reload trusted certs (left hand) */
938 trust_user_certs(wc);
940 case GDK_KEY_x: /* launch external handler (left hand) */
941 if (c->external_handler_uri != NULL)
942 g_free(c->external_handler_uri);
943 c->external_handler_uri = g_strdup(
944 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
945 external_handler_run(NULL, NULL, c);
947 case GDK_KEY_a: /* go one tab to the left (left hand) */
948 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
950 case GDK_KEY_s: /* go one tab to the right (left hand) */
951 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
955 /* navigate backward (left hand) */
956 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
958 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
961 /* navigate forward (left hand) */
962 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
964 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
973 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
975 if (event->type == GDK_KEY_PRESS)
977 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
979 switch (((GdkEventKey *)event)->keyval)
981 case GDK_KEY_d: /* close window (left hand) */
983 downloadmanager_delete(dm.win, NULL);
993 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
995 struct Client *c = (struct Client *)data;
999 if (key_common(widget, event, data))
1002 if (event->type == GDK_KEY_PRESS)
1004 switch (((GdkEventKey *)event)->keyval)
1006 case GDK_KEY_KP_Enter:
1007 case GDK_KEY_Return:
1008 gtk_widget_grab_focus(c->web_view);
1009 t = gtk_entry_get_text(GTK_ENTRY(c->location));
1010 if (t != NULL && t[0] == ':' && t[1] == '/')
1012 if (search_text != NULL)
1013 g_free(search_text);
1014 search_text = g_strdup(t + 2);
1017 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
1019 f = ensure_uri_scheme(t);
1020 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
1024 case GDK_KEY_Escape:
1025 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
1026 gtk_entry_set_text(GTK_ENTRY(c->location),
1027 (t == NULL ? __NAME__ : t));
1036 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
1038 struct Client *c = (struct Client *)data;
1042 if (key_common(widget, event, data))
1045 if (event->type == GDK_KEY_PRESS)
1047 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
1049 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
1050 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
1053 else if (event->type == GDK_BUTTON_RELEASE)
1055 switch (((GdkEventButton *)event)->button)
1058 if (c->hover_uri != NULL)
1060 client_new(c->hover_uri, NULL, TRUE, FALSE);
1065 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1068 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
1072 else if (event->type == GDK_SCROLL)
1074 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
1075 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
1077 gdk_event_get_scroll_deltas(event, &dx, &dy);
1078 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1080 z = dx != 0 ? global_zoom : z;
1081 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1093 GIOChannel *channel = NULL;
1094 gchar *path = NULL, *buf = NULL;
1095 gchar **tokens = NULL;
1097 keywords = g_hash_table_new(g_str_hash, g_str_equal);
1099 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1101 channel = g_io_channel_new_file(path, "r", &err);
1102 if (channel != NULL)
1104 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1105 == G_IO_STATUS_NORMAL)
1110 tokens = g_strsplit(buf, " ", 2);
1111 if (tokens[0] != NULL && tokens[1] != NULL)
1112 g_hash_table_insert(keywords, g_strdup(tokens[0]),
1113 g_strdup(tokens[1]));
1118 g_io_channel_shutdown(channel, FALSE, NULL);
1124 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1126 gboolean ret = FALSE;
1127 gchar **tokens = NULL;
1128 gchar *val = NULL, *escaped = NULL, *uri = NULL;
1130 tokens = g_strsplit(t, " ", 2);
1131 if (tokens[0] != NULL && tokens[1] != NULL)
1133 val = g_hash_table_lookup(keywords, tokens[0]);
1136 escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1137 uri = g_strdup_printf((gchar *)val, escaped);
1138 webkit_web_view_load_uri(web_view, uri);
1150 mainwindow_setup(void)
1152 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1153 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1154 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1155 gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
1157 mw.notebook = gtk_notebook_new();
1158 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1159 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), tab_pos);
1160 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1164 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1165 WebKitHitTestResult *ht, gpointer data)
1167 struct Client *c = (struct Client *)data;
1168 GSimpleAction *action = NULL;
1169 WebKitContextMenuItem *mi = NULL;
1170 const gchar *uri = NULL;
1174 if (webkit_hit_test_result_context_is_link(ht))
1175 uri = webkit_hit_test_result_get_link_uri(ht);
1176 else if (webkit_hit_test_result_context_is_image(ht))
1177 uri = webkit_hit_test_result_get_image_uri(ht);
1178 else if (webkit_hit_test_result_context_is_media(ht))
1179 uri = webkit_hit_test_result_get_media_uri(ht);
1183 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1185 if (c->external_handler_uri != NULL)
1186 g_free(c->external_handler_uri);
1187 c->external_handler_uri = g_strdup(uri);
1188 action = g_simple_action_new("external_handler", NULL);
1189 g_signal_connect(G_OBJECT(action), "activate",
1190 G_CALLBACK(external_handler_run), data);
1191 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1192 "Open with external handler",
1194 webkit_context_menu_append(menu, mi);
1195 g_object_unref(action);
1198 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1203 quit_if_nothing_active(void)
1213 gtk_widget_show_all(dm.win);
1220 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1224 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1228 client_new(uri, NULL, TRUE, TRUE);
1235 run_user_scripts(WebKitWebView *web_view)
1237 gchar *base = NULL, *path = NULL, *contents = NULL;
1238 const gchar *entry = NULL;
1239 GDir *scriptdir = NULL;
1241 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts", NULL);
1242 scriptdir = g_dir_open(base, 0, NULL);
1243 if (scriptdir != NULL)
1245 while ((entry = g_dir_read_name(scriptdir)) != NULL)
1247 path = g_build_filename(base, entry, NULL);
1248 if (g_str_has_suffix(path, ".js"))
1250 if (g_file_get_contents(path, &contents, NULL, NULL))
1252 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1258 g_dir_close(scriptdir);
1265 search(gpointer data, gint direction)
1267 struct Client *c = (struct Client *)data;
1268 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1269 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1271 if (search_text == NULL)
1277 webkit_find_controller_search(fc, search_text,
1278 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1279 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1283 webkit_find_controller_search_next(fc);
1286 webkit_find_controller_search_previous(fc);
1292 show_web_view(WebKitWebView *web_view, gpointer data)
1294 struct Client *c = (struct Client *)data;
1299 gtk_widget_show_all(mw.win);
1301 if (c->focus_new_tab)
1303 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1305 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
1307 gtk_widget_grab_focus(c->web_view);
1312 trust_user_certs(WebKitWebContext *wc)
1314 GTlsCertificate *cert;
1315 const gchar *basedir, *file, *absfile;
1318 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1319 dir = g_dir_open(basedir, 0, NULL);
1322 file = g_dir_read_name(dir);
1323 while (file != NULL)
1325 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1327 cert = g_tls_certificate_new_from_file(absfile, NULL);
1329 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1331 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1332 file = g_dir_read_name(dir);
1340 main(int argc, char **argv)
1345 gtk_init(&argc, &argv);
1346 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1347 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1349 grab_environment_configuration();
1351 while ((opt = getopt(argc, argv, "C")) != -1)
1356 cooperative_instances = FALSE;
1359 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1365 if (cooperative_instances)
1366 cooperation_setup();
1367 downloadmanager_setup();
1371 if (!cooperative_instances || cooperative_alone)
1373 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1375 webkit_web_context_set_web_extensions_directory(
1376 webkit_web_context_get_default(), c
1381 client_new(home_uri, NULL, TRUE, TRUE);
1384 for (i = optind; i < argc; i++)
1385 client_new(argv[i], NULL, TRUE, TRUE);
1388 if (!cooperative_instances || cooperative_alone)