1
0
Fork 0
mirror of https://github.com/transmission/transmission synced 2024-12-26 09:37:56 +00:00

refactor some of the torrent opening code

This commit is contained in:
Mitchell Livingston 2008-01-05 02:48:10 +00:00
parent f2a6d79928
commit 093b59f754
3 changed files with 49 additions and 35 deletions

View file

@ -38,6 +38,14 @@
@class TorrentTableView; @class TorrentTableView;
typedef enum
{
ADD_NORMAL,
ADD_SHOW_OPTIONS,
ADD_URL,
ADD_CREATED
} addType;
@interface Controller : NSObject <GrowlApplicationBridgeDelegate> @interface Controller : NSObject <GrowlApplicationBridgeDelegate>
{ {
tr_handle * fLib; tr_handle * fLib;
@ -109,8 +117,7 @@
} }
- (void) openFiles: (NSArray *) filenames; - (void) openFiles: (NSArray *) filenames;
- (void) openFiles: (NSArray *) filenames forcePath: (NSString *) path ignoreDownloadFolder: (BOOL) ignore - (void) openFiles: (NSArray *) filenames addType: (addType) type forcePath: (NSString *) path;
deleteTorrentFile: (torrentFileState) deleteTorrent;
- (void) askOpenConfirmed: (Torrent *) torrent; - (void) askOpenConfirmed: (Torrent *) torrent;
- (void) openCreatedFile: (NSNotification *) notification; - (void) openCreatedFile: (NSNotification *) notification;
- (void) openFilesWithDict: (NSDictionary *) dictionary; - (void) openFilesWithDict: (NSDictionary *) dictionary;

View file

