11 #include <gdk/gdkkeysyms.h>
13 #include <webkit2/webkit2.h>
14 #include <JavaScriptCore/JavaScript.h>
17 void client_destroy(GtkWidget *, gpointer);
18 WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean,
20 WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
22 void cooperation_setup(void);
23 void changed_download_progress(GObject *, GParamSpec *, gpointer);
24 void changed_load_progress(GObject *, GParamSpec *, gpointer);
25 void changed_favicon(GObject *, GParamSpec *, gpointer);
26 void changed_title(GObject *, GParamSpec *, gpointer);
27 void changed_uri(GObject *, GParamSpec *, gpointer);
28 gboolean crashed_web_view(WebKitWebView *, gpointer);
29 gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
30 WebKitPolicyDecisionType, gpointer);
31 gboolean download_handle(WebKitDownload *, gchar *, gpointer);
32 void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
33 void downloadmanager_cancel(GtkToolButton *, gpointer);
34 gboolean downloadmanager_delete(GtkWidget *, gpointer);
35 void downloadmanager_setup(void);
36 gchar *ensure_uri_scheme(const gchar *);
37 void grab_environment_configuration(void);
38 void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
39 void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
40 void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
41 gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
42 gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
43 gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
44 gboolean key_tablabel(GtkWidget *, GdkEvent *, gpointer);
45 gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
46 void mainwindow_setup(void);
47 gboolean quit_if_nothing_active(void);
48 gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
49 void run_user_scripts(WebKitWebView *);
50 void search(gpointer, gint);
51 void show_web_view(WebKitWebView *, gpointer);
52 void trust_user_certs(WebKitWebContext *);
57 gchar *external_handler_uri;
65 gboolean focus_new_tab;
74 struct DownloadManager
82 const gchar *accepted_language[2] = { NULL, NULL };
83 gint clients = 0, downloads = 0;
84 gboolean cooperative_alone = TRUE;
85 gboolean cooperative_instances = TRUE;
86 int cooperative_pipe_fp = 0;
87 gchar *download_dir = "/var/tmp";
88 gboolean enable_console_to_stdout = FALSE;
89 gchar *fifo_suffix = "main";
90 gdouble global_zoom = 1.0;
91 gchar *history_file = NULL;
92 gchar *home_uri = "about:blank";
93 gboolean initial_wc_setup_done = FALSE;
94 gchar *search_text = NULL;
95 GtkPositionType tab_pos = GTK_POS_TOP;
96 gint tab_width_chars = 20;
97 gchar *user_agent = NULL;
101 client_destroy(GtkWidget *widget, gpointer data)
103 struct Client *c = (struct Client *)data;
106 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
107 changed_load_progress, c);
109 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
111 fprintf(stderr, __NAME__": Tab index was -1, bamboozled\n");
113 gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
118 quit_if_nothing_active();
122 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show,
126 WebKitWebContext *wc;
128 GtkWidget *evbox, *tabbox;
130 if (uri != NULL && cooperative_instances && !cooperative_alone)
132 f = ensure_uri_scheme(uri);
133 write(cooperative_pipe_fp, f, strlen(f));
134 write(cooperative_pipe_fp, "\n", 1);
139 c = calloc(1, sizeof(struct Client));
142 fprintf(stderr, __NAME__": fatal: calloc failed\n");
146 c->focus_new_tab = focus_tab;
148 if (related_wv == NULL)
149 c->web_view = webkit_web_view_new();
151 c->web_view = webkit_web_view_new_with_related_view(related_wv);
152 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
154 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
155 g_signal_connect(G_OBJECT(c->web_view), "notify::favicon",
156 G_CALLBACK(changed_favicon), c);
157 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
158 G_CALLBACK(changed_title), c);
159 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
160 G_CALLBACK(changed_uri), c);
161 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
162 G_CALLBACK(changed_load_progress), c);
163 g_signal_connect(G_OBJECT(c->web_view), "create",
164 G_CALLBACK(client_new_request), NULL);
165 g_signal_connect(G_OBJECT(c->web_view), "close",
166 G_CALLBACK(client_destroy), c);
167 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
168 G_CALLBACK(decide_policy), NULL);
169 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
170 G_CALLBACK(key_web_view), c);
171 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
172 G_CALLBACK(key_web_view), c);
173 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
174 G_CALLBACK(key_web_view), c);
175 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
176 G_CALLBACK(hover_web_view), c);
177 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
178 G_CALLBACK(crashed_web_view), c);
180 if (!initial_wc_setup_done)
182 if (accepted_language[0] != NULL)
183 webkit_web_context_set_preferred_languages(wc, accepted_language);
185 g_signal_connect(G_OBJECT(wc), "download-started",
186 G_CALLBACK(download_handle_start), NULL);
188 trust_user_certs(wc);
190 webkit_web_context_set_favicon_database_directory(wc, NULL);
192 initial_wc_setup_done = TRUE;
195 if (user_agent != NULL)
196 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
197 "user-agent", user_agent, NULL);
199 if (enable_console_to_stdout)
200 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
202 webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
204 c->location = gtk_entry_new();
205 g_signal_connect(G_OBJECT(c->location), "key-press-event",
206 G_CALLBACK(key_location), c);
207 g_signal_connect(G_OBJECT(c->location), "icon-release",
208 G_CALLBACK(icon_location), c);
209 /* XXX This is a workaround. Setting this to NULL (which is done in
210 * grab_feeds_finished() if no feed has been detected) adds a little
211 * padding left of the text. Not sure why. The point of this call
212 * right here is to have that padding right from the start. This
213 * avoids a graphical artifact. */
214 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
215 GTK_ENTRY_ICON_PRIMARY,
218 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
219 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
220 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
222 c->tabicon = gtk_image_new_from_icon_name("text-html", GTK_ICON_SIZE_SMALL_TOOLBAR);
224 c->tablabel = gtk_label_new(__NAME__);
225 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
226 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), tab_width_chars);
227 gtk_widget_set_has_tooltip(c->tablabel, TRUE);
229 /* XXX I don't own a HiDPI screen, so I don't know if scale_factor
230 * does the right thing. */
231 tabbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL,
232 5 * gtk_widget_get_scale_factor(mw.win));
233 gtk_box_pack_start(GTK_BOX(tabbox), c->tabicon, FALSE, FALSE, 0);
234 gtk_box_pack_start(GTK_BOX(tabbox), c->tablabel, TRUE, TRUE, 0);
236 evbox = gtk_event_box_new();
237 gtk_container_add(GTK_CONTAINER(evbox), tabbox);
238 g_signal_connect(G_OBJECT(evbox), "button-release-event",
239 G_CALLBACK(key_tablabel), c);
241 gtk_widget_add_events(evbox, GDK_SCROLL_MASK);
242 g_signal_connect(G_OBJECT(evbox), "scroll-event",
243 G_CALLBACK(key_tablabel), c);
245 /* This only shows the event box and the label inside, nothing else.
246 * Needed because the evbox/label is "internal" to the notebook and
247 * not part of the normal "widget tree" (IIUC). */
248 gtk_widget_show_all(evbox);
250 gtk_notebook_insert_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox,
251 gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook)) + 1);
252 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
255 show_web_view(NULL, c);
257 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
258 G_CALLBACK(show_web_view), c);
262 f = ensure_uri_scheme(uri);
263 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
269 return WEBKIT_WEB_VIEW(c->web_view);
273 client_new_request(WebKitWebView *web_view,
274 WebKitNavigationAction *navigation_action, gpointer data)
276 return client_new(NULL, web_view, FALSE, FALSE);
280 cooperation_setup(void)
283 gchar *fifofilename, *fifopath;
285 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
286 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
287 g_free(fifofilename);
289 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
290 mkfifo(fifopath, 0600);
292 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
293 if (!cooperative_pipe_fp)
295 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
299 if (write(cooperative_pipe_fp, "", 0) == -1)
301 /* Could not do an empty write to the FIFO which means there's
302 * no one listening. */
303 close(cooperative_pipe_fp);
304 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
305 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
308 cooperative_alone = FALSE;
315 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
317 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
318 WebKitURIResponse *resp;
319 GtkToolItem *tb = GTK_TOOL_ITEM(data);
322 gchar *t, *filename, *base;
324 p = webkit_download_get_estimated_progress(download);
328 resp = webkit_download_get_response(download);
329 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
331 uri = webkit_download_get_destination(download);
332 filename = g_filename_from_uri(uri, NULL, NULL);
333 if (filename == NULL)
335 /* This really should not happen because WebKit uses that URI to
336 * write to a file... */
337 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
338 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
339 webkit_uri_response_get_uri(resp), p, size_mb);
343 base = g_path_get_basename(filename);
344 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
348 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
353 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
355 struct Client *c = (struct Client *)data;
358 "a = document.querySelectorAll('"
359 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
360 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
367 " for (i = 0; i < a.length; i++)"
369 " url = encodeURIComponent(a[i].href);"
370 " if ('title' in a[i] && a[i].title != '')"
371 " title = encodeURIComponent(a[i].title);"
374 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
379 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
384 /* The page has loaded fully. We now run the short JavaScript
385 * snippet above that operates on the DOM. It tries to grab all
386 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
388 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
390 grab_feeds_finished, c);
392 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
394 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
398 changed_favicon(GObject *obj, GParamSpec *pspec, gpointer data)
400 struct Client *c = (struct Client *)data;
402 int w, h, w_should, h_should;
403 GdkPixbuf *pb, *pb_scaled;
405 f = webkit_web_view_get_favicon(WEBKIT_WEB_VIEW(c->web_view));
408 gtk_image_set_from_icon_name(GTK_IMAGE(c->tabicon), "text-html",
409 GTK_ICON_SIZE_SMALL_TOOLBAR);
413 w = cairo_image_surface_get_width(f);
414 h = cairo_image_surface_get_height(f);
415 pb = gdk_pixbuf_get_from_surface(f, 0, 0, w, h);
418 w_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
419 h_should = 16 * gtk_widget_get_scale_factor(c->tabicon);
420 pb_scaled = gdk_pixbuf_scale_simple(pb, w_should, h_should,
421 GDK_INTERP_BILINEAR);
422 gtk_image_set_from_pixbuf(GTK_IMAGE(c->tabicon), pb_scaled);
424 g_object_unref(pb_scaled);
431 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
434 struct Client *c = (struct Client *)data;
436 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
437 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
439 u = u == NULL ? __NAME__ : u;
440 u = u[0] == 0 ? __NAME__ : u;
442 t = t == NULL ? u : t;
443 t = t[0] == 0 ? u : t;
445 gtk_label_set_text(GTK_LABEL(c->tablabel), t);
446 gtk_widget_set_tooltip_text(c->tablabel, t);
450 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
453 struct Client *c = (struct Client *)data;
456 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
458 /* When a web process crashes, we get a "notify::uri" signal, but we
459 * can no longer read a meaningful URI. It's just an empty string
460 * now. Not updating the location bar in this scenario is important,
461 * because we would override the "WEB PROCESS CRASHED" message. */
462 if (t != NULL && strlen(t) > 0)
464 gtk_entry_set_text(GTK_ENTRY(c->location), t);
466 if (history_file != NULL)
468 fp = fopen(history_file, "a");
471 fprintf(fp, "%s\n", t);
475 perror(__NAME__": Error opening history file");
481 crashed_web_view(WebKitWebView *web_view, gpointer data)
484 struct Client *c = (struct Client *)data;
486 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
487 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
488 gtk_entry_set_text(GTK_ENTRY(c->location), t);
495 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
496 WebKitPolicyDecisionType type, gpointer data)
498 WebKitResponsePolicyDecision *r;
502 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
503 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
504 if (!webkit_response_policy_decision_is_mime_type_supported(r))
505 webkit_policy_decision_download(decision);
507 webkit_policy_decision_use(decision);
510 /* Use whatever default there is. */
517 download_handle_finished(WebKitDownload *download, gpointer data)
523 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
526 g_signal_connect(G_OBJECT(download), "decide-destination",
527 G_CALLBACK(download_handle), data);
531 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
533 gchar *sug_clean, *path, *path2 = NULL, *uri;
538 sug_clean = g_strdup(suggested_filename);
539 for (i = 0; i < strlen(sug_clean); i++)
540 if (sug_clean[i] == G_DIR_SEPARATOR)
543 path = g_build_filename(download_dir, sug_clean, NULL);
544 path2 = g_strdup(path);
545 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
549 path2 = g_strdup_printf("%s.%d", path, suffix);
555 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
556 webkit_download_cancel(download);
560 uri = g_filename_to_uri(path2, NULL, NULL);
561 webkit_download_set_destination(download, uri);
564 tb = gtk_tool_button_new(NULL, NULL);
565 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
566 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
567 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
568 gtk_widget_show_all(dm.win);
570 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
571 G_CALLBACK(changed_download_progress), tb);
574 g_signal_connect(G_OBJECT(download), "finished",
575 G_CALLBACK(download_handle_finished), NULL);
577 g_object_ref(download);
578 g_signal_connect(G_OBJECT(tb), "clicked",
579 G_CALLBACK(downloadmanager_cancel), download);
586 /* Propagate -- to whom it may concern. */
591 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
593 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
595 webkit_download_cancel(download);
596 g_object_unref(download);
598 gtk_widget_destroy(GTK_WIDGET(tb));
602 downloadmanager_delete(GtkWidget *obj, gpointer data)
604 if (!quit_if_nothing_active())
605 gtk_widget_hide(dm.win);
611 downloadmanager_setup(void)
613 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
614 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
615 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
616 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
617 g_signal_connect(G_OBJECT(dm.win), "delete-event",
618 G_CALLBACK(downloadmanager_delete), NULL);
619 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
620 G_CALLBACK(key_downloadmanager), NULL);
622 dm.toolbar = gtk_toolbar_new();
623 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
624 GTK_ORIENTATION_VERTICAL);
625 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
626 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
628 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
629 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
630 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
631 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
633 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
637 ensure_uri_scheme(const gchar *t)
641 f = g_ascii_strdown(t, -1);
642 if (!g_str_has_prefix(f, "http:") &&
643 !g_str_has_prefix(f, "https:") &&
644 !g_str_has_prefix(f, "file:") &&
645 !g_str_has_prefix(f, "about:") &&
646 !g_str_has_prefix(f, "data:"))
649 fabs = realpath(t, NULL);
652 f = g_strdup_printf("file://%s", fabs);
656 f = g_strdup_printf("http://%s", t);
664 grab_environment_configuration(void)
668 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
670 accepted_language[0] = g_strdup(e);
672 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
674 download_dir = g_strdup(e);
676 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
678 enable_console_to_stdout = TRUE;
680 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
682 fifo_suffix = g_strdup(e);
684 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
686 history_file = g_strdup(e);
688 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
690 home_uri = g_strdup(e);
692 e = g_getenv(__NAME_UPPERCASE__"_TAB_POS");
695 if (strcmp(e, "top") == 0)
696 tab_pos = GTK_POS_TOP;
697 if (strcmp(e, "right") == 0)
698 tab_pos = GTK_POS_RIGHT;
699 if (strcmp(e, "bottom") == 0)
700 tab_pos = GTK_POS_BOTTOM;
701 if (strcmp(e, "left") == 0)
702 tab_pos = GTK_POS_LEFT;
705 e = g_getenv(__NAME_UPPERCASE__"_TAB_WIDTH_CHARS");
707 tab_width_chars = atoi(e);
709 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
711 user_agent = g_strdup(e);
713 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
715 global_zoom = atof(e);
719 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
721 struct Client *c = (struct Client *)data;
722 WebKitJavascriptResult *js_result;
724 JSCException *exception;
728 g_free(c->feed_html);
731 /* This was taken almost verbatim from the example in WebKit's
734 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
736 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
740 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
745 value = webkit_javascript_result_get_js_value(js_result);
746 if (jsc_value_is_string(value))
748 str_value = jsc_value_to_string(value);
749 exception = jsc_context_get_exception(jsc_value_get_context(value));
750 if (exception != NULL)
752 fprintf(stderr, __NAME__": Error running javascript: %s\n",
753 jsc_exception_get_message(exception));
756 c->feed_html = str_value;
758 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
759 GTK_ENTRY_ICON_PRIMARY,
760 "application-rss+xml-symbolic");
761 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
762 GTK_ENTRY_ICON_PRIMARY,
767 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
768 GTK_ENTRY_ICON_PRIMARY,
772 webkit_javascript_result_unref(js_result);
776 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
779 struct Client *c = (struct Client *)data;
781 if (!gtk_widget_is_focus(c->location))
783 if (webkit_hit_test_result_context_is_link(ht))
785 gtk_entry_set_text(GTK_ENTRY(c->location),
786 webkit_hit_test_result_get_link_uri(ht));
788 if (c->hover_uri != NULL)
789 g_free(c->hover_uri);
790 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
794 gtk_entry_set_text(GTK_ENTRY(c->location),
795 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
797 if (c->hover_uri != NULL)
798 g_free(c->hover_uri);
805 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
808 struct Client *c = (struct Client *)data;
810 gchar *data_template =
815 " <meta charset=\"UTF-8\">"
816 " <title>Feeds</title>"
819 " <p>Feeds found on this page:</p>"
826 if (c->feed_html != NULL)
828 /* What we're actually trying to do is show a simple HTML page
829 * that lists all the feeds on the current page. The function
830 * webkit_web_view_load_html() looks like the proper way to do
831 * that. Sad thing is, it doesn't create a history entry, but
832 * instead simply replaces the content of the current page. This
833 * is not what we want.
835 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
836 * mechanism to show my custom HTML snippet *and* create a
839 * [0]: https://tools.ietf.org/html/rfc2397
840 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
841 d = g_strdup_printf(data_template, c->feed_html);
842 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
848 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
850 struct Client *c = (struct Client *)data;
851 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
854 if (event->type == GDK_KEY_PRESS)
856 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
858 switch (((GdkEventKey *)event)->keyval)
860 case GDK_KEY_q: /* close window (left hand) */
861 client_destroy(NULL, c);
863 case GDK_KEY_w: /* home (left hand) */
864 f = ensure_uri_scheme(home_uri);
865 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
868 case GDK_KEY_e: /* new tab (left hand) */
869 f = ensure_uri_scheme(home_uri);
870 client_new(f, NULL, TRUE, TRUE);
873 case GDK_KEY_r: /* reload (left hand) */
874 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
877 case GDK_KEY_d: /* download manager (left hand) */
878 gtk_widget_show_all(dm.win);
880 case GDK_KEY_2: /* search forward (left hand) */
881 case GDK_KEY_n: /* search forward (maybe both hands) */
884 case GDK_KEY_3: /* search backward (left hand) */
887 case GDK_KEY_l: /* location (BOTH hands) */
888 gtk_widget_grab_focus(c->location);
890 case GDK_KEY_k: /* initiate search (BOTH hands) */
891 gtk_widget_grab_focus(c->location);
892 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
893 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
895 case GDK_KEY_c: /* reload trusted certs (left hand) */
896 trust_user_certs(wc);
898 case GDK_KEY_a: /* go one tab to the left (left hand) */
899 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
901 case GDK_KEY_s: /* go one tab to the right (left hand) */
902 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
906 /* navigate backward (left hand) */
907 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
909 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
912 /* navigate forward (left hand) */
913 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
915 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
924 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
926 if (event->type == GDK_KEY_PRESS)
928 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
930 switch (((GdkEventKey *)event)->keyval)
932 case GDK_KEY_d: /* close window (left hand) */
934 downloadmanager_delete(dm.win, NULL);
944 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
946 struct Client *c = (struct Client *)data;
950 if (key_common(widget, event, data))
953 if (event->type == GDK_KEY_PRESS)
955 switch (((GdkEventKey *)event)->keyval)
957 case GDK_KEY_KP_Enter:
959 gtk_widget_grab_focus(c->web_view);
960 t = gtk_entry_get_text(GTK_ENTRY(c->location));
961 if (t != NULL && t[0] == ':' && t[1] == '/')
963 if (search_text != NULL)
965 search_text = g_strdup(t + 2);
970 f = ensure_uri_scheme(t);
971 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
976 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
977 gtk_entry_set_text(GTK_ENTRY(c->location),
978 (t == NULL ? __NAME__ : t));
987 key_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
989 GdkScrollDirection direction;
991 if (event->type == GDK_BUTTON_RELEASE)
993 switch (((GdkEventButton *)event)->button)
996 client_destroy(NULL, data);
1000 else if (event->type == GDK_SCROLL)
1002 gdk_event_get_scroll_direction(event, &direction);
1006 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
1008 case GDK_SCROLL_DOWN:
1009 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
1020 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
1022 struct Client *c = (struct Client *)data;
1026 if (key_common(widget, event, data))
1029 if (event->type == GDK_KEY_PRESS)
1031 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
1033 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
1034 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
1037 else if (event->type == GDK_BUTTON_RELEASE)
1039 switch (((GdkEventButton *)event)->button)
1042 if (c->hover_uri != NULL)
1044 client_new(c->hover_uri, NULL, TRUE, FALSE);
1049 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
1052 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
1056 else if (event->type == GDK_SCROLL)
1058 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
1059 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
1061 gdk_event_get_scroll_deltas(event, &dx, &dy);
1062 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1064 z = dx != 0 ? global_zoom : z;
1065 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1074 mainwindow_setup(void)
1076 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1077 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1078 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1079 gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
1081 mw.notebook = gtk_notebook_new();
1082 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1083 gtk_notebook_set_tab_pos(GTK_NOTEBOOK(mw.notebook), tab_pos);
1084 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1088 quit_if_nothing_active(void)
1098 gtk_widget_show_all(dm.win);
1105 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1109 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1113 client_new(uri, NULL, TRUE, TRUE);
1120 run_user_scripts(WebKitWebView *web_view)
1122 gchar *base = NULL, *path = NULL, *contents = NULL;
1123 const gchar *entry = NULL;
1124 GDir *scriptdir = NULL;
1126 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts", NULL);
1127 scriptdir = g_dir_open(base, 0, NULL);
1128 if (scriptdir != NULL)
1130 while ((entry = g_dir_read_name(scriptdir)) != NULL)
1132 path = g_build_filename(base, entry, NULL);
1133 if (g_str_has_suffix(path, ".js"))
1135 if (g_file_get_contents(path, &contents, NULL, NULL))
1137 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1143 g_dir_close(scriptdir);
1150 search(gpointer data, gint direction)
1152 struct Client *c = (struct Client *)data;
1153 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1154 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1156 if (search_text == NULL)
1162 webkit_find_controller_search(fc, search_text,
1163 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1164 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1168 webkit_find_controller_search_next(fc);
1171 webkit_find_controller_search_previous(fc);
1177 show_web_view(WebKitWebView *web_view, gpointer data)
1179 struct Client *c = (struct Client *)data;
1184 gtk_widget_show_all(mw.win);
1186 if (c->focus_new_tab)
1188 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1190 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
1192 gtk_widget_grab_focus(c->web_view);
1197 trust_user_certs(WebKitWebContext *wc)
1199 GTlsCertificate *cert;
1200 const gchar *basedir, *file, *absfile;
1203 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1204 dir = g_dir_open(basedir, 0, NULL);
1207 file = g_dir_read_name(dir);
1208 while (file != NULL)
1210 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1212 cert = g_tls_certificate_new_from_file(absfile, NULL);
1214 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1216 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1217 file = g_dir_read_name(dir);
1225 main(int argc, char **argv)
1230 gtk_init(&argc, &argv);
1231 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1232 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1234 grab_environment_configuration();
1236 while ((opt = getopt(argc, argv, "C")) != -1)
1241 cooperative_instances = FALSE;
1244 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1249 if (cooperative_instances)
1250 cooperation_setup();
1251 downloadmanager_setup();
1255 if (!cooperative_instances || cooperative_alone)
1257 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1259 webkit_web_context_set_web_extensions_directory(
1260 webkit_web_context_get_default(), c
1265 client_new(home_uri, NULL, TRUE, TRUE);
1268 for (i = optind; i < argc; i++)
1269 client_new(argv[i], NULL, TRUE, TRUE);
1272 if (!cooperative_instances || cooperative_alone)