11 #include <gdk/gdkkeysyms.h>
13 #include <webkit2/webkit2.h>
14 #include <JavaScriptCore/JavaScript.h>
17 void client_destroy(GtkWidget *, gpointer);
18 WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean,
20 WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
22 void cooperation_setup(void);
23 void changed_download_progress(GObject *, GParamSpec *, gpointer);
24 void changed_load_progress(GObject *, GParamSpec *, gpointer);
25 void changed_favicon(GObject *, GParamSpec *, gpointer);
26 void changed_title(GObject *, GParamSpec *, gpointer);
27 void changed_uri(GObject *, GParamSpec *, gpointer);
28 gboolean crashed_web_view(WebKitWebView *, gpointer);
29 gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
30 WebKitPolicyDecisionType, gpointer);
31 gboolean download_handle(WebKitDownload *, gchar *, gpointer);
32 void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
33 void downloadmanager_cancel(GtkToolButton *, gpointer);
34 gboolean downloadmanager_delete(GtkWidget *, gpointer);
35 void downloadmanager_setup(void);
36 gchar *ensure_uri_scheme(const gchar *);
37 void external_handler_run(GSimpleAction *, GVariant *, gpointer);
38 void grab_environment_configuration(void);
39 void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
40 void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
41 void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
42 gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
43 gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
44 gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
45 gboolean key_tablabel(GtkWidget *, GdkEvent *, gpointer);
46 gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
47 void mainwindow_setup(void);
48 gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
49 WebKitHitTestResult *, gpointer);
50 gboolean quit_if_nothing_active(void);
51 gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
52 void run_user_scripts(WebKitWebView *);
53 void search(gpointer, gint);
54 void show_web_view(WebKitWebView *, gpointer);
55 void trust_user_certs(WebKitWebContext *);
60 gchar *external_handler_uri;
68 gboolean focus_new_tab;
77 struct DownloadManager
85 const gchar *accepted_language[2] = { NULL, NULL };
86 gint clients = 0, downloads = 0;
87 gboolean cooperative_alone = TRUE;
88 gboolean cooperative_instances = TRUE;
89 int cooperative_pipe_fp = 0;
90 gchar *download_dir = "/var/tmp";
91 gboolean enable_console_to_stdout = FALSE;
92 gchar *fifo_suffix = "main";
93 gdouble global_zoom = 1.0;
94 gchar *history_file = NULL;
95 gchar *home_uri = "about:blank";
96 gboolean initial_wc_setup_done = FALSE;
97 gchar *search_text = NULL;
98 GtkPositionType tab_pos = GTK_POS_TOP;
99 gint tab_width_chars = 20;
100 gchar *user_agent = NULL;
104 client_destroy(GtkWidget *widget, gpointer data)
106 struct Client *c = (struct Client *)data;
109 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
110 changed_load_progress, c);
112 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
114 fprintf(stderr, __NAME__": Tab index was -1, bamboozled\n");
116 gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
121 quit_if_nothing_active();
125 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
129 WebKitWebContext *wc;
131 GtkWidget *evbox, *tabbox;
133 if (uri != NULL && cooperative_instances && !cooperative_alone)
135 f = ensure_uri_scheme(uri);
136 write(cooperative_pipe_fp, f, strlen(f));
137 write(cooperative_pipe_fp, "\n", 1);
142 c = calloc(1, sizeof(struct Client));
145 fprintf(stderr, __NAME__": fatal: calloc failed\n");
149 c->focus_new_tab = focus_tab;
151 if (related_wv == NULL)
152 c->web_view = webkit_web_view_new();
154 c->web_view = webkit_web_view_new_with_related_view(related_wv);
155 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
157 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
158 g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
159 G_CALLBACK(changed_favicon), c);
160 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
161 G_CALLBACK(changed_title), c);
162 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
163 G_CALLBACK(changed_uri), c);
164 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
165 G_CALLBACK(changed_load_progress), c);
166 g_signal_connect(G_OBJECT(c->web_view), "create",
167 G_CALLBACK(client_new_request), NULL);
168 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
169 G_CALLBACK(menu_web_view), c);
170 g_signal_connect(G_OBJECT(c->web_view), "close",
171 G_CALLBACK(client_destroy), c);
172 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
173 G_CALLBACK(decide_policy), NULL);
174 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
175 G_CALLBACK(key_web_view), c);
176 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
177 G_CALLBACK(key_web_view), c);
178 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
179 G_CALLBACK(key_web_view), c);
180 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
181 G_CALLBACK(hover_web_view), c);
182 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
183 G_CALLBACK(crashed_web_view), c);
185 if (!initial_wc_setup_done)
187 if (accepted_language[0] != NULL)
188 webkit_web_context_set_preferred_languages(wc, accepted_language);
190 g_signal_connect(G_OBJECT(wc), "download-started",
191 G_CALLBACK(download_handle_start), NULL);
193 trust_user_certs(wc);
195 webkit_web_context_set_favicon_database_directory(wc, NULL);
197 initial_wc_setup_done = TRUE;
200 if (user_agent != NULL)
201 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
202 "user-agent", user_agent, NULL);
204 if (enable_console_to_stdout)
205 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
207 webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
209 c->location = gtk_entry_new();
210 g_signal_connect(G_OBJECT(c->location), "key-press-event",
211 G_CALLBACK(key_location), c);
212 g_signal_connect(G_OBJECT(c->location), "icon-release",
213 G_CALLBACK(icon_location), c);
214 /* XXX This is a workaround. Setting this to NULL (which is done in
215 * grab_feeds_finished() if no feed has been detected) adds a little
216 * padding left of the text. Not sure why. The point of this call
217 * right here is to have that padding right from the start. This
218 * avoids a graphical artifact. */
219 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
220 GTK_ENTRY_ICON_PRIMARY,
223 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
224 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
225 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
227 c->tabicon = gtk_image_new_from_icon_name("text-html", GTK_ICON_SIZE_SMALL_TOOLBAR);
229 c->tablabel = gtk_label_new(__NAME__);
230 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
231 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), tab_width_chars);
232 gtk_widget_set_has_tooltip(c->tablabel, TRUE);
234 /* XXX I don't own a HiDPI screen, so I don't know if scale_factor
235 * does the right thing. */
236 tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
237 5 * gtk_widget_get_scale_factor(mw.win));
238 gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
239 gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
241 evbox = gtk_event_box_new();
242 gtk_container_add(GTK_CONTAINER(evbox), tabbox);
243 g_signal_connect(G_OBJECT(evbox), "button-release-event",
244 G_CALLBACK(key_tablabel), c);
246 gtk_widget_add_events(evbox, GDK_SCROLL_MASK);
247 g_signal_connect(G_OBJECT(evbox), "scroll-event",
248 G_CALLBACK(key_tablabel), c);
250 /* This only shows the event box and the label inside, nothing else.
251 * Needed because the evbox/label is "internal" to the notebook and
252 * not part of the normal "widget tree" (IIUC). */
253 gtk_widget_show_all(evbox);
255 gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox,
256 gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1);
257 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
260 show_web_view(NULL, c);
262 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
263 G_CALLBACK(show_web_view), c);
267 f = ensure_uri_scheme(uri);
268 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
274 return WEBKIT_WEB_VIEW(c->web_view);
278 client_new_request(WebKitWebView *web_view,
279 WebKitNavigationAction *navigation_action, gpointer data)
281 return client_new(NULL, web_view, FALSE, FALSE);
285 cooperation_setup(void)
288 gchar *fifofilename, *fifopath;
290 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
291 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
292 g_free(fifofilename);
294 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
295 mkfifo(fifopath, 0600);
297 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
298 if (!cooperative_pipe_fp)
300 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
304 if (write(cooperative_pipe_fp, "", 0) == -1)
306 /* Could not do an empty write to the FIFO which means there's
307 * no one listening. */
308 close(cooperative_pipe_fp);
309 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
310 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
313 cooperative_alone = FALSE;
320 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
322 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
323 WebKitURIResponse *resp;
324 GtkToolItem *tb = GTK_TOOL_ITEM(data);
327 gchar *t, *filename, *base;
329 p = webkit_download_get_estimated_progress(download);
333 resp = webkit_download_get_response(download);
334 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
336 uri = webkit_download_get_destination(download);
337 filename = g_filename_from_uri(uri, NULL, NULL);
338 if (filename == NULL)
340 /* This really should not happen because WebKit uses that URI to
341 * write to a file... */
342 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
343 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
344 webkit_uri_response_get_uri(resp), p, size_mb);
348 base = g_path_get_basename(filename);
349 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
353 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
358 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
360 struct Client *c = (struct Client *)data;
363 "a = document.querySelectorAll('"
364 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
365 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
372 " for (i = 0; i < a.length; i++)"
374 " url = encodeURIComponent(a[i].href);"
375 " if ('title' in a[i] && a[i].title != '')"
376 " title = encodeURIComponent(a[i].title);"
379 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
384 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
389 /* The page has loaded fully. We now run the short JavaScript
390 * snippet above that operates on the DOM. It tries to grab all
391 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
393 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
395 grab_feeds_finished, c);
397 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
399 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
403 changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
405 struct Client *c = (struct Client *)data;
407 int w, h, w_should, h_should;
408 GdkPixbuf *pb, *pb_scaled;
410 f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
413 gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html",
414 GTK_ICON_SIZE_SMALL_TOOLBAR);
418 w = cairo_image_surface_get_width(f);
419 h = cairo_image_surface_get_height(f);
420 pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
423 w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
424 h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
425 pb_scaled = gdk_pixbuf_scale_simple(pb, w_should, h_should,
426 GDK_INTERP_BILINEAR);
427 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon), pb_scaled);
429 g_object_unref(pb_scaled);
436 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
439 struct Client *c = (struct Client *)data;
441 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
442 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
444 u = u == NULL ? __NAME__ : u;
445 u = u[0] == 0 ? __NAME__ : u;
447 t = t == NULL ? u : t;
448 t = t[0] == 0 ? u : t;
450 gtk_label_set_text(GTK_LABEL(c->tablabel), t);
451 gtk_widget_set_tooltip_text(c->tablabel, t);
455 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
458 struct Client *c = (struct Client *)data;
461 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
463 /* When a web process crashes, we get a "notify::uri" signal, but we
464 * can no longer read a meaningful URI. It's just an empty string
465 * now. Not updating the location bar in this scenario is important,
466 * because we would override the "WEB PROCESS CRASHED" message. */
467 if (t != NULL && strlen(t) > 0)
469 gtk_entry_set_text(GTK_ENTRY(c->location), t);
471 if (history_file != NULL)
473 fp = fopen(history_file, "a");
476 fprintf(fp, "%s\n", t);
480 perror(__NAME__": Error opening history file");
486 crashed_web_view(WebKitWebView *web_view, gpointer data)
489 struct Client *c = (struct Client *)data;
491 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
492 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
493 gtk_entry_set_text(GTK_ENTRY(c->location), t);
500 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
501 WebKitPolicyDecisionType type, gpointer data)
503 WebKitResponsePolicyDecision *r;
507 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
508 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
509 if (!webkit_response_policy_decision_is_mime_type_supported(r))
510 webkit_policy_decision_download(decision);
512 webkit_policy_decision_use(decision);
515 /* Use whatever default there is. */
522 download_handle_finished(WebKitDownload *download, gpointer data)
528 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
531 g_signal_connect(G_OBJECT(download), "decide-destination",
532 G_CALLBACK(download_handle), data);
536 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
538 gchar *sug_clean, *path, *path2 = NULL, *uri;
543 sug_clean = g_strdup(suggested_filename);
544 for (i = 0; i < strlen(sug_clean); i++)
545 if (sug_clean[i] == G_DIR_SEPARATOR)
548 path = g_build_filename(download_dir, sug_clean, NULL);
549 path2 = g_strdup(path);
550 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
554 path2 = g_strdup_printf("%s.%d", path, suffix);
560 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
561 webkit_download_cancel(download);
565 uri = g_filename_to_uri(path2, NULL, NULL);
566 webkit_download_set_destination(download, uri);
569 tb = gtk_tool_button_new(NULL, NULL);
570 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
571 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
572 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
573 gtk_widget_show_all(dm.win);
575 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
576 G_CALLBACK(changed_download_progress), tb);
579 g_signal_connect(G_OBJECT(download), "finished",
580 G_CALLBACK(download_handle_finished), NULL);
582 g_object_ref(download);
583 g_signal_connect(G_OBJECT(tb), "clicked",
584 G_CALLBACK(downloadmanager_cancel), download);
591 /* Propagate -- to whom it may concern. */
596 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
598 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
600 webkit_download_cancel(download);
601 g_object_unref(download);
603 gtk_widget_destroy(GTK_WIDGET(tb));
607 downloadmanager_delete(GtkWidget *obj, gpointer data)
609 if (!quit_if_nothing_active())
610 gtk_widget_hide(dm.win);
616 downloadmanager_setup(void)
618 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
619 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
620 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
621 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
622 g_signal_connect(G_OBJECT(dm.win), "delete-event",
623 G_CALLBACK(downloadmanager_delete), NULL);
624 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
625 G_CALLBACK(key_downloadmanager), NULL);
627 dm.toolbar = gtk_toolbar_new();
628 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
629 GTK_ORIENTATION_VERTICAL);
630 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
631 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
633 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
634 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
635 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
636 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
638 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
642 ensure_uri_scheme(const gchar *t)
646 f = g_ascii_strdown(t, -1);
647 if (!g_str_has_prefix(f, "http:") &&
648 !g_str_has_prefix(f, "https:") &&
649 !g_str_has_prefix(f, "file:") &&
650 !g_str_has_prefix(f, "about:") &&
651 !g_str_has_prefix(f, "data:"))
654 fabs = realpath(t, NULL);
657 f = g_strdup_printf("file://%s", fabs);
661 f = g_strdup_printf("http://%s", t);
669 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
671 struct Client *c = (struct Client *)data;
672 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
679 argv[2] = c->external_handler_uri;
680 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
683 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
688 g_spawn_close_pid(pid);
692 grab_environment_configuration(void)
696 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
698 accepted_language[0] = g_strdup(e);
700 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
702 download_dir = g_strdup(e);
704 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
706 enable_console_to_stdout = TRUE;
708 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
710 fifo_suffix = g_strdup(e);
712 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
714 history_file = g_strdup(e);
716 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
718 home_uri = g_strdup(e);
720 e = g_getenv(__NAME_UPPERCASE__"_TAB_POS");
723 if (strcmp(e, "top") == 0)
724 tab_pos = GTK_POS_TOP;
725 if (strcmp(e, "right") == 0)
726 tab_pos = GTK_POS_RIGHT;
727 if (strcmp(e, "bottom") == 0)
728 tab_pos = GTK_POS_BOTTOM;
729 if (strcmp(e, "left") == 0)
730 tab_pos = GTK_POS_LEFT;
733 e = g_getenv(__NAME_UPPERCASE__"_TAB_WIDTH_CHARS");
735 tab_width_chars = atoi(e);
737 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
739 user_agent = g_strdup(e);
741 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
743 global_zoom = atof(e);
747 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
749 struct Client *c = (struct Client *)data;
750 WebKitJavascriptResult *js_result;
752 JSCException *exception;
756 g_free(c->feed_html);
759 /* This was taken almost verbatim from the example in WebKit's
762 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
764 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
768 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
773 value = webkit_javascript_result_get_js_value(js_result);
774 if (jsc_value_is_string(value))
776 str_value = jsc_value_to_string(value);
777 exception = jsc_context_get_exception(jsc_value_get_context(value));
778 if (exception != NULL)
780 fprintf(stderr, __NAME__": Error running javascript: %s\n",
781 jsc_exception_get_message(exception));
784 c->feed_html = str_value;
786 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
787 GTK_ENTRY_ICON_PRIMARY,
788 "application-rss+xml-symbolic");
789 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
790 GTK_ENTRY_ICON_PRIMARY,
795 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
796 GTK_ENTRY_ICON_PRIMARY,
800 webkit_javascript_result_unref(js_result);
804 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
807 struct Client *c = (struct Client *)data;
809 if (!gtk_widget_is_focus(c->location))
811 if (webkit_hit_test_result_context_is_link(ht))
813 gtk_entry_set_text(GTK_ENTRY(c->location),
814 webkit_hit_test_result_get_link_uri(ht));
816 if (c->hover_uri != NULL)
817 g_free(c->hover_uri);
818 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
822 gtk_entry_set_text(GTK_ENTRY(c->location),
823 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
825 if (c->hover_uri != NULL)
826 g_free(c->hover_uri);
833 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
836 struct Client *c = (struct Client *)data;
838 gchar *data_template =
843 " <meta charset=\"UTF-8\">"
844 " <title>Feeds</title>"
847 " <p>Feeds found on this page:</p>"
854 if (c->feed_html != NULL)
856 /* What we're actually trying to do is show a simple HTML page
857 * that lists all the feeds on the current page. The function
858 * webkit_web_view_load_html() looks like the proper way to do
859 * that. Sad thing is, it doesn't create a history entry, but
860 * instead simply replaces the content of the current page. This
861 * is not what we want.
863 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
864 * mechanism to show my custom HTML snippet *and* create a
867 * [0]: https://tools.ietf.org/html/rfc2397
868 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
869 d = g_strdup_printf(data_template, c->feed_html);
870 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
876 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
878 struct Client *c = (struct Client *)data;
879 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
882 if (event->type == GDK_KEY_PRESS)
884 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
886 switch (((GdkEventKey *)event)->keyval)
888 case GDK_KEY_q: /* close window (left hand) */
889 client_destroy(NULL, c);
891 case GDK_KEY_w: /* home (left hand) */
892 f = ensure_uri_scheme(home_uri);
893 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
896 case GDK_KEY_e: /* new tab (left hand) */
897 f = ensure_uri_scheme(home_uri);
898 client_new(f, NULL, TRUE, TRUE);
901 case GDK_KEY_r: /* reload (left hand) */
902 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
905 case GDK_KEY_d: /* download manager (left hand) */
906 gtk_widget_show_all(dm.win);
908 case GDK_KEY_2: /* search forward (left hand) */
909 case GDK_KEY_n: /* search forward (maybe both hands) */
912 case GDK_KEY_3: /* search backward (left hand) */
915 case GDK_KEY_l: /* location (BOTH hands) */
916 gtk_widget_grab_focus(c->location);
918 case GDK_KEY_k: /* initiate search (BOTH hands) */
919 gtk_widget_grab_focus(c->location);
920 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
921 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
923 case GDK_KEY_c: /* reload trusted certs (left hand) */
924 trust_user_certs(wc);
926 case GDK_KEY_x: /* launch external handler (left hand) */
927 if (c->external_handler_uri != NULL)
928 g_free(c->external_handler_uri);
929 c->external_handler_uri = g_strdup(
930 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
931 external_handler_run(NULL, NULL, c);
933 case GDK_KEY_a: /* go one tab to the left (left hand) */
934 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
936 case GDK_KEY_s: /* go one tab to the right (left hand) */
937 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
941 /* navigate backward (left hand) */
942 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
944 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
947 /* navigate forward (left hand) */
948 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
950 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
959 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
961 if (event->type == GDK_KEY_PRESS)
963 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
965 switch (((GdkEventKey *)event)->keyval)
967 case GDK_KEY_d: /* close window (left hand) */
969 downloadmanager_delete(dm.win, NULL);
979 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
981 struct Client *c = (struct Client *)data;
985 if (key_common(widget, event, data))
988 if (event->type == GDK_KEY_PRESS)
990 switch (((GdkEventKey *)event)->keyval)
992 case GDK_KEY_KP_Enter:
994 gtk_widget_grab_focus(c->web_view);
995 t = gtk_entry_get_text(GTK_ENTRY(c->location));
996 if (t != NULL && t[0] == ':' && t[1] == '/')
998 if (search_text != NULL)
1000 search_text = g_strdup(t + 2);
1005 f = ensure_uri_scheme(t);
1006 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
1010 case GDK_KEY_Escape:
1011 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
1012 gtk_entry_set_text(GTK_ENTRY(c->location),
1013 (t == NULL ? __NAME__ : t));
1022 key_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
1024 GdkScrollDirection direction;
1026 if (event->type == GDK_BUTTON_RELEASE)
1028 switch (((GdkEventButton *)event)->button)
1031 client_destroy(NULL, data);
1035 else if (event->type == GDK_SCROLL)
1037 gdk_event_get_scroll_direction(event, &direction);
1041 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
1043 case GDK_SCROLL_DOWN:
1044 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
1055 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
1057 struct Client *c = (struct Client *)data;
1061 if (key_common(widget, event, data))
1064 if (event->type == GDK_KEY_PRESS)
1066 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
1068 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
1069 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
1072 else if (event->type == GDK_BUTTON_RELEASE)
1074 switch (((GdkEventButton *)event)->button)
1077 if (c->hover_uri != NULL)
1079 client_new(c->hover_uri, NULL, TRUE, FALSE);
1084 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1087 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
1091 else if (event->type == GDK_SCROLL)
1093 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
1094 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
1096 gdk_event_get_scroll_deltas(event, &dx, &dy);
1097 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1099 z = dx != 0 ? global_zoom : z;
1100 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1109 mainwindow_setup(void)
1111 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1112 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1113 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1114 gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
1116 mw.notebook = gtk_notebook_new();
1117 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1118 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), tab_pos);
1119 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1123 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1124 WebKitHitTestResult *ht, gpointer data)
1126 struct Client *c = (struct Client *)data;
1127 GSimpleAction *action = NULL;
1128 WebKitContextMenuItem *mi = NULL;
1129 const gchar *uri = NULL;
1133 if (webkit_hit_test_result_context_is_link(ht))
1134 uri = webkit_hit_test_result_get_link_uri(ht);
1135 else if (webkit_hit_test_result_context_is_image(ht))
1136 uri = webkit_hit_test_result_get_image_uri(ht);
1137 else if (webkit_hit_test_result_context_is_media(ht))
1138 uri = webkit_hit_test_result_get_media_uri(ht);
1142 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1144 if (c->external_handler_uri != NULL)
1145 g_free(c->external_handler_uri);
1146 c->external_handler_uri = g_strdup(uri);
1147 action = g_simple_action_new("external_handler", NULL);
1148 g_signal_connect(G_OBJECT(action), "activate",
1149 G_CALLBACK(external_handler_run), data);
1150 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1151 "Open with external handler",
1153 webkit_context_menu_append(menu, mi);
1154 g_object_unref(action);
1157 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1162 quit_if_nothing_active(void)
1172 gtk_widget_show_all(dm.win);
1179 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1183 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1187 client_new(uri, NULL, TRUE, TRUE);
1194 run_user_scripts(WebKitWebView *web_view)
1196 gchar *base = NULL, *path = NULL, *contents = NULL;
1197 const gchar *entry = NULL;
1198 GDir *scriptdir = NULL;
1200 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts", NULL);
1201 scriptdir = g_dir_open(base, 0, NULL);
1202 if (scriptdir != NULL)
1204 while ((entry = g_dir_read_name(scriptdir)) != NULL)
1206 path = g_build_filename(base, entry, NULL);
1207 if (g_str_has_suffix(path, ".js"))
1209 if (g_file_get_contents(path, &contents, NULL, NULL))
1211 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1217 g_dir_close(scriptdir);
1224 search(gpointer data, gint direction)
1226 struct Client *c = (struct Client *)data;
1227 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1228 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1230 if (search_text == NULL)
1236 webkit_find_controller_search(fc, search_text,
1237 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1238 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1242 webkit_find_controller_search_next(fc);
1245 webkit_find_controller_search_previous(fc);
1251 show_web_view(WebKitWebView *web_view, gpointer data)
1253 struct Client *c = (struct Client *)data;
1258 gtk_widget_show_all(mw.win);
1260 if (c->focus_new_tab)
1262 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1264 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
1266 gtk_widget_grab_focus(c->web_view);
1271 trust_user_certs(WebKitWebContext *wc)
1273 GTlsCertificate *cert;
1274 const gchar *basedir, *file, *absfile;
1277 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1278 dir = g_dir_open(basedir, 0, NULL);
1281 file = g_dir_read_name(dir);
1282 while (file != NULL)
1284 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1286 cert = g_tls_certificate_new_from_file(absfile, NULL);
1288 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1290 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1291 file = g_dir_read_name(dir);
1299 main(int argc, char **argv)
1304 gtk_init(&argc, &argv);
1305 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1306 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1308 grab_environment_configuration();
1310 while ((opt = getopt(argc, argv, "C")) != -1)
1315 cooperative_instances = FALSE;
1318 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1323 if (cooperative_instances)
1324 cooperation_setup();
1325 downloadmanager_setup();
1329 if (!cooperative_instances || cooperative_alone)
1331 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1333 webkit_web_context_set_web_extensions_directory(
1334 webkit_web_context_get_default(), c
1339 client_new(home_uri, NULL, TRUE, TRUE);
1342 for (i = optind; i < argc; i++)
1343 client_new(argv[i], NULL, TRUE, TRUE);
1346 if (!cooperative_instances || cooperative_alone)