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