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