From b1d96fcd6bf7d54ee921f446fe26d3f6210f9c02 Mon Sep 17 00:00:00 2001 From: Sven Weidauer Date: Sat, 29 Jan 2011 10:24:54 +0000 Subject: Download window asks if download should be cancelled before being closed and displays an estimate of the time remaining. svn path=/trunk/netsurf/; revision=11521 --- cocoa/DownloadWindowController.m | 60 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 4 deletions(-) (limited to 'cocoa/DownloadWindowController.m') diff --git a/cocoa/DownloadWindowController.m b/cocoa/DownloadWindowController.m index 701b8817a..adce55f5d 100644 --- a/cocoa/DownloadWindowController.m +++ b/cocoa/DownloadWindowController.m @@ -20,6 +20,7 @@ #import "desktop/download.h" #import "desktop/gui.h" +#import "cocoa/gui.h" @interface DownloadWindowController () @@ -29,6 +30,7 @@ - (void)savePanelDidEnd:(NSSavePanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo; - (void)alertDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo; +- (void)askCancelDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo; - (BOOL) receivedData: (NSData *)data; @@ -164,6 +166,33 @@ static void cocoa_register_download( DownloadWindowController *download ); [self removeIfPossible]; } +- (BOOL) windowShouldClose: (id)sender; +{ + if ([[NSUserDefaults standardUserDefaults] boolForKey: kAlwaysCancelDownload]) return YES; + + NSAlert *ask = [NSAlert alertWithMessageText: @"Cancel download?" defaultButton: @"Yes" + alternateButton: @"No" otherButton: nil + informativeTextWithFormat: @"Should the download of '%@' really be cancelled?", [self fileName]]; + [ask setShowsSuppressionButton: YES]; + [ask beginSheetModalForWindow: [self window] modalDelegate: self + didEndSelector: @selector(askCancelDidEnd:returnCode:contextInfo:) contextInfo: NULL]; + return NO; +} + +- (void) windowWillClose: (NSNotification *)notification; +{ + [self abort]; +} + +- (void)askCancelDidEnd:(NSAlert *)alert returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo; +{ + if (returnCode == NSOKButton) { + [[NSUserDefaults standardUserDefaults] setBool: [[alert suppressionButton] state] == NSOnState + forKey: kAlwaysCancelDownload]; + [self close]; + } +} + #pragma mark - #pragma mark Properties @@ -211,23 +240,46 @@ static NSString *cocoa_file_size_string( float size ) return [NSString stringWithFormat:@"%1.1f TB", size]; } +static NSString *cocoa_time_string( unsigned seconds ) +{ + if (seconds <= 10) return @"less than 10 seconds"; + + if (seconds < 60) return [NSString stringWithFormat: @"%u seconds", seconds]; + + unsigned minutes = seconds / 60; + seconds = seconds % 60; + + if (minutes < 60) return [NSString stringWithFormat: @"%u:%02u minutes", minutes, seconds]; + + unsigned hours = minutes / 60; + minutes = minutes % 60; + + return [NSString stringWithFormat: @"%2:%02u hours", hours, minutes]; +} + - (NSString *) statusText; { NSString *speedString = @""; - float elapsedTime = [[NSDate date] timeIntervalSinceDate: startDate]; + float speed = 0.0; + NSTimeInterval elapsedTime = [[NSDate date] timeIntervalSinceDate: startDate]; if (elapsedTime >= 0.1) { - float speed = (float)receivedSize / elapsedTime; + speed = (float)receivedSize / elapsedTime; speedString = [NSString stringWithFormat: @" (%@/s)", cocoa_file_size_string( speed )]; } + NSString *timeRemainingString = @""; NSString *totalSizeString = @""; if (totalSize != 0) { + if (speed > 0.0) { + float timeRemaining = (float)(totalSize - receivedSize) / speed; + timeRemainingString = [NSString stringWithFormat: @": %@", cocoa_time_string( timeRemaining )]; + } totalSizeString = [NSString stringWithFormat: @" of %@", cocoa_file_size_string( totalSize )]; } - return [NSString stringWithFormat: @"%@%@%@", cocoa_file_size_string( receivedSize ), - totalSizeString, speedString]; + return [NSString stringWithFormat: @"%@%@%@%@", cocoa_file_size_string( receivedSize ), + totalSizeString, speedString, timeRemainingString]; } + (NSSet *) keyPathsForValuesAffectingFileName; -- cgit v1.2.3