From 119c818d51b6721a07e259d85da2a7fa661f2200 Mon Sep 17 00:00:00 2001 From: Damiano Galassi Date: Sun, 13 Sep 2015 08:05:35 +0200 Subject: [PATCH] Add a new sheet to configure the media characterist tags --- Classes/SBAppDelegate.m | 8 +- Classes/SBDocument.h | 2 +- Classes/SBDocument.m | 50 +++++++--- Classes/SBMediaTagsController.h | 17 +++- Classes/SBMediaTagsController.m | 171 ++++++++++++++++++++++++++++++-- English.lproj/MainMenu.xib | 10 +- English.lproj/SBDocument.xib | 17 +++- 7 files changed, 245 insertions(+), 30 deletions(-) diff --git a/Classes/SBAppDelegate.m b/Classes/SBAppDelegate.m index 305fe8d..3e58be7 100644 --- a/Classes/SBAppDelegate.m +++ b/Classes/SBAppDelegate.m @@ -222,11 +222,17 @@ - (nullable __kindof NSDocument *)documentForURL:(NSURL *)url { - for (NSDocument *doc in self.documents) { + NSArray<__kindof NSDocument *> *documents = nil; + @synchronized(self) { + documents = [self.documents copy]; + } + + for (NSDocument *doc in documents) { if ([doc.fileURL isEqualTo:url.filePathURL]) { return doc; } } + return nil; } diff --git a/Classes/SBDocument.h b/Classes/SBDocument.h index 417871b..7c898bb 100755 --- a/Classes/SBDocument.h +++ b/Classes/SBDocument.h @@ -37,7 +37,7 @@ NS_ASSUME_NONNULL_BEGIN NSViewController *propertyView; IBOutlet NSView *targetView; - id importWindow; + NSWindowController *_sheet; IBOutlet NSWindow *offsetWindow; IBOutlet NSTextField *offset; diff --git a/Classes/SBDocument.m b/Classes/SBDocument.m index c858a46..e5713a1 100755 --- a/Classes/SBDocument.m +++ b/Classes/SBDocument.m @@ -20,6 +20,8 @@ #import "SBMetadataSearchController.h" #import "SBArtworkSelector.h" +#import "SBMediaTagsController.h" + #import #import #import @@ -86,7 +88,7 @@ + (BOOL)canConcurrentlyReadDocumentsOfType:(NSString *)type { - return YES; + return NO; } - (BOOL)isEntireFileLoaded @@ -373,6 +375,9 @@ if (action == @selector(showTrackOffsetSheet:) && [fileTracksTable selectedRow] != -1) return YES; + if (action == @selector(showMediaCharacteristicTags:) && [fileTracksTable selectedRow] != -1) + return YES; + if (action == @selector(addChaptersEvery:)) return YES; @@ -593,8 +598,8 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn - (void)didEndSheet:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo { - [[importWindow window] orderOut:nil]; - [importWindow autorelease], importWindow = nil; + [_sheet.window orderOut:nil]; + [_sheet autorelease], _sheet = nil; [fileTracksTable reloadData]; [self reloadPropertyView]; @@ -640,9 +645,9 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn filename = self.fileURL.lastPathComponent; } - importWindow = [[SBMetadataSearchController alloc] initWithDelegate:self searchString:filename]; + _sheet = [[SBMetadataSearchController alloc] initWithDelegate:self searchString:filename]; - [NSApp beginSheet:[importWindow window] + [NSApp beginSheet:_sheet.window modalForWindow:documentWindow modalDelegate:self didEndSelector:@selector(didEndSheet:returnCode:contextInfo:) @@ -686,7 +691,7 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn [self updateChangeCount:NSChangeDone]; - [NSApp endSheet: offsetWindow]; + [NSApp endSheet:offsetWindow]; [offsetWindow orderOut:self]; } @@ -696,6 +701,26 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn [offsetWindow orderOut:self]; } +- (IBAction)showMediaCharacteristicTags:(id)sender +{ + _sheet = [[SBMediaTagsController alloc] initWithTrack:self.mp4.tracks[fileTracksTable.selectedRow]]; + + [NSApp beginSheet:_sheet.window + modalForWindow:documentWindow + modalDelegate:self + didEndSelector:@selector(mediaSheetDidEnd:returnCode:contextInfo:) + contextInfo:nil]; +} + +- (void)mediaSheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo { + if (returnCode == NSOKButton) { + [self updateChangeCount:NSChangeDone]; + } + [_sheet.window orderOut:self]; + [_sheet release]; + _sheet = nil; +} + - (IBAction)deleteTrack:(id)sender { if ([fileTracksTable selectedRow] == -1 || [fileTracksTable editedRow] != -1) { @@ -771,15 +796,15 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn { NSError *error = nil; - importWindow = [[SBFileImport alloc] initWithURLs:fileURLs delegate:self error:&error]; + _sheet = [[SBFileImport alloc] initWithURLs:fileURLs delegate:self error:&error]; - if (importWindow) { - if ([importWindow onlyContainsSubtitleTracks]) { - [importWindow addTracks:self]; + if (_sheet) { + if ([(SBFileImport *)_sheet onlyContainsSubtitleTracks]) { + [(SBFileImport *)_sheet addTracks:self]; [self didEndSheet:nil returnCode:NSOKButton contextInfo:nil]; } else { // show the dialog - [NSApp beginSheet:[importWindow window] modalForWindow:documentWindow + [NSApp beginSheet:_sheet.window modalForWindow:documentWindow modalDelegate:self didEndSelector:@selector(didEndSheet:returnCode:contextInfo:) contextInfo:NULL]; } } @@ -968,9 +993,6 @@ objectValueForTableColumn:(NSTableColumn *)tableColumn - (void)dealloc { - [fileTracksTable setDelegate:nil]; - [fileTracksTable setDataSource:nil]; - [propertyView release]; propertyView = nil; diff --git a/Classes/SBMediaTagsController.h b/Classes/SBMediaTagsController.h index a0572ce..d79acd0 100644 --- a/Classes/SBMediaTagsController.h +++ b/Classes/SBMediaTagsController.h @@ -8,6 +8,21 @@ #import -@interface SBMediaTagsController : NSWindowController +NS_ASSUME_NONNULL_BEGIN + +@class MP42Track; +@class SBMediaTag; + +@interface SBMediaTagsController : NSWindowController { + @private + MP42Track *_track; + NSArray *_tags; + + IBOutlet NSTableView *_tableView; +} + +- (instancetype)initWithTrack:(MP42Track *)track; @end + +NS_ASSUME_NONNULL_END diff --git a/Classes/SBMediaTagsController.m b/Classes/SBMediaTagsController.m index 13903c1..774ab8e 100644 --- a/Classes/SBMediaTagsController.m +++ b/Classes/SBMediaTagsController.m @@ -7,17 +7,176 @@ // #import "SBMediaTagsController.h" +#import -@interface SBMediaTagsController () +#pragma mark - Helpers + +@interface SBMediaTag : NSObject { + BOOL _state; + NSString *_name; + NSString *_localizedName; + NSString *_localizedDescription; + +} + +@property (nonatomic, readwrite) BOOL state; +@property (nonatomic, readonly, nonnull) NSString *name; @end -@implementation SBMediaTagsController +@implementation SBMediaTag -- (void)windowDidLoad { - [super windowDidLoad]; - - // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file. +- (instancetype)initWithName:(NSString *)name state:(BOOL)state { + self = [super init]; + if (self) { + _state = state; + _name = [name copy]; + } + + return self; +} + +@synthesize state = _state; +@synthesize name = _name; + +- (void)dealloc { + [_name release]; + _name = nil; + [super dealloc]; +} + +@end + +@interface SBCheckBoxTableCellView : NSTableCellView { + IBOutlet NSButton *_checkBox; + SBMediaTag *_representedTag; +} + + +@property (nonatomic, readwrite, retain, nonnull) SBMediaTag *representedTag; + +@end + +@implementation SBCheckBoxTableCellView + +@synthesize representedTag = _representedTag; + +- (void)setRepresentedTag:(SBMediaTag *)representedTag { + [_representedTag autorelease]; + _representedTag = [representedTag retain]; + + _checkBox.title = _representedTag.name; + _checkBox.state = _representedTag.state; +} + +@end + +@interface SBMediaTagsController () + +@property (nonatomic, readonly, nonnull) MP42Track *track; +@property (nonatomic, readonly, nonnull) NSArray *tags; + +@end + +#pragma mark - Main Class + +@implementation SBMediaTagsController + +- (instancetype)init { + self = [super initWithWindowNibName:@"SBMediaTagsController"]; + if (self) { + } + return self; +} + +- (instancetype)initWithTrack:(MP42Track *)track { + self = [self init]; + if (self) { + _track = [track retain]; + NSArray *predefinedTags = @[@"public.main-program-content", @"public.auxiliary-content", + @"public.subtitles.forced-only", @"public.accessibility.transcribes-spoken-dialog", + @"public.accessibility.describes-music-and-sound", @"public.easy-to-read", + @"public.accessibility.describes-video", @"public.translation.dubbed", + @"public.translation.voice-over", @"public.translation"]; + + NSMutableArray *tags = [[NSMutableArray alloc] init]; + + // Add the predefined tags + for (NSString *availableTag in predefinedTags) { + BOOL state = [track.mediaCharacteristicTags containsObject:availableTag] ? YES : NO; + SBMediaTag *tag = [[SBMediaTag alloc] initWithName:availableTag state:state]; + [tags addObject:tag]; + [tag release]; + } + + // Keep the custom ones if present + NSMutableSet *custom = [track.mediaCharacteristicTags mutableCopy]; + [custom minusSet:[NSSet setWithArray:predefinedTags]]; + + for (NSString *customTag in custom) { + SBMediaTag *tag = [[SBMediaTag alloc] initWithName:customTag state:YES]; + [tags addObject:tag]; + [tag release]; + } + + [custom release]; + + _tags = [tags copy]; + [tags release]; + } + return self; +} + +@synthesize track = _track; +@synthesize tags = _tags; + +- (void)dealloc { + [_tableView setDelegate:nil]; + [_tableView setDataSource:nil]; + [_track release]; + _track = nil; + [_tags release]; + _tags = nil; + + [super dealloc]; +} + +- (void)windowDidLoad { + [super windowDidLoad]; +} + +- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView { + return self.tags.count; +} + +- (nullable NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row { + SBCheckBoxTableCellView *result = [tableView makeViewWithIdentifier:@"SBCheckBoxTableCellView" owner:self]; + result.representedTag = self.tags[row]; + + return result; +} + +- (IBAction)setTagState:(NSButton *)sender { + NSInteger row = [_tableView rowForView:sender]; + self.tags[row].state = sender.state; +} + +- (IBAction)done:(id)sender { + NSMutableSet *set = [NSMutableSet set]; + + for (SBMediaTag *tag in self.tags) { + if (tag.state == YES) { + [set addObject:tag.name]; + } + } + + self.track.mediaCharacteristicTags = set; + + [NSApp endSheet:self.window returnCode:NSOKButton]; +} + +- (IBAction)cancel:(id)sender{ + [NSApp endSheet:self.window returnCode:NSCancelButton]; } @end diff --git a/English.lproj/MainMenu.xib b/English.lproj/MainMenu.xib index f08b274..b84f3c8 100755 --- a/English.lproj/MainMenu.xib +++ b/English.lproj/MainMenu.xib @@ -1,8 +1,8 @@ - + - + @@ -242,6 +242,12 @@ + + + + + + diff --git a/English.lproj/SBDocument.xib b/English.lproj/SBDocument.xib index 1fce1e1..2d35531 100755 --- a/English.lproj/SBDocument.xib +++ b/English.lproj/SBDocument.xib @@ -1,8 +1,8 @@ - + - + @@ -213,7 +213,7 @@ - + @@ -274,10 +274,17 @@ - + + - + + + + + + +