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