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