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