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