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