From 467cae922fe561d2860da7d85e39c908958489b6 Mon Sep 17 00:00:00 2001 From: Kalle Olavi Niemitalo Date: Sat, 27 Oct 2007 18:24:42 +0300 Subject: [PATCH] Don't let UI access transferQueue directly. --- TFUSBController.h | 3 ++- TFUSBController.m | 18 ++++++++++++++---- UIElements.m | 14 +++++++------- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/TFUSBController.h b/TFUSBController.h index 319d15a..1d759a9 100644 --- a/TFUSBController.h +++ b/TFUSBController.h @@ -82,8 +82,9 @@ typedef struct { - (void) addPriorityTransfer:(id)newTransfer; - (void) addTransfer:(id)newTransfer atIndex:(int)front; //-1 for end of array - (void) setDH:(id)newDH tableView:(id)tv; +- (BOOL) hasCurrentTransfer; +- (id) currentTransferInfo; - (void) clearQueues; -- (id) transferQueue; - (id) pausedQueue; @end diff --git a/TFUSBController.m b/TFUSBController.m index 3b255dc..8654b6a 100644 --- a/TFUSBController.m +++ b/TFUSBController.m @@ -894,10 +894,6 @@ initDevice(USBDeviceContext *device) [pausedQueue removeAllObjects]; } -- (id) transferQueue { - return transferQueue; -} - - (id) pausedQueue { return pausedQueue; } @@ -1030,4 +1026,18 @@ initDevice(USBDeviceContext *device) NSLog(@"New rate set: %i", rate); } +- (BOOL) hasCurrentTransfer { + return [transferQueue count] > 0; +} + +- (id) currentTransferInfo { + if ([transferQueue count] == 0) + return nil; + else { + // Make a copy of the object so that it can be safely manipulated + // in a different thread from the original. + return [[transferQueue objectAtIndex:0] copy]; + } +} + @end diff --git a/UIElements.m b/UIElements.m index c834cce..1a43aef 100644 --- a/UIElements.m +++ b/UIElements.m @@ -125,7 +125,7 @@ if ([previewDrawer state] == NSDrawerClosedState) return; // don't bother getting previews if closed if ([tableView numberOfSelectedRows] != 1 || !connected) return; - if ([[tfusbc transferQueue] count] > 0) return; //for now, change to a priority transfer later? + if ([tfusbc hasCurrentTransfer]) return; //for now, change to a priority transfer later? id currentSelectedItem = [[dh displayedList] objectAtIndex:[tableView selectedRow]]; int type = [[currentSelectedItem objectForKey:@"type"] intValue]; if (type == 2) { //file @@ -135,7 +135,7 @@ NSString* tempFile = NSTemporaryDirectory(); // download preview to temp folder tempFile = [tempFile stringByAppendingPathComponent:@"MacTFTemp"]; [tfusbc addTransfer:[NSDictionary dictionaryWithObjectsAndKeys:currentSelectedItem,@"filename",currentPath,@"path",tempFile,@"savePath",[NSNumber numberWithUnsignedLongLong:0],@"offset",@"download",@"transferType",[NSNumber numberWithBool:NO],@"looping",[NSNumber numberWithInt:0],@"existingTime",nil] atIndex:-1]; - while ([[tfusbc transferQueue] count] > 0) { + while ([tfusbc hasCurrentTransfer]) { usleep(10); } [preview setString:[NSString stringWithContentsOfFile:tempFile]]; @@ -144,7 +144,7 @@ NSString* tempFile = NSTemporaryDirectory(); // download first 64k to temp folder tempFile = [tempFile stringByAppendingPathComponent:@"MacTFTemp"]; [tfusbc addTransfer:[NSDictionary dictionaryWithObjectsAndKeys:currentSelectedItem,@"filename",currentPath,@"path",tempFile,@"savePath",[NSNumber numberWithUnsignedLongLong:0],@"offset",@"download",@"transferType",[NSNumber numberWithBool:NO],@"looping",[NSNumber numberWithInt:0],@"existingTime",nil] atIndex:-1]; - while ([[tfusbc transferQueue] count] > 0) { + while ([tfusbc hasCurrentTransfer]) { usleep(10); } NSDictionary* recData = [dh extractDataFromRecHeader:tempFile forModel:[prefs objectForKey:@"modelType"]]; //then parse and display headers @@ -219,7 +219,7 @@ [NSThread detachNewThreadSelector:@selector(transfer:) toTarget:tfusbc withObject:nil]; } else { // already connected connected = NO; - if ([[tfusbc transferQueue] count] != 0) { //gives a few seconds for busy thread to notice it should not be connected before cutting the device off + if ([tfusbc hasCurrentTransfer]) { //gives a few seconds for busy thread to notice it should not be connected before cutting the device off sleep(3); } [tfusbc closeDevice:context]; @@ -634,7 +634,7 @@ } - (IBAction) toggleTurbo:(id)sender { - if ([[tfusbc transferQueue] count] > 0) { //have a current transfer, lets toggle it + if ([tfusbc hasCurrentTransfer]) { //have a current transfer, lets toggle it [tfusbc addPriorityTransfer:[NSDictionary dictionaryWithObjectsAndKeys:@"turbo",@"transferType",[NSNumber numberWithBool:[turboCB state]],@"turboOn",nil]]; } } @@ -647,7 +647,7 @@ } else { [turboCB setState:1]; } - if ([[tfusbc transferQueue] count] > 0) { //have a current transfer, lets toggle it + if ([tfusbc hasCurrentTransfer]) { //have a current transfer, lets toggle it [tfusbc addPriorityTransfer:[NSDictionary dictionaryWithObjectsAndKeys:@"turbo",@"transferType",[NSNumber numberWithBool:[turboCB state]],@"turboOn",nil]]; } } @@ -777,7 +777,7 @@ return paths; [pauseButton setImage:[NSImage imageNamed:@"DownloadResume.tif"]]; [pauseButton setAlternateImage:[NSImage imageNamed:@"DownloadResumePressed.tif"]]; //add priority "halt" to queue naming file - id current = [[tfusbc transferQueue] objectAtIndex:0]; + id current = [tfusbc currentTransferInfo]; [tfusbc addPriorityTransfer:[NSDictionary dictionaryWithObjectsAndKeys:@"pause",@"transferType",[current objectForKey:@"filename"],@"filename",nil]]; paused = YES; [currentlyField setStringValue:[NSLocalizedString(@"PAUSED", @"Paused: ") stringByAppendingString:[[current objectForKey:@"filename"]objectForKey:@"name"]]]; -- 2.11.4.GIT