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_title(GObject *, GParamSpec *, gpointer);
26 static void changed_uri(GObject *, GParamSpec *, gpointer);
27 static gboolean crashed_web_view(WebKitWebView *, gpointer);
28 static gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
29 WebKitPolicyDecisionType, gpointer);
30 static gboolean download_handle(WebKitDownload *, gchar *, gpointer);
31 static void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
32 static void downloadmanager_cancel(GtkToolButton *, gpointer);
33 static gboolean downloadmanager_delete(GtkWidget *, gpointer);
34 static void downloadmanager_setup(void);
35 static gchar *ensure_uri_scheme(const gchar *);
36 static void external_handler_run(GSimpleAction *, GVariant *, gpointer);
37 static void grab_environment_configuration(void);
38 static void grab_feeds_finished(GObject *, GAsyncResult *, gpointer);
39 static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
40 static void icon_location(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer);
41 static gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
42 static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
43 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
44 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
45 static void keywords_load(void);
46 static gboolean keywords_try_search(WebKitWebView *, const gchar *);
47 static void mainwindow_setup(void);
48 static void mainwindow_title(gint);
49 static void mainwindow_title_before(GtkNotebook *, GtkWidget *, guint, gpointer);
50 static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
51 WebKitHitTestResult *, gpointer);
52 static gboolean quit_if_nothing_active(void);
53 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
54 static void run_user_scripts(WebKitWebView *);
55 static void search(gpointer, gint);
56 static void show_web_view(WebKitWebView *, gpointer);
57 static void trust_user_certs(WebKitWebContext *);
62 gchar *external_handler_uri;
77 struct DownloadManager
85 static const gchar *accepted_language[2] = { NULL, NULL };
86 static gint clients = 0, downloads = 0;
87 static gboolean cooperative_alone = TRUE;
88 static gboolean cooperative_instances = TRUE;
89 static int cooperative_pipe_fp = 0;
90 static gchar *download_dir = "/var/tmp";
91 static gboolean enable_console_to_stdout = FALSE;
92 static gchar *fifo_suffix = "main";
93 static gdouble global_zoom = 1.0;
94 static gchar *history_file = NULL;
95 static gchar *home_uri = "about:blank";
96 static gboolean initial_wc_setup_done = FALSE;
97 static GHashTable *keywords = NULL;
98 static gchar *search_text = NULL;
99 static gchar *user_agent = NULL;
103 button_tablabel(GtkWidget *widget, GdkEvent *event, gpointer data)
105 if (event->type == GDK_BUTTON_RELEASE)
107 switch (((GdkEventButton *)event)->button)
110 client_destroy(NULL, data);
118 client_destroy(GtkWidget *widget, gpointer data)
120 struct Client *c = (struct Client *)data;
123 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
124 changed_load_progress, c);
126 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
128 fprintf(stderr, __NAME__": Tab index was -1, bamboozled\n");
130 gtk_notebook_remove_page(GTK_NOTEBOOK(mw.notebook), idx);
135 quit_if_nothing_active();
139 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
142 WebKitWebContext *wc;
146 if (uri != NULL && cooperative_instances && !cooperative_alone)
148 f = ensure_uri_scheme(uri);
149 write(cooperative_pipe_fp, f, strlen(f));
150 write(cooperative_pipe_fp, "\n", 1);
155 c = calloc(1, sizeof(struct Client));
158 fprintf(stderr, __NAME__": fatal: calloc failed\n");
162 if (related_wv == NULL)
163 c->web_view = webkit_web_view_new();
165 c->web_view = webkit_web_view_new_with_related_view(related_wv);
166 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
168 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
169 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
170 G_CALLBACK(changed_title), c);
171 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
172 G_CALLBACK(changed_uri), c);
173 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
174 G_CALLBACK(changed_load_progress), c);
175 g_signal_connect(G_OBJECT(c->web_view), "create",
176 G_CALLBACK(client_new_request), NULL);
177 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
178 G_CALLBACK(menu_web_view), c);
179 g_signal_connect(G_OBJECT(c->web_view), "close",
180 G_CALLBACK(client_destroy), c);
181 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
182 G_CALLBACK(decide_policy), NULL);
183 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
184 G_CALLBACK(key_web_view), c);
185 g_signal_connect(G_OBJECT(c->web_view), "button-release-event",
186 G_CALLBACK(key_web_view), c);
187 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
188 G_CALLBACK(key_web_view), c);
189 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
190 G_CALLBACK(hover_web_view), c);
191 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
192 G_CALLBACK(crashed_web_view), c);
194 if (!initial_wc_setup_done)
196 if (accepted_language[0] != NULL)
197 webkit_web_context_set_preferred_languages(wc, accepted_language);
199 g_signal_connect(G_OBJECT(wc), "download-started",
200 G_CALLBACK(download_handle_start), NULL);
202 trust_user_certs(wc);
204 initial_wc_setup_done = TRUE;
207 if (user_agent != NULL)
208 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
209 "user-agent", user_agent, NULL);
211 if (enable_console_to_stdout)
212 webkit_settings_set_enable_write_console_messages_to_stdout(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
214 webkit_settings_set_enable_developer_extras(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view)), TRUE);
216 c->location = gtk_entry_new();
217 g_signal_connect(G_OBJECT(c->location), "key-press-event",
218 G_CALLBACK(key_location), c);
219 g_signal_connect(G_OBJECT(c->location), "icon-release",
220 G_CALLBACK(icon_location), c);
221 /* XXX This is a workaround. Setting this to NULL (which is done in
222 * grab_feeds_finished() if no feed has been detected) adds a little
223 * padding left of the text. Not sure why. The point of this call
224 * right here is to have that padding right from the start. This
225 * avoids a graphical artifact. */
226 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
227 GTK_ENTRY_ICON_PRIMARY,
230 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
231 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
232 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
234 c->tablabel = gtk_label_new(__NAME__);
235 gtk_label_set_ellipsize(GTK_LABEL(c->tablabel), PANGO_ELLIPSIZE_END);
236 gtk_label_set_width_chars(GTK_LABEL(c->tablabel), 20);
238 evbox = gtk_event_box_new();
239 gtk_container_add(GTK_CONTAINER(evbox), c->tablabel);
240 g_signal_connect(G_OBJECT(evbox), "button-release-event",
241 G_CALLBACK(button_tablabel), c);
243 /* This only shows the event box and the label inside, nothing else.
244 * Needed because the evbox/label is "internal" to the notebook and
245 * not part of the normal "widget tree" (IIUC). */
246 gtk_widget_show_all(evbox);
248 gtk_notebook_append_page(GTK_NOTEBOOK(mw.notebook), c->vbox, evbox);
249 gtk_notebook_set_tab_reorderable(GTK_NOTEBOOK(mw.notebook), c->vbox, TRUE);
252 show_web_view(NULL, c);
254 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
255 G_CALLBACK(show_web_view), c);
259 f = ensure_uri_scheme(uri);
260 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
266 return WEBKIT_WEB_VIEW(c->web_view);
270 client_new_request(WebKitWebView *web_view,
271 WebKitNavigationAction *navigation_action, gpointer data)
273 return client_new(NULL, web_view, FALSE);
277 cooperation_setup(void)
280 gchar *fifofilename, *fifopath;
282 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
283 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
284 g_free(fifofilename);
286 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
287 mkfifo(fifopath, 0600);
289 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
290 if (!cooperative_pipe_fp)
292 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
296 if (write(cooperative_pipe_fp, "", 0) == -1)
298 /* Could not do an empty write to the FIFO which means there's
299 * no one listening. */
300 close(cooperative_pipe_fp);
301 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
302 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
305 cooperative_alone = FALSE;
312 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
314 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
315 WebKitURIResponse *resp;
316 GtkToolItem *tb = GTK_TOOL_ITEM(data);
319 gchar *t, *filename, *base;
321 p = webkit_download_get_estimated_progress(download);
325 resp = webkit_download_get_response(download);
326 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
328 uri = webkit_download_get_destination(download);
329 filename = g_filename_from_uri(uri, NULL, NULL);
330 if (filename == NULL)
332 /* This really should not happen because WebKit uses that URI to
333 * write to a file... */
334 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
335 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
336 webkit_uri_response_get_uri(resp), p, size_mb);
340 base = g_path_get_basename(filename);
341 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
345 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
350 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
352 struct Client *c = (struct Client *)data;
355 "a = document.querySelectorAll('"
356 " html > head > link[rel=\"alternate\"][href][type=\"application/atom+xml\"],"
357 " html > head > link[rel=\"alternate\"][href][type=\"application/rss+xml\"]"
364 " for (i = 0; i < a.length; i++)"
366 " url = encodeURIComponent(a[i].href);"
367 " if ('title' in a[i] && a[i].title != '')"
368 " title = encodeURIComponent(a[i].title);"
371 " out += '<li><a href=\"' + url + '\">' + title + '</a></li>';"
376 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
381 /* The page has loaded fully. We now run the short JavaScript
382 * snippet above that operates on the DOM. It tries to grab all
383 * occurences of <link rel="alternate" ...>, i.e. RSS/Atom feed
385 webkit_web_view_run_javascript(WEBKIT_WEB_VIEW(c->web_view),
387 grab_feeds_finished, c);
389 run_user_scripts(WEBKIT_WEB_VIEW(c->web_view));
391 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
395 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
398 struct Client *c = (struct Client *)data;
400 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
401 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
403 u = u == NULL ? __NAME__ : u;
404 u = u[0] == 0 ? __NAME__ : u;
406 t = t == NULL ? u : t;
407 t = t[0] == 0 ? u : t;
409 gtk_label_set_text(GTK_LABEL(c->tablabel), t);
410 mainwindow_title(-1);
414 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
417 struct Client *c = (struct Client *)data;
420 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
422 /* When a web process crashes, we get a "notify::uri" signal, but we
423 * can no longer read a meaningful URI. It's just an empty string
424 * now. Not updating the location bar in this scenario is important,
425 * because we would override the "WEB PROCESS CRASHED" message. */
426 if (t != NULL && strlen(t) > 0)
428 gtk_entry_set_text(GTK_ENTRY(c->location), t);
430 if (history_file != NULL)
432 fp = fopen(history_file, "a");
435 fprintf(fp, "%s\n", t);
439 perror(__NAME__": Error opening history file");
445 crashed_web_view(WebKitWebView *web_view, gpointer data)
448 struct Client *c = (struct Client *)data;
450 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
451 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
452 gtk_entry_set_text(GTK_ENTRY(c->location), t);
459 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
460 WebKitPolicyDecisionType type, gpointer data)
462 WebKitResponsePolicyDecision *r;
466 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
467 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
468 if (!webkit_response_policy_decision_is_mime_type_supported(r))
469 webkit_policy_decision_download(decision);
471 webkit_policy_decision_use(decision);
474 /* Use whatever default there is. */
481 download_handle_finished(WebKitDownload *download, gpointer data)
487 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
490 g_signal_connect(G_OBJECT(download), "decide-destination",
491 G_CALLBACK(download_handle), data);
495 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
497 gchar *sug_clean, *path, *path2 = NULL, *uri;
502 sug_clean = g_strdup(suggested_filename);
503 for (i = 0; i < strlen(sug_clean); i++)
504 if (sug_clean[i] == G_DIR_SEPARATOR)
507 path = g_build_filename(download_dir, sug_clean, NULL);
508 path2 = g_strdup(path);
509 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
513 path2 = g_strdup_printf("%s.%d", path, suffix);
519 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
520 webkit_download_cancel(download);
524 uri = g_filename_to_uri(path2, NULL, NULL);
525 webkit_download_set_destination(download, uri);
528 tb = gtk_tool_button_new(NULL, NULL);
529 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
530 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
531 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
532 gtk_widget_show_all(dm.win);
534 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
535 G_CALLBACK(changed_download_progress), tb);
538 g_signal_connect(G_OBJECT(download), "finished",
539 G_CALLBACK(download_handle_finished), NULL);
541 g_object_ref(download);
542 g_signal_connect(G_OBJECT(tb), "clicked",
543 G_CALLBACK(downloadmanager_cancel), download);
550 /* Propagate -- to whom it may concern. */
555 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
557 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
559 webkit_download_cancel(download);
560 g_object_unref(download);
562 gtk_widget_destroy(GTK_WIDGET(tb));
566 downloadmanager_delete(GtkWidget *obj, gpointer data)
568 if (!quit_if_nothing_active())
569 gtk_widget_hide(dm.win);
575 downloadmanager_setup(void)
577 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
578 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
579 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
580 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
581 g_signal_connect(G_OBJECT(dm.win), "delete-event",
582 G_CALLBACK(downloadmanager_delete), NULL);
583 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
584 G_CALLBACK(key_downloadmanager), NULL);
586 dm.toolbar = gtk_toolbar_new();
587 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
588 GTK_ORIENTATION_VERTICAL);
589 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
590 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
592 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
593 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
594 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
595 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
597 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
601 ensure_uri_scheme(const gchar *t)
605 f = g_ascii_strdown(t, -1);
606 if (!g_str_has_prefix(f, "http:") &&
607 !g_str_has_prefix(f, "https:") &&
608 !g_str_has_prefix(f, "file:") &&
609 !g_str_has_prefix(f, "about:") &&
610 !g_str_has_prefix(f, "data:"))
613 fabs = realpath(t, NULL);
616 f = g_strdup_printf("file://%s", fabs);
620 f = g_strdup_printf("http://%s", t);
628 external_handler_run(GSimpleAction *simple, GVariant *param, gpointer data)
630 struct Client *c = (struct Client *)data;
631 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
638 argv[2] = c->external_handler_uri;
639 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
642 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
647 g_spawn_close_pid(pid);
651 grab_environment_configuration(void)
655 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
657 accepted_language[0] = g_strdup(e);
659 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
661 download_dir = g_strdup(e);
663 e = g_getenv(__NAME_UPPERCASE__"_ENABLE_CONSOLE_TO_STDOUT");
665 enable_console_to_stdout = TRUE;
667 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
669 fifo_suffix = g_strdup(e);
671 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
673 history_file = g_strdup(e);
675 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
677 home_uri = g_strdup(e);
679 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
681 user_agent = g_strdup(e);
683 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
685 global_zoom = atof(e);
689 grab_feeds_finished(GObject *object, GAsyncResult *result, gpointer data)
691 struct Client *c = (struct Client *)data;
692 WebKitJavascriptResult *js_result;
694 JSCException *exception;
698 g_free(c->feed_html);
701 /* This was taken almost verbatim from the example in WebKit's
704 * https://webkitgtk.org/reference/webkit2gtk/stable/WebKitWebView.html#webkit-web-view-run-javascript-finish */
706 js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object),
710 fprintf(stderr, __NAME__": Error running javascript: %s\n", err->message);
715 value = webkit_javascript_result_get_js_value(js_result);
716 if (jsc_value_is_string(value))
718 str_value = jsc_value_to_string(value);
719 exception = jsc_context_get_exception(jsc_value_get_context(value));
720 if (exception != NULL)
722 fprintf(stderr, __NAME__": Error running javascript: %s\n",
723 jsc_exception_get_message(exception));
726 c->feed_html = str_value;
728 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
729 GTK_ENTRY_ICON_PRIMARY,
730 "application-rss+xml-symbolic");
731 gtk_entry_set_icon_activatable(GTK_ENTRY(c->location),
732 GTK_ENTRY_ICON_PRIMARY,
737 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(c->location),
738 GTK_ENTRY_ICON_PRIMARY,
742 webkit_javascript_result_unref(js_result);
746 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
749 struct Client *c = (struct Client *)data;
751 if (!gtk_widget_is_focus(c->location))
753 if (webkit_hit_test_result_context_is_link(ht))
755 gtk_entry_set_text(GTK_ENTRY(c->location),
756 webkit_hit_test_result_get_link_uri(ht));
758 if (c->hover_uri != NULL)
759 g_free(c->hover_uri);
760 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
764 gtk_entry_set_text(GTK_ENTRY(c->location),
765 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
767 if (c->hover_uri != NULL)
768 g_free(c->hover_uri);
775 icon_location(GtkEntry *entry, GtkEntryIconPosition icon_pos, GdkEvent *event,
778 struct Client *c = (struct Client *)data;
780 gchar *data_template =
785 " <meta charset=\"UTF-8\">"
786 " <title>Feeds</title>"
789 " <p>Feeds found on this page:</p>"
796 if (c->feed_html != NULL)
798 /* What we're actually trying to do is show a simple HTML page
799 * that lists all the feeds on the current page. The function
800 * webkit_web_view_load_html() looks like the proper way to do
801 * that. Sad thing is, it doesn't create a history entry, but
802 * instead simply replaces the content of the current page. This
803 * is not what we want.
805 * RFC 2397 [0] defines the data URI scheme [1]. We abuse this
806 * mechanism to show my custom HTML snippet *and* create a
809 * [0]: https://tools.ietf.org/html/rfc2397
810 * [1]: https://en.wikipedia.org/wiki/Data_URI_scheme */
811 d = g_strdup_printf(data_template, c->feed_html);
812 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), d);
818 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
820 struct Client *c = (struct Client *)data;
821 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
824 if (event->type == GDK_KEY_PRESS)
826 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
828 switch (((GdkEventKey *)event)->keyval)
830 case GDK_KEY_q: /* close window (left hand) */
831 client_destroy(NULL, c);
833 case GDK_KEY_w: /* home (left hand) */
834 f = ensure_uri_scheme(home_uri);
835 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
838 case GDK_KEY_e: /* new tab (left hand) */
839 f = ensure_uri_scheme(home_uri);
840 client_new(f, NULL, TRUE);
843 case GDK_KEY_r: /* reload (left hand) */
844 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
847 case GDK_KEY_d: /* download manager (left hand) */
848 gtk_widget_show_all(dm.win);
850 case GDK_KEY_2: /* search forward (left hand) */
851 case GDK_KEY_n: /* search forward (maybe both hands) */
854 case GDK_KEY_3: /* search backward (left hand) */
857 case GDK_KEY_l: /* location (BOTH hands) */
858 gtk_widget_grab_focus(c->location);
860 case GDK_KEY_k: /* initiate search (BOTH hands) */
861 gtk_widget_grab_focus(c->location);
862 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
863 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
865 case GDK_KEY_c: /* reload trusted certs (left hand) */
866 trust_user_certs(wc);
868 case GDK_KEY_x: /* launch external handler (left hand) */
869 if (c->external_handler_uri != NULL)
870 g_free(c->external_handler_uri);
871 c->external_handler_uri = g_strdup(
872 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
873 external_handler_run(NULL, NULL, c);
875 case GDK_KEY_a: /* go one tab to the left (left hand) */
876 gtk_notebook_prev_page(GTK_NOTEBOOK(mw.notebook));
878 case GDK_KEY_s: /* go one tab to the right (left hand) */
879 gtk_notebook_next_page(GTK_NOTEBOOK(mw.notebook));
883 /* navigate backward (left hand) */
884 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
886 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
889 /* navigate forward (left hand) */
890 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
892 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
901 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
903 if (event->type == GDK_KEY_PRESS)
905 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
907 switch (((GdkEventKey *)event)->keyval)
909 case GDK_KEY_d: /* close window (left hand) */
911 downloadmanager_delete(dm.win, NULL);
921 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
923 struct Client *c = (struct Client *)data;
927 if (key_common(widget, event, data))
930 if (event->type == GDK_KEY_PRESS)
932 switch (((GdkEventKey *)event)->keyval)
934 case GDK_KEY_KP_Enter:
936 gtk_widget_grab_focus(c->web_view);
937 t = gtk_entry_get_text(GTK_ENTRY(c->location));
938 if (t != NULL && t[0] == ':' && t[1] == '/')
940 if (search_text != NULL)
942 search_text = g_strdup(t + 2);
945 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
947 f = ensure_uri_scheme(t);
948 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
953 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
954 gtk_entry_set_text(GTK_ENTRY(c->location),
955 (t == NULL ? __NAME__ : t));
964 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
966 struct Client *c = (struct Client *)data;
970 if (key_common(widget, event, data))
973 if (event->type == GDK_KEY_PRESS)
975 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
977 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
978 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
981 else if (event->type == GDK_BUTTON_RELEASE)
983 switch (((GdkEventButton *)event)->button)
986 if (c->hover_uri != NULL)
988 client_new(c->hover_uri, NULL, TRUE);
993 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
996 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
1000 else if (event->type == GDK_SCROLL)
1002 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
1003 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
1005 gdk_event_get_scroll_deltas(event, &dx, &dy);
1006 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
1008 z = dx != 0 ? global_zoom : z;
1009 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
1021 GIOChannel *channel = NULL;
1022 gchar *path = NULL, *buf = NULL;
1023 gchar **tokens = NULL;
1025 keywords = g_hash_table_new(g_str_hash, g_str_equal);
1027 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
1029 channel = g_io_channel_new_file(path, "r", &err);
1030 if (channel != NULL)
1032 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
1033 == G_IO_STATUS_NORMAL)
1038 tokens = g_strsplit(buf, " ", 2);
1039 if (tokens[0] != NULL && tokens[1] != NULL)
1040 g_hash_table_insert(keywords, g_strdup(tokens[0]),
1041 g_strdup(tokens[1]));
1046 g_io_channel_shutdown(channel, FALSE, NULL);
1052 keywords_try_search(WebKitWebView *web_view, const gchar *t)
1054 gboolean ret = FALSE;
1055 gchar **tokens = NULL;
1056 gchar *val = NULL, *escaped = NULL, *uri = NULL;
1058 tokens = g_strsplit(t, " ", 2);
1059 if (tokens[0] != NULL && tokens[1] != NULL)
1061 val = g_hash_table_lookup(keywords, tokens[0]);
1064 escaped = g_uri_escape_string(tokens[1], NULL, TRUE);
1065 uri = g_strdup_printf((gchar *)val, escaped);
1066 webkit_web_view_load_uri(web_view, uri);
1078 mainwindow_setup(void)
1080 mw.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1081 gtk_window_set_default_size(GTK_WINDOW(mw.win), 800, 600);
1082 g_signal_connect(G_OBJECT(mw.win), "destroy", gtk_main_quit, NULL);
1083 gtk_window_set_title(GTK_WINDOW(mw.win), __NAME__);
1085 mw.notebook = gtk_notebook_new();
1086 gtk_notebook_set_scrollable(GTK_NOTEBOOK(mw.notebook), TRUE);
1087 gtk_container_add(GTK_CONTAINER(mw.win), mw.notebook);
1088 g_signal_connect(G_OBJECT(mw.notebook), "switch-page",
1089 G_CALLBACK(mainwindow_title_before), NULL);
1093 mainwindow_title_before(GtkNotebook *nb, GtkWidget *p, guint idx, gpointer data)
1095 mainwindow_title(idx);
1099 mainwindow_title(gint idx)
1101 GtkWidget *child, *evbox, *label;
1106 idx = gtk_notebook_get_current_page(GTK_NOTEBOOK(mw.notebook));
1111 child = gtk_notebook_get_nth_page(GTK_NOTEBOOK(mw.notebook), idx);
1115 evbox = gtk_notebook_get_tab_label(GTK_NOTEBOOK(mw.notebook), child);
1116 label = gtk_bin_get_child(GTK_BIN(evbox));
1117 text = gtk_label_get_text(GTK_LABEL(label));
1118 gtk_window_set_title(GTK_WINDOW(mw.win), text);
1122 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
1123 WebKitHitTestResult *ht, gpointer data)
1125 struct Client *c = (struct Client *)data;
1126 GSimpleAction *action = NULL;
1127 WebKitContextMenuItem *mi = NULL;
1128 const gchar *uri = NULL;
1132 if (webkit_hit_test_result_context_is_link(ht))
1133 uri = webkit_hit_test_result_get_link_uri(ht);
1134 else if (webkit_hit_test_result_context_is_image(ht))
1135 uri = webkit_hit_test_result_get_image_uri(ht);
1136 else if (webkit_hit_test_result_context_is_media(ht))
1137 uri = webkit_hit_test_result_get_media_uri(ht);
1141 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
1143 if (c->external_handler_uri != NULL)
1144 g_free(c->external_handler_uri);
1145 c->external_handler_uri = g_strdup(uri);
1146 action = g_simple_action_new("external_handler", NULL);
1147 g_signal_connect(G_OBJECT(action), "activate",
1148 G_CALLBACK(external_handler_run), data);
1149 mi = webkit_context_menu_item_new_from_gaction(G_ACTION(action),
1150 "Open with external handler",
1152 webkit_context_menu_append(menu, mi);
1153 g_object_unref(action);
1156 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
1161 quit_if_nothing_active(void)
1171 gtk_widget_show_all(dm.win);
1178 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
1182 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
1186 client_new(uri, NULL, TRUE);
1193 run_user_scripts(WebKitWebView *web_view)
1195 gchar *base = NULL, *path = NULL, *contents = NULL;
1196 const gchar *entry = NULL;
1197 GDir *scriptdir = NULL;
1199 base = g_build_filename(g_get_user_config_dir(), __NAME__, "user-scripts", NULL);
1200 scriptdir = g_dir_open(base, 0, NULL);
1201 if (scriptdir != NULL)
1203 while ((entry = g_dir_read_name(scriptdir)) != NULL)
1205 path = g_build_filename(base, entry, NULL);
1206 if (g_str_has_suffix(path, ".js"))
1208 if (g_file_get_contents(path, &contents, NULL, NULL))
1210 webkit_web_view_run_javascript(web_view, contents, NULL, NULL, NULL);
1216 g_dir_close(scriptdir);
1223 search(gpointer data, gint direction)
1225 struct Client *c = (struct Client *)data;
1226 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
1227 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
1229 if (search_text == NULL)
1235 webkit_find_controller_search(fc, search_text,
1236 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
1237 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
1241 webkit_find_controller_search_next(fc);
1244 webkit_find_controller_search_previous(fc);
1250 show_web_view(WebKitWebView *web_view, gpointer data)
1252 struct Client *c = (struct Client *)data;
1257 gtk_widget_show_all(mw.win);
1259 idx = gtk_notebook_page_num(GTK_NOTEBOOK(mw.notebook), c->vbox);
1261 gtk_notebook_set_current_page(GTK_NOTEBOOK(mw.notebook), idx);
1263 gtk_widget_grab_focus(c->web_view);
1267 trust_user_certs(WebKitWebContext *wc)
1269 GTlsCertificate *cert;
1270 const gchar *basedir, *file, *absfile;
1273 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1274 dir = g_dir_open(basedir, 0, NULL);
1277 file = g_dir_read_name(dir);
1278 while (file != NULL)
1280 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1282 cert = g_tls_certificate_new_from_file(absfile, NULL);
1284 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1286 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1287 file = g_dir_read_name(dir);
1295 main(int argc, char **argv)
1300 gtk_init(&argc, &argv);
1301 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1302 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1304 grab_environment_configuration();
1306 while ((opt = getopt(argc, argv, "C")) != -1)
1311 cooperative_instances = FALSE;
1314 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1320 if (cooperative_instances)
1321 cooperation_setup();
1322 downloadmanager_setup();
1326 if (!cooperative_instances || cooperative_alone)
1328 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1330 webkit_web_context_set_web_extensions_directory(
1331 webkit_web_context_get_default(), c
1336 client_new(home_uri, NULL, TRUE);
1339 for (i = optind; i < argc; i++)
1340 client_new(argv[i], NULL, TRUE);
1343 if (!cooperative_instances || cooperative_alone)