NetSurf
file.c
Go to the documentation of this file.
1/*
2 * Copyright 2011 Chris Young <chris@unsatisfactorysoftware.co.uk>
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#include <proto/asl.h>
20#include <proto/dos.h>
21#include <proto/exec.h>
22#include <proto/icon.h>
23#include <workbench/icon.h>
24
25#include <string.h>
26
27#include "utils/utils.h"
28#include "utils/nsoption.h"
29#include "utils/file.h"
30#include "utils/messages.h"
31#include "utils/nsurl.h"
33#include "netsurf/content.h"
36#include "desktop/save_pdf.h"
37#include "desktop/save_text.h"
38
39#include "amiga/gui.h"
40#include "amiga/bitmap.h"
41#include "amiga/download.h"
42#include "amiga/file.h"
43#include "amiga/filetype.h"
44#include "amiga/icon.h"
45#include "amiga/iff_dr2d.h"
46#include "amiga/misc.h"
47#include "amiga/save_pdf.h"
48#include "amiga/theme.h"
49
50static struct Hook aslhookfunc;
51
52struct FileRequester *filereq;
53struct FileRequester *savereq;
54
55HOOKF(ULONG, ami_file_asl_mime_hook, struct FileRequester *, fr, struct AnchorPathOld *)
56{
57 char fname[1024];
58 BOOL ret = FALSE;
59 char *mt = NULL;
60 lwc_string *lwc_mt = NULL;
61 lwc_error lerror;
62 content_type ct;
63
64 if(msg->ap_Info.fib_DirEntryType > 0) return(TRUE);
65
66 strcpy(fname,fr->fr_Drawer);
67 AddPart(fname, msg->ap_Info.fib_FileName,1024);
68
69 mt = strdup(fetch_filetype(fname));
70 lerror = lwc_intern_string(mt, strlen(mt), &lwc_mt);
71 if (lerror != lwc_error_ok)
72 return FALSE;
73
75 lwc_string_unref(lwc_mt);
76
77 if(ct != CONTENT_NONE) ret = TRUE;
78
79 free(mt);
80 return ret;
81}
82
83void ami_file_open(struct gui_window_2 *gwin)
84{
85 char *temp;
86 nsurl *url;
87
88 if(AslRequestTags(filereq,
89 ASLFR_TitleText, messages_get("NetSurf"),
90 ASLFR_Window, ami_gui2_get_window(gwin),
91 ASLFR_SleepWindow, TRUE,
92 ASLFR_Screen, ami_gui_get_screen(),
93 ASLFR_DoSaveMode, FALSE,
94 ASLFR_RejectIcons, TRUE,
95 ASLFR_FilterFunc, &aslhookfunc,
96 TAG_DONE))
97 {
98 if((temp = malloc(1024)))
99 {
100 strlcpy(temp, filereq->fr_Drawer, 1024);
101 AddPart(temp, filereq->fr_File, 1024);
102
103 if (netsurf_path_to_nsurl(temp, &url) != NSERROR_OK) {
104 amiga_warn_user("NoMemory", 0);
105 } else {
107 url,
108 NULL,
110 NULL,
111 NULL,
112 NULL);
113 nsurl_unref(url);
114 }
115
116 free(temp);
117 }
118 }
119}
120
121static void ami_file_set_type(const char *path, lwc_string *mime_type)
122{
124 const char *default_type;
125
126 switch(type) {
127 case CONTENT_HTML:
128 default_type = "html";
129 break;
130 default:
131 default_type = NULL;
132 break;
133 }
134
135 if (default_type != NULL) {
136 struct DiskObject *dobj = NULL;
137
138 dobj = GetIconTags(NULL,ICONGETA_GetDefaultName,default_type,
139 ICONGETA_GetDefaultType,WBPROJECT,
140 TAG_DONE);
141
142 PutIconTags(path, dobj,
143 ICONPUTA_NotifyWorkbench, TRUE, TAG_DONE);
144 }
145}
146
147void ami_file_save(int type, char *fname, struct Window *win,
148 struct hlcache_handle *object, struct hlcache_handle *favicon,
149 struct browser_window *bw)
150{
151 BPTR lock, fh;
152 const uint8_t *source_data;
153 char *selection;
154 size_t source_size;
155 struct bitmap *bm;
156
158
159 if(ami_download_check_overwrite(fname, win, 0)) {
160 switch(type) {
162 source_data = content_get_source_data(object, &source_size);
163 if(source_data) {
164 BPTR fh;
165 if((fh = FOpen(fname, MODE_NEWFILE,0))) {
166 FWrite(fh, source_data, 1, source_size);
167 FClose(fh);
168 }
169 }
170 break;
171
172 case AMINS_SAVE_TEXT:
173 save_as_text(object, fname);
174 break;
175
177 if((lock = CreateDir(fname))) {
178 UnLock(lock);
179 save_complete(object, fname, ami_file_set_type);
180 amiga_icon_superimpose_favicon(fname, favicon, NULL);
181 }
182 break;
183
184 case AMINS_SAVE_PDF:
185#ifdef WITH_PDF_EXPORT
186 if(save_as_pdf(object, fname))
187 amiga_icon_superimpose_favicon(fname, favicon, "pdf");
188#endif
189 break;
190
191 case AMINS_SAVE_IFF:
192 if((bm = content_get_bitmap(object))) {
195 amiga_bitmap_save(bm, fname, 0);
196 }
197#ifdef WITH_NS_SVG
198 else if(ami_mime_compare(object, "svg") == true) {
199 ami_save_svg(object, fname);
200 }
201#endif
202 break;
203
206 if(selection) {
207 fh = FOpen(fname, MODE_NEWFILE,0);
208 if (fh) {
209 FWrite(fh,
210 selection,
211 1,
212 strlen(selection));
213 FClose(fh);
214 }
215 free(selection);
216 }
217 break;
218 }
219 if(object) SetComment(fname, nsurl_access(hlcache_handle_get_url(object)));
220 }
221
223}
224
225void ami_file_save_req(int type, struct gui_window_2 *gwin,
226 struct hlcache_handle *object)
227{
228 char *fname = malloc(1024);
229 char *initial_fname = NULL;
230 char *fname_with_ext = NULL;
231 bool strip_ext = true;
232
233 if(object) {
234 if(type == AMINS_SAVE_SOURCE) strip_ext = false;
235 nsurl_nice(hlcache_handle_get_url(object), &initial_fname, strip_ext);
236 }
237
238 if(initial_fname != NULL) {
239 fname_with_ext = malloc(strlen(initial_fname) + 5); /* 5 = .ext\0 */
240
241 strcpy(fname_with_ext, initial_fname);
242
243 switch(type)
244 {
245 case AMINS_SAVE_TEXT:
247 strcat(fname_with_ext, ".txt");
248 break;
249 case AMINS_SAVE_IFF:
250 strcat(fname_with_ext, ".iff");
251 break;
252 case AMINS_SAVE_PDF:
253 strcat(fname_with_ext, ".pdf");
254 break;
255 default:
256 break;
257 }
258
259 if(initial_fname) free(initial_fname);
260 }
261
262 if(AslRequestTags(savereq,
263 ASLFR_Window, ami_gui2_get_window(gwin),
264 ASLFR_SleepWindow, TRUE,
265 ASLFR_TitleText, messages_get("NetSurf"),
266 ASLFR_Screen, ami_gui_get_screen(),
267 ASLFR_InitialFile, fname_with_ext ? fname_with_ext : "",
268 TAG_DONE))
269 {
270 strlcpy(fname, savereq->fr_Drawer, 1024);
271 AddPart(fname, savereq->fr_File, 1024);
272
273 ami_file_save(type, fname, ami_gui2_get_window(gwin), object,
275 }
276
277 if(fname) free(fname);
278 if(fname_with_ext) free(fname_with_ext);
279}
280
282{
283 const char *initial_dir = nsoption_charp(download_dir);
284 Tag initial_dir_tag = ASLFR_InitialDrawer;
285
286 if(initial_dir == NULL) initial_dir_tag = TAG_IGNORE;
287
288 filereq = (struct FileRequester *)AllocAslRequest(ASL_FileRequest, NULL);
289 savereq = (struct FileRequester *)AllocAslRequestTags(ASL_FileRequest,
290 ASLFR_DoSaveMode, TRUE,
291 ASLFR_RejectIcons, TRUE,
292 initial_dir_tag, initial_dir,
293 TAG_DONE);
294
295 aslhookfunc.h_Entry = (void *)&ami_file_asl_mime_hook;
296 aslhookfunc.h_SubEntry = NULL;
297 aslhookfunc.h_Data = NULL;
298}
299
301{
302 FreeAslRequest(filereq);
303 FreeAslRequest(savereq);
304}
305
const char * fetch_filetype(const char *unix_path)
Determine the MIME type of a local file.
Definition: filetype.c:58
bool ami_mime_compare(struct hlcache_handle *c, const char *type)
Compare the MIME type of an hlcache_handle to a DefIcons type.
Definition: filetype.c:614
struct Screen * ami_gui_get_screen(void)
Get a pointer to the screen NetSurf is running on.
Definition: gui.c:405
struct browser_window * ami_gui2_get_browser_window(struct gui_window_2 *gwin)
Get browser window from gui_window_2.
Definition: gui.c:427
struct gui_window * ami_gui2_get_gui_window(struct gui_window_2 *gwin)
Get gui_window from gui_window_2.
Definition: gui.c:445
struct hlcache_handle * ami_gui_get_favicon(struct gui_window *gw)
Get favicon from gui_window.
Definition: gui.c:482
struct Window * ami_gui2_get_window(struct gui_window_2 *gwin)
Get window from gui_window_2.
Definition: gui.c:572
nserror amiga_warn_user(const char *warning, const char *detail)
Warn the user of an event.
Definition: misc.c:79
void ami_update_pointer(struct Window *win, gui_pointer_shape shape)
Definition: theme.c:221
Browser window creation and manipulation interface.
nserror browser_window_navigate(struct browser_window *bw, struct nsurl *url, struct nsurl *referrer, enum browser_window_nav_flags flags, char *post_urlenc, struct fetch_multipart_data *post_multipart, struct hlcache_handle *parent)
Start fetching a page in a browser window.
char * browser_window_get_selection(struct browser_window *bw)
Get the current selection from a root browser window, ownership passed to caller, who must free() it.
@ BW_NAVIGATE_HISTORY
this will form a new history node (don't set for back/reload/etc)
content_type content_factory_type_from_mime_type(lwc_string *mime_type)
Compute the generic content type for a MIME type.
content_type
The type of a content.
Definition: content_type.h:53
@ CONTENT_NONE
no type for content
Definition: content_type.h:55
@ CONTENT_HTML
content is HTML
Definition: content_type.h:58
PDF Plotting.
@ NSERROR_OK
No error.
Definition: errors.h:30
const char * type
Definition: filetype.cpp:44
void ami_bitmap_set_url(struct bitmap *bm, struct nsurl *url)
Set bitmap URL.
Definition: bitmap.c:734
bool amiga_bitmap_save(void *bitmap, const char *path, unsigned flags)
Save a bitmap in the platform's native format.
Definition: bitmap.c:255
void ami_bitmap_set_title(struct bitmap *bm, const char *title)
Set bitmap title.
Definition: bitmap.c:740
BOOL ami_download_check_overwrite(const char *file, struct Window *win, ULONG size)
Definition: download.c:481
void ami_file_save_req(int type, struct gui_window_2 *gwin, struct hlcache_handle *object)
Definition: file.c:225
struct FileRequester * filereq
Definition: file.c:52
HOOKF(ULONG, ami_file_asl_mime_hook, struct FileRequester *, fr, struct AnchorPathOld *)
Definition: file.c:55
struct FileRequester * savereq
Definition: file.c:53
void ami_file_req_free(void)
Definition: file.c:300
void ami_file_req_init(void)
Definition: file.c:281
void ami_file_open(struct gui_window_2 *gwin)
Definition: file.c:83
static void ami_file_set_type(const char *path, lwc_string *mime_type)
Definition: file.c:121
void ami_file_save(int type, char *fname, struct Window *win, struct hlcache_handle *object, struct hlcache_handle *favicon, struct browser_window *bw)
Definition: file.c:147
static struct Hook aslhookfunc
Definition: file.c:50
@ AMINS_SAVE_IFF
Definition: file.h:32
@ AMINS_SAVE_PDF
Definition: file.h:31
@ AMINS_SAVE_TEXT
Definition: file.h:29
@ AMINS_SAVE_SELECTION
Definition: file.h:33
@ AMINS_SAVE_SOURCE
Definition: file.h:28
@ AMINS_SAVE_COMPLETE
Definition: file.h:30
static char * download_dir
Current download directory.
Definition: download.c:144
void amiga_icon_superimpose_favicon(char *path, struct hlcache_handle *icon, char *type)
Definition: icon.c:445
Content for image/x-amiga-icon (icon.library interface).
Public content interface.
struct nsurl * hlcache_handle_get_url(const struct hlcache_handle *handle)
Retrieve the URL associated with a high level cache handle.
const char * content_get_title(struct hlcache_handle *h)
Retrieve title associated with content.
Definition: content.c:1106
struct bitmap * content_get_bitmap(struct hlcache_handle *h)
Retrieve the bitmap contained in an image content.
Definition: content.c:1264
const uint8_t * content_get_source_data(struct hlcache_handle *h, size_t *size)
Retrieve source of content.
Definition: content.c:1209
@ GUI_POINTER_WAIT
Definition: mouse.h:127
@ GUI_POINTER_DEFAULT
Definition: mouse.h:113
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).
NetSurf URL handling (interface).
void nsurl_unref(nsurl *url)
Drop a reference to a NetSurf URL object.
const char * nsurl_access(const nsurl *url)
Access a NetSurf URL object as a string.
nserror nsurl_nice(const nsurl *url, char **result, bool remove_extensions)
Attempt to find a nice filename for a URL.
struct nsurl nsurl
NetSurf URL object.
Definition: nsurl.h:31
#define AnchorPathOld
Definition: os3support.h:131
#define FOpen(A, B, C)
Definition: os3support.h:158
#define FClose(A)
Definition: os3support.h:159
nserror save_complete(hlcache_handle *c, const char *path, save_complete_set_type_cb set_type)
Save an HTML page with all dependencies.
Save HTML document with dependencies (interface).
void save_as_text(struct hlcache_handle *c, char *path)
Extract the text from an HTML content and save it as a text file.
Definition: save_text.c:57
Text export of HTML (interface).
Interface to utility string handling.
RISC OS wimp toolkit bitmap.
Definition: bitmap.c:68
Browser window data.
High-level cache handle.
Definition: hlcache.c:66
nserror netsurf_path_to_nsurl(const char *path, struct nsurl **url)
Create a nsurl from a path.
Definition: file.c:307
Default operations table for files.
Option reading and saving interface.
#define nsoption_charp(OPTION)
Get the value of a string option.
Definition: nsoption.h:331
Interface to a number of general purpose functionality.
static nserror path(const struct redraw_context *ctx, const plot_style_t *pstyle, const float *p, unsigned int n, const float transform[6])
Plots a path.
Definition: plot.c:821