2010-10-05 23:14:46 +04:00
|
|
|
/*
|
|
|
|
* Copyright 2010 Stephen Fryatt <stevef@netsurf-browser.org>
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* URL Suggestion Menu (implementation).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include "oslib/wimp.h"
|
|
|
|
#include "content/content_type.h"
|
|
|
|
#include "content/urldb.h"
|
|
|
|
#include "riscos/menus.h"
|
|
|
|
#include "riscos/url_suggest.h"
|
|
|
|
#include "utils/messages.h"
|
|
|
|
|
|
|
|
struct url_suggest_item {
|
|
|
|
const char *url; /*< The URL being stored. */
|
|
|
|
unsigned int weight; /*< A weight assigned to the URL. */
|
|
|
|
struct url_suggest_item *next; /*< The next URL in the list. */
|
|
|
|
};
|
|
|
|
|
2012-10-11 14:20:02 +04:00
|
|
|
static bool ro_gui_url_suggest_callback(nsurl *url,
|
2011-02-21 02:16:33 +03:00
|
|
|
const struct url_data *data);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
static int suggest_entries;
|
|
|
|
static time_t suggest_time;
|
|
|
|
static struct url_suggest_item *suggest_list;
|
|
|
|
|
2011-02-21 02:16:33 +03:00
|
|
|
static wimp_MENU(URL_SUGGEST_MAX_URLS) url_suggest_menu_block;
|
|
|
|
wimp_menu *ro_gui_url_suggest_menu = (wimp_menu *) &url_suggest_menu_block;
|
|
|
|
|
|
|
|
|
2010-10-05 23:14:46 +04:00
|
|
|
/**
|
2011-02-21 02:16:33 +03:00
|
|
|
* Initialise the URL suggestion menu. This MUST be called before anything
|
|
|
|
* tries to use the URL menu.
|
2010-10-05 23:14:46 +04:00
|
|
|
*
|
|
|
|
* /return true if initialisation was OK; else false.
|
|
|
|
*/
|
|
|
|
|
2011-02-21 02:16:33 +03:00
|
|
|
bool ro_gui_url_suggest_init(void)
|
2010-10-05 23:14:46 +04:00
|
|
|
{
|
2011-02-21 02:16:33 +03:00
|
|
|
ro_gui_url_suggest_menu->title_data.indirected_text.text =
|
2010-10-05 23:14:46 +04:00
|
|
|
(char *) messages_get("URLSuggest");
|
2011-02-21 02:16:33 +03:00
|
|
|
ro_gui_menu_init_structure((wimp_menu *) ro_gui_url_suggest_menu,
|
2010-10-05 23:14:46 +04:00
|
|
|
URL_SUGGEST_MAX_URLS);
|
|
|
|
|
|
|
|
suggest_entries = 0;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-02-21 02:16:33 +03:00
|
|
|
/**
|
|
|
|
* Check if there is a URL suggestion menu available for use.
|
|
|
|
*
|
|
|
|
* \TODO -- Ideally this should be able to decide if there's a menu
|
|
|
|
* available without actually having to build it all.
|
|
|
|
*
|
|
|
|
* /return true if the menu has entries; else false.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool ro_gui_url_suggest_get_menu_available(void)
|
|
|
|
{
|
|
|
|
return ro_gui_url_suggest_prepare_menu();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-05 23:14:46 +04:00
|
|
|
/**
|
|
|
|
* Builds the URL suggestion menu. This is called by ro_gui_menu_create() when
|
|
|
|
* it is asked to display the url_suggest_menu.
|
|
|
|
*
|
|
|
|
* /return true if the menu has entries; else false.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool ro_gui_url_suggest_prepare_menu(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct url_suggest_item *list, *next;
|
|
|
|
|
|
|
|
/* Fetch the URLs we want to include from URLdb. */
|
|
|
|
|
|
|
|
suggest_entries = 0;
|
|
|
|
suggest_list = NULL;
|
|
|
|
suggest_time = time(NULL);
|
|
|
|
|
2011-02-21 02:16:33 +03:00
|
|
|
urldb_iterate_entries(ro_gui_url_suggest_callback);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
|
|
|
/* If any menu entries were found, put them into the menu. The list
|
|
|
|
* is in reverse order, last to first, so the menu is filled backwards.
|
|
|
|
* Entries from the list are freed as we go.
|
|
|
|
*/
|
|
|
|
|
|
|
|
assert(suggest_entries <= URL_SUGGEST_MAX_URLS);
|
|
|
|
|
|
|
|
if (suggest_entries > 0) {
|
|
|
|
i = suggest_entries;
|
|
|
|
|
|
|
|
list = suggest_list;
|
|
|
|
suggest_list = NULL;
|
|
|
|
|
|
|
|
while (list != NULL && i > 0) {
|
|
|
|
i--;
|
|
|
|
|
2011-02-21 02:16:33 +03:00
|
|
|
ro_gui_url_suggest_menu->entries[i].menu_flags = 0;
|
|
|
|
ro_gui_url_suggest_menu->
|
|
|
|
entries[i].data.indirected_text.text =
|
2010-10-05 23:14:46 +04:00
|
|
|
(char *) list->url;
|
2011-02-21 02:16:33 +03:00
|
|
|
ro_gui_url_suggest_menu->
|
|
|
|
entries[i].data.indirected_text.size =
|
2010-10-05 23:14:46 +04:00
|
|
|
strlen(list->url) + 1;
|
|
|
|
|
|
|
|
next = list->next;
|
|
|
|
free(list);
|
|
|
|
list = next;
|
|
|
|
}
|
|
|
|
|
2010-10-10 13:13:18 +04:00
|
|
|
assert(i == 0);
|
2010-10-05 23:14:46 +04:00
|
|
|
|
2011-02-21 02:16:33 +03:00
|
|
|
ro_gui_url_suggest_menu->entries[0].menu_flags |=
|
2010-10-05 23:14:46 +04:00
|
|
|
wimp_MENU_TITLE_INDIRECTED;
|
2011-02-21 02:16:33 +03:00
|
|
|
ro_gui_url_suggest_menu->
|
|
|
|
entries[suggest_entries - 1].menu_flags |=
|
2010-10-05 23:14:46 +04:00
|
|
|
wimp_MENU_LAST;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback function for urldb_iterate_entries
|
|
|
|
*
|
|
|
|
* \param url URL which matches
|
|
|
|
* \param data Data associated with URL
|
|
|
|
* \return true to continue iteration, false otherwise
|
|
|
|
*/
|
|
|
|
|
2012-10-11 14:20:02 +04:00
|
|
|
bool ro_gui_url_suggest_callback(nsurl *url, const struct url_data *data)
|
2010-10-05 23:14:46 +04:00
|
|
|
{
|
|
|
|
int count;
|
|
|
|
unsigned int weight;
|
|
|
|
struct url_suggest_item **list, *new, *old;
|
|
|
|
|
|
|
|
/* Ignore unvisited URLs, and those that don't apply to HTML or Text. */
|
|
|
|
|
2010-10-10 13:13:18 +04:00
|
|
|
if (data->visits <= 0 || (data->type != CONTENT_HTML &&
|
2010-10-05 23:14:46 +04:00
|
|
|
data->type != CONTENT_TEXTPLAIN))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Calculate a weight for the URL. */
|
|
|
|
|
|
|
|
weight = (suggest_time - data->last_visit) / data->visits;
|
|
|
|
|
|
|
|
/* Hunt through those URLs already found to see if we want to add
|
|
|
|
* this one. Smaller weights carry higher priority.
|
|
|
|
*
|
|
|
|
* The list is sorted into reverse order, so that lowest weight
|
|
|
|
* items are nearest the head. Therefore, items are dropped from
|
|
|
|
* the head, making things simpler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
list = &suggest_list;
|
|
|
|
count = 0;
|
|
|
|
|
|
|
|
while (*list != NULL && weight < (*list)->weight) {
|
|
|
|
list = &((*list)->next);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count > 0 || suggest_entries < URL_SUGGEST_MAX_URLS) {
|
|
|
|
new = (struct url_suggest_item *)
|
|
|
|
malloc(sizeof(struct url_suggest_item));
|
|
|
|
|
|
|
|
if (new != NULL) {
|
|
|
|
suggest_entries++;
|
2012-10-11 14:20:02 +04:00
|
|
|
/* TODO: keeping pointers to URLdb data is bad.
|
|
|
|
* should be nsurl_ref(url) or
|
|
|
|
* take a copy of the string. */
|
|
|
|
new->url = nsurl_access(url);
|
2010-10-05 23:14:46 +04:00
|
|
|
new->weight = weight;
|
|
|
|
new->next = *list;
|
|
|
|
|
|
|
|
*list = new;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If adding the URL gave us too many menu items, drop the lowest
|
|
|
|
* priority ones until the list is the right length again.
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (suggest_list != NULL && suggest_entries > URL_SUGGEST_MAX_URLS) {
|
|
|
|
old = suggest_list;
|
|
|
|
suggest_list = suggest_list->next;
|
2012-10-11 14:20:02 +04:00
|
|
|
|
2010-10-05 23:14:46 +04:00
|
|
|
free(old);
|
|
|
|
suggest_entries--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-02-21 02:16:33 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Process a selection from the URL Suggest menu.
|
|
|
|
*
|
|
|
|
* \param *selection The menu selection.
|
|
|
|
* \return Pointer to the URL that was selected, or NULL for none.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const char *ro_gui_url_suggest_get_selection(wimp_selection *selection)
|
|
|
|
{
|
2012-10-11 14:20:02 +04:00
|
|
|
const char *url = NULL;
|
2011-02-21 02:16:33 +03:00
|
|
|
|
|
|
|
if (selection->items[0] >= 0)
|
|
|
|
url = ro_gui_url_suggest_menu->entries[selection->items[0]].
|
|
|
|
data.indirected_text.text;
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|