2005-01-14 01:42:39 +03:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2005 James Bursa <bursa@users.sourceforge.net>
|
2007-08-08 20:16:03 +04: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/>.
|
2005-01-14 01:42:39 +03:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
|
* Theme auto-installing.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <stdbool.h>
|
2014-06-06 02:01:37 +04:00
|
|
|
|
#include <oslib/osfile.h>
|
|
|
|
|
#include <oslib/wimp.h>
|
|
|
|
|
|
|
|
|
|
#include "utils/nsoption.h"
|
|
|
|
|
#include "utils/log.h"
|
|
|
|
|
#include "utils/messages.h"
|
2007-05-31 02:39:54 +04:00
|
|
|
|
#include "content/content.h"
|
2010-04-12 03:04:06 +04:00
|
|
|
|
#include "content/hlcache.h"
|
2014-11-06 17:19:15 +03:00
|
|
|
|
#include "desktop/theme.h"
|
2014-06-06 02:01:37 +04:00
|
|
|
|
|
2007-05-31 02:39:54 +04:00
|
|
|
|
#include "riscos/dialog.h"
|
|
|
|
|
#include "riscos/gui.h"
|
|
|
|
|
#include "riscos/theme.h"
|
|
|
|
|
#include "riscos/wimp.h"
|
|
|
|
|
#include "riscos/wimp_event.h"
|
2005-01-14 01:42:39 +03:00
|
|
|
|
|
|
|
|
|
|
2010-04-12 03:04:06 +04:00
|
|
|
|
static hlcache_handle *theme_install_content = NULL;
|
2005-01-23 19:09:05 +03:00
|
|
|
|
static struct theme_descriptor theme_install_descriptor;
|
2005-01-14 01:42:39 +03:00
|
|
|
|
wimp_w dialog_theme_install;
|
|
|
|
|
|
2005-03-09 03:20:45 +03:00
|
|
|
|
|
2005-12-31 07:40:49 +03:00
|
|
|
|
static void theme_install_close(wimp_w w);
|
2010-04-12 03:04:06 +04:00
|
|
|
|
static nserror theme_install_callback(hlcache_handle *handle,
|
|
|
|
|
const hlcache_event *event, void *pw);
|
|
|
|
|
static bool theme_install_read(const char *source_data,
|
|
|
|
|
unsigned long source_size);
|
2005-01-14 01:42:39 +03:00
|
|
|
|
|
2005-03-09 03:20:45 +03:00
|
|
|
|
|
2005-01-14 01:42:39 +03:00
|
|
|
|
/**
|
|
|
|
|
* Handle a CONTENT_THEME that has started loading.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-04-12 03:04:06 +04:00
|
|
|
|
void theme_install_start(hlcache_handle *c)
|
2005-01-14 01:42:39 +03:00
|
|
|
|
{
|
2010-04-12 03:04:06 +04:00
|
|
|
|
assert(c != NULL);
|
|
|
|
|
assert(content_get_type(c) == CONTENT_THEME);
|
2005-01-14 01:42:39 +03:00
|
|
|
|
|
2005-12-31 07:40:49 +03:00
|
|
|
|
if (ro_gui_dialog_open_top(dialog_theme_install, NULL, 0, 0)) {
|
2016-04-26 17:16:33 +03:00
|
|
|
|
ro_warn_user("ThemeInstActive", 0);
|
2005-01-14 01:42:39 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-09 03:20:45 +03:00
|
|
|
|
/* stop theme sitting in memory cache */
|
2010-04-12 03:04:06 +04:00
|
|
|
|
content_invalidate_reuse_data(c);
|
|
|
|
|
|
|
|
|
|
hlcache_handle_replace_callback(c, theme_install_callback, NULL);
|
2005-01-14 01:42:39 +03:00
|
|
|
|
|
|
|
|
|
ro_gui_set_icon_string(dialog_theme_install, ICON_THEME_INSTALL_MESSAGE,
|
2008-08-05 05:23:04 +04:00
|
|
|
|
messages_get("ThemeInstDown"), true);
|
2005-01-14 01:42:39 +03:00
|
|
|
|
ro_gui_set_icon_shaded_state(dialog_theme_install,
|
|
|
|
|
ICON_THEME_INSTALL_INSTALL, true);
|
2005-12-31 07:40:49 +03:00
|
|
|
|
ro_gui_wimp_event_register_close_window(dialog_theme_install,
|
|
|
|
|
theme_install_close);
|
2005-01-14 01:42:39 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Callback for fetchcache() for theme install fetches.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-04-12 03:04:06 +04:00
|
|
|
|
nserror theme_install_callback(hlcache_handle *handle,
|
|
|
|
|
const hlcache_event *event, void *pw)
|
2005-01-14 01:42:39 +03:00
|
|
|
|
{
|
2010-04-12 03:04:06 +04:00
|
|
|
|
switch (event->type) {
|
2005-03-09 03:20:45 +03:00
|
|
|
|
|
|
|
|
|
case CONTENT_MSG_DONE:
|
2010-04-12 03:04:06 +04:00
|
|
|
|
{
|
|
|
|
|
const char *source_data;
|
|
|
|
|
unsigned long source_size;
|
2014-05-31 02:06:30 +04:00
|
|
|
|
int author_indent = 0;
|
|
|
|
|
char buffer[256];
|
2010-04-12 03:04:06 +04:00
|
|
|
|
|
|
|
|
|
theme_install_content = handle;
|
|
|
|
|
|
|
|
|
|
source_data = content_get_source_data(handle, &source_size);
|
|
|
|
|
|
|
|
|
|
if (!theme_install_read(source_data, source_size)) {
|
2016-04-26 17:16:33 +03:00
|
|
|
|
ro_warn_user("ThemeInvalid", 0);
|
2005-12-31 07:40:49 +03:00
|
|
|
|
theme_install_close(dialog_theme_install);
|
2005-01-14 01:42:39 +03:00
|
|
|
|
break;
|
2005-03-09 03:20:45 +03:00
|
|
|
|
}
|
2005-01-14 01:42:39 +03:00
|
|
|
|
|
2005-03-09 03:20:45 +03:00
|
|
|
|
/* remove '<27> ' from the start of the data */
|
|
|
|
|
if (theme_install_descriptor.author[0] == '<EFBFBD>')
|
|
|
|
|
author_indent++;
|
|
|
|
|
while (theme_install_descriptor.author[author_indent] == ' ')
|
|
|
|
|
author_indent++;
|
|
|
|
|
snprintf(buffer, sizeof buffer, messages_get("ThemeInstall"),
|
|
|
|
|
theme_install_descriptor.name,
|
|
|
|
|
&theme_install_descriptor.author[author_indent]);
|
|
|
|
|
buffer[sizeof buffer - 1] = '\0';
|
|
|
|
|
ro_gui_set_icon_string(dialog_theme_install,
|
|
|
|
|
ICON_THEME_INSTALL_MESSAGE,
|
2008-08-05 05:23:04 +04:00
|
|
|
|
buffer, true);
|
2005-03-09 03:20:45 +03:00
|
|
|
|
ro_gui_set_icon_shaded_state(dialog_theme_install,
|
|
|
|
|
ICON_THEME_INSTALL_INSTALL, false);
|
2010-04-12 03:04:06 +04:00
|
|
|
|
}
|
2005-03-09 03:20:45 +03:00
|
|
|
|
break;
|
2005-01-23 19:09:05 +03:00
|
|
|
|
|
2005-03-09 03:20:45 +03:00
|
|
|
|
case CONTENT_MSG_ERROR:
|
2005-12-31 07:40:49 +03:00
|
|
|
|
theme_install_close(dialog_theme_install);
|
2016-04-26 17:16:33 +03:00
|
|
|
|
ro_warn_user(event->data.error, 0);
|
2005-03-09 03:20:45 +03:00
|
|
|
|
break;
|
2005-01-14 01:42:39 +03:00
|
|
|
|
|
2005-03-09 03:20:45 +03:00
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
2010-04-12 03:04:06 +04:00
|
|
|
|
|
|
|
|
|
return NSERROR_OK;
|
2005-03-09 03:20:45 +03:00
|
|
|
|
}
|
2005-01-14 01:42:39 +03:00
|
|
|
|
|
2005-03-09 03:20:45 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fill in theme_install_descriptor from received theme data.
|
|
|
|
|
*
|
|
|
|
|
* \param source_data received data
|
|
|
|
|
* \param source_size size of data
|
|
|
|
|
* \return true if data is a correct theme, false on error
|
|
|
|
|
*
|
|
|
|
|
* If the data is a correct theme, theme_install_descriptor is filled in.
|
|
|
|
|
*/
|
|
|
|
|
|
2010-04-12 03:04:06 +04:00
|
|
|
|
bool theme_install_read(const char *source_data, unsigned long source_size)
|
2005-03-09 03:20:45 +03:00
|
|
|
|
{
|
2010-04-12 03:04:06 +04:00
|
|
|
|
const void *data = source_data;
|
2009-03-27 04:27:27 +03:00
|
|
|
|
|
2005-03-09 03:20:45 +03:00
|
|
|
|
if (source_size < sizeof(struct theme_file_header))
|
|
|
|
|
return false;
|
|
|
|
|
if (!ro_gui_theme_read_file_header(&theme_install_descriptor,
|
2009-03-27 04:27:27 +03:00
|
|
|
|
(struct theme_file_header *) data))
|
2005-03-09 03:20:45 +03:00
|
|
|
|
return false;
|
|
|
|
|
if (source_size - sizeof(struct theme_file_header) !=
|
|
|
|
|
theme_install_descriptor.compressed_size)
|
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-01-14 01:42:39 +03:00
|
|
|
|
/**
|
2005-03-09 03:20:45 +03:00
|
|
|
|
* Install the downloaded theme.
|
|
|
|
|
*
|
2005-12-31 07:40:49 +03:00
|
|
|
|
* \param w the theme install window handle
|
2005-01-14 01:42:39 +03:00
|
|
|
|
*/
|
2005-03-09 03:20:45 +03:00
|
|
|
|
|
2005-12-31 07:40:49 +03:00
|
|
|
|
bool ro_gui_theme_install_apply(wimp_w w)
|
2005-03-09 03:20:45 +03:00
|
|
|
|
{
|
2005-01-23 19:09:05 +03:00
|
|
|
|
char theme_save[256];
|
|
|
|
|
char *theme_file;
|
|
|
|
|
struct theme_descriptor *theme_install;
|
2005-03-09 03:20:45 +03:00
|
|
|
|
os_error *error;
|
2006-07-03 05:14:46 +04:00
|
|
|
|
char *fix;
|
2010-04-12 03:04:06 +04:00
|
|
|
|
const char *source_data;
|
|
|
|
|
unsigned long source_size;
|
2005-01-23 19:09:05 +03:00
|
|
|
|
|
2005-03-09 03:20:45 +03:00
|
|
|
|
assert(theme_install_content);
|
|
|
|
|
|
2006-07-03 05:14:46 +04:00
|
|
|
|
/* convert spaces to hard spaces */
|
|
|
|
|
theme_file = strdup(theme_install_descriptor.name);
|
|
|
|
|
if (!theme_file) {
|
2015-05-28 18:08:46 +03:00
|
|
|
|
LOG("malloc failed");
|
2016-04-26 17:16:33 +03:00
|
|
|
|
ro_warn_user("NoMemory", 0);
|
2009-05-30 03:13:28 +04:00
|
|
|
|
return false;
|
2005-03-09 03:20:45 +03:00
|
|
|
|
}
|
2009-03-27 04:27:27 +03:00
|
|
|
|
for (fix = theme_file; *fix != '\0'; fix++)
|
2006-07-03 05:14:46 +04:00
|
|
|
|
if (*fix == ' ')
|
|
|
|
|
*fix = 160; /* hard space */
|
2005-03-09 03:20:45 +03:00
|
|
|
|
|
2005-12-31 07:40:49 +03:00
|
|
|
|
/* simply overwrite previous theme versions */
|
2006-01-08 04:51:33 +03:00
|
|
|
|
snprintf(theme_save, sizeof theme_save, "%s.%s",
|
2012-03-22 13:34:34 +04:00
|
|
|
|
nsoption_charp(theme_save), theme_file);
|
2005-03-09 03:20:45 +03:00
|
|
|
|
|
2005-12-31 07:40:49 +03:00
|
|
|
|
theme_save[sizeof theme_save - 1] = '\0';
|
2010-04-12 03:04:06 +04:00
|
|
|
|
|
|
|
|
|
source_data = content_get_source_data(theme_install_content,
|
|
|
|
|
&source_size);
|
|
|
|
|
|
2005-03-09 03:20:45 +03:00
|
|
|
|
error = xosfile_save_stamped(theme_save, 0xffd,
|
2010-04-12 03:04:06 +04:00
|
|
|
|
(byte *) source_data,
|
|
|
|
|
(byte *) source_data + source_size);
|
2005-03-09 03:20:45 +03:00
|
|
|
|
if (error) {
|
2015-05-28 18:08:46 +03:00
|
|
|
|
LOG("xosfile_save_stamped: 0x%x: %s", error->errnum, error->errmess);
|
2016-04-26 17:16:33 +03:00
|
|
|
|
ro_warn_user("ThemeInstallErr", 0);
|
2005-04-29 09:50:00 +04:00
|
|
|
|
free(theme_file);
|
2005-12-31 07:40:49 +03:00
|
|
|
|
return false;
|
2005-03-09 03:20:45 +03:00
|
|
|
|
}
|
2006-01-08 04:51:33 +03:00
|
|
|
|
|
2005-12-31 07:40:49 +03:00
|
|
|
|
/* apply the new theme */
|
|
|
|
|
ro_gui_theme_get_available();
|
|
|
|
|
theme_install = ro_gui_theme_find(theme_file);
|
|
|
|
|
if (!theme_install || !ro_gui_theme_apply(theme_install)) {
|
2016-04-26 17:16:33 +03:00
|
|
|
|
ro_warn_user("ThemeApplyErr", 0);
|
2005-12-31 07:40:49 +03:00
|
|
|
|
} else {
|
2012-03-22 13:34:34 +04:00
|
|
|
|
nsoption_set_charp(theme, strdup(theme_install->leafname));
|
2005-01-23 19:09:05 +03:00
|
|
|
|
}
|
2005-05-18 03:46:36 +04:00
|
|
|
|
free(theme_file);
|
2005-12-31 07:40:49 +03:00
|
|
|
|
ro_gui_save_options();
|
|
|
|
|
return true;
|
2005-01-23 19:09:05 +03:00
|
|
|
|
}
|
2005-01-14 01:42:39 +03:00
|
|
|
|
|
2005-03-09 03:20:45 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Close the theme installer and free resources.
|
|
|
|
|
*/
|
|
|
|
|
|
2005-12-31 07:40:49 +03:00
|
|
|
|
void theme_install_close(wimp_w w)
|
2005-03-09 03:20:45 +03:00
|
|
|
|
{
|
2005-01-23 19:09:05 +03:00
|
|
|
|
if (theme_install_content)
|
2010-04-12 03:04:06 +04:00
|
|
|
|
hlcache_handle_release(theme_install_content);
|
|
|
|
|
|
2005-01-23 19:09:05 +03:00
|
|
|
|
theme_install_content = NULL;
|
2005-01-14 01:42:39 +03:00
|
|
|
|
}
|