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