From d730f1699f8e728c114b8adafbb05880bd261684 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Sun, 16 Mar 2014 22:50:58 +0100 Subject: [PATCH] edit: selector validation and fixed an exception. --- Classes/SBQueue.h | 4 +++- Classes/SBQueue.m | 14 +++++++++++++- Classes/SBQueueController.m | 20 +++++++++++++------- 3 files changed, 29 insertions(+), 9 deletions(-) diff --git a/Classes/SBQueue.h b/Classes/SBQueue.h index 54aa240..1a98ad3 100644 --- a/Classes/SBQueue.h +++ b/Classes/SBQueue.h @@ -9,7 +9,8 @@ #import #import -@class SBQueueItem; +#import "SBQueueItem.h" + @class MP42File; typedef NS_ENUM(NSUInteger, SBQueueStatus) { @@ -58,6 +59,7 @@ extern NSString *SBQueueCancelledNotification; - (SBQueueItem *)itemAtIndex:(NSUInteger)index; - (NSUInteger)indexOfItem:(SBQueueItem *)item; +- (NSIndexSet *)indexesOfItemsWithStatus:(SBQueueItemStatus)status; - (NSArray *)itemsAtIndexes:(NSIndexSet *)indexes; - (void)insertItem:(SBQueueItem *)anItem atIndex:(NSUInteger)index; diff --git a/Classes/SBQueue.m b/Classes/SBQueue.m index acadfca..b270f71 100644 --- a/Classes/SBQueue.m +++ b/Classes/SBQueue.m @@ -7,7 +7,6 @@ // #import "SBQueue.h" -#import "SBQueueItem.h" #import "MetadataImporter.h" @@ -147,6 +146,18 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; return index; } +- (NSIndexSet *)indexesOfItemsWithStatus:(SBQueueItemStatus)status { + NSMutableIndexSet *indexes = [NSMutableIndexSet indexSet]; + dispatch_sync(self.itemsQueue, ^{ + [self.items enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { + if (((SBQueueItem *)obj).status == status) { + [indexes addIndex:idx]; + } + }]; + }); + return indexes; +} + - (void)insertItem:(SBQueueItem *)anItem atIndex:(NSUInteger)index { dispatch_sync(self.itemsQueue, ^{ [self.items insertObject:anItem atIndex:index]; @@ -314,6 +325,7 @@ NSString *SBQueueCancelledNotification = @"SBQueueCancelledNotification"; #endif [attributes release]; + return noErr; } diff --git a/Classes/SBQueueController.m b/Classes/SBQueueController.m index 8b14081..61e737a 100755 --- a/Classes/SBQueueController.m +++ b/Classes/SBQueueController.m @@ -93,14 +93,14 @@ static NSString *fileType = @"mp4"; // Register to the queue notifications NSOperationQueue *mainQueue = [NSOperationQueue mainQueue]; - [[NSNotificationCenter defaultCenter] addObserverForName:SBQueueWorkingNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) { + [[NSNotificationCenter defaultCenter] addObserverForName:SBQueueWorkingNotification object:self.queue queue:mainQueue usingBlock:^(NSNotification *note) { NSDictionary *info = [note userInfo]; [_countLabel setStringValue:[info valueForKey:@"ProgressString"]]; [_progressIndicator setIndeterminate:NO]; [_progressIndicator setDoubleValue:[[info valueForKey:@"Progress"] doubleValue]]; }]; - [[NSNotificationCenter defaultCenter] addObserverForName:SBQueueCompletedNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) { + [[NSNotificationCenter defaultCenter] addObserverForName:SBQueueCompletedNotification object:self.queue queue:mainQueue usingBlock:^(NSNotification *note) { [_progressIndicator setHidden:YES]; [_progressIndicator stopAnimation:self]; [_progressIndicator setDoubleValue:0]; @@ -112,7 +112,7 @@ static NSString *fileType = @"mp4"; [self updateUI]; }]; - [[NSNotificationCenter defaultCenter] addObserverForName:SBQueueFailedNotification object:nil queue:mainQueue usingBlock:^(NSNotification *note) { + [[NSNotificationCenter defaultCenter] addObserverForName:SBQueueFailedNotification object:self.queue queue:mainQueue usingBlock:^(NSNotification *note) { NSDictionary *info = [note userInfo]; [NSApp presentError:[info valueForKey:@"Error"]]; }]; @@ -443,7 +443,7 @@ static NSString *fileType = @"mp4"; } - (IBAction)removeCompletedItems:(id)sender { - NSIndexSet *indexes = [self.queue removeCompletedItems]; + NSIndexSet *indexes = [self.queue indexesOfItemsWithStatus:SBQueueItemStatusCompleted]; if ([indexes count]) { if ([NSTableView instancesRespondToSelector:@selector(beginUpdates)]) { @@ -451,6 +451,7 @@ static NSString *fileType = @"mp4"; [_tableView beginUpdates]; [_tableView removeRowsAtIndexes:indexes withAnimation:NSTableViewAnimationEffectFade]; [_tableView endUpdates]; + [self.queue removeItemsAtIndexes:indexes]; #endif } else { [_tableView reloadData]; @@ -478,9 +479,14 @@ static NSString *fileType = @"mp4"; } } - if (action == @selector(edit:)) - return YES; - + if (action == @selector(edit:)) { + if ([_tableView clickedRow] != -1) { + SBQueueItem *item = [self.queue itemAtIndex:[_tableView clickedRow]]; + if (item.status == SBQueueItemStatusReady) + return YES; + } + } + if (action == @selector(removeCompletedItems:)) return YES;