summaryrefslogtreecommitdiff
path: root/cocoa/DownloadWindowController.m
diff options
context:
space:
mode:
authorSven Weidauer <sven.weidauer@gmail.com>2011-01-29 10:24:54 +0000
committerSven Weidauer <sven.weidauer@gmail.com>2011-01-29 10:24:54 +0000
commitb1d96fcd6bf7d54ee921f446fe26d3f6210f9c02 (patch)
treed33931e58eddd6af56549a21894637eb587949a3 /cocoa/DownloadWindowController.m
parent672cb3741211f24404c556dba67b953a4414664e (diff)
downloadnetsurf-b1d96fcd6bf7d54ee921f446fe26d3f6210f9c02.tar.gz
netsurf-b1d96fcd6bf7d54ee921f446fe26d3f6210f9c02.tar.bz2
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
Diffstat (limited to 'cocoa/DownloadWindowController.m')
-rw-r--r--cocoa/DownloadWindowController.m60
1 files changed, 56 insertions, 4 deletions
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;