2009-12-18 02:55:02 +03:00
|
|
|
/*
|
2014-05-25 03:57:48 +04:00
|
|
|
* Copyright 2014 Vincent Sanders <vince@netsurf-browser.org>
|
2009-12-18 02:55:02 +03:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2014-05-25 03:57:48 +04:00
|
|
|
|
|
|
|
/**
|
2019-08-04 13:22:40 +03:00
|
|
|
* \file
|
2014-05-25 03:57:48 +04:00
|
|
|
* \brief core web search facilities implementation.
|
2009-12-18 02:55:02 +03:00
|
|
|
*/
|
|
|
|
|
2016-04-19 01:04:16 +03:00
|
|
|
#include <stdlib.h>
|
2020-06-25 01:49:59 +03:00
|
|
|
#include <string.h>
|
2016-04-19 01:04:16 +03:00
|
|
|
|
|
|
|
#include "utils/utils.h"
|
2014-05-25 03:57:48 +04:00
|
|
|
#include "utils/log.h"
|
|
|
|
#include "utils/url.h"
|
|
|
|
#include "utils/nsoption.h"
|
2016-06-06 10:59:23 +03:00
|
|
|
#include "netsurf/content.h"
|
2010-03-28 16:56:39 +04:00
|
|
|
#include "content/hlcache.h"
|
2014-05-25 03:57:48 +04:00
|
|
|
|
2009-12-18 02:55:02 +03:00
|
|
|
#include "desktop/searchweb.h"
|
2014-10-16 12:48:09 +04:00
|
|
|
#include "desktop/gui_internal.h"
|
2009-12-18 02:55:02 +03:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
struct search_provider {
|
2009-12-18 02:55:02 +03:00
|
|
|
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 */
|
2014-05-25 03:57:48 +04:00
|
|
|
hlcache_handle *ico_handle;
|
|
|
|
};
|
2009-12-18 02:55:02 +03:00
|
|
|
|
2014-06-03 18:01:14 +04:00
|
|
|
static struct search_web_ctx_s {
|
2014-05-25 03:57:48 +04:00
|
|
|
struct search_provider *providers; /* web search providers */
|
|
|
|
size_t providers_count; /* number of providers */
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
size_t current; /* current provider */
|
2009-12-18 02:55:02 +03:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
hlcache_handle *default_ico_handle;
|
2013-02-14 22:21:11 +04:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
} search_web_ctx;
|
2009-12-18 02:55:02 +03:00
|
|
|
|
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
static const char *default_providers = "Google|www.google.com|http://www.google.com/search?q=%s|http://www.google.com/favicon.ico|\n";
|
2010-04-18 20:07:51 +04:00
|
|
|
|
2014-06-03 18:01:14 +04:00
|
|
|
static const char *default_search_icon_url = "resource:icons/search.png";
|
2009-12-18 02:55:02 +03:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2014-05-25 03:57:48 +04:00
|
|
|
* Read providers file.
|
|
|
|
*
|
2016-11-21 03:00:32 +03:00
|
|
|
* Allocates storage of sufficient size for the providers file and
|
2014-05-25 03:57:48 +04:00
|
|
|
* reads the entire file in.
|
|
|
|
*
|
|
|
|
* \param fname The filename to read.
|
|
|
|
* \param providers_out A pointer to place the result buffer in.
|
|
|
|
* \param providers_size_out Size of buffer.
|
|
|
|
* \return NSERROR_OK and providers_out updated or appropriate error code.
|
2009-12-18 02:55:02 +03:00
|
|
|
*/
|
2014-05-25 03:57:48 +04:00
|
|
|
static nserror
|
|
|
|
read_providers(const char *fname,
|
|
|
|
char **providers_out,
|
|
|
|
size_t *providers_size_out)
|
2009-12-18 02:55:02 +03:00
|
|
|
{
|
2014-05-25 03:57:48 +04:00
|
|
|
FILE *providersf;
|
|
|
|
long ftellsize;
|
|
|
|
size_t fsize;
|
|
|
|
char *providersd;
|
|
|
|
|
|
|
|
if (fname == NULL) {
|
|
|
|
return NSERROR_BAD_PARAMETER;
|
|
|
|
}
|
2009-12-18 02:55:02 +03:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
providersf = fopen(fname, "r");
|
|
|
|
if (providersf == NULL) {
|
|
|
|
return NSERROR_NOT_FOUND;
|
|
|
|
}
|
2009-12-18 02:55:02 +03:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
if (fseek(providersf, 0, SEEK_END) != 0) {
|
|
|
|
fclose(providersf);
|
|
|
|
return NSERROR_INVALID;
|
|
|
|
}
|
2009-12-18 02:55:02 +03:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
ftellsize = ftell(providersf);
|
|
|
|
if (ftellsize < 0) {
|
|
|
|
fclose(providersf);
|
|
|
|
return NSERROR_INVALID;
|
|
|
|
}
|
|
|
|
fsize = ftellsize;
|
2009-12-18 02:55:02 +03:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
if (fseek(providersf, 0, SEEK_SET) != 0) {
|
|
|
|
fclose(providersf);
|
|
|
|
return NSERROR_INVALID;
|
|
|
|
}
|
2009-12-18 02:55:02 +03:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
providersd = malloc(fsize + 1);
|
|
|
|
if (providersd == NULL) {
|
|
|
|
fclose(providersf);
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
2009-12-18 02:55:02 +03:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
if (fread(providersd, 1, fsize, providersf) != fsize) {
|
|
|
|
fclose(providersf);
|
|
|
|
free(providersd);
|
2014-05-26 17:03:20 +04:00
|
|
|
return NSERROR_BAD_SIZE;
|
2014-05-25 03:57:48 +04:00
|
|
|
}
|
|
|
|
providersd[fsize] = 0; /* ensure null terminated */
|
|
|
|
|
|
|
|
fclose(providersf);
|
|
|
|
|
|
|
|
*providers_out = providersd;
|
|
|
|
*providers_size_out = fsize;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
2009-12-18 02:55:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-25 03:57:48 +04:00
|
|
|
* parse search providers from a memory block.
|
|
|
|
*
|
2014-06-03 18:01:14 +04:00
|
|
|
* \param providersd The provider info data.
|
2014-05-25 03:57:48 +04:00
|
|
|
* \param providers_size The size of the provider data.
|
|
|
|
* \param providers_out The resulting provider array.
|
|
|
|
* \param providers_count The number of providers in the output array.
|
2016-11-21 03:00:32 +03:00
|
|
|
* \return NSERROR_OK on success or error code on failure.
|
2009-12-18 02:55:02 +03:00
|
|
|
*/
|
2014-05-25 03:57:48 +04:00
|
|
|
static nserror
|
|
|
|
parse_providers(char *providersd,
|
|
|
|
size_t providers_size,
|
|
|
|
struct search_provider **providers_out,
|
|
|
|
size_t *providers_count)
|
2009-12-18 02:55:02 +03:00
|
|
|
{
|
2014-05-25 03:57:48 +04:00
|
|
|
size_t pcount = 0; /* number of providers */
|
|
|
|
size_t pidx;
|
|
|
|
char *nl = providersd;
|
|
|
|
struct search_provider *providers;
|
|
|
|
|
|
|
|
/* count newlines */
|
|
|
|
while (nl != NULL) {
|
|
|
|
nl = strchr(nl, '\n');
|
|
|
|
if (nl != NULL) {
|
|
|
|
nl++;
|
|
|
|
pcount+=1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pcount == 0) {
|
|
|
|
return NSERROR_INVALID;
|
2009-12-18 02:55:02 +03:00
|
|
|
}
|
2014-05-25 03:57:48 +04:00
|
|
|
|
|
|
|
providers = malloc(pcount * sizeof(*providers));
|
|
|
|
if (providers == NULL) {
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
nl = providersd;
|
|
|
|
for (pidx = 0; pidx < pcount; pidx++) {
|
|
|
|
providers[pidx].name = nl;
|
|
|
|
nl = strchr(nl, '|');
|
|
|
|
if (nl == NULL) {
|
|
|
|
free(providers);
|
|
|
|
return NSERROR_INVALID;
|
|
|
|
}
|
|
|
|
*nl = 0;
|
|
|
|
nl++;
|
|
|
|
|
|
|
|
providers[pidx].hostname = nl;
|
|
|
|
nl = strchr(nl, '|');
|
|
|
|
if (nl == NULL) {
|
|
|
|
free(providers);
|
|
|
|
return NSERROR_INVALID;
|
|
|
|
}
|
|
|
|
*nl = 0;
|
|
|
|
nl++;
|
|
|
|
|
|
|
|
providers[pidx].searchstring = nl;
|
|
|
|
nl = strchr(nl, '|');
|
|
|
|
if (nl == NULL) {
|
|
|
|
free(providers);
|
|
|
|
return NSERROR_INVALID;
|
|
|
|
}
|
|
|
|
*nl = 0;
|
|
|
|
nl++;
|
|
|
|
|
|
|
|
providers[pidx].ico = nl;
|
|
|
|
nl = strchr(nl, '|');
|
|
|
|
if (nl == NULL) {
|
|
|
|
free(providers);
|
|
|
|
return NSERROR_INVALID;
|
|
|
|
}
|
|
|
|
*nl = 0;
|
|
|
|
nl++;
|
|
|
|
|
|
|
|
/* skip newline */
|
|
|
|
nl = strchr(nl, '\n');
|
|
|
|
if (nl == NULL) {
|
|
|
|
free(providers);
|
|
|
|
return NSERROR_INVALID;
|
|
|
|
}
|
|
|
|
nl++;
|
|
|
|
|
|
|
|
providers[pidx].ico_handle = NULL;
|
2009-12-18 02:55:02 +03:00
|
|
|
}
|
2014-05-25 03:57:48 +04:00
|
|
|
|
|
|
|
*providers_out = providers;
|
|
|
|
*providers_count = pcount;
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
2009-12-18 02:55:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-25 03:57:48 +04:00
|
|
|
* create a url for a search provider and a term
|
|
|
|
*
|
2014-11-10 00:49:33 +03:00
|
|
|
* \param provider The provider to use.
|
2014-05-25 03:57:48 +04:00
|
|
|
* \param term The term being searched for.
|
|
|
|
* \param url_out The resulting url.
|
2016-11-21 03:00:32 +03:00
|
|
|
* \return NSERROR_OK on success or appropriate error code.
|
2009-12-18 02:55:02 +03:00
|
|
|
*/
|
2014-05-25 03:57:48 +04:00
|
|
|
static nserror
|
|
|
|
make_search_nsurl(struct search_provider *provider,
|
|
|
|
const char *term,
|
|
|
|
nsurl **url_out)
|
2009-12-18 02:55:02 +03:00
|
|
|
{
|
2014-05-25 03:57:48 +04:00
|
|
|
nserror ret;
|
|
|
|
nsurl *url;
|
|
|
|
char *eterm; /* escaped term */
|
|
|
|
char *searchstr; /* the providers search string */
|
|
|
|
char *urlstr; /* the escaped term substituted into the provider */
|
|
|
|
char *urlstro;
|
|
|
|
size_t urlstr_len;
|
|
|
|
|
|
|
|
/* escape the search term and join it to the search url */
|
2016-07-24 23:00:29 +03:00
|
|
|
ret = url_escape(term, true, NULL, &eterm);
|
2014-05-25 03:57:48 +04:00
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
return ret;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
searchstr = provider->searchstring;
|
|
|
|
|
|
|
|
urlstr_len = strlen(searchstr) + strlen(eterm) + 1;
|
|
|
|
urlstro = urlstr = malloc(urlstr_len);
|
|
|
|
if (urlstr == NULL) {
|
|
|
|
free(eterm);
|
|
|
|
return NSERROR_NOMEM;
|
2009-12-18 02:55:02 +03:00
|
|
|
}
|
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
/* composite search url */
|
|
|
|
for ( ; *searchstr != 0; searchstr++, urlstro++) {
|
|
|
|
*urlstro = *searchstr;
|
|
|
|
if ((*searchstr == '%') && (searchstr[1] == 's')) {
|
|
|
|
searchstr++; /* skip % */
|
|
|
|
memcpy(urlstro, eterm, strlen(eterm));
|
|
|
|
urlstro += strlen(eterm) - 1;
|
|
|
|
}
|
2009-12-18 02:55:02 +03:00
|
|
|
}
|
2014-05-25 03:57:48 +04:00
|
|
|
free(eterm);
|
2014-05-25 18:02:30 +04:00
|
|
|
*urlstro = '\0'; /* ensure string is NULL-terminated */
|
2009-12-18 02:55:02 +03:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
ret = nsurl_create(urlstr, &url);
|
|
|
|
free(urlstr);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
return ret;
|
2011-10-03 19:56:47 +04:00
|
|
|
}
|
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
*url_out = url;
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
2011-10-03 19:56:47 +04:00
|
|
|
|
2014-06-03 18:01:14 +04:00
|
|
|
/**
|
|
|
|
* callback for hlcache icon fetch events.
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
search_web_ico_callback(hlcache_handle *ico,
|
|
|
|
const hlcache_event *event,
|
|
|
|
void *pw)
|
|
|
|
{
|
|
|
|
struct search_provider *provider = pw;
|
|
|
|
|
|
|
|
switch (event->type) {
|
|
|
|
|
|
|
|
case CONTENT_MSG_DONE:
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
NSLOG(netsurf, INFO, "icon '%s' retrieved",
|
|
|
|
nsurl_access(hlcache_handle_get_url(ico)));
|
2014-06-03 18:01:14 +04:00
|
|
|
guit->search_web->provider_update(provider->name,
|
|
|
|
content_get_bitmap(ico));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CONTENT_MSG_ERROR:
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
NSLOG(netsurf, INFO, "icon %s error: %s",
|
|
|
|
nsurl_access(hlcache_handle_get_url(ico)),
|
2019-08-05 16:29:53 +03:00
|
|
|
event->data.errordata.errormsg);
|
2017-09-11 18:43:25 +03:00
|
|
|
|
2014-06-03 18:01:14 +04:00
|
|
|
hlcache_handle_release(ico);
|
|
|
|
/* clear reference to released handle */
|
|
|
|
provider->ico_handle = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
/* exported interface documented in desktop/searchweb.h */
|
|
|
|
nserror
|
|
|
|
search_web_omni(const char *term,
|
|
|
|
enum search_web_omni_flags flags,
|
|
|
|
struct nsurl **url_out)
|
|
|
|
{
|
|
|
|
nserror ret;
|
|
|
|
nsurl *url;
|
|
|
|
char *eterm; /* encoded/altered search term */
|
|
|
|
|
|
|
|
if ((flags & SEARCH_WEB_OMNI_SEARCHONLY) == 0) {
|
|
|
|
|
|
|
|
/* first check to see if the term is a url */
|
|
|
|
ret = nsurl_create(term, &url);
|
|
|
|
if (ret == NSERROR_OK) {
|
|
|
|
*url_out = url;
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try with adding default scheme */
|
|
|
|
eterm = malloc(strlen(term) + SLEN("http://") + 1);
|
|
|
|
if (eterm == NULL) {
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
sprintf(eterm, "http://%s", term);
|
|
|
|
ret = nsurl_create(eterm, &url);
|
|
|
|
free(eterm);
|
|
|
|
if (ret == NSERROR_OK) {
|
|
|
|
*url_out = url;
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do not pass to search if user has disabled the option */
|
|
|
|
if (nsoption_bool(search_url_bar) == false) {
|
|
|
|
return NSERROR_BAD_URL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* must be initialised */
|
|
|
|
if (search_web_ctx.providers == NULL) {
|
|
|
|
return NSERROR_INIT_FAILED;
|
|
|
|
}
|
2011-10-03 19:56:47 +04:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
/* turn search into a nsurl */
|
|
|
|
ret = make_search_nsurl(&search_web_ctx.providers[search_web_ctx.current], term, &url);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
return ret;
|
|
|
|
}
|
2010-03-28 16:56:39 +04:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
*url_out = url;
|
|
|
|
return NSERROR_OK;
|
2009-12-18 02:55:02 +03:00
|
|
|
}
|
|
|
|
|
2019-09-01 01:53:51 +03:00
|
|
|
/* exported interface documented in desktop/searchweb.h */
|
|
|
|
nserror search_web_get_provider_bitmap(struct bitmap **bitmap_out)
|
|
|
|
{
|
|
|
|
struct search_provider *provider;
|
|
|
|
struct bitmap *ico_bitmap = NULL;
|
|
|
|
|
|
|
|
/* must be initialised */
|
|
|
|
if (search_web_ctx.providers == NULL) {
|
|
|
|
return NSERROR_INIT_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
provider = &search_web_ctx.providers[search_web_ctx.current];
|
|
|
|
|
|
|
|
/* set the icon now (if we can) at least to the default */
|
|
|
|
if (provider->ico_handle != NULL) {
|
|
|
|
ico_bitmap = content_get_bitmap(provider->ico_handle);
|
|
|
|
}
|
|
|
|
if ((ico_bitmap == NULL) &&
|
|
|
|
(search_web_ctx.default_ico_handle != NULL)) {
|
|
|
|
ico_bitmap = content_get_bitmap(search_web_ctx.default_ico_handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
*bitmap_out = ico_bitmap;
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
/* exported interface documented in desktop/searchweb.h */
|
|
|
|
nserror search_web_select_provider(int selection)
|
2009-12-18 02:55:02 +03:00
|
|
|
{
|
2014-05-25 03:57:48 +04:00
|
|
|
struct search_provider *provider;
|
|
|
|
struct bitmap *ico_bitmap = NULL;
|
|
|
|
|
|
|
|
/* must be initialised */
|
|
|
|
if (search_web_ctx.providers == NULL) {
|
|
|
|
return NSERROR_INIT_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* negative value just selects whatevers current */
|
|
|
|
if (selection >= 0) {
|
|
|
|
/* ensure selection lies within acceptable range */
|
|
|
|
if ((size_t)selection < search_web_ctx.providers_count) {
|
|
|
|
search_web_ctx.current = selection;
|
|
|
|
} else {
|
|
|
|
/* out of range */
|
|
|
|
search_web_ctx.current = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
provider = &search_web_ctx.providers[search_web_ctx.current];
|
|
|
|
|
|
|
|
/* set the icon now (if we can) at least to the default */
|
|
|
|
if (provider->ico_handle != NULL) {
|
|
|
|
ico_bitmap = content_get_bitmap(provider->ico_handle);
|
|
|
|
}
|
2014-05-27 14:39:02 +04:00
|
|
|
if ((ico_bitmap == NULL) &&
|
|
|
|
(search_web_ctx.default_ico_handle != NULL)) {
|
2014-05-25 03:57:48 +04:00
|
|
|
ico_bitmap = content_get_bitmap(search_web_ctx.default_ico_handle);
|
|
|
|
}
|
2014-05-27 14:39:02 +04:00
|
|
|
/* update the callback with the provider change. Bitmap may
|
2014-05-25 03:57:48 +04:00
|
|
|
* be NULL at this point.
|
|
|
|
*/
|
|
|
|
guit->search_web->provider_update(provider->name, ico_bitmap);
|
|
|
|
|
|
|
|
|
2016-11-21 03:00:32 +03:00
|
|
|
/* if the providers icon has not been retrieved get it now */
|
2014-05-25 03:57:48 +04:00
|
|
|
if (provider->ico_handle == NULL) {
|
|
|
|
nsurl *icon_nsurl;
|
2014-05-27 14:39:02 +04:00
|
|
|
nserror ret;
|
2014-05-25 03:57:48 +04:00
|
|
|
|
|
|
|
/* create search icon url */
|
|
|
|
ret = nsurl_create(provider->ico, &icon_nsurl);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = hlcache_handle_retrieve(icon_nsurl, 0, NULL, NULL,
|
|
|
|
search_web_ico_callback,
|
2014-06-03 18:01:14 +04:00
|
|
|
provider,
|
2014-05-25 03:57:48 +04:00
|
|
|
NULL, CONTENT_IMAGE,
|
|
|
|
&provider->ico_handle);
|
|
|
|
nsurl_unref(icon_nsurl);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
provider->ico_handle = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
2009-12-18 02:55:02 +03:00
|
|
|
}
|
|
|
|
|
2014-06-03 18:01:14 +04:00
|
|
|
/**
|
|
|
|
* callback for hlcache icon fetch events.
|
|
|
|
*/
|
|
|
|
static nserror
|
|
|
|
default_ico_callback(hlcache_handle *ico,
|
|
|
|
const hlcache_event *event,
|
|
|
|
void *pw)
|
|
|
|
{
|
|
|
|
struct search_web_ctx_s *ctx = pw;
|
|
|
|
|
|
|
|
switch (event->type) {
|
|
|
|
|
|
|
|
case CONTENT_MSG_DONE:
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
NSLOG(netsurf, INFO, "default icon '%s' retrieved",
|
|
|
|
nsurl_access(hlcache_handle_get_url(ico)));
|
2014-06-03 18:01:14 +04:00
|
|
|
|
|
|
|
/* only set to default icon if providers icon has no handle */
|
|
|
|
if (ctx->providers[search_web_ctx.current].ico_handle == NULL) {
|
|
|
|
guit->search_web->provider_update(
|
|
|
|
ctx->providers[search_web_ctx.current].name,
|
|
|
|
content_get_bitmap(ico));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CONTENT_MSG_ERROR:
|
Use coccinelle to change logging macro calls in c files
for F in $(git ls-files '*.c');do spatch --sp-file foo.cocci --in-place ${F};done
@@ expression E; @@
-LOG(E);
+NSLOG(netsurf, INFO, E);
@@ expression E, E1; @@
-LOG(E, E1);
+NSLOG(netsurf, INFO, E, E1);
@@ expression E, E1, E2; @@
-LOG(E, E1, E2);
+NSLOG(netsurf, INFO, E, E1, E2);
@@ expression E, E1, E2, E3; @@
-LOG(E, E1, E2, E3);
+NSLOG(netsurf, INFO, E, E1, E2, E3);
@@ expression E, E1, E2, E3, E4; @@
-LOG(E, E1, E2, E3, E4);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4);
@@ expression E, E1, E2, E3, E4, E5; @@
-LOG(E, E1, E2, E3, E4, E5);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5);
@@ expression E, E1, E2, E3, E4, E5, E6; @@
-LOG(E, E1, E2, E3, E4, E5, E6);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6);
@@ expression E, E1, E2, E3, E4, E5, E6, E7; @@
-LOG(E, E1, E2, E3, E4, E5, E6, E7);
+NSLOG(netsurf, INFO, E, E1, E2, E3, E4, E5, E6, E7);
2017-09-06 20:28:12 +03:00
|
|
|
NSLOG(netsurf, INFO, "icon %s error: %s",
|
|
|
|
nsurl_access(hlcache_handle_get_url(ico)),
|
2019-08-05 16:29:53 +03:00
|
|
|
event->data.errordata.errormsg);
|
2017-09-11 18:43:25 +03:00
|
|
|
|
2014-06-03 18:01:14 +04:00
|
|
|
hlcache_handle_release(ico);
|
|
|
|
/* clear reference to released handle */
|
|
|
|
ctx->default_ico_handle = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-03 18:40:28 +04:00
|
|
|
/* exported interface documented in desktop/searchweb.h */
|
|
|
|
ssize_t search_web_iterate_providers(ssize_t from, const char **name)
|
|
|
|
{
|
|
|
|
if (from < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((size_t)from >= search_web_ctx.providers_count)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
*name = search_web_ctx.providers[from].name;
|
|
|
|
|
|
|
|
return from + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
/* exported interface documented in desktop/searchweb.h */
|
|
|
|
nserror search_web_init(const char *provider_fname)
|
2011-01-20 16:51:41 +03:00
|
|
|
{
|
2014-05-25 03:57:48 +04:00
|
|
|
nserror ret;
|
|
|
|
char *providers;
|
|
|
|
size_t providers_size;
|
|
|
|
nsurl *icon_nsurl;
|
|
|
|
|
|
|
|
/* create search icon url */
|
|
|
|
ret = nsurl_create(default_search_icon_url, &icon_nsurl);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
return ret;
|
2011-01-20 16:51:41 +03:00
|
|
|
}
|
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
/* get a list of providers */
|
|
|
|
ret = read_providers(provider_fname, &providers, &providers_size);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
providers = strdup(default_providers);
|
|
|
|
if (providers == NULL) {
|
|
|
|
return NSERROR_NOMEM;
|
|
|
|
}
|
|
|
|
providers_size = strlen(providers);
|
|
|
|
}
|
2009-12-18 02:55:02 +03:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
/* parse list of providers */
|
|
|
|
ret = parse_providers(providers,
|
|
|
|
providers_size,
|
|
|
|
&search_web_ctx.providers,
|
|
|
|
&search_web_ctx.providers_count);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
free(providers);
|
|
|
|
return ret;
|
|
|
|
}
|
2009-12-18 02:55:02 +03:00
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
/* get default search icon */
|
2019-09-01 01:53:51 +03:00
|
|
|
ret = hlcache_handle_retrieve(icon_nsurl,
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
2014-06-03 18:01:14 +04:00
|
|
|
default_ico_callback,
|
|
|
|
&search_web_ctx,
|
2019-09-01 01:53:51 +03:00
|
|
|
NULL,
|
|
|
|
CONTENT_IMAGE,
|
2014-05-25 03:57:48 +04:00
|
|
|
&search_web_ctx.default_ico_handle);
|
|
|
|
nsurl_unref(icon_nsurl);
|
|
|
|
if (ret != NSERROR_OK) {
|
|
|
|
search_web_ctx.default_ico_handle = NULL;
|
|
|
|
free(search_web_ctx.providers);
|
|
|
|
search_web_ctx.providers = NULL;
|
|
|
|
free(providers);
|
|
|
|
return ret;
|
|
|
|
}
|
2009-12-18 02:55:02 +03:00
|
|
|
|
|
|
|
|
2014-05-25 03:57:48 +04:00
|
|
|
return NSERROR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* exported interface documented in desktop/searchweb.h */
|
|
|
|
nserror search_web_finalise(void)
|
|
|
|
{
|
|
|
|
size_t pidx;
|
|
|
|
|
|
|
|
/* must be initialised */
|
|
|
|
if (search_web_ctx.providers == NULL) {
|
|
|
|
return NSERROR_INIT_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (search_web_ctx.default_ico_handle != NULL) {
|
|
|
|
hlcache_handle_release(search_web_ctx.default_ico_handle);
|
2009-12-18 02:55:02 +03:00
|
|
|
}
|
2014-05-25 03:57:48 +04:00
|
|
|
for (pidx = 0; pidx < search_web_ctx.providers_count; pidx++) {
|
|
|
|
if (search_web_ctx.providers[pidx].ico_handle != NULL) {
|
|
|
|
hlcache_handle_release(search_web_ctx.providers[pidx].ico_handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All the search provider data is held in a single block for
|
|
|
|
* efficiency.
|
|
|
|
*/
|
|
|
|
free(search_web_ctx.providers[0].name);
|
|
|
|
|
|
|
|
free(search_web_ctx.providers);
|
|
|
|
search_web_ctx.providers = NULL;
|
2010-03-28 16:56:39 +04:00
|
|
|
|
|
|
|
return NSERROR_OK;
|
2009-12-18 02:55:02 +03:00
|
|
|
}
|