-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUCFileBrowser.m
More file actions
328 lines (275 loc) · 8.07 KB
/
UCFileBrowser.m
File metadata and controls
328 lines (275 loc) · 8.07 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
325
326
327
328
//
// UCFileBrowser.m
// Chain
//
// Created by Christoph on 11.06.2009.
// Copyright 2009-2010 Useless Coding. All rights reserved.
//
#import "UCFileBrowser.h"
#import "UCPreferencesController.h"
@implementation UCFileBrowser
- (id)init
{
if(self=[super init])
{
folderOperations = nil;
}
return self;
}
- (void)dealloc
{
[folderOperations release];
[fileList release];
[super dealloc];
}
#pragma mark -
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
[self currentFileDidChange];
NSLog(@"Did load Nib");
// Toolbar?!
}
- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError
{
NSString * path = [absoluteURL path];
fileList = [[UCFileList alloc] initForPath:path withTypes:[self filterTypes]];
if(fileList==nil)
{
if(outError!=NULL)
{
*outError = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileNoSuchFileError userInfo:nil];
}
return NO;
}
if([fileList currentFile]==nil)
{
if(outError!=NULL)
{
*outError = [NSError errorWithDomain:@"UCChainErrors" code:42 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
NSLocalizedString(@"The folder contains no files of a suitable kind.", @"Unable to open folder reason"), NSLocalizedFailureReasonErrorKey,
NSLocalizedString(@"Try opening a file by choosing the Open command from the File menu.", @"Unable to open folder suggestion"), NSLocalizedRecoverySuggestionErrorKey,
nil]];
}
return NO;
}
else
{
[self setFileURL:[NSURL fileURLWithPath:[fileList currentFile]]];
}
return YES;
}
#pragma mark Document
- (NSArray *)filterTypes
{
return [NSArray arrayWithObject:(NSString *)kUTTypeData];
}
- (void)setCurrentFile:(NSString *)filePath
{
[self setFileURL:[NSURL fileURLWithPath:filePath]];
[[self windowControllers] makeObjectsPerformSelector:@selector(synchronizeWindowTitleWithDocumentName)];
[self currentFileDidChange];
}
- (void)currentFileDidChange
{
}
- (UCFolderOperations *)folderOperations
{
if(folderOperations==nil)
{
folderOperations = [[UCFolderOperations alloc] init];
}
return folderOperations;
}
- (NSUInteger)fileCount
{
return fileList.count;
}
- (NSInteger)currentFileIndex
{
return fileList.currentIndex;
}
#pragma mark File Operations
- (void)copy:(BOOL)copying confirmedFileTo:(NSString *)aFolder withName:(NSString *)newName
{
if(newName==nil)
{
newName = [[[self fileURL] path] lastPathComponent];
}
NSString * suggestedName;
if([UCFolderOperations canHaveFileNamed:newName inFolder:aFolder suggestedName:&suggestedName])
{
if(copying)
{
[self copyFileTo:aFolder withName:newName];
}
else
{
[self moveFileTo:aFolder withName:newName];
}
}
else
{
NSAlert * alert = [[self folderOperations] conflictSheetForName:newName inFolder:aFolder withSuggestion:suggestedName];
[alert beginSheetModalForWindow:[self windowForSheet] modalDelegate:self didEndSelector:@selector(conflictDidEnd:returnCode:contextInfo:) contextInfo:[[NSDictionary alloc] initWithObjectsAndKeys:
newName, @"UCConflictName",
aFolder, @"UCTargetFolder",
copying?@"YES":@"NO", @"UCCopying",
nil]];
[[self folderOperations] activateSheet:alert];
}
}
- (void)moveConfirmedFileTo:(NSString *)aFolder withName:(NSString *)newName
{
[self copy:NO confirmedFileTo:aFolder withName:newName];
}
- (void)copyConfirmedFileTo:(NSString *)aFolder withName:(NSString *)newName
{
[self copy:YES confirmedFileTo:aFolder withName:newName];
}
- (void)moveFileTo:(NSString *)aFolder withName:(NSString *)newName
{
// delegate to UCFolderOperations
// handle Errors here
[[NSFileManager defaultManager] moveItemAtPath:[[self fileURL] path] toPath:[aFolder stringByAppendingPathComponent:newName] error:NULL];
//[self gotoNext:self];
NSLog(@"%@ -=> %@ (%@).", [self fileURL], newName, aFolder);
}
- (void)copyFileTo:(NSString *)aFolder withName:(NSString *)newName
{
[[NSFileManager defaultManager] copyItemAtPath:[[self fileURL] path] toPath:[aFolder stringByAppendingPathComponent:newName] error:NULL];
NSLog(@"%@ +=> %@ (%@).", [self fileURL], newName, aFolder);
}
#pragma mark Actions
- (IBAction)gotoFirst:(id)sender
{
[fileList setCurrentIndex:0];
[self setCurrentFile:[fileList currentFile]];
}
- (IBAction)gotoPrevious:(id)sender
{
NSUInteger prev = fileList.currentIndex-1;
if(prev==-1)
{
[fileList setCurrentIndex:fileList.count-1];
[self setCurrentFile:[fileList currentFile]];
}
else
{
[fileList setCurrentIndex:prev];
[self setCurrentFile:[fileList currentFile]];
}
}
- (IBAction)gotoNext:(id)sender
{
NSUInteger next = fileList.currentIndex+1;
if(next==fileList.count)
{
[fileList setCurrentIndex:0];
[self setCurrentFile:[fileList currentFile]];
}
else
{
[fileList setCurrentIndex:next];
[self setCurrentFile:[fileList currentFile]];
}
}
- (IBAction)gotoLast:(id)sender
{
[fileList setCurrentIndex:fileList.count-1];
[self setCurrentFile:[fileList currentFile]];
}
- (IBAction)gotoIndex:(id)sender
{
}
#pragma mark -
- (IBAction)moveTo:(id)sender
{
if([sender tag]==0)
{
NSOpenPanel * op = [UCFolderOperations folderChooserWithPrompt:NSLocalizedString(@"Move", @"Panel prompt for other Move target")];
[op beginSheetForDirectory:nil file:nil modalForWindow:[self windowForSheet] modalDelegate:self didEndSelector:@selector(chooseDidEnd:returnCode:copying:) contextInfo:NO];
}
else
{
[self moveConfirmedFileTo:[UCPreferencesController folderPathForIndex:[sender tag]-1] withName:nil];
}
}
- (IBAction)copyTo:(id)sender
{
if([sender tag]==0)
{
NSOpenPanel * op = [UCFolderOperations folderChooserWithPrompt:NSLocalizedString(@"Copy", @"Panel prompt for other Copy target")];
[op beginSheetForDirectory:nil file:nil modalForWindow:[self windowForSheet] modalDelegate:self didEndSelector:@selector(chooseDidEnd:returnCode:copying:) contextInfo:(void *)YES];
}
else
{
[self copyConfirmedFileTo:[UCPreferencesController folderPathForIndex:[sender tag]-1] withName:nil];
}
}
- (IBAction)moveAgain:(id)sender
{
}
- (IBAction)moveToTrash:(id)sender
{
}
- (IBAction)rename:(id)sender
{
NSAlert * alert = [[self folderOperations] renameSheetForFileURL:[self fileURL] withDisplayName:[self displayName]];
[alert beginSheetModalForWindow:[self windowForSheet] modalDelegate:self didEndSelector:@selector(renameDidEnd:returnCode:contextInfo:) contextInfo:nil];
[[self folderOperations] activateSheet:alert];
}
#pragma mark Sheet Handling
- (void)renameDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
[[alert window] orderOut:self];
if(returnCode==NSAlertFirstButtonReturn) // Rename
{
[self moveConfirmedFileTo:fileList.folder withName:[[self folderOperations] sheetNameValue]];
}
}
- (void)conflictDidEnd:(NSAlert *)alert returnCode:(int)returnCode contextInfo:(NSDictionary *)contextInfo
{
[[alert window] orderOut:self];
NSString * targetFolder = [[[contextInfo objectForKey:@"UCTargetFolder"] retain] autorelease];
NSString * conflictName = [[[contextInfo objectForKey:@"UCConflictName"] retain] autorelease];
BOOL copying = [[contextInfo objectForKey:@"UCCopying"] boolValue];
[contextInfo release];
if(returnCode==NSAlertFirstButtonReturn) // Copy/Move with new Name
{
[self copy:copying confirmedFileTo:targetFolder withName:[[self folderOperations] sheetNameValue]];
}
else if(returnCode==NSAlertThirdButtonReturn) // Copy/Move Replacing
{
if(copying)
{
[self copyFileTo:targetFolder withName:conflictName];
}
else
{
[self moveFileTo:targetFolder withName:conflictName];
}
}
}
- (void)chooseDidEnd:(NSOpenPanel *)panel returnCode:(int)returnCode copying:(BOOL)copying
{
[panel orderOut:self];
if(returnCode==NSOKButton)
{
[self copy:copying confirmedFileTo:[[panel filenames] objectAtIndex:0] withName:nil];
}
}
#pragma mark Validation
- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)anItem
{
if([anItem action]==@selector(gotoLast:))
{
return fileList.currentIndex!=fileList.count-1;
}
else if([anItem action]==@selector(gotoFirst:))
{
return fileList.currentIndex!=0;
}
return YES;
}
@end