Basic download manager

This commit is contained in:
anthony 2020-12-09 20:18:22 +00:00
parent f0aeee7f97
commit d04c87012a
7 changed files with 170 additions and 5 deletions

View File

@ -24,7 +24,7 @@ Works for basic use, can navigate to, and browse websites.
What still needs doing
----------------
Tabs, downloads, bookmarks, history,
Tabs, downloads, bookmarks, history, search,
other bits and bobs probably.

View File

@ -24,5 +24,6 @@ id downloadsWindowController;
}
-(void)showDownloadsWindow;
-(NSURL*)requestDownloadDestination;
@end

View File

@ -66,6 +66,13 @@ static nserror set_defaults(struct nsoption_s *defaults)
}
}
-(NSURL*)requestDownloadDestination {
NSSavePanel *savePanel = [NSOpenPanel savePanel];
[savePanel setDirectory: NSHomeDirectory()];
[savePanel runModal];
return [savePanel URL];
}
@end
int main(int argc, char **argv) {

View File

@ -0,0 +1,30 @@
#import <Cocoa/Cocoa.h>
@class DownloadManager;
@interface DownloadItem: NSObject {
BOOL completed;
BOOL cancelled;
NSInteger size;
NSInteger written;
NSURL *destination;
NSOutputStream *outputStream;
NSString *error;
DownloadManager *manager;
}
-(BOOL)appendToDownload: (NSData*)data;
-(void)cancel;
-(void)complete;
-(BOOL)isComplete;
-(void)failWithMessage: (NSString*)message;
-(NSURL*)destination;
-(double)percentCompletion;
@end
@interface DownloadManager: NSObject {
NSMutableArray *downloads;
}
+(DownloadManager*)defaultDownloadManager;
-(DownloadItem*)createDownloadForDestination: (NSURL*)path withSizeInBytes: (NSInteger)size;
-(NSArray*)downloads;
@end

View File

@ -0,0 +1,112 @@
#import <Cocoa/Cocoa.h>
#import "DownloadManager.h"
@implementation DownloadItem
-(id)initWithManager: (DownloadManager*)aManager destination: (NSURL*)aDestination size: (NSInteger)aSize {
if (self = [super init]) {
error = nil;
written = 0;
completed = NO;
size = aSize;
[aDestination retain];
destination = aDestination;
manager = aManager;
outputStream = [NSOutputStream outputStreamToFileAtPath: [destination path]
append: NO];
[outputStream retain];
[outputStream open];
}
return self;
}
-(void)dealloc {
[destination release];
[outputStream close];
[outputStream release];
if (error) {
[error release];
}
[super dealloc];
}
-(BOOL)appendToDownload: (NSData*)data {
NSInteger len = [data length];
NSInteger writtenNow = [outputStream write: [data bytes] maxLength: len];
written += writtenNow;
return writtenNow == len;
}
-(void)cancel {
if (!completed) {
[self complete];
}
}
-(void)failWithMessage: (NSString*)message {
if (!completed) {
[message retain];
error = message;
[self complete];
}
}
-(void)complete {
[outputStream close];
completed = YES;
}
-(BOOL)isComplete {
return completed;
}
-(NSURL*)destination {
return destination;
}
-(double)percentCompletion {
if (written == size) {
return 1.0;
} else {
return (double)written / size;
}
}
@end
@implementation DownloadManager
+(DownloadManager*)defaultDownloadManager {
static DownloadManager *manager;
if (!manager) {
manager = [[DownloadManager alloc] init];
}
return manager;
}
-(id)init {
if (self = [super init]) {
downloads = [[NSMutableArray alloc] init];
}
return self;
}
-(void)dealloc {
[downloads release];
[super dealloc];
}
-(DownloadItem*)createDownloadForDestination: (NSURL*)path withSizeInBytes: (NSInteger)size {
DownloadItem *item = [[DownloadItem alloc] initWithManager: self destination: path
size: size];
[downloads addObject: item];
[item release];
return item;
}
-(NSArray*)downloads {
return downloads;
}
@end

View File

@ -41,6 +41,7 @@ S_FRONTEND := \
AppDelegate.m \
BrowserWindowController.m \
DownloadsWindowController.m \
DownloadManager.m \
PlotView.m \
NetsurfCallback.m \
tables/misc.m \

View File

@ -2,7 +2,9 @@
#import "netsurf/netsurf.h"
#import "netsurf/download.h"
#import "desktop/download.h"
#import "DownloadManager.h"
#import "AppDelegate.h"
/**********************/
/****** Download ******/
@ -11,25 +13,37 @@
// This won't really return a window ref, but a ref to a download item.
static struct gui_download_window *gnustep_download_create(struct download_context *ctx, struct gui_window *parent) {
NSLog(@"gnustep_download_create");
// Save dialog.. then...
NSURL *url = [[NSApp delegate] requestDownloadDestination];
NSInteger dataSize = download_context_get_total_length(ctx);
DownloadItem *download = [[DownloadManager defaultDownloadManager]
createDownloadForDestination: url withSizeInBytes: dataSize];
[[NSApp delegate] showDownloadsWindow];
return NULL;
return (struct gui_download_window*)download;
}
// ??
static nserror gnustep_download_data(struct gui_download_window *dw, const char *data, unsigned int size) {
NSLog(@"gnustep_download_data");
return NSERROR_OK;
BOOL success = [(id)dw appendToDownload: [NSData dataWithBytesNoCopy: (void*)data
length: size]];
if (success) {
return NSERROR_OK;
} else {
return NSERROR_SAVE_FAILED;
}
}
// Error occurred during download
static void gnustep_download_error(struct gui_download_window *dw, const char *error_msg) {
NSLog(@"gnustep_download_error");
[(id)dw failWithMessage: [NSString stringWithCString: error_msg]];
}
// Download completed
static void gnustep_download_done(struct gui_download_window *dw) {
NSLog(@"gnustep_download_done");
[(id)dw complete];
}
struct gui_download_table gnustep_download_table = {