11 #include <gdk/gdkkeysyms.h>
13 #include <webkit2/webkit2.h>
16 static void client_destroy(GtkWidget *, gpointer);
17 static gboolean client_destroy_request(WebKitWebView *, gpointer);
18 static WebKitWebView *client_new(const gchar *, WebKitWebView *, gboolean);
19 static WebKitWebView *client_new_request(WebKitWebView *, WebKitNavigationAction *,
21 static void cooperation_setup(void);
22 static void changed_download_progress(GObject *, GParamSpec *, gpointer);
23 static void changed_load_progress(GObject *, GParamSpec *, gpointer);
24 static void changed_title(GObject *, GParamSpec *, gpointer);
25 static void changed_uri(GObject *, GParamSpec *, gpointer);
26 static gboolean crashed_web_view(WebKitWebView *, gpointer);
27 static gboolean decide_policy(WebKitWebView *, WebKitPolicyDecision *,
28 WebKitPolicyDecisionType, gpointer);
29 static gboolean download_handle(WebKitDownload *, gchar *, gpointer);
30 static void download_handle_start(WebKitWebView *, WebKitDownload *, gpointer);
31 static void downloadmanager_cancel(GtkToolButton *, gpointer data);
32 static void downloadmanager_setup(void);
33 static gchar *ensure_uri_scheme(const gchar *);
34 static void external_handler_run(GtkAction *, gpointer);
35 static void grab_environment_configuration(void);
36 static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
37 static gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
38 static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
39 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
40 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
41 static void keywords_load(void);
42 static gboolean keywords_try_search(WebKitWebView *, const gchar *);
43 static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
44 WebKitHitTestResult *, gpointer);
45 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
46 static void search(gpointer, gint);
47 static void show_web_view(WebKitWebView *, gpointer);
48 static Window tabbed_launch(void);
49 static void trust_user_certs(WebKitWebContext *);
54 gchar *external_handler_uri;
62 struct DownloadManager
70 static const gchar *accepted_language[2] = { NULL, NULL };
71 static gint clients = 0;
72 static gboolean cooperative_alone = TRUE;
73 static gboolean cooperative_instances = TRUE;
74 static int cooperative_pipe_fp = 0;
75 static gchar *download_dir = "/var/tmp";
76 static Window embed = 0;
77 static gchar *fifo_suffix = "main";
78 static gdouble global_zoom = 1.0;
79 static gchar *history_file = NULL;
80 static gchar *home_uri = "about:blank";
81 static gboolean initial_wc_setup_done = FALSE;
82 static GHashTable *keywords = NULL;
83 static gchar *search_text = NULL;
84 static gboolean tabbed_automagic = TRUE;
85 static gchar *user_agent = NULL;
89 client_destroy(GtkWidget *obj, gpointer data)
91 struct Client *c = (struct Client *)data;
93 g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
94 changed_load_progress, c);
104 client_destroy_request(WebKitWebView *web_view, gpointer data)
106 struct Client *c = (struct Client *)data;
108 gtk_widget_destroy(c->win);
114 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
117 WebKitWebContext *wc;
120 if (uri != NULL && cooperative_instances && !cooperative_alone)
122 f = ensure_uri_scheme(uri);
123 write(cooperative_pipe_fp, f, strlen(f));
124 write(cooperative_pipe_fp, "\n", 1);
129 c = calloc(1, sizeof(struct Client));
132 fprintf(stderr, __NAME__": fatal: calloc failed\n");
138 c->win = gtk_plug_new(embed);
139 if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
141 fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
142 gtk_widget_destroy(c->win);
149 c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
151 gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
153 g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
154 gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
156 if (related_wv == NULL)
157 c->web_view = webkit_web_view_new();
159 c->web_view = webkit_web_view_new_with_related_view(related_wv);
160 wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
162 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
163 g_signal_connect(G_OBJECT(c->web_view), "notify::title",
164 G_CALLBACK(changed_title), c);
165 g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
166 G_CALLBACK(changed_uri), c);
167 g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
168 G_CALLBACK(changed_load_progress), c);
169 g_signal_connect(G_OBJECT(c->web_view), "create",
170 G_CALLBACK(client_new_request), NULL);
171 g_signal_connect(G_OBJECT(c->web_view), "context-menu",
172 G_CALLBACK(menu_web_view), c);
173 g_signal_connect(G_OBJECT(c->web_view), "close",
174 G_CALLBACK(client_destroy_request), c);
175 g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
176 G_CALLBACK(decide_policy), NULL);
177 g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
178 G_CALLBACK(key_web_view), c);
179 g_signal_connect(G_OBJECT(c->web_view), "button-press-event",
180 G_CALLBACK(key_web_view), c);
181 g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
182 G_CALLBACK(key_web_view), c);
183 g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
184 G_CALLBACK(hover_web_view), c);
185 g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
186 G_CALLBACK(crashed_web_view), c);
188 if (!initial_wc_setup_done)
190 if (accepted_language[0] != NULL)
191 webkit_web_context_set_preferred_languages(wc, accepted_language);
193 g_signal_connect(G_OBJECT(wc), "download-started",
194 G_CALLBACK(download_handle_start), NULL);
196 trust_user_certs(wc);
198 initial_wc_setup_done = TRUE;
201 if (user_agent != NULL)
202 g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
203 "user-agent", user_agent, NULL);
205 c->location = gtk_entry_new();
206 g_signal_connect(G_OBJECT(c->location), "key-press-event",
207 G_CALLBACK(key_location), c);
209 c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
210 gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
211 gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
213 gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
216 show_web_view(NULL, c);
218 g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
219 G_CALLBACK(show_web_view), c);
223 f = ensure_uri_scheme(uri);
224 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
230 return WEBKIT_WEB_VIEW(c->web_view);
234 client_new_request(WebKitWebView *web_view,
235 WebKitNavigationAction *navigation_action, gpointer data)
237 return client_new(NULL, web_view, FALSE);
241 cooperation_setup(void)
244 gchar *fifofilename, *fifopath;
246 fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
247 fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
248 g_free(fifofilename);
250 if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
251 mkfifo(fifopath, 0600);
253 cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
254 if (!cooperative_pipe_fp)
256 fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
260 if (write(cooperative_pipe_fp, "", 0) == -1)
262 /* Could not do an empty write to the FIFO which means there's
263 * no one listening. */
264 close(cooperative_pipe_fp);
265 towatch = g_io_channel_new_file(fifopath, "r+", NULL);
266 g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
269 cooperative_alone = FALSE;
276 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
278 WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
279 WebKitURIResponse *resp;
280 GtkToolItem *tb = GTK_TOOL_ITEM(data);
283 gchar *t, *filename, *base;
285 p = webkit_download_get_estimated_progress(download);
289 resp = webkit_download_get_response(download);
290 size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
292 uri = webkit_download_get_destination(download);
293 filename = g_filename_from_uri(uri, NULL, NULL);
294 if (filename == NULL)
296 /* This really should not happen because WebKit uses that URI to
297 * write to a file... */
298 fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
299 t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
300 webkit_uri_response_get_uri(resp), p, size_mb);
304 base = g_path_get_basename(filename);
305 t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
309 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
314 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
316 struct Client *c = (struct Client *)data;
319 p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
320 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
324 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
327 struct Client *c = (struct Client *)data;
329 u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
330 t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
332 u = u == NULL ? __NAME__ : u;
333 u = u[0] == 0 ? __NAME__ : u;
335 t = t == NULL ? u : t;
336 t = t[0] == 0 ? u : t;
338 gtk_window_set_title(GTK_WINDOW(c->win), t);
342 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
345 struct Client *c = (struct Client *)data;
348 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
350 /* When a web process crashes, we get a "notify::uri" signal, but we
351 * can no longer read a meaningful URI. It's just an empty string
352 * now. Not updating the location bar in this scenario is important,
353 * because we would override the "WEB PROCESS CRASHED" message. */
354 if (t != NULL && strlen(t) > 0)
356 gtk_entry_set_text(GTK_ENTRY(c->location), t);
358 if (history_file != NULL)
360 fp = fopen(history_file, "a");
363 fprintf(fp, "%s\n", t);
367 perror(__NAME__": Error opening history file");
373 crashed_web_view(WebKitWebView *web_view, gpointer data)
376 struct Client *c = (struct Client *)data;
378 t = g_strdup_printf("WEB PROCESS CRASHED: %s",
379 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(web_view)));
380 gtk_entry_set_text(GTK_ENTRY(c->location), t);
387 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
388 WebKitPolicyDecisionType type, gpointer data)
390 WebKitResponsePolicyDecision *r;
394 case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
395 r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
396 if (!webkit_response_policy_decision_is_mime_type_supported(r))
397 webkit_policy_decision_download(decision);
399 webkit_policy_decision_use(decision);
402 /* Use whatever default there is. */
409 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
412 g_signal_connect(G_OBJECT(download), "decide-destination",
413 G_CALLBACK(download_handle), data);
417 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
419 gchar *sug_clean, *path, *path2 = NULL, *uri;
424 sug_clean = g_strdup(suggested_filename);
425 for (i = 0; i < strlen(sug_clean); i++)
426 if (sug_clean[i] == G_DIR_SEPARATOR)
429 path = g_build_filename(download_dir, sug_clean, NULL);
430 path2 = g_strdup(path);
431 while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
435 path2 = g_strdup_printf("%s.%d", path, suffix);
441 fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
442 webkit_download_cancel(download);
446 uri = g_filename_to_uri(path2, NULL, NULL);
447 webkit_download_set_destination(download, uri);
450 tb = gtk_tool_button_new(NULL, NULL);
451 gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
452 gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
453 gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
454 gtk_widget_show_all(dm.win);
456 g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
457 G_CALLBACK(changed_download_progress), tb);
459 g_object_ref(download);
460 g_signal_connect(G_OBJECT(tb), "clicked",
461 G_CALLBACK(downloadmanager_cancel), download);
468 /* Propagate -- to whom it may concern. */
473 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
475 WebKitDownload *download = WEBKIT_DOWNLOAD(data);
477 webkit_download_cancel(download);
478 g_object_unref(download);
480 gtk_widget_destroy(GTK_WIDGET(tb));
484 downloadmanager_setup(void)
486 dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
487 gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
488 gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
489 gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
490 g_signal_connect(G_OBJECT(dm.win), "delete-event",
491 G_CALLBACK(gtk_widget_hide_on_delete), NULL);
492 g_signal_connect(G_OBJECT(dm.win), "key-press-event",
493 G_CALLBACK(key_downloadmanager), NULL);
495 dm.toolbar = gtk_toolbar_new();
496 gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
497 GTK_ORIENTATION_VERTICAL);
498 gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
499 gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
501 dm.scroll = gtk_scrolled_window_new(NULL, NULL);
502 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
503 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
504 gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
506 gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
510 ensure_uri_scheme(const gchar *t)
514 f = g_ascii_strdown(t, -1);
515 if (!g_str_has_prefix(f, "http:") &&
516 !g_str_has_prefix(f, "https:") &&
517 !g_str_has_prefix(f, "file:") &&
518 !g_str_has_prefix(f, "about:"))
521 fabs = realpath(t, NULL);
524 f = g_strdup_printf("file://%s", fabs);
528 f = g_strdup_printf("http://%s", t);
536 external_handler_run(GtkAction *action, gpointer data)
538 struct Client *c = (struct Client *)data;
539 gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
545 argv[2] = c->external_handler_uri;
546 if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
549 fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
554 g_spawn_close_pid(pid);
558 grab_environment_configuration(void)
562 e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
564 accepted_language[0] = g_strdup(e);
566 e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
568 download_dir = g_strdup(e);
570 e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
572 fifo_suffix = g_strdup(e);
574 e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
576 history_file = g_strdup(e);
578 e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
580 home_uri = g_strdup(e);
582 e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
584 user_agent = g_strdup(e);
586 e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
588 global_zoom = atof(e);
592 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
595 struct Client *c = (struct Client *)data;
597 if (!gtk_widget_is_focus(c->location))
599 if (webkit_hit_test_result_context_is_link(ht))
601 gtk_entry_set_text(GTK_ENTRY(c->location),
602 webkit_hit_test_result_get_link_uri(ht));
604 if (c->hover_uri != NULL)
605 g_free(c->hover_uri);
606 c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
610 gtk_entry_set_text(GTK_ENTRY(c->location),
611 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
613 if (c->hover_uri != NULL)
614 g_free(c->hover_uri);
621 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
623 struct Client *c = (struct Client *)data;
624 WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
627 if (event->type == GDK_KEY_PRESS)
629 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
631 switch (((GdkEventKey *)event)->keyval)
633 case GDK_KEY_q: /* close window (left hand) */
634 gtk_widget_destroy(c->win);
636 case GDK_KEY_w: /* home (left hand) */
637 f = ensure_uri_scheme(home_uri);
638 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
641 case GDK_KEY_e: /* new tab (left hand) */
642 f = ensure_uri_scheme(home_uri);
643 client_new(f, NULL, TRUE);
646 case GDK_KEY_r: /* reload (left hand) */
647 webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
650 case GDK_KEY_d: /* download manager (left hand) */
651 gtk_widget_show_all(dm.win);
653 case GDK_KEY_2: /* search forward (left hand) */
654 case GDK_KEY_n: /* search forward (maybe both hands) */
657 case GDK_KEY_3: /* search backward (left hand) */
660 case GDK_KEY_l: /* location (BOTH hands) */
661 gtk_widget_grab_focus(c->location);
663 case GDK_KEY_k: /* initiate search (BOTH hands) */
664 gtk_widget_grab_focus(c->location);
665 gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
666 gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
668 case GDK_KEY_c: /* reload trusted certs (left hand) */
669 trust_user_certs(wc);
671 case GDK_KEY_x: /* launch external handler (left hand) */
672 if (c->external_handler_uri != NULL)
673 g_free(c->external_handler_uri);
674 c->external_handler_uri = g_strdup(
675 webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
676 external_handler_run(NULL, c);
680 /* navigate backward (left hand) */
681 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
683 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
686 /* navigate forward (left hand) */
687 else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
689 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
698 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
700 if (event->type == GDK_KEY_PRESS)
702 if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
704 switch (((GdkEventKey *)event)->keyval)
706 case GDK_KEY_d: /* close window (left hand) */
707 gtk_widget_hide(dm.win);
717 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
719 struct Client *c = (struct Client *)data;
723 if (key_common(widget, event, data))
726 if (event->type == GDK_KEY_PRESS)
728 switch (((GdkEventKey *)event)->keyval)
730 case GDK_KEY_KP_Enter:
732 gtk_widget_grab_focus(c->web_view);
733 t = gtk_entry_get_text(GTK_ENTRY(c->location));
734 if (t != NULL && t[0] == ':' && t[1] == '/')
736 if (search_text != NULL)
738 search_text = g_strdup(t + 2); /* XXX whacky */
741 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
743 f = ensure_uri_scheme(t);
744 webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
749 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
750 gtk_entry_set_text(GTK_ENTRY(c->location),
751 (t == NULL ? __NAME__ : t));
760 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
762 struct Client *c = (struct Client *)data;
766 if (key_common(widget, event, data))
769 if (event->type == GDK_KEY_PRESS)
771 if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
773 webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
774 gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
777 else if (event->type == GDK_BUTTON_PRESS)
779 switch (((GdkEventButton *)event)->button)
782 if (c->hover_uri != NULL)
784 client_new(c->hover_uri, NULL, TRUE);
789 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
792 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
796 else if (event->type == GDK_SCROLL)
798 if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
799 ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
801 gdk_event_get_scroll_deltas(event, &dx, &dy);
802 z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
804 z = dx != 0 ? global_zoom : z;
805 webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
817 GIOChannel *channel = NULL;
818 gchar *path = NULL, *buf = NULL;
819 gchar **tokens = NULL;
821 keywords = g_hash_table_new(g_str_hash, g_str_equal);
823 path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
825 channel = g_io_channel_new_file(path, "r", &err);
828 while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
829 == G_IO_STATUS_NORMAL)
834 tokens = g_strsplit(buf, " ", 2);
835 if (tokens[0] != NULL && tokens[1] != NULL)
836 g_hash_table_insert(keywords, g_strdup(tokens[0]),
837 g_strdup(tokens[1]));
842 g_io_channel_shutdown(channel, FALSE, NULL);
848 keywords_try_search(WebKitWebView *web_view, const gchar *t)
850 gboolean ret = FALSE;
851 gchar **tokens = NULL;
852 gchar *val = NULL, *uri = NULL;
854 tokens = g_strsplit(t, " ", 2);
855 if (tokens[0] != NULL && tokens[1] != NULL)
857 val = g_hash_table_lookup(keywords, tokens[0]);
860 uri = g_strdup_printf((gchar *)val, tokens[1]);
861 webkit_web_view_load_uri(web_view, uri);
872 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
873 WebKitHitTestResult *ht, gpointer data)
875 struct Client *c = (struct Client *)data;
876 GtkAction *action = NULL;
877 WebKitContextMenuItem *mi = NULL;
878 const gchar *uri = NULL;
882 if (webkit_hit_test_result_context_is_link(ht))
883 uri = webkit_hit_test_result_get_link_uri(ht);
884 else if (webkit_hit_test_result_context_is_image(ht))
885 uri = webkit_hit_test_result_get_image_uri(ht);
886 else if (webkit_hit_test_result_context_is_media(ht))
887 uri = webkit_hit_test_result_get_media_uri(ht);
891 webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
893 if (c->external_handler_uri != NULL)
894 g_free(c->external_handler_uri);
895 c->external_handler_uri = g_strdup(uri);
896 action = gtk_action_new("external_handler", "Open with external handler",
898 g_signal_connect(G_OBJECT(action), "activate",
899 G_CALLBACK(external_handler_run), data);
900 mi = webkit_context_menu_item_new(action);
901 webkit_context_menu_append(menu, mi);
904 /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
909 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
913 g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
917 client_new(uri, NULL, TRUE);
924 search(gpointer data, gint direction)
926 struct Client *c = (struct Client *)data;
927 WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
928 WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
930 if (search_text == NULL)
936 webkit_find_controller_search(fc, search_text,
937 WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
938 WEBKIT_FIND_OPTIONS_WRAP_AROUND,
942 webkit_find_controller_search_next(fc);
945 webkit_find_controller_search_previous(fc);
951 show_web_view(WebKitWebView *web_view, gpointer data)
953 struct Client *c = (struct Client *)data;
957 gtk_widget_grab_focus(c->web_view);
958 gtk_widget_show_all(c->win);
965 GIOChannel *tabbed_stdout_channel;
967 gchar *output = NULL;
968 char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
971 if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
972 NULL, NULL, NULL, &tabbed_stdout, NULL,
975 fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
980 tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
981 if (tabbed_stdout_channel == NULL)
983 fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
986 g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
987 g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
990 fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
994 plug_into = strtol(output, NULL, 16);
997 fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
1002 trust_user_certs(WebKitWebContext *wc)
1004 GTlsCertificate *cert;
1005 const gchar *basedir, *file, *absfile;
1008 basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1009 dir = g_dir_open(basedir, 0, NULL);
1012 file = g_dir_read_name(dir);
1013 while (file != NULL)
1015 absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1017 cert = g_tls_certificate_new_from_file(absfile, NULL);
1019 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1021 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1022 file = g_dir_read_name(dir);
1030 main(int argc, char **argv)
1035 gtk_init(&argc, &argv);
1036 webkit_web_context_set_process_model(webkit_web_context_get_default(),
1037 WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1039 grab_environment_configuration();
1041 while ((opt = getopt(argc, argv, "e:CT")) != -1)
1046 embed = atol(optarg);
1047 tabbed_automagic = FALSE;
1050 cooperative_instances = FALSE;
1053 tabbed_automagic = FALSE;
1056 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1062 if (cooperative_instances)
1063 cooperation_setup();
1064 downloadmanager_setup();
1066 if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1067 embed = tabbed_launch();
1069 if (!cooperative_instances || cooperative_alone)
1071 c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1073 webkit_web_context_set_web_extensions_directory(
1074 webkit_web_context_get_default(), c
1079 client_new(home_uri, NULL, TRUE);
1082 for (i = optind; i < argc; i++)
1083 client_new(argv[i], NULL, TRUE);
1086 if (!cooperative_instances || cooperative_alone)