Files
mtkports/www-client/netsurf/patches/0012-native-downloads.patch
T

114 lines
3.3 KiB
Diff

diff --git a/frontends/monkey/download.c b/frontends/monkey/download.c
index 1f77439..2eef168 100644
--- a/frontends/monkey/download.c
+++ b/frontends/monkey/download.c
@@ -17,8 +17,13 @@
*/
#include <stdio.h>
+#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
+#include <string.h>
+#if defined(__montauk__)
+#include <montauk.h>
+#endif
#include "utils/errors.h"
#include "utils/ring.h"
@@ -36,6 +41,11 @@ struct gui_download_window {
struct gui_window *g;
download_context *dlctx;
uint32_t dwin_num;
+#if defined(__montauk__)
+ int file_handle;
+ unsigned long offset;
+ char path[256];
+#endif
char *host; /* ignore */
};
@@ -51,6 +61,34 @@ gui_download_window_create(download_context *ctx,
ret->g = parent;
ret->dwin_num = dwin_ctr++;
ret->dlctx = ctx;
+#if defined(__montauk__)
+ ret->file_handle = -1;
+ char user[64] = {0};
+ if (_mtk_syscall2(MTK_SYS_GETUSER, (long)user, sizeof(user)) < 0 || user[0] == 0)
+ strcpy(user, "admin");
+ char directory[192];
+ snprintf(directory, sizeof(directory), "0:/users/%s/Downloads", user);
+ mtk_mkdir(directory);
+ const char *source = download_context_get_filename(ctx);
+ char filename[96];
+ if (source == NULL || source[0] == 0) source = "download.bin";
+ size_t n = 0;
+ for (; source[n] != 0 && n + 1 < sizeof(filename); n++) {
+ unsigned char c = (unsigned char)source[n];
+ filename[n] = (c < 32 || c == '/' || c == '\\' || c == ':') ? '_' : (char)c;
+ }
+ filename[n] = 0;
+ snprintf(ret->path, sizeof(ret->path), "%s/%s", directory, filename);
+ ret->file_handle = mtk_create(ret->path);
+ if (ret->file_handle < 0) {
+ free(ret);
+ return NULL;
+ }
+ if (parent != NULL) {
+ snprintf(parent->status, sizeof(parent->status), "Downloading %s", filename);
+ parent->dirty = true;
+ }
+#endif
RING_INSERT(dw_ring, ret);
@@ -64,21 +102,47 @@ static nserror
gui_download_window_data(struct gui_download_window *dw,
const char *data, unsigned int size)
{
+#if defined(__montauk__)
+ if (dw->file_handle < 0 || mtk_write(dw->file_handle,
+ (const uint8_t *)data, dw->offset, size) != (int)size)
+ return NSERROR_SAVE_FAILED;
+ dw->offset += size;
+ if (dw->g != NULL) {
+ snprintf(dw->g->status, sizeof(dw->g->status), "Downloading: %lu bytes", dw->offset);
+ dw->g->dirty = true;
+ }
+ return NSERROR_OK;
+#else
moutf(MOUT_DOWNLOAD, "DATA DWIN %u SIZE %u DATA %s",
dw->dwin_num, size, data);
return NSERROR_OK;
+#endif
}
static void
gui_download_window_error(struct gui_download_window *dw,
const char *error_msg)
{
+#if defined(__montauk__)
+ if (dw->file_handle >= 0) { mtk_close(dw->file_handle); dw->file_handle = -1; }
+ if (dw->g != NULL) {
+ snprintf(dw->g->status, sizeof(dw->g->status), "Download failed: %s", error_msg);
+ dw->g->dirty = true;
+ }
+#endif
moutf(MOUT_DOWNLOAD, "ERROR DWIN %u ERROR %s", dw->dwin_num, error_msg);
}
static void
gui_download_window_done(struct gui_download_window *dw)
{
+#if defined(__montauk__)
+ if (dw->file_handle >= 0) { mtk_close(dw->file_handle); dw->file_handle = -1; }
+ if (dw->g != NULL) {
+ snprintf(dw->g->status, sizeof(dw->g->status), "Saved %s", dw->path);
+ dw->g->dirty = true;
+ }
+#endif
moutf(MOUT_DOWNLOAD, "DONE DWIN %u", dw->dwin_num);
RING_REMOVE(dw_ring, dw);
download_context_destroy(dw->dlctx);