-
Notifications
You must be signed in to change notification settings - Fork 0
/
XeeControllerFileActions.m
322 lines (253 loc) · 10.4 KB
/
XeeControllerFileActions.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
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
#import "XeeControllerFileActions.h"
#import "XeeImage.h"
#import "XeeImageSource.h"
#import "XeeDestinationList.h"
#import "XeeCollisionPanel.h"
#import "XeeRenamePanel.h"
#import "XeeDelegate.h"
#import "XeeStringAdditions.h"
@implementation XeeController (FileActions)
-(IBAction)revealInFinder:(id)sender
{
if(![self validateAction:_cmd]) { NSBeep(); return; }
NSString *filename=[window representedFilename];
if(!filename) return;
[[NSWorkspace sharedWorkspace] selectFile:filename inFileViewerRootedAtPath:nil];
}
-(IBAction)renameFileFromMenu:(id)sender
{
if(![self validateAction:_cmd]) { NSBeep(); return; }
if(!renamepanel)
{
NSNib *nib=[[[NSNib alloc] initWithNibNamed:@"RenamePanel" bundle:nil] autorelease];
[nib instantiateNibWithOwner:self topLevelObjects:nil];
}
[source setActionsBlocked:YES];
[renamepanel run:fullscreenwindow?nil:window filename:[source filenameOfCurrentImage]
delegate:self didEndSelector:@selector(renamePanelEnd:returnCode:filename:)];
}
-(void)renamePanelEnd:(XeeRenamePanel *)panel returnCode:(int)res filename:(NSString *)newname
{
if(res)
{
[self displayPossibleError:[source renameCurrentImageTo:[newname stringByMappingSlashToColon]]];
}
[source setActionsBlocked:NO];
}
-(IBAction)deleteFileFromMenu:(id)sender
{
if([source isCurrentImageRemote]) { [self askAndDelete:sender]; return; }
if(![self validateAction:_cmd]) { NSBeep(); return; }
[self setResizeBlockFromSender:sender];
[self displayPossibleError:[source deleteCurrentImage]];
[self setResizeBlock:NO];
}
-(IBAction)askAndDelete:(id)sender
{
if(![self validateAction:_cmd]) { NSBeep(); return; }
[self setResizeBlockFromSender:sender];
NSAlert *alert=[[NSAlert alloc] init];
if([source isCurrentImageRemote])
{
[alert setInformativeText:[NSString stringWithFormat:NSLocalizedString(@"Do you want to delete the image file \"%@\"?\nThe file will be removed immediately.",@"Content of the delete confirmation dialog for remote files"),
[source descriptiveNameOfCurrentImage]]];
[alert setAlertStyle:NSCriticalAlertStyle];
}
else
{
[alert setInformativeText:[NSString stringWithFormat:NSLocalizedString(@"Do you want to delete the image file \"%@\"?",@"Content of the delete confirmation dialog"),
[source descriptiveNameOfCurrentImage]]];
[alert setIcon:[[[NSImage alloc] initWithContentsOfFile:@"/System/Library/CoreServices/Dock.app/Contents/Resources/trashfull.png"] autorelease]];
}
[alert setMessageText:NSLocalizedString(@"Delete File",@"Title of the delete confirmation dialog")];
[alert addButtonWithTitle:NSLocalizedString(@"Delete",@"Delete button")];
[alert addButtonWithTitle:NSLocalizedString(@"Cancel",@"Cancel button")];
[source setActionsBlocked:YES];
if(fullscreenwindow) [self deleteAlertEnd:alert returnCode:[alert runModal] contextInfo:NULL];
else [alert beginSheetModalForWindow:window modalDelegate:self didEndSelector:@selector(deleteAlertEnd:returnCode:contextInfo:) contextInfo:NULL];
[self setResizeBlock:NO];
}
-(void)deleteAlertEnd:(NSAlert *)alert returnCode:(int)res contextInfo:(void *)info
{
if(res==NSAlertFirstButtonReturn) [self displayPossibleError:[source deleteCurrentImage]];
[source setActionsBlocked:NO];
}
-(IBAction)moveFile:(id)sender
{
if(![self validateAction:_cmd]) { NSBeep(); return; }
[self triggerDrawer:XeeMoveMode];
}
-(IBAction)copyFile:(id)sender
{
if(![self validateAction:_cmd]) { NSBeep(); return; }
[self triggerDrawer:XeeCopyMode];
}
-(IBAction)copyToDestination1:(id)sender { [self transferToDestination:1 mode:XeeCopyMode]; }
-(IBAction)copyToDestination2:(id)sender { [self transferToDestination:2 mode:XeeCopyMode]; }
-(IBAction)copyToDestination3:(id)sender { [self transferToDestination:3 mode:XeeCopyMode]; }
-(IBAction)copyToDestination4:(id)sender { [self transferToDestination:4 mode:XeeCopyMode]; }
-(IBAction)copyToDestination5:(id)sender { [self transferToDestination:5 mode:XeeCopyMode]; }
-(IBAction)copyToDestination6:(id)sender { [self transferToDestination:6 mode:XeeCopyMode]; }
-(IBAction)copyToDestination7:(id)sender { [self transferToDestination:7 mode:XeeCopyMode]; }
-(IBAction)copyToDestination8:(id)sender { [self transferToDestination:8 mode:XeeCopyMode]; }
-(IBAction)copyToDestination9:(id)sender { [self transferToDestination:9 mode:XeeCopyMode]; }
-(IBAction)copyToDestination10:(id)sender { [self transferToDestination:10 mode:XeeCopyMode]; }
-(IBAction)moveToDestination1:(id)sender { [self transferToDestination:1 mode:XeeMoveMode]; }
-(IBAction)moveToDestination2:(id)sender { [self transferToDestination:2 mode:XeeMoveMode]; }
-(IBAction)moveToDestination3:(id)sender { [self transferToDestination:3 mode:XeeMoveMode]; }
-(IBAction)moveToDestination4:(id)sender { [self transferToDestination:4 mode:XeeMoveMode]; }
-(IBAction)moveToDestination5:(id)sender { [self transferToDestination:5 mode:XeeMoveMode]; }
-(IBAction)moveToDestination6:(id)sender { [self transferToDestination:6 mode:XeeMoveMode]; }
-(IBAction)moveToDestination7:(id)sender { [self transferToDestination:7 mode:XeeMoveMode]; }
-(IBAction)moveToDestination8:(id)sender { [self transferToDestination:8 mode:XeeMoveMode]; }
-(IBAction)moveToDestination9:(id)sender { [self transferToDestination:9 mode:XeeMoveMode]; }
-(IBAction)moveToDestination10:(id)sender { [self transferToDestination:10 mode:XeeMoveMode]; }
-(void)triggerDrawer:(int)mode
{
int newseg;
if(mode==XeeCopyMode) newseg=0;
else newseg=1;
int state=[drawer state];
if(state==NSDrawerClosedState||state==NSDrawerClosingState)
{
[drawer openOnEdge:NSMaxXEdge];
NSRect frame=[window frame];
NSRect visible=[[window screen] visibleFrame];
float drawerwidth=[drawer contentSize].width+XeeDrawerEdgeWidth;
// See if the drawer fits on screen, and if not, move and resize to make it fit.
if(frame.origin.x+frame.size.width+drawerwidth>visible.origin.x+visible.size.width)
{
frame.origin.x=visible.origin.x+visible.size.width-frame.size.width-drawerwidth;
if(frame.origin.x<visible.origin.x)
{
frame.size.width+=frame.origin.x;
frame.origin.x=0;
}
[window setFrame:frame display:YES animate:YES];
}
}
else
{
if([drawerseg selectedSegment]==newseg) [closebutton performClick:nil];
}
[drawerseg setSelectedSegment:newseg];
[destinationtable switchMode:drawerseg];
}
-(void)drawerWillOpen:(NSNotification *)notification
{
[window makeFirstResponder:destinationtable];
[imageview setNextKeyView:destinationtable];
[[drawer contentView] setNextResponder:window];
}
-(void)drawerDidClose:(NSNotification *)notification
{
[imageview setNextKeyView:nil];
}
-(void)destinationListClick:(id)sender
{
if(!currimage) return;
if(fullscreenwindow) return; // just to be safe
if([drawerseg selectedSegment]==0) drawer_mode=XeeCopyMode;
else drawer_mode=XeeMoveMode;
int index=[sender selectedRow];
if(index==0)
{
NSOpenPanel *panel=[NSOpenPanel openPanel];
[panel setCanChooseDirectories:YES];
[panel setCanChooseFiles:NO];
[panel setCanCreateDirectories:YES];
if(drawer_mode==XeeMoveMode) [panel setPrompt:NSLocalizedString(@"Move",@"Move button")];
else if(drawer_mode==XeeCopyMode) [panel setPrompt:NSLocalizedString(@"Copy",@"Copy button and menuitem")];
[source setActionsBlocked:YES];
[panel beginSheetForDirectory:nil file:nil types:nil modalForWindow:window modalDelegate:self
didEndSelector:@selector(destinationPanelEnd:returnCode:contextInfo:) contextInfo:NULL];
}
else [self transferToDestination:index mode:drawer_mode];
}
-(void)destinationPanelEnd:(NSOpenPanel *)panel returnCode:(int)res contextInfo:(void *)info
{
if(res==NSOKButton)
{
NSString *destdir=[[panel filenames] objectAtIndex:0];
NSString *destination=[destdir stringByAppendingPathComponent:[source filenameOfCurrentImage]];
[XeeDestinationView suggestInsertion:destdir];
[self attemptToTransferCurrentImageTo:destination mode:drawer_mode];
}
else
{
[source setActionsBlocked:NO];
}
}
-(void)transferToDestination:(int)index mode:(int)mode
{
if(mode==XeeCopyMode&&![source canCopyCurrentImage]) { NSBeep(); return; }
else if(mode==XeeMoveMode&&![source canMoveCurrentImage]) { NSBeep(); return; }
NSString *path=[destinationtable pathForRow:index];
if(!path) { NSBeep(); return; }
NSString *destination=[path stringByAppendingPathComponent:[source filenameOfCurrentImage]];
[self attemptToTransferCurrentImageTo:destination mode:mode];
}
-(void)attemptToTransferCurrentImageTo:(NSString *)destination mode:(int)mode
{
if([source isCurrentImageAtPath:destination])
{
[self displayErrorMessage:NSLocalizedString(@"File already there",@"Title of the move/copy to same folder dialog")
text:NSLocalizedString(@"The source and destination locations are the same.",@"Content of the move/copy to same folder dialog")];
[source setActionsBlocked:NO];
return;
}
NSDictionary *destinfo=[[NSFileManager defaultManager] fileAttributesAtPath:destination traverseLink:YES];
if(destinfo)
{
if(!collisionpanel)
{
NSNib *nib=[[[NSNib alloc] initWithNibNamed:@"CollisionPanel" bundle:nil] autorelease];
[nib instantiateNibWithOwner:self topLevelObjects:nil];
}
[source setActionsBlocked:YES];
[collisionpanel run:fullscreenwindow?nil:window sourceImage:currimage
size:[source sizeOfCurrentImage] date:[source dateOfCurrentImage]
destinationPath:destination mode:mode delegate:self
didEndSelector:@selector(collisionPanelEnd:returnCode:path:mode:)];
}
else
{
// [self performSelector:@selector() withObject: afterDelay:0];
[self transferCurrentImageTo:destination mode:mode];
[source setActionsBlocked:NO];
}
}
-(void)collisionPanelEnd:(XeeCollisionPanel *)panel returnCode:(int)res path:(NSString *)destination mode:(int)mode
{
if(res==1)
{
[self transferCurrentImageTo:destination mode:mode];
[source setActionsBlocked:NO];
}
else if(res==2)
{
[self attemptToTransferCurrentImageTo:destination mode:mode];
}
else
{
[source setActionsBlocked:NO];
}
}
-(void)transferCurrentImageTo:(NSString *)destination mode:(int)mode
{
if(mode==XeeMoveMode) [self displayPossibleError:[source moveCurrentImageTo:destination]];
else [self displayPossibleError:[source copyCurrentImageTo:destination]];
}
-(IBAction)launchAppFromMenu:(id)sender
{
if(![self validateAction:_cmd]) { NSBeep(); return; }
NSString *app=[sender representedObject];
[self displayPossibleError:[source openCurrentImageInApp:app]];
}
-(IBAction)launchDefaultEditor:(id)sender
{
if(![self validateAction:_cmd]) { NSBeep(); return; }
NSString *app=[maindelegate defaultEditor];
[self displayPossibleError:[source openCurrentImageInApp:app]];
}
@end