diff --git a/macosx/Controller.h b/macosx/Controller.h index 6b03d8edc..910985094 100644 --- a/macosx/Controller.h +++ b/macosx/Controller.h @@ -105,7 +105,9 @@ typedef enum NSMutableDictionary * fPendingTorrentDownloads; NSMutableSet * fAddingTransfers; + NSMutableSet * fAddWindows; + URLSheetWindowController * fUrlSheetController; BOOL fGlobalPopoverShown; BOOL fSoundPlaying; @@ -128,7 +130,6 @@ typedef enum - (void) openURL: (NSString *) urlString; - (void) openURLShowSheet: (id) sender; -- (void) urlSheetDidEnd: (URLSheetWindowController *) controller url: (NSString *) urlString returnCode: (NSInteger) returnCode; - (void) quitSheetDidEnd: (NSWindow *) sheet returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo; diff --git a/macosx/Controller.m b/macosx/Controller.m index 130580e02..d4740ace9 100644 --- a/macosx/Controller.m +++ b/macosx/Controller.m @@ -752,6 +752,13 @@ static void sleepCallback(void * controller, io_service_t y, natural_t messageTy { [download cancel]; + [fPendingTorrentDownloads removeObjectForKey: [[download request] URL]]; + if ([fPendingTorrentDownloads count] == 0) + { + [fPendingTorrentDownloads release]; + fPendingTorrentDownloads = nil; + } + NSRunAlertPanel(NSLocalizedString(@"Torrent download failed", "Download not a torrent -> title"), [NSString stringWithFormat: NSLocalizedString(@"It appears that the file \"%@\" from %@ is not a torrent file.", "Download not a torrent -> message"), suggestedName, @@ -767,11 +774,7 @@ static void sleepCallback(void * controller, io_service_t y, natural_t messageTy -(void) download: (NSURLDownload *) download didCreateDestination: (NSString *) path { - if (!fPendingTorrentDownloads) - fPendingTorrentDownloads = [[NSMutableDictionary alloc] init]; - - [fPendingTorrentDownloads setObject: [NSDictionary dictionaryWithObjectsAndKeys: - path, @"Path", download, @"Download", nil] forKey: [[download request] URL]]; + [(NSMutableDictionary *)[fPendingTorrentDownloads objectForKey: [[download request] URL]] setObject: path forKey: @"Path"]; } - (void) download: (NSURLDownload *) download didFailWithError: (NSError *) error @@ -1203,21 +1206,41 @@ static void sleepCallback(void * controller, io_service_t y, natural_t messageTy NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: urlString] cachePolicy: NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval: 60]; - [[NSURLDownload alloc] initWithRequest: request delegate: self]; + + if ([fPendingTorrentDownloads objectForKey: [request URL]]) + { + NSLog(@"Already downloading %@", [request URL]); + return; + } + + NSURLDownload * download = [[NSURLDownload alloc] initWithRequest: request delegate: self]; + + if (!fPendingTorrentDownloads) + fPendingTorrentDownloads = [[NSMutableDictionary alloc] init]; + [fPendingTorrentDownloads setObject: [NSMutableDictionary dictionaryWithObject: download forKey: @"Download"] forKey: [request URL]]; } } - (void) openURLShowSheet: (id) sender { - [[[URLSheetWindowController alloc] initWithController: self] beginSheetForWindow: fWindow]; + if (!fUrlSheetController) + { + fUrlSheetController = [[URLSheetWindowController alloc] initWithController: self]; + + [NSApp beginSheet: [fUrlSheetController window] modalForWindow: fWindow modalDelegate: self didEndSelector: @selector(urlSheetDidEnd:returnCode:contextInfo:) contextInfo: nil]; + } } -- (void) urlSheetDidEnd: (URLSheetWindowController *) controller url: (NSString *) urlString returnCode: (NSInteger) returnCode +- (void) urlSheetDidEnd: (NSWindow *) sheet returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo { if (returnCode == 1) + { + NSString * urlString = [fUrlSheetController urlString]; [self performSelectorOnMainThread: @selector(openURL:) withObject: urlString waitUntilDone: NO]; + } - [controller release]; + [fUrlSheetController release]; + fUrlSheetController = nil; } - (void) createFile: (id) sender diff --git a/macosx/URLSheetWindowController.h b/macosx/URLSheetWindowController.h index b4ac263cf..3a505f93c 100644 --- a/macosx/URLSheetWindowController.h +++ b/macosx/URLSheetWindowController.h @@ -36,9 +36,10 @@ } - (id) initWithController: (Controller *) controller; -- (void) beginSheetForWindow: (NSWindow *) window; - (void) openURLEndSheet: (id) sender; - (void) openURLCancelEndSheet: (id) sender; +- (NSString *) urlString; + @end diff --git a/macosx/URLSheetWindowController.m b/macosx/URLSheetWindowController.m index aad117f21..12aeb0235 100644 --- a/macosx/URLSheetWindowController.m +++ b/macosx/URLSheetWindowController.m @@ -80,12 +80,6 @@ NSString * urlString = nil; [fCancelButton setFrame: cancelFrame]; } -- (IBAction) beginSheetForWindow: (NSWindow *) window -{ - [NSApp beginSheet: [self window] modalForWindow: window modalDelegate: self - didEndSelector: @selector(sheetDidEnd:returnCode:contextInfo:) contextInfo: nil]; -} - - (void) openURLEndSheet: (id) sender { [[self window] orderOut: sender]; @@ -98,11 +92,9 @@ NSString * urlString = nil; [NSApp endSheet: [self window] returnCode: 0]; } -- (void) sheetDidEnd: (NSWindow *) sheet returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo +- (NSString *) urlString { - [urlString release]; - urlString = [[fTextField stringValue] retain]; - [fController urlSheetDidEnd: self url: urlString returnCode: returnCode]; + return [[[fTextField stringValue] retain] autorelease]; } - (void) controlTextDidChange: (NSNotification *) notification