2009-12-18 02:55:02 +03:00
|
|
|
/*
|
|
|
|
* 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"
|
2010-03-28 16:56:39 +04:00
|
|
|
#include "content/hlcache.h"
|
2009-12-18 02:55:02 +03:00
|
|
|
#include "desktop/browser.h"
|
|
|
|
#include "desktop/gui.h"
|
2013-05-26 01:46:27 +04:00
|
|
|
#include "utils/nsoption.h"
|
2009-12-18 02:55:02 +03:00
|
|
|
#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;
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
static hlcache_handle *search_ico = NULL;
|
2009-12-18 02:55:02 +03:00
|
|
|
char *search_engines_file_location;
|
|
|
|
char *search_default_ico_location;
|
|
|
|
|
2010-04-08 18:14:27 +04:00
|
|
|
#ifdef WITH_BMP
|
2010-03-28 16:56:39 +04:00
|
|
|
static nserror search_web_ico_callback(hlcache_handle *ico,
|
|
|
|
const hlcache_event *event, void *pw);
|
2010-04-08 18:14:27 +04:00
|
|
|
#endif
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2009-12-18 02:55:02 +03:00
|
|
|
/**
|
|
|
|
* 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;
|
2013-02-14 22:21:11 +04:00
|
|
|
char *urltxt;
|
|
|
|
nsurl *url;
|
|
|
|
nserror error;
|
|
|
|
|
|
|
|
if (url_escape(searchterm,0, true, NULL, &encsearchterm) != URL_FUNC_OK)
|
2009-12-18 02:55:02 +03:00
|
|
|
return false;
|
2013-02-14 22:21:11 +04:00
|
|
|
|
|
|
|
urltxt = search_web_get_url(encsearchterm);
|
2009-12-18 02:55:02 +03:00
|
|
|
free(encsearchterm);
|
2013-02-14 22:21:11 +04:00
|
|
|
|
|
|
|
error = nsurl_create(urltxt, &url);
|
|
|
|
if (error == NSERROR_OK) {
|
2013-02-18 15:51:42 +04:00
|
|
|
error = browser_window_create(BROWSER_WINDOW_VERIFIABLE |
|
|
|
|
BROWSER_WINDOW_HISTORY |
|
|
|
|
BROWSER_WINDOW_TAB,
|
2013-02-14 22:21:11 +04:00
|
|
|
url,
|
|
|
|
NULL,
|
|
|
|
bw,
|
|
|
|
NULL);
|
|
|
|
nsurl_unref(url);
|
|
|
|
}
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
warn_user(messages_get_errorcode(error), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(urltxt);
|
2009-12-18 02:55:02 +03:00
|
|
|
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)
|
|
|
|
{
|
2011-02-16 01:58:56 +03:00
|
|
|
/** \todo Implement this properly */
|
2010-04-18 20:07:51 +04:00
|
|
|
|
2011-02-16 01:58:56 +03:00
|
|
|
/* For now, everything is an URL */
|
2009-12-18 02:55:02 +03:00
|
|
|
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;
|
2010-01-02 05:57:18 +03:00
|
|
|
FILE *f;
|
2009-12-18 02:55:02 +03:00
|
|
|
if (search_engines_file_location == NULL)
|
|
|
|
return;
|
2010-01-02 05:57:18 +03:00
|
|
|
f = fopen(search_engines_file_location, "r");
|
2009-12-18 02:55:02 +03:00
|
|
|
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;
|
|
|
|
}
|
2009-12-18 23:15:50 +03:00
|
|
|
fclose(f);
|
2009-12-18 02:55:02 +03:00
|
|
|
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)
|
|
|
|
{
|
2010-04-08 18:14:27 +04:00
|
|
|
#if !defined(WITH_BMP)
|
|
|
|
/* This function is of limited use when no BMP support
|
|
|
|
* is enabled, given the icons it is fetching are BMPs
|
|
|
|
* more often than not. This also avoids an issue where
|
|
|
|
* all this code goes mad if BMP support is not enabled.
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
#else
|
2011-05-07 00:40:09 +04:00
|
|
|
content_type accept = CONTENT_IMAGE;
|
2009-12-18 02:55:02 +03:00
|
|
|
char *url;
|
2010-03-28 16:56:39 +04:00
|
|
|
nserror error;
|
2011-10-03 19:56:47 +04:00
|
|
|
nsurl *icon_nsurl;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2009-12-18 02:55:02 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url == NULL) {
|
|
|
|
warn_user(messages_get("NoMemory"), 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-03 19:56:47 +04:00
|
|
|
error = nsurl_create(url, &icon_nsurl);
|
|
|
|
if (error != NSERROR_OK) {
|
|
|
|
free(url);
|
|
|
|
search_ico = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
error = hlcache_handle_retrieve(icon_nsurl, 0, NULL, NULL,
|
2010-04-04 21:17:24 +04:00
|
|
|
search_web_ico_callback, NULL, NULL, accept,
|
|
|
|
&search_ico);
|
2011-10-03 19:56:47 +04:00
|
|
|
|
|
|
|
nsurl_unref(icon_nsurl);
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
if (error != NSERROR_OK)
|
|
|
|
search_ico = NULL;
|
|
|
|
|
|
|
|
free(url);
|
2010-04-08 18:14:27 +04:00
|
|
|
#endif /* WITH_BMP */
|
2009-12-18 02:55:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns a reference to the static global search_ico [ / NULL]
|
|
|
|
* caller may adjust ico's settings; clearing / free()ing is the core's
|
|
|
|
* responsibility
|
|
|
|
*/
|
|
|
|
|
2010-03-28 16:56:39 +04:00
|
|
|
hlcache_handle *search_web_ico(void)
|
2009-12-18 02:55:02 +03:00
|
|
|
{
|
|
|
|
return search_ico;
|
|
|
|
}
|
|
|
|
|
2011-01-20 16:51:41 +03:00
|
|
|
/**
|
|
|
|
* Cleans up any remaining resources during shutdown.
|
|
|
|
*/
|
|
|
|
void search_web_cleanup(void)
|
|
|
|
{
|
|
|
|
if (search_ico != NULL) {
|
|
|
|
hlcache_handle_release(search_ico);
|
|
|
|
search_ico = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-18 02:55:02 +03:00
|
|
|
/**
|
|
|
|
* callback function to cache ico then notify front when successful
|
|
|
|
* else retry default from local file system
|
|
|
|
*/
|
|
|
|
|
2010-04-08 18:14:27 +04:00
|
|
|
#ifdef WITH_BMP
|
2010-03-28 16:56:39 +04:00
|
|
|
nserror search_web_ico_callback(hlcache_handle *ico,
|
|
|
|
const hlcache_event *event, void *pw)
|
2009-12-18 02:55:02 +03:00
|
|
|
{
|
2010-03-28 16:56:39 +04:00
|
|
|
switch (event->type) {
|
2009-12-18 02:55:02 +03:00
|
|
|
|
|
|
|
case CONTENT_MSG_DONE:
|
2011-12-04 18:55:23 +04:00
|
|
|
LOG(("got favicon '%s'", nsurl_access(hlcache_handle_get_url(ico))));
|
2011-05-07 00:40:09 +04:00
|
|
|
gui_window_set_search_ico(search_ico);
|
2009-12-18 02:55:02 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONTENT_MSG_ERROR:
|
2011-12-04 18:56:54 +04:00
|
|
|
LOG(("favicon %s error: %s",
|
|
|
|
nsurl_access(hlcache_handle_get_url(ico)),
|
|
|
|
event->data.error));
|
2010-03-28 16:56:39 +04:00
|
|
|
hlcache_handle_release(search_ico);
|
|
|
|
search_ico = NULL;
|
2009-12-18 02:55:02 +03:00
|
|
|
search_web_retrieve_ico(true);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
2014-01-05 18:39:08 +04:00
|
|
|
break;
|
2009-12-18 02:55:02 +03:00
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
return NSERROR_OK;
|
2009-12-18 02:55:02 +03:00
|
|
|
}
|
2010-04-08 18:14:27 +04:00
|
|
|
#endif /* WITH_BMP */
|