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