3 * Copyright (c) 2010, 2011 Marco Peereboom <marco@peereboom.us>
4 * Copyright (c) 2011 Stevan Andjelkovic <stevan@student.chalmers.se>
5 * Copyright (c) 2010 Edd Barrett <vext01@gmail.com>
6 * Copyright (c) 2011 Todd T. Fries <todd@fries.net>
7 * Copyright (c) 2011 Raphael Graf <r@undefined.ch>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24 * multi letter commands
25 * pre and post counts for commands
26 * autocompletion on various inputs
27 * create privacy browsing
28 * - encrypted local data
44 #include <sys/types.h>
46 #if defined(__linux__)
47 #include "linux/util.h"
48 #include "linux/tree.h"
49 #elif defined(__FreeBSD__)
51 #include "freebsd/util.h"
57 #include <sys/queue.h>
59 #include <sys/socket.h>
63 #include <gdk/gdkkeysyms.h>
64 #include <webkit/webkit.h>
65 #include <libsoup/soup.h>
66 #include <gnutls/gnutls.h>
67 #include <JavaScriptCore/JavaScript.h>
68 #include <gnutls/x509.h>
70 #include "javascript.h"
73 javascript.h borrowed from vimprobable2 under the following license:
75 Copyright (c) 2009 Leon Winter
76 Copyright (c) 2009 Hannes Schueller
77 Copyright (c) 2009 Matto Fransen
79 Permission is hereby granted, free of charge, to any person obtaining a copy
80 of this software and associated documentation files (the "Software"), to deal
81 in the Software without restriction, including without limitation the rights
82 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
83 copies of the Software, and to permit persons to whom the Software is
84 furnished to do so, subject to the following conditions:
86 The above copyright notice and this permission notice shall be included in
87 all copies or substantial portions of the Software.
89 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
90 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
91 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
92 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
93 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
94 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
98 static char *version
= "$xxxterm$";
100 /* hooked functions */
101 void (*_soup_cookie_jar_add_cookie
)(SoupCookieJar
*, SoupCookie
*);
102 void (*_soup_cookie_jar_delete_cookie
)(SoupCookieJar
*,
107 #define DPRINTF(x...) do { if (swm_debug) fprintf(stderr, x); } while (0)
108 #define DNPRINTF(n,x...) do { if (swm_debug & n) fprintf(stderr, x); } while (0)
109 #define XT_D_MOVE 0x0001
110 #define XT_D_KEY 0x0002
111 #define XT_D_TAB 0x0004
112 #define XT_D_URL 0x0008
113 #define XT_D_CMD 0x0010
114 #define XT_D_NAV 0x0020
115 #define XT_D_DOWNLOAD 0x0040
116 #define XT_D_CONFIG 0x0080
117 #define XT_D_JS 0x0100
118 #define XT_D_FAVORITE 0x0200
119 #define XT_D_PRINTING 0x0400
120 #define XT_D_COOKIE 0x0800
121 #define XT_D_KEYBINDING 0x1000
122 u_int32_t swm_debug
= 0
138 #define DPRINTF(x...)
139 #define DNPRINTF(n,x...)
142 #define LENGTH(x) (sizeof x / sizeof x[0])
143 #define CLEAN(mask) (mask & ~(GDK_MOD2_MASK) & \
144 ~(GDK_BUTTON1_MASK) & \
145 ~(GDK_BUTTON2_MASK) & \
146 ~(GDK_BUTTON3_MASK) & \
147 ~(GDK_BUTTON4_MASK) & \
159 TAILQ_ENTRY(tab
) entry
;
161 GtkWidget
*tab_content
;
164 GtkWidget
*uri_entry
;
165 GtkWidget
*search_entry
;
167 GtkWidget
*browser_win
;
168 GtkWidget
*statusbar
;
175 GtkWidget
*js_toggle
;
176 GtkEntryCompletion
*completion
;
180 WebKitWebHistoryItem
*item
;
181 WebKitWebBackForwardList
*bfl
;
184 WebKitNetworkRequest
*icon_request
;
185 WebKitDownload
*icon_download
;
186 GdkPixbuf
*icon_pixbuf
;
187 gchar
*icon_dest_uri
;
189 /* adjustments for browser */
192 GtkAdjustment
*adjust_h
;
193 GtkAdjustment
*adjust_v
;
199 int xtp_meaning
; /* identifies dls/favorites */
204 #define XT_HINT_NONE (0)
205 #define XT_HINT_NUMERICAL (1)
206 #define XT_HINT_ALPHANUM (2)
210 /* custom stylesheet */
219 WebKitWebSettings
*settings
;
223 TAILQ_HEAD(tab_list
, tab
);
226 RB_ENTRY(history
) entry
;
230 RB_HEAD(history_list
, history
);
233 RB_ENTRY(download
) entry
;
235 WebKitDownload
*download
;
238 RB_HEAD(download_list
, download
);
241 RB_ENTRY(domain
) entry
;
243 int handy
; /* app use */
245 RB_HEAD(domain_list
, domain
);
248 TAILQ_ENTRY(undo
) entry
;
251 int back
; /* Keeps track of how many back
252 * history items there are. */
254 TAILQ_HEAD(undo_tailq
, undo
);
256 /* starts from 1 to catch atoi() failures when calling xtp_handle_dl() */
257 int next_download_id
= 1;
265 #define XT_NAME ("XXXTerm")
266 #define XT_DIR (".xxxterm")
267 #define XT_CACHE_DIR ("cache")
268 #define XT_CERT_DIR ("certs/")
269 #define XT_SESSIONS_DIR ("sessions/")
270 #define XT_CONF_FILE ("xxxterm.conf")
271 #define XT_FAVS_FILE ("favorites")
272 #define XT_SAVED_TABS_FILE ("main_session")
273 #define XT_RESTART_TABS_FILE ("restart_tabs")
274 #define XT_SOCKET_FILE ("socket")
275 #define XT_HISTORY_FILE ("history")
276 #define XT_SAVE_SESSION_ID ("SESSION_NAME=")
277 #define XT_CB_HANDLED (TRUE)
278 #define XT_CB_PASSTHROUGH (FALSE)
279 #define XT_DOCTYPE "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>"
280 #define XT_HTML_TAG "<html xmlns='http://www.w3.org/1999/xhtml'>"
281 #define XT_DLMAN_REFRESH "10"
282 #define XT_PAGE_STYLE "<style type='text/css'>\n" \
283 "td {overflow: hidden;" \
284 " padding: 2px 2px 2px 2px;" \
285 " border: 1px solid black}\n" \
286 "tr:hover {background: #ffff99 ;}\n" \
287 "th {background-color: #cccccc;" \
288 " border: 1px solid black}" \
289 "table {border-spacing: 0; " \
291 " border: 1px black solid;}\n" \
293 " border: 1px solid black;" \
299 " background: green;}" \
301 " font-size: small;" \
302 " text-align: center;}" \
304 #define XT_MAX_URL_LENGTH (4096) /* 1 page is atomic, don't make bigger */
305 #define XT_MAX_UNDO_CLOSE_TAB (32)
306 #define XT_RESERVED_CHARS "$&+,/:;=?@ \"<>#%%{}|^~[]`"
307 #define XT_PRINT_EXTRA_MARGIN 10
310 #define XT_COLOR_RED "#cc0000"
311 #define XT_COLOR_YELLOW "#ffff66"
312 #define XT_COLOR_BLUE "lightblue"
313 #define XT_COLOR_GREEN "#99ff66"
314 #define XT_COLOR_WHITE "white"
315 #define XT_COLOR_BLACK "black"
318 * xxxterm "protocol" (xtp)
319 * We use this for managing stuff like downloads and favorites. They
320 * make magical HTML pages in memory which have xxxt:// links in order
321 * to communicate with xxxterm's internals. These links take the format:
322 * xxxt://class/session_key/action/arg
324 * Don't begin xtp class/actions as 0. atoi returns that on error.
326 * Typically we have not put addition of items in this framework, as
327 * adding items is either done via an ex-command or via a keybinding instead.
330 #define XT_XTP_STR "xxxt://"
332 /* XTP classes (xxxt://<class>) */
333 #define XT_XTP_INVALID 0 /* invalid */
334 #define XT_XTP_DL 1 /* downloads */
335 #define XT_XTP_HL 2 /* history */
336 #define XT_XTP_CL 3 /* cookies */
337 #define XT_XTP_FL 4 /* favorites */
339 /* XTP download actions */
340 #define XT_XTP_DL_LIST 1
341 #define XT_XTP_DL_CANCEL 2
342 #define XT_XTP_DL_REMOVE 3
344 /* XTP history actions */
345 #define XT_XTP_HL_LIST 1
346 #define XT_XTP_HL_REMOVE 2
348 /* XTP cookie actions */
349 #define XT_XTP_CL_LIST 1
350 #define XT_XTP_CL_REMOVE 2
352 /* XTP cookie actions */
353 #define XT_XTP_FL_LIST 1
354 #define XT_XTP_FL_REMOVE 2
356 /* xtp tab meanings - identifies which tabs have xtp pages in */
357 #define XT_XTP_TAB_MEANING_NORMAL 0 /* normal url */
358 #define XT_XTP_TAB_MEANING_DL 1 /* download manager in this tab */
359 #define XT_XTP_TAB_MEANING_FL 2 /* favorite manager in this tab */
360 #define XT_XTP_TAB_MEANING_HL 3 /* history manager in this tab */
361 #define XT_XTP_TAB_MEANING_CL 4 /* cookie manager in this tab */
364 #define XT_MOVE_INVALID (0)
365 #define XT_MOVE_DOWN (1)
366 #define XT_MOVE_UP (2)
367 #define XT_MOVE_BOTTOM (3)
368 #define XT_MOVE_TOP (4)
369 #define XT_MOVE_PAGEDOWN (5)
370 #define XT_MOVE_PAGEUP (6)
371 #define XT_MOVE_HALFDOWN (7)
372 #define XT_MOVE_HALFUP (8)
373 #define XT_MOVE_LEFT (9)
374 #define XT_MOVE_FARLEFT (10)
375 #define XT_MOVE_RIGHT (11)
376 #define XT_MOVE_FARRIGHT (12)
378 #define XT_TAB_LAST (-4)
379 #define XT_TAB_FIRST (-3)
380 #define XT_TAB_PREV (-2)
381 #define XT_TAB_NEXT (-1)
382 #define XT_TAB_INVALID (0)
383 #define XT_TAB_NEW (1)
384 #define XT_TAB_DELETE (2)
385 #define XT_TAB_DELQUIT (3)
386 #define XT_TAB_OPEN (4)
387 #define XT_TAB_UNDO_CLOSE (5)
388 #define XT_TAB_SHOW (6)
389 #define XT_TAB_HIDE (7)
391 #define XT_NAV_INVALID (0)
392 #define XT_NAV_BACK (1)
393 #define XT_NAV_FORWARD (2)
394 #define XT_NAV_RELOAD (3)
395 #define XT_NAV_RELOAD_CACHE (4)
397 #define XT_FOCUS_INVALID (0)
398 #define XT_FOCUS_URI (1)
399 #define XT_FOCUS_SEARCH (2)
401 #define XT_SEARCH_INVALID (0)
402 #define XT_SEARCH_NEXT (1)
403 #define XT_SEARCH_PREV (2)
405 #define XT_PASTE_CURRENT_TAB (0)
406 #define XT_PASTE_NEW_TAB (1)
408 #define XT_FONT_SET (0)
410 #define XT_URL_SHOW (1)
411 #define XT_URL_HIDE (2)
413 #define XT_STATUSBAR_SHOW (1)
414 #define XT_STATUSBAR_HIDE (2)
416 #define XT_WL_TOGGLE (1<<0)
417 #define XT_WL_ENABLE (1<<1)
418 #define XT_WL_DISABLE (1<<2)
419 #define XT_WL_FQDN (1<<3) /* default */
420 #define XT_WL_TOPLEVEL (1<<4)
421 #define XT_WL_PERSISTENT (1<<5)
422 #define XT_WL_SESSION (1<<6)
424 #define XT_SHOW (1<<7)
425 #define XT_DELETE (1<<8)
426 #define XT_SAVE (1<<9)
427 #define XT_OPEN (1<<10)
429 #define XT_CMD_OPEN (0)
430 #define XT_CMD_OPEN_CURRENT (1)
431 #define XT_CMD_TABNEW (2)
432 #define XT_CMD_TABNEW_CURRENT (3)
434 #define XT_STATUS_NOTHING (0)
435 #define XT_STATUS_LINK (1)
436 #define XT_STATUS_URI (2)
437 #define XT_STATUS_LOADING (3)
439 #define XT_SES_DONOTHING (0)
440 #define XT_SES_CLOSETABS (1)
442 #define XT_BM_NORMAL (0)
443 #define XT_BM_WHITELIST (1)
444 #define XT_BM_KIOSK (2)
452 TAILQ_ENTRY(mime_type
) entry
;
454 TAILQ_HEAD(mime_type_list
, mime_type
);
460 TAILQ_ENTRY(alias
) entry
;
462 TAILQ_HEAD(alias_list
, alias
);
464 /* settings that require restart */
465 int tabless
= 0; /* allow only 1 tab */
466 int enable_socket
= 0;
467 int single_instance
= 0; /* only allow one xxxterm to run */
468 int fancy_bar
= 1; /* fancy toolbar */
469 int browser_mode
= XT_BM_NORMAL
;
471 /* runtime settings */
472 int show_tabs
= 1; /* show tabs on notebook */
473 int show_url
= 1; /* show url toolbar on notebook */
474 int show_statusbar
= 0; /* vimperator style status bar */
475 int ctrl_click_focus
= 0; /* ctrl click gets focus */
476 int cookies_enabled
= 1; /* enable cookies */
477 int read_only_cookies
= 0; /* enable to not write cookies */
478 int enable_scripts
= 1;
479 int enable_plugins
= 0;
480 int default_font_size
= 12;
481 gfloat default_zoom_level
= 1.0;
482 int window_height
= 768;
483 int window_width
= 1024;
484 int icon_size
= 2; /* 1 = smallest, 2+ = bigger */
485 unsigned refresh_interval
= 10; /* download refresh interval */
486 int enable_cookie_whitelist
= 0;
487 int enable_js_whitelist
= 0;
488 time_t session_timeout
= 3600; /* cookie session timeout */
489 int cookie_policy
= SOUP_COOKIE_JAR_ACCEPT_ALWAYS
;
490 char *ssl_ca_file
= NULL
;
491 char *resource_dir
= NULL
;
492 gboolean ssl_strict_certs
= FALSE
;
493 int append_next
= 1; /* append tab after current tab */
495 char *search_string
= NULL
;
496 char *http_proxy
= NULL
;
497 char download_dir
[PATH_MAX
];
498 char runtime_settings
[PATH_MAX
]; /* override of settings */
499 int allow_volatile_cookies
= 0;
500 int save_global_history
= 0; /* save global history to disk */
501 char *user_agent
= NULL
;
502 int save_rejected_cookies
= 0;
503 time_t session_autosave
= 0;
504 int guess_search
= 0;
505 int dns_prefetch
= FALSE
;
506 gint max_connections
= 25;
507 gint max_host_connections
= 5;
511 int set_download_dir(struct settings
*, char *);
512 int set_work_dir(struct settings
*, char *);
513 int set_runtime_dir(struct settings
*, char *);
514 int set_browser_mode(struct settings
*, char *);
515 int set_cookie_policy(struct settings
*, char *);
516 int add_alias(struct settings
*, char *);
517 int add_mime_type(struct settings
*, char *);
518 int add_cookie_wl(struct settings
*, char *);
519 int add_js_wl(struct settings
*, char *);
520 int add_kb(struct settings
*, char *);
521 void button_set_stockid(GtkWidget
*, char *);
522 GtkWidget
* create_button(char *, char *, int);
524 char *get_browser_mode(struct settings
*);
525 char *get_cookie_policy(struct settings
*);
527 char *get_download_dir(struct settings
*);
528 char *get_work_dir(struct settings
*);
529 char *get_runtime_dir(struct settings
*);
531 void walk_alias(struct settings
*, void (*)(struct settings
*, char *, void *), void *);
532 void walk_cookie_wl(struct settings
*, void (*)(struct settings
*, char *, void *), void *);
533 void walk_js_wl(struct settings
*, void (*)(struct settings
*, char *, void *), void *);
534 void walk_kb(struct settings
*, void (*)(struct settings
*, char *, void *), void *);
535 void walk_mime_type(struct settings
*, void (*)(struct settings
*, char *, void *), void *);
538 int (*set
)(struct settings
*, char *);
539 char *(*get
)(struct settings
*);
540 void (*walk
)(struct settings
*, void (*cb
)(struct settings
*, char *, void *), void *);
543 struct special s_browser_mode
= {
549 struct special s_cookie
= {
555 struct special s_alias
= {
561 struct special s_mime
= {
567 struct special s_js
= {
573 struct special s_kb
= {
579 struct special s_cookie_wl
= {
585 struct special s_download_dir
= {
591 struct special s_work_dir
= {
600 #define XT_S_INVALID (0)
603 #define XT_S_FLOAT (3)
605 #define XT_SF_RESTART (1<<0)
606 #define XT_SF_RUNTIME (1<<1)
612 { "append_next", XT_S_INT
, 0, &append_next
, NULL
, NULL
},
613 { "allow_volatile_cookies", XT_S_INT
, 0, &allow_volatile_cookies
, NULL
, NULL
},
614 { "browser_mode", XT_S_INT
, 0, NULL
, NULL
,&s_browser_mode
},
615 { "cookie_policy", XT_S_INT
, 0, NULL
, NULL
,&s_cookie
},
616 { "cookies_enabled", XT_S_INT
, 0, &cookies_enabled
, NULL
, NULL
},
617 { "ctrl_click_focus", XT_S_INT
, 0, &ctrl_click_focus
, NULL
, NULL
},
618 { "default_font_size", XT_S_INT
, 0, &default_font_size
, NULL
, NULL
},
619 { "default_zoom_level", XT_S_FLOAT
, 0, NULL
, NULL
, NULL
, &default_zoom_level
},
620 { "download_dir", XT_S_STR
, 0, NULL
, NULL
,&s_download_dir
},
621 { "enable_cookie_whitelist", XT_S_INT
, 0, &enable_cookie_whitelist
, NULL
, NULL
},
622 { "enable_js_whitelist", XT_S_INT
, 0, &enable_js_whitelist
, NULL
, NULL
},
623 { "enable_plugins", XT_S_INT
, 0, &enable_plugins
, NULL
, NULL
},
624 { "enable_scripts", XT_S_INT
, 0, &enable_scripts
, NULL
, NULL
},
625 { "enable_socket", XT_S_INT
, XT_SF_RESTART
,&enable_socket
, NULL
, NULL
},
626 { "fancy_bar", XT_S_INT
, XT_SF_RESTART
,&fancy_bar
, NULL
, NULL
},
627 { "home", XT_S_STR
, 0, NULL
, &home
, NULL
},
628 { "http_proxy", XT_S_STR
, 0, NULL
, &http_proxy
, NULL
},
629 { "icon_size", XT_S_INT
, 0, &icon_size
, NULL
, NULL
},
630 { "max_connections", XT_S_INT
, XT_SF_RESTART
,&max_connections
, NULL
, NULL
},
631 { "max_host_connections", XT_S_INT
, XT_SF_RESTART
,&max_host_connections
, NULL
, NULL
},
632 { "read_only_cookies", XT_S_INT
, 0, &read_only_cookies
, NULL
, NULL
},
633 { "refresh_interval", XT_S_INT
, 0, &refresh_interval
, NULL
, NULL
},
634 { "resource_dir", XT_S_STR
, 0, NULL
, &resource_dir
, NULL
},
635 { "search_string", XT_S_STR
, 0, NULL
, &search_string
, NULL
},
636 { "save_global_history", XT_S_INT
, XT_SF_RESTART
,&save_global_history
, NULL
, NULL
},
637 { "save_rejected_cookies", XT_S_INT
, XT_SF_RESTART
,&save_rejected_cookies
, NULL
, NULL
},
638 { "session_timeout", XT_S_INT
, 0, &session_timeout
, NULL
, NULL
},
639 { "session_autosave", XT_S_INT
, 0, &session_autosave
, NULL
, NULL
},
640 { "single_instance", XT_S_INT
, XT_SF_RESTART
,&single_instance
, NULL
, NULL
},
641 { "show_tabs", XT_S_INT
, 0, &show_tabs
, NULL
, NULL
},
642 { "show_url", XT_S_INT
, 0, &show_url
, NULL
, NULL
},
643 { "guess_search", XT_S_INT
, 0, &guess_search
, NULL
, NULL
},
644 { "show_statusbar", XT_S_INT
, 0, &show_statusbar
, NULL
, NULL
},
645 { "ssl_ca_file", XT_S_STR
, 0, NULL
, &ssl_ca_file
, NULL
},
646 { "ssl_strict_certs", XT_S_INT
, 0, &ssl_strict_certs
, NULL
, NULL
},
647 { "user_agent", XT_S_STR
, 0, NULL
, &user_agent
, NULL
},
648 { "window_height", XT_S_INT
, 0, &window_height
, NULL
, NULL
},
649 { "window_width", XT_S_INT
, 0, &window_width
, NULL
, NULL
},
650 { "work_dir", XT_S_STR
, 0, NULL
, NULL
,&s_work_dir
},
652 /* runtime settings */
653 { "alias", XT_S_STR
, XT_SF_RUNTIME
, NULL
, NULL
, &s_alias
},
654 { "cookie_wl", XT_S_STR
, XT_SF_RUNTIME
, NULL
, NULL
, &s_cookie_wl
},
655 { "js_wl", XT_S_STR
, XT_SF_RUNTIME
, NULL
, NULL
, &s_js
},
656 { "keybinding", XT_S_STR
, XT_SF_RUNTIME
, NULL
, NULL
, &s_kb
},
657 { "mime_type", XT_S_STR
, XT_SF_RUNTIME
, NULL
, NULL
, &s_mime
},
660 int about(struct tab
*, struct karg
*);
661 int blank(struct tab
*, struct karg
*);
662 int cookie_show_wl(struct tab
*, struct karg
*);
663 int js_show_wl(struct tab
*, struct karg
*);
664 int help(struct tab
*, struct karg
*);
665 int set(struct tab
*, struct karg
*);
666 int stats(struct tab
*, struct karg
*);
667 int marco(struct tab
*, struct karg
*);
668 const char * marco_message(int *);
669 int xtp_page_cl(struct tab
*, struct karg
*);
670 int xtp_page_dl(struct tab
*, struct karg
*);
671 int xtp_page_fl(struct tab
*, struct karg
*);
672 int xtp_page_hl(struct tab
*, struct karg
*);
674 #define XT_URI_ABOUT ("about:")
675 #define XT_URI_ABOUT_LEN (strlen(XT_URI_ABOUT))
676 #define XT_URI_ABOUT_ABOUT ("about")
677 #define XT_URI_ABOUT_BLANK ("blank")
678 #define XT_URI_ABOUT_CERTS ("certs") /* XXX NOT YET */
679 #define XT_URI_ABOUT_COOKIEWL ("cookiewl")
680 #define XT_URI_ABOUT_COOKIEJAR ("cookiejar")
681 #define XT_URI_ABOUT_DOWNLOADS ("downloads")
682 #define XT_URI_ABOUT_FAVORITES ("favorites")
683 #define XT_URI_ABOUT_HELP ("help")
684 #define XT_URI_ABOUT_HISTORY ("history")
685 #define XT_URI_ABOUT_JSWL ("jswl")
686 #define XT_URI_ABOUT_SET ("set")
687 #define XT_URI_ABOUT_STATS ("stats")
688 #define XT_URI_ABOUT_MARCO ("marco")
692 int (*func
)(struct tab
*, struct karg
*);
694 { XT_URI_ABOUT_ABOUT
, about
},
695 { XT_URI_ABOUT_BLANK
, blank
},
696 { XT_URI_ABOUT_COOKIEWL
, cookie_show_wl
},
697 { XT_URI_ABOUT_COOKIEJAR
, xtp_page_cl
},
698 { XT_URI_ABOUT_DOWNLOADS
, xtp_page_dl
},
699 { XT_URI_ABOUT_FAVORITES
, xtp_page_fl
},
700 { XT_URI_ABOUT_HELP
, help
},
701 { XT_URI_ABOUT_HISTORY
, xtp_page_hl
},
702 { XT_URI_ABOUT_JSWL
, js_show_wl
},
703 { XT_URI_ABOUT_SET
, set
},
704 { XT_URI_ABOUT_STATS
, stats
},
705 { XT_URI_ABOUT_MARCO
, marco
},
709 extern char *__progname
;
712 GtkWidget
*main_window
;
713 GtkNotebook
*notebook
;
714 GtkWidget
*arrow
, *abtn
;
715 struct tab_list tabs
;
716 struct history_list hl
;
717 struct download_list downloads
;
718 struct domain_list c_wl
;
719 struct domain_list js_wl
;
720 struct undo_tailq undos
;
721 struct keybinding_list kbl
;
723 int updating_dl_tabs
= 0;
724 int updating_hl_tabs
= 0;
725 int updating_cl_tabs
= 0;
726 int updating_fl_tabs
= 0;
728 uint64_t blocked_cookies
= 0;
729 char named_session
[PATH_MAX
];
730 void update_favicon(struct tab
*);
731 int icon_size_map(int);
733 GtkListStore
*completion_model
;
734 void completion_add(struct tab
*);
735 void completion_add_uri(const gchar
*);
740 int saved_errno
, status
;
745 while ((pid
= waitpid(WAIT_ANY
, &status
, WNOHANG
)) != 0) {
749 if (errno
!= ECHILD
) {
751 clog_warn("sigchild: waitpid:");
757 if (WIFEXITED(status
)) {
758 if (WEXITSTATUS(status
) != 0) {
760 clog_warnx("sigchild: child exit status: %d",
761 WEXITSTATUS(status));
766 clog_warnx("sigchild: child is terminated abnormally");
775 is_g_object_setting(GObject
*o
, char *str
)
777 guint n_props
= 0, i
;
778 GParamSpec
**proplist
;
780 if (! G_IS_OBJECT(o
))
783 proplist
= g_object_class_list_properties(G_OBJECT_GET_CLASS(o
),
786 for (i
=0; i
< n_props
; i
++) {
787 if (! strcmp(proplist
[i
]->name
, str
))
794 * Display a web page from a HTML string in memory, rather than from a URL
797 load_webkit_string(struct tab
*t
, const char *str
, gchar
*title
)
803 /* we set this to indicate we want to manually do navaction */
805 t
->item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
807 webkit_web_view_load_string(t
->wv
, str
, NULL
, NULL
, "");
808 #if GTK_CHECK_VERSION(2, 20, 0)
809 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
810 gtk_widget_hide(t
->spinner
);
814 uri
= g_strdup_printf("%s%s", XT_URI_ABOUT
, title
);
815 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
818 snprintf(file
, sizeof file
, "%s/%s", resource_dir
, icons
[0]);
819 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
820 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
821 GTK_ENTRY_ICON_PRIMARY
, pb
);
822 gdk_pixbuf_unref(pb
);
827 set_status(struct tab
*t
, gchar
*s
, int status
)
835 case XT_STATUS_LOADING
:
836 type
= g_strdup_printf("Loading: %s", s
);
840 type
= g_strdup_printf("Link: %s", s
);
842 t
->status
= g_strdup(gtk_entry_get_text(GTK_ENTRY(t
->statusbar
)));
846 type
= g_strdup_printf("%s", s
);
848 t
->status
= g_strdup(type
);
852 t
->status
= g_strdup(s
);
854 case XT_STATUS_NOTHING
:
859 gtk_entry_set_text(GTK_ENTRY(t
->statusbar
), s
);
865 hide_oops(struct tab
*t
)
867 gtk_widget_hide(t
->oops
);
871 hide_cmd(struct tab
*t
)
873 gtk_widget_hide(t
->cmd
);
877 show_cmd(struct tab
*t
)
879 gtk_widget_hide(t
->oops
);
880 gtk_widget_show(t
->cmd
);
884 show_oops(struct tab
*t
, const char *fmt
, ...)
893 if (vasprintf(&msg
, fmt
, ap
) == -1)
894 errx(1, "show_oops failed");
897 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
898 gtk_widget_hide(t
->cmd
);
899 gtk_widget_show(t
->oops
);
902 /* XXX collapse with show_oops */
904 show_oops_s(const char *fmt
, ...)
908 struct tab
*ti
, *t
= NULL
;
913 TAILQ_FOREACH(ti
, &tabs
, entry
)
914 if (ti
->tab_id
== gtk_notebook_current_page(notebook
)) {
922 if (vasprintf(&msg
, fmt
, ap
) == -1)
923 errx(1, "show_oops_s failed");
926 gtk_entry_set_text(GTK_ENTRY(t
->oops
), msg
);
927 gtk_widget_hide(t
->cmd
);
928 gtk_widget_show(t
->oops
);
932 get_as_string(struct settings
*s
)
943 warnx("get_as_string skip %s\n", s
->name
);
944 } else if (s
->type
== XT_S_INT
)
945 r
= g_strdup_printf("%d", *s
->ival
);
946 else if (s
->type
== XT_S_STR
)
947 r
= g_strdup(*s
->sval
);
948 else if (s
->type
== XT_S_FLOAT
)
949 r
= g_strdup_printf("%f", *s
->fval
);
951 r
= g_strdup_printf("INVALID TYPE");
957 settings_walk(void (*cb
)(struct settings
*, char *, void *), void *cb_args
)
962 for (i
= 0; i
< LENGTH(rs
); i
++) {
963 if (rs
[i
].s
&& rs
[i
].s
->walk
)
964 rs
[i
].s
->walk(&rs
[i
], cb
, cb_args
);
966 s
= get_as_string(&rs
[i
]);
967 cb(&rs
[i
], s
, cb_args
);
974 set_browser_mode(struct settings
*s
, char *val
)
976 if (!strcmp(val
, "whitelist")) {
977 browser_mode
= XT_BM_WHITELIST
;
978 allow_volatile_cookies
= 0;
979 cookie_policy
= SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY
;
981 enable_cookie_whitelist
= 1;
982 read_only_cookies
= 0;
983 save_rejected_cookies
= 0;
984 session_timeout
= 3600;
986 enable_js_whitelist
= 1;
987 } else if (!strcmp(val
, "normal")) {
988 browser_mode
= XT_BM_NORMAL
;
989 allow_volatile_cookies
= 0;
990 cookie_policy
= SOUP_COOKIE_JAR_ACCEPT_ALWAYS
;
992 enable_cookie_whitelist
= 0;
993 read_only_cookies
= 0;
994 save_rejected_cookies
= 0;
995 session_timeout
= 3600;
997 enable_js_whitelist
= 0;
998 } else if (!strcmp(val
, "kiosk")) {
999 browser_mode
= XT_BM_KIOSK
;
1000 allow_volatile_cookies
= 0;
1001 cookie_policy
= SOUP_COOKIE_JAR_ACCEPT_ALWAYS
;
1002 cookies_enabled
= 1;
1003 enable_cookie_whitelist
= 0;
1004 read_only_cookies
= 0;
1005 save_rejected_cookies
= 0;
1006 session_timeout
= 3600;
1008 enable_js_whitelist
= 0;
1018 get_browser_mode(struct settings
*s
)
1022 if (browser_mode
== XT_BM_WHITELIST
)
1023 r
= g_strdup("whitelist");
1024 else if (browser_mode
== XT_BM_NORMAL
)
1025 r
= g_strdup("normal");
1026 else if (browser_mode
== XT_BM_KIOSK
)
1027 r
= g_strdup("kiosk");
1035 set_cookie_policy(struct settings
*s
, char *val
)
1037 if (!strcmp(val
, "no3rdparty"))
1038 cookie_policy
= SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY
;
1039 else if (!strcmp(val
, "accept"))
1040 cookie_policy
= SOUP_COOKIE_JAR_ACCEPT_ALWAYS
;
1041 else if (!strcmp(val
, "reject"))
1042 cookie_policy
= SOUP_COOKIE_JAR_ACCEPT_NEVER
;
1050 get_cookie_policy(struct settings
*s
)
1054 if (cookie_policy
== SOUP_COOKIE_JAR_ACCEPT_NO_THIRD_PARTY
)
1055 r
= g_strdup("no3rdparty");
1056 else if (cookie_policy
== SOUP_COOKIE_JAR_ACCEPT_ALWAYS
)
1057 r
= g_strdup("accept");
1058 else if (cookie_policy
== SOUP_COOKIE_JAR_ACCEPT_NEVER
)
1059 r
= g_strdup("reject");
1067 get_download_dir(struct settings
*s
)
1069 if (download_dir
[0] == '\0')
1071 return (g_strdup(download_dir
));
1075 set_download_dir(struct settings
*s
, char *val
)
1078 snprintf(download_dir
, sizeof download_dir
, "%s/%s",
1079 pwd
->pw_dir
, &val
[1]);
1081 strlcpy(download_dir
, val
, sizeof download_dir
);
1088 * We use these to prevent people putting xxxt:// URLs on
1089 * websites in the wild. We generate 8 bytes and represent in hex (16 chars)
1091 #define XT_XTP_SES_KEY_SZ 8
1092 #define XT_XTP_SES_KEY_HEX_FMT \
1093 "%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx"
1094 char *dl_session_key
; /* downloads */
1095 char *hl_session_key
; /* history list */
1096 char *cl_session_key
; /* cookie list */
1097 char *fl_session_key
; /* favorites list */
1099 char work_dir
[PATH_MAX
];
1100 char certs_dir
[PATH_MAX
];
1101 char cache_dir
[PATH_MAX
];
1102 char sessions_dir
[PATH_MAX
];
1103 char cookie_file
[PATH_MAX
];
1104 SoupURI
*proxy_uri
= NULL
;
1105 SoupSession
*session
;
1106 SoupCookieJar
*s_cookiejar
;
1107 SoupCookieJar
*p_cookiejar
;
1108 char rc_fname
[PATH_MAX
];
1110 struct mime_type_list mtl
;
1111 struct alias_list aliases
;
1114 struct tab
*create_new_tab(char *, struct undo
*, int);
1115 void delete_tab(struct tab
*);
1116 void adjustfont_webkit(struct tab
*, int);
1117 int run_script(struct tab
*, char *);
1118 int download_rb_cmp(struct download
*, struct download
*);
1119 gboolean
cmd_execute(struct tab
*t
, char *str
);
1122 history_rb_cmp(struct history
*h1
, struct history
*h2
)
1124 return (strcmp(h1
->uri
, h2
->uri
));
1126 RB_GENERATE(history_list
, history
, entry
, history_rb_cmp
);
1129 domain_rb_cmp(struct domain
*d1
, struct domain
*d2
)
1131 return (strcmp(d1
->d
, d2
->d
));
1133 RB_GENERATE(domain_list
, domain
, entry
, domain_rb_cmp
);
1136 get_work_dir(struct settings
*s
)
1138 if (work_dir
[0] == '\0')
1140 return (g_strdup(work_dir
));
1144 set_work_dir(struct settings
*s
, char *val
)
1147 snprintf(work_dir
, sizeof work_dir
, "%s/%s",
1148 pwd
->pw_dir
, &val
[1]);
1150 strlcpy(work_dir
, val
, sizeof work_dir
);
1156 * generate a session key to secure xtp commands.
1157 * pass in a ptr to the key in question and it will
1158 * be modified in place.
1161 generate_xtp_session_key(char **key
)
1163 uint8_t rand_bytes
[XT_XTP_SES_KEY_SZ
];
1169 /* make a new one */
1170 arc4random_buf(rand_bytes
, XT_XTP_SES_KEY_SZ
);
1171 *key
= g_strdup_printf(XT_XTP_SES_KEY_HEX_FMT
,
1172 rand_bytes
[0], rand_bytes
[1], rand_bytes
[2], rand_bytes
[3],
1173 rand_bytes
[4], rand_bytes
[5], rand_bytes
[6], rand_bytes
[7]);
1175 DNPRINTF(XT_D_DOWNLOAD
, "%s: new session key '%s'\n", __func__
, *key
);
1179 * validate a xtp session key.
1183 validate_xtp_session_key(struct tab
*t
, char *trusted
, char *untrusted
)
1185 if (strcmp(trusted
, untrusted
) != 0) {
1186 show_oops(t
, "%s: xtp session key mismatch possible spoof",
1195 download_rb_cmp(struct download
*e1
, struct download
*e2
)
1197 return (e1
->id
< e2
->id
? -1 : e1
->id
> e2
->id
);
1199 RB_GENERATE(download_list
, download
, entry
, download_rb_cmp
);
1201 struct valid_url_types
{
1212 valid_url_type(char *url
)
1216 for (i
= 0; i
< LENGTH(vut
); i
++)
1217 if (!strncasecmp(vut
[i
].type
, url
, strlen(vut
[i
].type
)))
1224 print_cookie(char *msg
, SoupCookie
*c
)
1230 DNPRINTF(XT_D_COOKIE
, "%s\n", msg
);
1231 DNPRINTF(XT_D_COOKIE
, "name : %s\n", c
->name
);
1232 DNPRINTF(XT_D_COOKIE
, "value : %s\n", c
->value
);
1233 DNPRINTF(XT_D_COOKIE
, "domain : %s\n", c
->domain
);
1234 DNPRINTF(XT_D_COOKIE
, "path : %s\n", c
->path
);
1235 DNPRINTF(XT_D_COOKIE
, "expires : %s\n",
1236 c
->expires
? soup_date_to_string(c
->expires
, SOUP_DATE_HTTP
) : "");
1237 DNPRINTF(XT_D_COOKIE
, "secure : %d\n", c
->secure
);
1238 DNPRINTF(XT_D_COOKIE
, "http_only: %d\n", c
->http_only
);
1239 DNPRINTF(XT_D_COOKIE
, "====================================\n");
1243 walk_alias(struct settings
*s
,
1244 void (*cb
)(struct settings
*, char *, void *), void *cb_args
)
1249 if (s
== NULL
|| cb
== NULL
) {
1250 show_oops_s("walk_alias invalid parameters");
1254 TAILQ_FOREACH(a
, &aliases
, entry
) {
1255 str
= g_strdup_printf("%s --> %s", a
->a_name
, a
->a_uri
);
1256 cb(s
, str
, cb_args
);
1262 match_alias(char *url_in
)
1266 char *url_out
= NULL
, *search
, *enc_arg
;
1268 search
= g_strdup(url_in
);
1270 if (strsep(&arg
, " \t") == NULL
) {
1271 show_oops_s("match_alias: NULL URL");
1275 TAILQ_FOREACH(a
, &aliases
, entry
) {
1276 if (!strcmp(search
, a
->a_name
))
1281 DNPRINTF(XT_D_URL
, "match_alias: matched alias %s\n",
1284 enc_arg
= soup_uri_encode(arg
, XT_RESERVED_CHARS
);
1285 url_out
= g_strdup_printf(a
->a_uri
, enc_arg
);
1288 url_out
= g_strdup(a
->a_uri
);
1296 guess_url_type(char *url_in
)
1299 char *url_out
= NULL
, *enc_search
= NULL
;
1301 url_out
= match_alias(url_in
);
1302 if (url_out
!= NULL
)
1307 * If there is no dot nor slash in the string and it isn't a
1308 * path to a local file and doesn't resolves to an IP, assume
1309 * that the user wants to search for the string.
1312 if (strchr(url_in
, '.') == NULL
&&
1313 strchr(url_in
, '/') == NULL
&&
1314 stat(url_in
, &sb
) != 0 &&
1315 gethostbyname(url_in
) == NULL
) {
1317 enc_search
= soup_uri_encode(url_in
, XT_RESERVED_CHARS
);
1318 url_out
= g_strdup_printf(search_string
, enc_search
);
1324 /* XXX not sure about this heuristic */
1325 if (stat(url_in
, &sb
) == 0)
1326 url_out
= g_strdup_printf("file://%s", url_in
);
1328 url_out
= g_strdup_printf("http://%s", url_in
); /* guess http */
1330 DNPRINTF(XT_D_URL
, "guess_url_type: guessed %s\n", url_out
);
1336 load_uri(struct tab
*t
, gchar
*uri
)
1339 gchar
*newuri
= NULL
;
1345 /* Strip leading spaces. */
1346 while(*uri
&& isspace(*uri
))
1349 if (strlen(uri
) == 0) {
1354 if (!strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
)) {
1355 for (i
= 0; i
< LENGTH(about_list
); i
++)
1356 if (!strcmp(&uri
[XT_URI_ABOUT_LEN
], about_list
[i
].name
)) {
1357 bzero(&args
, sizeof args
);
1358 about_list
[i
].func(t
, &args
);
1361 show_oops(t
, "invalid about page");
1365 if (valid_url_type(uri
)) {
1366 newuri
= guess_url_type(uri
);
1370 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
1371 webkit_web_view_load_uri(t
->wv
, uri
);
1378 get_uri(WebKitWebView
*wv
)
1380 WebKitWebFrame
*frame
;
1383 frame
= webkit_web_view_get_main_frame(wv
);
1384 uri
= webkit_web_frame_get_uri(frame
);
1386 if (uri
&& strlen(uri
) > 0)
1393 add_alias(struct settings
*s
, char *line
)
1396 struct alias
*a
= NULL
;
1398 if (s
== NULL
|| line
== NULL
) {
1399 show_oops_s("add_alias invalid parameters");
1404 a
= g_malloc(sizeof(*a
));
1406 if ((alias
= strsep(&l
, " \t,")) == NULL
|| l
== NULL
) {
1407 show_oops_s("add_alias: incomplete alias definition");
1410 if (strlen(alias
) == 0 || strlen(l
) == 0) {
1411 show_oops_s("add_alias: invalid alias definition");
1415 a
->a_name
= g_strdup(alias
);
1416 a
->a_uri
= g_strdup(l
);
1418 DNPRINTF(XT_D_CONFIG
, "add_alias: %s for %s\n", a
->a_name
, a
->a_uri
);
1420 TAILQ_INSERT_TAIL(&aliases
, a
, entry
);
1430 add_mime_type(struct settings
*s
, char *line
)
1434 struct mime_type
*m
= NULL
;
1435 int downloadfirst
= 0;
1437 /* XXX this could be smarter */
1439 if (line
== NULL
&& strlen(line
) == 0) {
1440 show_oops_s("add_mime_type invalid parameters");
1449 m
= g_malloc(sizeof(*m
));
1451 if ((mime_type
= strsep(&l
, " \t,")) == NULL
|| l
== NULL
) {
1452 show_oops_s("add_mime_type: invalid mime_type");
1455 if (mime_type
[strlen(mime_type
) - 1] == '*') {
1456 mime_type
[strlen(mime_type
) - 1] = '\0';
1461 if (strlen(mime_type
) == 0 || strlen(l
) == 0) {
1462 show_oops_s("add_mime_type: invalid mime_type");
1466 m
->mt_type
= g_strdup(mime_type
);
1467 m
->mt_action
= g_strdup(l
);
1468 m
->mt_download
= downloadfirst
;
1470 DNPRINTF(XT_D_CONFIG
, "add_mime_type: type %s action %s default %d\n",
1471 m
->mt_type
, m
->mt_action
, m
->mt_default
);
1473 TAILQ_INSERT_TAIL(&mtl
, m
, entry
);
1483 find_mime_type(char *mime_type
)
1485 struct mime_type
*m
, *def
= NULL
, *rv
= NULL
;
1487 TAILQ_FOREACH(m
, &mtl
, entry
) {
1488 if (m
->mt_default
&&
1489 !strncmp(mime_type
, m
->mt_type
, strlen(m
->mt_type
)))
1492 if (m
->mt_default
== 0 && !strcmp(mime_type
, m
->mt_type
)) {
1505 walk_mime_type(struct settings
*s
,
1506 void (*cb
)(struct settings
*, char *, void *), void *cb_args
)
1508 struct mime_type
*m
;
1511 if (s
== NULL
|| cb
== NULL
)
1512 show_oops_s("walk_mime_type invalid parameters");
1514 TAILQ_FOREACH(m
, &mtl
, entry
) {
1515 str
= g_strdup_printf("%s%s --> %s",
1517 m
->mt_default
? "*" : "",
1519 cb(s
, str
, cb_args
);
1525 wl_add(char *str
, struct domain_list
*wl
, int handy
)
1530 if (str
== NULL
|| wl
== NULL
)
1532 if (strlen(str
) < 2)
1535 DNPRINTF(XT_D_COOKIE
, "wl_add in: %s\n", str
);
1537 /* treat *.moo.com the same as .moo.com */
1538 if (str
[0] == '*' && str
[1] == '.')
1540 else if (str
[0] == '.')
1545 d
= g_malloc(sizeof *d
);
1547 d
->d
= g_strdup_printf(".%s", str
);
1549 d
->d
= g_strdup(str
);
1552 if (RB_INSERT(domain_list
, wl
, d
))
1555 DNPRINTF(XT_D_COOKIE
, "wl_add: %s\n", d
->d
);
1566 add_cookie_wl(struct settings
*s
, char *entry
)
1568 wl_add(entry
, &c_wl
, 1);
1573 walk_cookie_wl(struct settings
*s
,
1574 void (*cb
)(struct settings
*, char *, void *), void *cb_args
)
1578 if (s
== NULL
|| cb
== NULL
) {
1579 show_oops_s("walk_cookie_wl invalid parameters");
1583 RB_FOREACH_REVERSE(d
, domain_list
, &c_wl
)
1584 cb(s
, d
->d
, cb_args
);
1588 walk_js_wl(struct settings
*s
,
1589 void (*cb
)(struct settings
*, char *, void *), void *cb_args
)
1593 if (s
== NULL
|| cb
== NULL
) {
1594 show_oops_s("walk_js_wl invalid parameters");
1598 RB_FOREACH_REVERSE(d
, domain_list
, &js_wl
)
1599 cb(s
, d
->d
, cb_args
);
1603 add_js_wl(struct settings
*s
, char *entry
)
1605 wl_add(entry
, &js_wl
, 1 /* persistent */);
1610 wl_find(const gchar
*search
, struct domain_list
*wl
)
1613 struct domain
*d
= NULL
, dfind
;
1616 if (search
== NULL
|| wl
== NULL
)
1618 if (strlen(search
) < 2)
1621 if (search
[0] != '.')
1622 s
= g_strdup_printf(".%s", search
);
1624 s
= g_strdup(search
);
1626 for (i
= strlen(s
) - 1; i
>= 0; i
--) {
1629 d
= RB_FIND(domain_list
, wl
, &dfind
);
1643 wl_find_uri(const gchar
*s
, struct domain_list
*wl
)
1649 if (s
== NULL
|| wl
== NULL
)
1652 if (!strncmp(s
, "http://", strlen("http://")))
1653 s
= &s
[strlen("http://")];
1654 else if (!strncmp(s
, "https://", strlen("https://")))
1655 s
= &s
[strlen("https://")];
1660 for (i
= 0; i
< strlen(s
) + 1 /* yes er need this */; i
++)
1661 /* chop string at first slash */
1662 if (s
[i
] == '/' || s
[i
] == '\0') {
1665 r
= wl_find(ss
, wl
);
1674 get_toplevel_domain(char *domain
)
1681 if (strlen(domain
) < 2)
1684 s
= &domain
[strlen(domain
) - 1];
1685 while (s
!= domain
) {
1701 settings_add(char *var
, char *val
)
1708 for (i
= 0, rv
= 0; i
< LENGTH(rs
); i
++) {
1709 if (strcmp(var
, rs
[i
].name
))
1713 if (rs
[i
].s
->set(&rs
[i
], val
))
1714 errx(1, "invalid value for %s: %s", var
, val
);
1718 switch (rs
[i
].type
) {
1727 errx(1, "invalid sval for %s",
1741 errx(1, "invalid type for %s", var
);
1750 config_parse(char *filename
, int runtime
)
1753 char *line
, *cp
, *var
, *val
;
1754 size_t len
, lineno
= 0;
1756 char file
[PATH_MAX
];
1759 DNPRINTF(XT_D_CONFIG
, "config_parse: filename %s\n", filename
);
1761 if (filename
== NULL
)
1764 if (runtime
&& runtime_settings
[0] != '\0') {
1765 snprintf(file
, sizeof file
, "%s/%s",
1766 work_dir
, runtime_settings
);
1767 if (stat(file
, &sb
)) {
1768 warnx("runtime file doesn't exist, creating it");
1769 if ((f
= fopen(file
, "w")) == NULL
)
1771 fprintf(f
, "# AUTO GENERATED, DO NOT EDIT\n");
1775 strlcpy(file
, filename
, sizeof file
);
1777 if ((config
= fopen(file
, "r")) == NULL
) {
1778 warn("config_parse: cannot open %s", filename
);
1783 if ((line
= fparseln(config
, &len
, &lineno
, NULL
, 0)) == NULL
)
1784 if (feof(config
) || ferror(config
))
1788 cp
+= (long)strspn(cp
, WS
);
1789 if (cp
[0] == '\0') {
1795 if ((var
= strsep(&cp
, WS
)) == NULL
|| cp
== NULL
)
1796 errx(1, "invalid config file entry: %s", line
);
1798 cp
+= (long)strspn(cp
, WS
);
1800 if ((val
= strsep(&cp
, "\0")) == NULL
)
1803 DNPRINTF(XT_D_CONFIG
, "config_parse: %s=%s\n",var
,val
);
1804 handled
= settings_add(var
, val
);
1806 errx(1, "invalid conf file entry: %s=%s", var
, val
);
1815 js_ref_to_string(JSContextRef context
, JSValueRef ref
)
1821 jsref
= JSValueToStringCopy(context
, ref
, NULL
);
1825 l
= JSStringGetMaximumUTF8CStringSize(jsref
);
1828 JSStringGetUTF8CString(jsref
, s
, l
);
1829 JSStringRelease(jsref
);
1835 disable_hints(struct tab
*t
)
1837 bzero(t
->hint_buf
, sizeof t
->hint_buf
);
1838 bzero(t
->hint_num
, sizeof t
->hint_num
);
1839 run_script(t
, "vimprobable_clear()");
1841 t
->hint_mode
= XT_HINT_NONE
;
1845 enable_hints(struct tab
*t
)
1847 bzero(t
->hint_buf
, sizeof t
->hint_buf
);
1848 run_script(t
, "vimprobable_show_hints()");
1850 t
->hint_mode
= XT_HINT_NONE
;
1853 #define XT_JS_OPEN ("open;")
1854 #define XT_JS_OPEN_LEN (strlen(XT_JS_OPEN))
1855 #define XT_JS_FIRE ("fire;")
1856 #define XT_JS_FIRE_LEN (strlen(XT_JS_FIRE))
1857 #define XT_JS_FOUND ("found;")
1858 #define XT_JS_FOUND_LEN (strlen(XT_JS_FOUND))
1861 run_script(struct tab
*t
, char *s
)
1863 JSGlobalContextRef ctx
;
1864 WebKitWebFrame
*frame
;
1866 JSValueRef val
, exception
;
1869 DNPRINTF(XT_D_JS
, "run_script: tab %d %s\n",
1870 t
->tab_id
, s
== (char *)JS_HINTING
? "JS_HINTING" : s
);
1872 frame
= webkit_web_view_get_main_frame(t
->wv
);
1873 ctx
= webkit_web_frame_get_global_context(frame
);
1875 str
= JSStringCreateWithUTF8CString(s
);
1876 val
= JSEvaluateScript(ctx
, str
, JSContextGetGlobalObject(ctx
),
1877 NULL
, 0, &exception
);
1878 JSStringRelease(str
);
1880 DNPRINTF(XT_D_JS
, "run_script: val %p\n", val
);
1882 es
= js_ref_to_string(ctx
, exception
);
1883 DNPRINTF(XT_D_JS
, "run_script: exception %s\n", es
);
1887 es
= js_ref_to_string(ctx
, val
);
1888 DNPRINTF(XT_D_JS
, "run_script: val %s\n", es
);
1890 /* handle return value right here */
1891 if (!strncmp(es
, XT_JS_OPEN
, XT_JS_OPEN_LEN
)) {
1893 webkit_web_view_load_uri(t
->wv
, &es
[XT_JS_OPEN_LEN
]);
1896 if (!strncmp(es
, XT_JS_FIRE
, XT_JS_FIRE_LEN
)) {
1897 snprintf(buf
, sizeof buf
, "vimprobable_fire(%s)",
1898 &es
[XT_JS_FIRE_LEN
]);
1903 if (!strncmp(es
, XT_JS_FOUND
, XT_JS_FOUND_LEN
)) {
1904 if (atoi(&es
[XT_JS_FOUND_LEN
]) == 0)
1915 hint(struct tab
*t
, struct karg
*args
)
1918 DNPRINTF(XT_D_JS
, "hint: tab %d\n", t
->tab_id
);
1920 if (t
->hints_on
== 0)
1929 apply_style(struct tab
*t
)
1931 g_object_set(G_OBJECT(t
->settings
),
1932 "user-stylesheet-uri", t
->stylesheet
, (char *)NULL
);
1936 userstyle(struct tab
*t
, struct karg
*args
)
1938 DNPRINTF(XT_D_JS
, "userstyle: tab %d\n", t
->tab_id
);
1942 g_object_set(G_OBJECT(t
->settings
),
1943 "user-stylesheet-uri", NULL
, (char *)NULL
);
1952 * Doesn't work fully, due to the following bug:
1953 * https://bugs.webkit.org/show_bug.cgi?id=51747
1956 restore_global_history(void)
1958 char file
[PATH_MAX
];
1964 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_HISTORY_FILE
);
1966 if ((f
= fopen(file
, "r")) == NULL
) {
1967 warnx("%s: fopen", __func__
);
1972 if ((uri
= fparseln(f
, NULL
, NULL
, NULL
, 0)) == NULL
)
1973 if (feof(f
) || ferror(f
))
1976 if ((title
= fparseln(f
, NULL
, NULL
, NULL
, 0)) == NULL
)
1977 if (feof(f
) || ferror(f
)) {
1979 warnx("%s: broken history file\n", __func__
);
1983 if (uri
&& strlen(uri
) && title
&& strlen(title
)) {
1984 webkit_web_history_item_new_with_data(uri
, title
);
1985 h
= g_malloc(sizeof(struct history
));
1986 h
->uri
= g_strdup(uri
);
1987 h
->title
= g_strdup(title
);
1988 RB_INSERT(history_list
, &hl
, h
);
1989 completion_add_uri(h
->uri
);
1991 warnx("%s: failed to restore history\n", __func__
);
2007 save_global_history_to_disk(struct tab
*t
)
2009 char file
[PATH_MAX
];
2013 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_HISTORY_FILE
);
2015 if ((f
= fopen(file
, "w")) == NULL
) {
2016 show_oops(t
, "%s: global history file: %s",
2017 __func__
, strerror(errno
));
2021 RB_FOREACH_REVERSE(h
, history_list
, &hl
) {
2022 if (h
->uri
&& h
->title
)
2023 fprintf(f
, "%s\n%s\n", h
->uri
, h
->title
);
2032 quit(struct tab
*t
, struct karg
*args
)
2034 if (save_global_history
)
2035 save_global_history_to_disk(t
);
2043 open_tabs(struct tab
*t
, struct karg
*a
)
2045 char file
[PATH_MAX
];
2049 struct tab
*ti
, *tt
;
2054 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
2055 if ((f
= fopen(file
, "r")) == NULL
)
2058 ti
= TAILQ_LAST(&tabs
, tab_list
);
2061 if ((uri
= fparseln(f
, NULL
, NULL
, NULL
, 0)) == NULL
)
2062 if (feof(f
) || ferror(f
))
2065 /* retrieve session name */
2066 if (uri
&& g_str_has_prefix(uri
, XT_SAVE_SESSION_ID
)) {
2067 strlcpy(named_session
,
2068 &uri
[strlen(XT_SAVE_SESSION_ID
)],
2069 sizeof named_session
);
2073 if (uri
&& strlen(uri
))
2074 create_new_tab(uri
, NULL
, 1);
2080 /* close open tabs */
2081 if (a
->i
== XT_SES_CLOSETABS
&& ti
!= NULL
) {
2083 tt
= TAILQ_FIRST(&tabs
);
2102 restore_saved_tabs(void)
2104 char file
[PATH_MAX
];
2105 int unlink_file
= 0;
2110 snprintf(file
, sizeof file
, "%s/%s",
2111 sessions_dir
, XT_RESTART_TABS_FILE
);
2112 if (stat(file
, &sb
) == -1)
2113 a
.s
= XT_SAVED_TABS_FILE
;
2116 a
.s
= XT_RESTART_TABS_FILE
;
2119 a
.i
= XT_SES_DONOTHING
;
2120 rv
= open_tabs(NULL
, &a
);
2129 save_tabs(struct tab
*t
, struct karg
*a
)
2131 char file
[PATH_MAX
];
2136 const gchar
**arr
= NULL
;
2141 snprintf(file
, sizeof file
, "%s/%s",
2142 sessions_dir
, named_session
);
2144 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, a
->s
);
2146 if ((f
= fopen(file
, "w")) == NULL
) {
2147 show_oops(t
, "Can't open save_tabs file: %s", strerror(errno
));
2151 /* save session name */
2152 fprintf(f
, "%s%s\n", XT_SAVE_SESSION_ID
, named_session
);
2154 /* save tabs, in the order they are arranged in the notebook */
2155 TAILQ_FOREACH(ti
, &tabs
, entry
)
2158 arr
= g_malloc0(len
* sizeof(gchar
*));
2160 TAILQ_FOREACH(ti
, &tabs
, entry
) {
2161 if ((uri
= get_uri(ti
->wv
)) != NULL
)
2162 arr
[gtk_notebook_page_num(notebook
, ti
->vbox
)] = uri
;
2165 for (i
= 0; i
< len
; i
++)
2167 fprintf(f
, "%s\n", arr
[i
]);
2176 save_tabs_and_quit(struct tab
*t
, struct karg
*args
)
2188 yank_uri(struct tab
*t
, struct karg
*args
)
2191 GtkClipboard
*clipboard
;
2193 if ((uri
= get_uri(t
->wv
)) == NULL
)
2196 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
2197 gtk_clipboard_set_text(clipboard
, uri
, -1);
2208 paste_uri_cb(GtkClipboard
*clipboard
, const gchar
*text
, gpointer data
)
2210 struct paste_args
*pap
;
2212 if (data
== NULL
|| text
== NULL
|| !strlen(text
))
2215 pap
= (struct paste_args
*)data
;
2218 case XT_PASTE_CURRENT_TAB
:
2219 load_uri(pap
->t
, (gchar
*)text
);
2221 case XT_PASTE_NEW_TAB
:
2222 create_new_tab((gchar
*)text
, NULL
, 1);
2230 paste_uri(struct tab
*t
, struct karg
*args
)
2232 GtkClipboard
*clipboard
;
2233 struct paste_args
*pap
;
2235 pap
= g_malloc(sizeof(struct paste_args
));
2240 clipboard
= gtk_clipboard_get(GDK_SELECTION_PRIMARY
);
2241 gtk_clipboard_request_text(clipboard
, paste_uri_cb
, pap
);
2247 find_domain(const gchar
*s
, int add_dot
)
2250 char *r
= NULL
, *ss
= NULL
;
2255 if (!strncmp(s
, "http://", strlen("http://")))
2256 s
= &s
[strlen("http://")];
2257 else if (!strncmp(s
, "https://", strlen("https://")))
2258 s
= &s
[strlen("https://")];
2264 for (i
= 0; i
< strlen(ss
) + 1 /* yes er need this */; i
++)
2265 /* chop string at first slash */
2266 if (ss
[i
] == '/' || ss
[i
] == '\0') {
2269 r
= g_strdup_printf(".%s", ss
);
2280 toggle_cwl(struct tab
*t
, struct karg
*args
)
2284 char *dom
= NULL
, *dom_toggle
= NULL
;
2290 uri
= get_uri(t
->wv
);
2291 dom
= find_domain(uri
, 1);
2292 d
= wl_find(dom
, &c_wl
);
2299 if (args
->i
& XT_WL_TOGGLE
)
2301 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
2303 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
2306 if (args
->i
& XT_WL_TOPLEVEL
)
2307 dom_toggle
= get_toplevel_domain(dom
);
2312 /* enable cookies for domain */
2313 wl_add(dom_toggle
, &c_wl
, 0);
2315 /* disable cookies for domain */
2316 RB_REMOVE(domain_list
, &c_wl
, d
);
2318 webkit_web_view_reload(t
->wv
);
2325 toggle_js(struct tab
*t
, struct karg
*args
)
2330 char *dom
= NULL
, *dom_toggle
= NULL
;
2335 g_object_get(G_OBJECT(t
->settings
),
2336 "enable-scripts", &es
, (char *)NULL
);
2337 if (args
->i
& XT_WL_TOGGLE
)
2339 else if ((args
->i
& XT_WL_ENABLE
) && es
!= 1)
2341 else if ((args
->i
& XT_WL_DISABLE
) && es
!= 0)
2346 uri
= get_uri(t
->wv
);
2347 dom
= find_domain(uri
, 1);
2349 if (uri
== NULL
|| dom
== NULL
) {
2350 show_oops(t
, "Can't toggle domain in JavaScript white list");
2354 if (args
->i
& XT_WL_TOPLEVEL
)
2355 dom_toggle
= get_toplevel_domain(dom
);
2360 button_set_stockid(t
->js_toggle
, GTK_STOCK_MEDIA_PLAY
);
2361 wl_add(dom_toggle
, &js_wl
, 0 /* session */);
2363 d
= wl_find(dom_toggle
, &js_wl
);
2365 RB_REMOVE(domain_list
, &js_wl
, d
);
2366 button_set_stockid(t
->js_toggle
, GTK_STOCK_MEDIA_PAUSE
);
2368 g_object_set(G_OBJECT(t
->settings
),
2369 "enable-scripts", es
, (char *)NULL
);
2370 g_object_set(G_OBJECT(t
->settings
),
2371 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
2372 webkit_web_view_set_settings(t
->wv
, t
->settings
);
2373 webkit_web_view_reload(t
->wv
);
2381 js_toggle_cb(GtkWidget
*w
, struct tab
*t
)
2385 a
.i
= XT_WL_TOGGLE
| XT_WL_FQDN
;
2390 toggle_src(struct tab
*t
, struct karg
*args
)
2397 mode
= webkit_web_view_get_view_source_mode(t
->wv
);
2398 webkit_web_view_set_view_source_mode(t
->wv
, !mode
);
2399 webkit_web_view_reload(t
->wv
);
2405 focus_webview(struct tab
*t
)
2410 /* only grab focus if we are visible */
2411 if (gtk_notebook_get_current_page(notebook
) == t
->tab_id
)
2412 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
2416 focus(struct tab
*t
, struct karg
*args
)
2418 if (t
== NULL
|| args
== NULL
)
2424 if (args
->i
== XT_FOCUS_URI
)
2425 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
2426 else if (args
->i
== XT_FOCUS_SEARCH
)
2427 gtk_widget_grab_focus(GTK_WIDGET(t
->search_entry
));
2433 stats(struct tab
*t
, struct karg
*args
)
2435 char *stats
, *s
, line
[64 * 1024];
2436 uint64_t line_count
= 0;
2440 show_oops_s("stats invalid parameters");
2443 if (save_rejected_cookies
) {
2444 if ((r_cookie_f
= fopen(rc_fname
, "r"))) {
2446 s
= fgets(line
, sizeof line
, r_cookie_f
);
2447 if (s
== NULL
|| feof(r_cookie_f
) ||
2453 snprintf(line
, sizeof line
,
2454 "<br>Cookies blocked(*) total: %llu", line_count
);
2456 show_oops(t
, "Can't open blocked cookies file: %s",
2460 stats
= g_strdup_printf(XT_DOCTYPE
2463 "<title>Statistics</title>"
2465 "<h1>Statistics</h1>"
2467 "Cookies blocked(*) this session: %llu"
2469 "<p><small><b>*</b> results vary based on settings"
2475 load_webkit_string(t
, stats
, XT_URI_ABOUT_STATS
);
2482 marco(struct tab
*t
, struct karg
*args
)
2484 char *message
, line
[64 * 1024];
2488 show_oops_s("marco invalid parameters");
2491 snprintf(line
, sizeof line
, "<br>%s", marco_message(&len
));
2493 message
= g_strdup_printf(XT_DOCTYPE
2496 "<title>Marco Sez...</title>"
2505 load_webkit_string(t
, message
, XT_URI_ABOUT_MARCO
);
2512 blank(struct tab
*t
, struct karg
*args
)
2515 show_oops_s("about invalid parameters");
2517 load_webkit_string(t
, "", XT_URI_ABOUT_BLANK
);
2522 about(struct tab
*t
, struct karg
*args
)
2527 show_oops_s("about invalid parameters");
2529 about
= g_strdup_printf(XT_DOCTYPE
2532 "<title>About</title>"
2536 "<b>Version: %s</b><p>"
2539 "<li>Marco Peereboom <marco@peereboom.us></li>"
2540 "<li>Stevan Andjelkovic <stevan@student.chalmers.se></li>"
2541 "<li>Edd Barrett <vext01@gmail.com> </li>"
2542 "<li>Todd T. Fries <todd@fries.net> </li>"
2544 "Copyrights and licenses can be found on the XXXterm "
2545 "<a href=\"http://opensource.conformal.com/wiki/XXXTerm\">website</a>"
2551 load_webkit_string(t
, about
, XT_URI_ABOUT_ABOUT
);
2558 help(struct tab
*t
, struct karg
*args
)
2563 show_oops_s("help invalid parameters");
2568 "<title>XXXterm</title>"
2569 "<meta http-equiv=\"REFRESH\" content=\"0;"
2570 "url=http://opensource.conformal.com/cgi-bin/man-cgi?xxxterm\">"
2573 "XXXterm man page <a href=\"http://opensource.conformal.com/"
2574 "cgi-bin/man-cgi?xxxterm\">http://opensource.conformal.com/"
2575 "cgi-bin/man-cgi?xxxterm</a>"
2580 load_webkit_string(t
, help
, XT_URI_ABOUT_HELP
);
2586 * update all favorite tabs apart from one. Pass NULL if
2587 * you want to update all.
2590 update_favorite_tabs(struct tab
*apart_from
)
2593 if (!updating_fl_tabs
) {
2594 updating_fl_tabs
= 1; /* stop infinite recursion */
2595 TAILQ_FOREACH(t
, &tabs
, entry
)
2596 if ((t
->xtp_meaning
== XT_XTP_TAB_MEANING_FL
)
2597 && (t
!= apart_from
))
2598 xtp_page_fl(t
, NULL
);
2599 updating_fl_tabs
= 0;
2603 /* show a list of favorites (bookmarks) */
2605 xtp_page_fl(struct tab
*t
, struct karg
*args
)
2607 char file
[PATH_MAX
];
2609 char *uri
= NULL
, *title
= NULL
;
2610 size_t len
, lineno
= 0;
2612 char *header
, *body
, *tmp
, *html
= NULL
;
2614 DNPRINTF(XT_D_FAVORITE
, "%s:", __func__
);
2617 warn("%s: bad param", __func__
);
2619 /* mark tab as favorite list */
2620 t
->xtp_meaning
= XT_XTP_TAB_MEANING_FL
;
2622 /* new session key */
2623 if (!updating_fl_tabs
)
2624 generate_xtp_session_key(&fl_session_key
);
2626 /* open favorites */
2627 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_FAVS_FILE
);
2628 if ((f
= fopen(file
, "r")) == NULL
) {
2629 show_oops(t
, "Can't open favorites file: %s", strerror(errno
));
2634 header
= g_strdup_printf(XT_DOCTYPE XT_HTML_TAG
"\n<head>"
2635 "<title>Favorites</title>\n"
2638 "<h1>Favorites</h1>\n",
2642 body
= g_strdup_printf("<div align='center'><table><tr>"
2643 "<th style='width: 4%%'>#</th><th>Link</th>"
2644 "<th style='width: 15%%'>Remove</th></tr>\n");
2647 if ((title
= fparseln(f
, &len
, &lineno
, NULL
, 0)) == NULL
)
2648 if (feof(f
) || ferror(f
))
2656 if ((uri
= fparseln(f
, &len
, &lineno
, NULL
, 0)) == NULL
)
2657 if (feof(f
) || ferror(f
)) {
2658 show_oops(t
, "favorites file corrupt");
2664 body
= g_strdup_printf("%s<tr>"
2666 "<td><a href='%s'>%s</a></td>"
2667 "<td style='text-align: center'>"
2668 "<a href='%s%d/%s/%d/%d'>X</a></td>"
2670 body
, i
, uri
, title
,
2671 XT_XTP_STR
, XT_XTP_FL
, fl_session_key
, XT_XTP_FL_REMOVE
, i
);
2683 /* if none, say so */
2686 body
= g_strdup_printf("%s<tr>"
2687 "<td colspan='3' style='text-align: center'>"
2688 "No favorites - To add one use the 'favadd' command."
2689 "</td></tr>", body
);
2700 html
= g_strdup_printf("%s%s</table></div></html>",
2702 load_webkit_string(t
, html
, XT_URI_ABOUT_FAVORITES
);
2705 update_favorite_tabs(t
);
2718 show_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
2719 size_t cert_count
, char *title
)
2721 gnutls_datum_t cinfo
;
2722 char *tmp
, *header
, *body
, *footer
;
2725 header
= g_strdup_printf("<html><head><title>%s</title></head><body>", title
);
2726 footer
= g_strdup("</body></html>");
2727 body
= g_strdup("");
2729 for (i
= 0; i
< cert_count
; i
++) {
2730 if (gnutls_x509_crt_print(certs
[i
], GNUTLS_CRT_PRINT_FULL
,
2735 body
= g_strdup_printf("%s<h2>Cert #%d</h2><pre>%s</pre>",
2736 body
, i
, cinfo
.data
);
2737 gnutls_free(cinfo
.data
);
2741 tmp
= g_strdup_printf("%s%s%s", header
, body
, footer
);
2745 load_webkit_string(t
, tmp
, XT_URI_ABOUT_CERTS
);
2750 ca_cmd(struct tab
*t
, struct karg
*args
)
2753 int rv
= 1, certs
= 0, certs_read
;
2756 gnutls_x509_crt_t
*c
= NULL
;
2757 char *certs_buf
= NULL
, *s
;
2759 if ((f
= fopen(ssl_ca_file
, "r")) == NULL
) {
2760 show_oops(t
, "Can't open CA file: %s", strerror(errno
));
2764 if (fstat(fileno(f
), &sb
) == -1) {
2765 show_oops(t
, "Can't stat CA file: %s", strerror(errno
));
2769 certs_buf
= g_malloc(sb
.st_size
+ 1);
2770 if (fread(certs_buf
, 1, sb
.st_size
, f
) != sb
.st_size
) {
2771 show_oops(t
, "Can't read CA file: %s", strerror(errno
));
2774 certs_buf
[sb
.st_size
] = '\0';
2777 while ((s
= strstr(s
, "BEGIN CERTIFICATE"))) {
2779 s
+= strlen("BEGIN CERTIFICATE");
2782 bzero(&dt
, sizeof dt
);
2783 dt
.data
= certs_buf
;
2784 dt
.size
= sb
.st_size
;
2785 c
= g_malloc(sizeof(gnutls_x509_crt_t
) * certs
);
2786 certs_read
= gnutls_x509_crt_list_import(c
, &certs
, &dt
,
2787 GNUTLS_X509_FMT_PEM
, 0);
2788 if (certs_read
<= 0) {
2789 show_oops(t
, "No cert(s) available");
2792 show_certs(t
, c
, certs_read
, "Certificate Authority Certificates");
2805 connect_socket_from_uri(const gchar
*uri
, char *domain
, size_t domain_sz
)
2808 struct addrinfo hints
, *res
= NULL
, *ai
;
2812 if (uri
&& !g_str_has_prefix(uri
, "https://"))
2815 su
= soup_uri_new(uri
);
2818 if (!SOUP_URI_VALID_FOR_HTTP(su
))
2821 snprintf(port
, sizeof port
, "%d", su
->port
);
2822 bzero(&hints
, sizeof(struct addrinfo
));
2823 hints
.ai_flags
= AI_CANONNAME
;
2824 hints
.ai_family
= AF_UNSPEC
;
2825 hints
.ai_socktype
= SOCK_STREAM
;
2827 if (getaddrinfo(su
->host
, port
, &hints
, &res
))
2830 for (ai
= res
; ai
; ai
= ai
->ai_next
) {
2831 if (ai
->ai_family
!= AF_INET
&& ai
->ai_family
!= AF_INET6
)
2834 s
= socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
2837 if (setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &on
,
2841 if (connect(s
, ai
->ai_addr
, ai
->ai_addrlen
) < 0)
2846 strlcpy(domain
, su
->host
, domain_sz
);
2857 stop_tls(gnutls_session_t gsession
, gnutls_certificate_credentials_t xcred
)
2860 gnutls_deinit(gsession
);
2862 gnutls_certificate_free_credentials(xcred
);
2868 start_tls(struct tab
*t
, int s
, gnutls_session_t
*gs
,
2869 gnutls_certificate_credentials_t
*xc
)
2871 gnutls_certificate_credentials_t xcred
;
2872 gnutls_session_t gsession
;
2875 if (gs
== NULL
|| xc
== NULL
)
2881 gnutls_certificate_allocate_credentials(&xcred
);
2882 gnutls_certificate_set_x509_trust_file(xcred
, ssl_ca_file
,
2883 GNUTLS_X509_FMT_PEM
);
2884 gnutls_init(&gsession
, GNUTLS_CLIENT
);
2885 gnutls_priority_set_direct(gsession
, "PERFORMANCE", NULL
);
2886 gnutls_credentials_set(gsession
, GNUTLS_CRD_CERTIFICATE
, xcred
);
2887 gnutls_transport_set_ptr(gsession
, (gnutls_transport_ptr_t
)(long)s
);
2888 if ((rv
= gnutls_handshake(gsession
)) < 0) {
2889 show_oops(t
, "gnutls_handshake failed %d fatal %d %s",
2891 gnutls_error_is_fatal(rv
),
2892 gnutls_strerror_name(rv
));
2893 stop_tls(gsession
, xcred
);
2897 gnutls_credentials_type_t cred
;
2898 cred
= gnutls_auth_get_type(gsession
);
2899 if (cred
!= GNUTLS_CRD_CERTIFICATE
) {
2900 stop_tls(gsession
, xcred
);
2912 get_connection_certs(gnutls_session_t gsession
, gnutls_x509_crt_t
**certs
,
2916 const gnutls_datum_t
*cl
;
2917 gnutls_x509_crt_t
*all_certs
;
2920 if (certs
== NULL
|| cert_count
== NULL
)
2922 if (gnutls_certificate_type_get(gsession
) != GNUTLS_CRT_X509
)
2924 cl
= gnutls_certificate_get_peers(gsession
, &len
);
2928 all_certs
= g_malloc(sizeof(gnutls_x509_crt_t
) * len
);
2929 for (i
= 0; i
< len
; i
++) {
2930 gnutls_x509_crt_init(&all_certs
[i
]);
2931 if (gnutls_x509_crt_import(all_certs
[i
], &cl
[i
],
2932 GNUTLS_X509_FMT_PEM
< 0)) {
2946 free_connection_certs(gnutls_x509_crt_t
*certs
, size_t cert_count
)
2950 for (i
= 0; i
< cert_count
; i
++)
2951 gnutls_x509_crt_deinit(certs
[i
]);
2956 save_certs(struct tab
*t
, gnutls_x509_crt_t
*certs
,
2957 size_t cert_count
, char *domain
)
2960 char cert_buf
[64 * 1024], file
[PATH_MAX
];
2965 if (t
== NULL
|| certs
== NULL
|| cert_count
<= 0 || domain
== NULL
)
2968 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
2969 if ((f
= fopen(file
, "w")) == NULL
) {
2970 show_oops(t
, "Can't create cert file %s %s",
2971 file
, strerror(errno
));
2975 for (i
= 0; i
< cert_count
; i
++) {
2976 cert_buf_sz
= sizeof cert_buf
;
2977 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
2978 cert_buf
, &cert_buf_sz
)) {
2979 show_oops(t
, "gnutls_x509_crt_export failed");
2982 if (fwrite(cert_buf
, cert_buf_sz
, 1, f
) != 1) {
2983 show_oops(t
, "Can't write certs: %s", strerror(errno
));
2988 /* not the best spot but oh well */
2989 gdk_color_parse("lightblue", &color
);
2990 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
2991 gtk_widget_modify_base(t
->statusbar
, GTK_STATE_NORMAL
, &color
);
2992 gdk_color_parse(XT_COLOR_BLACK
, &color
);
2993 gtk_widget_modify_text(t
->statusbar
, GTK_STATE_NORMAL
, &color
);
2999 load_compare_cert(struct tab
*t
, struct karg
*args
)
3002 char domain
[8182], file
[PATH_MAX
];
3003 char cert_buf
[64 * 1024], r_cert_buf
[64 * 1024];
3004 int s
= -1, rv
= 1, i
;
3008 gnutls_session_t gsession
;
3009 gnutls_x509_crt_t
*certs
;
3010 gnutls_certificate_credentials_t xcred
;
3015 if ((uri
= get_uri(t
->wv
)) == NULL
)
3018 if ((s
= connect_socket_from_uri(uri
, domain
, sizeof domain
)) == -1)
3022 if (start_tls(t
, s
, &gsession
, &xcred
)) {
3023 show_oops(t
, "Start TLS failed");
3028 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
3029 show_oops(t
, "Can't get connection certificates");
3033 snprintf(file
, sizeof file
, "%s/%s", certs_dir
, domain
);
3034 if ((f
= fopen(file
, "r")) == NULL
)
3037 for (i
= 0; i
< cert_count
; i
++) {
3038 cert_buf_sz
= sizeof cert_buf
;
3039 if (gnutls_x509_crt_export(certs
[i
], GNUTLS_X509_FMT_PEM
,
3040 cert_buf
, &cert_buf_sz
)) {
3043 if (fread(r_cert_buf
, cert_buf_sz
, 1, f
) != 1) {
3044 rv
= -1; /* critical */
3047 if (bcmp(r_cert_buf
, cert_buf
, sizeof cert_buf_sz
)) {
3048 rv
= -1; /* critical */
3057 free_connection_certs(certs
, cert_count
);
3059 /* we close the socket first for speed */
3062 stop_tls(gsession
, xcred
);
3068 cert_cmd(struct tab
*t
, struct karg
*args
)
3074 gnutls_session_t gsession
;
3075 gnutls_x509_crt_t
*certs
;
3076 gnutls_certificate_credentials_t xcred
;
3081 if ((uri
= get_uri(t
->wv
)) == NULL
) {
3082 show_oops(t
, "Invalid URI");
3086 if ((s
= connect_socket_from_uri(uri
, domain
, sizeof domain
)) == -1) {
3087 show_oops(t
, "Invalid certidicate URI: %s", uri
);
3092 if (start_tls(t
, s
, &gsession
, &xcred
)) {
3093 show_oops(t
, "Start TLS failed");
3098 if (get_connection_certs(gsession
, &certs
, &cert_count
)) {
3099 show_oops(t
, "get_connection_certs failed");
3103 if (args
->i
& XT_SHOW
)
3104 show_certs(t
, certs
, cert_count
, "Certificate Chain");
3105 else if (args
->i
& XT_SAVE
)
3106 save_certs(t
, certs
, cert_count
, domain
);
3108 free_connection_certs(certs
, cert_count
);
3110 /* we close the socket first for speed */
3113 stop_tls(gsession
, xcred
);
3119 remove_cookie(int index
)
3125 DNPRINTF(XT_D_COOKIE
, "remove_cookie: %d\n", index
);
3127 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
3129 for (i
= 1; cf
; cf
= cf
->next
, i
++) {
3133 print_cookie("remove cookie", c
);
3134 soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
3139 soup_cookies_free(cf
);
3145 wl_show(struct tab
*t
, struct karg
*args
, char *title
, struct domain_list
*wl
)
3148 char *tmp
, *header
, *body
, *footer
;
3150 /* we set this to indicate we want to manually do navaction */
3151 t
->item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
3153 header
= g_strdup_printf("<title>%s</title><html><body><h1>%s</h1>",
3155 footer
= g_strdup("</body></html>");
3156 body
= g_strdup("");
3159 if (args
->i
& XT_WL_PERSISTENT
) {
3161 body
= g_strdup_printf("%s<h2>Persistent</h2>", body
);
3163 RB_FOREACH(d
, domain_list
, wl
) {
3167 body
= g_strdup_printf("%s%s<br>", body
, d
->d
);
3173 if (args
->i
& XT_WL_SESSION
) {
3175 body
= g_strdup_printf("%s<h2>Session</h2>", body
);
3177 RB_FOREACH(d
, domain_list
, wl
) {
3181 body
= g_strdup_printf("%s%s<br>", body
, d
->d
);
3186 tmp
= g_strdup_printf("%s%s%s", header
, body
, footer
);
3191 load_webkit_string(t
, tmp
, XT_URI_ABOUT_JSWL
);
3193 load_webkit_string(t
, tmp
, XT_URI_ABOUT_COOKIEWL
);
3199 wl_save(struct tab
*t
, struct karg
*args
, int js
)
3201 char file
[PATH_MAX
];
3203 char *line
= NULL
, *lt
= NULL
;
3206 char *dom
= NULL
, *dom_save
= NULL
;
3212 if (t
== NULL
|| args
== NULL
)
3215 if (runtime_settings
[0] == '\0')
3218 snprintf(file
, sizeof file
, "%s/%s", work_dir
, runtime_settings
);
3219 if ((f
= fopen(file
, "r+")) == NULL
)
3222 uri
= get_uri(t
->wv
);
3223 dom
= find_domain(uri
, 1);
3224 if (uri
== NULL
|| dom
== NULL
) {
3225 show_oops(t
, "Can't add domain to %s white list",
3226 js
? "JavaScript" : "cookie");
3230 if (args
->i
& XT_WL_TOPLEVEL
) {
3232 if ((dom_save
= get_toplevel_domain(dom
)) == NULL
) {
3233 show_oops(t
, "invalid domain: %s", dom
);
3236 } else if (args
->i
& XT_WL_FQDN
) {
3242 lt
= g_strdup_printf("%s=%s", js
? "js_wl" : "cookie_wl", dom_save
);
3245 line
= fparseln(f
, &linelen
, NULL
, NULL
, 0);
3248 if (!strcmp(line
, lt
))
3254 fprintf(f
, "%s\n", lt
);
3259 d
= wl_find(dom_save
, &js_wl
);
3261 settings_add("js_wl", dom_save
);
3262 d
= wl_find(dom_save
, &js_wl
);
3266 d
= wl_find(dom_save
, &c_wl
);
3268 settings_add("cookie_wl", dom_save
);
3269 d
= wl_find(dom_save
, &c_wl
);
3273 /* find and add to persistent jar */
3274 cf
= soup_cookie_jar_all_cookies(s_cookiejar
);
3275 for (;cf
; cf
= cf
->next
) {
3277 if (!strcmp(dom_save
, ci
->domain
) ||
3278 !strcmp(&dom_save
[1], ci
->domain
)) /* deal with leading . */ {
3279 c
= soup_cookie_copy(ci
);
3280 _soup_cookie_jar_add_cookie(p_cookiejar
, c
);
3283 soup_cookies_free(cf
);
3301 js_show_wl(struct tab
*t
, struct karg
*args
)
3303 args
->i
= XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
;
3304 wl_show(t
, args
, "JavaScript White List", &js_wl
);
3310 cookie_show_wl(struct tab
*t
, struct karg
*args
)
3312 args
->i
= XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
;
3313 wl_show(t
, args
, "Cookie White List", &c_wl
);
3319 cookie_cmd(struct tab
*t
, struct karg
*args
)
3321 if (args
->i
& XT_SHOW
)
3322 wl_show(t
, args
, "Cookie White List", &c_wl
);
3323 else if (args
->i
& XT_WL_TOGGLE
)
3324 toggle_cwl(t
, args
);
3325 else if (args
->i
& XT_SAVE
)
3326 wl_save(t
, args
, 0);
3327 else if (args
->i
& XT_DELETE
)
3328 show_oops(t
, "'cookie delete' currently unimplemented");
3334 js_cmd(struct tab
*t
, struct karg
*args
)
3336 if (args
->i
& XT_SHOW
)
3337 wl_show(t
, args
, "JavaScript White List", &js_wl
);
3338 else if (args
->i
& XT_SAVE
)
3339 wl_save(t
, args
, 1);
3340 else if (args
->i
& XT_WL_TOGGLE
)
3342 else if (args
->i
& XT_DELETE
)
3343 show_oops(t
, "'js delete' currently unimplemented");
3349 add_favorite(struct tab
*t
, struct karg
*args
)
3351 char file
[PATH_MAX
];
3354 size_t urilen
, linelen
;
3355 const gchar
*uri
, *title
;
3360 /* don't allow adding of xtp pages to favorites */
3361 if (t
->xtp_meaning
!= XT_XTP_TAB_MEANING_NORMAL
) {
3362 show_oops(t
, "%s: can't add xtp pages to favorites", __func__
);
3366 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_FAVS_FILE
);
3367 if ((f
= fopen(file
, "r+")) == NULL
) {
3368 show_oops(t
, "Can't open favorites file: %s", strerror(errno
));
3372 title
= webkit_web_view_get_title(t
->wv
);
3373 uri
= get_uri(t
->wv
);
3378 if (title
== NULL
|| uri
== NULL
) {
3379 show_oops(t
, "can't add page to favorites");
3383 urilen
= strlen(uri
);
3386 if ((line
= fparseln(f
, &linelen
, NULL
, NULL
, 0)) == NULL
)
3387 if (feof(f
) || ferror(f
))
3390 if (linelen
== urilen
&& !strcmp(line
, uri
))
3397 fprintf(f
, "\n%s\n%s", title
, uri
);
3403 update_favorite_tabs(NULL
);
3409 navaction(struct tab
*t
, struct karg
*args
)
3411 WebKitWebHistoryItem
*item
;
3413 DNPRINTF(XT_D_NAV
, "navaction: tab %d opcode %d\n",
3414 t
->tab_id
, args
->i
);
3417 if (args
->i
== XT_NAV_BACK
)
3418 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
3420 item
= webkit_web_back_forward_list_get_forward_item(t
->bfl
);
3422 return (XT_CB_PASSTHROUGH
);
3423 webkit_web_view_load_uri(t
->wv
, webkit_web_history_item_get_uri(item
));
3425 return (XT_CB_PASSTHROUGH
);
3430 webkit_web_view_go_back(t
->wv
);
3432 case XT_NAV_FORWARD
:
3433 webkit_web_view_go_forward(t
->wv
);
3436 webkit_web_view_reload(t
->wv
);
3438 case XT_NAV_RELOAD_CACHE
:
3439 webkit_web_view_reload_bypass_cache(t
->wv
);
3442 return (XT_CB_PASSTHROUGH
);
3446 move(struct tab
*t
, struct karg
*args
)
3448 GtkAdjustment
*adjust
;
3449 double pi
, si
, pos
, ps
, upper
, lower
, max
;
3454 case XT_MOVE_BOTTOM
:
3456 case XT_MOVE_PAGEDOWN
:
3457 case XT_MOVE_PAGEUP
:
3458 case XT_MOVE_HALFDOWN
:
3459 case XT_MOVE_HALFUP
:
3460 adjust
= t
->adjust_v
;
3463 adjust
= t
->adjust_h
;
3467 pos
= gtk_adjustment_get_value(adjust
);
3468 ps
= gtk_adjustment_get_page_size(adjust
);
3469 upper
= gtk_adjustment_get_upper(adjust
);
3470 lower
= gtk_adjustment_get_lower(adjust
);
3471 si
= gtk_adjustment_get_step_increment(adjust
);
3472 pi
= gtk_adjustment_get_page_increment(adjust
);
3475 DNPRINTF(XT_D_MOVE
, "move: opcode %d %s pos %f ps %f upper %f lower %f "
3476 "max %f si %f pi %f\n",
3477 args
->i
, adjust
== t
->adjust_h
? "horizontal" : "vertical",
3478 pos
, ps
, upper
, lower
, max
, si
, pi
);
3484 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
3489 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
3491 case XT_MOVE_BOTTOM
:
3492 case XT_MOVE_FARRIGHT
:
3493 gtk_adjustment_set_value(adjust
, max
);
3496 case XT_MOVE_FARLEFT
:
3497 gtk_adjustment_set_value(adjust
, lower
);
3499 case XT_MOVE_PAGEDOWN
:
3501 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
3503 case XT_MOVE_PAGEUP
:
3505 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
3507 case XT_MOVE_HALFDOWN
:
3509 gtk_adjustment_set_value(adjust
, MIN(pos
, max
));
3511 case XT_MOVE_HALFUP
:
3513 gtk_adjustment_set_value(adjust
, MAX(pos
, lower
));
3516 return (XT_CB_PASSTHROUGH
);
3519 DNPRINTF(XT_D_MOVE
, "move: new pos %f %f\n", pos
, MIN(pos
, max
));
3521 return (XT_CB_HANDLED
);
3525 url_set_visibility(void)
3529 TAILQ_FOREACH(t
, &tabs
, entry
) {
3530 if (show_url
== 0) {
3531 gtk_widget_hide(t
->toolbar
);
3534 gtk_widget_show(t
->toolbar
);
3539 notebook_tab_set_visibility(GtkNotebook
*notebook
)
3542 gtk_notebook_set_show_tabs(notebook
, FALSE
);
3544 gtk_notebook_set_show_tabs(notebook
, TRUE
);
3548 statusbar_set_visibility(void)
3552 TAILQ_FOREACH(t
, &tabs
, entry
) {
3553 if (show_statusbar
== 0) {
3554 gtk_widget_hide(t
->statusbar
);
3557 gtk_widget_show(t
->statusbar
);
3562 url_set(struct tab
*t
, int enable_url_entry
)
3567 show_url
= enable_url_entry
;
3569 if (enable_url_entry
) {
3570 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->statusbar
),
3571 GTK_ENTRY_ICON_PRIMARY
, NULL
);
3572 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->statusbar
), 0);
3574 pixbuf
= gtk_entry_get_icon_pixbuf(GTK_ENTRY(t
->uri_entry
),
3575 GTK_ENTRY_ICON_PRIMARY
);
3577 gtk_entry_get_progress_fraction(GTK_ENTRY(t
->uri_entry
));
3578 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->statusbar
),
3579 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
3580 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->statusbar
),
3586 fullscreen(struct tab
*t
, struct karg
*args
)
3588 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
3591 return (XT_CB_PASSTHROUGH
);
3593 if (show_url
== 0) {
3601 url_set_visibility();
3602 notebook_tab_set_visibility(notebook
);
3604 return (XT_CB_HANDLED
);
3608 statusaction(struct tab
*t
, struct karg
*args
)
3610 int rv
= XT_CB_HANDLED
;
3612 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
3615 return (XT_CB_PASSTHROUGH
);
3618 case XT_STATUSBAR_SHOW
:
3619 if (show_statusbar
== 0) {
3621 statusbar_set_visibility();
3624 case XT_STATUSBAR_HIDE
:
3625 if (show_statusbar
== 1) {
3627 statusbar_set_visibility();
3635 urlaction(struct tab
*t
, struct karg
*args
)
3637 int rv
= XT_CB_HANDLED
;
3639 DNPRINTF(XT_D_TAB
, "%s: %p %d\n", __func__
, t
, args
->i
);
3642 return (XT_CB_PASSTHROUGH
);
3646 if (show_url
== 0) {
3648 url_set_visibility();
3652 if (show_url
== 1) {
3654 url_set_visibility();
3662 tabaction(struct tab
*t
, struct karg
*args
)
3664 int rv
= XT_CB_HANDLED
;
3665 char *url
= args
->s
;
3668 DNPRINTF(XT_D_TAB
, "tabaction: %p %d\n", t
, args
->i
);
3671 return (XT_CB_PASSTHROUGH
);
3675 if (strlen(url
) > 0)
3676 create_new_tab(url
, NULL
, 1);
3678 create_new_tab(NULL
, NULL
, 1);
3683 case XT_TAB_DELQUIT
:
3684 if (gtk_notebook_get_n_pages(notebook
) > 1)
3690 if (strlen(url
) > 0)
3693 rv
= XT_CB_PASSTHROUGH
;
3699 if (show_tabs
== 0) {
3701 notebook_tab_set_visibility(notebook
);
3705 if (show_tabs
== 1) {
3707 notebook_tab_set_visibility(notebook
);
3710 case XT_TAB_UNDO_CLOSE
:
3711 if (undo_count
== 0) {
3712 DNPRINTF(XT_D_TAB
, "%s: no tabs to undo close", __func__
);
3716 u
= TAILQ_FIRST(&undos
);
3717 create_new_tab(u
->uri
, u
, 1);
3719 TAILQ_REMOVE(&undos
, u
, entry
);
3721 /* u->history is freed in create_new_tab() */
3726 rv
= XT_CB_PASSTHROUGH
;
3740 resizetab(struct tab
*t
, struct karg
*args
)
3742 if (t
== NULL
|| args
== NULL
) {
3743 show_oops_s("resizetab invalid parameters");
3744 return (XT_CB_PASSTHROUGH
);
3747 DNPRINTF(XT_D_TAB
, "resizetab: tab %d %d\n",
3748 t
->tab_id
, args
->i
);
3750 adjustfont_webkit(t
, args
->i
);
3752 return (XT_CB_HANDLED
);
3756 movetab(struct tab
*t
, struct karg
*args
)
3761 if (t
== NULL
|| args
== NULL
) {
3762 show_oops_s("movetab invalid parameters");
3763 return (XT_CB_PASSTHROUGH
);
3766 DNPRINTF(XT_D_TAB
, "movetab: tab %d opcode %d\n",
3767 t
->tab_id
, args
->i
);
3769 if (args
->i
== XT_TAB_INVALID
)
3770 return (XT_CB_PASSTHROUGH
);
3772 if (args
->i
< XT_TAB_INVALID
) {
3773 /* next or previous tab */
3774 if (TAILQ_EMPTY(&tabs
))
3775 return (XT_CB_PASSTHROUGH
);
3779 if (strlen(args
->s
) == 0) {
3780 /* if at the last page, loop around to the first */
3781 if (gtk_notebook_get_current_page(notebook
) ==
3782 gtk_notebook_get_n_pages(notebook
) - 1)
3783 gtk_notebook_set_current_page(notebook
, 0);
3785 gtk_notebook_next_page(notebook
);
3787 x
= atoi(args
->s
) - 1;
3789 return (XT_CB_PASSTHROUGH
);
3791 if (t
->tab_id
== x
) {
3792 DNPRINTF(XT_D_TAB
, "movetab: do nothing\n");
3793 return (XT_CB_HANDLED
);
3795 TAILQ_FOREACH(tt
, &tabs
, entry
) {
3796 if (tt
->tab_id
== x
) {
3797 gtk_notebook_set_current_page(notebook
, x
);
3798 DNPRINTF(XT_D_TAB
, "movetab: going to %d\n", x
);
3806 /* if at the first page, loop around to the last */
3807 if (gtk_notebook_current_page(notebook
) == 0)
3808 gtk_notebook_set_current_page(notebook
,
3809 gtk_notebook_get_n_pages(notebook
) - 1);
3811 gtk_notebook_prev_page(notebook
);
3814 gtk_notebook_set_current_page(notebook
, 0);
3817 gtk_notebook_set_current_page(notebook
, -1);
3820 return (XT_CB_PASSTHROUGH
);
3823 return (XT_CB_HANDLED
);
3826 return (XT_CB_HANDLED
);
3830 command(struct tab
*t
, struct karg
*args
)
3832 char *s
= NULL
, *ss
= NULL
;
3836 if (t
== NULL
|| args
== NULL
) {
3837 show_oops_s("command invalid parameters");
3838 return (XT_CB_PASSTHROUGH
);
3857 case XT_CMD_OPEN_CURRENT
:
3860 case XT_CMD_TABNEW_CURRENT
:
3861 if (!s
) /* FALL THROUGH? */
3863 if ((uri
= get_uri(t
->wv
)) != NULL
) {
3864 ss
= g_strdup_printf("%s%s", s
, uri
);
3869 show_oops(t
, "command: invalid opcode %d", args
->i
);
3870 return (XT_CB_PASSTHROUGH
);
3873 DNPRINTF(XT_D_CMD
, "command: type %s\n", s
);
3875 gtk_entry_set_text(GTK_ENTRY(t
->cmd
), s
);
3876 gdk_color_parse(XT_COLOR_WHITE
, &color
);
3877 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
3879 gtk_widget_grab_focus(GTK_WIDGET(t
->cmd
));
3880 gtk_editable_set_position(GTK_EDITABLE(t
->cmd
), -1);
3885 return (XT_CB_HANDLED
);
3889 * Return a new string with a download row (in html)
3890 * appended. Old string is freed.
3893 xtp_page_dl_row(struct tab
*t
, char *html
, struct download
*dl
)
3896 WebKitDownloadStatus stat
;
3897 char *status_html
= NULL
, *cmd_html
= NULL
, *new_html
;
3899 char cur_sz
[FMT_SCALED_STRSIZE
];
3900 char tot_sz
[FMT_SCALED_STRSIZE
];
3903 DNPRINTF(XT_D_DOWNLOAD
, "%s: dl->id %d\n", __func__
, dl
->id
);
3905 /* All actions wil take this form:
3906 * xxxt://class/seskey
3908 xtp_prefix
= g_strdup_printf("%s%d/%s/",
3909 XT_XTP_STR
, XT_XTP_DL
, dl_session_key
);
3911 stat
= webkit_download_get_status(dl
->download
);
3914 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
3915 status_html
= g_strdup_printf("Finished");
3916 cmd_html
= g_strdup_printf(
3917 "<a href='%s%d/%d'>Remove</a>",
3918 xtp_prefix
, XT_XTP_DL_REMOVE
, dl
->id
);
3920 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
3921 /* gather size info */
3922 progress
= 100 * webkit_download_get_progress(dl
->download
);
3925 webkit_download_get_current_size(dl
->download
), cur_sz
);
3927 webkit_download_get_total_size(dl
->download
), tot_sz
);
3929 status_html
= g_strdup_printf(
3930 "<div style='width: 100%%' align='center'>"
3931 "<div class='progress-outer'>"
3932 "<div class='progress-inner' style='width: %.2f%%'>"
3933 "</div></div></div>"
3934 "<div class='dlstatus'>%s of %s (%.2f%%)</div>",
3935 progress
, cur_sz
, tot_sz
, progress
);
3937 cmd_html
= g_strdup_printf("<a href='%s%d/%d'>Cancel</a>",
3938 xtp_prefix
, XT_XTP_DL_CANCEL
, dl
->id
);
3942 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
3943 status_html
= g_strdup_printf("Cancelled");
3944 cmd_html
= g_strdup_printf("<a href='%s%d/%d'>Remove</a>",
3945 xtp_prefix
, XT_XTP_DL_REMOVE
, dl
->id
);
3947 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
3948 status_html
= g_strdup_printf("Error!");
3949 cmd_html
= g_strdup_printf("<a href='%s%d/%d'>Remove</a>",
3950 xtp_prefix
, XT_XTP_DL_REMOVE
, dl
->id
);
3952 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
3953 cmd_html
= g_strdup_printf("<a href='%s%d/%d'>Cancel</a>",
3954 xtp_prefix
, XT_XTP_DL_CANCEL
, dl
->id
);
3955 status_html
= g_strdup_printf("Starting");
3958 show_oops(t
, "%s: unknown download status", __func__
);
3961 new_html
= g_strdup_printf(
3962 "%s\n<tr><td>%s</td><td>%s</td>"
3963 "<td style='text-align:center'>%s</td></tr>\n",
3964 html
, basename(webkit_download_get_destination_uri(dl
->download
)),
3965 status_html
, cmd_html
);
3969 g_free(status_html
);
3980 * update all download tabs apart from one. Pass NULL if
3981 * you want to update all.
3984 update_download_tabs(struct tab
*apart_from
)
3987 if (!updating_dl_tabs
) {
3988 updating_dl_tabs
= 1; /* stop infinite recursion */
3989 TAILQ_FOREACH(t
, &tabs
, entry
)
3990 if ((t
->xtp_meaning
== XT_XTP_TAB_MEANING_DL
)
3991 && (t
!= apart_from
))
3992 xtp_page_dl(t
, NULL
);
3993 updating_dl_tabs
= 0;
3998 * update all cookie tabs apart from one. Pass NULL if
3999 * you want to update all.
4002 update_cookie_tabs(struct tab
*apart_from
)
4005 if (!updating_cl_tabs
) {
4006 updating_cl_tabs
= 1; /* stop infinite recursion */
4007 TAILQ_FOREACH(t
, &tabs
, entry
)
4008 if ((t
->xtp_meaning
== XT_XTP_TAB_MEANING_CL
)
4009 && (t
!= apart_from
))
4010 xtp_page_cl(t
, NULL
);
4011 updating_cl_tabs
= 0;
4016 * update all history tabs apart from one. Pass NULL if
4017 * you want to update all.
4020 update_history_tabs(struct tab
*apart_from
)
4024 if (!updating_hl_tabs
) {
4025 updating_hl_tabs
= 1; /* stop infinite recursion */
4026 TAILQ_FOREACH(t
, &tabs
, entry
)
4027 if ((t
->xtp_meaning
== XT_XTP_TAB_MEANING_HL
)
4028 && (t
!= apart_from
))
4029 xtp_page_hl(t
, NULL
);
4030 updating_hl_tabs
= 0;
4034 /* cookie management XTP page */
4036 xtp_page_cl(struct tab
*t
, struct karg
*args
)
4038 char *header
, *body
, *footer
, *page
, *tmp
;
4039 int i
= 1; /* all ids start 1 */
4040 GSList
*sc
, *pc
, *pc_start
;
4042 char *type
, *table_headers
;
4043 char *last_domain
= strdup("");
4045 DNPRINTF(XT_D_CMD
, "%s", __func__
);
4048 show_oops_s("%s invalid parameters", __func__
);
4051 /* mark this tab as cookie jar */
4052 t
->xtp_meaning
= XT_XTP_TAB_MEANING_CL
;
4054 /* Generate a new session key */
4055 if (!updating_cl_tabs
)
4056 generate_xtp_session_key(&cl_session_key
);
4059 header
= g_strdup_printf(XT_DOCTYPE XT_HTML_TAG
4060 "\n<head><title>Cookie Jar</title>\n" XT_PAGE_STYLE
4061 "</head><body><h1>Cookie Jar</h1>\n");
4064 table_headers
= g_strdup_printf("<div align='center'><table><tr>"
4071 "<th>HTTP<br />only</th>"
4072 "<th>Rm</th></tr>\n");
4074 sc
= soup_cookie_jar_all_cookies(s_cookiejar
);
4075 pc
= soup_cookie_jar_all_cookies(p_cookiejar
);
4079 for (; sc
; sc
= sc
->next
) {
4082 if (strcmp(last_domain
, c
->domain
) != 0) {
4085 last_domain
= strdup(c
->domain
);
4089 body
= g_strdup_printf("%s</table></div>"
4091 body
, c
->domain
, table_headers
);
4095 body
= g_strdup_printf("<h2>%s</h2>%s\n",
4096 c
->domain
, table_headers
);
4101 for (pc
= pc_start
; pc
; pc
= pc
->next
)
4102 if (soup_cookie_equal(pc
->data
, c
)) {
4103 type
= "Session + Persistent";
4108 body
= g_strdup_printf(
4110 "<td style='width: text-align: center'>%s</td>"
4111 "<td style='width: 1px'>%s</td>"
4112 "<td style='width=70%%;overflow: visible'>"
4113 " <textarea rows='4'>%s</textarea>"
4117 "<td style='width: 1px; text-align: center'>%d</td>"
4118 "<td style='width: 1px; text-align: center'>%d</td>"
4119 "<td style='width: 1px; text-align: center'>"
4120 "<a href='%s%d/%s/%d/%d'>X</a></td></tr>\n",
4127 soup_date_to_string(c
->expires
, SOUP_DATE_COOKIE
) : "",
4142 soup_cookies_free(sc
);
4143 soup_cookies_free(pc
);
4145 /* small message if there are none */
4147 body
= g_strdup_printf("%s\n<tr><td style='text-align:center'"
4148 "colspan='8'>No Cookies</td></tr>\n", table_headers
);
4152 footer
= g_strdup_printf("</table></div></body></html>");
4154 page
= g_strdup_printf("%s%s%s", header
, body
, footer
);
4159 g_free(table_headers
);
4160 g_free(last_domain
);
4162 load_webkit_string(t
, page
, XT_URI_ABOUT_COOKIEJAR
);
4163 update_cookie_tabs(t
);
4171 xtp_page_hl(struct tab
*t
, struct karg
*args
)
4173 char *header
, *body
, *footer
, *page
, *tmp
;
4175 int i
= 1; /* all ids start 1 */
4177 DNPRINTF(XT_D_CMD
, "%s", __func__
);
4180 show_oops_s("%s invalid parameters", __func__
);
4184 /* mark this tab as history manager */
4185 t
->xtp_meaning
= XT_XTP_TAB_MEANING_HL
;
4187 /* Generate a new session key */
4188 if (!updating_hl_tabs
)
4189 generate_xtp_session_key(&hl_session_key
);
4192 header
= g_strdup_printf(XT_DOCTYPE XT_HTML_TAG
"\n<head>"
4193 "<title>History</title>\n"
4196 "<h1>History</h1>\n",
4200 body
= g_strdup_printf("<div align='center'><table><tr>"
4201 "<th>URI</th><th>Title</th><th style='width: 15%%'>Remove</th></tr>\n");
4203 RB_FOREACH_REVERSE(h
, history_list
, &hl
) {
4205 body
= g_strdup_printf(
4207 "<td><a href='%s'>%s</a></td>"
4209 "<td style='text-align: center'>"
4210 "<a href='%s%d/%s/%d/%d'>X</a></td></tr>\n",
4211 body
, h
->uri
, h
->uri
, h
->title
,
4212 XT_XTP_STR
, XT_XTP_HL
, hl_session_key
,
4213 XT_XTP_HL_REMOVE
, i
);
4219 /* small message if there are none */
4222 body
= g_strdup_printf("%s\n<tr><td style='text-align:center'"
4223 "colspan='3'>No History</td></tr>\n", body
);
4228 footer
= g_strdup_printf("</table></div></body></html>");
4230 page
= g_strdup_printf("%s%s%s", header
, body
, footer
);
4233 * update all history manager tabs as the xtp session
4234 * key has now changed. No need to update the current tab.
4235 * Already did that above.
4237 update_history_tabs(t
);
4243 load_webkit_string(t
, page
, XT_URI_ABOUT_HISTORY
);
4250 * Generate a web page detailing the status of any downloads
4253 xtp_page_dl(struct tab
*t
, struct karg
*args
)
4255 struct download
*dl
;
4256 char *header
, *body
, *footer
, *page
, *tmp
;
4260 DNPRINTF(XT_D_DOWNLOAD
, "%s", __func__
);
4263 show_oops_s("%s invalid parameters", __func__
);
4266 /* mark as a download manager tab */
4267 t
->xtp_meaning
= XT_XTP_TAB_MEANING_DL
;
4270 * Generate a new session key for next page instance.
4271 * This only happens for the top level call to xtp_page_dl()
4272 * in which case updating_dl_tabs is 0.
4274 if (!updating_dl_tabs
)
4275 generate_xtp_session_key(&dl_session_key
);
4277 /* header - with refresh so as to update */
4278 if (refresh_interval
>= 1)
4279 ref
= g_strdup_printf(
4280 "<meta http-equiv='refresh' content='%u"
4281 ";url=%s%d/%s/%d' />\n",
4291 header
= g_strdup_printf(
4293 "<title>Downloads</title>\n%s%s</head>\n",
4294 XT_DOCTYPE XT_HTML_TAG
,
4298 body
= g_strdup_printf("<body><h1>Downloads</h1><div align='center'>"
4299 "<p>\n<a href='%s%d/%s/%d'>\n[ Refresh Downloads ]</a>\n"
4300 "</p><table><tr><th style='width: 60%%'>"
4301 "File</th>\n<th>Progress</th><th>Command</th></tr>\n",
4302 XT_XTP_STR
, XT_XTP_DL
, dl_session_key
, XT_XTP_DL_LIST
);
4304 RB_FOREACH_REVERSE(dl
, download_list
, &downloads
) {
4305 body
= xtp_page_dl_row(t
, body
, dl
);
4309 /* message if no downloads in list */
4312 body
= g_strdup_printf("%s\n<tr><td colspan='3'"
4313 " style='text-align: center'>"
4314 "No downloads</td></tr>\n", body
);
4319 footer
= g_strdup_printf("</table></div></body></html>");
4321 page
= g_strdup_printf("%s%s%s", header
, body
, footer
);
4325 * update all download manager tabs as the xtp session
4326 * key has now changed. No need to update the current tab.
4327 * Already did that above.
4329 update_download_tabs(t
);
4336 load_webkit_string(t
, page
, XT_URI_ABOUT_DOWNLOADS
);
4343 search(struct tab
*t
, struct karg
*args
)
4347 if (t
== NULL
|| args
== NULL
) {
4348 show_oops_s("search invalid parameters");
4351 if (t
->search_text
== NULL
) {
4352 if (global_search
== NULL
)
4353 return (XT_CB_PASSTHROUGH
);
4355 t
->search_text
= g_strdup(global_search
);
4356 webkit_web_view_mark_text_matches(t
->wv
, global_search
, FALSE
, 0);
4357 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
4361 DNPRINTF(XT_D_CMD
, "search: tab %d opc %d forw %d text %s\n",
4362 t
->tab_id
, args
->i
, t
->search_forward
, t
->search_text
);
4365 case XT_SEARCH_NEXT
:
4366 d
= t
->search_forward
;
4368 case XT_SEARCH_PREV
:
4369 d
= !t
->search_forward
;
4372 return (XT_CB_PASSTHROUGH
);
4375 webkit_web_view_search_text(t
->wv
, t
->search_text
, FALSE
, d
, TRUE
);
4377 return (XT_CB_HANDLED
);
4380 struct settings_args
{
4386 print_setting(struct settings
*s
, char *val
, void *cb_args
)
4389 struct settings_args
*sa
= cb_args
;
4394 if (s
->flags
& XT_SF_RUNTIME
)
4400 *sa
->body
= g_strdup_printf(
4402 "<td style='background-color: %s; width: 10%%; word-break: break-all'>%s</td>"
4403 "<td style='background-color: %s; width: 20%%; word-break: break-all'>%s</td>",
4415 set(struct tab
*t
, struct karg
*args
)
4417 char *header
, *body
, *footer
, *page
, *tmp
;
4419 struct settings_args sa
;
4421 bzero(&sa
, sizeof sa
);
4425 header
= g_strdup_printf(XT_DOCTYPE XT_HTML_TAG
4426 "\n<head><title>Settings</title>\n"
4427 "</head><body><h1>Settings</h1>\n");
4430 body
= g_strdup_printf("<div align='center'><table><tr>"
4431 "<th align='left'>Setting</th>"
4432 "<th align='left'>Value</th></tr>\n");
4434 settings_walk(print_setting
, &sa
);
4437 /* small message if there are none */
4440 body
= g_strdup_printf("%s\n<tr><td style='text-align:center'"
4441 "colspan='2'>No settings</td></tr>\n", body
);
4446 footer
= g_strdup_printf("</table></div></body></html>");
4448 page
= g_strdup_printf("%s%s%s", header
, body
, footer
);
4454 load_webkit_string(t
, page
, XT_URI_ABOUT_SET
);
4456 return (XT_CB_PASSTHROUGH
);
4460 session_save(struct tab
*t
, char *filename
)
4465 if (strlen(filename
) == 0)
4468 if (filename
[0] == '.' || filename
[0] == '/')
4472 if (save_tabs(t
, &a
))
4474 strlcpy(named_session
, filename
, sizeof named_session
);
4482 session_open(struct tab
*t
, char *filename
)
4487 if (strlen(filename
) == 0)
4490 if (filename
[0] == '.' || filename
[0] == '/')
4494 a
.i
= XT_SES_CLOSETABS
;
4495 if (open_tabs(t
, &a
))
4498 strlcpy(named_session
, filename
, sizeof named_session
);
4506 session_delete(struct tab
*t
, char *filename
)
4508 char file
[PATH_MAX
];
4511 if (strlen(filename
) == 0)
4514 if (filename
[0] == '.' || filename
[0] == '/')
4517 snprintf(file
, sizeof file
, "%s/%s", sessions_dir
, filename
);
4521 if (!strcmp(filename
, named_session
))
4522 strlcpy(named_session
, XT_SAVED_TABS_FILE
,
4523 sizeof named_session
);
4531 session_cmd(struct tab
*t
, struct karg
*args
)
4533 char *filename
= args
->s
;
4538 if (args
->i
& XT_SHOW
)
4539 show_oops(t
, "Current session: %s", named_session
[0] == '\0' ?
4540 XT_SAVED_TABS_FILE
: named_session
);
4541 else if (args
->i
& XT_SAVE
) {
4542 if (session_save(t
, filename
)) {
4543 show_oops(t
, "Can't save session: %s",
4544 filename
? filename
: "INVALID");
4547 } else if (args
->i
& XT_OPEN
) {
4548 if (session_open(t
, filename
)) {
4549 show_oops(t
, "Can't open session: %s",
4550 filename
? filename
: "INVALID");
4553 } else if (args
->i
& XT_DELETE
) {
4554 if (session_delete(t
, filename
)) {
4555 show_oops(t
, "Can't delete session: %s",
4556 filename
? filename
: "INVALID");
4561 return (XT_CB_PASSTHROUGH
);
4565 * Make a hardcopy of the page
4568 print_page(struct tab
*t
, struct karg
*args
)
4570 WebKitWebFrame
*frame
;
4572 GtkPrintOperation
*op
;
4573 GtkPrintOperationAction action
;
4574 GtkPrintOperationResult print_res
;
4575 GError
*g_err
= NULL
;
4576 int marg_l
, marg_r
, marg_t
, marg_b
;
4578 DNPRINTF(XT_D_PRINTING
, "%s:", __func__
);
4580 ps
= gtk_page_setup_new();
4581 op
= gtk_print_operation_new();
4582 action
= GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG
;
4583 frame
= webkit_web_view_get_main_frame(t
->wv
);
4585 /* the default margins are too small, so we will bump them */
4586 marg_l
= gtk_page_setup_get_left_margin(ps
, GTK_UNIT_MM
) +
4587 XT_PRINT_EXTRA_MARGIN
;
4588 marg_r
= gtk_page_setup_get_right_margin(ps
, GTK_UNIT_MM
) +
4589 XT_PRINT_EXTRA_MARGIN
;
4590 marg_t
= gtk_page_setup_get_top_margin(ps
, GTK_UNIT_MM
) +
4591 XT_PRINT_EXTRA_MARGIN
;
4592 marg_b
= gtk_page_setup_get_bottom_margin(ps
, GTK_UNIT_MM
) +
4593 XT_PRINT_EXTRA_MARGIN
;
4596 gtk_page_setup_set_left_margin(ps
, marg_l
, GTK_UNIT_MM
);
4597 gtk_page_setup_set_right_margin(ps
, marg_r
, GTK_UNIT_MM
);
4598 gtk_page_setup_set_top_margin(ps
, marg_t
, GTK_UNIT_MM
);
4599 gtk_page_setup_set_bottom_margin(ps
, marg_b
, GTK_UNIT_MM
);
4601 gtk_print_operation_set_default_page_setup(op
, ps
);
4603 /* this appears to free 'op' and 'ps' */
4604 print_res
= webkit_web_frame_print_full(frame
, op
, action
, &g_err
);
4606 /* check it worked */
4607 if (print_res
== GTK_PRINT_OPERATION_RESULT_ERROR
) {
4608 show_oops_s("can't print: %s", g_err
->message
);
4609 g_error_free (g_err
);
4617 go_home(struct tab
*t
, struct karg
*args
)
4624 restart(struct tab
*t
, struct karg
*args
)
4628 a
.s
= XT_RESTART_TABS_FILE
;
4630 execvp(start_argv
[0], start_argv
);
4636 #define CTRL GDK_CONTROL_MASK
4637 #define MOD1 GDK_MOD1_MASK
4638 #define SHFT GDK_SHIFT_MASK
4640 /* inherent to GTK not all keys will be caught at all times */
4641 /* XXX sort key bindings */
4642 struct key_binding
{
4647 TAILQ_ENTRY(key_binding
) entry
; /* in bss so no need to init */
4649 { "cookiejar", MOD1
, 0, GDK_j
},
4650 { "downloadmgr", MOD1
, 0, GDK_d
},
4651 { "history", MOD1
, 0, GDK_h
},
4652 { "print", CTRL
, 0, GDK_p
},
4653 { "search", 0, 0, GDK_slash
},
4654 { "searchb", 0, 0, GDK_question
},
4655 { "command", 0, 0, GDK_colon
},
4656 { "qa", CTRL
, 0, GDK_q
},
4657 { "restart", MOD1
, 0, GDK_q
},
4658 { "js toggle", CTRL
, 0, GDK_j
},
4659 { "cookie toggle", MOD1
, 0, GDK_c
},
4660 { "togglesrc", CTRL
, 0, GDK_s
},
4661 { "yankuri", 0, 0, GDK_y
},
4662 { "pasteuricur", 0, 0, GDK_p
},
4663 { "pasteurinew", 0, 0, GDK_P
},
4666 { "searchnext", 0, 0, GDK_n
},
4667 { "searchprevious", 0, 0, GDK_N
},
4670 { "focusaddress", 0, 0, GDK_F6
},
4671 { "focussearch", 0, 0, GDK_F7
},
4674 { "hinting", 0, 0, GDK_f
},
4676 /* custom stylesheet */
4677 { "userstyle", 0, 0, GDK_i
},
4680 { "goback", 0, 0, GDK_BackSpace
},
4681 { "goback", MOD1
, 0, GDK_Left
},
4682 { "goforward", SHFT
, 0, GDK_BackSpace
},
4683 { "goforward", MOD1
, 0, GDK_Right
},
4684 { "reload", 0, 0, GDK_F5
},
4685 { "reload", CTRL
, 0, GDK_r
},
4686 { "reloadforce", CTRL
, 0, GDK_R
},
4687 { "reload", CTRL
, 0, GDK_l
},
4688 { "favorites", MOD1
, 1, GDK_f
},
4690 /* vertical movement */
4691 { "scrolldown", 0, 0, GDK_j
},
4692 { "scrolldown", 0, 0, GDK_Down
},
4693 { "scrollup", 0, 0, GDK_Up
},
4694 { "scrollup", 0, 0, GDK_k
},
4695 { "scrollbottom", 0, 0, GDK_G
},
4696 { "scrollbottom", 0, 0, GDK_End
},
4697 { "scrolltop", 0, 0, GDK_Home
},
4698 { "scrolltop", 0, 0, GDK_g
},
4699 { "scrollpagedown", 0, 0, GDK_space
},
4700 { "scrollpagedown", CTRL
, 0, GDK_f
},
4701 { "scrollhalfdown", CTRL
, 0, GDK_d
},
4702 { "scrollpagedown", 0, 0, GDK_Page_Down
},
4703 { "scrollpageup", 0, 0, GDK_Page_Up
},
4704 { "scrollpageup", CTRL
, 0, GDK_b
},
4705 { "scrollhalfup", CTRL
, 0, GDK_u
},
4706 /* horizontal movement */
4707 { "scrollright", 0, 0, GDK_l
},
4708 { "scrollright", 0, 0, GDK_Right
},
4709 { "scrollleft", 0, 0, GDK_Left
},
4710 { "scrollleft", 0, 0, GDK_h
},
4711 { "scrollfarright", 0, 0, GDK_dollar
},
4712 { "scrollfarleft", 0, 0, GDK_0
},
4715 { "tabnew", CTRL
, 0, GDK_t
},
4716 { "tabclose", CTRL
, 1, GDK_w
},
4717 { "tabundoclose", 0, 0, GDK_U
},
4718 { "tabnext 1", CTRL
, 0, GDK_1
},
4719 { "tabnext 2", CTRL
, 0, GDK_2
},
4720 { "tabnext 3", CTRL
, 0, GDK_3
},
4721 { "tabnext 4", CTRL
, 0, GDK_4
},
4722 { "tabnext 5", CTRL
, 0, GDK_5
},
4723 { "tabnext 6", CTRL
, 0, GDK_6
},
4724 { "tabnext 7", CTRL
, 0, GDK_7
},
4725 { "tabnext 8", CTRL
, 0, GDK_8
},
4726 { "tabnext 9", CTRL
, 0, GDK_9
},
4727 { "tabnext 10", CTRL
, 0, GDK_0
},
4728 { "tabfirst", CTRL
, 0, GDK_less
},
4729 { "tablast", CTRL
, 0, GDK_greater
},
4730 { "tabprevious", CTRL
, 0, GDK_Left
},
4731 { "tabnext", CTRL
, 0, GDK_Right
},
4732 { "focusout", CTRL
, 0, GDK_minus
},
4733 { "focusin", CTRL
, 0, GDK_plus
},
4734 { "focusin", CTRL
, 0, GDK_equal
},
4736 /* command aliases (handy when -S flag is used) */
4737 { "promptopen", 0, 0, GDK_F9
},
4738 { "promptopencurrent", 0, 0, GDK_F10
},
4739 { "prompttabnew", 0, 0, GDK_F11
},
4740 { "prompttabnewcurrent",0, 0, GDK_F12
},
4742 TAILQ_HEAD(keybinding_list
, key_binding
);
4745 walk_kb(struct settings
*s
,
4746 void (*cb
)(struct settings
*, char *, void *), void *cb_args
)
4748 struct key_binding
*k
;
4751 if (s
== NULL
|| cb
== NULL
) {
4752 show_oops_s("walk_kb invalid parameters");
4756 TAILQ_FOREACH(k
, &kbl
, entry
) {
4762 if (gdk_keyval_name(k
->key
) == NULL
)
4765 strlcat(str
, k
->cmd
, sizeof str
);
4766 strlcat(str
, ",", sizeof str
);
4768 if (k
->mask
& GDK_SHIFT_MASK
)
4769 strlcat(str
, "S-", sizeof str
);
4770 if (k
->mask
& GDK_CONTROL_MASK
)
4771 strlcat(str
, "C-", sizeof str
);
4772 if (k
->mask
& GDK_MOD1_MASK
)
4773 strlcat(str
, "M1-", sizeof str
);
4774 if (k
->mask
& GDK_MOD2_MASK
)
4775 strlcat(str
, "M2-", sizeof str
);
4776 if (k
->mask
& GDK_MOD3_MASK
)
4777 strlcat(str
, "M3-", sizeof str
);
4778 if (k
->mask
& GDK_MOD4_MASK
)
4779 strlcat(str
, "M4-", sizeof str
);
4780 if (k
->mask
& GDK_MOD5_MASK
)
4781 strlcat(str
, "M5-", sizeof str
);
4783 strlcat(str
, gdk_keyval_name(k
->key
), sizeof str
);
4784 cb(s
, str
, cb_args
);
4789 init_keybindings(void)
4792 struct key_binding
*k
;
4794 for (i
= 0; i
< LENGTH(keys
); i
++) {
4795 k
= g_malloc0(sizeof *k
);
4796 k
->cmd
= keys
[i
].cmd
;
4797 k
->mask
= keys
[i
].mask
;
4798 k
->use_in_entry
= keys
[i
].use_in_entry
;
4799 k
->key
= keys
[i
].key
;
4800 TAILQ_INSERT_HEAD(&kbl
, k
, entry
);
4802 DNPRINTF(XT_D_KEYBINDING
, "init_keybindings: added: %s\n",
4803 k
->cmd
? k
->cmd
: "unnamed key");
4808 keybinding_clearall(void)
4810 struct key_binding
*k
, *next
;
4812 for (k
= TAILQ_FIRST(&kbl
); k
; k
= next
) {
4813 next
= TAILQ_NEXT(k
, entry
);
4817 DNPRINTF(XT_D_KEYBINDING
, "keybinding_clearall: %s\n",
4818 k
->cmd
? k
->cmd
: "unnamed key");
4819 TAILQ_REMOVE(&kbl
, k
, entry
);
4825 keybinding_add(char *cmd
, char *key
, int use_in_entry
)
4827 struct key_binding
*k
;
4828 guint keyval
, mask
= 0;
4831 DNPRINTF(XT_D_KEYBINDING
, "keybinding_add: %s %s\n", cmd
, key
);
4833 /* Keys which are to be used in entry have been prefixed with an
4834 * exclamation mark. */
4838 /* find modifier keys */
4839 if (strstr(key
, "S-"))
4840 mask
|= GDK_SHIFT_MASK
;
4841 if (strstr(key
, "C-"))
4842 mask
|= GDK_CONTROL_MASK
;
4843 if (strstr(key
, "M1-"))
4844 mask
|= GDK_MOD1_MASK
;
4845 if (strstr(key
, "M2-"))
4846 mask
|= GDK_MOD2_MASK
;
4847 if (strstr(key
, "M3-"))
4848 mask
|= GDK_MOD3_MASK
;
4849 if (strstr(key
, "M4-"))
4850 mask
|= GDK_MOD4_MASK
;
4851 if (strstr(key
, "M5-"))
4852 mask
|= GDK_MOD5_MASK
;
4855 for (i
= strlen(key
) - 1; i
> 0; i
--)
4859 /* validate keyname */
4860 keyval
= gdk_keyval_from_name(key
);
4861 if (keyval
== GDK_VoidSymbol
) {
4862 warnx("invalid keybinding name %s", key
);
4865 /* must run this test too, gtk+ doesn't handle 10 for example */
4866 if (gdk_keyval_name(keyval
) == NULL
) {
4867 warnx("invalid keybinding name %s", key
);
4871 /* Remove eventual dupes. */
4872 TAILQ_FOREACH(k
, &kbl
, entry
)
4873 if (k
->key
== keyval
&& k
->mask
== mask
) {
4874 TAILQ_REMOVE(&kbl
, k
, entry
);
4880 k
= g_malloc0(sizeof *k
);
4881 k
->cmd
= g_strdup(cmd
);
4883 k
->use_in_entry
= use_in_entry
;
4886 DNPRINTF(XT_D_KEYBINDING
, "keybinding_add: %s 0x%x %d 0x%x\n",
4891 DNPRINTF(XT_D_KEYBINDING
, "keybinding_add: adding: %s %s\n",
4892 k
->cmd
, gdk_keyval_name(keyval
));
4894 TAILQ_INSERT_HEAD(&kbl
, k
, entry
);
4900 add_kb(struct settings
*s
, char *entry
)
4904 DNPRINTF(XT_D_KEYBINDING
, "add_kb: %s\n", entry
);
4906 /* clearall is special */
4907 if (!strcmp(entry
, "clearall")) {
4908 keybinding_clearall();
4912 kb
= strstr(entry
, ",");
4918 return (keybinding_add(entry
, key
, key
[0] == '!'));
4924 int (*func
)(struct tab
*, struct karg
*);
4926 bool userarg
; /* allow free text arg */
4928 { "command", 0, command
, {.i
= ':'}, FALSE
},
4929 { "search", 0, command
, {.i
= '/'}, FALSE
},
4930 { "searchb", 0, command
, {.i
= '?'}, FALSE
},
4931 { "togglesrc", 0, toggle_src
, {0}, FALSE
},
4933 /* yanking and pasting */
4934 { "yankuri", 0, yank_uri
, {0}, FALSE
},
4935 /* XXX: pasteuri{cur,new} do not work from the cmd_entry? */
4936 { "pasteuricur", 0, paste_uri
, {.i
= XT_PASTE_CURRENT_TAB
}, FALSE
},
4937 { "pasteurinew", 0, paste_uri
, {.i
= XT_PASTE_NEW_TAB
}, FALSE
},
4940 { "searchnext", 0, search
, {.i
= XT_SEARCH_NEXT
}, FALSE
},
4941 { "searchprevious", 0, search
, {.i
= XT_SEARCH_PREV
}, FALSE
},
4944 { "focusaddress", 0, focus
, {.i
= XT_FOCUS_URI
}, FALSE
},
4945 { "focussearch", 0, focus
, {.i
= XT_FOCUS_SEARCH
}, FALSE
},
4948 { "hinting", 0, hint
, {.i
= 0}, FALSE
},
4950 /* custom stylesheet */
4951 { "userstyle", 0, userstyle
, {.i
= 0 }, FALSE
},
4954 { "goback", 0, navaction
, {.i
= XT_NAV_BACK
}, FALSE
},
4955 { "goforward", 0, navaction
, {.i
= XT_NAV_FORWARD
}, FALSE
},
4956 { "reload", 0, navaction
, {.i
= XT_NAV_RELOAD
}, FALSE
},
4957 { "reloadforce", 0, navaction
, {.i
= XT_NAV_RELOAD_CACHE
}, FALSE
},
4959 /* vertical movement */
4960 { "scrolldown", 0, move
, {.i
= XT_MOVE_DOWN
}, FALSE
},
4961 { "scrollup", 0, move
, {.i
= XT_MOVE_UP
}, FALSE
},
4962 { "scrollbottom", 0, move
, {.i
= XT_MOVE_BOTTOM
}, FALSE
},
4963 { "scrolltop", 0, move
, {.i
= XT_MOVE_TOP
}, FALSE
},
4964 { "1", 0, move
, {.i
= XT_MOVE_TOP
}, FALSE
},
4965 { "scrollhalfdown", 0, move
, {.i
= XT_MOVE_HALFDOWN
},FALSE
},
4966 { "scrollhalfup", 0, move
, {.i
= XT_MOVE_HALFUP
}, FALSE
},
4967 { "scrollpagedown", 0, move
, {.i
= XT_MOVE_PAGEDOWN
},FALSE
},
4968 { "scrollpageup", 0, move
, {.i
= XT_MOVE_PAGEUP
}, FALSE
},
4969 /* horizontal movement */
4970 { "scrollright", 0, move
, {.i
= XT_MOVE_RIGHT
}, FALSE
},
4971 { "scrollleft", 0, move
, {.i
= XT_MOVE_LEFT
}, FALSE
},
4972 { "scrollfarright", 0, move
, {.i
= XT_MOVE_FARRIGHT
},FALSE
},
4973 { "scrollfarleft", 0, move
, {.i
= XT_MOVE_FARLEFT
}, FALSE
},
4976 { "favorites", 0, xtp_page_fl
, {0}, FALSE
},
4977 { "fav", 0, xtp_page_fl
, {0}, FALSE
},
4978 { "favadd", 0, add_favorite
, {0}, FALSE
},
4980 { "qall", 0, quit
, {0}, FALSE
},
4981 { "quitall", 0, quit
, {0}, FALSE
},
4982 { "w", 0, save_tabs
, {0}, FALSE
},
4983 { "wq", 0, save_tabs_and_quit
, {0}, FALSE
},
4984 { "help", 0, help
, {0}, FALSE
},
4985 { "about", 0, about
, {0}, FALSE
},
4986 { "stats", 0, stats
, {0}, FALSE
},
4987 { "version", 0, about
, {0}, FALSE
},
4990 { "js", 0, js_cmd
, {.i
= XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
}, FALSE
},
4991 { "save", 1, js_cmd
, {.i
= XT_SAVE
| XT_WL_FQDN
}, FALSE
},
4992 { "domain", 2, js_cmd
, {.i
= XT_SAVE
| XT_WL_TOPLEVEL
}, FALSE
},
4993 { "fqdn", 2, js_cmd
, {.i
= XT_SAVE
| XT_WL_FQDN
}, FALSE
},
4994 { "show", 1, js_cmd
, {.i
= XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
}, FALSE
},
4995 { "all", 2, js_cmd
, {.i
= XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
}, FALSE
},
4996 { "persistent", 2, js_cmd
, {.i
= XT_SHOW
| XT_WL_PERSISTENT
}, FALSE
},
4997 { "session", 2, js_cmd
, {.i
= XT_SHOW
| XT_WL_SESSION
}, FALSE
},
4998 { "toggle", 1, js_cmd
, {.i
= XT_WL_TOGGLE
| XT_WL_FQDN
}, FALSE
},
4999 { "domain", 2, js_cmd
, {.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
}, FALSE
},
5000 { "fqdn", 2, js_cmd
, {.i
= XT_WL_TOGGLE
| XT_WL_FQDN
}, FALSE
},
5002 /* cookie command */
5003 { "cookie", 0, cookie_cmd
, {0}, FALSE
},
5004 { "save", 1, cookie_cmd
, {.i
= XT_SAVE
| XT_WL_FQDN
}, FALSE
},
5005 { "domain", 2, cookie_cmd
, {.i
= XT_SAVE
| XT_WL_TOPLEVEL
}, FALSE
},
5006 { "fqdn", 2, cookie_cmd
, {.i
= XT_SAVE
| XT_WL_FQDN
}, FALSE
},
5007 { "show", 1, cookie_cmd
, {.i
= XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
}, FALSE
},
5008 { "all", 2, cookie_cmd
, {.i
= XT_SHOW
| XT_WL_PERSISTENT
| XT_WL_SESSION
}, FALSE
},
5009 { "persistent", 2, cookie_cmd
, {.i
= XT_SHOW
| XT_WL_PERSISTENT
}, FALSE
},
5010 { "session", 2, cookie_cmd
, {.i
= XT_SHOW
| XT_WL_SESSION
}, FALSE
},
5011 { "toggle", 1, cookie_cmd
, {.i
= XT_WL_TOGGLE
| XT_WL_FQDN
}, FALSE
},
5012 { "domain", 2, cookie_cmd
, {.i
= XT_WL_TOGGLE
| XT_WL_TOPLEVEL
}, FALSE
},
5013 { "fqdn", 2, cookie_cmd
, {.i
= XT_WL_TOGGLE
| XT_WL_FQDN
}, FALSE
},
5016 { "cookiejar", 0, xtp_page_cl
, {0}, FALSE
},
5019 { "cert", 0, cert_cmd
, {.i
= XT_SHOW
}, FALSE
},
5020 { "save", 1, cert_cmd
, {.i
= XT_SAVE
}, FALSE
},
5021 { "show", 1, cert_cmd
, {.i
= XT_SHOW
}, FALSE
},
5023 { "ca", 0, ca_cmd
, {0}, FALSE
},
5024 { "downloadmgr", 0, xtp_page_dl
, {0}, FALSE
},
5025 { "dl", 0, xtp_page_dl
, {0}, FALSE
},
5026 { "h", 0, xtp_page_hl
, {0}, FALSE
},
5027 { "history", 0, xtp_page_hl
, {0}, FALSE
},
5028 { "home", 0, go_home
, {0}, FALSE
},
5029 { "restart", 0, restart
, {0}, FALSE
},
5030 { "urlhide", 0, urlaction
, {.i
= XT_URL_HIDE
}, FALSE
},
5031 { "urlshow", 0, urlaction
, {.i
= XT_URL_SHOW
}, FALSE
},
5032 { "statushide", 0, statusaction
, {.i
= XT_STATUSBAR_HIDE
}, FALSE
},
5033 { "statusshow", 0, statusaction
, {.i
= XT_STATUSBAR_SHOW
}, FALSE
},
5035 { "print", 0, print_page
, {0}, FALSE
},
5038 { "open", 0, tabaction
, {.i
= XT_TAB_OPEN
}, TRUE
},
5039 { "tabnew", 0, tabaction
, {.i
= XT_TAB_NEW
}, TRUE
},
5040 { "tabedit", 0, tabaction
, {.i
= XT_TAB_NEW
}, TRUE
},
5041 { "tabclose", 0, tabaction
, {.i
= XT_TAB_DELETE
}, FALSE
},
5042 { "tabundoclose", 0, tabaction
, {.i
= XT_TAB_UNDO_CLOSE
} },
5043 { "tabshow", 0, tabaction
, {.i
= XT_TAB_SHOW
}, FALSE
},
5044 { "tabhide", 0, tabaction
, {.i
= XT_TAB_HIDE
}, FALSE
},
5045 { "quit", 0, tabaction
, {.i
= XT_TAB_DELQUIT
}, FALSE
},
5046 { "q", 0, tabaction
, {.i
= XT_TAB_DELQUIT
}, FALSE
},
5048 /* XXX add count to these commands */
5049 { "tabfirst", 0, movetab
, {.i
= XT_TAB_FIRST
}, FALSE
},
5050 { "tabrewind", 0, movetab
, {.i
= XT_TAB_FIRST
}, FALSE
},
5051 { "tablast", 0, movetab
, {.i
= XT_TAB_LAST
}, FALSE
},
5052 { "tabprevious", 0, movetab
, {.i
= XT_TAB_PREV
}, FALSE
},
5053 { "tabnext", 0, movetab
, {.i
= XT_TAB_NEXT
}, TRUE
},
5054 { "focusout", 0, resizetab
, {.i
= -1}, FALSE
},
5055 { "focusin", 0, resizetab
, {.i
= 1}, FALSE
},
5057 /* command aliases (handy when -S flag is used) */
5058 { "promptopen", 0, command
, {.i
= XT_CMD_OPEN
}, FALSE
},
5059 { "promptopencurrent", 0, command
, {.i
= XT_CMD_OPEN_CURRENT
}, FALSE
},
5060 { "prompttabnew", 0, command
, {.i
= XT_CMD_TABNEW
}, FALSE
},
5061 { "prompttabnewcurrent",0, command
, {.i
= XT_CMD_TABNEW_CURRENT
}, FALSE
},
5064 { "set", 0, set
, {0}, FALSE
},
5065 { "fullscreen", 0, fullscreen
, {0}, FALSE
},
5066 { "f", 0, fullscreen
, {0}, FALSE
},
5069 { "session", 0, session_cmd
, {.i
= XT_SHOW
}, FALSE
},
5070 { "delete", 1, session_cmd
, {.i
= XT_DELETE
}, TRUE
},
5071 { "open", 1, session_cmd
, {.i
= XT_OPEN
}, TRUE
},
5072 { "save", 1, session_cmd
, {.i
= XT_SAVE
}, TRUE
},
5073 { "show", 1, session_cmd
, {.i
= XT_SHOW
}, FALSE
},
5080 } cmd_status
= {-1, 0};
5083 wv_button_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
5089 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 8 /* btn 4 */) {
5095 } else if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 9 /* btn 5 */) {
5097 a
.i
= XT_NAV_FORWARD
;
5107 tab_close_cb(GtkWidget
*btn
, GdkEventButton
*e
, struct tab
*t
)
5109 DNPRINTF(XT_D_TAB
, "tab_close_cb: tab %d\n", t
->tab_id
);
5111 if (e
->type
== GDK_BUTTON_PRESS
&& e
->button
== 1)
5118 * cancel, remove, etc. downloads
5121 xtp_handle_dl(struct tab
*t
, uint8_t cmd
, int id
)
5123 struct download find
, *d
= NULL
;
5125 DNPRINTF(XT_D_DOWNLOAD
, "download control: cmd %d, id %d\n", cmd
, id
);
5127 /* some commands require a valid download id */
5128 if (cmd
!= XT_XTP_DL_LIST
) {
5129 /* lookup download in question */
5131 d
= RB_FIND(download_list
, &downloads
, &find
);
5134 show_oops(t
, "%s: no such download", __func__
);
5139 /* decide what to do */
5141 case XT_XTP_DL_CANCEL
:
5142 webkit_download_cancel(d
->download
);
5144 case XT_XTP_DL_REMOVE
:
5145 webkit_download_cancel(d
->download
); /* just incase */
5146 g_object_unref(d
->download
);
5147 RB_REMOVE(download_list
, &downloads
, d
);
5149 case XT_XTP_DL_LIST
:
5153 show_oops(t
, "%s: unknown command", __func__
);
5156 xtp_page_dl(t
, NULL
);
5160 * Actions on history, only does one thing for now, but
5161 * we provide the function for future actions
5164 xtp_handle_hl(struct tab
*t
, uint8_t cmd
, int id
)
5166 struct history
*h
, *next
;
5170 case XT_XTP_HL_REMOVE
:
5171 /* walk backwards, as listed in reverse */
5172 for (h
= RB_MAX(history_list
, &hl
); h
!= NULL
; h
= next
) {
5173 next
= RB_PREV(history_list
, &hl
, h
);
5175 RB_REMOVE(history_list
, &hl
, h
);
5176 g_free((gpointer
) h
->title
);
5177 g_free((gpointer
) h
->uri
);
5184 case XT_XTP_HL_LIST
:
5185 /* Nothing - just xtp_page_hl() below */
5188 show_oops(t
, "%s: unknown command", __func__
);
5192 xtp_page_hl(t
, NULL
);
5195 /* remove a favorite */
5197 remove_favorite(struct tab
*t
, int index
)
5199 char file
[PATH_MAX
], *title
, *uri
= NULL
;
5200 char *new_favs
, *tmp
;
5205 /* open favorites */
5206 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_FAVS_FILE
);
5208 if ((f
= fopen(file
, "r")) == NULL
) {
5209 show_oops(t
, "%s: can't open favorites: %s",
5210 __func__
, strerror(errno
));
5214 /* build a string which will become the new favroites file */
5215 new_favs
= g_strdup_printf("%s", "");
5218 if ((title
= fparseln(f
, &len
, &lineno
, NULL
, 0)) == NULL
)
5219 if (feof(f
) || ferror(f
))
5221 /* XXX THIS IS NOT THE RIGHT HEURISTIC */
5228 if ((uri
= fparseln(f
, &len
, &lineno
, NULL
, 0)) == NULL
) {
5229 if (feof(f
) || ferror(f
)) {
5230 show_oops(t
, "%s: can't parse favorites %s",
5231 __func__
, strerror(errno
));
5236 /* as long as this isn't the one we are deleting add to file */
5239 new_favs
= g_strdup_printf("%s%s\n%s\n",
5240 new_favs
, title
, uri
);
5252 /* write back new favorites file */
5253 if ((f
= fopen(file
, "w")) == NULL
) {
5254 show_oops(t
, "%s: can't open favorites: %s",
5255 __func__
, strerror(errno
));
5259 fwrite(new_favs
, strlen(new_favs
), 1, f
);
5272 xtp_handle_fl(struct tab
*t
, uint8_t cmd
, int arg
)
5275 case XT_XTP_FL_LIST
:
5276 /* nothing, just the below call to xtp_page_fl() */
5278 case XT_XTP_FL_REMOVE
:
5279 remove_favorite(t
, arg
);
5282 show_oops(t
, "%s: invalid favorites command", __func__
);
5286 xtp_page_fl(t
, NULL
);
5290 xtp_handle_cl(struct tab
*t
, uint8_t cmd
, int arg
)
5293 case XT_XTP_CL_LIST
:
5294 /* nothing, just xtp_page_cl() */
5296 case XT_XTP_CL_REMOVE
:
5300 show_oops(t
, "%s: unknown cookie xtp command", __func__
);
5304 xtp_page_cl(t
, NULL
);
5307 /* link an XTP class to it's session key and handler function */
5308 struct xtp_despatch
{
5311 void (*handle_func
)(struct tab
*, uint8_t, int);
5314 struct xtp_despatch xtp_despatches
[] = {
5315 { XT_XTP_DL
, &dl_session_key
, xtp_handle_dl
},
5316 { XT_XTP_HL
, &hl_session_key
, xtp_handle_hl
},
5317 { XT_XTP_FL
, &fl_session_key
, xtp_handle_fl
},
5318 { XT_XTP_CL
, &cl_session_key
, xtp_handle_cl
},
5319 { XT_XTP_INVALID
, NULL
, NULL
}
5323 * is the url xtp protocol? (xxxt://)
5324 * if so, parse and despatch correct bahvior
5327 parse_xtp_url(struct tab
*t
, const char *url
)
5329 char *dup
= NULL
, *p
, *last
;
5330 uint8_t n_tokens
= 0;
5331 char *tokens
[4] = {NULL
, NULL
, NULL
, ""};
5332 struct xtp_despatch
*dsp
, *dsp_match
= NULL
;
5337 * tokens array meaning:
5339 * tokens[1] = session key
5340 * tokens[2] = action
5341 * tokens[3] = optional argument
5344 DNPRINTF(XT_D_URL
, "%s: url %s\n", __func__
, url
);
5346 /*xtp tab meaning is normal unless proven special */
5347 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
5349 if (strncmp(url
, XT_XTP_STR
, strlen(XT_XTP_STR
)))
5352 dup
= g_strdup(url
+ strlen(XT_XTP_STR
));
5354 /* split out the url */
5355 for ((p
= strtok_r(dup
, "/", &last
)); p
;
5356 (p
= strtok_r(NULL
, "/", &last
))) {
5358 tokens
[n_tokens
++] = p
;
5361 /* should be atleast three fields 'class/seskey/command/arg' */
5365 dsp
= xtp_despatches
;
5366 req_class
= atoi(tokens
[0]);
5367 while (dsp
->xtp_class
) {
5368 if (dsp
->xtp_class
== req_class
) {
5375 /* did we find one atall? */
5376 if (dsp_match
== NULL
) {
5377 show_oops(t
, "%s: no matching xtp despatch found", __func__
);
5381 /* check session key and call despatch function */
5382 if (validate_xtp_session_key(t
, *(dsp_match
->session_key
), tokens
[1])) {
5383 ret
= TRUE
; /* all is well, this was a valid xtp request */
5384 dsp_match
->handle_func(t
, atoi(tokens
[2]), atoi(tokens
[3]));
5397 activate_uri_entry_cb(GtkWidget
* entry
, struct tab
*t
)
5399 const gchar
*uri
= gtk_entry_get_text(GTK_ENTRY(entry
));
5401 DNPRINTF(XT_D_URL
, "activate_uri_entry_cb: %s\n", uri
);
5404 show_oops_s("activate_uri_entry_cb invalid parameters");
5409 show_oops(t
, "activate_uri_entry_cb no uri");
5413 uri
+= strspn(uri
, "\t ");
5415 /* if xxxt:// treat specially */
5416 if (parse_xtp_url(t
, uri
))
5419 /* otherwise continue to load page normally */
5420 load_uri(t
, (gchar
*)uri
);
5425 activate_search_entry_cb(GtkWidget
* entry
, struct tab
*t
)
5427 const gchar
*search
= gtk_entry_get_text(GTK_ENTRY(entry
));
5428 char *newuri
= NULL
;
5431 DNPRINTF(XT_D_URL
, "activate_search_entry_cb: %s\n", search
);
5434 show_oops_s("activate_search_entry_cb invalid parameters");
5438 if (search_string
== NULL
) {
5439 show_oops(t
, "no search_string");
5443 enc_search
= soup_uri_encode(search
, XT_RESERVED_CHARS
);
5444 newuri
= g_strdup_printf(search_string
, enc_search
);
5447 webkit_web_view_load_uri(t
->wv
, newuri
);
5455 check_and_set_js(const gchar
*uri
, struct tab
*t
)
5457 struct domain
*d
= NULL
;
5460 if (uri
== NULL
|| t
== NULL
)
5463 if ((d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
5468 DNPRINTF(XT_D_JS
, "check_and_set_js: %s %s\n",
5469 es
? "enable" : "disable", uri
);
5471 g_object_set(G_OBJECT(t
->settings
),
5472 "enable-scripts", es
, (char *)NULL
);
5473 g_object_set(G_OBJECT(t
->settings
),
5474 "javascript-can-open-windows-automatically", es
, (char *)NULL
);
5475 webkit_web_view_set_settings(t
->wv
, t
->settings
);
5477 button_set_stockid(t
->js_toggle
,
5478 es
? GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
);
5482 show_ca_status(struct tab
*t
, const char *uri
)
5484 WebKitWebFrame
*frame
;
5485 WebKitWebDataSource
*source
;
5486 WebKitNetworkRequest
*request
;
5487 SoupMessage
*message
;
5489 gchar
*col_str
= XT_COLOR_WHITE
;
5492 DNPRINTF(XT_D_URL
, "show_ca_status: %d %s %s\n",
5493 ssl_strict_certs
, ssl_ca_file
, uri
);
5497 if (ssl_ca_file
== NULL
) {
5498 if (g_str_has_prefix(uri
, "http://"))
5500 if (g_str_has_prefix(uri
, "https://")) {
5501 col_str
= XT_COLOR_RED
;
5506 if (g_str_has_prefix(uri
, "http://") ||
5507 !g_str_has_prefix(uri
, "https://"))
5510 frame
= webkit_web_view_get_main_frame(t
->wv
);
5511 source
= webkit_web_frame_get_data_source(frame
);
5512 request
= webkit_web_data_source_get_request(source
);
5513 message
= webkit_network_request_get_message(request
);
5515 if (message
&& (soup_message_get_flags(message
) &
5516 SOUP_MESSAGE_CERTIFICATE_TRUSTED
)) {
5517 col_str
= XT_COLOR_GREEN
;
5520 r
= load_compare_cert(t
, NULL
);
5522 col_str
= XT_COLOR_BLUE
;
5524 col_str
= XT_COLOR_YELLOW
;
5526 col_str
= XT_COLOR_RED
;
5531 gdk_color_parse(col_str
, &color
);
5532 gtk_widget_modify_base(t
->uri_entry
, GTK_STATE_NORMAL
, &color
);
5534 if (!strcmp(col_str
, XT_COLOR_WHITE
)) {
5535 gtk_widget_modify_text(t
->statusbar
, GTK_STATE_NORMAL
,
5537 gdk_color_parse(XT_COLOR_BLACK
, &color
);
5538 gtk_widget_modify_base(t
->statusbar
, GTK_STATE_NORMAL
,
5541 gtk_widget_modify_base(t
->statusbar
, GTK_STATE_NORMAL
,
5543 gdk_color_parse(XT_COLOR_BLACK
, &color
);
5544 gtk_widget_modify_text(t
->statusbar
, GTK_STATE_NORMAL
,
5551 free_favicon(struct tab
*t
)
5553 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p req %p pix %p\n",
5554 __func__
, t
->icon_download
, t
->icon_request
, t
->icon_pixbuf
);
5556 if (t
->icon_request
)
5557 g_object_unref(t
->icon_request
);
5559 g_object_unref(t
->icon_pixbuf
);
5560 if (t
->icon_dest_uri
)
5561 g_free(t
->icon_dest_uri
);
5563 t
->icon_pixbuf
= NULL
;
5564 t
->icon_request
= NULL
;
5565 t
->icon_dest_uri
= NULL
;
5569 xt_icon_from_name(struct tab
*t
, gchar
*name
)
5571 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->uri_entry
),
5572 GTK_ENTRY_ICON_PRIMARY
, "text-html");
5574 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->statusbar
),
5575 GTK_ENTRY_ICON_PRIMARY
, "text-html");
5577 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->statusbar
),
5578 GTK_ENTRY_ICON_PRIMARY
, NULL
);
5582 xt_icon_from_pixbuf(struct tab
*t
, GdkPixbuf
*pixbuf
)
5584 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->uri_entry
),
5585 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
5587 gtk_entry_set_icon_from_pixbuf(GTK_ENTRY(t
->statusbar
),
5588 GTK_ENTRY_ICON_PRIMARY
, pixbuf
);
5590 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(t
->statusbar
),
5591 GTK_ENTRY_ICON_PRIMARY
, NULL
);
5595 is_valid_icon(char *file
)
5598 const char *mime_type
;
5602 gf
= g_file_new_for_path(file
);
5603 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
5605 mime_type
= g_file_info_get_content_type(fi
);
5606 valid
= g_strcmp0(mime_type
, "image/x-ico") == 0 ||
5607 g_strcmp0(mime_type
, "image/vnd.microsoft.icon") == 0 ||
5608 g_strcmp0(mime_type
, "image/png") == 0 ||
5609 g_strcmp0(mime_type
, "image/gif") == 0 ||
5610 g_strcmp0(mime_type
, "application/octet-stream") == 0;
5618 set_favicon_from_file(struct tab
*t
, char *file
)
5621 GdkPixbuf
*pixbuf
, *scaled
;
5624 if (t
== NULL
|| file
== NULL
)
5626 if (t
->icon_pixbuf
) {
5627 DNPRINTF(XT_D_DOWNLOAD
, "%s: icon already set\n", __func__
);
5631 if (g_str_has_prefix(file
, "file://"))
5632 file
+= strlen("file://");
5633 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading %s\n", __func__
, file
);
5635 if (!stat(file
, &sb
)) {
5636 if (sb
.st_size
== 0 || !is_valid_icon(file
)) {
5637 /* corrupt icon so trash it */
5638 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
5641 /* no need to set icon to default here */
5646 pixbuf
= gdk_pixbuf_new_from_file(file
, NULL
);
5647 if (pixbuf
== NULL
) {
5648 xt_icon_from_name(t
, "text-html");
5652 g_object_get(pixbuf
, "width", &width
, "height", &height
,
5654 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d icon size %dx%d\n",
5655 __func__
, t
->tab_id
, width
, height
);
5657 if (width
> 16 || height
> 16) {
5658 scaled
= gdk_pixbuf_scale_simple(pixbuf
, 16, 16,
5659 GDK_INTERP_BILINEAR
);
5660 g_object_unref(pixbuf
);
5664 if (scaled
== NULL
) {
5665 scaled
= gdk_pixbuf_scale_simple(pixbuf
, 16, 16,
5666 GDK_INTERP_BILINEAR
);
5670 t
->icon_pixbuf
= scaled
;
5671 xt_icon_from_pixbuf(t
, t
->icon_pixbuf
);
5675 favicon_download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
5678 WebKitDownloadStatus status
= webkit_download_get_status(download
);
5679 struct tab
*tt
= NULL
, *t
= NULL
;
5682 * find the webview instead of passing in the tab as it could have been
5683 * deleted from underneath us.
5685 TAILQ_FOREACH(tt
, &tabs
, entry
) {
5694 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d status %d\n",
5695 __func__
, t
->tab_id
, status
);
5698 case WEBKIT_DOWNLOAD_STATUS_ERROR
:
5700 t
->icon_download
= NULL
;
5703 case WEBKIT_DOWNLOAD_STATUS_CREATED
:
5706 case WEBKIT_DOWNLOAD_STATUS_STARTED
:
5709 case WEBKIT_DOWNLOAD_STATUS_CANCELLED
:
5711 DNPRINTF(XT_D_DOWNLOAD
, "%s: freeing favicon %d\n",
5712 __func__
, t
->tab_id
);
5713 t
->icon_download
= NULL
;
5716 case WEBKIT_DOWNLOAD_STATUS_FINISHED
:
5719 DNPRINTF(XT_D_DOWNLOAD
, "%s: setting icon to %s\n",
5720 __func__
, t
->icon_dest_uri
);
5721 set_favicon_from_file(t
, t
->icon_dest_uri
);
5722 /* these will be freed post callback */
5723 t
->icon_request
= NULL
;
5724 t
->icon_download
= NULL
;
5732 abort_favicon_download(struct tab
*t
)
5734 DNPRINTF(XT_D_DOWNLOAD
, "%s: down %p\n", __func__
, t
->icon_download
);
5736 if (t
->icon_download
) {
5737 g_signal_handlers_disconnect_by_func(G_OBJECT(t
->icon_download
),
5738 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
5739 webkit_download_cancel(t
->icon_download
);
5740 t
->icon_download
= NULL
;
5744 xt_icon_from_name(t
, "text-html");
5748 notify_icon_loaded_cb(WebKitWebView
*wv
, gchar
*uri
, struct tab
*t
)
5750 gchar
*name_hash
, file
[PATH_MAX
];
5753 DNPRINTF(XT_D_DOWNLOAD
, "notify_icon_loaded_cb %s\n", uri
);
5755 if (uri
== NULL
|| t
== NULL
)
5758 if (t
->icon_request
) {
5759 DNPRINTF(XT_D_DOWNLOAD
, "%s: download in progress\n", __func__
);
5763 /* check to see if we got the icon in cache */
5764 name_hash
= g_compute_checksum_for_string(G_CHECKSUM_SHA256
, uri
, -1);
5765 snprintf(file
, sizeof file
, "%s/%s.ico", cache_dir
, name_hash
);
5768 if (!stat(file
, &sb
)) {
5769 if (sb
.st_size
> 0) {
5770 DNPRINTF(XT_D_DOWNLOAD
, "%s: loading from cache %s\n",
5772 set_favicon_from_file(t
, file
);
5776 /* corrupt icon so trash it */
5777 DNPRINTF(XT_D_DOWNLOAD
, "%s: corrupt icon %s\n",
5782 /* create download for icon */
5783 t
->icon_request
= webkit_network_request_new(uri
);
5784 if (t
->icon_request
== NULL
) {
5785 DNPRINTF(XT_D_DOWNLOAD
, "%s: invalid uri %s\n",
5790 t
->icon_download
= webkit_download_new(t
->icon_request
);
5791 if (t
->icon_download
== NULL
) {
5792 fprintf(stderr
, "%s: icon_download", __func__
);
5796 /* we have to free icon_dest_uri later */
5797 t
->icon_dest_uri
= g_strdup_printf("file://%s", file
);
5798 webkit_download_set_destination_uri(t
->icon_download
,
5801 if (webkit_download_get_status(t
->icon_download
) ==
5802 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
5803 fprintf(stderr
, "%s: download failed to start", __func__
);
5804 g_object_unref(t
->icon_request
);
5805 g_free(t
->icon_dest_uri
);
5806 t
->icon_request
= NULL
;
5807 t
->icon_dest_uri
= NULL
;
5811 g_signal_connect(G_OBJECT(t
->icon_download
), "notify::status",
5812 G_CALLBACK(favicon_download_status_changed_cb
), t
->wv
);
5814 webkit_download_start(t
->icon_download
);
5818 notify_load_status_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
5820 const gchar
*set
= NULL
, *uri
= NULL
, *title
= NULL
;
5821 struct history
*h
, find
;
5822 const gchar
*s_loading
;
5825 DNPRINTF(XT_D_URL
, "notify_load_status_cb: %d %s\n",
5826 webkit_web_view_get_load_status(wview
), get_uri(wview
) ? get_uri(wview
) : "NOTHING");
5829 show_oops_s("notify_load_status_cb invalid paramters");
5833 switch (webkit_web_view_get_load_status(wview
)) {
5834 case WEBKIT_LOAD_PROVISIONAL
:
5836 abort_favicon_download(t
);
5837 #if GTK_CHECK_VERSION(2, 20, 0)
5838 gtk_widget_show(t
->spinner
);
5839 gtk_spinner_start(GTK_SPINNER(t
->spinner
));
5841 gtk_label_set_text(GTK_LABEL(t
->label
), "Loading");
5843 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), TRUE
);
5849 case WEBKIT_LOAD_COMMITTED
:
5851 if ((uri
= get_uri(wview
)) != NULL
) {
5852 if (strncmp(uri
, XT_URI_ABOUT
, XT_URI_ABOUT_LEN
))
5853 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), uri
);
5859 set_status(t
, (char *)uri
, XT_STATUS_LOADING
);
5862 /* check if js white listing is enabled */
5863 if (enable_js_whitelist
) {
5864 uri
= get_uri(wview
);
5865 check_and_set_js(uri
, t
);
5871 show_ca_status(t
, uri
);
5873 /* we know enough to autosave the session */
5874 if (session_autosave
) {
5880 case WEBKIT_LOAD_FIRST_VISUALLY_NON_EMPTY_LAYOUT
:
5884 case WEBKIT_LOAD_FINISHED
:
5886 uri
= get_uri(wview
);
5890 if (!strncmp(uri
, "http://", strlen("http://")) ||
5891 !strncmp(uri
, "https://", strlen("https://")) ||
5892 !strncmp(uri
, "file://", strlen("file://"))) {
5894 h
= RB_FIND(history_list
, &hl
, &find
);
5896 title
= webkit_web_view_get_title(wview
);
5897 set
= title
? title
: uri
;
5898 h
= g_malloc(sizeof *h
);
5899 h
->uri
= g_strdup(uri
);
5900 h
->title
= g_strdup(set
);
5901 RB_INSERT(history_list
, &hl
, h
);
5902 completion_add_uri(h
->uri
);
5903 update_history_tabs(NULL
);
5907 set_status(t
, (char *)uri
, XT_STATUS_URI
);
5908 #if WEBKIT_CHECK_VERSION(1, 1, 18)
5909 case WEBKIT_LOAD_FAILED
:
5912 #if GTK_CHECK_VERSION(2, 20, 0)
5913 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
5914 gtk_widget_hide(t
->spinner
);
5916 s_loading
= gtk_label_get_text(GTK_LABEL(t
->label
));
5917 if (s_loading
&& !strcmp(s_loading
, "Loading"))
5918 gtk_label_set_text(GTK_LABEL(t
->label
), "(untitled)");
5920 gtk_widget_set_sensitive(GTK_WIDGET(t
->stop
), FALSE
);
5925 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
), TRUE
);
5927 gtk_widget_set_sensitive(GTK_WIDGET(t
->backward
),
5928 webkit_web_view_can_go_back(wview
));
5930 gtk_widget_set_sensitive(GTK_WIDGET(t
->forward
),
5931 webkit_web_view_can_go_forward(wview
));
5933 /* take focus if we are visible */
5938 notify_title_cb(WebKitWebView
* wview
, GParamSpec
* pspec
, struct tab
*t
)
5940 const gchar
*set
= NULL
, *title
= NULL
;
5942 title
= webkit_web_view_get_title(wview
);
5943 set
= title
? title
: get_uri(wview
);
5945 gtk_label_set_text(GTK_LABEL(t
->label
), set
);
5946 gtk_window_set_title(GTK_WINDOW(main_window
), set
);
5948 gtk_label_set_text(GTK_LABEL(t
->label
), "(untitled)");
5949 gtk_window_set_title(GTK_WINDOW(main_window
), XT_NAME
);
5954 webview_load_finished_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
5956 run_script(t
, JS_HINTING
);
5960 webview_progress_changed_cb(WebKitWebView
*wv
, int progress
, struct tab
*t
)
5962 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->uri_entry
),
5963 progress
== 100 ? 0 : (double)progress
/ 100);
5964 if (show_url
== 0) {
5965 gtk_entry_set_progress_fraction(GTK_ENTRY(t
->statusbar
),
5966 progress
== 100 ? 0 : (double)progress
/ 100);
5971 webview_npd_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
,
5972 WebKitNetworkRequest
*request
, WebKitWebNavigationAction
*na
,
5973 WebKitWebPolicyDecision
*pd
, struct tab
*t
)
5976 WebKitWebNavigationReason reason
;
5977 struct domain
*d
= NULL
;
5980 show_oops_s("webview_npd_cb invalid parameters");
5984 DNPRINTF(XT_D_NAV
, "webview_npd_cb: ctrl_click %d %s\n",
5986 webkit_network_request_get_uri(request
));
5988 uri
= (char *)webkit_network_request_get_uri(request
);
5990 /* if this is an xtp url, we don't load anything else */
5991 if (parse_xtp_url(t
, uri
))
5994 if (t
->ctrl_click
) {
5996 create_new_tab(uri
, NULL
, ctrl_click_focus
);
5997 webkit_web_policy_decision_ignore(pd
);
5998 return (TRUE
); /* we made the decission */
6002 * This is a little hairy but it comes down to this:
6003 * when we run in whitelist mode we have to assist the browser in
6004 * opening the URL that it would have opened in a new tab.
6006 reason
= webkit_web_navigation_action_get_reason(na
);
6007 if (reason
== WEBKIT_WEB_NAVIGATION_REASON_LINK_CLICKED
) {
6008 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1)
6009 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
6012 webkit_web_policy_decision_use(pd
);
6013 return (TRUE
); /* we made the decission */
6020 webview_cwv_cb(WebKitWebView
*wv
, WebKitWebFrame
*wf
, struct tab
*t
)
6023 struct domain
*d
= NULL
;
6025 WebKitWebView
*webview
= NULL
;
6027 DNPRINTF(XT_D_NAV
, "webview_cwv_cb: %s\n",
6028 webkit_web_view_get_uri(wv
));
6031 /* open in current tab */
6033 } else if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
6034 uri
= webkit_web_view_get_uri(wv
);
6035 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
6038 tt
= create_new_tab(NULL
, NULL
, 1);
6040 } else if (enable_scripts
== 1) {
6041 tt
= create_new_tab(NULL
, NULL
, 1);
6049 webview_closewv_cb(WebKitWebView
*wv
, struct tab
*t
)
6052 struct domain
*d
= NULL
;
6054 DNPRINTF(XT_D_NAV
, "webview_close_cb: %d\n", t
->tab_id
);
6056 if (enable_scripts
== 0 && enable_cookie_whitelist
== 1) {
6057 uri
= webkit_web_view_get_uri(wv
);
6058 if (uri
&& (d
= wl_find_uri(uri
, &js_wl
)) == NULL
)
6062 } else if (enable_scripts
== 1)
6069 webview_event_cb(GtkWidget
*w
, GdkEventButton
*e
, struct tab
*t
)
6071 /* we can not eat the event without throwing gtk off so defer it */
6073 /* catch middle click */
6074 if (e
->type
== GDK_BUTTON_RELEASE
&& e
->button
== 2) {
6079 /* catch ctrl click */
6080 if (e
->type
== GDK_BUTTON_RELEASE
&&
6081 CLEAN(e
->state
) == GDK_CONTROL_MASK
)
6086 return (XT_CB_PASSTHROUGH
);
6090 run_mimehandler(struct tab
*t
, char *mime_type
, WebKitNetworkRequest
*request
)
6092 struct mime_type
*m
;
6094 m
= find_mime_type(mime_type
);
6102 show_oops(t
, "can't fork mime handler");
6111 execlp(m
->mt_action
, m
->mt_action
,
6112 webkit_network_request_get_uri(request
), (void *)NULL
);
6121 get_mime_type(char *file
)
6123 const char *mime_type
;
6127 if (g_str_has_prefix(file
, "file://"))
6128 file
+= strlen("file://");
6130 gf
= g_file_new_for_path(file
);
6131 fi
= g_file_query_info(gf
, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
, 0,
6133 mime_type
= g_file_info_get_content_type(fi
);
6141 run_download_mimehandler(char *mime_type
, char *file
)
6143 struct mime_type
*m
;
6145 m
= find_mime_type(mime_type
);
6151 show_oops_s("can't fork download mime handler");
6160 if (g_str_has_prefix(file
, "file://"))
6161 file
+= strlen("file://");
6162 execlp(m
->mt_action
, m
->mt_action
, file
, (void *)NULL
);
6171 download_status_changed_cb(WebKitDownload
*download
, GParamSpec
*spec
,
6174 WebKitDownloadStatus status
;
6175 const gchar
*file
= NULL
, *mime
= NULL
;
6177 if (download
== NULL
)
6179 status
= webkit_download_get_status(download
);
6180 if (status
!= WEBKIT_DOWNLOAD_STATUS_FINISHED
)
6183 file
= webkit_download_get_destination_uri(download
);
6186 mime
= get_mime_type((char *)file
);
6190 run_download_mimehandler((char *)mime
, (char *)file
);
6194 webview_mimetype_cb(WebKitWebView
*wv
, WebKitWebFrame
*frame
,
6195 WebKitNetworkRequest
*request
, char *mime_type
,
6196 WebKitWebPolicyDecision
*decision
, struct tab
*t
)
6199 show_oops_s("webview_mimetype_cb invalid parameters");
6203 DNPRINTF(XT_D_DOWNLOAD
, "webview_mimetype_cb: tab %d mime %s\n",
6204 t
->tab_id
, mime_type
);
6206 if (run_mimehandler(t
, mime_type
, request
) == 0) {
6207 webkit_web_policy_decision_ignore(decision
);
6212 if (webkit_web_view_can_show_mime_type(wv
, mime_type
) == FALSE
) {
6213 webkit_web_policy_decision_download(decision
);
6221 webview_download_cb(WebKitWebView
*wv
, WebKitDownload
*wk_download
,
6224 const gchar
*filename
;
6226 struct download
*download_entry
;
6229 if (wk_download
== NULL
|| t
== NULL
) {
6230 show_oops_s("%s invalid parameters", __func__
);
6234 filename
= webkit_download_get_suggested_filename(wk_download
);
6235 if (filename
== NULL
)
6236 return (FALSE
); /* abort download */
6238 uri
= g_strdup_printf("file://%s/%s", download_dir
, filename
);
6240 DNPRINTF(XT_D_DOWNLOAD
, "%s: tab %d filename %s "
6241 "local %s\n", __func__
, t
->tab_id
, filename
, uri
);
6243 webkit_download_set_destination_uri(wk_download
, uri
);
6245 if (webkit_download_get_status(wk_download
) ==
6246 WEBKIT_DOWNLOAD_STATUS_ERROR
) {
6247 show_oops(t
, "%s: download failed to start", __func__
);
6249 gtk_label_set_text(GTK_LABEL(t
->label
), "Download Failed");
6251 /* connect "download first" mime handler */
6252 g_signal_connect(G_OBJECT(wk_download
), "notify::status",
6253 G_CALLBACK(download_status_changed_cb
), NULL
);
6255 download_entry
= g_malloc(sizeof(struct download
));
6256 download_entry
->download
= wk_download
;
6257 download_entry
->tab
= t
;
6258 download_entry
->id
= next_download_id
++;
6259 RB_INSERT(download_list
, &downloads
, download_entry
);
6260 /* get from history */
6261 g_object_ref(wk_download
);
6262 gtk_label_set_text(GTK_LABEL(t
->label
), "Downloading");
6263 show_oops(t
, "Download of '%s' started...",
6264 basename(webkit_download_get_destination_uri(wk_download
)));
6270 /* sync other download manager tabs */
6271 update_download_tabs(NULL
);
6274 * NOTE: never redirect/render the current tab before this
6275 * function returns. This will cause the download to never start.
6277 return (ret
); /* start download */
6281 webview_hover_cb(WebKitWebView
*wv
, gchar
*title
, gchar
*uri
, struct tab
*t
)
6283 DNPRINTF(XT_D_KEY
, "webview_hover_cb: %s %s\n", title
, uri
);
6286 show_oops_s("webview_hover_cb");
6291 set_status(t
, uri
, XT_STATUS_LINK
);
6294 set_status(t
, t
->status
, XT_STATUS_NOTHING
);
6299 handle_keypress(struct tab
*t
, GdkEventKey
*e
, int entry
)
6301 struct key_binding
*k
;
6303 TAILQ_FOREACH(k
, &kbl
, entry
)
6304 if (e
->keyval
== k
->key
&& (entry
? k
->use_in_entry
: 1)) {
6306 if ((e
->state
& (CTRL
| MOD1
)) == 0)
6307 return (cmd_execute(t
, k
->cmd
));
6308 } else if ((e
->state
& k
->mask
) == k
->mask
) {
6309 return (cmd_execute(t
, k
->cmd
));
6313 return (XT_CB_PASSTHROUGH
);
6317 wv_keypress_after_cb(GtkWidget
*w
, GdkEventKey
*e
, struct tab
*t
)
6319 char s
[2], buf
[128];
6320 const char *errstr
= NULL
;
6323 /* don't use w directly; use t->whatever instead */
6326 show_oops_s("wv_keypress_after_cb");
6327 return (XT_CB_PASSTHROUGH
);
6330 DNPRINTF(XT_D_KEY
, "wv_keypress_after_cb: keyval 0x%x mask 0x%x t %p\n",
6331 e
->keyval
, e
->state
, t
);
6335 if (CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Escape
) {
6337 return (XT_CB_HANDLED
);
6341 if (CLEAN(e
->state
) == 0 && e
->keyval
== GDK_Return
) {
6342 link
= strtonum(t
->hint_num
, 1, 1000, &errstr
);
6344 /* we have a string */
6346 /* we have a number */
6347 snprintf(buf
, sizeof buf
, "vimprobable_fire(%s)",
6355 /* XXX unfuck this */
6356 if (CLEAN(e
->state
) == 0 && e
->keyval
== GDK_BackSpace
) {
6357 if (t
->hint_mode
== XT_HINT_NUMERICAL
) {
6358 /* last input was numerical */
6360 l
= strlen(t
->hint_num
);
6367 t
->hint_num
[l
] = '\0';
6371 } else if (t
->hint_mode
== XT_HINT_ALPHANUM
) {
6372 /* last input was alphanumerical */
6374 l
= strlen(t
->hint_buf
);
6381 t
->hint_buf
[l
] = '\0';
6391 /* numerical input */
6392 if (CLEAN(e
->state
) == 0 &&
6393 ((e
->keyval
>= GDK_0
&& e
->keyval
<= GDK_9
) || (e
->keyval
>= GDK_KP_0
&& e
->keyval
<= GDK_KP_9
))) {
6394 snprintf(s
, sizeof s
, "%c", e
->keyval
);
6395 strlcat(t
->hint_num
, s
, sizeof t
->hint_num
);
6396 DNPRINTF(XT_D_JS
, "wv_keypress_after_cb: numerical %s\n",
6399 link
= strtonum(t
->hint_num
, 1, 1000, &errstr
);
6401 DNPRINTF(XT_D_JS
, "wv_keypress_after_cb: invalid link number\n");
6404 snprintf(buf
, sizeof buf
, "vimprobable_update_hints(%s)",
6406 t
->hint_mode
= XT_HINT_NUMERICAL
;
6410 /* empty the counter buffer */
6411 bzero(t
->hint_buf
, sizeof t
->hint_buf
);
6412 return (XT_CB_HANDLED
);
6415 /* alphanumerical input */
6417 (CLEAN(e
->state
) == 0 && e
->keyval
>= GDK_a
&& e
->keyval
<= GDK_z
) ||
6418 (CLEAN(e
->state
) == GDK_SHIFT_MASK
&& e
->keyval
>= GDK_A
&& e
->keyval
<= GDK_Z
) ||
6419 (CLEAN(e
->state
) == 0 && ((e
->keyval
>= GDK_0
&& e
->keyval
<= GDK_9
) ||
6420 ((e
->keyval
>= GDK_KP_0
&& e
->keyval
<= GDK_KP_9
) && (t
->hint_mode
!= XT_HINT_NUMERICAL
))))) {
6421 snprintf(s
, sizeof s
, "%c", e
->keyval
);
6422 strlcat(t
->hint_buf
, s
, sizeof t
->hint_buf
);
6423 DNPRINTF(XT_D_JS
, "wv_keypress_after_cb: alphanumerical %s\n",
6426 snprintf(buf
, sizeof buf
, "vimprobable_cleanup()");
6429 snprintf(buf
, sizeof buf
, "vimprobable_show_hints('%s')",
6431 t
->hint_mode
= XT_HINT_ALPHANUM
;
6434 /* empty the counter buffer */
6435 bzero(t
->hint_num
, sizeof t
->hint_num
);
6436 return (XT_CB_HANDLED
);
6439 return (XT_CB_HANDLED
);
6442 return (handle_keypress(t
, e
, 0));
6446 wv_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6450 return (XT_CB_PASSTHROUGH
);
6454 cmd_keyrelease_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6456 const gchar
*c
= gtk_entry_get_text(w
);
6460 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x t %p\n",
6461 e
->keyval
, e
->state
, t
);
6464 show_oops_s("cmd_keyrelease_cb invalid parameters");
6465 return (XT_CB_PASSTHROUGH
);
6468 DNPRINTF(XT_D_CMD
, "cmd_keyrelease_cb: keyval 0x%x mask 0x%x t %p\n",
6469 e
->keyval
, e
->state
, t
);
6473 if (strlen(c
) == 1) {
6474 webkit_web_view_unmark_text_matches(t
->wv
);
6480 else if (c
[0] == '?')
6486 if (webkit_web_view_search_text(t
->wv
, &c
[1], FALSE
, forward
, TRUE
) ==
6488 /* not found, mark red */
6489 gdk_color_parse(XT_COLOR_RED
, &color
);
6490 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
6491 /* unmark and remove selection */
6492 webkit_web_view_unmark_text_matches(t
->wv
);
6493 /* my kingdom for a way to unselect text in webview */
6495 /* found, highlight all */
6496 webkit_web_view_unmark_text_matches(t
->wv
);
6497 webkit_web_view_mark_text_matches(t
->wv
, &c
[1], FALSE
, 0);
6498 webkit_web_view_set_highlight_text_matches(t
->wv
, TRUE
);
6499 gdk_color_parse(XT_COLOR_WHITE
, &color
);
6500 gtk_widget_modify_base(t
->cmd
, GTK_STATE_NORMAL
, &color
);
6503 return (XT_CB_PASSTHROUGH
);
6507 match_uri(const gchar
*uri
, const gchar
*key
) {
6510 gboolean match
= FALSE
;
6514 if (!strncmp(key
, uri
, len
))
6517 voffset
= strstr(uri
, "/") + 2;
6518 if (!strncmp(key
, voffset
, len
))
6520 else if (g_str_has_prefix(voffset
, "www.")) {
6521 voffset
= voffset
+ strlen("www.");
6522 if (!strncmp(key
, voffset
, len
))
6531 cmd_getlist(int id
, char *key
)
6536 if (id
>= 0 && (cmds
[id
].userarg
&& (cmds
[id
].arg
.i
== XT_TAB_OPEN
|| cmds
[id
].arg
.i
== XT_TAB_NEW
))) {
6537 RB_FOREACH_REVERSE(h
, history_list
, &hl
)
6538 if (match_uri(h
->uri
, key
)) {
6539 cmd_status
.list
[c
] = (char *)h
->uri
;
6548 dep
= (id
== -1) ? 0 : cmds
[id
].level
+ 1;
6550 for (i
= id
+ 1; i
< LENGTH(cmds
); i
++) {
6551 if(cmds
[i
].level
< dep
)
6553 if (cmds
[i
].level
== dep
&& !strncmp(key
, cmds
[i
].cmd
, strlen(key
)))
6554 cmd_status
.list
[c
++] = cmds
[i
].cmd
;
6562 cmd_getnext(int dir
)
6564 cmd_status
.index
+= dir
;
6566 if (cmd_status
.index
< 0)
6567 cmd_status
.index
= cmd_status
.len
- 1;
6568 else if (cmd_status
.index
>= cmd_status
.len
)
6569 cmd_status
.index
= 0;
6571 return cmd_status
.list
[cmd_status
.index
];
6575 cmd_tokenize(char *s
, char *tokens
[])
6579 size_t len
= strlen(s
);
6580 bool blank
= len
== 0 || (len
> 0 && s
[len
-1] == ' ');
6582 for (tok
= strtok_r(s
, " ", &last
); tok
&& i
< 3; tok
= strtok_r(NULL
, " ", &last
), i
++)
6592 cmd_complete(struct tab
*t
, char *str
, int dir
)
6594 GtkEntry
*w
= GTK_ENTRY(t
->cmd
);
6595 int i
, j
, levels
, c
= 0, dep
= 0, parent
= -1, matchcount
= 0;
6596 char *tok
, *match
, *s
= g_strdup(str
);
6598 char res
[XT_MAX_URL_LENGTH
+ 32] = ":";
6600 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: complete %s\n", str
);
6602 levels
= cmd_tokenize(s
, tokens
);
6605 for (i
= 0; i
< levels
- 1; i
++) {
6608 for (j
= c
; j
< LENGTH(cmds
); j
++) {
6609 if (cmds
[j
].level
< dep
)
6611 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
, strlen(tok
))) {
6614 if (strlen(tok
) == strlen(cmds
[j
].cmd
)) {
6621 if (matchcount
== 1) {
6622 strlcat(res
, tok
, sizeof res
);
6623 strlcat(res
, " ", sizeof res
);
6631 if (cmd_status
.index
== -1)
6632 cmd_getlist(parent
, tokens
[i
]);
6634 if (cmd_status
.len
> 0) {
6635 match
= cmd_getnext(dir
);
6636 strlcat(res
, match
, sizeof res
);
6637 gtk_entry_set_text(w
, res
);
6638 gtk_editable_set_position(GTK_EDITABLE(w
), -1);
6643 cmd_execute(struct tab
*t
, char *str
)
6645 struct cmd
*cmd
= NULL
;
6646 char *tok
, *last
, *s
= g_strdup(str
);
6647 int j
, len
, c
= 0, dep
= 0, matchcount
= 0;
6649 for (tok
= strtok_r(s
, " ", &last
); tok
;
6650 tok
= strtok_r(NULL
, " ", &last
)) {
6652 for (j
= c
; j
< LENGTH(cmds
); j
++) {
6653 if (cmds
[j
].level
< dep
)
6655 len
= (tok
[strlen(tok
) - 1] == '!') ? strlen(tok
) - 1: strlen(tok
);
6656 if (cmds
[j
].level
== dep
&& !strncmp(tok
, cmds
[j
].cmd
, len
)) {
6660 if (len
== strlen(cmds
[j
].cmd
)) {
6666 if (matchcount
== 1) {
6671 show_oops(t
, "Invalid command: %s", str
);
6673 return (XT_CB_PASSTHROUGH
);
6678 cmd
->arg
.s
= last
? g_strdup(last
) : g_strdup("");
6680 cmd
->arg
.s
= g_strdup(tok
);
6682 /* arg->s contains last token */
6683 cmd
->func(t
, &cmd
->arg
);
6688 return (XT_CB_HANDLED
);
6692 entry_key_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6695 show_oops_s("entry_key_cb invalid parameters");
6696 return (XT_CB_PASSTHROUGH
);
6699 DNPRINTF(XT_D_CMD
, "entry_key_cb: keyval 0x%x mask 0x%x t %p\n",
6700 e
->keyval
, e
->state
, t
);
6704 if (e
->keyval
== GDK_Escape
) {
6705 /* don't use focus_webview(t) because we want to type :cmds */
6706 gtk_widget_grab_focus(GTK_WIDGET(t
->wv
));
6709 return (handle_keypress(t
, e
, 1));
6713 cmd_keypress_cb(GtkEntry
*w
, GdkEventKey
*e
, struct tab
*t
)
6715 int rv
= XT_CB_HANDLED
;
6716 const gchar
*c
= gtk_entry_get_text(w
);
6719 show_oops_s("cmd_keypress_cb parameters");
6720 return (XT_CB_PASSTHROUGH
);
6723 DNPRINTF(XT_D_CMD
, "cmd_keypress_cb: keyval 0x%x mask 0x%x t %p\n",
6724 e
->keyval
, e
->state
, t
);
6728 e
->keyval
= GDK_Escape
;
6729 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?'))
6730 e
->keyval
= GDK_Escape
;
6732 if (e
->keyval
!= GDK_Tab
&& e
->keyval
!= GDK_Shift_L
&& e
->keyval
!= GDK_ISO_Left_Tab
)
6733 cmd_status
.index
= -1;
6735 switch (e
->keyval
) {
6738 cmd_complete(t
, (char *)&c
[1], 1);
6740 case GDK_ISO_Left_Tab
:
6742 cmd_complete(t
, (char *)&c
[1], -1);
6746 if (!(!strcmp(c
, ":") || !strcmp(c
, "/") || !strcmp(c
, "?")))
6754 if (c
[0] == '/' || c
[0] == '?')
6755 webkit_web_view_unmark_text_matches(t
->wv
);
6759 rv
= XT_CB_PASSTHROUGH
;
6765 cmd_focusout_cb(GtkWidget
*w
, GdkEventFocus
*e
, struct tab
*t
)
6768 show_oops_s("cmd_focusout_cb invalid parameters");
6769 return (XT_CB_PASSTHROUGH
);
6771 DNPRINTF(XT_D_CMD
, "cmd_focusout_cb: tab %d\n", t
->tab_id
);
6776 if (show_url
== 0 || t
->focus_wv
)
6779 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
6781 return (XT_CB_PASSTHROUGH
);
6785 cmd_activate_cb(GtkEntry
*entry
, struct tab
*t
)
6788 const gchar
*c
= gtk_entry_get_text(entry
);
6791 show_oops_s("cmd_activate_cb invalid parameters");
6795 DNPRINTF(XT_D_CMD
, "cmd_activate_cb: tab %d %s\n", t
->tab_id
, c
);
6802 else if (!(c
[0] == ':' || c
[0] == '/' || c
[0] == '?'))
6808 if (c
[0] == '/' || c
[0] == '?') {
6809 if (t
->search_text
) {
6810 g_free(t
->search_text
);
6811 t
->search_text
= NULL
;
6814 t
->search_text
= g_strdup(s
);
6816 g_free(global_search
);
6817 global_search
= g_strdup(s
);
6818 t
->search_forward
= c
[0] == '/';
6830 backward_cb(GtkWidget
*w
, struct tab
*t
)
6835 show_oops_s("backward_cb invalid parameters");
6839 DNPRINTF(XT_D_NAV
, "backward_cb: tab %d\n", t
->tab_id
);
6846 forward_cb(GtkWidget
*w
, struct tab
*t
)
6851 show_oops_s("forward_cb invalid parameters");
6855 DNPRINTF(XT_D_NAV
, "forward_cb: tab %d\n", t
->tab_id
);
6857 a
.i
= XT_NAV_FORWARD
;
6862 home_cb(GtkWidget
*w
, struct tab
*t
)
6865 show_oops_s("home_cb invalid parameters");
6869 DNPRINTF(XT_D_NAV
, "home_cb: tab %d\n", t
->tab_id
);
6875 stop_cb(GtkWidget
*w
, struct tab
*t
)
6877 WebKitWebFrame
*frame
;
6880 show_oops_s("stop_cb invalid parameters");
6884 DNPRINTF(XT_D_NAV
, "stop_cb: tab %d\n", t
->tab_id
);
6886 frame
= webkit_web_view_get_main_frame(t
->wv
);
6887 if (frame
== NULL
) {
6888 show_oops(t
, "stop_cb: no frame");
6892 webkit_web_frame_stop_loading(frame
);
6893 abort_favicon_download(t
);
6897 setup_webkit(struct tab
*t
)
6899 if (is_g_object_setting(G_OBJECT(t
->settings
), "enable-dns-prefetching"))
6900 g_object_set(G_OBJECT(t
->settings
), "enable-dns-prefetching",
6901 FALSE
, (char *)NULL
);
6903 warnx("webkit does not have \"enable-dns-prefetching\" property");
6904 g_object_set(G_OBJECT(t
->settings
),
6905 "user-agent", t
->user_agent
, (char *)NULL
);
6906 g_object_set(G_OBJECT(t
->settings
),
6907 "enable-scripts", enable_scripts
, (char *)NULL
);
6908 g_object_set(G_OBJECT(t
->settings
),
6909 "enable-plugins", enable_plugins
, (char *)NULL
);
6910 g_object_set(G_OBJECT(t
->settings
),
6911 "javascript-can-open-windows-automatically", enable_scripts
, (char *)NULL
);
6912 g_object_set(G_OBJECT(t
->wv
),
6913 "full-content-zoom", TRUE
, (char *)NULL
);
6914 adjustfont_webkit(t
, XT_FONT_SET
);
6916 webkit_web_view_set_settings(t
->wv
, t
->settings
);
6920 create_browser(struct tab
*t
)
6926 show_oops_s("create_browser invalid parameters");
6930 t
->sb_h
= GTK_SCROLLBAR(gtk_hscrollbar_new(NULL
));
6931 t
->sb_v
= GTK_SCROLLBAR(gtk_vscrollbar_new(NULL
));
6932 t
->adjust_h
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_h
));
6933 t
->adjust_v
= gtk_range_get_adjustment(GTK_RANGE(t
->sb_v
));
6935 w
= gtk_scrolled_window_new(t
->adjust_h
, t
->adjust_v
);
6936 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(w
),
6937 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
6939 t
->wv
= WEBKIT_WEB_VIEW(webkit_web_view_new());
6940 gtk_container_add(GTK_CONTAINER(w
), GTK_WIDGET(t
->wv
));
6943 t
->settings
= webkit_web_settings_new();
6945 if (user_agent
== NULL
) {
6946 g_object_get(G_OBJECT(t
->settings
), "user-agent", &strval
,
6948 t
->user_agent
= g_strdup_printf("%s %s+", strval
, version
);
6951 t
->user_agent
= g_strdup(user_agent
);
6953 t
->stylesheet
= g_strdup_printf("file://%s/style.css", resource_dir
);
6965 w
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
6966 gtk_window_set_default_size(GTK_WINDOW(w
), window_width
, window_height
);
6967 gtk_widget_set_name(w
, "xxxterm");
6968 gtk_window_set_wmclass(GTK_WINDOW(w
), "xxxterm", "XXXTerm");
6969 g_signal_connect(G_OBJECT(w
), "delete_event",
6970 G_CALLBACK (gtk_main_quit
), NULL
);
6976 create_kiosk_toolbar(struct tab
*t
)
6978 GtkWidget
*toolbar
= NULL
, *b
;
6980 b
= gtk_hbox_new(FALSE
, 0);
6982 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
6984 /* backward button */
6985 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
6986 gtk_widget_set_sensitive(t
->backward
, FALSE
);
6987 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
6988 G_CALLBACK(backward_cb
), t
);
6989 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, TRUE
, TRUE
, 0);
6991 /* forward button */
6992 t
->forward
= create_button("Forward", GTK_STOCK_GO_FORWARD
, 0);
6993 gtk_widget_set_sensitive(t
->forward
, FALSE
);
6994 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
6995 G_CALLBACK(forward_cb
), t
);
6996 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, TRUE
, TRUE
, 0);
6999 t
->gohome
= create_button("Home", GTK_STOCK_HOME
, 0);
7000 gtk_widget_set_sensitive(t
->gohome
, true);
7001 g_signal_connect(G_OBJECT(t
->gohome
), "clicked",
7002 G_CALLBACK(home_cb
), t
);
7003 gtk_box_pack_start(GTK_BOX(b
), t
->gohome
, TRUE
, TRUE
, 0);
7005 /* create widgets but don't use them */
7006 t
->uri_entry
= gtk_entry_new();
7007 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
7008 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
7009 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
7015 create_toolbar(struct tab
*t
)
7017 GtkWidget
*toolbar
= NULL
, *b
, *eb1
;
7019 b
= gtk_hbox_new(FALSE
, 0);
7021 gtk_container_set_border_width(GTK_CONTAINER(toolbar
), 0);
7024 /* backward button */
7025 t
->backward
= create_button("Back", GTK_STOCK_GO_BACK
, 0);
7026 gtk_widget_set_sensitive(t
->backward
, FALSE
);
7027 g_signal_connect(G_OBJECT(t
->backward
), "clicked",
7028 G_CALLBACK(backward_cb
), t
);
7029 gtk_box_pack_start(GTK_BOX(b
), t
->backward
, FALSE
, FALSE
, 0);
7031 /* forward button */
7032 t
->forward
= create_button("Forward",GTK_STOCK_GO_FORWARD
, 0);
7033 gtk_widget_set_sensitive(t
->forward
, FALSE
);
7034 g_signal_connect(G_OBJECT(t
->forward
), "clicked",
7035 G_CALLBACK(forward_cb
), t
);
7036 gtk_box_pack_start(GTK_BOX(b
), t
->forward
, FALSE
,
7040 t
->stop
= create_button("Stop", GTK_STOCK_STOP
, 0);
7041 gtk_widget_set_sensitive(t
->stop
, FALSE
);
7042 g_signal_connect(G_OBJECT(t
->stop
), "clicked",
7043 G_CALLBACK(stop_cb
), t
);
7044 gtk_box_pack_start(GTK_BOX(b
), t
->stop
, FALSE
,
7048 t
->js_toggle
= create_button("JS-Toggle", enable_scripts
?
7049 GTK_STOCK_MEDIA_PLAY
: GTK_STOCK_MEDIA_PAUSE
, 0);
7050 gtk_widget_set_sensitive(t
->js_toggle
, TRUE
);
7051 g_signal_connect(G_OBJECT(t
->js_toggle
), "clicked",
7052 G_CALLBACK(js_toggle_cb
), t
);
7053 gtk_box_pack_start(GTK_BOX(b
), t
->js_toggle
, FALSE
, FALSE
, 0);
7056 t
->uri_entry
= gtk_entry_new();
7057 g_signal_connect(G_OBJECT(t
->uri_entry
), "activate",
7058 G_CALLBACK(activate_uri_entry_cb
), t
);
7059 g_signal_connect(G_OBJECT(t
->uri_entry
), "key-press-event",
7060 G_CALLBACK(entry_key_cb
), t
);
7062 eb1
= gtk_hbox_new(FALSE
, 0);
7063 gtk_container_set_border_width(GTK_CONTAINER(eb1
), 1);
7064 gtk_box_pack_start(GTK_BOX(eb1
), t
->uri_entry
, TRUE
, TRUE
, 0);
7065 gtk_box_pack_start(GTK_BOX(b
), eb1
, TRUE
, TRUE
, 0);
7068 if (fancy_bar
&& search_string
) {
7070 t
->search_entry
= gtk_entry_new();
7071 gtk_entry_set_width_chars(GTK_ENTRY(t
->search_entry
), 30);
7072 g_signal_connect(G_OBJECT(t
->search_entry
), "activate",
7073 G_CALLBACK(activate_search_entry_cb
), t
);
7074 g_signal_connect(G_OBJECT(t
->search_entry
), "key-press-event",
7075 G_CALLBACK(entry_key_cb
), t
);
7076 gtk_widget_set_size_request(t
->search_entry
, -1, -1);
7077 eb2
= gtk_hbox_new(FALSE
, 0);
7078 gtk_container_set_border_width(GTK_CONTAINER(eb2
), 1);
7079 gtk_box_pack_start(GTK_BOX(eb2
), t
->search_entry
, TRUE
, TRUE
,
7081 gtk_box_pack_start(GTK_BOX(b
), eb2
, FALSE
, FALSE
, 0);
7091 TAILQ_FOREACH(t
, &tabs
, entry
)
7092 t
->tab_id
= gtk_notebook_page_num(notebook
, t
->vbox
);
7096 undo_close_tab_save(struct tab
*t
)
7100 struct undo
*u1
, *u2
;
7102 WebKitWebHistoryItem
*item
;
7104 if ((uri
= get_uri(t
->wv
)) == NULL
)
7107 u1
= g_malloc0(sizeof(struct undo
));
7108 u1
->uri
= g_strdup(uri
);
7110 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7112 m
= webkit_web_back_forward_list_get_forward_length(t
->bfl
);
7113 n
= webkit_web_back_forward_list_get_back_length(t
->bfl
);
7116 /* forward history */
7117 items
= webkit_web_back_forward_list_get_forward_list_with_limit(t
->bfl
, m
);
7121 u1
->history
= g_list_prepend(u1
->history
,
7122 webkit_web_history_item_copy(item
));
7123 items
= g_list_next(items
);
7128 item
= webkit_web_back_forward_list_get_current_item(t
->bfl
);
7129 u1
->history
= g_list_prepend(u1
->history
,
7130 webkit_web_history_item_copy(item
));
7134 items
= webkit_web_back_forward_list_get_back_list_with_limit(t
->bfl
, n
);
7138 u1
->history
= g_list_prepend(u1
->history
,
7139 webkit_web_history_item_copy(item
));
7140 items
= g_list_next(items
);
7143 TAILQ_INSERT_HEAD(&undos
, u1
, entry
);
7145 if (undo_count
> XT_MAX_UNDO_CLOSE_TAB
) {
7146 u2
= TAILQ_LAST(&undos
, undo_tailq
);
7147 TAILQ_REMOVE(&undos
, u2
, entry
);
7149 g_list_free(u2
->history
);
7158 delete_tab(struct tab
*t
)
7162 DNPRINTF(XT_D_TAB
, "delete_tab: %p\n", t
);
7167 TAILQ_REMOVE(&tabs
, t
, entry
);
7169 /* halt all webkit activity */
7170 abort_favicon_download(t
);
7171 webkit_web_view_stop_loading(t
->wv
);
7172 undo_close_tab_save(t
);
7174 if (browser_mode
== XT_BM_KIOSK
) {
7175 gtk_widget_destroy(t
->uri_entry
);
7176 gtk_widget_destroy(t
->stop
);
7177 gtk_widget_destroy(t
->js_toggle
);
7180 gtk_widget_destroy(t
->vbox
);
7181 g_free(t
->user_agent
);
7182 g_free(t
->stylesheet
);
7186 if (TAILQ_EMPTY(&tabs
)) {
7187 if (browser_mode
== XT_BM_KIOSK
)
7188 create_new_tab(home
, NULL
, 1);
7190 create_new_tab(NULL
, NULL
, 1);
7193 /* recreate session */
7194 if (session_autosave
) {
7201 adjustfont_webkit(struct tab
*t
, int adjust
)
7206 show_oops_s("adjustfont_webkit invalid parameters");
7210 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
7211 if (adjust
== XT_FONT_SET
) {
7212 t
->font_size
= default_font_size
;
7213 zoom
= default_zoom_level
;
7214 t
->font_size
+= adjust
;
7215 g_object_set(G_OBJECT(t
->settings
), "default-font-size",
7216 t
->font_size
, (char *)NULL
);
7217 g_object_get(G_OBJECT(t
->settings
), "default-font-size",
7218 &t
->font_size
, (char *)NULL
);
7220 t
->font_size
+= adjust
;
7221 zoom
+= adjust
/25.0;
7226 g_object_set(G_OBJECT(t
->wv
), "zoom-level", zoom
, (char *)NULL
);
7227 g_object_get(G_OBJECT(t
->wv
), "zoom-level", &zoom
, (char *)NULL
);
7231 append_tab(struct tab
*t
)
7236 TAILQ_INSERT_TAIL(&tabs
, t
, entry
);
7237 t
->tab_id
= gtk_notebook_append_page(notebook
, t
->vbox
, t
->tab_content
);
7241 create_new_tab(char *title
, struct undo
*u
, int focus
)
7244 int load
= 1, id
, notfound
;
7246 WebKitWebHistoryItem
*item
;
7250 DNPRINTF(XT_D_TAB
, "create_new_tab: title %s focus %d\n", title
, focus
);
7252 if (tabless
&& !TAILQ_EMPTY(&tabs
)) {
7253 DNPRINTF(XT_D_TAB
, "create_new_tab: new tab rejected\n");
7257 t
= g_malloc0(sizeof *t
);
7259 if (title
== NULL
) {
7260 title
= "(untitled)";
7264 t
->vbox
= gtk_vbox_new(FALSE
, 0);
7266 /* label + button for tab */
7267 b
= gtk_hbox_new(FALSE
, 0);
7270 #if GTK_CHECK_VERSION(2, 20, 0)
7271 t
->spinner
= gtk_spinner_new ();
7273 t
->label
= gtk_label_new(title
);
7274 bb
= create_button("Close", GTK_STOCK_CLOSE
, 1);
7275 gtk_widget_set_size_request(t
->label
, 100, 0);
7276 gtk_widget_set_size_request(b
, 130, 0);
7278 gtk_box_pack_start(GTK_BOX(b
), bb
, FALSE
, FALSE
, 0);
7279 gtk_box_pack_start(GTK_BOX(b
), t
->label
, FALSE
, FALSE
, 0);
7280 #if GTK_CHECK_VERSION(2, 20, 0)
7281 gtk_box_pack_start(GTK_BOX(b
), t
->spinner
, FALSE
, FALSE
, 0);
7285 if (browser_mode
== XT_BM_KIOSK
)
7286 t
->toolbar
= create_kiosk_toolbar(t
);
7288 t
->toolbar
= create_toolbar(t
);
7290 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->toolbar
, FALSE
, FALSE
, 0);
7293 t
->browser_win
= create_browser(t
);
7294 gtk_box_pack_start(GTK_BOX(t
->vbox
), t
->browser_win
, TRUE
, TRUE
, 0);
7296 /* oops message for user feedback */
7297 t
->oops
= gtk_entry_new();
7298 gtk_entry_set_inner_border(GTK_ENTRY(t
->oops
), NULL
);
7299 gtk_entry_set_has_frame(GTK_ENTRY(t
->oops
), FALSE
);
7300 gtk_widget_set_can_focus(GTK_WIDGET(t
->oops
), FALSE
);
7301 gdk_color_parse(XT_COLOR_RED
, &color
);
7302 gtk_widget_modify_base(t
->oops
, GTK_STATE_NORMAL
, &color
);
7303 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->oops
, FALSE
, FALSE
, 0);
7306 t
->cmd
= gtk_entry_new();
7307 gtk_entry_set_inner_border(GTK_ENTRY(t
->cmd
), NULL
);
7308 gtk_entry_set_has_frame(GTK_ENTRY(t
->cmd
), FALSE
);
7309 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->cmd
, FALSE
, FALSE
, 0);
7312 t
->statusbar
= gtk_entry_new();
7313 gtk_entry_set_inner_border(GTK_ENTRY(t
->statusbar
), NULL
);
7314 gtk_entry_set_has_frame(GTK_ENTRY(t
->statusbar
), FALSE
);
7315 gtk_widget_set_can_focus(GTK_WIDGET(t
->statusbar
), FALSE
);
7316 gdk_color_parse(XT_COLOR_BLACK
, &color
);
7317 gtk_widget_modify_base(t
->statusbar
, GTK_STATE_NORMAL
, &color
);
7318 gdk_color_parse(XT_COLOR_WHITE
, &color
);
7319 gtk_widget_modify_text(t
->statusbar
, GTK_STATE_NORMAL
, &color
);
7320 gtk_box_pack_end(GTK_BOX(t
->vbox
), t
->statusbar
, FALSE
, FALSE
, 0);
7322 /* xtp meaning is normal by default */
7323 t
->xtp_meaning
= XT_XTP_TAB_MEANING_NORMAL
;
7325 /* set empty favicon */
7326 xt_icon_from_name(t
, "text-html");
7328 /* and show it all */
7329 gtk_widget_show_all(b
);
7330 gtk_widget_show_all(t
->vbox
);
7332 if (append_next
== 0 || gtk_notebook_get_n_pages(notebook
) == 0)
7336 id
= gtk_notebook_get_current_page(notebook
);
7337 TAILQ_FOREACH(tt
, &tabs
, entry
) {
7338 if (tt
->tab_id
== id
) {
7340 TAILQ_INSERT_AFTER(&tabs
, tt
, t
, entry
);
7341 gtk_notebook_insert_page(notebook
, t
->vbox
, b
,
7351 #if GTK_CHECK_VERSION(2, 20, 0)
7352 /* turn spinner off if we are a new tab without uri */
7354 gtk_spinner_stop(GTK_SPINNER(t
->spinner
));
7355 gtk_widget_hide(t
->spinner
);
7358 /* make notebook tabs reorderable */
7359 gtk_notebook_set_tab_reorderable(notebook
, t
->vbox
, TRUE
);
7361 g_object_connect(G_OBJECT(t
->cmd
),
7362 "signal::key-press-event", G_CALLBACK(cmd_keypress_cb
), t
,
7363 "signal::key-release-event", G_CALLBACK(cmd_keyrelease_cb
), t
,
7364 "signal::focus-out-event", G_CALLBACK(cmd_focusout_cb
), t
,
7365 "signal::activate", G_CALLBACK(cmd_activate_cb
), t
,
7368 /* reuse wv_button_cb to hide oops */
7369 g_object_connect(G_OBJECT(t
->oops
),
7370 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7373 g_object_connect(G_OBJECT(t
->wv
),
7374 "signal::key-press-event", G_CALLBACK(wv_keypress_cb
), t
,
7375 "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb
), t
,
7376 "signal::hovering-over-link", G_CALLBACK(webview_hover_cb
), t
,
7377 "signal::download-requested", G_CALLBACK(webview_download_cb
), t
,
7378 "signal::mime-type-policy-decision-requested", G_CALLBACK(webview_mimetype_cb
), t
,
7379 "signal::navigation-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7380 "signal::new-window-policy-decision-requested", G_CALLBACK(webview_npd_cb
), t
,
7381 "signal::create-web-view", G_CALLBACK(webview_cwv_cb
), t
,
7382 "signal::close-web-view", G_CALLBACK(webview_closewv_cb
), t
,
7383 "signal::event", G_CALLBACK(webview_event_cb
), t
,
7384 "signal::load-finished", G_CALLBACK(webview_load_finished_cb
), t
,
7385 "signal::load-progress-changed", G_CALLBACK(webview_progress_changed_cb
), t
,
7386 #if WEBKIT_CHECK_VERSION(1, 1, 18)
7387 "signal::icon-loaded", G_CALLBACK(notify_icon_loaded_cb
), t
,
7389 "signal::button_press_event", G_CALLBACK(wv_button_cb
), t
,
7391 g_signal_connect(t
->wv
,
7392 "notify::load-status", G_CALLBACK(notify_load_status_cb
), t
);
7393 g_signal_connect(t
->wv
,
7394 "notify::title", G_CALLBACK(notify_title_cb
), t
);
7396 /* hijack the unused keys as if we were the browser */
7397 g_object_connect(G_OBJECT(t
->toolbar
),
7398 "signal-after::key-press-event", G_CALLBACK(wv_keypress_after_cb
), t
,
7401 g_signal_connect(G_OBJECT(bb
), "button_press_event",
7402 G_CALLBACK(tab_close_cb
), t
);
7407 url_set_visibility();
7408 statusbar_set_visibility();
7411 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
7412 DNPRINTF(XT_D_TAB
, "create_new_tab: going to tab: %d\n",
7417 gtk_entry_set_text(GTK_ENTRY(t
->uri_entry
), title
);
7421 gtk_widget_grab_focus(GTK_WIDGET(t
->uri_entry
));
7426 t
->bfl
= webkit_web_view_get_back_forward_list(t
->wv
);
7427 /* restore the tab's history */
7428 if (u
&& u
->history
) {
7432 webkit_web_back_forward_list_add_item(t
->bfl
, item
);
7433 items
= g_list_next(items
);
7436 item
= g_list_nth_data(u
->history
, u
->back
);
7438 webkit_web_view_go_to_back_forward_item(t
->wv
, item
);
7441 g_list_free(u
->history
);
7443 webkit_web_back_forward_list_clear(t
->bfl
);
7449 notebook_switchpage_cb(GtkNotebook
*nb
, GtkNotebookPage
*nbp
, guint pn
,
7455 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: tab: %d\n", pn
);
7457 TAILQ_FOREACH(t
, &tabs
, entry
) {
7458 if (t
->tab_id
== pn
) {
7459 DNPRINTF(XT_D_TAB
, "notebook_switchpage_cb: going to "
7462 uri
= webkit_web_view_get_title(t
->wv
);
7465 gtk_window_set_title(GTK_WINDOW(main_window
), uri
);
7477 menuitem_response(struct tab
*t
)
7479 gtk_notebook_set_current_page(notebook
, t
->tab_id
);
7483 arrow_cb(GtkWidget
*w
, GdkEventButton
*event
, gpointer user_data
)
7485 GtkWidget
*menu
, *menu_items
;
7486 GdkEventButton
*bevent
;
7490 if (event
->type
== GDK_BUTTON_PRESS
) {
7491 bevent
= (GdkEventButton
*) event
;
7492 menu
= gtk_menu_new();
7494 TAILQ_FOREACH(ti
, &tabs
, entry
) {
7495 if ((uri
= get_uri(ti
->wv
)) == NULL
)
7496 /* XXX make sure there is something to print */
7497 /* XXX add gui pages in here to look purdy */
7499 menu_items
= gtk_menu_item_new_with_label(uri
);
7500 gtk_menu_append(GTK_MENU (menu
), menu_items
);
7501 gtk_widget_show(menu_items
);
7503 gtk_signal_connect_object(GTK_OBJECT(menu_items
),
7504 "activate", GTK_SIGNAL_FUNC(menuitem_response
),
7508 gtk_menu_popup(GTK_MENU(menu
), NULL
, NULL
, NULL
, NULL
,
7509 bevent
->button
, bevent
->time
);
7511 /* unref object so it'll free itself when popped down */
7512 g_object_ref_sink(menu
);
7513 g_object_unref(menu
);
7515 return (TRUE
/* eat event */);
7518 return (FALSE
/* propagate */);
7522 icon_size_map(int icon_size
)
7524 if (icon_size
<= GTK_ICON_SIZE_INVALID
||
7525 icon_size
> GTK_ICON_SIZE_DIALOG
)
7526 return (GTK_ICON_SIZE_SMALL_TOOLBAR
);
7532 create_button(char *name
, char *stockid
, int size
)
7534 GtkWidget
*button
, *image
;
7538 rcstring
= g_strdup_printf(
7539 "style \"%s-style\"\n"
7541 " GtkWidget::focus-padding = 0\n"
7542 " GtkWidget::focus-line-width = 0\n"
7546 "widget \"*.%s\" style \"%s-style\"", name
, name
, name
);
7547 gtk_rc_parse_string(rcstring
);
7549 button
= gtk_button_new();
7550 gtk_button_set_focus_on_click(GTK_BUTTON(button
), FALSE
);
7551 gtk_icon_size
= icon_size_map(size
? size
: icon_size
);
7553 image
= gtk_image_new_from_stock(stockid
, gtk_icon_size
);
7554 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
7555 gtk_container_set_border_width(GTK_CONTAINER(button
), 1);
7556 gtk_container_add(GTK_CONTAINER(button
), GTK_WIDGET(image
));
7557 gtk_widget_set_name(button
, name
);
7558 gtk_button_set_relief(GTK_BUTTON(button
), GTK_RELIEF_NONE
);
7564 button_set_stockid(GtkWidget
*button
, char *stockid
)
7568 image
= gtk_image_new_from_stock(stockid
, icon_size_map(icon_size
));
7569 gtk_widget_set_size_request(GTK_WIDGET(image
), -1, -1);
7570 gtk_button_set_image(GTK_BUTTON(button
), image
);
7579 char file
[PATH_MAX
];
7582 vbox
= gtk_vbox_new(FALSE
, 0);
7583 gtk_box_set_spacing(GTK_BOX(vbox
), 0);
7584 notebook
= GTK_NOTEBOOK(gtk_notebook_new());
7585 gtk_notebook_set_tab_hborder(notebook
, 0);
7586 gtk_notebook_set_tab_vborder(notebook
, 0);
7587 gtk_notebook_set_scrollable(notebook
, TRUE
);
7588 notebook_tab_set_visibility(notebook
);
7589 gtk_notebook_set_show_border(notebook
, FALSE
);
7590 gtk_widget_set_can_focus(GTK_WIDGET(notebook
), FALSE
);
7592 abtn
= gtk_button_new();
7593 arrow
= gtk_arrow_new(GTK_ARROW_DOWN
, GTK_SHADOW_NONE
);
7594 gtk_widget_set_size_request(arrow
, -1, -1);
7595 gtk_container_add(GTK_CONTAINER(abtn
), arrow
);
7596 gtk_widget_set_size_request(abtn
, -1, 20);
7598 #if GTK_CHECK_VERSION(2, 20, 0)
7599 gtk_notebook_set_action_widget(notebook
, abtn
, GTK_PACK_END
);
7601 gtk_widget_set_size_request(GTK_WIDGET(notebook
), -1, -1);
7602 gtk_box_pack_start(GTK_BOX(vbox
), GTK_WIDGET(notebook
), TRUE
, TRUE
, 0);
7603 gtk_widget_set_size_request(vbox
, -1, -1);
7605 g_object_connect(G_OBJECT(notebook
),
7606 "signal::switch-page", G_CALLBACK(notebook_switchpage_cb
), NULL
,
7608 g_signal_connect(G_OBJECT(abtn
), "button_press_event",
7609 G_CALLBACK(arrow_cb
), NULL
);
7611 main_window
= create_window();
7612 gtk_container_add(GTK_CONTAINER(main_window
), vbox
);
7613 gtk_window_set_title(GTK_WINDOW(main_window
), XT_NAME
);
7616 for (i
= 0; i
< LENGTH(icons
); i
++) {
7617 snprintf(file
, sizeof file
, "%s/%s", resource_dir
, icons
[i
]);
7618 pb
= gdk_pixbuf_new_from_file(file
, NULL
);
7619 l
= g_list_append(l
, pb
);
7621 gtk_window_set_default_icon_list(l
);
7623 gtk_widget_show_all(abtn
);
7624 gtk_widget_show_all(main_window
);
7628 set_hook(void **hook
, char *name
)
7631 errx(1, "set_hook");
7633 if (*hook
== NULL
) {
7634 *hook
= dlsym(RTLD_NEXT
, name
);
7636 errx(1, "can't hook %s", name
);
7640 /* override libsoup soup_cookie_equal because it doesn't look at domain */
7642 soup_cookie_equal(SoupCookie
*cookie1
, SoupCookie
*cookie2
)
7644 g_return_val_if_fail(cookie1
, FALSE
);
7645 g_return_val_if_fail(cookie2
, FALSE
);
7647 return (!strcmp (cookie1
->name
, cookie2
->name
) &&
7648 !strcmp (cookie1
->value
, cookie2
->value
) &&
7649 !strcmp (cookie1
->path
, cookie2
->path
) &&
7650 !strcmp (cookie1
->domain
, cookie2
->domain
));
7654 transfer_cookies(void)
7657 SoupCookie
*sc
, *pc
;
7659 cf
= soup_cookie_jar_all_cookies(p_cookiejar
);
7661 for (;cf
; cf
= cf
->next
) {
7663 sc
= soup_cookie_copy(pc
);
7664 _soup_cookie_jar_add_cookie(s_cookiejar
, sc
);
7667 soup_cookies_free(cf
);
7671 soup_cookie_jar_delete_cookie(SoupCookieJar
*jar
, SoupCookie
*c
)
7676 print_cookie("soup_cookie_jar_delete_cookie", c
);
7678 if (cookies_enabled
== 0)
7681 if (jar
== NULL
|| c
== NULL
)
7684 /* find and remove from persistent jar */
7685 cf
= soup_cookie_jar_all_cookies(p_cookiejar
);
7687 for (;cf
; cf
= cf
->next
) {
7689 if (soup_cookie_equal(ci
, c
)) {
7690 _soup_cookie_jar_delete_cookie(p_cookiejar
, ci
);
7695 soup_cookies_free(cf
);
7697 /* delete from session jar */
7698 _soup_cookie_jar_delete_cookie(s_cookiejar
, c
);
7702 soup_cookie_jar_add_cookie(SoupCookieJar
*jar
, SoupCookie
*cookie
)
7704 struct domain
*d
= NULL
;
7708 DNPRINTF(XT_D_COOKIE
, "soup_cookie_jar_add_cookie: %p %p %p\n",
7709 jar
, p_cookiejar
, s_cookiejar
);
7711 if (cookies_enabled
== 0)
7714 /* see if we are up and running */
7715 if (p_cookiejar
== NULL
) {
7716 _soup_cookie_jar_add_cookie(jar
, cookie
);
7719 /* disallow p_cookiejar adds, shouldn't happen */
7720 if (jar
== p_cookiejar
)
7724 if (jar
== NULL
|| cookie
== NULL
)
7727 if (enable_cookie_whitelist
&&
7728 (d
= wl_find(cookie
->domain
, &c_wl
)) == NULL
) {
7730 DNPRINTF(XT_D_COOKIE
,
7731 "soup_cookie_jar_add_cookie: reject %s\n",
7733 if (save_rejected_cookies
) {
7734 if ((r_cookie_f
= fopen(rc_fname
, "a+")) == NULL
) {
7735 show_oops_s("can't open reject cookie file");
7738 fseek(r_cookie_f
, 0, SEEK_END
);
7739 fprintf(r_cookie_f
, "%s%s\t%s\t%s\t%s\t%lu\t%s\t%s\n",
7740 cookie
->http_only
? "#HttpOnly_" : "",
7742 *cookie
->domain
== '.' ? "TRUE" : "FALSE",
7744 cookie
->secure
? "TRUE" : "FALSE",
7746 (gulong
)soup_date_to_time_t(cookie
->expires
) :
7753 if (!allow_volatile_cookies
)
7757 if (cookie
->expires
== NULL
&& session_timeout
) {
7758 soup_cookie_set_expires(cookie
,
7759 soup_date_new_from_now(session_timeout
));
7760 print_cookie("modified add cookie", cookie
);
7763 /* see if we are white listed for persistence */
7764 if ((d
&& d
->handy
) || (enable_cookie_whitelist
== 0)) {
7765 /* add to persistent jar */
7766 c
= soup_cookie_copy(cookie
);
7767 print_cookie("soup_cookie_jar_add_cookie p_cookiejar", c
);
7768 _soup_cookie_jar_add_cookie(p_cookiejar
, c
);
7771 /* add to session jar */
7772 print_cookie("soup_cookie_jar_add_cookie s_cookiejar", cookie
);
7773 _soup_cookie_jar_add_cookie(s_cookiejar
, cookie
);
7779 char file
[PATH_MAX
];
7781 set_hook((void *)&_soup_cookie_jar_add_cookie
,
7782 "soup_cookie_jar_add_cookie");
7783 set_hook((void *)&_soup_cookie_jar_delete_cookie
,
7784 "soup_cookie_jar_delete_cookie");
7786 if (cookies_enabled
== 0)
7790 * the following code is intricate due to overriding several libsoup
7792 * do not alter order of these operations.
7795 /* rejected cookies */
7796 if (save_rejected_cookies
)
7797 snprintf(rc_fname
, sizeof file
, "%s/rejected.txt", work_dir
);
7799 /* persistent cookies */
7800 snprintf(file
, sizeof file
, "%s/cookies.txt", work_dir
);
7801 p_cookiejar
= soup_cookie_jar_text_new(file
, read_only_cookies
);
7803 /* session cookies */
7804 s_cookiejar
= soup_cookie_jar_new();
7805 g_object_set(G_OBJECT(s_cookiejar
), SOUP_COOKIE_JAR_ACCEPT_POLICY
,
7806 cookie_policy
, (void *)NULL
);
7809 soup_session_add_feature(session
, (SoupSessionFeature
*)s_cookiejar
);
7813 setup_proxy(char *uri
)
7816 g_object_set(session
, "proxy_uri", NULL
, (char *)NULL
);
7817 soup_uri_free(proxy_uri
);
7821 if (http_proxy
!= uri
) {
7828 http_proxy
= g_strdup(uri
);
7829 DNPRINTF(XT_D_CONFIG
, "setup_proxy: %s\n", uri
);
7830 proxy_uri
= soup_uri_new(http_proxy
);
7831 g_object_set(session
, "proxy-uri", proxy_uri
, (char *)NULL
);
7836 send_cmd_to_socket(char *cmd
)
7839 struct sockaddr_un sa
;
7841 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7842 warnx("%s: socket", __func__
);
7846 sa
.sun_family
= AF_UNIX
;
7847 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
7848 work_dir
, XT_SOCKET_FILE
);
7851 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7852 warnx("%s: connect", __func__
);
7856 if (send(s
, cmd
, strlen(cmd
) + 1, 0) == -1) {
7857 warnx("%s: send", __func__
);
7868 socket_watcher(gpointer data
, gint fd
, GdkInputCondition cond
)
7871 char str
[XT_MAX_URL_LENGTH
];
7872 socklen_t t
= sizeof(struct sockaddr_un
);
7873 struct sockaddr_un sa
;
7879 if ((s
= accept(fd
, (struct sockaddr
*)&sa
, &t
)) == -1) {
7884 if (getpeereid(s
, &uid
, &gid
) == -1) {
7888 if (uid
!= getuid() || gid
!= getgid()) {
7889 warnx("unauthorized user");
7895 warnx("not a valid user");
7899 n
= recv(s
, str
, sizeof(str
), 0);
7903 tt
= TAILQ_LAST(&tabs
, tab_list
);
7904 cmd_execute(tt
, str
);
7911 struct sockaddr_un sa
;
7913 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7914 warn("is_running: socket");
7918 sa
.sun_family
= AF_UNIX
;
7919 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
7920 work_dir
, XT_SOCKET_FILE
);
7923 /* connect to see if there is a listener */
7924 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1)
7925 rv
= 0; /* not running */
7927 rv
= 1; /* already running */
7938 struct sockaddr_un sa
;
7940 if ((s
= socket(AF_UNIX
, SOCK_STREAM
, 0)) == -1) {
7941 warn("build_socket: socket");
7945 sa
.sun_family
= AF_UNIX
;
7946 snprintf(sa
.sun_path
, sizeof(sa
.sun_path
), "%s/%s",
7947 work_dir
, XT_SOCKET_FILE
);
7950 /* connect to see if there is a listener */
7951 if (connect(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7952 /* no listener so we will */
7953 unlink(sa
.sun_path
);
7955 if (bind(s
, (struct sockaddr
*)&sa
, len
) == -1) {
7956 warn("build_socket: bind");
7960 if (listen(s
, 1) == -1) {
7961 warn("build_socket: listen");
7974 completion_select_cb(GtkEntryCompletion
*widget
, GtkTreeModel
*model
,
7975 GtkTreeIter
*iter
, struct tab
*t
)
7979 gtk_tree_model_get(model
, iter
, 0, &value
, -1);
7986 completion_add_uri(const gchar
*uri
)
7990 /* add uri to list_store */
7991 gtk_list_store_append(completion_model
, &iter
);
7992 gtk_list_store_set(completion_model
, &iter
, 0, uri
, -1);
7996 completion_match(GtkEntryCompletion
*completion
, const gchar
*key
,
7997 GtkTreeIter
*iter
, gpointer user_data
)
8000 gboolean match
= FALSE
;
8002 gtk_tree_model_get(GTK_TREE_MODEL(completion_model
), iter
, 0, &value
,
8008 match
= match_uri(value
, key
);
8015 completion_add(struct tab
*t
)
8017 /* enable completion for tab */
8018 t
->completion
= gtk_entry_completion_new();
8019 gtk_entry_completion_set_text_column(t
->completion
, 0);
8020 gtk_entry_set_completion(GTK_ENTRY(t
->uri_entry
), t
->completion
);
8021 gtk_entry_completion_set_model(t
->completion
,
8022 GTK_TREE_MODEL(completion_model
));
8023 gtk_entry_completion_set_match_func(t
->completion
, completion_match
,
8025 gtk_entry_completion_set_minimum_key_length(t
->completion
, 1);
8026 g_signal_connect(G_OBJECT (t
->completion
), "match-selected",
8027 G_CALLBACK(completion_select_cb
), t
);
8034 "%s [-nSTVt][-f file][-s session] url ...\n", __progname
);
8039 main(int argc
, char *argv
[])
8042 int c
, s
, optn
= 0, opte
= 0, focus
= 1;
8043 char conf
[PATH_MAX
] = { '\0' };
8044 char file
[PATH_MAX
];
8045 char *env_proxy
= NULL
;
8048 struct sigaction sact
;
8049 gchar
*priority
= g_strdup("NORMAL");
8053 strlcpy(named_session
, XT_SAVED_TABS_FILE
, sizeof named_session
);
8055 while ((c
= getopt(argc
, argv
, "STVf:s:tne")) != -1) {
8064 errx(0 , "Version: %s", version
);
8067 strlcpy(conf
, optarg
, sizeof(conf
));
8070 strlcpy(named_session
, optarg
, sizeof(named_session
));
8091 RB_INIT(&downloads
);
8095 TAILQ_INIT(&aliases
);
8101 gnutls_global_init();
8103 /* generate session keys for xtp pages */
8104 generate_xtp_session_key(&dl_session_key
);
8105 generate_xtp_session_key(&hl_session_key
);
8106 generate_xtp_session_key(&cl_session_key
);
8107 generate_xtp_session_key(&fl_session_key
);
8110 gtk_init(&argc
, &argv
);
8111 if (!g_thread_supported())
8112 g_thread_init(NULL
);
8115 bzero(&sact
, sizeof(sact
));
8116 sigemptyset(&sact
.sa_mask
);
8117 sact
.sa_handler
= sigchild
;
8118 sact
.sa_flags
= SA_NOCLDSTOP
;
8119 sigaction(SIGCHLD
, &sact
, NULL
);
8121 /* set download dir */
8122 pwd
= getpwuid(getuid());
8124 errx(1, "invalid user %d", getuid());
8125 strlcpy(download_dir
, pwd
->pw_dir
, sizeof download_dir
);
8127 /* set default string settings */
8128 home
= g_strdup("http://www.peereboom.us");
8129 search_string
= g_strdup("https://ssl.scroogle.org/cgi-bin/nbbwssl.cgi?Gw=%s");
8130 resource_dir
= g_strdup("/usr/local/share/xxxterm/");
8131 strlcpy(runtime_settings
,"runtime", sizeof runtime_settings
);
8133 /* read config file */
8134 if (strlen(conf
) == 0)
8135 snprintf(conf
, sizeof conf
, "%s/.%s",
8136 pwd
->pw_dir
, XT_CONF_FILE
);
8137 config_parse(conf
, 0);
8139 /* working directory */
8140 if (strlen(work_dir
) == 0)
8141 snprintf(work_dir
, sizeof work_dir
, "%s/%s",
8142 pwd
->pw_dir
, XT_DIR
);
8143 if (stat(work_dir
, &sb
)) {
8144 if (mkdir(work_dir
, S_IRWXU
) == -1)
8145 err(1, "mkdir work_dir");
8146 if (stat(work_dir
, &sb
))
8147 err(1, "stat work_dir");
8149 if (S_ISDIR(sb
.st_mode
) == 0)
8150 errx(1, "%s not a dir", work_dir
);
8151 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
8152 warnx("fixing invalid permissions on %s", work_dir
);
8153 if (chmod(work_dir
, S_IRWXU
) == -1)
8157 /* icon cache dir */
8158 snprintf(cache_dir
, sizeof cache_dir
, "%s/%s", work_dir
, XT_CACHE_DIR
);
8159 if (stat(cache_dir
, &sb
)) {
8160 if (mkdir(cache_dir
, S_IRWXU
) == -1)
8161 err(1, "mkdir cache_dir");
8162 if (stat(cache_dir
, &sb
))
8163 err(1, "stat cache_dir");
8165 if (S_ISDIR(sb
.st_mode
) == 0)
8166 errx(1, "%s not a dir", cache_dir
);
8167 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
8168 warnx("fixing invalid permissions on %s", cache_dir
);
8169 if (chmod(cache_dir
, S_IRWXU
) == -1)
8174 snprintf(certs_dir
, sizeof certs_dir
, "%s/%s", work_dir
, XT_CERT_DIR
);
8175 if (stat(certs_dir
, &sb
)) {
8176 if (mkdir(certs_dir
, S_IRWXU
) == -1)
8177 err(1, "mkdir certs_dir");
8178 if (stat(certs_dir
, &sb
))
8179 err(1, "stat certs_dir");
8181 if (S_ISDIR(sb
.st_mode
) == 0)
8182 errx(1, "%s not a dir", certs_dir
);
8183 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
8184 warnx("fixing invalid permissions on %s", certs_dir
);
8185 if (chmod(certs_dir
, S_IRWXU
) == -1)
8190 snprintf(sessions_dir
, sizeof sessions_dir
, "%s/%s",
8191 work_dir
, XT_SESSIONS_DIR
);
8192 if (stat(sessions_dir
, &sb
)) {
8193 if (mkdir(sessions_dir
, S_IRWXU
) == -1)
8194 err(1, "mkdir sessions_dir");
8195 if (stat(sessions_dir
, &sb
))
8196 err(1, "stat sessions_dir");
8198 if (S_ISDIR(sb
.st_mode
) == 0)
8199 errx(1, "%s not a dir", sessions_dir
);
8200 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
8201 warnx("fixing invalid permissions on %s", sessions_dir
);
8202 if (chmod(sessions_dir
, S_IRWXU
) == -1)
8205 /* runtime settings that can override config file */
8206 if (runtime_settings
[0] != '\0')
8207 config_parse(runtime_settings
, 1);
8210 if (!strcmp(download_dir
, pwd
->pw_dir
))
8211 strlcat(download_dir
, "/downloads", sizeof download_dir
);
8212 if (stat(download_dir
, &sb
)) {
8213 if (mkdir(download_dir
, S_IRWXU
) == -1)
8214 err(1, "mkdir download_dir");
8215 if (stat(download_dir
, &sb
))
8216 err(1, "stat download_dir");
8218 if (S_ISDIR(sb
.st_mode
) == 0)
8219 errx(1, "%s not a dir", download_dir
);
8220 if (((sb
.st_mode
& (S_IRWXU
| S_IRWXG
| S_IRWXO
))) != S_IRWXU
) {
8221 warnx("fixing invalid permissions on %s", download_dir
);
8222 if (chmod(download_dir
, S_IRWXU
) == -1)
8226 /* favorites file */
8227 snprintf(file
, sizeof file
, "%s/%s", work_dir
, XT_FAVS_FILE
);
8228 if (stat(file
, &sb
)) {
8229 warnx("favorites file doesn't exist, creating it");
8230 if ((f
= fopen(file
, "w")) == NULL
)
8231 err(1, "favorites");
8236 session
= webkit_get_default_session();
8237 /* XXX ssl-priority property not quite available yet */
8238 if (is_g_object_setting(G_OBJECT(session
), "ssl-priority"))
8239 g_object_set(G_OBJECT(session
), "ssl-priority", priority
,
8242 warnx("session does not have \"ssl-priority\" property");
8247 if (stat(ssl_ca_file
, &sb
)) {
8248 warn("no CA file: %s", ssl_ca_file
);
8249 g_free(ssl_ca_file
);
8252 g_object_set(session
,
8253 SOUP_SESSION_SSL_CA_FILE
, ssl_ca_file
,
8254 SOUP_SESSION_SSL_STRICT
, ssl_strict_certs
,
8259 env_proxy
= getenv("http_proxy");
8261 setup_proxy(env_proxy
);
8263 setup_proxy(http_proxy
);
8266 send_cmd_to_socket(argv
[0]);
8270 /* set some connection parameters */
8271 g_object_set(session
, "max-conns", max_connections
, (char *)NULL
);
8272 g_object_set(session
, "max-conns-per-host", max_host_connections
,
8275 /* see if there is already an xxxterm running */
8276 if (single_instance
&& is_running()) {
8278 warnx("already running");
8284 cmd
= g_strdup_printf("%s %s", "tabnew", argv
[0]);
8285 send_cmd_to_socket(cmd
);
8295 /* uri completion */
8296 completion_model
= gtk_list_store_new(1, G_TYPE_STRING
);
8301 if (save_global_history
)
8302 restore_global_history();
8304 if (!strcmp(named_session
, XT_SAVED_TABS_FILE
))
8305 restore_saved_tabs();
8307 a
.s
= named_session
;
8308 a
.i
= XT_SES_DONOTHING
;
8309 open_tabs(NULL
, &a
);
8313 create_new_tab(argv
[0], NULL
, focus
);
8320 if (TAILQ_EMPTY(&tabs
))
8321 create_new_tab(home
, NULL
, 1);
8324 if ((s
= build_socket()) != -1)
8325 gdk_input_add(s
, GDK_INPUT_READ
, socket_watcher
, NULL
);
8329 gnutls_global_deinit();