| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2005, 2006 Stephen F. Booth <me@sbooth.org> |
|---|
| 5 | * |
|---|
| 6 | * This program is free software; you can redistribute it and/or modify |
|---|
| 7 | * it under the terms of the GNU General Public License as published by |
|---|
| 8 | * the Free Software Foundation; either version 2 of the License, or |
|---|
| 9 | * (at your option) any later version. |
|---|
| 10 | * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with this program; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #import "RenameFilesSheet.h" |
|---|
| 22 | |
|---|
| 23 | enum { |
|---|
| 24 | kTitleButtonTag = 0, |
|---|
| 25 | kArtistButtonTag = 1, |
|---|
| 26 | kAlbumButtonTag = 2, |
|---|
| 27 | kYearButtonTag = 3, |
|---|
| 28 | kGenreButtonTag = 4, |
|---|
| 29 | kComposerButtonTag = 5, |
|---|
| 30 | kMCNButtonTag = 6, |
|---|
| 31 | kISRCButtonTag = 7, |
|---|
| 32 | kEncoderButtonTag = 8, |
|---|
| 33 | kCommentButtonTag = 9, |
|---|
| 34 | kCustomButtonTag = 10, |
|---|
| 35 | kTrackNumberButtonTag = 11, |
|---|
| 36 | kTrackTotalButtonTag = 12, |
|---|
| 37 | kDiscNumberButtonTag = 13, |
|---|
| 38 | kDiscTotalButtonTag = 14, |
|---|
| 39 | kCompilationButtonTag = 15 |
|---|
| 40 | }; |
|---|
| 41 | |
|---|
| 42 | @implementation RenameFilesSheet |
|---|
| 43 | |
|---|
| 44 | - (id) init; |
|---|
| 45 | { |
|---|
| 46 | if((self = [super init])) { |
|---|
| 47 | if(NO == [NSBundle loadNibNamed:@"RenameFilesSheet" owner:self]) { |
|---|
| 48 | @throw [NSException exceptionWithName:@"MissingResourceException" reason:NSLocalizedStringFromTable(@"Unable to find the resource \"RenameFilesSheet.nib\".", @"Errors", @"") userInfo:nil]; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | return self; |
|---|
| 52 | } |
|---|
| 53 | return nil; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | - (void) setDelegate:(id <RenameFilesSheetDelegateMethods>)delegate { _delegate = delegate; } |
|---|
| 57 | - (id <RenameFilesSheetDelegateMethods>) delegate { return _delegate; } |
|---|
| 58 | |
|---|
| 59 | - (void) awakeFromNib |
|---|
| 60 | { |
|---|
| 61 | NSArray *patterns = nil; |
|---|
| 62 | NSString *pattern = nil; |
|---|
| 63 | |
|---|
| 64 | patterns = [[NSUserDefaults standardUserDefaults] stringArrayForKey:@"renameFilesPatterns"]; |
|---|
| 65 | if(0 < [patterns count]) { |
|---|
| 66 | [_pattern setStringValue:[patterns objectAtIndex:0]]; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | pattern = [_pattern stringValue]; |
|---|
| 70 | [_renameButton setEnabled:(nil != pattern && 0 != [pattern length])]; |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | - (void) showSheet |
|---|
| 74 | { |
|---|
| 75 | [[NSApplication sharedApplication] beginSheet:_sheet modalForWindow:[_delegate windowForSheet] modalDelegate:self didEndSelector:@selector(didEndSheet:returnCode:contextInfo:) contextInfo:nil]; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | - (IBAction) cancel:(id)sender |
|---|
| 79 | { |
|---|
| 80 | [[NSApplication sharedApplication] endSheet:_sheet]; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | - (IBAction) rename:(id)sender |
|---|
| 84 | { |
|---|
| 85 | NSString *pattern = [_pattern stringValue]; |
|---|
| 86 | NSMutableArray *patterns = nil; |
|---|
| 87 | |
|---|
| 88 | patterns = [[[[NSUserDefaults standardUserDefaults] arrayForKey:@"renameFilesPatterns"] mutableCopy] autorelease]; |
|---|
| 89 | |
|---|
| 90 | if([patterns containsObject:pattern]) { |
|---|
| 91 | [patterns removeObject:pattern]; |
|---|
| 92 | } |
|---|
| 93 | |
|---|
| 94 | [patterns insertObject:pattern atIndex:0]; |
|---|
| 95 | |
|---|
| 96 | while(10 < [patterns count]) { |
|---|
| 97 | [patterns removeLastObject]; |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | [[NSUserDefaults standardUserDefaults] setObject:patterns forKey:@"renameFilesPatterns"]; |
|---|
| 101 | |
|---|
| 102 | [_delegate renameFilesUsingPattern:pattern]; |
|---|
| 103 | [[NSApplication sharedApplication] endSheet:_sheet]; |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | - (void) didEndSheet:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo |
|---|
| 107 | { |
|---|
| 108 | [sheet orderOut:self]; |
|---|
| 109 | [self release]; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | - (IBAction) patternTokenButtonClicked:(id)sender |
|---|
| 113 | { |
|---|
| 114 | NSString *string = nil; |
|---|
| 115 | NSText *fieldEditor; |
|---|
| 116 | |
|---|
| 117 | switch([(NSButton *)sender tag]) { |
|---|
| 118 | case kTitleButtonTag: string = @"{title}"; break; |
|---|
| 119 | case kArtistButtonTag: string = @"{artist}"; break; |
|---|
| 120 | case kAlbumButtonTag: string = @"{album}"; break; |
|---|
| 121 | case kYearButtonTag: string = @"{year}"; break; |
|---|
| 122 | case kGenreButtonTag: string = @"{genre}"; break; |
|---|
| 123 | case kComposerButtonTag: string = @"{composer}"; break; |
|---|
| 124 | case kMCNButtonTag: string = @"{MCN}"; break; |
|---|
| 125 | case kISRCButtonTag: string = @"{ISRC}"; break; |
|---|
| 126 | case kEncoderButtonTag: string = @"{encoder}"; break; |
|---|
| 127 | case kCommentButtonTag: string = @"{comment}"; break; |
|---|
| 128 | case kCustomButtonTag: string = @"{custom}"; break; |
|---|
| 129 | case kTrackNumberButtonTag: string = @"{trackNumber}"; break; |
|---|
| 130 | case kTrackTotalButtonTag: string = @"{trackTotal}"; break; |
|---|
| 131 | case kDiscNumberButtonTag: string = @"{discNumber}"; break; |
|---|
| 132 | case kDiscTotalButtonTag: string = @"{discTotal}"; break; |
|---|
| 133 | case kCompilationButtonTag: string = @"{compilation}"; break; |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | fieldEditor = [_pattern currentEditor]; |
|---|
| 137 | if(nil == fieldEditor) { |
|---|
| 138 | [_pattern setStringValue:string]; |
|---|
| 139 | } |
|---|
| 140 | else { |
|---|
| 141 | if([_pattern textShouldBeginEditing:fieldEditor]) { |
|---|
| 142 | [fieldEditor replaceCharactersInRange:[fieldEditor selectedRange] withString:string]; |
|---|
| 143 | [_pattern textShouldEndEditing:fieldEditor]; |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | [_renameButton setEnabled:YES]; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | - (void) controlTextDidChange:(NSNotification *)aNotification |
|---|
| 151 | { |
|---|
| 152 | NSString *pattern = [_pattern stringValue]; |
|---|
| 153 | [_renameButton setEnabled:(nil != pattern && 0 != [pattern length])]; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | - (void) comboBoxSelectionDidChange:(NSNotification *)aNotification |
|---|
| 157 | { |
|---|
| 158 | NSString *pattern; |
|---|
| 159 | |
|---|
| 160 | [_pattern setObjectValue:[_pattern objectValueOfSelectedItem]]; |
|---|
| 161 | pattern = [_pattern stringValue]; |
|---|
| 162 | [_renameButton setEnabled:(nil != pattern && 0 != [pattern length])]; |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | @end |
|---|