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 /* XXX I don't own a HiDPI screen, so I don't know if scale_factor
249 * does the right thing. */
250 tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
251 5 * gtk_widget_get_scale_factor(mw.win));
252 gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
253 gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
255 evbox = gtk_event_box_new();
256 gtk_container_add(GTK_CONTAINER(evbox), tabbox);
257 g_signal_connect(G_OBJECT(evbox), "button-release-event",
258 G_CALLBACK(button_tablabel), c);
260 /* This only shows the event box and the label inside, nothing else.
261 * Needed because the evbox/label is "internal" to the notebook and
262 * not part of the normal "widget tree" (IIUC). */
263 gtk_widget_show_all(evbox);
265 gtk_notebook_append_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox);
266 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
269 show_web_view(NULL, c);
271 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
272 G_CALLBACK(show_web_view), c);
276 f = ensure_uri_scheme(uri);
277 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
283 return WEBKIT_WEB_VIEW(c->web_view);
287 client_new_request(WebKitWebView *web_view,
288 WebKitNavigationAction *navigation_action, gpointer data)
290 return client_new(NULL, web_view, FALSE);
294 cooperation_setup(void)
297 gchar *fifofilename, *fifopath;
299 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
300 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
301 g_free(fifofilename);
303 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
304 mkfifo(fifopath, 0600);
306 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
307 if (!cooperative_pipe_fp)
309 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
313 if (write(cooperative_pipe_fp, "", 0) == -1)
315 /* Could not do an empty write to the FIFO which means there's
316 * no one listening. */
317 close(cooperative_pipe_fp);
318 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
319 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
322 cooperative_alone = FALSE;
329 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
331 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
332 WebKitURIResponse *resp;
333 GtkToolItem *tb = GTK_TOOL_ITEM(data);
336 gchar *t, *filename, *base;
338 p = webkit_download_get_estimated_progress(download);
342 resp = webkit_download_get_response(download);
343 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
345 uri = webkit_download_get_destination(download);
346 filename = g_filename_from_uri(uri, NULL, NULL);
347 if (filename == NULL)
349 /* This really should not happen because WebKit uses that URI to
350 * write to a file... */
351 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
352 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
353 webkit_uri_response_get_uri(resp), p, size_mb);
357 base = g_path_get_basename(filename);
358 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
362 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
367 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
369 struct Client *c = (struct Client *)data;
372 "a = document.querySelectorAll('"
373 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
374 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
381 " for (i = 0; i < a.length; i++)"
383 " url = encodeURIComponent(a[i].href);"
384 " if ('title' in a[i] && a[i].title != '')"
385 " title = encodeURIComponent(a[i].title);"
388 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
393 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
398 /* The page has loaded fully. We now run the short JavaScript
399 * snippet above that operates on the DOM. It tries to grab all
400 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
402 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
404 grab_feeds_finished, c);
406 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
408 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
412 changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
414 struct Client *c = (struct Client *)data;
416 int w, h, w_should, h_should;
417 GdkPixbuf *pb, *pb_scaled;
419 f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
422 w = cairo_image_surface_get_width(f);
423 h = cairo_image_surface_get_height(f);
424 pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
427 w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
428 h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
429 pb_scaled = gdk_pixbuf_scale_simple(pb, w_should, h_should,
430 GDK_INTERP_BILINEAR);
431 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon), pb_scaled);
433 g_object_unref(pb_scaled);
440 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
443 struct Client *c = (struct Client *)data;
445 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
446 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
448 u = u == NULL ? __NAME__ : u;
449 u = u[0] == 0 ? __NAME__ : u;
451 t = t == NULL ? u : t;
452 t = t[0] == 0 ? u : t;
454 gtk_label_set_text(GTK_LABEL(c->tablabel), t);
455 mainwindow_title(-1);
459 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
462 struct Client *c = (struct Client *)data;
465 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
467 /* When a web process crashes, we get a "notify::uri" signal, but we
468 * can no longer read a meaningful URI. It's just an empty string
469 * now. Not updating the location bar in this scenario is important,
470 * because we would override the "WEB PROCESS CRASHED" message. */
471 if (t != NULL && strlen(t) > 0)
473 gtk_entry_set_text(GTK_ENTRY(c->location), t);
475 if (history_file != NULL)
477 fp = fopen(history_file, "a");
480 fprintf(fp, "%s\n", t);
484 perror(__NAME__": Error opening history file");
490 crashed_web_view(WebKitWebView *web_view, gpointer data)
493 struct Client *c = (struct Client *)data;
495 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
496 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
497 gtk_entry_set_text(GTK_ENTRY(c->location), t);
504 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
505 WebKitPolicyDecisionType type, gpointer data)
507 WebKitResponsePolicyDecision *r;
511 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
512 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
513 if (!webkit_response_policy_decision_is_mime_type_supported(r))
514 webkit_policy_decision_download(decision);
516 webkit_policy_decision_use(decision);
519 /* Use whatever default there is. */
526 download_handle_finished(WebKitDownload *download, gpointer data)
532 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
535 g_signal_connect(G_OBJECT(download), "decide-destination",
536 G_CALLBACK(download_handle), data);
540 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
542 gchar *sug_clean, *path, *path2 = NULL, *uri;
547 sug_clean = g_strdup(suggested_filename);
548 for (i = 0; i < strlen(sug_clean); i++)
549 if (sug_clean[i] == G_DIR_SEPARATOR)
552 path = g_build_filename(download_dir, sug_clean, NULL);
553 path2 = g_strdup(path);
554 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
558 path2 = g_strdup_printf("%s.%d", path, suffix);
564 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
565 webkit_download_cancel(download);
569 uri = g_filename_to_uri(path2, NULL, NULL);
570 webkit_download_set_destination(download, uri);
573 tb = gtk_tool_button_new(NULL, NULL);
574 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
575 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
576 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
577 gtk_widget_show_all(dm.win);
579 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
580 G_CALLBACK(changed_download_progress), tb);
583 g_signal_connect(G_OBJECT(download), "finished",
584 G_CALLBACK(download_handle_finished), NULL);
586 g_object_ref(download);
587 g_signal_connect(G_OBJECT(tb), "clicked",
588 G_CALLBACK(downloadmanager_cancel), download);
595 /* Propagate -- to whom it may concern. */
600 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
602 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
604 webkit_download_cancel(download);
605 g_object_unref(download);
607 gtk_widget_destroy(GTK_WIDGET(tb));
611 downloadmanager_delete(GtkWidget *obj, gpointer data)
613 if (!quit_if_nothing_active())
614 gtk_widget_hide(dm.win);
620 downloadmanager_setup(void)
622 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
623 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
624 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
625 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
626 g_signal_connect(G_OBJECT(dm.win), "delete-event",
627 G_CALLBACK(downloadmanager_delete), NULL);
628 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
629 G_CALLBACK(key_downloadmanager), NULL);
631 dm.toolbar = gtk_toolbar_new();
632 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
633 GTK_ORIENTATION_VERTICAL);
634 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
635 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
637 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
638 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
639 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
640 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
642 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
646 ensure_uri_scheme(const gchar *t)
650 f = g_ascii_strdown(t, -1);
651 if (!g_str_has_prefix(f, "http:") &&
652 !g_str_has_prefix(f, "https:") &&
653 !g_str_has_prefix(f, "file:") &&
654 !g_str_has_prefix(f, "about:") &&
655 !g_str_has_prefix(f, "data:"))
658 fabs = realpath(t, NULL);
661 f = g_strdup_printf("file://%s", fabs);
665 f = g_strdup_printf("http://%s", t);
673 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
675 struct Client *c = (struct Client *)data;
676 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
683 argv[2] = c->external_handler_uri;
684 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
687 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
692 g_spawn_close_pid(pid);
696 grab_environment_configuration(void)
700 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
702 accepted_language[0] = g_strdup(e);
704 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
706 download_dir = g_strdup(e);
708 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
710 enable_console_to_stdout = TRUE;
712 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
714 fifo_suffix = g_strdup(e);
716 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
718 history_file = g_strdup(e);
720 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
722 home_uri = g_strdup(e);
724 e = g_getenv(__NAME_UPPERCASE__"_TAB_POS");
727 if (strcmp(e, "top") == 0)
728 tab_pos = GTK_POS_TOP;
729 if (strcmp(e, "right") == 0)
730 tab_pos = GTK_POS_RIGHT;
731 if (strcmp(e, "bottom") == 0)
732 tab_pos = GTK_POS_BOTTOM;
733 if (strcmp(e, "left") == 0)
734 tab_pos = GTK_POS_LEFT;
737 e = g_getenv(__NAME_UPPERCASE__"_TAB_WIDTH_CHARS");
739 tab_width_chars = atoi(e);
741 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
743 user_agent = g_strdup(e);
745 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
747 global_zoom = atof(e);
751 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
753 struct Client *c = (struct Client *)data;
754 WebKitJavascriptResult *js_result;
756 JSCException *exception;
760 g_free(c->feed_html);
763 /* This was taken almost verbatim from the example in WebKit's
766 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
768 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
772 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
777 value = webkit_javascript_result_get_js_value(js_result);
778 if (jsc_value_is_string(value))
780 str_value = jsc_value_to_string(value);
781 exception = jsc_context_get_exception(jsc_value_get_context(value));
782 if (exception != NULL)
784 fprintf(stderr, __NAME__": Error running javascript: %s\n",
785 jsc_exception_get_message(exception));
788 c->feed_html = str_value;
790 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
791 GTK_ENTRY_ICON_PRIMARY,
792 "application-rss+xml-symbolic");
793 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
794 GTK_ENTRY_ICON_PRIMARY,
799 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
800 GTK_ENTRY_ICON_PRIMARY,
804 webkit_javascript_result_unref(js_result);
808 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
811 struct Client *c = (struct Client *)data;
813 if (!gtk_widget_is_focus(c->location))
815 if (webkit_hit_test_result_context_is_link(ht))
817 gtk_entry_set_text(GTK_ENTRY(c->location),
818 webkit_hit_test_result_get_link_uri(ht));
820 if (c->hover_uri != NULL)
821 g_free(c->hover_uri);
822 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
826 gtk_entry_set_text(GTK_ENTRY(c->location),
827 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
829 if (c->hover_uri != NULL)
830 g_free(c->hover_uri);
837 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
840 struct Client *c = (struct Client *)data;
842 gchar *data_template =
847 " <meta charset=\"UTF-8\">"
848 " <title>Feeds</title>"
851 " <p>Feeds found on this page:</p>"
858 if (c->feed_html != NULL)
860 /* What we're actually trying to do is show a simple HTML page
861 * that lists all the feeds on the current page. The function
862 * webkit_web_view_load_html() looks like the proper way to do
863 * that. Sad thing is, it doesn't create a history entry, but
864 * instead simply replaces the content of the current page. This
865 * is not what we want.
867 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
868 * mechanism to show my custom HTML snippet *and* create a
871 * [0]: https://tools.ietf.org/html/rfc2397
872 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
873 d = g_strdup_printf(data_template, c->feed_html);
874 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
880 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
882 struct Client *c = (struct Client *)data;
883 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
886 if (event->type == GDK_KEY_PRESS)
888 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
890 switch (((GdkEventKey *)event)->keyval)
892 case GDK_KEY_q: /* close window (left hand) */
893 client_destroy(NULL, c);
895 case GDK_KEY_w: /* home (left hand) */
896 f = ensure_uri_scheme(home_uri);
897 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
900 case GDK_KEY_e: /* new tab (left hand) */
901 f = ensure_uri_scheme(home_uri);
902 client_new(f, NULL, TRUE);
905 case GDK_KEY_r: /* reload (left hand) */
906 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
909 case GDK_KEY_d: /* download manager (left hand) */
910 gtk_widget_show_all(dm.win);
912 case GDK_KEY_2: /* search forward (left hand) */
913 case GDK_KEY_n: /* search forward (maybe both hands) */
916 case GDK_KEY_3: /* search backward (left hand) */
919 case GDK_KEY_l: /* location (BOTH hands) */
920 gtk_widget_grab_focus(c->location);
922 case GDK_KEY_k: /* initiate search (BOTH hands) */
923 gtk_widget_grab_focus(c->location);
924 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
925 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
927 case GDK_KEY_c: /* reload trusted certs (left hand) */
928 trust_user_certs(wc);
930 case GDK_KEY_x: /* launch external handler (left hand) */
931 if (c->external_handler_uri != NULL)
932 g_free(c->external_handler_uri);
933 c->external_handler_uri = g_strdup(
934 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
935 external_handler_run(NULL, NULL, c);
937 case GDK_KEY_a: /* go one tab to the left (left hand) */
938 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
940 case GDK_KEY_s: /* go one tab to the right (left hand) */
941 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
945 /* navigate backward (left hand) */
946 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
948 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
951 /* navigate forward (left hand) */
952 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
954 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
963 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
965 if (event->type == GDK_KEY_PRESS)
967 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
969 switch (((GdkEventKey *)event)->keyval)
971 case GDK_KEY_d: /* close window (left hand) */
973 downloadmanager_delete(dm.win, NULL);
983 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
985 struct Client *c = (struct Client *)data;
989 if (key_common(widget, event, data))
992 if (event->type == GDK_KEY_PRESS)
994 switch (((GdkEventKey *)event)->keyval)
996 case GDK_KEY_KP_Enter:
998 gtk_widget_grab_focus(c->web_view);
999 t = gtk_entry_get_text(GTK_ENTRY(c->location));
1000 if (t != NULL && t[0] == ':' && t[1] == '/')
1002 if (search_text != NULL)
1003 g_free(search_text);
1004 search_text = g_strdup(t + 2);
1007 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
1009 f = ensure_uri_scheme(t);
1010 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
1014 case GDK_KEY_Escape:
1015 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
1016 gtk_entry_set_text(GTK_ENTRY(c->location),
1017 (t == NULL ? __NAME__ : t));
1026 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
1028 struct Client *c = (struct Client *)data;
1032 if (key_common(widget, event, data))
1035 if (event->type == GDK_KEY_PRESS)
1037 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
1039 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
1040 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
1043 else if (event->type == GDK_BUTTON_RELEASE)
1045 switch (((GdkEventButton *)event)->button)
1048 if (c->hover_uri != NULL)
1050 client_new(c->hover_uri, NULL, TRUE);
1055 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1058 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
1062 else if (event->type == GDK_SCROLL)
1064 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
1065 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
1067 gdk_event_get_scroll_deltas(event, &dx, &dy);
1068 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1070 z = dx != 0 ? global_zoom : z;
1071 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1083 GIOChannel *channel = NULL;
1084 gchar *path = NULL, *buf = NULL;
1085 gchar **tokens = NULL;
1087 keywords = g_hash_table_new(g_str_hash, g_str_equal);
1089 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1091 channel = g_io_channel_new_file(path, "r", &err);
1092 if (channel != NULL)
1094 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1095 == G_IO_STATUS_NORMAL)
1100 tokens = g_strsplit(buf, " ", 2);
1101 if (tokens[0] != NULL && tokens[1] != NULL)
1102 g_hash_table_insert(keywords, g_strdup(tokens[0]),
1103 g_strdup(tokens[1]));
1108 g_io_channel_shutdown(channel, FALSE, NULL);
1114 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1116 gboolean ret = FALSE;
1117 gchar **tokens = NULL;
1118 gchar *val = NULL, *escaped = NULL, *uri = NULL;
1120 tokens = g_strsplit(t, " ", 2);
1121 if (tokens[0] != NULL && tokens[1] != NULL)
1123 val = g_hash_table_lookup(keywords, tokens[0]);
1126 escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1127 uri = g_strdup_printf((gchar *)val, escaped);
1128 webkit_web_view_load_uri(web_view, uri);
1140 mainwindow_setup(void)
1142 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1143 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1144 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1145 gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
1147 mw.notebook = gtk_notebook_new();
1148 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1149 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), tab_pos);
1150 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1151 g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
1152 G_CALLBACK(mainwindow_title_before), NULL);
1156 mainwindow_title_before(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
1158 mainwindow_title(idx);
1162 mainwindow_title(gint idx)
1164 GtkWidget *child, *tabbox, *evbox, *label;
1165 GList *tabbox_children, *last;
1170 idx = gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook));
1175 child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
1179 evbox = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
1180 tabbox = gtk_bin_get_child(GTK_BIN(evbox));
1181 tabbox_children = gtk_container_get_children(GTK_CONTAINER(tabbox));
1182 last = g_list_last(tabbox_children);
1184 text = gtk_label_get_text(GTK_LABEL(label));
1185 gtk_window_set_title(GTK_WINDOW(mw.win), text);
1186 g_list_free(tabbox_children);
1190 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1191 WebKitHitTestResult *ht, gpointer data)
1193 struct Client *c = (struct Client *)data;
1194 GSimpleAction *action = NULL;
1195 WebKitContextMenuItem *mi = NULL;
1196 const gchar *uri = NULL;
1200 if (webkit_hit_test_result_context_is_link(ht))
1201 uri = webkit_hit_test_result_get_link_uri(ht);
1202 else if (webkit_hit_test_result_context_is_image(ht))
1203 uri = webkit_hit_test_result_get_image_uri(ht);
1204 else if (webkit_hit_test_result_context_is_media(ht))
1205 uri = webkit_hit_test_result_get_media_uri(ht);
1209 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1211 if (c->external_handler_uri != NULL)
1212 g_free(c->external_handler_uri);
1213 c->external_handler_uri = g_strdup(uri);
1214 action = g_simple_action_new("external_handler", NULL);
1215 g_signal_connect(G_OBJECT(action), "activate",
1216 G_CALLBACK(external_handler_run), data);
1217 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1218 "Open with external handler",
1220 webkit_context_menu_append(menu, mi);
1221 g_object_unref(action);
1224 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1229 quit_if_nothing_active(void)
1239 gtk_widget_show_all(dm.win);
1246 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1250 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1254 client_new(uri, NULL, TRUE);
1261 run_user_scripts(WebKitWebView *web_view)
1263 gchar *base = NULL, *path = NULL, *contents = NULL;
1264 const gchar *entry = NULL;
1265 GDir *scriptdir = NULL;
1267 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts", NULL);
1268 scriptdir = g_dir_open(base, 0, NULL);
1269 if (scriptdir != NULL)
1271 while ((entry = g_dir_read_name(scriptdir)) != NULL)
1273 path = g_build_filename(base, entry, NULL);
1274 if (g_str_has_suffix(path, ".js"))
1276 if (g_file_get_contents(path, &contents, NULL, NULL))
1278 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1284 g_dir_close(scriptdir);
1291 search(gpointer data, gint direction)
1293 struct Client *c = (struct Client *)data;
1294 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1295 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1297 if (search_text == NULL)
1303 webkit_find_controller_search(fc, search_text,
1304 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1305 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1309 webkit_find_controller_search_next(fc);
1312 webkit_find_controller_search_previous(fc);
1318 show_web_view(WebKitWebView *web_view, gpointer data)
1320 struct Client *c = (struct Client *)data;
1325 gtk_widget_show_all(mw.win);
1327 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1329 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
1331 gtk_widget_grab_focus(c->web_view);
1335 trust_user_certs(WebKitWebContext *wc)
1337 GTlsCertificate *cert;
1338 const gchar *basedir, *file, *absfile;
1341 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1342 dir = g_dir_open(basedir, 0, NULL);
1345 file = g_dir_read_name(dir);
1346 while (file != NULL)
1348 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1350 cert = g_tls_certificate_new_from_file(absfile, NULL);
1352 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1354 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1355 file = g_dir_read_name(dir);
1363 main(int argc, char **argv)
1368 gtk_init(&argc, &argv);
1369 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1370 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1372 grab_environment_configuration();
1374 while ((opt = getopt(argc, argv, "C")) != -1)
1379 cooperative_instances = FALSE;
1382 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1388 if (cooperative_instances)
1389 cooperation_setup();
1390 downloadmanager_setup();
1394 if (!cooperative_instances || cooperative_alone)
1396 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1398 webkit_web_context_set_web_extensions_directory(
1399 webkit_web_context_get_default(), c
1404 client_new(home_uri, NULL, TRUE);
1407 for (i = optind; i < argc; i++)
1408 client_new(argv[i], NULL, TRUE);
1411 if (!cooperative_instances || cooperative_alone)