]> git.armaanb.net Git - chorizo.git/blob - browser.c
112c698406472393ad2204a7d284fb1202a72c8e
[chorizo.git] / browser.c
1 #include <limits.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <string.h>
8
9 #include <gtk/gtk.h>
10 #include <gtk/gtkx.h>
11 #include <gdk/gdkkeysyms.h>
12 #include <gio/gio.h>
13 #include <webkit2/webkit2.h>
14
15
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 *,
20                                          gpointer);
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 crashed_web_view_reload(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 data);
33 static void downloadmanager_setup(void);
34 static gchar *ensure_uri_scheme(const gchar *);
35 static void external_handler_run(GtkAction *, gpointer);
36 static void grab_environment_configuration(void);
37 static void hover_web_view(WebKitWebView *, WebKitHitTestResult *, guint, gpointer);
38 static gboolean key_common(GtkWidget *, GdkEvent *, gpointer);
39 static gboolean key_downloadmanager(GtkWidget *, GdkEvent *, gpointer);
40 static gboolean key_location(GtkWidget *, GdkEvent *, gpointer);
41 static gboolean key_web_view(GtkWidget *, GdkEvent *, gpointer);
42 static void keywords_load(void);
43 static gboolean keywords_try_search(WebKitWebView *, const gchar *);
44 static gboolean menu_web_view(WebKitWebView *, WebKitContextMenu *, GdkEvent *,
45                               WebKitHitTestResult *, gpointer);
46 static gboolean remote_msg(GIOChannel *, GIOCondition, gpointer);
47 static void search(gpointer, gint);
48 static void show_web_view(WebKitWebView *, gpointer);
49 static Window tabbed_launch(void);
50 static void trust_user_certs(WebKitWebContext *);
51
52
53 struct Client
54 {
55     gchar *external_handler_uri;
56     gchar *hover_uri;
57     GtkWidget *location;
58     GtkWidget *vbox;
59     GtkWidget *web_view;
60     GtkWidget *win;
61 };
62
63 struct DownloadManager
64 {
65     GtkWidget *scroll;
66     GtkWidget *toolbar;
67     GtkWidget *win;
68 } dm;
69
70
71 static const gchar *accepted_language[2] = { NULL, NULL };
72 static gint clients = 0;
73 static gboolean cooperative_alone = TRUE;
74 static gboolean cooperative_instances = TRUE;
75 static int cooperative_pipe_fp = 0;
76 static int crash_autoreload_delay = 2;
77 static gchar *download_dir = "/var/tmp";
78 static Window embed = 0;
79 static gchar *fifo_suffix = "main";
80 static gdouble global_zoom = 1.0;
81 static gchar *history_file = NULL;
82 static gchar *home_uri = "about:blank";
83 static gboolean initial_wc_setup_done = FALSE;
84 static GHashTable *keywords = NULL;
85 static gchar *search_text = NULL;
86 static gboolean tabbed_automagic = TRUE;
87 static gchar *user_agent = NULL;
88
89
90 void
91 client_destroy(GtkWidget *obj, gpointer data)
92 {
93     struct Client *c = (struct Client *)data;
94
95     g_signal_handlers_disconnect_by_func(G_OBJECT(c->web_view),
96                                          changed_load_progress, c);
97
98     free(c);
99     clients--;
100
101     if (clients == 0)
102         gtk_main_quit();
103 }
104
105 gboolean
106 client_destroy_request(WebKitWebView *web_view, gpointer data)
107 {
108     struct Client *c = (struct Client *)data;
109
110     gtk_widget_destroy(c->win);
111
112     return TRUE;
113 }
114
115 WebKitWebView *
116 client_new(const gchar *uri, WebKitWebView *related_wv, gboolean show)
117 {
118     struct Client *c;
119     WebKitWebContext *wc;
120     gchar *f;
121
122     if (uri != NULL && cooperative_instances && !cooperative_alone)
123     {
124         f = ensure_uri_scheme(uri);
125         write(cooperative_pipe_fp, f, strlen(f));
126         write(cooperative_pipe_fp, "\n", 1);
127         g_free(f);
128         return NULL;
129     }
130
131     c = calloc(1, sizeof(struct Client));
132     if (!c)
133     {
134         fprintf(stderr, __NAME__": fatal: calloc failed\n");
135         exit(EXIT_FAILURE);
136     }
137
138     if (embed != 0)
139     {
140         c->win = gtk_plug_new(embed);
141         if (!gtk_plug_get_embedded(GTK_PLUG(c->win)))
142         {
143             fprintf(stderr, __NAME__": Can't plug-in to XID %ld.\n", embed);
144             gtk_widget_destroy(c->win);
145             c->win = NULL;
146             embed = 0;
147         }
148     }
149
150     if (c->win == NULL)
151         c->win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
152
153     gtk_window_set_default_size(GTK_WINDOW(c->win), 800, 600);
154
155     g_signal_connect(G_OBJECT(c->win), "destroy", G_CALLBACK(client_destroy), c);
156     gtk_window_set_title(GTK_WINDOW(c->win), __NAME__);
157
158     if (related_wv == NULL)
159         c->web_view = webkit_web_view_new();
160     else
161         c->web_view = webkit_web_view_new_with_related_view(related_wv);
162     wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
163
164     webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), global_zoom);
165     g_signal_connect(G_OBJECT(c->web_view), "notify::title",
166                      G_CALLBACK(changed_title), c);
167     g_signal_connect(G_OBJECT(c->web_view), "notify::uri",
168                      G_CALLBACK(changed_uri), c);
169     g_signal_connect(G_OBJECT(c->web_view), "notify::estimated-load-progress",
170                      G_CALLBACK(changed_load_progress), c);
171     g_signal_connect(G_OBJECT(c->web_view), "create",
172                      G_CALLBACK(client_new_request), NULL);
173     g_signal_connect(G_OBJECT(c->web_view), "context-menu",
174                      G_CALLBACK(menu_web_view), c);
175     g_signal_connect(G_OBJECT(c->web_view), "close",
176                      G_CALLBACK(client_destroy_request), c);
177     g_signal_connect(G_OBJECT(c->web_view), "decide-policy",
178                      G_CALLBACK(decide_policy), NULL);
179     g_signal_connect(G_OBJECT(c->web_view), "key-press-event",
180                      G_CALLBACK(key_web_view), c);
181     g_signal_connect(G_OBJECT(c->web_view), "button-press-event",
182                      G_CALLBACK(key_web_view), c);
183     g_signal_connect(G_OBJECT(c->web_view), "scroll-event",
184                      G_CALLBACK(key_web_view), c);
185     g_signal_connect(G_OBJECT(c->web_view), "mouse-target-changed",
186                      G_CALLBACK(hover_web_view), c);
187     g_signal_connect(G_OBJECT(c->web_view), "web-process-crashed",
188                      G_CALLBACK(crashed_web_view), c);
189
190     if (!initial_wc_setup_done)
191     {
192         if (accepted_language[0] != NULL)
193             webkit_web_context_set_preferred_languages(wc, accepted_language);
194
195         g_signal_connect(G_OBJECT(wc), "download-started",
196                          G_CALLBACK(download_handle_start), NULL);
197
198         trust_user_certs(wc);
199
200         initial_wc_setup_done = TRUE;
201     }
202
203     if (user_agent != NULL)
204         g_object_set(G_OBJECT(webkit_web_view_get_settings(WEBKIT_WEB_VIEW(c->web_view))),
205                      "user-agent", user_agent, NULL);
206
207     c->location = gtk_entry_new();
208     g_signal_connect(G_OBJECT(c->location), "key-press-event",
209                      G_CALLBACK(key_location), c);
210
211     c->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
212     gtk_box_pack_start(GTK_BOX(c->vbox), c->location, FALSE, FALSE, 0);
213     gtk_box_pack_start(GTK_BOX(c->vbox), c->web_view, TRUE, TRUE, 0);
214
215     gtk_container_add(GTK_CONTAINER(c->win), c->vbox);
216
217     if (show)
218         show_web_view(NULL, c);
219     else
220         g_signal_connect(G_OBJECT(c->web_view), "ready-to-show",
221                          G_CALLBACK(show_web_view), c);
222
223     if (uri != NULL)
224     {
225         f = ensure_uri_scheme(uri);
226         webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
227         g_free(f);
228     }
229
230     clients++;
231
232     return WEBKIT_WEB_VIEW(c->web_view);
233 }
234
235 WebKitWebView *
236 client_new_request(WebKitWebView *web_view,
237                    WebKitNavigationAction *navigation_action, gpointer data)
238 {
239     return client_new(NULL, web_view, FALSE);
240 }
241
242 void
243 cooperation_setup(void)
244 {
245     GIOChannel *towatch;
246     gchar *fifofilename, *fifopath;
247
248     fifofilename = g_strdup_printf("%s-%s", __NAME__".fifo", fifo_suffix);
249     fifopath = g_build_filename(g_get_user_runtime_dir(), fifofilename, NULL);
250     g_free(fifofilename);
251
252     if (!g_file_test(fifopath, G_FILE_TEST_EXISTS))
253         mkfifo(fifopath, 0600);
254
255     cooperative_pipe_fp = open(fifopath, O_WRONLY | O_NONBLOCK);
256     if (!cooperative_pipe_fp)
257     {
258         fprintf(stderr, __NAME__": Can't open FIFO at all.\n");
259     }
260     else
261     {
262         if (write(cooperative_pipe_fp, "", 0) == -1)
263         {
264             /* Could not do an empty write to the FIFO which means there's
265              * no one listening. */
266             close(cooperative_pipe_fp);
267             towatch = g_io_channel_new_file(fifopath, "r+", NULL);
268             g_io_add_watch(towatch, G_IO_IN, (GIOFunc)remote_msg, NULL);
269         }
270         else
271             cooperative_alone = FALSE;
272     }
273
274     g_free(fifopath);
275 }
276
277 void
278 changed_download_progress(GObject *obj, GParamSpec *pspec, gpointer data)
279 {
280     WebKitDownload *download = WEBKIT_DOWNLOAD(obj);
281     WebKitURIResponse *resp;
282     GtkToolItem *tb = GTK_TOOL_ITEM(data);
283     gdouble p, size_mb;
284     const gchar *uri;
285     gchar *t, *filename, *base;
286
287     p = webkit_download_get_estimated_progress(download);
288     p = p > 1 ? 1 : p;
289     p = p < 0 ? 0 : p;
290     p *= 100;
291     resp = webkit_download_get_response(download);
292     size_mb = webkit_uri_response_get_content_length(resp) / 1e6;
293
294     uri = webkit_download_get_destination(download);
295     filename = g_filename_from_uri(uri, NULL, NULL);
296     if (filename == NULL)
297     {
298         /* This really should not happen because WebKit uses that URI to
299          * write to a file... */
300         fprintf(stderr, __NAME__": Could not construct file name from URI!\n");
301         t = g_strdup_printf("%s (%.0f%% of %.1f MB)",
302                             webkit_uri_response_get_uri(resp), p, size_mb);
303     }
304     else
305     {
306         base = g_path_get_basename(filename);
307         t = g_strdup_printf("%s (%.0f%% of %.1f MB)", base, p, size_mb);
308         g_free(filename);
309         g_free(base);
310     }
311     gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), t);
312     g_free(t);
313 }
314
315 void
316 changed_load_progress(GObject *obj, GParamSpec *pspec, gpointer data)
317 {
318     struct Client *c = (struct Client *)data;
319     gdouble p;
320
321     p = webkit_web_view_get_estimated_load_progress(WEBKIT_WEB_VIEW(c->web_view));
322     gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), p);
323 }
324
325 void
326 changed_title(GObject *obj, GParamSpec *pspec, gpointer data)
327 {
328     const gchar *t, *u;
329     struct Client *c = (struct Client *)data;
330
331     u = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
332     t = webkit_web_view_get_title(WEBKIT_WEB_VIEW(c->web_view));
333
334     u = u == NULL ? __NAME__ : u;
335     u = u[0] == 0 ? __NAME__ : u;
336
337     t = t == NULL ? u : t;
338     t = t[0] == 0 ? u : t;
339
340     gtk_window_set_title(GTK_WINDOW(c->win), t);
341 }
342
343 void
344 changed_uri(GObject *obj, GParamSpec *pspec, gpointer data)
345 {
346     const gchar *t;
347     struct Client *c = (struct Client *)data;
348     FILE *fp;
349
350     t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
351     gtk_entry_set_text(GTK_ENTRY(c->location), (t == NULL ? __NAME__ : t));
352
353     if (t != NULL && history_file != NULL)
354     {
355         fp = fopen(history_file, "a");
356         if (fp != NULL)
357         {
358             fprintf(fp, "%s\n", t);
359             fclose(fp);
360         }
361         else
362             perror(__NAME__": Error opening history file");
363     }
364 }
365
366 gboolean
367 crashed_web_view(WebKitWebView *web_view, gpointer data)
368 {
369     fprintf(stderr, __NAME__": WebView crashed!\n");
370     if (crash_autoreload_delay >= 1)
371     {
372         fprintf(stderr, __NAME__": Reloading WebView in %d seconds.\n",
373                 crash_autoreload_delay);
374         g_timeout_add_seconds(crash_autoreload_delay, crashed_web_view_reload,
375                               web_view);
376     }
377
378     return TRUE;
379 }
380
381 gboolean
382 crashed_web_view_reload(gpointer data)
383 {
384     webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(data));
385
386     return G_SOURCE_REMOVE;
387 }
388
389 gboolean
390 decide_policy(WebKitWebView *web_view, WebKitPolicyDecision *decision,
391               WebKitPolicyDecisionType type, gpointer data)
392 {
393     WebKitResponsePolicyDecision *r;
394
395     switch (type)
396     {
397         case WEBKIT_POLICY_DECISION_TYPE_RESPONSE:
398             r = WEBKIT_RESPONSE_POLICY_DECISION(decision);
399             if (!webkit_response_policy_decision_is_mime_type_supported(r))
400                 webkit_policy_decision_download(decision);
401             else
402                 webkit_policy_decision_use(decision);
403             break;
404         default:
405             /* Use whatever default there is. */
406             return FALSE;
407     }
408     return TRUE;
409 }
410
411 void
412 download_handle_start(WebKitWebView *web_view, WebKitDownload *download,
413                       gpointer data)
414 {
415     g_signal_connect(G_OBJECT(download), "decide-destination",
416                      G_CALLBACK(download_handle), data);
417 }
418
419 gboolean
420 download_handle(WebKitDownload *download, gchar *suggested_filename, gpointer data)
421 {
422     gchar *sug_clean, *path, *path2 = NULL, *uri;
423     GtkToolItem *tb;
424     int suffix = 1;
425     size_t i;
426
427     sug_clean = g_strdup(suggested_filename);
428     for (i = 0; i < strlen(sug_clean); i++)
429         if (sug_clean[i] == G_DIR_SEPARATOR)
430             sug_clean[i] = '_';
431
432     path = g_build_filename(download_dir, sug_clean, NULL);
433     path2 = g_strdup(path);
434     while (g_file_test(path2, G_FILE_TEST_EXISTS) && suffix < 1000)
435     {
436         g_free(path2);
437
438         path2 = g_strdup_printf("%s.%d", path, suffix);
439         suffix++;
440     }
441
442     if (suffix == 1000)
443     {
444         fprintf(stderr, __NAME__": Suffix reached limit for download.\n");
445         webkit_download_cancel(download);
446     }
447     else
448     {
449         uri = g_filename_to_uri(path2, NULL, NULL);
450         webkit_download_set_destination(download, uri);
451         g_free(uri);
452
453         tb = gtk_tool_button_new(NULL, NULL);
454         gtk_tool_button_set_icon_name(GTK_TOOL_BUTTON(tb), "gtk-delete");
455         gtk_tool_button_set_label(GTK_TOOL_BUTTON(tb), sug_clean);
456         gtk_toolbar_insert(GTK_TOOLBAR(dm.toolbar), tb, 0);
457         gtk_widget_show_all(dm.win);
458
459         g_signal_connect(G_OBJECT(download), "notify::estimated-progress",
460                          G_CALLBACK(changed_download_progress), tb);
461
462         g_object_ref(download);
463         g_signal_connect(G_OBJECT(tb), "clicked",
464                          G_CALLBACK(downloadmanager_cancel), download);
465     }
466
467     g_free(sug_clean);
468     g_free(path);
469     g_free(path2);
470
471     /* Propagate -- to whom it may concern. */
472     return FALSE;
473 }
474
475 void
476 downloadmanager_cancel(GtkToolButton *tb, gpointer data)
477 {
478     WebKitDownload *download = WEBKIT_DOWNLOAD(data);
479
480     webkit_download_cancel(download);
481     g_object_unref(download);
482
483     gtk_widget_destroy(GTK_WIDGET(tb));
484 }
485
486 void
487 downloadmanager_setup(void)
488 {
489     dm.win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
490     gtk_window_set_type_hint(GTK_WINDOW(dm.win), GDK_WINDOW_TYPE_HINT_DIALOG);
491     gtk_window_set_default_size(GTK_WINDOW(dm.win), 500, 250);
492     gtk_window_set_title(GTK_WINDOW(dm.win), __NAME__" - Download Manager");
493     g_signal_connect(G_OBJECT(dm.win), "delete-event",
494                      G_CALLBACK(gtk_widget_hide_on_delete), NULL);
495     g_signal_connect(G_OBJECT(dm.win), "key-press-event",
496                      G_CALLBACK(key_downloadmanager), NULL);
497
498     dm.toolbar = gtk_toolbar_new();
499     gtk_orientable_set_orientation(GTK_ORIENTABLE(dm.toolbar),
500                                    GTK_ORIENTATION_VERTICAL);
501     gtk_toolbar_set_style(GTK_TOOLBAR(dm.toolbar), GTK_TOOLBAR_BOTH_HORIZ);
502     gtk_toolbar_set_show_arrow(GTK_TOOLBAR(dm.toolbar), FALSE);
503
504     dm.scroll = gtk_scrolled_window_new(NULL, NULL);
505     gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(dm.scroll),
506                                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
507     gtk_container_add(GTK_CONTAINER(dm.scroll), dm.toolbar);
508
509     gtk_container_add(GTK_CONTAINER(dm.win), dm.scroll);
510 }
511
512 gchar *
513 ensure_uri_scheme(const gchar *t)
514 {
515     gchar *f, *fabs;
516
517     f = g_ascii_strdown(t, -1);
518     if (!g_str_has_prefix(f, "http:") &&
519         !g_str_has_prefix(f, "https:") &&
520         !g_str_has_prefix(f, "file:") &&
521         !g_str_has_prefix(f, "about:"))
522     {
523         g_free(f);
524         fabs = realpath(t, NULL);
525         if (fabs != NULL)
526         {
527             f = g_strdup_printf("file://%s", fabs);
528             free(fabs);
529         }
530         else
531             f = g_strdup_printf("http://%s", t);
532         return f;
533     }
534     else
535         return g_strdup(t);
536 }
537
538 void
539 external_handler_run(GtkAction *action, gpointer data)
540 {
541     struct Client *c = (struct Client *)data;
542     gchar *argv[] = { "lariza-external-handler", "-u", NULL, NULL };
543     GPid pid;
544     GError *err = NULL;
545
546     (void)action;
547
548     argv[2] = c->external_handler_uri;
549     if (!g_spawn_async(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL,
550                        &pid, &err))
551     {
552         fprintf(stderr, __NAME__": Could not launch key handler: %s\n",
553                 err->message);
554         g_error_free(err);
555     }
556     else
557         g_spawn_close_pid(pid);
558 }
559
560 void
561 grab_environment_configuration(void)
562 {
563     const gchar *e;
564
565     e = g_getenv(__NAME_UPPERCASE__"_ACCEPTED_LANGUAGE");
566     if (e != NULL)
567         accepted_language[0] = g_strdup(e);
568
569     e = g_getenv(__NAME_UPPERCASE__"_CRASH_AUTORELOAD_DELAY");
570     if (e != NULL)
571         crash_autoreload_delay = atoi(e);
572
573     e = g_getenv(__NAME_UPPERCASE__"_DOWNLOAD_DIR");
574     if (e != NULL)
575         download_dir = g_strdup(e);
576
577     e = g_getenv(__NAME_UPPERCASE__"_FIFO_SUFFIX");
578     if (e != NULL)
579         fifo_suffix = g_strdup(e);
580
581     e = g_getenv(__NAME_UPPERCASE__"_HISTORY_FILE");
582     if (e != NULL)
583         history_file = g_strdup(e);
584
585     e = g_getenv(__NAME_UPPERCASE__"_HOME_URI");
586     if (e != NULL)
587         home_uri = g_strdup(e);
588
589     e = g_getenv(__NAME_UPPERCASE__"_USER_AGENT");
590     if (e != NULL)
591         user_agent = g_strdup(e);
592
593     e = g_getenv(__NAME_UPPERCASE__"_ZOOM");
594     if (e != NULL)
595         global_zoom = atof(e);
596 }
597
598 void
599 hover_web_view(WebKitWebView *web_view, WebKitHitTestResult *ht, guint modifiers,
600                gpointer data)
601 {
602     struct Client *c = (struct Client *)data;
603
604     if (!gtk_widget_is_focus(c->location))
605     {
606         if (webkit_hit_test_result_context_is_link(ht))
607         {
608             gtk_entry_set_text(GTK_ENTRY(c->location),
609                                webkit_hit_test_result_get_link_uri(ht));
610
611             if (c->hover_uri != NULL)
612                 g_free(c->hover_uri);
613             c->hover_uri = g_strdup(webkit_hit_test_result_get_link_uri(ht));
614         }
615         else
616         {
617             gtk_entry_set_text(GTK_ENTRY(c->location),
618                                webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
619
620             if (c->hover_uri != NULL)
621                 g_free(c->hover_uri);
622             c->hover_uri = NULL;
623         }
624     }
625 }
626
627 gboolean
628 key_common(GtkWidget *widget, GdkEvent *event, gpointer data)
629 {
630     struct Client *c = (struct Client *)data;
631     WebKitWebContext *wc = webkit_web_view_get_context(WEBKIT_WEB_VIEW(c->web_view));
632     gchar *f;
633
634     if (event->type == GDK_KEY_PRESS)
635     {
636         if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
637         {
638             switch (((GdkEventKey *)event)->keyval)
639             {
640                 case GDK_KEY_q:  /* close window (left hand) */
641                     gtk_widget_destroy(c->win);
642                     return TRUE;
643                 case GDK_KEY_w:  /* home (left hand) */
644                     f = ensure_uri_scheme(home_uri);
645                     webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
646                     g_free(f);
647                     return TRUE;
648                 case GDK_KEY_e:  /* new tab (left hand) */
649                     f = ensure_uri_scheme(home_uri);
650                     client_new(f, NULL, TRUE);
651                     g_free(f);
652                     return TRUE;
653                 case GDK_KEY_r:  /* reload (left hand) */
654                     webkit_web_view_reload_bypass_cache(WEBKIT_WEB_VIEW(
655                                                         c->web_view));
656                     return TRUE;
657                 case GDK_KEY_d:  /* download manager (left hand) */
658                     gtk_widget_show_all(dm.win);
659                     return TRUE;
660                 case GDK_KEY_2:  /* search forward (left hand) */
661                 case GDK_KEY_n:  /* search forward (maybe both hands) */
662                     search(c, 1);
663                     return TRUE;
664                 case GDK_KEY_3:  /* search backward (left hand) */
665                     search(c, -1);
666                     return TRUE;
667                 case GDK_KEY_l:  /* location (BOTH hands) */
668                     gtk_widget_grab_focus(c->location);
669                     return TRUE;
670                 case GDK_KEY_k:  /* initiate search (BOTH hands) */
671                     gtk_widget_grab_focus(c->location);
672                     gtk_entry_set_text(GTK_ENTRY(c->location), ":/");
673                     gtk_editable_set_position(GTK_EDITABLE(c->location), -1);
674                     return TRUE;
675                 case GDK_KEY_c:  /* reload trusted certs (left hand) */
676                     trust_user_certs(wc);
677                     return TRUE;
678                 case GDK_KEY_x:  /* launch external handler (left hand) */
679                     if (c->external_handler_uri != NULL)
680                         g_free(c->external_handler_uri);
681                     c->external_handler_uri = g_strdup(
682                         webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view)));
683                     external_handler_run(NULL, c);
684                     return TRUE;
685             }
686         }
687         /* navigate backward (left hand) */
688         else if (((GdkEventKey *)event)->keyval == GDK_KEY_F2)
689         {
690             webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
691             return TRUE;
692         }
693         /* navigate forward (left hand) */
694         else if (((GdkEventKey *)event)->keyval == GDK_KEY_F3)
695         {
696             webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
697             return TRUE;
698         }
699     }
700
701     return FALSE;
702 }
703
704 gboolean
705 key_downloadmanager(GtkWidget *widget, GdkEvent *event, gpointer data)
706 {
707     if (event->type == GDK_KEY_PRESS)
708     {
709         if (((GdkEventKey *)event)->state & GDK_MOD1_MASK)
710         {
711             switch (((GdkEventKey *)event)->keyval)
712             {
713                 case GDK_KEY_d:  /* close window (left hand) */
714                     gtk_widget_hide(dm.win);
715                     return TRUE;
716             }
717         }
718     }
719
720     return FALSE;
721 }
722
723 gboolean
724 key_location(GtkWidget *widget, GdkEvent *event, gpointer data)
725 {
726     struct Client *c = (struct Client *)data;
727     const gchar *t;
728     gchar *f;
729
730     if (key_common(widget, event, data))
731         return TRUE;
732
733     if (event->type == GDK_KEY_PRESS)
734     {
735         switch (((GdkEventKey *)event)->keyval)
736         {
737             case GDK_KEY_KP_Enter:
738             case GDK_KEY_Return:
739                 gtk_widget_grab_focus(c->web_view);
740                 t = gtk_entry_get_text(GTK_ENTRY(c->location));
741                 if (t != NULL && t[0] == ':' && t[1] == '/')
742                 {
743                     if (search_text != NULL)
744                         g_free(search_text);
745                     search_text = g_strdup(t + 2);  /* XXX whacky */
746                     search(c, 0);
747                 }
748                 else if (!keywords_try_search(WEBKIT_WEB_VIEW(c->web_view), t))
749                 {
750                     f = ensure_uri_scheme(t);
751                     webkit_web_view_load_uri(WEBKIT_WEB_VIEW(c->web_view), f);
752                     g_free(f);
753                 }
754                 return TRUE;
755             case GDK_KEY_Escape:
756                 t = webkit_web_view_get_uri(WEBKIT_WEB_VIEW(c->web_view));
757                 gtk_entry_set_text(GTK_ENTRY(c->location),
758                                    (t == NULL ? __NAME__ : t));
759                 return TRUE;
760         }
761     }
762
763     return FALSE;
764 }
765
766 gboolean
767 key_web_view(GtkWidget *widget, GdkEvent *event, gpointer data)
768 {
769     struct Client *c = (struct Client *)data;
770     gdouble dx, dy;
771     gfloat z;
772
773     if (key_common(widget, event, data))
774         return TRUE;
775
776     if (event->type == GDK_KEY_PRESS)
777     {
778         if (((GdkEventKey *)event)->keyval == GDK_KEY_Escape)
779         {
780             webkit_web_view_stop_loading(WEBKIT_WEB_VIEW(c->web_view));
781             gtk_entry_set_progress_fraction(GTK_ENTRY(c->location), 0);
782         }
783     }
784     else if (event->type == GDK_BUTTON_PRESS)
785     {
786         switch (((GdkEventButton *)event)->button)
787         {
788             case 2:
789                 if (c->hover_uri != NULL)
790                 {
791                     client_new(c->hover_uri, NULL, TRUE);
792                     return TRUE;
793                 }
794                 break;
795             case 8:
796                 webkit_web_view_go_back(WEBKIT_WEB_VIEW(c->web_view));
797                 return TRUE;
798             case 9:
799                 webkit_web_view_go_forward(WEBKIT_WEB_VIEW(c->web_view));
800                 return TRUE;
801         }
802     }
803     else if (event->type == GDK_SCROLL)
804     {
805         if (((GdkEventScroll *)event)->state & GDK_MOD1_MASK ||
806             ((GdkEventScroll *)event)->state & GDK_CONTROL_MASK)
807         {
808             gdk_event_get_scroll_deltas(event, &dx, &dy);
809             z = webkit_web_view_get_zoom_level(WEBKIT_WEB_VIEW(c->web_view));
810             z += -dy * 0.1;
811             z = dx != 0 ? global_zoom : z;
812             webkit_web_view_set_zoom_level(WEBKIT_WEB_VIEW(c->web_view), z);
813             return TRUE;
814         }
815     }
816
817     return FALSE;
818 }
819
820 void
821 keywords_load(void)
822 {
823     GError *err = NULL;
824     GIOChannel *channel = NULL;
825     gchar *path = NULL, *buf = NULL;
826     gchar **tokens = NULL;
827
828     keywords = g_hash_table_new(g_str_hash, g_str_equal);
829
830     path = g_build_filename(g_get_user_config_dir(), __NAME__, "keywordsearch",
831                             NULL);
832     channel = g_io_channel_new_file(path, "r", &err);
833     if (channel != NULL)
834     {
835         while (g_io_channel_read_line(channel, &buf, NULL, NULL, NULL)
836                == G_IO_STATUS_NORMAL)
837         {
838             g_strstrip(buf);
839             if (buf[0] != '#')
840             {
841                 tokens = g_strsplit(buf, " ", 2);
842                 if (tokens[0] != NULL && tokens[1] != NULL)
843                     g_hash_table_insert(keywords, g_strdup(tokens[0]),
844                                         g_strdup(tokens[1]));
845                 g_strfreev(tokens);
846             }
847             g_free(buf);
848         }
849         g_io_channel_shutdown(channel, FALSE, NULL);
850     }
851     g_free(path);
852 }
853
854 gboolean
855 keywords_try_search(WebKitWebView *web_view, const gchar *t)
856 {
857     gboolean ret = FALSE;
858     gchar **tokens = NULL;
859     gchar *val = NULL, *uri = NULL;
860
861     tokens = g_strsplit(t, " ", 2);
862     if (tokens[0] != NULL && tokens[1] != NULL)
863     {
864         val = g_hash_table_lookup(keywords, tokens[0]);
865         if (val != NULL)
866         {
867             uri = g_strdup_printf((gchar *)val, tokens[1]);
868             webkit_web_view_load_uri(web_view, uri);
869             g_free(uri);
870             ret = TRUE;
871         }
872     }
873     g_strfreev(tokens);
874
875     return ret;
876 }
877
878 gboolean
879 menu_web_view(WebKitWebView *web_view, WebKitContextMenu *menu, GdkEvent *ev,
880               WebKitHitTestResult *ht, gpointer data)
881 {
882     struct Client *c = (struct Client *)data;
883     GtkAction *action = NULL;
884     WebKitContextMenuItem *mi = NULL;
885     const gchar *uri = NULL;
886
887     (void)ev;
888
889     if (webkit_hit_test_result_context_is_link(ht))
890         uri = webkit_hit_test_result_get_link_uri(ht);
891     else if (webkit_hit_test_result_context_is_image(ht))
892         uri = webkit_hit_test_result_get_image_uri(ht);
893     else if (webkit_hit_test_result_context_is_media(ht))
894         uri = webkit_hit_test_result_get_media_uri(ht);
895
896     if (uri != NULL)
897     {
898         webkit_context_menu_append(menu, webkit_context_menu_item_new_separator());
899
900         if (c->external_handler_uri != NULL)
901             g_free(c->external_handler_uri);
902         c->external_handler_uri = g_strdup(uri);
903         action = gtk_action_new("external_handler", "Open with external handler",
904                                 NULL, NULL);
905         g_signal_connect(G_OBJECT(action), "activate",
906                          G_CALLBACK(external_handler_run), data);
907         mi = webkit_context_menu_item_new(action);
908         webkit_context_menu_append(menu, mi);
909     }
910
911     /* FALSE = Show the menu. (TRUE = Don't ever show it.) */
912     return FALSE;
913 }
914
915 gboolean
916 remote_msg(GIOChannel *channel, GIOCondition condition, gpointer data)
917 {
918     gchar *uri = NULL;
919
920     g_io_channel_read_line(channel, &uri, NULL, NULL, NULL);
921     if (uri)
922     {
923         g_strstrip(uri);
924         client_new(uri, NULL, TRUE);
925         g_free(uri);
926     }
927     return TRUE;
928 }
929
930 void
931 search(gpointer data, gint direction)
932 {
933     struct Client *c = (struct Client *)data;
934     WebKitWebView *web_view = WEBKIT_WEB_VIEW(c->web_view);
935     WebKitFindController *fc = webkit_web_view_get_find_controller(web_view);
936
937     if (search_text == NULL)
938         return;
939
940     switch (direction)
941     {
942         case 0:
943             webkit_find_controller_search(fc, search_text,
944                                           WEBKIT_FIND_OPTIONS_CASE_INSENSITIVE |
945                                           WEBKIT_FIND_OPTIONS_WRAP_AROUND,
946                                           G_MAXUINT);
947             break;
948         case 1:
949             webkit_find_controller_search_next(fc);
950             break;
951         case -1:
952             webkit_find_controller_search_previous(fc);
953             break;
954     }
955 }
956
957 void
958 show_web_view(WebKitWebView *web_view, gpointer data)
959 {
960     struct Client *c = (struct Client *)data;
961
962     (void)web_view;
963
964     gtk_widget_grab_focus(c->web_view);
965     gtk_widget_show_all(c->win);
966 }
967
968 Window
969 tabbed_launch(void)
970 {
971     gint tabbed_stdout;
972     GIOChannel *tabbed_stdout_channel;
973     GError *err = NULL;
974     gchar *output = NULL;
975     char *argv[] = { "tabbed", "-c", "-d", "-p", "s1", "-n", __NAME__, NULL };
976     Window plug_into;
977
978     if (!g_spawn_async_with_pipes(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL,
979                                   NULL, NULL, NULL, &tabbed_stdout, NULL,
980                                   &err))
981     {
982         fprintf(stderr, __NAME__": Could not launch tabbed: %s\n", err->message);
983         g_error_free(err);
984         return 0;
985     }
986
987     tabbed_stdout_channel = g_io_channel_unix_new(tabbed_stdout);
988     if (tabbed_stdout_channel == NULL)
989     {
990         fprintf(stderr, __NAME__": Could open tabbed's stdout\n");
991         return 0;
992     }
993     g_io_channel_read_line(tabbed_stdout_channel, &output, NULL, NULL, NULL);
994     g_io_channel_shutdown(tabbed_stdout_channel, FALSE, NULL);
995     if (output == NULL)
996     {
997         fprintf(stderr, __NAME__": Could not read XID from tabbed\n");
998         return 0;
999     }
1000     g_strstrip(output);
1001     plug_into = strtol(output, NULL, 16);
1002     g_free(output);
1003     if (plug_into == 0)
1004         fprintf(stderr, __NAME__": The XID from tabbed is 0\n");
1005     return plug_into;
1006 }
1007
1008 void
1009 trust_user_certs(WebKitWebContext *wc)
1010 {
1011     GTlsCertificate *cert;
1012     const gchar *basedir, *file, *absfile;
1013     GDir *dir;
1014
1015     basedir = g_build_filename(g_get_user_config_dir(), __NAME__, "certs", NULL);
1016     dir = g_dir_open(basedir, 0, NULL);
1017     if (dir != NULL)
1018     {
1019         file = g_dir_read_name(dir);
1020         while (file != NULL)
1021         {
1022             absfile = g_build_filename(g_get_user_config_dir(), __NAME__, "certs",
1023                                        file, NULL);
1024             cert = g_tls_certificate_new_from_file(absfile, NULL);
1025             if (cert == NULL)
1026                 fprintf(stderr, __NAME__": Could not load trusted cert '%s'\n", file);
1027             else
1028                 webkit_web_context_allow_tls_certificate_for_host(wc, cert, file);
1029             file = g_dir_read_name(dir);
1030         }
1031         g_dir_close(dir);
1032     }
1033 }
1034
1035
1036 int
1037 main(int argc, char **argv)
1038 {
1039     gchar *c;
1040     int opt, i;
1041
1042     gtk_init(&argc, &argv);
1043     webkit_web_context_set_process_model(webkit_web_context_get_default(),
1044         WEBKIT_PROCESS_MODEL_MULTIPLE_SECONDARY_PROCESSES);
1045
1046     grab_environment_configuration();
1047
1048     while ((opt = getopt(argc, argv, "e:CT")) != -1)
1049     {
1050         switch (opt)
1051         {
1052             case 'e':
1053                 embed = atol(optarg);
1054                 tabbed_automagic = FALSE;
1055                 break;
1056             case 'C':
1057                 cooperative_instances = FALSE;
1058                 break;
1059             case 'T':
1060                 tabbed_automagic = FALSE;
1061                 break;
1062             default:
1063                 fprintf(stderr, "Usage: "__NAME__" [OPTION]... [URI]...\n");
1064                 exit(EXIT_FAILURE);
1065         }
1066     }
1067
1068     keywords_load();
1069     if (cooperative_instances)
1070         cooperation_setup();
1071     downloadmanager_setup();
1072
1073     if (tabbed_automagic && !(cooperative_instances && !cooperative_alone))
1074         embed = tabbed_launch();
1075
1076     if (!cooperative_instances || cooperative_alone)
1077     {
1078         c = g_build_filename(g_get_user_config_dir(), __NAME__, "web_extensions",
1079                              NULL);
1080         webkit_web_context_set_web_extensions_directory(
1081             webkit_web_context_get_default(), c
1082         );
1083     }
1084
1085     if (optind >= argc)
1086         client_new(home_uri, NULL, TRUE);
1087     else
1088     {
1089         for (i = optind; i < argc; i++)
1090             client_new(argv[i], NULL, TRUE);
1091     }
1092
1093     if (!cooperative_instances || cooperative_alone)
1094         gtk_main();
1095     exit(EXIT_SUCCESS);
1096 }