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