netsurf/desktop/options.c

688 lines
18 KiB
C
Raw Normal View History

/*
* Copyright 2003 Phil Mellor <monkeyson@users.sourceforge.net>
* Copyright 2003 John M Bell <jmb202@ecs.soton.ac.uk>
* Copyright 2004 James Bursa <bursa@users.sourceforge.net>
* Copyright 2005 Richard Wilson <info@tinct.net>
*
* This file is part of NetSurf, http://www.netsurf-browser.org/
*
* NetSurf is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* NetSurf is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** \file
* Option reading and saving (implementation).
*
* Options are stored in the format key:value, one per line. For bool options,
* value is "0" or "1".
*/
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <libxml/HTMLparser.h>
#include <libxml/HTMLtree.h>
#include "content/urldb.h"
#include "css/css.h"
#include "desktop/options.h"
#include "desktop/tree.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/utils.h"
#if defined(riscos)
#include "riscos/options.h"
#elif defined(nsgtk)
#include "gtk/options.h"
#else
#define EXTRA_OPTION_DEFINE
#define EXTRA_OPTION_TABLE
#endif
/** An HTTP proxy should be used. */
bool option_http_proxy = false;
/** Hostname of proxy. */
char *option_http_proxy_host = 0;
/** Proxy port. */
int option_http_proxy_port = 8080;
/** Proxy authentication method. */
int option_http_proxy_auth = OPTION_HTTP_PROXY_AUTH_NONE;
/** Proxy authentication user name */
char *option_http_proxy_auth_user = 0;
/** Proxy authentication password */
char *option_http_proxy_auth_pass = 0;
/** Default font size / 0.1pt. */
int option_font_size = 128;
/** Minimum font size. */
int option_font_min_size = 85;
/** Default sans serif font */
char *option_font_sans;
/** Default serif font */
char *option_font_serif;
/** Default monospace font */
char *option_font_mono;
/** Default cursive font */
char *option_font_cursive;
/** Default fantasy font */
char *option_font_fantasy;
/** Accept-Language header. */
char *option_accept_language = 0;
/** Accept-Charset header. */
char *option_accept_charset = 0;
/** Preferred maximum size of memory cache / bytes. */
int option_memory_cache_size = 2 * 1024 * 1024;
/** Preferred expiry age of disc cache / days. */
int option_disc_cache_age = 28;
/** Whether to block advertisements */
bool option_block_ads = false;
/** Minimum GIF animation delay */
int option_minimum_gif_delay = 10;
/** Whether to send the referer HTTP header */
bool option_send_referer = true;
/** Whether to animate images */
bool option_animate_images = true;
/** How many days to retain URL data for */
int option_expire_url = 28;
/** Default font family */
int option_font_default = CSS_FONT_FAMILY_SANS_SERIF;
/** ca-bundle location */
char *option_ca_bundle = 0;
/** ca-path location */
char *option_ca_path = 0;
/** Cookie file location */
char *option_cookie_file = 0;
/** Cookie jar loaction */
char *option_cookie_jar = 0;
/** Home page location */
char *option_homepage_url = 0;
/** URL completion in url bar */
bool option_url_suggestion = true;
/** default x position of new windows */
int option_window_x = 0;
/** default y position of new windows */
int option_window_y = 0;
/** default width of new windows */
int option_window_width = 0;
/** default height of new windows */
int option_window_height = 0;
/** width of screen when above options were saved */
int option_window_screen_width = 0;
/** height of screen when above options were saved */
int option_window_screen_height = 0;
/** default size of status bar vs. h scroll bar */
#ifdef nsgtk
int option_toolbar_status_width = 400;
#else
int option_toolbar_status_width = 6667;
#endif
/** default window scale */
int option_scale = 100;
/* Whether to reflow web pages while objects are fetching */
bool option_incremental_reflow = true;
/* Minimum time between HTML reflows while objects are fetching */
#ifdef riscos
unsigned int option_min_reflow_period = 100; /* time in cs */
#else
unsigned int option_min_reflow_period = 25; /* time in cs */
#endif
/* Fetcher configuration */
/** Maximum simultaneous active fetchers */
int option_max_fetchers = 24;
/** Maximum simultaneous active fetchers per host.
* (<=option_max_fetchers else it makes no sense)
* Note that rfc2616 section 8.1.4 says that there should be no more than
* two keepalive connections per host. None of the main browsers follow this
* as it slows page fetches down considerably.
* See https://bugzilla.mozilla.org/show_bug.cgi?id=423377#c4
*/
int option_max_fetchers_per_host = 5;
/** Maximum number of inactive fetchers cached.
* The total number of handles netsurf will therefore have open
* is this plus option_max_fetchers.
*/
int option_max_cached_fetch_handles = 6;
/** Suppress debug output from cURL. */
bool option_suppress_curl_debug = true;
/** Whether to allow target="_blank" */
bool option_target_blank = true;
EXTRA_OPTION_DEFINE
struct {
const char *key;
enum { OPTION_BOOL, OPTION_INTEGER, OPTION_STRING } type;
void *p;
} option_table[] = {
{ "http_proxy", OPTION_BOOL, &option_http_proxy },
{ "http_proxy_host", OPTION_STRING, &option_http_proxy_host },
{ "http_proxy_port", OPTION_INTEGER, &option_http_proxy_port },
Merged revisions 4114-4265,4267-4272,4275-4285,4287-4325 via svnmerge from svn://source.netsurf-browser.org/branches/mikeL/netsurf ........ r4116 | mikeL | 2008-05-01 22:15:12 +0100 (Thu, 01 May 2008) | 2 lines Redesigned right-click menu: added back, forward, and reload, previously default menu items are now hidden unless view->toolbars->menu bar is disabled ........ r4117 | mikeL | 2008-05-02 03:54:10 +0100 (Fri, 02 May 2008) | 2 lines Added a Current Page button to Preferences next to the url entry for setting the home page ........ r4118 | mikeL | 2008-05-02 04:27:37 +0100 (Fri, 02 May 2008) | 1 line Cleanup ........ r4119 | mikeL | 2008-05-02 21:09:44 +0100 (Fri, 02 May 2008) | 1 line Preferences window is now initialized the first time edit->preferences is clicked instead of during nsgtk initialization. Expedites start-up time because preferences is a non-essential dialog. ........ r4123 | mikeL | 2008-05-04 15:43:20 +0100 (Sun, 04 May 2008) | 1 line Converted Preferences window to a dialog. Fixed spacing and naming in Preferences dialog. Split Preferences section of the Glade file into its own gtk_options.glade file. Moved all Preferences related files to gtk/dialogs. ........ r4127 | mikeL | 2008-05-05 20:45:44 +0100 (Mon, 05 May 2008) | 1 line Fixed a glaring error in directory structure. All glade files are now stored in the res directory. ........ r4128 | mikeL | 2008-05-05 20:52:08 +0100 (Mon, 05 May 2008) | 1 line Added directory 'dialogs' to revision control ........ r4129 | mikeL | 2008-05-05 20:58:53 +0100 (Mon, 05 May 2008) | 1 line Cleaned up gtk folder by removing gtk_options.c and gtk_options.h (Moved to dialogs folder) ........ r4130 | mikeL | 2008-05-05 23:42:15 +0100 (Mon, 05 May 2008) | 1 line Huzzah\! Preferences dialog is now instant apply\! ........ r4131 | mikeL | 2008-05-06 20:43:26 +0100 (Tue, 06 May 2008) | 1 line nsgtk_reflow_all_windows() is now called whenever an option that requires redrawing of the current page is changed. Preferences dialog is now *completely* instant apply ........ r4132 | mikeL | 2008-05-06 20:55:05 +0100 (Tue, 06 May 2008) | 1 line Renamed macros to better reflect their purpose ........ r4133 | mikeL | 2008-05-06 21:30:44 +0100 (Tue, 06 May 2008) | 1 line Added gtk/res/options.glade to svn control ........ r4134 | mikeL | 2008-05-06 21:39:12 +0100 (Tue, 06 May 2008) | 1 line Preferences window is now resizable ........ r4135 | mikeL | 2008-05-06 21:42:00 +0100 (Tue, 06 May 2008) | 1 line Fixed reload menu item in popup menu ........ r4136 | mikeL | 2008-05-07 00:24:35 +0100 (Wed, 07 May 2008) | 1 line Proxy configurations are now insensitive if proxy type is set to no proxy. Changing the home page with the Current Page button is now saved. Moved Current Page button in Preferences window and added Default Page. ........ r4137 | mikeL | 2008-05-07 00:41:29 +0100 (Wed, 07 May 2008) | 1 line Solved a problem with all options being saved prematurely on initialization ........ r4145 | mikeL | 2008-05-11 18:07:06 +0100 (Sun, 11 May 2008) | 1 line Optimized prefences window signal handling, entry signals are now caught on focus-out-event and checked for changes ........ r4146 | mikeL | 2008-05-11 18:16:04 +0100 (Sun, 11 May 2008) | 1 line Fixed regression where 'Set Current Page' button stopped working ........ r4147 | mikeL | 2008-05-11 18:32:10 +0100 (Sun, 11 May 2008) | 1 line Added 'Default Page' button functionality ........ r4148 | mikeL | 2008-05-11 18:33:03 +0100 (Sun, 11 May 2008) | 1 line Updated TODO list ........ r4149 | mikeL | 2008-05-12 01:04:00 +0100 (Mon, 12 May 2008) | 1 line Added 'Hide Advertisement' functionality ........ r4150 | mikeL | 2008-05-12 01:30:40 +0100 (Mon, 12 May 2008) | 1 line Re-activated the option 'Disable Animation' and fixed bug where it would actually enable animation ........ r4151 | mikeL | 2008-05-12 01:42:43 +0100 (Mon, 12 May 2008) | 1 line Added 'Send site referral information' functionality ........ r4152 | mikeL | 2008-05-12 02:01:00 +0100 (Mon, 12 May 2008) | 1 line Added 'Disc cache age' functionality ........ r4154 | mikeL | 2008-05-13 20:35:29 +0100 (Tue, 13 May 2008) | 1 line Fixed regression where preferences were not written to file in some cases and preferences dialog could not be reopened. ........ r4158 | mikeL | 2008-05-14 21:57:50 +0100 (Wed, 14 May 2008) | 1 line Added 'preview' button to fonts tab of preferences dialog. nsgtk_reflow_all_windows is now called only when that button is clicked. ........ r4162 | mikeL | 2008-05-15 20:09:30 +0100 (Thu, 15 May 2008) | 1 line Possible fix to segfault due to invalid test ........ r4163 | mikeL | 2008-05-15 20:25:28 +0100 (Thu, 15 May 2008) | 1 line Fixed typo that caused preferences dialog to be unopenable ........ r4164 | mikeL | 2008-05-15 20:57:49 +0100 (Thu, 15 May 2008) | 1 line Added initialization statement for preferences_dialog to make sure that ........ r4169 | mikeL | 2008-05-17 00:30:54 +0100 (Sat, 17 May 2008) | 1 line Added a 'parent_window' initialization parameter to the preferences dialog so that it may center itself on parent. This also optimizes the closing code as destroy is now called on the dialog when the main window is closed ........ r4174 | mikeL | 2008-05-18 15:46:43 +0100 (Sun, 18 May 2008) | 1 line Animation speed option is now insensitive when 'Disable animations' is true ........ r4175 | mikeL | 2008-05-18 15:50:06 +0100 (Sun, 18 May 2008) | 1 line Added tooltip to preview button ........ r4176 | mikeL | 2008-05-18 16:04:05 +0100 (Sun, 18 May 2008) | 1 line Animation speed is now always sensitive ........ r4177 | mikeL | 2008-05-19 02:32:21 +0100 (Mon, 19 May 2008) | 1 line Redesigned about dialog as a GtkAboutDialog and removed the respective section from netsurf.glade (May need string revision) ........ r4178 | mikeL | 2008-05-19 02:36:15 +0100 (Mon, 19 May 2008) | 1 line Fixed compile warning relating to improper cast of netsurf_version ........ r4180 | mikeL | 2008-05-19 21:42:04 +0100 (Mon, 19 May 2008) | 1 line Updated credits ........ r4181 | mikeL | 2008-05-19 21:43:16 +0100 (Mon, 19 May 2008) | 1 line launch_url is no longer static ........ r4182 | mikeL | 2008-05-19 21:50:03 +0100 (Mon, 19 May 2008) | 1 line netsurf_version is now const in function which addresses jmb's concern ........ r4183 | mikeL | 2008-05-19 21:59:55 +0100 (Mon, 19 May 2008) | 1 line Updated header ........ r4184 | mikeL | 2008-05-19 22:03:28 +0100 (Mon, 19 May 2008) | 1 line local variables are now static ........ r4186 | mikeL | 2008-05-21 22:03:43 +0100 (Wed, 21 May 2008) | 1 line Removed 'Main Development Team' from the beginning of all credits ........ r4219 | mikeL | 2008-05-28 18:17:12 +0100 (Wed, 28 May 2008) | 1 line Removed wndOpenFile from glade file. ........ r4236 | mikeL | 2008-05-31 23:25:32 +0100 (Sat, 31 May 2008) | 1 line Fixed proxy options for both riscos and gtk versions thanks to a tip from Leon Stringer ........ r4275 | mikeL | 2008-06-06 17:16:29 +0100 (Fri, 06 Jun 2008) | 1 line Added support for dragging, clicks are now emited on button release ........ r4276 | mikeL | 2008-06-06 17:18:18 +0100 (Fri, 06 Jun 2008) | 1 line gtk_window.c now sets the current_redraw_browser. Text selection now highlights properly ........ r4277 | mikeL | 2008-06-06 17:47:35 +0100 (Fri, 06 Jun 2008) | 1 line Cleaned up the button detection code ........ r4278 | mikeL | 2008-06-06 17:58:51 +0100 (Fri, 06 Jun 2008) | 1 line Removed leftover variables ........ r4279 | mikeL | 2008-06-06 18:13:58 +0100 (Fri, 06 Jun 2008) | 1 line Enabled 'Select All' ........ r4280 | mikeL | 2008-06-06 18:31:56 +0100 (Fri, 06 Jun 2008) | 1 line Fixed bug where mouse state would always remain as PRESS even if dragging ........ r4281 | mikeL | 2008-06-06 18:45:16 +0100 (Fri, 06 Jun 2008) | 1 line Fixed regression where mouse state was cleared improperly ........ r4287 | mikeL | 2008-06-07 00:21:32 +0100 (Sat, 07 Jun 2008) | 1 line Mouse code can now handle modifiers. Fixed bug where end of drag was not detected until mouse moved after being released. Improved mouse handling. ........ r4294 | mikeL | 2008-06-07 03:21:03 +0100 (Sat, 07 Jun 2008) | 1 line Added ability to handle modifiers pressed during a drag event. Mouse movement handling now uses switch statements (Thanks tlsa) ........ r4295 | mikeL | 2008-06-07 03:40:11 +0100 (Sat, 07 Jun 2008) | 1 line Fixed bug where modifier keys were being detected incorrectly ........ r4296 | mikeL | 2008-06-07 03:42:31 +0100 (Sat, 07 Jun 2008) | 1 line Reversed accidental file modification ........ r4299 | mikeL | 2008-06-07 21:32:15 +0100 (Sat, 07 Jun 2008) | 1 line Fixed inclusion of gtk_about source file instead of header ........ r4300 | mikeL | 2008-06-07 22:27:39 +0100 (Sat, 07 Jun 2008) | 1 line Fixed problem with about.h not linking properly. ........ r4301 | mikeL | 2008-06-07 22:37:28 +0100 (Sat, 07 Jun 2008) | 1 line Moved definitions of gui_window and browser_mouse to gtk_window.h. Moved all selection related functions to gtk_selection.c. Implemented copy functionality. ........ r4302 | mikeL | 2008-06-07 22:48:18 +0100 (Sat, 07 Jun 2008) | 1 line Implemented paste functionality ........ r4303 | mikeL | 2008-06-07 22:48:40 +0100 (Sat, 07 Jun 2008) | 1 line Removed old test case ........ r4304 | mikeL | 2008-06-07 22:53:56 +0100 (Sat, 07 Jun 2008) | 1 line Fixed bug where click would be sent at the end of a button 2 drag (Thanks tlsa) ........ r4305 | mikeL | 2008-06-07 23:48:01 +0100 (Sat, 07 Jun 2008) | 1 line Prevented gui_copy_to_clipboard from overwriting clipboard with a NULL string. ........ r4306 | mikeL | 2008-06-08 00:00:55 +0100 (Sun, 08 Jun 2008) | 1 line Fixed regression where the state of the modifier keys would alternate as the mouse moved. Fixed bug that prevented drags from being registered if a modifier key was pressed ........ r4308 | mikeL | 2008-06-08 00:53:26 +0100 (Sun, 08 Jun 2008) | 1 line Changed 'Select All' menu item to stock ........ r4314 | mikeL | 2008-06-09 19:09:23 +0100 (Mon, 09 Jun 2008) | 1 line Added function nsgtk_scaffolding_set_sensitive ........ r4315 | mikeL | 2008-06-09 19:14:14 +0100 (Mon, 09 Jun 2008) | 1 line Added function nsgtk_scaffolding_set_sensitive ........ r4316 | mikeL | 2008-06-09 19:20:16 +0100 (Mon, 09 Jun 2008) | 1 line Removed option 'Use Cairo for anti-aliased rendering' ........ r4317 | mikeL | 2008-06-09 20:10:55 +0100 (Mon, 09 Jun 2008) | 1 line Fixed bug where text would be selected in all windows instead of only the active one ........ r4318 | mikeL | 2008-06-09 20:29:42 +0100 (Mon, 09 Jun 2008) | 1 line (Drastically) Improved redraw handling. (Thanks jmb) ........ r4319 | mikeL | 2008-06-09 21:10:17 +0100 (Mon, 09 Jun 2008) | 1 line Removed old test case ........ r4320 | mikeL | 2008-06-10 07:27:32 +0100 (Tue, 10 Jun 2008) | 1 line Implemented (almost) the rest of the clipboard functionality. Clipboard functions now handle the url bar correctly. Clipboard menu items are now appropriately sensitive. ........ r4321 | mikeL | 2008-06-10 19:08:05 +0100 (Tue, 10 Jun 2008) | 1 line Added clipboard functions to contextual right click menu ........ r4323 | mikeL | 2008-06-10 23:57:43 +0100 (Tue, 10 Jun 2008) | 1 line Fixed regression where modifiers key states would be cleared on button release. Modifier keys are now set only on button press and can only be removed during motion or upon release. Fixed spacing (thanks tlsa) ........ svn path=/trunk/netsurf/; revision=4326
2008-06-11 14:57:44 +04:00
{ "http_proxy_auth", OPTION_INTEGER, &option_http_proxy_auth },
{ "http_proxy_auth_user", OPTION_STRING, &option_http_proxy_auth_user },
{ "http_proxy_auth_pass", OPTION_STRING, &option_http_proxy_auth_pass },
{ "font_size", OPTION_INTEGER, &option_font_size },
{ "font_min_size", OPTION_INTEGER, &option_font_min_size },
{ "font_sans", OPTION_STRING, &option_font_sans },
{ "font_serif", OPTION_STRING, &option_font_serif },
{ "font_mono", OPTION_STRING, &option_font_mono },
{ "font_cursive", OPTION_STRING, &option_font_cursive },
{ "font_fantasy", OPTION_STRING, &option_font_fantasy },
{ "accept_language", OPTION_STRING, &option_accept_language },
{ "accept_charset", OPTION_STRING, &option_accept_charset },
{ "memory_cache_size", OPTION_INTEGER, &option_memory_cache_size },
{ "disc_cache_age", OPTION_INTEGER, &option_disc_cache_age },
{ "block_advertisements", OPTION_BOOL, &option_block_ads },
{ "minimum_gif_delay", OPTION_INTEGER, &option_minimum_gif_delay },
{ "send_referer", OPTION_BOOL, &option_send_referer },
{ "animate_images", OPTION_BOOL, &option_animate_images },
{ "expire_url", OPTION_INTEGER, &option_expire_url },
{ "font_default", OPTION_INTEGER, &option_font_default },
{ "ca_bundle", OPTION_STRING, &option_ca_bundle },
{ "ca_path", OPTION_STRING, &option_ca_path },
{ "cookie_file", OPTION_STRING, &option_cookie_file },
{ "cookie_jar", OPTION_STRING, &option_cookie_jar },
{ "homepage_url", OPTION_STRING, &option_homepage_url },
{ "url_suggestion", OPTION_BOOL, &option_url_suggestion },
{ "window_x", OPTION_INTEGER, &option_window_x },
{ "window_y", OPTION_INTEGER, &option_window_y },
{ "window_width", OPTION_INTEGER, &option_window_width },
{ "window_height", OPTION_INTEGER, &option_window_height },
{ "window_screen_width", OPTION_INTEGER, &option_window_screen_width },
{ "window_screen_height",OPTION_INTEGER, &option_window_screen_height },
{ "toolbar_status_size", OPTION_INTEGER, &option_toolbar_status_width },
{ "scale", OPTION_INTEGER, &option_scale },
{ "incremental_reflow", OPTION_BOOL, &option_incremental_reflow },
{ "min_reflow_period", OPTION_INTEGER, &option_min_reflow_period },
/* Fetcher options */
{ "max_fetchers", OPTION_INTEGER, &option_max_fetchers },
{ "max_fetchers_per_host",
OPTION_INTEGER, &option_max_fetchers_per_host },
{ "max_cached_fetch_handles",
OPTION_INTEGER, &option_max_cached_fetch_handles },
{ "suppress_curl_debug", OPTION_BOOL, &option_suppress_curl_debug },
{ "target_blank",
OPTION_BOOL, &option_target_blank },
EXTRA_OPTION_TABLE
};
#define option_table_entries (sizeof option_table / sizeof option_table[0])
static void options_load_tree_directory(xmlNode *ul, struct node *directory);
static void options_load_tree_entry(xmlNode *li, struct node *directory);
xmlNode *options_find_tree_element(xmlNode *node, const char *name);
bool options_save_tree_directory(struct node *directory, xmlNode *node);
bool options_save_tree_entry(struct node *entry, xmlNode *node);
/**
* Read options from a file.
*
* \param path name of file to read options from
*
* Option variables corresponding to lines in the file are updated. Missing
* options are unchanged. If the file fails to open, options are unchanged.
*/
void options_read(const char *path)
{
char s[100];
FILE *fp;
fp = fopen(path, "r");
if (!fp) {
LOG(("failed to open file '%s'", path));
return;
}
while (fgets(s, 100, fp)) {
char *colon, *value;
unsigned int i;
if (s[0] == 0 || s[0] == '#')
continue;
colon = strchr(s, ':');
if (colon == 0)
continue;
s[strlen(s) - 1] = 0; /* remove \n at end */
*colon = 0; /* terminate key */
value = colon + 1;
for (i = 0; i != option_table_entries; i++) {
if (strcasecmp(s, option_table[i].key) != 0)
continue;
switch (option_table[i].type) {
case OPTION_BOOL:
*((bool *) option_table[i].p) =
value[0] == '1';
break;
case OPTION_INTEGER:
*((int *) option_table[i].p) =
atoi(value);
break;
case OPTION_STRING:
free(*((char **) option_table[i].p));
*((char **) option_table[i].p) =
strdup(value);
break;
}
break;
}
}
fclose(fp);
if (option_font_size < 50)
option_font_size = 50;
if (1000 < option_font_size)
option_font_size = 1000;
if (option_font_min_size < 10)
option_font_min_size = 10;
if (500 < option_font_min_size)
option_font_min_size = 500;
if (option_memory_cache_size < 0)
option_memory_cache_size = 0;
}
/**
* Save options to a file.
*
* \param path name of file to write options to
*
* Errors are ignored.
*/
void options_write(const char *path)
{
unsigned int i;
FILE *fp;
fp = fopen(path, "w");
if (!fp) {
LOG(("failed to open file '%s' for writing", path));
return;
}
for (i = 0; i != option_table_entries; i++) {
fprintf(fp, "%s:", option_table[i].key);
switch (option_table[i].type) {
case OPTION_BOOL:
fprintf(fp, "%c", *((bool *) option_table[i].p) ?
'1' : '0');
break;
case OPTION_INTEGER:
fprintf(fp, "%i", *((int *) option_table[i].p));
break;
case OPTION_STRING:
if (*((char **) option_table[i].p))
fprintf(fp, "%s", *((char **) option_table[i].p));
break;
}
fprintf(fp, "\n");
}
fclose(fp);
}
/**
* Dump user options to stderr
*/
void options_dump(void)
{
unsigned int i;
for (i = 0; i != option_table_entries; i++) {
fprintf(stderr, "%s:", option_table[i].key);
switch (option_table[i].type) {
case OPTION_BOOL:
fprintf(stderr, "%c",
*((bool *) option_table[i].p) ?
'1' : '0');
break;
case OPTION_INTEGER:
fprintf(stderr, "%i",
*((int *) option_table[i].p));
break;
case OPTION_STRING:
if (*((char **) option_table[i].p))
fprintf(stderr, "%s",
*((char **) option_table[i].p));
break;
}
fprintf(stderr, "\n");
}
}
/**
* Loads a hotlist as a tree from a specified file.
*
* \param filename name of file to read
* \return the hotlist file represented as a tree, or NULL on failure
*/
struct tree *options_load_tree(const char *filename) {
xmlDoc *doc;
xmlNode *html, *body, *ul;
struct tree *tree;
doc = htmlParseFile(filename, "iso-8859-1");
if (!doc) {
warn_user("HotlistLoadError", messages_get("ParsingFail"));
return NULL;
}
html = options_find_tree_element((xmlNode *) doc, "html");
body = options_find_tree_element(html, "body");
ul = options_find_tree_element(body, "ul");
if (!ul) {
xmlFreeDoc(doc);
warn_user("HotlistLoadError",
"(<html>...<body>...<ul> not found.)");
return NULL;
}
tree = calloc(sizeof(struct tree), 1);
if (!tree) {
xmlFreeDoc(doc);
warn_user("NoMemory", 0);
return NULL;
}
tree->root = tree_create_folder_node(NULL, "Root");
if (!tree->root) return NULL;
options_load_tree_directory(ul, tree->root);
tree->root->expanded = true;
tree_initialise(tree);
xmlFreeDoc(doc);
return tree;
}
/**
* Parse a directory represented as a ul.
*
* \param ul xmlNode for parsed ul
* \param directory directory to add this directory to
*/
void options_load_tree_directory(xmlNode *ul, struct node *directory) {
char *title;
struct node *dir;
xmlNode *n;
assert(ul);
assert(directory);
for (n = ul->children; n; n = n->next) {
/* The ul may contain entries as a li, or directories as
* an h4 followed by a ul. Non-element nodes may be present
* (eg. text, comments), and are ignored. */
if (n->type != XML_ELEMENT_NODE)
continue;
if (strcmp((const char *) n->name, "li") == 0) {
/* entry */
options_load_tree_entry(n, directory);
} else if (strcmp((const char *) n->name, "h4") == 0) {
/* directory */
title = (char *) xmlNodeGetContent(n);
if (!title) {
warn_user("HotlistLoadError", "(Empty <h4> "
"or memory exhausted.)");
return;
}
for (n = n->next;
n && n->type != XML_ELEMENT_NODE;
n = n->next)
;
if (!n || strcmp((const char *) n->name, "ul") != 0) {
/* next element isn't expected ul */
free(title);
warn_user("HotlistLoadError", "(Expected "
"<ul> not present.)");
return;
}
dir = tree_create_folder_node(directory, title);
if (!dir)
return;
options_load_tree_directory(n, dir);
}
}
}
/**
* Parse an entry represented as a li.
*
* \param li xmlNode for parsed li
* \param directory directory to add this entry to
*/
void options_load_tree_entry(xmlNode *li, struct node *directory) {
char *url = 0;
char *title = 0;
struct node *entry;
xmlNode *n;
const struct url_data *data;
for (n = li->children; n; n = n->next) {
/* The li must contain an "a" element */
if (n->type == XML_ELEMENT_NODE &&
strcmp((const char *) n->name, "a") == 0) {
url = (char *) xmlGetProp(n, (const xmlChar *) "href");
title = (char *) xmlNodeGetContent(n);
}
}
if (!url || !title) {
warn_user("HotlistLoadError", "(Missing <a> in <li> or "
"memory exhausted.)");
return;
}
data = urldb_get_url_data(url);
if (!data) {
/* No entry in database, so add one */
urldb_add_url(url);
/* now attempt to get url data */
data = urldb_get_url_data(url);
}
if (!data)
return;
/* Make this URL persistent */
urldb_set_url_persistence(url, true);
if (!data->title)
urldb_set_url_title(url, title);
entry = tree_create_URL_node(directory, url, data, title);
xmlFree(url);
xmlFree(title);
}
/**
* Search the children of an xmlNode for an element.
*
* \param node xmlNode to search children of, or 0
* \param name name of element to find
* \return first child of node which is an element and matches name, or
* 0 if not found or parameter node is 0
*/
xmlNode *options_find_tree_element(xmlNode *node, const char *name) {
xmlNode *n;
if (!node)
return 0;
for (n = node->children;
n && !(n->type == XML_ELEMENT_NODE &&
strcmp((const char *) n->name, name) == 0);
n = n->next)
;
return n;
}
/**
* Perform a save to a specified file
*
* /param filename the file to save to
*/
bool options_save_tree(struct tree *tree, const char *filename, const char *page_title) {
int res;
xmlDoc *doc;
xmlNode *html, *head, *title, *body;
/* Unfortunately the Browse Hotlist format is invalid HTML,
* so this is a lie. */
doc = htmlNewDoc(
(const xmlChar *) "http://www.w3.org/TR/html4/strict.dtd",
(const xmlChar *) "-//W3C//DTD HTML 4.01//EN");
if (!doc) {
warn_user("NoMemory", 0);
return false;
}
html = xmlNewNode(NULL, (const xmlChar *) "html");
if (!html) {
warn_user("NoMemory", 0);
xmlFreeDoc(doc);
return false;
}
xmlDocSetRootElement(doc, html);
head = xmlNewChild(html, NULL, (const xmlChar *) "head", NULL);
if (!head) {
warn_user("NoMemory", 0);
xmlFreeDoc(doc);
return false;
}
title = xmlNewTextChild(head, NULL, (const xmlChar *) "title",
(const xmlChar *) page_title);
if (!title) {
warn_user("NoMemory", 0);
xmlFreeDoc(doc);
return false;
}
body = xmlNewChild(html, NULL, (const xmlChar *) "body", NULL);
if (!body) {
warn_user("NoMemory", 0);
xmlFreeDoc(doc);
return false;
}
if (!options_save_tree_directory(tree->root, body)) {
warn_user("NoMemory", 0);
xmlFreeDoc(doc);
return false;
}
doc->charset = XML_CHAR_ENCODING_UTF8;
res = htmlSaveFileEnc(filename, doc, "iso-8859-1");
if (res == -1) {
warn_user("HotlistSaveError", 0);
xmlFreeDoc(doc);
return false;
}
xmlFreeDoc(doc);
return true;
}
/**
* Add a directory to the HTML tree for saving.
*
* \param directory hotlist directory to add
* \param node node to add ul to
* \return true on success, false on memory exhaustion
*/
bool options_save_tree_directory(struct node *directory, xmlNode *node) {
struct node *child;
xmlNode *ul, *h4;
ul = xmlNewChild(node, NULL, (const xmlChar *) "ul", NULL);
if (!ul)
return false;
for (child = directory->child; child; child = child->next) {
if (!child->folder) {
/* entry */
if (!options_save_tree_entry(child, ul))
return false;
} else {
/* directory */
/* invalid HTML */
h4 = xmlNewTextChild(ul, NULL,
(const xmlChar *) "h4",
(const xmlChar *) child->data.text);
if (!h4)
return false;
if (!options_save_tree_directory(child, ul))
return false;
} }
return true;
}
/**
* Add an entry to the HTML tree for saving.
*
* The node must contain a sequence of node_elements in the following order:
*
* \param entry hotlist entry to add
* \param node node to add li to
* \return true on success, false on memory exhaustion
*/
bool options_save_tree_entry(struct node *entry, xmlNode *node) {
xmlNode *li, *a;
xmlAttr *href;
struct node_element *element;
li = xmlNewChild(node, NULL, (const xmlChar *) "li", NULL);
if (!li)
return false;
a = xmlNewTextChild(li, NULL, (const xmlChar *) "a",
(const xmlChar *) entry->data.text);
if (!a)
return false;
element = tree_find_element(entry, TREE_ELEMENT_URL);
if (!element)
return false;
href = xmlNewProp(a, (const xmlChar *) "href",
(const xmlChar *) element->text);
if (!href)
return false;
return true;
}