@ -665,7 +665,7 @@ void sleepCallBack(void * controller, io_service_t y, natural_t messageType, voi
{ {
NSString * path = [[fPendingTorrentDownloads objectForKey: [[download request] URL]] objectForKey: @"Path"]; NSString * path = [[fPendingTorrentDownloads objectForKey: [[download request] URL]] objectForKey: @"Path"];
[self openFiles: [NSArray arrayWithObject: path] forcePath: nil ignoreDownloadFolder: YES deleteTorrentFile: TORRENT_FILE_DELETE]; [self openFiles: [NSArray arrayWithObject: path] addType: ADD_URL forcePath: nil];
[fPendingTorrentDownloads removeObjectForKey: [[download request] URL]]; [fPendingTorrentDownloads removeObjectForKey: [[download request] URL]];
[download release]; [download release];
@ -678,13 +678,10 @@ void sleepCallBack(void * controller, io_service_t y, natural_t messageType, voi
- (void) application: (NSApplication *) app openFiles: (NSArray *) filenames - (void) application: (NSApplication *) app openFiles: (NSArray *) filenames
{ {
[self openFiles: filenames forcePath: nil ignoreDownloadFolder: NO deleteTorrentFile: TORRENT_FILE_DEFAULT]; [self openFiles: filenames addType: ADD_NORMAL forcePath: nil];
} }
#warning change ignore to add, and add an ignore only for torrents downloaded - (void) openFiles: (NSArray *) filenames addType: (addType) type forcePath: (NSString *) path
//perhaps use an enum of different open types?
- (void) openFiles: (NSArray *) filenames forcePath: (NSString *) path ignoreDownloadFolder: (BOOL) ignore
deleteTorrentFile: (torrentFileState) deleteTorrent
{ {
#warning make submethod #warning make submethod
if (!path && [fDefaults boolForKey: @"UseIncompleteDownloadFolder"] if (!path && [fDefaults boolForKey: @"UseIncompleteDownloadFolder"]
@ -701,10 +698,8 @@ void sleepCallBack(void * controller, io_service_t y, natural_t messageType, voi
[panel setMessage: NSLocalizedString(@"The incomplete folder cannot be used. Choose a new location or cancel for none.", [panel setMessage: NSLocalizedString(@"The incomplete folder cannot be used. Choose a new location or cancel for none.",
"Default incomplete folder cannot be used alert -> message")]; "Default incomplete folder cannot be used alert -> message")];
NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: filenames, @"Filenames",
filenames, @"Filenames", [NSNumber numberWithInt: type], @"AddType", nil];
[NSNumber numberWithBool: ignore], @"Ignore",
[NSNumber numberWithInt: deleteTorrent], @"DeleteTorrent", nil];
[panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: fWindow modalDelegate: self [panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: fWindow modalDelegate: self
didEndSelector: @selector(incompleteChoiceClosed:returnCode:contextInfo:) contextInfo: dict]; didEndSelector: @selector(incompleteChoiceClosed:returnCode:contextInfo:) contextInfo: dict];
@ -725,10 +720,8 @@ void sleepCallBack(void * controller, io_service_t y, natural_t messageType, voi
[panel setMessage: NSLocalizedString(@"The download folder cannot be used. Choose a new location.", [panel setMessage: NSLocalizedString(@"The download folder cannot be used. Choose a new location.",
"Default folder cannot be used alert -> message")]; "Default folder cannot be used alert -> message")];
NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys: filenames, @"Filenames",
filenames, @"Filenames", [NSNumber numberWithInt: type], @"AddType", nil];
[NSNumber numberWithBool: ignore], @"Ignore",
[NSNumber numberWithInt: deleteTorrent], @"DeleteTorrent", nil];
[panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: fWindow modalDelegate: self [panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: fWindow modalDelegate: self
didEndSelector: @selector(downloadChoiceClosed:returnCode:contextInfo:) contextInfo: dict]; didEndSelector: @selector(downloadChoiceClosed:returnCode:contextInfo:) contextInfo: dict];
@ -739,15 +732,30 @@ void sleepCallBack(void * controller, io_service_t y, natural_t messageType, voi
NSString * torrentPath; NSString * torrentPath;
tr_info info; tr_info info;
NSEnumerator * enumerator = [filenames objectEnumerator]; NSEnumerator * enumerator = [filenames objectEnumerator];
BOOL showWindow = ignore || [fDefaults boolForKey: @"DownloadAsk"];
BOOL showWindow = type == ADD_SHOW_OPTIONS || [fDefaults boolForKey: @"DownloadAsk"];
torrentFileState deleteTorrentFile;
switch (type)
{
case ADD_CREATED:
deleteTorrentFile = TORRENT_FILE_SAVE;
break;
case ADD_URL:
deleteTorrentFile = TORRENT_FILE_DELETE;
break;
default:
deleteTorrentFile = TORRENT_FILE_DEFAULT;
}
while ((torrentPath = [enumerator nextObject])) while ((torrentPath = [enumerator nextObject]))
{ {
NSString * location; NSString * location;
if (path) if (path)
location = path; location = [path stringByExpandingTildeInPath];
else if ([fDefaults boolForKey: @"DownloadLocationConstant"]) else if ([fDefaults boolForKey: @"DownloadLocationConstant"])
location = [[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath]; location = [[fDefaults stringForKey: @"DownloadFolder"] stringByExpandingTildeInPath];
else if (!ignore) else if (type != ADD_URL)
location = [torrentPath stringByDeletingLastPathComponent]; location = [torrentPath stringByDeletingLastPathComponent];
else else
location = nil; location = nil;
@ -764,7 +772,7 @@ void sleepCallBack(void * controller, io_service_t y, natural_t messageType, voi
tr_ctorFree(ctor); tr_ctorFree(ctor);
if (!(torrent = [[Torrent alloc] initWithPath: torrentPath location: location if (!(torrent = [[Torrent alloc] initWithPath: torrentPath location: location
deleteTorrentFile: showWindow ? TORRENT_FILE_SAVE : deleteTorrent lib: fLib])) deleteTorrentFile: showWindow ? TORRENT_FILE_SAVE : deleteTorrentFile lib: fLib]))
continue; continue;
//add it to the "File -> Open Recent" menu //add it to the "File -> Open Recent" menu
@ -773,7 +781,7 @@ void sleepCallBack(void * controller, io_service_t y, natural_t messageType, voi
if (showWindow) if (showWindow)
{ {
AddWindowController * addController = [[AddWindowController alloc] initWithTorrent: torrent destination: location AddWindowController * addController = [[AddWindowController alloc] initWithTorrent: torrent destination: location
controller: self deleteTorrent: deleteTorrent]; controller: self deleteTorrent: deleteTorrentFile];
[addController showWindow: self]; [addController showWindow: self];
} }
else else
@ -791,6 +799,7 @@ void sleepCallBack(void * controller, io_service_t y, natural_t messageType, voi
- (void) askOpenConfirmed: (Torrent *) torrent - (void) askOpenConfirmed: (Torrent *) torrent
{ {
#warning properly reset queue number
[torrent update]; [torrent update];
[fTorrents addObject: torrent]; [fTorrents addObject: torrent];
[torrent release]; [torrent release];
@ -801,8 +810,8 @@ void sleepCallBack(void * controller, io_service_t y, natural_t messageType, voi
- (void) openCreatedFile: (NSNotification *) notification - (void) openCreatedFile: (NSNotification *) notification
{ {
NSDictionary * dict = [notification userInfo]; NSDictionary * dict = [notification userInfo];
[self openFiles: [NSArray arrayWithObject: [dict objectForKey: @"File"]] forcePath: [dict objectForKey: @"Path"] [self openFiles: [NSArray arrayWithObject: [dict objectForKey: @"File"]] addType: ADD_CREATED
ignoreDownloadFolder: NO deleteTorrentFile: TORRENT_FILE_SAVE]; forcePath: [dict objectForKey: @"Path"]];
[dict release]; [dict release];
} }
@ -829,9 +838,8 @@ void sleepCallBack(void * controller, io_service_t y, natural_t messageType, voi
- (void) openFilesWithDict: (NSDictionary *) dictionary - (void) openFilesWithDict: (NSDictionary *) dictionary
{ {
[self openFiles: [dictionary objectForKey: @"Filenames"] forcePath: nil [self openFiles: [dictionary objectForKey: @"Filenames"] addType: [[dictionary objectForKey: @"AddType"] intValue]
ignoreDownloadFolder: [[dictionary objectForKey: @"Ignore"] boolValue] forcePath: nil];
deleteTorrentFile: [[dictionary objectForKey: @"DeleteTorrent"] intValue]];
[dictionary release]; [dictionary release];
} }
@ -844,7 +852,7 @@ void sleepCallBack(void * controller, io_service_t y, natural_t messageType, voi
- (void) openFiles: (NSArray *) filenames - (void) openFiles: (NSArray *) filenames
{ {
[self openFiles: filenames forcePath: nil ignoreDownloadFolder: NO deleteTorrentFile: TORRENT_FILE_DEFAULT]; [self openFiles: filenames addType: ADD_NORMAL forcePath: nil];
} }
- (void) openShowSheet: (id) sender - (void) openShowSheet: (id) sender
@ -860,20 +868,20 @@ void sleepCallBack(void * controller, io_service_t y, natural_t messageType, voi
contextInfo: [NSNumber numberWithBool: sender == fOpenIgnoreDownloadFolder]]; contextInfo: [NSNumber numberWithBool: sender == fOpenIgnoreDownloadFolder]];
} }
- (void) openSheetClosed: (NSOpenPanel *) panel returnCode: (int) code contextInfo: (NSNumber *) ignore - (void) openSheetClosed: (NSOpenPanel *) panel returnCode: (int) code contextInfo: (NSNumber *) useOptions
{ {
if (code == NSOKButton) if (code == NSOKButton)
{ {
NSDictionary * dictionary = [[NSDictionary alloc] initWithObjectsAndKeys: NSDictionary * dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
[panel filenames], @"Files", ignore, @"Ignore", nil]; [panel filenames], @"Files", useOptions, @"UseOptions", nil];
[self performSelectorOnMainThread: @selector(openFromSheet:) withObject: dictionary waitUntilDone: NO]; [self performSelectorOnMainThread: @selector(openFromSheet:) withObject: dictionary waitUntilDone: NO];
} }
} }
- (void) openFromSheet: (NSDictionary *) dictionary - (void) openFromSheet: (NSDictionary *) dictionary
{ {
[self openFiles: [dictionary objectForKey: @"Files"] forcePath: nil [self openFiles: [dictionary objectForKey: @"Files"] addType: [[dictionary objectForKey: @"UseOptions"] boolValue]
ignoreDownloadFolder: [[dictionary objectForKey: @"Ignore"] boolValue] deleteTorrentFile: TORRENT_FILE_DEFAULT]; ? ADD_SHOW_OPTIONS : ADD_NORMAL forcePath: nil];
[dictionary release]; [dictionary release];
} }
@ -3818,8 +3826,7 @@ void sleepCallBack(void * controller, io_service_t y, natural_t messageType, voi
{ {
int oldCount = [fTorrents count]; int oldCount = [fTorrents count];
[self openFiles: [NSArray arrayWithObject: path] forcePath: directory ignoreDownloadFolder: NO [self openFiles: [NSArray arrayWithObject: path] addType: ADD_NORMAL forcePath: directory];
deleteTorrentFile: TORRENT_FILE_DEFAULT];
return [fTorrents count] > oldCount; return [fTorrents count] > oldCount;
} }

View file

@ -64,9 +64,9 @@
} }
//check for old version download location //check for old version download location
if ([fDefaults stringForKey: @"DownloadChoice"]) NSString * choice;
if ((choice = [fDefaults stringForKey: @"DownloadChoice"]))
{ {
NSString * choice = [fDefaults stringForKey: @"DownloadChoice"];
[fDefaults setBool: [choice isEqualToString: @"Constant"] forKey: @"DownloadLocationConstant"]; [fDefaults setBool: [choice isEqualToString: @"Constant"] forKey: @"DownloadLocationConstant"];
[fDefaults setBool: [choice isEqualToString: @"Ask"] forKey: @"DownloadAsk"]; [fDefaults setBool: [choice isEqualToString: @"Ask"] forKey: @"DownloadAsk"];