NetSurf
theme_install.c
Go to the documentation of this file.
1/*
2 * Copyright 2005 James Bursa <bursa@users.sourceforge.net>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19/** \file
20 * Theme auto-installing.
21 */
22
23#include <assert.h>
24#include <stdbool.h>
25#include <oslib/osfile.h>
26#include <oslib/wimp.h>
27
28#include "utils/nsoption.h"
29#include "utils/log.h"
30#include "utils/messages.h"
31#include "netsurf/content.h"
32#include "content/hlcache.h"
33#include "desktop/theme.h"
34
35#include "riscos/dialog.h"
36#include "riscos/gui.h"
37#include "riscos/theme.h"
38#include "riscos/wimp.h"
39#include "riscos/wimp_event.h"
40
41
45
46
47static void theme_install_close(wimp_w w);
49 const hlcache_event *event, void *pw);
50
51
52/**
53 * Handle a CONTENT_THEME that has started loading.
54 */
55
57{
58 assert(c != NULL);
59 assert(content_get_type(c) == CONTENT_THEME);
60
62 ro_warn_user("ThemeInstActive", 0);
63 return;
64 }
65
66 /* stop theme sitting in memory cache */
68
70
72 messages_get("ThemeInstDown"), true);
77}
78
79
80/**
81 * Fill in theme_install_descriptor from received theme data.
82 *
83 * \param source_data received data
84 * \param source_size size of data
85 * \return true if data is a correct theme, false on error
86 *
87 * If the data is a correct theme, theme_install_descriptor is filled in.
88 */
89
90static bool
91theme_install_read(const uint8_t *source_data, size_t source_size)
92{
93 const void *data = source_data;
94
95 if (source_size < sizeof(struct theme_file_header))
96 return false;
98 (struct theme_file_header *) data))
99 return false;
100 if (source_size - sizeof(struct theme_file_header) !=
102 return false;
103 return true;
104}
105
106
107/**
108 * Callback for fetchcache() for theme install fetches.
109 */
110
112 const hlcache_event *event, void *pw)
113{
114 switch (event->type) {
115
116 case CONTENT_MSG_DONE:
117 {
118 const uint8_t *source_data;
119 size_t source_size;
120 int author_indent = 0;
121 char buffer[256];
122
123 theme_install_content = handle;
124
125 source_data = content_get_source_data(handle, &source_size);
126
127 if (!theme_install_read(source_data, source_size)) {
128 ro_warn_user("ThemeInvalid", 0);
130 break;
131 }
132
133 /* remove '© ' from the start of the data */
134 if (theme_install_descriptor.author[0] == '©')
135 author_indent++;
136 while (theme_install_descriptor.author[author_indent] == ' ')
137 author_indent++;
138 snprintf(buffer, sizeof buffer, messages_get("ThemeInstall"),
140 &theme_install_descriptor.author[author_indent]);
141 buffer[sizeof buffer - 1] = '\0';
144 buffer, true);
147 }
148 break;
149
153 break;
154
155 default:
156 break;
157 }
158
159 return NSERROR_OK;
160}
161
162
163
164
165/**
166 * Install the downloaded theme.
167 *
168 * \param w the theme install window handle
169 */
170
172{
173 char theme_save[256];
174 char *theme_file;
175 struct theme_descriptor *theme_install;
176 os_error *error;
177 char *fix;
178 const uint8_t *source_data;
179 size_t source_size;
180
181 assert(theme_install_content);
182
183 /* convert spaces to hard spaces */
184 theme_file = strdup(theme_install_descriptor.name);
185 if (!theme_file) {
186 NSLOG(netsurf, INFO, "malloc failed");
187 ro_warn_user("NoMemory", 0);
188 return false;
189 }
190 for (fix = theme_file; *fix != '\0'; fix++)
191 if (*fix == ' ')
192 *fix = 160; /* hard space */
193
194 /* simply overwrite previous theme versions */
195 snprintf(theme_save, sizeof theme_save, "%s.%s",
196 nsoption_charp(theme_save), theme_file);
197
198 theme_save[sizeof theme_save - 1] = '\0';
199
201 &source_size);
202
203 error = xosfile_save_stamped(theme_save, 0xffd,
204 (byte *) source_data,
205 (byte *) source_data + source_size);
206 if (error) {
207 NSLOG(netsurf, INFO, "xosfile_save_stamped: 0x%x: %s",
208 error->errnum, error->errmess);
209 ro_warn_user("ThemeInstallErr", 0);
210 free(theme_file);
211 return false;
212 }
213
214 /* apply the new theme */
216 theme_install = ro_gui_theme_find(theme_file);
217 if (!theme_install || !ro_gui_theme_apply(theme_install)) {
218 ro_warn_user("ThemeApplyErr", 0);
219 } else {
220 nsoption_set_charp(theme, strdup(theme_install->leafname));
221 }
222 free(theme_file);
224 return true;
225}
226
227
228/**
229 * Close the theme installer and free resources.
230 */
231
233{
236
238}
static osspriteop_area * buffer
The buffer characteristics.
Definition: buffer.c:55
@ CONTENT_THEME
RISC OS themes content.
Definition: content_type.h:73
@ CONTENT_MSG_DONE
content has finished processing
Definition: content_type.h:119
@ CONTENT_MSG_ERROR
error occurred
Definition: content_type.h:122
GUI Themeing interface.
void ro_gui_save_options(void)
Save the current options.
Definition: dialog.c:670
bool ro_gui_dialog_open_top(wimp_w w, struct toolbar *toolbar, int width, int height)
Moves a window to the top of the stack.
Definition: dialog.c:413
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_OK
No error.
Definition: errors.h:30
Window themes(interface).
nserror hlcache_handle_replace_callback(hlcache_handle *handle, hlcache_handle_callback cb, void *pw)
Replace a high-level cache handle's callback.
Definition: hlcache.c:860
nserror hlcache_handle_release(hlcache_handle *handle)
Release a high-level cache handle.
Definition: hlcache.c:740
High-level resource cache interface.
Public content interface.
const uint8_t * content_get_source_data(struct hlcache_handle *h, size_t *size)
Retrieve source of content.
Definition: content.c:1209
void content_invalidate_reuse_data(struct hlcache_handle *h)
Invalidate content reuse data.
Definition: content.c:1229
content_type content_get_type(struct hlcache_handle *h)
Retrieve computed type of content.
Definition: content.c:1061
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
const char * messages_get(const char *key)
Fast lookup of a message by key from the standard Messages hash.
Definition: messages.c:241
Localised message support (interface).
nserror ro_warn_user(const char *warning, const char *detail)
Display a warning for a serious problem (eg memory exhaustion).
Definition: gui.c:2076
#define ICON_THEME_INSTALL_MESSAGE
Definition: gui.h:208
#define ICON_THEME_INSTALL_INSTALL
Definition: gui.h:209
bool ro_gui_theme_read_file_header(struct theme_descriptor *descriptor, struct theme_file_header *file_header)
Fills in the basic details for a descriptor from a file header.
Definition: theme.c:452
struct theme_descriptor * ro_gui_theme_get_available(void)
Reads and caches the currently available themes.
Definition: theme.c:125
struct theme_descriptor * ro_gui_theme_find(const char *leafname)
Finds a theme from the cached values.
Definition: theme.c:100
bool ro_gui_theme_apply(struct theme_descriptor *descriptor)
Applies the theme to all current windows and subsequent ones.
Definition: theme.c:666
High-level cache event.
Definition: hlcache.h:43
content_msg type
Event type.
Definition: hlcache.h:44
union content_msg_data data
Event data.
Definition: hlcache.h:45
High-level cache handle.
Definition: hlcache.c:66
char * leafname
theme leafname
Definition: theme.h:73
char name[32]
theme name
Definition: theme.h:75
char author[64]
theme author
Definition: theme.h:76
unsigned int compressed_size
compressed sprite size
Definition: theme.h:84
Definition: theme.h:64
static struct hlcache_handle * theme_install_content
Definition: theme_install.c:42
static struct theme_descriptor theme_install_descriptor
Definition: theme_install.c:43
bool ro_gui_theme_install_apply(wimp_w w)
Install the downloaded theme.
static nserror theme_install_callback(struct hlcache_handle *handle, const hlcache_event *event, void *pw)
Callback for fetchcache() for theme install fetches.
static bool theme_install_read(const uint8_t *source_data, size_t source_size)
Fill in theme_install_descriptor from received theme data.
Definition: theme_install.c:91
static void theme_install_close(wimp_w w)
Close the theme installer and free resources.
wimp_w dialog_theme_install
Definition: theme_install.c:44
void theme_install_start(struct hlcache_handle *c)
Handle a CONTENT_THEME that has started loading.
Definition: theme_install.c:56
struct content_msg_data::@99 errordata
CONTENT_MSG_ERROR - Error from content or underlying fetch.
const char * errormsg
The message.
Definition: content.h:95
Option reading and saving interface.
#define nsoption_charp(OPTION)
Get the value of a string option.
Definition: nsoption.h:297
#define nsoption_set_charp(OPTION, VALUE)
set string option in default table
Definition: nsoption.h:338
void ro_gui_set_icon_string(wimp_w w, wimp_i i, const char *text, bool is_utf8)
Set the contents of a text or sprite icon to a string.
Definition: wimp.c:269
void ro_gui_set_icon_shaded_state(wimp_w w, wimp_i i, bool state)
Set the shaded state of an icon.
Definition: wimp.c:487
General RISC OS WIMP/OS library functions (interface).
bool ro_gui_wimp_event_register_close_window(wimp_w w, void(*callback)(wimp_w w))
Register a function to be called after the window has been closed.
Definition: wimp_event.c:1492
Automated RISC OS WIMP event handling (interface).