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