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