-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProtectionPanel.m
executable file
·228 lines (200 loc) · 6.31 KB
/
ProtectionPanel.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
This file is part of LineBreak.
LineBreak 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.
LineBreak 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 LineBreak; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Copyright (c) 2001-2008 Josh Aas.
*/
#import "ProtectionPanel.h"
#import "Prefs.h"
@implementation ProtectionPanel
static ProtectionPanel *sharedInstance = nil;
+(ProtectionPanel*)sharedInstance {
return sharedInstance ? sharedInstance : [[self alloc] init];
}
-(id)init {
if (sharedInstance) {
[self dealloc];
}
else {
sharedInstance = [super init];
}
return sharedInstance;
}
-(void)dealloc {
if (mLastExtensionValue != nil) {
[mLastExtensionValue release];
}
[super dealloc];
}
-(void)awakeFromNib {
if ([self isExcludeSelected]) {
[mOnlyOrExcludeMatrix selectCellAtRow:0 column:0];
}
else {
[mOnlyOrExcludeMatrix selectCellAtRow:1 column:0];
}
mLastExtensionValue = nil;
}
/*
ACTIONS
*/
-(IBAction)showPanel:(id)sender {
if (!mSetDefaultButton) {
NSWindow *theWindow;
[NSBundle loadNibNamed:@"ProtectionPanel" owner:self];
theWindow = [mSetDefaultButton window];
[theWindow setMenu:nil];
[theWindow center];
}
[[mSetDefaultButton window] makeKeyAndOrderFront:nil];
}
-(IBAction)addExtension:(id)sender {
int i;
BOOL badFlag = NO;
NSMutableArray *currentFileExtensions;
if ([self isExcludeSelected]) {
currentFileExtensions = [NSMutableArray arrayWithArray:[preferences arrayForKey:NonTextFileExtensionsKey]];
}
else {
currentFileExtensions = [NSMutableArray arrayWithArray:[preferences arrayForKey:TextFileExtensionsKey]];
}
for (i = 0; i < [currentFileExtensions count]; i++) {
if ([[[mExtensionTextField stringValue] lowercaseString] isEqualToString:[currentFileExtensions objectAtIndex:i]]) {
badFlag = YES;
}
}
if ([[[mExtensionTextField stringValue] lowercaseString] isEqualToString:@""]) {
badFlag = YES;
}
if (!badFlag) {
[currentFileExtensions addObject:[[mExtensionTextField stringValue] lowercaseString]];
[currentFileExtensions sortUsingSelector:@selector(compare:)];
if ([self isExcludeSelected]) {
[preferences setObject:[NSArray arrayWithArray:currentFileExtensions] forKey:NonTextFileExtensionsKey];
}
else {
[preferences setObject:[NSArray arrayWithArray:currentFileExtensions] forKey:TextFileExtensionsKey];
}
}
[mAddExtensionButton setEnabled:NO];
[mExtensionTextField setObjectValue:nil];
[mExtensionsTable reloadData];
[mExtensionsTable deselectAll:self];
}
-(IBAction)removeSelection:(id)sender {
NSMutableArray *currentFileExtensions;
NSIndexSet *selections = [mExtensionsTable selectedRowIndexes];
if ([self isExcludeSelected]) {
currentFileExtensions = [NSMutableArray arrayWithArray:[preferences arrayForKey:NonTextFileExtensionsKey]];
}
else {
currentFileExtensions = [NSMutableArray arrayWithArray:[preferences arrayForKey:TextFileExtensionsKey]];
}
NSUInteger i = [selections lastIndex];
while (i != NSNotFound) {
[currentFileExtensions removeObjectAtIndex:i];
i = [selections indexLessThanIndex:i];
}
if ([self isExcludeSelected]) {
[preferences setObject:[NSArray arrayWithArray:currentFileExtensions] forKey:NonTextFileExtensionsKey];
}
else {
[preferences setObject:[NSArray arrayWithArray:currentFileExtensions] forKey:TextFileExtensionsKey];
}
[mExtensionsTable reloadData];
[mExtensionsTable deselectAll:self];
}
-(IBAction)setDefaultExtensions:(id)sender {
if ([self isExcludeSelected]) {
[preferences removeObjectForKey:NonTextFileExtensionsKey];
}
else {
[preferences removeObjectForKey:TextFileExtensionsKey];
}
[mExtensionsTable reloadData];
}
-(IBAction)changeOnlyOrExcludeState:(id)sender {
if ([mOnlyOrExcludeMatrix selectedCell] == mOnlyRB) {
[preferences setObject:@"only" forKey:ExcludeOrOnlyFileExtensions];
}
else {
[preferences setObject:@"exclude" forKey:ExcludeOrOnlyFileExtensions];
}
[mExtensionsTable reloadData];
}
/*
METHODS
*/
-(NSArray*)getCurrentFileExtensions {
if ([self isExcludeSelected]) {
return [preferences arrayForKey:NonTextFileExtensionsKey];
}
else {
return [preferences arrayForKey:TextFileExtensionsKey];
}
}
-(BOOL)isExcludeSelected {
if ([[preferences stringForKey:ExcludeOrOnlyFileExtensions] isEqualToString:@"exclude"]) {
return YES;
}
else {
return NO;
}
}
/*
DELEGATES
*/
-(int)numberOfRowsInTableView:(NSTableView *)aTableView {
if ([self isExcludeSelected]) {
return [[preferences arrayForKey:NonTextFileExtensionsKey] count];
}
else {
return [[preferences arrayForKey:TextFileExtensionsKey] count];
}
}
-(id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex {
if ([self isExcludeSelected]) {
return [[preferences arrayForKey:NonTextFileExtensionsKey] objectAtIndex:rowIndex];
}
else {
return [[preferences arrayForKey:TextFileExtensionsKey] objectAtIndex:rowIndex];
}
}
// Don't allow table editing
-(BOOL)tableView:(NSTableView*)aTableView shouldEditTableColumn:(NSTableColumn*)aTableColumn row:(int)rowIndex {
return NO;
}
-(void)controlTextDidChange:(NSNotification*)aNotification {
if ([[mExtensionTextField stringValue] length] > 5) {
[mExtensionTextField setStringValue:mLastExtensionValue];
NSBeep();
}
if ([[mExtensionTextField stringValue] length] > 0) {
[mAddExtensionButton setEnabled:YES];
}
else {
[mAddExtensionButton setEnabled:NO];
}
if (mLastExtensionValue != nil) {
[mLastExtensionValue release];
}
mLastExtensionValue = [[mExtensionTextField stringValue] retain];
}
-(void)tableViewSelectionDidChange:(NSNotification*)aNotification {
if ([mExtensionsTable numberOfSelectedRows] > 0) {
[mRemoveSelectionButton setEnabled:YES];
}
else {
[mRemoveSelectionButton setEnabled:NO];
}
}
@end