-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUCPreferencesController.m
More file actions
324 lines (266 loc) · 8.71 KB
/
UCPreferencesController.m
File metadata and controls
324 lines (266 loc) · 8.71 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//
// UCPreferencesController.m
// Chain
//
// Created by Christoph on 11.06.2009.
// Copyright 2009 Useless Coding. All rights reserved.
//
#import "UCPreferencesController.h"
#import "UCChainController.h"
#import "UCFolderOperations.h"
NSString *const UCPBTypeFolderIndexSets = @"de.sigma-server.useless.chain.folder.indexset";
@implementation UCPreferencesController
+ (NSString *)folderPathForIndex:(NSUInteger)index
{
NSArray * theFolders = [[NSUserDefaults standardUserDefaults] objectForKey:UCDefaultsFoldersKey];
return [theFolders objectAtIndex:index];
}
#pragma mark -
- (id) init
{
if(self=[super init])
{
folders = [[[NSUserDefaults standardUserDefaults] objectForKey:UCDefaultsFoldersKey] mutableCopy];
}
return self;
}
- (void)dealloc
{
[activePane release];
[folders release];
[super dealloc];
}
#pragma mark -
- (void)awakeFromNib
{
[foldersTable registerForDraggedTypes:[NSArray arrayWithObjects:UCPBTypeFolderIndexSets, NSFilenamesPboardType, nil]];
[foldersTable setDraggingSourceOperationMask:NSDragOperationMove forLocal:YES];
[foldersTable setDraggingSourceOperationMask:NSDragOperationCopy forLocal:NO];
NSToolbar * toolbar = [[NSToolbar alloc] initWithIdentifier:@"UCPreferences"];
[toolbar setDelegate:self];
[toolbar setSelectedItemIdentifier:@"UCGeneralItem"];
[window setToolbar:toolbar];
[toolbar release];
activePane = [@"UCGeneralItem" retain];
[window setContentSize:[[self paneForIdentifier:activePane] frame].size];
[[window contentView] addSubview:[self paneForIdentifier:activePane]];
[[window contentView] setWantsLayer:YES];
[window center];
}
- (void)show
{
[self showAndCenter:YES];
}
- (void)showAndCenter:(BOOL)centering
{
if(window==nil)
{ [NSBundle loadNibNamed:@"Preferences" owner:self]; }
[self validateButton];
if(centering)
{
[window center];
}
[window makeKeyAndOrderFront:self];
}
- (void)showFolders
{
[self showAndCenter:NO];
[[window toolbar] setSelectedItemIdentifier:@"UCFolderItem"];
[self setPaneWithIdentifier:@"UCFolderItem"];
}
- (void)updateUserDefaults
{
[[NSUserDefaults standardUserDefaults] setObject:folders forKey:UCDefaultsFoldersKey];
[[NSNotificationCenter defaultCenter] postNotificationName:UCDefaultsChangedNotification object:self];
}
#pragma mark Actions
- (IBAction)addFolder:(id)sender
{
NSOpenPanel * op = [UCFolderOperations folderChooserWithPrompt:NSLocalizedString(@"Add", @"Panel Prompt for new target folder") allowingMultiple:YES];
[op beginSheetForDirectory:nil file:nil modalForWindow:window modalDelegate:self didEndSelector:@selector(chooseDidEnd:returnCode:contextInfo:) contextInfo:nil];
}
- (IBAction)switchPane:(id)sender
{
[self setPaneWithIdentifier:[sender itemIdentifier]];
}
#pragma mark Prefpanes
- (void)setPaneWithIdentifier:(NSString *)identifier
{
[self setPane:[self paneForIdentifier:identifier]];
[activePane release];
activePane = [identifier copy];
}
- (void)setPane:(NSView *)pane
{
NSView * oldPane = [self paneForIdentifier:activePane];
[NSAnimationContext beginGrouping];
[[[window contentView] animator] replaceSubview:oldPane with:pane];
[[window animator] setFrame:[self frameForPane:pane] display:YES];
[NSAnimationContext endGrouping];
}
- (NSView *)paneForIdentifier:(NSString *)identifier
{
if([identifier isEqualToString:@"UCGeneralItem"])
{
return prefGeneral;
}
else if([identifier isEqualToString:@"UCFolderItem"])
{
return prefFolders;
}
else
{
return nil;
}
}
- (NSRect)frameForPane:(NSView *)pane
{
NSRect theFrame = [window frame];
NSSize oldSize = [[window contentView] frame].size;
CGFloat oldHeight=theFrame.size.height;
theFrame.size.width = theFrame.size.width-oldSize.width+[pane frame].size.width;
theFrame.size.height = theFrame.size.height-oldSize.height+[pane frame].size.height;
theFrame.origin.y = theFrame.origin.y+oldHeight - theFrame.size.height;
return theFrame;
}
#pragma mark Data Source
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return [folders count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
if([[aTableColumn identifier] isEqualToString:@"UCIcon"])
{
NSImage * icon = [[NSWorkspace sharedWorkspace] iconForFile:[folders objectAtIndex:rowIndex]];
[icon setSize:NSMakeSize(16, 16)];
return icon;
}
else if([[aTableColumn identifier] isEqualToString:@"UCIndex"])
{
if(rowIndex<9)
{
return [NSString stringWithFormat:@"\u2318%d", rowIndex+1];
}
else
{
return nil;
}
}
return [[NSFileManager defaultManager] displayNameAtPath:[folders objectAtIndex:rowIndex]];
}
- (void)tableView:(NSTableView *)aTableView deleteRowsWithIndexes:(NSIndexSet *)rowIndexes
{
[folders removeObjectsAtIndexes:[foldersTable selectedRowIndexes]];
[self updateUserDefaults];
}
- (BOOL)tableView:(NSTableView *)aTableView writeRowsWithIndexes:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)pboard
{
[pboard declareTypes:[NSArray arrayWithObjects:UCPBTypeFolderIndexSets, nil] owner:self];
[pboard setData:[NSKeyedArchiver archivedDataWithRootObject:rowIndexes] forType:UCPBTypeFolderIndexSets];
return YES;
}
- (NSDragOperation)tableView:(NSTableView *)aTableView validateDrop:(id<NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)operation
{
if(aTableView==[info draggingSource] && operation==NSTableViewDropAbove)
{ return NSDragOperationMove; }
else if(operation==NSTableViewDropAbove)
{ return NSDragOperationCopy; }
return NSDragOperationNone;
}
- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id<NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)operation
{
NSPasteboard * pb = [info draggingPasteboard];
NSString * bestType;
NSIndexSet * rows;
bestType = [pb availableTypeFromArray:[NSArray arrayWithObjects:UCPBTypeFolderIndexSets, NSFilenamesPboardType, nil]];
if([bestType isEqualToString:UCPBTypeFolderIndexSets])
{
NSIndexSet * set = [NSKeyedUnarchiver unarchiveObjectWithData:[pb dataForType:bestType]];
NSArray * items = [folders objectsAtIndexes:set];
[folders removeObjectsAtIndexes:set];
row -= [set countOfIndexesInRange:NSMakeRange(0, row)];
rows = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(row, [set count])];
[folders insertObjects:items atIndexes:rows];
}
else if([bestType isEqualToString:NSFilenamesPboardType])
{
NSArray * files = [pb propertyListForType:bestType];
NSUInteger count = [files count];
NSMutableArray * goodFiles = [NSMutableArray arrayWithCapacity:count];
NSFileManager * fm = [NSFileManager defaultManager];
NSString * folder;
NSUInteger i;
BOOL isFolder;
for(i=0; i<count; i++)
{
folder = (NSString *)[files objectAtIndex:i];
if([fm fileExistsAtPath:folder isDirectory:&isFolder])
{
if(isFolder)
{ [goodFiles addObject:folder]; }
}
}
rows = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(row, [goodFiles count])];
[folders insertObjects:goodFiles atIndexes:rows];
}
else
{
return NO;
}
[aTableView reloadData];
[aTableView selectRowIndexes:rows byExtendingSelection:NO];
[self updateUserDefaults];
return YES;
}
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
{
[self validateButton];
}
#pragma mark Toolbar
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar
{
return [NSArray arrayWithObjects:@"UCGeneralItem", @"UCFolderItem", nil];
}
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
{
return [self toolbarAllowedItemIdentifiers:toolbar];
}
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
{
return [self toolbarAllowedItemIdentifiers:toolbar];
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
NSToolbarItem * item = [[NSToolbarItem alloc] initWithItemIdentifier:itemIdentifier];
if([itemIdentifier isEqualToString:@"UCGeneralItem"])
{
[item setImage:[NSImage imageNamed:NSImageNamePreferencesGeneral]];
[item setLabel:NSLocalizedString(@"General", @"General Toolbar Label")];
}
else if([itemIdentifier isEqualToString:@"UCFolderItem"])
{
[item setImage:[NSImage imageNamed:NSImageNameFolderSmart]];
[item setLabel:NSLocalizedString(@"Folders", @"Folders Toolbar Label")];
}
[item setTarget:self];
[item setAction:@selector(switchPane:)];
return [item autorelease];
}
#pragma mark Validation
- (void)validateButton
{
[removeButton setEnabled:[foldersTable selectedRow]!=-1];
}
#pragma mark -
- (void)chooseDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
[panel orderOut:self];
if(returnCode==NSOKButton)
{
[folders addObjectsFromArray:[panel filenames]];
[foldersTable reloadData];
[self updateUserDefaults];
}
}
@end