diff options
Diffstat (limited to 'desktop')
-rw-r--r-- | desktop/browser.c | 24 | ||||
-rw-r--r-- | desktop/browser.h | 3 | ||||
-rw-r--r-- | desktop/gui.h | 9 | ||||
-rw-r--r-- | desktop/options.c | 8 | ||||
-rw-r--r-- | desktop/options.h | 2 | ||||
-rw-r--r-- | desktop/save_complete.c | 755 | ||||
-rw-r--r-- | desktop/save_complete.h | 42 | ||||
-rw-r--r-- | desktop/search.c | 705 | ||||
-rw-r--r-- | desktop/search.h | 91 | ||||
-rw-r--r-- | desktop/searchweb.c | 292 | ||||
-rw-r--r-- | desktop/searchweb.h | 79 |
11 files changed, 2002 insertions, 8 deletions
diff --git a/desktop/browser.c b/desktop/browser.c index cf28006c5..1072c3b22 100644 --- a/desktop/browser.c +++ b/desktop/browser.c @@ -87,12 +87,13 @@ static bool browser_window_check_throbber(struct browser_window *bw); static void browser_window_convert_to_download(struct browser_window *bw); static void browser_window_start_throbber(struct browser_window *bw); static void browser_window_stop_throbber(struct browser_window *bw); +static void browser_window_set_icon(struct browser_window *bw); static void browser_window_set_status(struct browser_window *bw, const char *text); static void browser_window_set_pointer(struct gui_window *g, gui_pointer_shape shape); static void download_window_callback(fetch_msg msg, void *p, const void *data, - unsigned long size); + unsigned long size, fetch_error_code errorcode); static void browser_window_destroy_children(struct browser_window *bw); static void browser_window_destroy_internal(struct browser_window *bw); static void browser_window_set_scale_internal(struct browser_window *bw, @@ -167,6 +168,7 @@ struct browser_window *browser_window_create(const char *url, if (url) browser_window_go(bw, url, referer, history_add); + return bw; } @@ -418,8 +420,6 @@ void browser_window_callback(content_msg msg, struct content *c, } #endif else { - browser_window_refresh_url_bar(bw, c->url, bw->frag_id); - bw->refresh_interval = -1; browser_window_set_status(bw, c->status_message); } @@ -491,6 +491,7 @@ void browser_window_callback(content_msg msg, struct content *c, browser_window_update(bw, false); browser_window_set_status(bw, c->status_message); browser_window_stop_throbber(bw); + browser_window_set_icon(bw); history_update(bw->history, c); hotlist_visited(c); free(bw->referer); @@ -764,6 +765,21 @@ bool browser_window_check_throbber(struct browser_window *bw) return false; } +/** + * when ready, set icon at top level + * \param bw browser_window + * current implementation ignores lower-levels' link rels completely + */ +void browser_window_set_icon(struct browser_window *bw) +{ + while (bw->parent) + bw = bw->parent; + if ((bw->current_content != NULL) && (bw->current_content->type == CONTENT_HTML)) + gui_window_set_icon(bw->window, + bw->current_content->data.html.favicon); + else + gui_window_set_icon(bw->window, NULL); +} /** * Redraw browser window, set extent to content, and update title. @@ -1298,7 +1314,7 @@ void browser_window_find_target_internal(struct browser_window *bw, */ void download_window_callback(fetch_msg msg, void *p, const void *data, - unsigned long size) + unsigned long size, fetch_error_code errorcode) { struct gui_download_window *download_window = p; diff --git a/desktop/browser.h b/desktop/browser.h index 8f9c2760e..ee777c832 100644 --- a/desktop/browser.h +++ b/desktop/browser.h @@ -170,6 +170,9 @@ struct browser_window { /** Last time a link was followed in this window */ unsigned int last_action; + + /** search context for free text search */ + struct search_context *search_context; struct form_control *visible_select_menu; }; diff --git a/desktop/gui.h b/desktop/gui.h index 6449c1edb..ca557ef8a 100644 --- a/desktop/gui.h +++ b/desktop/gui.h @@ -56,8 +56,7 @@ typedef enum { GUI_POINTER_DEFAULT, GUI_POINTER_POINT, GUI_POINTER_CARET, #include "utils/config.h" #include "content/content.h" #include "desktop/browser.h" - -extern struct gui_window *search_current_window; +#include "desktop/search.h" void gui_init(int argc, char** argv); void gui_init2(int argc, char** argv); @@ -67,6 +66,7 @@ void gui_quit(void); struct gui_window *gui_create_browser_window(struct browser_window *bw, struct browser_window *clone, bool new_tab); +struct browser_window *gui_window_get_browser_window(struct gui_window *g); void gui_window_destroy(struct gui_window *g); void gui_window_set_title(struct gui_window *g, const char *title); void gui_window_redraw(struct gui_window *g, int x0, int y0, int x1, int y1); @@ -88,6 +88,8 @@ void gui_window_hide_pointer(struct gui_window *g); void gui_window_set_url(struct gui_window *g, const char *url); void gui_window_start_throbber(struct gui_window *g); void gui_window_stop_throbber(struct gui_window *g); +void gui_window_set_icon(struct gui_window *g, struct content *icon); +void gui_window_set_search_ico(struct content *ico); void gui_window_place_caret(struct gui_window *g, int x, int y, int height); void gui_window_remove_caret(struct gui_window *g); void gui_window_new_content(struct gui_window *g); @@ -125,7 +127,8 @@ void gui_launch_url(const char *url); bool gui_search_term_highlighted(struct gui_window *g, unsigned start_offset, unsigned end_offset, - unsigned *start_idx, unsigned *end_idx); + unsigned *start_idx, unsigned *end_idx, + struct search_context *context); struct ssl_cert_info; diff --git a/desktop/options.c b/desktop/options.c index 41896bfc7..1b639527d 100644 --- a/desktop/options.c +++ b/desktop/options.c @@ -111,12 +111,16 @@ char *option_ca_bundle = 0; char *option_ca_path = 0; /** Cookie file location */ char *option_cookie_file = 0; -/** Cookie jar loaction */ +/** Cookie jar location */ char *option_cookie_jar = 0; /** Home page location */ char *option_homepage_url = 0; +/** search web from url bar */ +bool option_search_url_bar = false; /** URL completion in url bar */ bool option_url_suggestion = true; +/** default web search provider */ +int option_search_provider = 0; /** default x position of new windows */ int option_window_x = 0; /** default y position of new windows */ @@ -231,6 +235,8 @@ struct { { "cookie_file", OPTION_STRING, &option_cookie_file }, { "cookie_jar", OPTION_STRING, &option_cookie_jar }, { "homepage_url", OPTION_STRING, &option_homepage_url }, + { "search_url_bar", OPTION_BOOL, &option_search_url_bar}, + { "search_provider", OPTION_INTEGER, &option_search_provider}, { "url_suggestion", OPTION_BOOL, &option_url_suggestion }, { "window_x", OPTION_INTEGER, &option_window_x }, { "window_y", OPTION_INTEGER, &option_window_y }, diff --git a/desktop/options.h b/desktop/options.h index a25160818..ca92ee90a 100644 --- a/desktop/options.h +++ b/desktop/options.h @@ -70,6 +70,8 @@ extern char *option_ca_path; extern char *option_cookie_file; extern char *option_cookie_jar; extern char *option_homepage_url; +extern bool option_search_url_bar; +extern int option_search_provider; extern bool option_target_blank; extern bool option_button_2_tab; extern bool option_url_suggestion; diff --git a/desktop/save_complete.c b/desktop/save_complete.c new file mode 100644 index 000000000..48438908d --- /dev/null +++ b/desktop/save_complete.c @@ -0,0 +1,755 @@ +/* + * Copyright 2004 John M Bell <jmb202@ecs.soton.ac.uk> + * Copyright 2004-2007 James Bursa <bursa@users.sourceforge.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 + * Save HTML document with dependencies (implementation). + */ + +#include "utils/config.h" + +#define _GNU_SOURCE /* for strndup */ +#include <assert.h> +#include <ctype.h> +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <sys/types.h> +#include <regex.h> +#include <libxml/HTMLtree.h> +#include <libxml/parserInternals.h> +#include "utils/config.h" +#include "content/content.h" +#include "css/css.h" +#include "render/box.h" +#include "desktop/save_complete.h" +#include "utils/log.h" +#include "utils/url.h" +#include "utils/utils.h" + +regex_t save_complete_import_re; + +/** An entry in save_complete_list. */ +struct save_complete_entry { + struct content *content; + struct save_complete_entry *next; /**< Next entry in list */ +}; + +static bool save_complete_html(struct content *c, const char *path, + bool index, struct save_complete_entry **list); +static bool save_imported_sheets(struct content *c, const char *path, + struct save_complete_entry **list); +static char * rewrite_stylesheet_urls(const char *source, unsigned int size, + int *osize, const char *base, + struct save_complete_entry *list); +static bool rewrite_document_urls(xmlDoc *doc, const char *base, + struct save_complete_entry *list); +static bool rewrite_urls(xmlNode *n, const char *base, + struct save_complete_entry *list); +static bool rewrite_url(xmlNode *n, const char *attr, const char *base, + struct save_complete_entry *list); +static bool save_complete_list_add(struct content *content, + struct save_complete_entry **list); +static struct content * save_complete_list_find(const char *url, + struct save_complete_entry *list); +static bool save_complete_list_check(struct content *content, + struct save_complete_entry *list); +/* static void save_complete_list_dump(void); */ +static bool save_complete_inventory(const char *path, + struct save_complete_entry *list); + +/** + * Save an HTML page with all dependencies. + * + * \param c CONTENT_HTML to save + * \param path directory to save to (must exist) + * \return true on success, false on error and error reported + */ + +bool save_complete(struct content *c, const char *path) +{ + bool result; + struct save_complete_entry *list = NULL; + + result = save_complete_html(c, path, true, &list); + + if (result) + result = save_complete_inventory(path, list); + + /* free save_complete_list */ + while (list) { + struct save_complete_entry *next = list->next; + free(list); + list = next; + } + + return result; +} + + +/** + * Save an HTML page with all dependencies, recursing through imported pages. + * + * \param c CONTENT_HTML to save + * \param path directory to save to (must exist) + * \param index true to save as "index" + * \return true on success, false on error and error reported + */ + +bool save_complete_html(struct content *c, const char *path, bool index, + struct save_complete_entry **list) +{ + char filename[256]; + unsigned int i; + xmlDocPtr doc; + bool res; + + if (c->type != CONTENT_HTML) + return false; + + if (save_complete_list_check(c, *list)) + return true; + + /* save stylesheets, ignoring the base and adblocking sheets */ + for (i = STYLESHEET_START; i != c->data.html.stylesheet_count; i++) { + struct content *css = c->data.html.stylesheets[i].c; + char *source; + int source_len; + bool is_style; + + if (!css) + continue; + if (save_complete_list_check(css, *list)) + continue; + + is_style = (strcmp(css->url, c->data.html.base_url) == 0); + + if (is_style == false) { + if (!save_complete_list_add(css, list)) { + warn_user("NoMemory", 0); + return false; + } + } + + if (!save_imported_sheets(css, path, list)) + return false; + + if (is_style) + continue; /* don't save <style> elements */ + + snprintf(filename, sizeof filename, "%p", css); + source = rewrite_stylesheet_urls(css->source_data, + css->source_size, &source_len, css->url, + *list); + if (!source) { + warn_user("NoMemory", 0); + return false; + } + res = save_complete_gui_save(path, filename, source_len, + source, CONTENT_CSS); + free(source); + if (res == false) + return false; + } + + /* save objects */ + for (i = 0; i != c->data.html.object_count; i++) { + struct content *obj = c->data.html.object[i].content; + + /* skip difficult content types */ + if (!obj || obj->type >= CONTENT_OTHER || !obj->source_data) + continue; + if (save_complete_list_check(obj, *list)) + continue; + + if (!save_complete_list_add(obj, list)) { + warn_user("NoMemory", 0); + return false; + } + + if (obj->type == CONTENT_HTML) { + if (!save_complete_html(obj, path, false, list)) + return false; + continue; + } + + snprintf(filename, sizeof filename, "%p", obj); + res = save_complete_gui_save(path, filename, + obj->source_size, obj->source_data, obj->type); + if(res == false) + return false; + } + + /*save_complete_list_dump();*/ + + /* copy document */ + doc = xmlCopyDoc(c->data.html.document, 1); + if (doc == NULL) { + warn_user("NoMemory", 0); + return false; + } + + /* rewrite all urls we know about */ + if (!rewrite_document_urls(doc, c->data.html.base_url, *list)) { + xmlFreeDoc(doc); + warn_user("NoMemory", 0); + return false; + } + + /* save the html file out last of all */ + if (index) + snprintf(filename, sizeof filename, "index"); + else + snprintf(filename, sizeof filename, "%p", c); + + errno = 0; + if (save_complete_htmlSaveFileFormat(path, filename, doc, 0, 0) == -1) { + if (errno) + warn_user("SaveError", strerror(errno)); + else + warn_user("SaveError", "htmlSaveFileFormat failed"); + + xmlFreeDoc(doc); + return false; + } + + xmlFreeDoc(doc); + + return true; +} + + +/** + * Save stylesheets imported by a CONTENT_CSS. + * + * \param c a CONTENT_CSS + * \param path path to save to + * \return true on success, false on error and error reported + */ + +bool save_imported_sheets(struct content *c, const char *path, + struct save_complete_entry **list) +{ + char filename[256]; + unsigned int j; + char *source; + int source_len; + bool res; + + for (j = 0; j != c->data.css.import_count; j++) { + struct content *css = c->data.css.imports[j].c; + + if (!css) + continue; + if (save_complete_list_check(css, *list)) + continue; + + if (!save_complete_list_add(css, list)) { + warn_user("NoMemory", 0); + return false; + } + + if (!save_imported_sheets(css, path, list)) + return false; + + snprintf(filename, sizeof filename, "%p", css); + source = rewrite_stylesheet_urls(css->source_data, + css->source_size, &source_len, css->url, + *list); + if (!source) { + warn_user("NoMemory", 0); + return false; + } + + res = save_complete_gui_save(path, filename, source_len, + source, CONTENT_CSS); + free(source); + if (res == false) + return false; + } + + return true; +} + + +/** + * Initialise the save_complete module. + */ + +void save_complete_init(void) +{ + /* Match an @import rule - see CSS 2.1 G.1. */ + regcomp_wrapper(&save_complete_import_re, + "@import" /* IMPORT_SYM */ + "[ \t\r\n\f]*" /* S* */ + /* 1 */ + "(" /* [ */ + /* 2 3 */ + "\"(([^\"]|[\\]\")*)\"" /* STRING (approximated) */ + "|" + /* 4 5 */ + "'(([^']|[\\]')*)'" + "|" /* | */ + "url\\([ \t\r\n\f]*" /* URI (approximated) */ + /* 6 7 */ + "\"(([^\"]|[\\]\")*)\"" + "[ \t\r\n\f]*\\)" + "|" + "url\\([ \t\r\n\f]*" + /* 8 9 */ + "'(([^']|[\\]')*)'" + "[ \t\r\n\f]*\\)" + "|" + "url\\([ \t\r\n\f]*" + /* 10 */ + "([^) \t\r\n\f]*)" + "[ \t\r\n\f]*\\)" + ")", /* ] */ + REG_EXTENDED | REG_ICASE); +} + + +/** + * Rewrite stylesheet \@import rules for save complete. + * + * @param source stylesheet source + * @param size size of source + * @param osize updated with the size of the result + * @param base url of stylesheet + * @return converted source, or 0 on out of memory + */ + +char * rewrite_stylesheet_urls(const char *source, unsigned int size, + int *osize, const char *base, + struct save_complete_entry *list) +{ + char *res; + const char *url; + char *url2; + char buf[20]; + unsigned int offset = 0; + int url_len = 0; + struct content *content; + int m; + unsigned int i; + unsigned int imports = 0; + regmatch_t match[11]; + url_func_result result; + + /* count number occurences of @import to (over)estimate result size */ + /* can't use strstr because source is not 0-terminated string */ + for (i = 0; 7 < size && i != size - 7; i++) { + if (source[i] == '@' && + tolower(source[i + 1]) == 'i' && + tolower(source[i + 2]) == 'm' && + tolower(source[i + 3]) == 'p' && + tolower(source[i + 4]) == 'o' && + tolower(source[i + 5]) == 'r' && + tolower(source[i + 6]) == 't') + imports++; + } + + res = malloc(size + imports * 20); + if (!res) + return 0; + *osize = 0; + + while (offset < size) { + m = regexec(&save_complete_import_re, source + offset, + 11, match, 0); + if (m) + break; + + /*for (unsigned int i = 0; i != 11; i++) { + if (match[i].rm_so == -1) + continue; + fprintf(stderr, "%i: '%.*s'\n", i, + match[i].rm_eo - match[i].rm_so, + source + offset + match[i].rm_so); + }*/ + + url = 0; + if (match[2].rm_so != -1) { + url = source + offset + match[2].rm_so; + url_len = match[2].rm_eo - match[2].rm_so; + } else if (match[4].rm_so != -1) { + url = source + offset + match[4].rm_so; + url_len = match[4].rm_eo - match[4].rm_so; + } else if (match[6].rm_so != -1) { + url = source + offset + match[6].rm_so; + url_len = match[6].rm_eo - match[6].rm_so; + } else if (match[8].rm_so != -1) { + url = source + offset + match[8].rm_so; + url_len = match[8].rm_eo - match[8].rm_so; + } else if (match[10].rm_so != -1) { + url = source + offset + match[10].rm_so; + url_len = match[10].rm_eo - match[10].rm_so; + } + assert(url); + + url2 = strndup(url, url_len); + if (!url2) { + free(res); + return 0; + } + result = url_join(url2, base, (char**)&url); + free(url2); + if (result == URL_FUNC_NOMEM) { + free(res); + return 0; + } + + /* copy data before match */ + memcpy(res + *osize, source + offset, match[0].rm_so); + *osize += match[0].rm_so; + + if (result == URL_FUNC_OK) { + content = save_complete_list_find(url, list); + if (content) { + /* replace import */ + snprintf(buf, sizeof buf, "@import '%p'", + content); + memcpy(res + *osize, buf, strlen(buf)); + *osize += strlen(buf); + } else { + /* copy import */ + memcpy(res + *osize, source + offset + match[0].rm_so, + match[0].rm_eo - match[0].rm_so); + *osize += match[0].rm_eo - match[0].rm_so; + } + } + else { + /* copy import */ + memcpy(res + *osize, source + offset + match[0].rm_so, + match[0].rm_eo - match[0].rm_so); + *osize += match[0].rm_eo - match[0].rm_so; + } + + assert(0 < match[0].rm_eo); + offset += match[0].rm_eo; + } + + /* copy rest of source */ + if (offset < size) { + memcpy(res + *osize, source + offset, size - offset); + *osize += size - offset; + } + + return res; +} + + +/** + * Rewrite URLs in a HTML document to be relative. + * + * \param doc root of the document tree + * \param base base url of document + * \return true on success, false on out of memory + */ + +bool rewrite_document_urls(xmlDoc *doc, const char *base, + struct save_complete_entry *list) +{ + xmlNode *node; + + for (node = doc->children; node; node = node->next) + if (node->type == XML_ELEMENT_NODE) + if (!rewrite_urls(node, base, list)) + return false; + + return true; +} + + +/** + * Traverse tree, rewriting URLs as we go. + * + * \param n xmlNode of type XML_ELEMENT_NODE to rewrite + * \param base base url of document + * \return true on success, false on out of memory + * + * URLs in the tree rooted at element n are rewritten. + */ + +bool rewrite_urls(xmlNode *n, const char *base, + struct save_complete_entry *list) +{ + xmlNode *child; + + assert(n->type == XML_ELEMENT_NODE); + + /** + * We only need to consider the following cases: + * + * Attribute: Elements: + * + * 1) data <object> + * 2) href <a> <area> <link> + * 3) src <script> <input> <frame> <iframe> <img> + * 4) n/a <style> + * 5) n/a any <base> tag + * 6) background any (except those above) + */ + if (!n->name) { + /* ignore */ + } + /* 1 */ + else if (strcmp((const char *) n->name, "object") == 0) { + if (!rewrite_url(n, "data", base, list)) + return false; + } + /* 2 */ + else if (strcmp((const char *) n->name, "a") == 0 || + strcmp((const char *) n->name, "area") == 0 || + strcmp((const char *) n->name, "link") == 0) { + if (!rewrite_url(n, "href", base, list)) + return false; + } + /* 3 */ + else if (strcmp((const char *) n->name, "frame") == 0 || + strcmp((const char *) n->name, "iframe") == 0 || + strcmp((const char *) n->name, "input") == 0 || + strcmp((const char *) n->name, "img") == 0 || + strcmp((const char *) n->name, "script") == 0) { + if (!rewrite_url(n, "src", base, list)) + return false; + } + /* 4 */ + else if (strcmp((const char *) n->name, "style") == 0) { + unsigned int len; + xmlChar *content; + + for (child = n->children; child != 0; child = child->next) { + /* Get current content */ + content = xmlNodeGetContent(child); + if (!content) + /* unfortunately we don't know if this is + * due to memory exhaustion, or because + * there is no content for this node */ + continue; + + /* Rewrite @import rules */ + char *rewritten = rewrite_stylesheet_urls( + (const char *) content, + strlen((const char *) content), + (int *) &len, base, list); + xmlFree(content); + if (!rewritten) + return false; + + /* set new content */ + xmlNodeSetContentLen(child, + (const xmlChar*)rewritten, + len); + } + + return true; + } + /* 5 */ + else if (strcmp((const char *) n->name, "base") == 0) { + /* simply remove any <base> tags from the document */ + xmlUnlinkNode(n); + xmlFreeNode(n); + /* base tags have no content, so there's no point recursing + * additionally, we've just destroyed this node, so trying + * to recurse would result in bad things happening */ + return true; + } + /* 6 */ + else { + if (!rewrite_url(n, "background", base, list)) + return false; + } + + /* now recurse */ + for (child = n->children; child;) { + /* we must extract the next child now, as if the current + * child is a <base> element, it will be removed from the + * tree (see 5, above), thus preventing extraction of the + * next child */ + xmlNode *next = child->next; + if (child->type == XML_ELEMENT_NODE) { + if (!rewrite_urls(child, base, list)) + return false; + } + child = next; + } + + return true; +} + + +/** + * Rewrite an URL in a HTML document. + * + * \param n The node to modify + * \param attr The html attribute to modify + * \param base base url of document + * \return true on success, false on out of memory + */ + +bool rewrite_url(xmlNode *n, const char *attr, const char *base, + struct save_complete_entry *list) +{ + char *url, *data; + char rel[20]; + struct content *content; + url_func_result res; + + if (!xmlHasProp(n, (const xmlChar *) attr)) + return true; + + data = (char *) xmlGetProp(n, (const xmlChar *) attr); + if (!data) + return false; + + res = url_join(data, base, &url); + xmlFree(data); + if (res == URL_FUNC_NOMEM) + return false; + else if (res == URL_FUNC_OK) { + content = save_complete_list_find(url, list); + if (content) { + /* found a match */ + free(url); + snprintf(rel, sizeof rel, "%p", content); + if (!xmlSetProp(n, (const xmlChar *) attr, + (xmlChar *) rel)) + return false; + } else { + /* no match found */ + if (!xmlSetProp(n, (const xmlChar *) attr, + (xmlChar *) url)) { + free(url); + return false; + } + free(url); + } + } + + return true; +} + + +/** + * Add a content to the save_complete_list. + * + * \param content content to add + * \return true on success, false on out of memory + */ + +bool save_complete_list_add(struct content *content, + struct save_complete_entry **list) +{ + struct save_complete_entry *entry; + entry = malloc(sizeof (*entry)); + if (!entry) + return false; + entry->content = content; + entry->next = *list; + *list = entry; + return true; +} + + +/** + * Look up a url in the save_complete_list. + * + * \param url url to find + * \return content if found, 0 otherwise + */ + +struct content * save_complete_list_find(const char *url, + struct save_complete_entry *list) +{ + struct save_complete_entry *entry; + for (entry = list; entry; entry = entry->next) + if (strcmp(url, entry->content->url) == 0) + return entry->content; + return 0; +} + + +/** + * Look up a content in the save_complete_list. + * + * \param content pointer to content + * \return true if the content is in the save_complete_list + */ + +bool save_complete_list_check(struct content *content, + struct save_complete_entry *list) +{ + struct save_complete_entry *entry; + for (entry = list; entry; entry = entry->next) + if (entry->content == content) + return true; + return false; +} + + +#if 0 +/** + * Dump save complete list to stderr + */ +void save_complete_list_dump(void) +{ + struct save_complete_entry *entry; + for (entry = save_complete_list; entry; entry = entry->next) + fprintf(stderr, "%p : %s\n", entry->content, + entry->content->url); +} +#endif + + +/** + * Create the inventory file listing original URLs. + */ + +bool save_complete_inventory(const char *path, + struct save_complete_entry *list) +{ + char urlpath[256]; + FILE *fp; + char *pathstring, *standardpath = (path[0] == '/') ? + (char *)(path + 1) : (char *)path; + + snprintf(urlpath, sizeof urlpath, "file:///%s/Inventory", + standardpath); + pathstring = url_to_path(urlpath); + if (pathstring == NULL) { + warn_user("NoMemory", 0); + return false; + } + fp = fopen(pathstring, "w"); + free(pathstring); + if (!fp) { + LOG(("fopen(): errno = %i", errno)); + warn_user("SaveError", strerror(errno)); + return false; + } + + struct save_complete_entry *entry; + for (entry = list; entry; entry = entry->next) + fprintf(fp, "%p %s\n", entry->content, entry->content->url); + + fclose(fp); + + return true; +} + diff --git a/desktop/save_complete.h b/desktop/save_complete.h new file mode 100644 index 000000000..e23092471 --- /dev/null +++ b/desktop/save_complete.h @@ -0,0 +1,42 @@ +/* + * Copyright 2004 John M Bell <jmb202@ecs.soton.ac.uk> + * Copyright 2009 Mark Benjamin <netsurf-browser.org.MarkBenjamin@dfgh.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 + * Save HTML document with dependencies (interface). + */ + +#ifndef _NETSURF_DESKTOP_SAVE_COMPLETE_H_ +#define _NETSURF_DESKTOP_SAVE_COMPLETE_H_ + +#include <stdbool.h> +#include <libxml/HTMLtree.h> +#include "content/content.h" + +struct content; + +void save_complete_init(void); +bool save_complete(struct content *c, const char *path); + +bool save_complete_gui_save(const char *path, const char *filename, + size_t len, const char *sourcedata, content_type type); + +int save_complete_htmlSaveFileFormat(const char *path, const char *filename, + xmlDocPtr cur, const char *encoding, int format); + +#endif diff --git a/desktop/search.c b/desktop/search.c new file mode 100644 index 000000000..018f40674 --- /dev/null +++ b/desktop/search.c @@ -0,0 +1,705 @@ +/* + * Copyright 2004 John M Bell <jmb202@ecs.soton.ac.uk> + * Copyright 2005 Adrian Lees <adrianl@users.sourceforge.net> + * Copyright 2009 Mark Benjamin <netsurf-browser.org.MarkBenjamin@dfgh.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 + * Free text search (core) + */ +#include "utils/config.h" + +#include <ctype.h> +#include <string.h> +#include "content/content.h" +#include "desktop/browser.h" +#include "desktop/gui.h" +#include "desktop/options.h" +#include "desktop/search.h" +#include "desktop/selection.h" +#include "render/box.h" +#include "render/html.h" +#include "utils/config.h" +#include "utils/log.h" +#include "utils/messages.h" +#include "utils/url.h" +#include "utils/utils.h" + + +#ifndef NOF_ELEMENTS +#define NOF_ELEMENTS(array) (sizeof(array)/sizeof(*(array))) +#endif + + +struct list_entry { + unsigned start_idx; /* start position of match */ + unsigned end_idx; /* end of match */ + + struct box *start_box; /* used only for html contents */ + struct box *end_box; + + struct selection *sel; + + struct list_entry *prev; + struct list_entry *next; +}; + +struct search_context { + struct browser_window *bw; + struct content *content; + char *string; + bool prev_case_sens; + bool newsearch; + bool insert; + void *p; /* front-specific data */ + struct search_callbacks *callbacks; + struct list_entry *found; + struct list_entry *current; /* first for select all */ +}; + +static void search_text(const char *string, int string_len, + struct search_context *context, search_flags_t flags); +static const char *find_pattern(const char *string, int s_len, + const char *pattern, int p_len, bool case_sens, + unsigned int *m_len); +static bool find_occurrences_html(const char *pattern, int p_len, + struct box *cur, bool case_sens, + struct search_context *context); +static bool find_occurrences_text(const char *pattern, int p_len, + struct content *c, bool case_sens, + struct search_context *context); +static struct list_entry *add_entry(unsigned start_idx, unsigned end_idx, + struct search_context *context); +static void free_matches(struct search_context *context); + + +/** + * create a search_context + * \param bw the browser_window the search_context is connected to + * \param callbacks the callbacks to modify appearance according to results + * \param p the pointer to send to the callbacks + * \return true for success + */ +bool search_create_context(struct browser_window *bw, + struct search_callbacks *callbacks, void *p) +{ + struct search_context *context = malloc(sizeof(struct search_context)); + struct list_entry *search_head = malloc(sizeof(struct list_entry)); + + if ((context == NULL) || (search_head == NULL)) { + warn_user("NoMemory", 0); + return false; + } + if (bw->search_context != NULL) + search_destroy_context(bw->search_context); + + search_head->start_idx = 0; + search_head->end_idx = 0; + search_head->start_box = NULL; + search_head->end_box = NULL; + search_head->sel = NULL; + search_head->prev = NULL; + search_head->next = NULL; + + context->found = search_head; + context->current = NULL; + context->content = NULL; + context->string = NULL; + context->prev_case_sens = false; + context->newsearch = true; + context->insert = true; + context->bw = bw; + context->callbacks = callbacks; + context->p = p; + + bw->search_context = context; + return true; +} +/** + * to simplify calls to search_step(); checks that the browser_window is + * non-NULL, creates a new search_context in case of a new search + * \param bw the browser_window the search refers to + * \param callbacks the callbacks to modify appearance according to results + * \param p a pointer returned to the callbacks + * \return true for success + */ +bool search_verify_new(struct browser_window *bw, + struct search_callbacks *callbacks, void *p) +{ + if (bw == NULL) + return false; + if (bw->search_context == NULL) + return search_create_context(bw, callbacks, p); + return true; +} + +/** + * Begins/continues the search process + * Note that this may be called many times for a single search. + * + * \param bw the browser_window to search in + * \param flags the flags forward/back etc + * \param string the string to match + */ + +void search_step(struct search_context *context, search_flags_t flags, + const char *string) +{ + int string_len; + int i = 0; + + if ((context == NULL) || (context->callbacks == NULL)) { + warn_user("SearchError", 0); + return; + } + + + if (context->callbacks->add_recent != NULL) + context->callbacks->add_recent(string, context->p); + + string_len = strlen(string); + for(i = 0; i < string_len; i++) + if (string[i] != '#' && string[i] != '*') break; + if (i >= string_len) { + free_matches(context); + if (context->callbacks->status != NULL) + context->callbacks->status(true, context->p); + if (context->callbacks->back_state != NULL) + context->callbacks->back_state(false, context->p); + if (context->callbacks->forward_state != NULL) + context->callbacks->forward_state(false, context->p); + gui_window_set_scroll(context->bw->window, 0, 0); + return; + } + search_text(string, string_len, context, flags); +} + +/** + * Release the memory used by the list of matches, + * deleting selection objects too + */ + +void free_matches(struct search_context *context) +{ + struct list_entry *a = context->found->next; + struct list_entry *b; + + /* empty the list before clearing and deleting the + selections because the the clearing updates the + screen immediately, causing nested accesses to the list */ + + context->found->prev = NULL; + context->found->next = NULL; + + for (; a; a = b) { + b = a->next; + if (a->sel) { + selection_clear(a->sel, true); + selection_destroy(a->sel); + } + free(a); + } +} + +/** + * Search for a string in the box tree + * + * \param string the string to search for + * \param string_len length of search string + */ +void search_text(const char *string, int string_len, + struct search_context *context, search_flags_t flags) +{ + struct rect bounds; + struct content *c; + struct box *box; + bool case_sensitive, forwards, showall; + + case_sensitive = ((flags & SEARCH_FLAG_CASE_SENSITIVE) != 0) ? + true : false; + forwards = ((flags & SEARCH_FLAG_FORWARDS) != 0) ? true : false; + showall = ((flags & SEARCH_FLAG_SHOWALL) != 0) ? true : false; + + if (context->bw == NULL) + return; + c = context->bw->current_content; + + /* only handle html contents */ + if ((!c) || (c->type != CONTENT_HTML && + c->type != CONTENT_TEXTPLAIN)) + return; + + box = c->data.html.layout; + + if (!box) + return; + + /* LOG(("do_search '%s' - '%s' (%p, %p) %p (%d, %d) %d", + search_data.string, string, search_data.content, c, search_data.found->next, + search_data.prev_case_sens, case_sens, forwards)); */ + + /* check if we need to start a new search or continue an old one */ + if (context->newsearch) { + bool res; + + if (context->string != NULL) + free(context->string); + context->current = NULL; + free_matches(context); + + context->string = malloc(string_len + 1); + if (context->string != NULL) { + memcpy(context->string, string, string_len); + context->string[string_len] = '\0'; + } + + if ((context->callbacks != NULL) && + (context->callbacks->hourglass != NULL)) + context->callbacks->hourglass(true, context->p); + + if (c->type == CONTENT_HTML) + res = find_occurrences_html(string, string_len, + box, case_sensitive, context); + else { + assert(c->type == CONTENT_TEXTPLAIN); + res = find_occurrences_text(string, string_len, + c, case_sensitive, context); + } + + if (!res) { + free_matches(context); + if ((context->callbacks != NULL) && + (context->callbacks->hourglass != + NULL)) + context->callbacks->hourglass(false, + context->p); + return; + } + if ((context->callbacks != NULL) && + (context->callbacks->hourglass != NULL)) + context->callbacks->hourglass(false, context->p); + + context->content = c; + context->prev_case_sens = case_sensitive; +/* LOG(("%d %p %p (%p, %p)", new, search_data.found->next, search_data.current, + search_data.current->prev, search_data.current->next)); */ + /* new search, beginning at the top of the page */ + context->current = context->found->next; + context->newsearch = false; + } + else if (context->current != NULL) { + /* continued search in the direction specified */ + if (forwards) { + if (context->current->next) + context->current = context->current->next; + } + else { + if (context->current->prev) + context->current = context->current->prev; + } + } + + if (context->callbacks == NULL) + return; + if (context->callbacks->status != NULL) + context->callbacks->status((context->current != NULL), + context->p); + search_show_all(showall, context); + + if (context->callbacks->back_state != NULL) + context->callbacks->back_state((context->current != NULL) && + (context->current->prev != NULL), + context->p); + if (context->callbacks->forward_state != NULL) + context->callbacks->forward_state((context->current != NULL) && + (context->current->next != NULL), context->p); + + if (context->current == NULL) + return; + + switch (c->type) { + case CONTENT_HTML: + /* get box position and jump to it */ + box_coords(context->current->start_box, + &bounds.x0, &bounds.y0); + /* \todo: move x0 in by correct idx */ + box_coords(context->current->end_box, + &bounds.x1, &bounds.y1); + /* \todo: move x1 in by correct idx */ + bounds.x1 += context->current->end_box->width; + bounds.y1 += context->current->end_box->height; + break; + + default: + assert(c->type == CONTENT_TEXTPLAIN); + textplain_coords_from_range(c, + context->current->start_idx, + context->current->end_idx, &bounds); + break; + } + + gui_window_scroll_visible(context->bw->window, + bounds.x0, bounds.y0, bounds.x1, bounds.y1); +} + +/** + * Find the first occurrence of 'match' in 'string' and return its index + * + * \param string the string to be searched (unterminated) + * \param s_len length of the string to be searched + * \param pattern the pattern for which we are searching (unterminated) + * \param p_len length of pattern + * \param case_sens true iff case sensitive match required + * \param m_len accepts length of match in bytes + * \return pointer to first match, NULL if none + */ + +const char *find_pattern(const char *string, int s_len, const char *pattern, + int p_len, bool case_sens, unsigned int *m_len) +{ + struct { const char *ss, *s, *p; bool first; } context[16]; + const char *ep = pattern + p_len; + const char *es = string + s_len; + const char *p = pattern - 1; /* a virtual '*' before the pattern */ + const char *ss = string; + const char *s = string; + bool first = true; + int top = 0; + + while (p < ep) { + bool matches; + if (p < pattern || *p == '*') { + char ch; + + /* skip any further asterisks; one is the same as many + */ + do p++; while (p < ep && *p == '*'); + + /* if we're at the end of the pattern, yes, it matches + */ + if (p >= ep) break; + + /* anything matches a # so continue matching from + here, and stack a context that will try to match + the wildcard against the next character */ + + ch = *p; + if (ch != '#') { + /* scan forwards until we find a match for + this char */ + if (!case_sens) ch = toupper(ch); + while (s < es) { + if (case_sens) { + if (*s == ch) break; + } else if (toupper(*s) == ch) + break; + s++; + } + } + + if (s < es) { + /* remember where we are in case the match + fails; we may then resume */ + if (top < (int)NOF_ELEMENTS(context)) { + context[top].ss = ss; + context[top].s = s + 1; + context[top].p = p - 1; + /* ptr to last asterisk */ + context[top].first = first; + top++; + } + + if (first) { + ss = s; + /* remember first non-'*' char */ + first = false; + } + + matches = true; + } + else + matches = false; + } + else if (s < es) { + char ch = *p; + if (ch == '#') + matches = true; + else { + if (case_sens) + matches = (*s == ch); + else + matches = (toupper(*s) == toupper(ch)); + } + if (matches && first) { + ss = s; /* remember first non-'*' char */ + first = false; + } + } + else + matches = false; + + if (matches) { + p++; s++; + } + else { + /* doesn't match, resume with stacked context if we have one */ + if (--top < 0) return NULL; /* no match, give up */ + + ss = context[top].ss; + s = context[top].s; + p = context[top].p; + first = context[top].first; + } + } + + /* end of pattern reached */ + *m_len = max(s - ss, 1); + return ss; +} + +/** + * Finds all occurrences of a given string in the html box tree + * + * \param pattern the string pattern to search for + * \param p_len pattern length + * \param cur pointer to the current box + * \param case_sens whether to perform a case sensitive search + * \return true on success, false on memory allocation failure + */ +bool find_occurrences_html(const char *pattern, int p_len, struct box *cur, + bool case_sens, struct search_context *context) +{ + struct box *a; + + /* ignore this box, if there's no visible text */ + if (!cur->object && cur->text) { + const char *text = cur->text; + unsigned length = cur->length; + + while (length > 0) { + struct list_entry *entry; + unsigned match_length; + unsigned match_offset; + const char *new_text; + const char *pos = find_pattern(text, length, + pattern, p_len, case_sens, + &match_length); + if (!pos) break; + + /* found string in box => add to list */ + match_offset = pos - cur->text; + + entry = add_entry(cur->byte_offset + match_offset, + cur->byte_offset + + match_offset + + match_length, context); + if (!entry) + return false; + + entry->start_box = cur; + entry->end_box = cur; + + new_text = pos + match_length; + length -= (new_text - text); + text = new_text; + } + } + + /* and recurse */ + for (a = cur->children; a; a = a->next) { + if (!find_occurrences_html(pattern, p_len, a, case_sens, + context)) + return false; + } + + return true; +} + +/** + * Finds all occurrences of a given string in a textplain content + * + * \param pattern the string pattern to search for + * \param p_len pattern length + * \param c the content to be searched + * \param case_sens wheteher to perform a case sensitive search + * \return true on success, false on memory allocation failure + */ + +bool find_occurrences_text(const char *pattern, int p_len, + struct content *c, bool case_sens, + struct search_context *context) +{ + int nlines = textplain_line_count(c); + int line; + + for(line = 0; line < nlines; line++) { + size_t offset, length; + const char *text = textplain_get_line(c, line, + &offset, &length); + if (text) { + while (length > 0) { + struct list_entry *entry; + unsigned match_length; + size_t start_idx; + const char *new_text; + const char *pos = find_pattern(text, length, + pattern, p_len, case_sens, + &match_length); + if (!pos) break; + + /* found string in line => add to list */ + start_idx = offset + (pos - text); + entry = add_entry(start_idx, start_idx + + match_length, context); + if (!entry) + return false; + + new_text = pos + match_length; + offset += (new_text - text); + length -= (new_text - text); + text = new_text; + } + } + } + + return true; +} + +/** + * Determines whether any portion of the given text box should be + * selected because it matches the current search string. + * + * \param g gui window + * \param start_offset byte offset within text of string to be checked + * \param end_offset byte offset within text + * \param start_idx byte offset within string of highlight start + * \param end_idx byte offset of highlight end + * \return true iff part of the box should be highlighted + */ + +bool gui_search_term_highlighted(struct gui_window *g, + unsigned start_offset, unsigned end_offset, + unsigned *start_idx, unsigned *end_idx, + struct search_context *context) +{ + if (g == context->bw->window) { + struct list_entry *a; + for(a = context->found->next; a; a = a->next) + if (a->sel && selection_defined(a->sel) && + selection_highlighted(a->sel, + start_offset, end_offset, + start_idx, end_idx)) + return true; + } + + return false; +} + +/** + * Specifies whether all matches or just the current match should + * be highlighted in the search text. + */ + +void search_show_all(bool all, struct search_context *context) +{ + struct list_entry *a; + + for (a = context->found->next; a; a = a->next) { + bool add = true; + if (!all && a != context->current) { + add = false; + if (a->sel) { + selection_clear(a->sel, true); + selection_destroy(a->sel); + a->sel = NULL; + } + } + if (add && !a->sel) { + a->sel = selection_create(context->bw); + if (a->sel) { + struct content *c = context->bw-> + current_content; + switch (c->type) { + case CONTENT_HTML: + selection_init(a->sel, + c->data.html.layout); + break; + default: + assert(c->type == + CONTENT_TEXTPLAIN); + selection_init(a->sel, NULL); + break; + } + selection_set_start(a->sel, a->start_idx); + selection_set_end(a->sel, a->end_idx); + } + } + } +} + +/** + * Add a new entry to the list of matches + * + * \param start_idx offset of match start within textual representation + * \param end_idx offset of match end + * \return pointer to added entry, NULL iff failed + */ + +struct list_entry *add_entry(unsigned start_idx, unsigned end_idx, + struct search_context *context) +{ + struct list_entry *entry; + + /* found string in box => add to list */ + entry = calloc(1, sizeof(*entry)); + if (!entry) { + warn_user("NoMemory", 0); + return NULL; + } + + entry->start_idx = start_idx; + entry->end_idx = end_idx; + entry->sel = NULL; + + entry->next = 0; + entry->prev = context->found->prev; + if (context->found->prev == NULL) + context->found->next = entry; + else + context->found->prev->next = entry; + context->found->prev = entry; + + return entry; +} + +/** + * Ends the search process, invalidating all state + * freeing the list of found boxes + */ +void search_destroy_context(struct search_context *context) +{ + if (context->bw != NULL) + context->bw->search_context = NULL; + if ((context->string != NULL) && (context->callbacks != NULL) && + (context->callbacks->add_recent != NULL)) { + context->callbacks->add_recent(context->string, context->p); + free(context->string); + } + free_matches(context); + free(context); +} + diff --git a/desktop/search.h b/desktop/search.h new file mode 100644 index 000000000..15a4f6c9e --- /dev/null +++ b/desktop/search.h @@ -0,0 +1,91 @@ +/* + * Copyright 2009 Mark Benjamin <netsurf-browser.org.MarkBenjamin@dfgh.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/>. + */ + +#ifndef _NETSURF_DESKTOP_SEARCH_H_ +#define _NETSURF_DESKTOP_SEARCH_H_ + +#include <ctype.h> +#include <string.h> + +struct search_context; + +typedef enum { + SEARCH_FLAG_CASE_SENSITIVE = (1 << 0), + SEARCH_FLAG_FORWARDS = (1 << 1), + SEARCH_FLAG_SHOWALL = (1 << 2) +} search_flags_t; + +/** + * called to clear the context; 'renews' the search too + */ +void search_destroy_context(struct search_context *context); + +/** + * Change the displayed search status. + * \param found search pattern matched in text + * \param p the pointer sent to search_step() / search_create_context() + */ +typedef void (*search_status_callback)(bool found, void *p); + +/** + * display hourglass while searching + * \param active start/stop indicator + * \param p the pointer sent to search_step() / search_create_context() + */ +typedef void (*search_hourglass_callback)(bool active, void *p); + +/** + * add search string to recent searches list + * front has full liberty how to implement the bare notification; + * core gives no guarantee of the integrity of the const char * + * \param string search pattern + * \param p the pointer sent to search_step() / search_create_context() + */ +typedef void (*search_add_recent_callback)(const char *string, void *p); + +/** + * activate search forwards button in gui + * \param active activate/inactivate + * \param p the pointer sent to search_step() / search_create_context() + */ +typedef void (*search_forward_state_callback)(bool active, void *p); + +/** + * activate search back button in gui + * \param active activate/inactivate + * \param p the pointer sent to search_step() / search_create_context() + */ +typedef void (*search_back_state_callback)(bool active, void *p); + +struct search_callbacks { + search_forward_state_callback forward_state; + search_back_state_callback back_state; + search_status_callback status; + search_hourglass_callback hourglass; + search_add_recent_callback add_recent; +}; + +bool search_verify_new(struct browser_window *bw, + struct search_callbacks *callbacks, void *p); +void search_step(struct search_context *context, search_flags_t flags, + const char * string); +bool search_create_context(struct browser_window *bw, + struct search_callbacks *callbacks, void *p); +void search_show_all(bool all, struct search_context *context); + +#endif diff --git a/desktop/searchweb.c b/desktop/searchweb.c new file mode 100644 index 000000000..c6dfaa6df --- /dev/null +++ b/desktop/searchweb.c @@ -0,0 +1,292 @@ +/* + * Copyright 2009 Mark Benjamin <netsurf-browser.org.MarkBenjamin@dfgh.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 + * web search (core) + */ +#include "utils/config.h" + +#include <ctype.h> +#include <string.h> +#include "content/content.h" +#include "content/fetchcache.h" +#include "content/fetch.h" +#include "desktop/browser.h" +#include "desktop/gui.h" +#include "desktop/options.h" +#include "desktop/searchweb.h" +#include "utils/config.h" +#include "utils/log.h" +#include "utils/messages.h" +#include "utils/url.h" +#include "utils/utils.h" + +static struct search_provider { + char *name; /**< readable name such as 'google', 'yahoo', etc */ + char *hostname; /**< host address such as www.google.com */ + char *searchstring; /** < such as "www.google.com?search=%s" */ + char *ico; /** < location of domain's favicon */ +} current_search_provider; + +static struct content *search_ico = NULL; +char *search_engines_file_location; +char *search_default_ico_location; + +/** + * creates a new browser window according to the search term + * \param searchterm such as "my search term" + */ + +bool search_web_new_window(struct browser_window *bw, const char *searchterm) +{ + char *encsearchterm; + char *url; + if (url_escape(searchterm,0, true, NULL, &encsearchterm) != + URL_FUNC_OK) + return false; + url = search_web_get_url(encsearchterm); + free(encsearchterm); + browser_window_create(url, bw, NULL, false, true); + free(url); + return true; +} + +/** simplistic way of checking whether an entry from the url bar is an + * url / a search; could be improved to properly test terms + */ + +bool search_is_url(const char *url) +{ + char *url2, *host; + + if (url_normalize(url, &url2) != URL_FUNC_OK) + return false; + + if (url_host(url2, &host) != URL_FUNC_OK) + return false; + + return true; +} + +/** + * caches the details of the current web search provider + * \param reference the enum value of the provider + * browser init code [as well as changing preferences code] should call + * search_web_provider_details(option_search_provider) + */ + +void search_web_provider_details(int reference) +{ + char buf[300]; + int ref = 0; + if (search_engines_file_location == NULL) + return; + FILE *f = fopen(search_engines_file_location, "r"); + if (f == NULL) + return; + while (fgets(buf, sizeof(buf), f) != NULL) { + if (buf[0] == '\0') + continue; + buf[strlen(buf)-1] = '\0'; + if (ref++ == (int)reference) + break; + } + if (current_search_provider.name != NULL) + free(current_search_provider.name); + current_search_provider.name = strdup(strtok(buf, "|")); + if (current_search_provider.hostname != NULL) + free(current_search_provider.hostname); + current_search_provider.hostname = strdup(strtok(NULL, "|")); + if (current_search_provider.searchstring != NULL) + free(current_search_provider.searchstring); + current_search_provider.searchstring = strdup(strtok(NULL, "|")); + if (current_search_provider.ico != NULL) + free(current_search_provider.ico); + current_search_provider.ico = strdup(strtok(NULL, "|")); + return; +} + +/** + * escapes a search term then creates the appropriate url from it + */ + +char *search_web_from_term(const char *searchterm) +{ + char *encsearchterm, *url; + if (url_escape(searchterm, 0, true, NULL, &encsearchterm) + != URL_FUNC_OK) + return strdup(searchterm); + url = search_web_get_url(encsearchterm); + free(encsearchterm); + return url; +} + +/** accessor for global search provider name */ + +char *search_web_provider_name(void) +{ + if (current_search_provider.name) + return strdup(current_search_provider.name); + return strdup("google"); +} + +/** accessor for global search provider hostname */ + +char *search_web_provider_host(void) +{ + if (current_search_provider.hostname) + return strdup(current_search_provider.hostname); + return strdup("www.google.com"); +} + +/** accessor for global search provider ico name */ + +char *search_web_ico_name(void) +{ + if (current_search_provider.ico) + return strdup(current_search_provider.ico); + return strdup("http://www.google.com/favicon.ico"); +} + +/** + * creates a full url from an encoded search term + */ + +char *search_web_get_url(const char *encsearchterm) +{ + char *pref, *ret; + int len; + if (current_search_provider.searchstring) + pref = strdup(current_search_provider.searchstring); + else + pref = strdup("http://www.google.com/search?q=%s"); + if (pref == NULL) { + warn_user(messages_get("NoMemory"), 0); + return NULL; + } + len = strlen(encsearchterm) + strlen(pref); + ret = malloc(len -1); /* + '\0' - "%s" */ + if (ret == NULL) { + warn_user(messages_get("NoMemory"), 0); + free(pref); + return NULL; + } + snprintf(ret, len-1, pref, encsearchterm); + free(pref); + return ret; +} + +/** + * function to retrieve the search web ico, from cache / from local + * filesystem / from the web + * \param localdefault true when there is no appropriate favicon + * update the search_ico cache else delay until fetcher callback + */ + +void search_web_retrieve_ico(bool localdefault) +{ + char *url; + if (localdefault) { + if (search_default_ico_location == NULL) + return; + url = malloc(SLEN("file://") + strlen( + search_default_ico_location) + 1); + if (url == NULL) { + warn_user(messages_get("NoMemory"), 0); + return; + } + strcpy(url, "file://"); + strcat(url, search_default_ico_location); + } else { + url = search_web_ico_name(); + } + + struct content *icocontent = NULL; + if (url == NULL) { + warn_user(messages_get("NoMemory"), 0); + return; + } + icocontent = fetchcache(url, search_web_ico_callback, + 0, 0, 20, 20, true, 0, + 0, false, false); + free(url); + if (icocontent == NULL) + return; + + fetchcache_go(icocontent, 0, search_web_ico_callback, + 0, 0, 20, 20, + 0, 0, false, 0); + + if (icocontent == NULL) + LOG(("web search ico loading delayed")); + else + search_ico = icocontent; +} + +/** + * returns a reference to the static global search_ico [ / NULL] + * caller may adjust ico's settings; clearing / free()ing is the core's + * responsibility + */ + +struct content *search_web_ico(void) +{ + return search_ico; +} + +/** + * callback function to cache ico then notify front when successful + * else retry default from local file system + */ + +void search_web_ico_callback(content_msg msg, struct content *ico, + intptr_t p1, intptr_t p2, union content_msg_data data) +{ + + switch (msg) { + case CONTENT_MSG_LOADING: + case CONTENT_MSG_READY: + break; + + case CONTENT_MSG_DONE: + LOG(("got favicon '%s'", ico->url)); + if (ico->type == CONTENT_ICO) { + search_ico = ico; /* cache */ + gui_window_set_search_ico(search_ico); + } else { + search_web_retrieve_ico(true); + } + break; + + case CONTENT_MSG_LAUNCH: + case CONTENT_MSG_ERROR: + LOG(("favicon %s error: %s", ico->url, data.error)); + ico = 0; + search_web_retrieve_ico(true); + break; + + case CONTENT_MSG_STATUS: + case CONTENT_MSG_NEWPTR: + case CONTENT_MSG_AUTH: + case CONTENT_MSG_SSL: + break; + + default: + assert(0); + } +} diff --git a/desktop/searchweb.h b/desktop/searchweb.h new file mode 100644 index 000000000..f8dcb9db0 --- /dev/null +++ b/desktop/searchweb.h @@ -0,0 +1,79 @@ +/* + * Copyright 2009 Mark Benjamin <netsurf-browser.org.MarkBenjamin@dfgh.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/>. + */ + +#ifndef _NETSURF_DESKTOP_SEARCH_WEB_H_ +#define _NETSURF_DESKTOP_SEARCH_WEB_H_ + +#include <ctype.h> +#include <string.h> +#include "content/content.h" +#include "desktop/browser.h" + +extern char *search_engines_file_location; +extern char *search_default_ico_location; + +/** + * open new tab/window for web search term + */ +bool search_web_new_window(struct browser_window *bw, const char *searchterm); + +/** + * retrieve full search url from unencoded search term + */ +char *search_web_from_term(const char *searchterm); + +/** + * retrieve full search url from encoded web search term + */ +char *search_web_get_url(const char *encsearchterm); + +/** + * cache details of web search provider from file + */ +void search_web_provider_details(int reference); + +/** + * retrieve name of web search provider + */ +char *search_web_provider_name(void); + +/** + * retrieve hostname of web search provider + */ +char *search_web_provider_host(void); + +/** + * retrieve name of .ico for search bar + */ +char *search_web_ico_name(void); + +/** + * check whether an URL is in fact a search term + * \param url the url being checked + * \return true for url, false for search + */ +bool search_is_url(const char *url); + +void search_web_retrieve_ico(bool localdefault); + +struct content *search_web_ico(void); + +void search_web_ico_callback(content_msg msg, struct content *ico, + intptr_t p1, intptr_t p2, union content_msg_data data); + +#endif |