The beginning of queueing. Has some work to go, but the basic infrastructure is set up.

This commit is contained in:
Mitchell Livingston
2006-06-23 15:06:27 +00:00
parent 70ea317d4a
commit 14cf4c87e6
11 changed files with 201 additions and 41 deletions

View File

@@ -133,6 +133,8 @@
- (void) ratioGlobalChange: (NSNotification *) notification;
- (void) ratioSingleChange: (NSNotification *) notification;
- (void) checkWaitingForFinished: (NSNotification *) notification;
- (void) sleepCallBack: (natural_t) messageType argument:
(void *) messageArgument;

View File

@@ -212,6 +212,9 @@ static void sleepCallBack(void * controller, io_service_t y,
[nc addObserver: self selector: @selector(ratioGlobalChange:)
name: @"RatioGlobalChange" object: nil];
[nc addObserver: self selector: @selector(checkWaitingForFinished:)
name: @"TorrentFinishedDownloading" object: nil];
//timer to update the interface
fCompleted = 0;
[self updateUI: nil];
@@ -317,8 +320,22 @@ static void sleepCallBack(void * controller, io_service_t y,
{
if (code == NSOKButton)
{
//setup for autostart
NSString * startSetting = [fDefaults stringForKey: @"StartSetting"];
BOOL waitToStart = [startSetting isEqualToString: @"Wait"];
int desiredActive, active = 0;
if (waitToStart)
{
desiredActive = [fDefaults integerForKey: @"WaitToStartNumber"];
Torrent * tempTorrent;
NSEnumerator * enumerator = [fTorrents objectEnumerator];
while ((tempTorrent = [enumerator nextObject]))
if ([tempTorrent isActive] && ![tempTorrent isSeeding])
active++;
}
[torrent setDownloadFolder: [[openPanel filenames] objectAtIndex: 0]];
if ([fDefaults boolForKey: @"AutoStartDownload"])
if ((waitToStart && active < desiredActive) || [startSetting isEqualToString: @"Start"])
[torrent startTransfer];
[fTorrents addObject: torrent];
@@ -328,12 +345,24 @@ static void sleepCallBack(void * controller, io_service_t y,
[NSApp stopModal];
}
- (void) application: (NSApplication *) sender
openFiles: (NSArray *) filenames
- (void) application: (NSApplication *) sender openFiles: (NSArray *) filenames
{
BOOL autoStart = [fDefaults boolForKey: @"AutoStartDownload"];
NSString * downloadChoice = [fDefaults stringForKey: @"DownloadChoice"], * torrentPath;
//setup for autostart
NSString * startSetting = [fDefaults stringForKey: @"StartSetting"];
BOOL waitToStart = [startSetting isEqualToString: @"Wait"];
int desiredActive, active = 0;
if (waitToStart && ![downloadChoice isEqualToString: @"Ask"])
{
desiredActive = [fDefaults integerForKey: @"WaitToStartNumber"];
Torrent * tempTorrent;
NSEnumerator * enumerator = [fTorrents objectEnumerator];
while ((tempTorrent = [enumerator nextObject]))
if ([tempTorrent isActive] && ![tempTorrent isSeeding])
active++;
}
Torrent * torrent;
NSEnumerator * enumerator = [filenames objectEnumerator];
while ((torrentPath = [enumerator nextObject]))
@@ -341,7 +370,7 @@ static void sleepCallBack(void * controller, io_service_t y,
if (!(torrent = [[Torrent alloc] initWithPath: torrentPath lib: fLib]))
continue;
/* Add it to the "File > Open Recent" menu */
//add it to the "File > Open Recent" menu
[[NSDocumentController sharedDocumentController]
noteNewRecentDocumentURL: [NSURL fileURLWithPath: torrentPath]];
@@ -370,8 +399,12 @@ static void sleepCallBack(void * controller, io_service_t y,
: [torrentPath stringByDeletingLastPathComponent];
[torrent setDownloadFolder: folder];
if (autoStart)
#warning should check if transfer was already done
if ((waitToStart && active < desiredActive) || [startSetting isEqualToString: @"Start"])
{
[torrent startTransfer];
active++;
}
[fTorrents addObject: torrent];
}
@@ -910,6 +943,46 @@ static void sleepCallBack(void * controller, io_service_t y,
[fInfoController updateInfoForTorrents: torrents];
}
- (void) checkWaitingForFinished: (NSNotification *) notification
{
//don't try to start a transfer if there should be none waiting
if (![[fDefaults stringForKey: @"StartSetting"] isEqualToString: @"Wait"])
return;
int desiredActive = [fDefaults integerForKey: @"WaitToStartNumber"], active = 0;
NSEnumerator * enumerator = [fTorrents objectEnumerator];
Torrent * torrent, * torrentToStart = nil;
while ((torrent = [enumerator nextObject]))
{
//ignore the torrent just stopped; for some reason it is not marked instantly as not active
if (torrent == [notification object])
continue;
if ([torrent isActive])
{
if (![torrent isSeeding])
{
active++;
if (active >= desiredActive)
return;
}
}
else
{
if (!torrentToStart && [torrent waitingToStart])
torrentToStart = torrent;
}
}
//since it hasn't returned, the queue amount has not been met
if (torrentToStart)
{
[torrentToStart startTransfer];
[self updateUI: nil];
}
}
- (int) numberOfRowsInTableView: (NSTableView *) t
{
return [fTorrents count];

View File

@@ -2,8 +2,6 @@
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>AutoStartDownload</key>
<true/>
<key>BadgeDownloadRate</key>
<false/>
<key>BadgeUploadRate</key>
@@ -50,6 +48,8 @@
<false/>
<key>Sort</key>
<string>Date</string>
<key>StartSetting</key>
<string>Start</string>
<key>StatusBar</key>
<true/>
<key>UpdateCheck</key>
@@ -58,5 +58,7 @@
<integer>20</integer>
<key>UseAdvancedBar</key>
<false/>
<key>WaitToStartNumber</key>
<integer>3</integer>
</dict>
</plist>

View File

@@ -5,7 +5,6 @@
{
ACTIONS = {
folderSheetShow = id;
setAutoStart = id;
setBadge = id;
setDownloadLocation = id;
setLimit = id;
@@ -15,12 +14,13 @@
setRatio = id;
setRatioCheck = id;
setShowMessage = id;
setStartSetting = id;
setUpdate = id;
setWaitToStart = id;
};
CLASS = PrefsController;
LANGUAGE = ObjC;
OUTLETS = {
fAutoStartCheck = NSButton;
fBadgeDownloadRateCheck = NSButton;
fBadgeUploadRateCheck = NSButton;
fCopyTorrentCheck = NSButton;
@@ -35,11 +35,13 @@
fRatioCheck = NSButton;
fRatioField = NSTextField;
fRemoveCheck = NSButton;
fStartMatrix = NSMatrix;
fTransfersView = NSView;
fUpdatePopUp = NSPopUpButton;
fUpdater = SUUpdater;
fUploadCheck = NSButton;
fUploadField = NSTextField;
fWaitToStartField = NSTextField;
};
SUPERCLASS = NSWindowController;
},

View File

@@ -7,18 +7,14 @@
<key>IBEditorPositions</key>
<dict>
<key>28</key>
<string>195 461 462 212 0 0 1152 842 </string>
<string>321 472 535 212 0 0 1152 842 </string>
<key>41</key>
<string>345 427 462 306 0 0 1152 842 </string>
<string>286 392 535 345 0 0 1152 842 </string>
<key>66</key>
<string>345 526 462 104 0 0 1152 842 </string>
<string>321 526 535 104 0 0 1152 842 </string>
</dict>
<key>IBFramework Version</key>
<string>446.1</string>
<key>IBOpenObjects</key>
<array>
<integer>41</integer>
</array>
<key>IBSystem Version</key>
<string>8I127</string>
</dict>

View File

@@ -408,11 +408,10 @@
- (void) setRatioCheck: (id) sender
{
NSButtonCell * selected = [fRatioMatrix selectedCell];
int ratioSetting;
if (selected == [fRatioMatrix cellWithTag: RATIO_CHECK_TAG])
int ratioSetting, tag = [[fRatioMatrix selectedCell] tag];
if (tag == RATIO_CHECK_TAG)
ratioSetting = RATIO_CHECK;
else if (selected == [fRatioMatrix cellWithTag: RATIO_NO_CHECK_TAG])
else if (tag == RATIO_NO_CHECK_TAG)
ratioSetting = RATIO_NO_CHECK;
else
ratioSetting = RATIO_GLOBAL;
@@ -423,7 +422,7 @@
[torrent setStopRatioSetting: ratioSetting];
[self setRatioLimit: fRatioLimitField];
[fRatioLimitField setEnabled: selected == [fRatioMatrix cellWithTag: RATIO_CHECK_TAG]];
[fRatioLimitField setEnabled: tag == RATIO_CHECK_TAG];
}
- (void) setRatioLimit: (id) sender

View File

@@ -36,7 +36,7 @@
IBOutlet NSPopUpButton * fFolderPopUp;
IBOutlet NSButton * fQuitCheck, * fRemoveCheck,
* fBadgeDownloadRateCheck, * fBadgeUploadRateCheck,
* fAutoStartCheck, * fCopyTorrentCheck, * fDeleteOriginalTorrentCheck;
* fCopyTorrentCheck, * fDeleteOriginalTorrentCheck;
IBOutlet NSPopUpButton * fUpdatePopUp;
IBOutlet NSTextField * fPortField, * fUploadField, * fDownloadField;
@@ -45,6 +45,9 @@
IBOutlet NSButton * fRatioCheck;
IBOutlet NSTextField * fRatioField;
IBOutlet NSMatrix * fStartMatrix;
IBOutlet NSTextField * fWaitToStartField;
IBOutlet SUUpdater * fUpdater;
NSString * fDownloadFolder;
@@ -57,7 +60,11 @@
- (void) setBadge: (id) sender;
- (void) setUpdate: (id) sender;
- (void) checkUpdate;
- (void) setAutoStart: (id) sender;
- (void) setStartSetting: (id) sender;
- (void) setWaitToStart: (id) sender;
- (void) setMoveTorrent: (id) sender;
- (void) setDownloadLocation: (id) sender;
- (void) folderSheetShow: (id) sender;

View File

@@ -32,6 +32,10 @@
#define DOWNLOAD_TORRENT 2
#define DOWNLOAD_ASK 3
#define START_YES_CHECK_TAG 0
#define START_WAIT_CHECK_TAG 1
#define START_NO_CHECK_TAG 2
#define UPDATE_DAILY 0
#define UPDATE_WEEKLY 1
#define UPDATE_NEVER 2
@@ -142,8 +146,19 @@
[fBadgeDownloadRateCheck setState: [fDefaults boolForKey: @"BadgeDownloadRate"]];
[fBadgeUploadRateCheck setState: [fDefaults boolForKey: @"BadgeUploadRate"]];
//set auto start
[fAutoStartCheck setState: [fDefaults boolForKey: @"AutoStartDownload"]];
//set start setting
NSString * startSetting = [fDefaults stringForKey: @"StartSetting"];
int tag;
if ([startSetting isEqualToString: @"Start"])
tag = START_YES_CHECK_TAG;
else if ([startSetting isEqualToString: @"Wait"])
tag = START_WAIT_CHECK_TAG;
else
tag = START_NO_CHECK_TAG;
[fStartMatrix selectCellWithTag: tag];
[fWaitToStartField setEnabled: tag == START_WAIT_CHECK_TAG];
[fWaitToStartField setIntValue: [fDefaults integerForKey: @"WaitToStartNumber"]];
//set private torrents
BOOL copyTorrents = [fDefaults boolForKey: @"SavePrivateTorrent"];
@@ -407,9 +422,37 @@
[fUpdater scheduleCheckWithInterval: seconds];
}
- (void) setAutoStart: (id) sender
- (void) setStartSetting: (id) sender
{
[fDefaults setBool: [sender state] forKey: @"AutoStartDownload"];
NSString * startSetting;
int tag = [[fStartMatrix selectedCell] tag];
if (tag == START_YES_CHECK_TAG)
startSetting = @"Start";
else if (tag == START_WAIT_CHECK_TAG)
startSetting = @"Wait";
else
startSetting = @"Manual";
[fDefaults setObject: startSetting forKey: @"StartSetting"];
[self setWaitToStart: fWaitToStartField];
[fWaitToStartField setEnabled: tag == START_WAIT_CHECK_TAG];
}
- (void) setWaitToStart: (id) sender
{
int waitNumber = [sender intValue];
if (![[sender stringValue] isEqualToString: [NSString stringWithInt: waitNumber]] || waitNumber < 1)
{
NSBeep();
waitNumber = [fDefaults floatForKey: @"WaitToStartNumber"];
[sender setIntValue: waitNumber];
}
else
[fDefaults setInteger: waitNumber forKey: @"WaitToStartNumber"];
#warning notification recheck
}
- (void) setMoveTorrent: (id) sender

View File

@@ -49,7 +49,7 @@
int fStopRatioSetting;
float fRatioLimit;
BOOL fFinishedSeeding;
BOOL fFinishedSeeding, fWaitToStart;
}
- (id) initWithPath: (NSString *) path lib: (tr_handle_t *) lib;
@@ -74,6 +74,9 @@
- (float) ratioLimit;
- (void) setRatioLimit: (float) limit;
- (void) setWaitToStart: (BOOL) wait;
- (BOOL) waitingToStart;
- (void) revealData;
- (void) trashData;
- (void) trashTorrent;

View File

@@ -30,7 +30,7 @@
- (id) initWithHash: (NSString *) hashString path: (NSString *) path lib: (tr_handle_t *) lib
privateTorrent: (NSNumber *) privateTorrent publicTorrent: (NSNumber *) publicTorrent
date: (NSDate *) date stopRatioSetting: (NSNumber *) stopRatioSetting
ratioLimit: (NSNumber *) ratioLimit;
ratioLimit: (NSNumber *) ratioLimit waitToStart: (NSNumber *) waitToStart;
- (void) trashFile: (NSString *) path;
@@ -42,7 +42,7 @@
- (id) initWithPath: (NSString *) path lib: (tr_handle_t *) lib
{
self = [self initWithHash: nil path: path lib: lib privateTorrent: nil publicTorrent: nil
date: nil stopRatioSetting: nil ratioLimit: nil];
date: nil stopRatioSetting: nil ratioLimit: nil waitToStart: nil];
if (self)
{
@@ -60,7 +60,8 @@
publicTorrent: [history objectForKey: @"PublicCopy"]
date: [history objectForKey: @"Date"]
stopRatioSetting: [history objectForKey: @"StopRatioSetting"]
ratioLimit: [history objectForKey: @"RatioLimit"]];
ratioLimit: [history objectForKey: @"RatioLimit"]
waitToStart: [history objectForKey: @"WaitToStart"]];
if (self)
{
@@ -85,7 +86,8 @@
[self isActive] ? @"NO" : @"YES", @"Paused",
[self date], @"Date",
[NSNumber numberWithInt: fStopRatioSetting], @"StopRatioSetting",
[NSNumber numberWithFloat: fRatioLimit], @"RatioLimit", nil];
[NSNumber numberWithFloat: fRatioLimit], @"RatioLimit",
[NSNumber numberWithBool: fWaitToStart], @"WaitToStart", nil];
if (fPrivateTorrent)
[history setObject: [self hashString] forKey: @"TorrentHash"];
@@ -158,7 +160,12 @@
switch (fStat->status)
{
case TR_STATUS_PAUSE:
[fStatusString setString: fFinishedSeeding ? @"Seeding Complete" : @"Paused"];
if (fFinishedSeeding)
[fStatusString setString: @"Seeding complete"];
else if (fWaitToStart)
[fStatusString setString: [@"Waiting to start" stringByAppendingEllipsis]];
else
[fStatusString setString: @"Paused"];
break;
case TR_STATUS_CHECK:
@@ -221,14 +228,23 @@
if (![self isActive])
{
tr_torrentStart(fHandle);
fFinishedSeeding = NO;
fWaitToStart = NO;
}
}
- (void) stopTransfer
{
if ([self isActive])
{
BOOL wasSeeding = [self isSeeding];
tr_torrentStop(fHandle);
if (!wasSeeding)
[[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentFinishedDownloading" object: self];
}
}
- (void) removeForever
@@ -240,13 +256,13 @@
- (void) sleep
{
if ((fResumeOnWake = [self isActive]))
[self stopTransfer];
tr_torrentStop(fHandle);
}
- (void) wakeUp
{
if (fResumeOnWake)
[self startTransfer];
tr_torrentStart(fHandle);
}
- (float) ratio
@@ -276,6 +292,16 @@
fRatioLimit = limit;
}
- (void) setWaitToStart: (BOOL) wait
{
fWaitToStart = wait;
}
- (BOOL) waitingToStart
{
return fWaitToStart;
}
- (void) revealData
{
[[NSWorkspace sharedWorkspace] selectFile: [self dataLocation] inFileViewerRootedAtPath: nil];
@@ -424,7 +450,11 @@
- (BOOL) justFinished
{
return tr_getFinished(fHandle);
BOOL finished = tr_getFinished(fHandle);
if (finished)
[[NSNotificationCenter defaultCenter] postNotificationName: @"TorrentFinishedDownloading" object: self];
return finished;
}
- (NSString *) progressString
@@ -528,7 +558,7 @@
- (id) initWithHash: (NSString *) hashString path: (NSString *) path lib: (tr_handle_t *) lib
privateTorrent: (NSNumber *) privateTorrent publicTorrent: (NSNumber *) publicTorrent
date: (NSDate *) date stopRatioSetting: (NSNumber *) stopRatioSetting
ratioLimit: (NSNumber *) ratioLimit
ratioLimit: (NSNumber *) ratioLimit waitToStart: (NSNumber *) waitToStart
{
if (!(self = [super init]))
return nil;
@@ -564,6 +594,9 @@
fRatioLimit = ratioLimit ? [ratioLimit floatValue] : [fDefaults floatForKey: @"RatioLimit"];
fFinishedSeeding = NO;
fWaitToStart = waitToStart ? [waitToStart boolValue]
: [[fDefaults stringForKey: @"StartSetting"] isEqualToString: @"Wait"];
NSString * fileType = fInfo->multifile ? NSFileTypeForHFSTypeCode('fldr') : [[self name] pathExtension];
fIcon = [[NSWorkspace sharedWorkspace] iconForFileType: fileType];
[fIcon retain];