forked from CocoaBob/xee
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathXeeCollisionPanel.m
152 lines (119 loc) · 4.51 KB
/
XeeCollisionPanel.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
#import "XeeCollisionPanel.h"
#import "XeeImage.h"
#import "XeeView.h"
#import "XeeControllerFileActions.h"
#import "XeeStringAdditions.h"
@implementation XeeCollisionPanel
-(void)run:(NSWindow *)window sourceImage:(XeeImage *)srcimage size:(off_t)srcsize
date:(NSDate *)srcdate destinationPath:(NSString *)destpath mode:(int)mode
delegate:(id)delegate didEndSelector:(SEL)selector
{
enddelegate=delegate;
endselector=selector;
destinationpath=[destpath retain];
transfermode=mode;
XeeImage *destimage=[XeeImage imageForFilename:destpath];
if(destimage)
{
float horiz_zoom=128/(float)[destimage width];
float vert_zoom=128/(float)[destimage height];
float min_zoom=horiz_zoom<vert_zoom?horiz_zoom:vert_zoom;
float zoom;
if(min_zoom<1) zoom=min_zoom;
else zoom=1;
[icon setImage:destimage];
[icon setImageSize:NSMakeSize(zoom*(float)[destimage width],zoom*(float)[destimage height])];
[NSThread detachNewThreadSelector:@selector(loadThumbnail:) toTarget:self withObject:destimage];
}
else
{
// TODO: display icon
/* NSImage *iconimage=[[NSWorkspace sharedWorkspace] iconForFile:destination];
[iconimage setSize:NSMakeSize(128,128)];
[icon setImage:[XeeNSImageiconimage];*/
[icon setImage:nil];
}
NSDictionary *destattrs=[[NSFileManager defaultManager] fileAttributesAtPath:destpath traverseLink:YES];
[titlefield setStringValue:[NSString stringWithFormat:NSLocalizedString(@"The file \"%@\" already exists.",
@"Title of the file exists dialog"),[[destpath lastPathComponent] stringByMappingColonToSlash]]];
[oldsize setStringValue:[NSString stringWithFormat:@"%qu",[destattrs fileSize]]];
[newsize setStringValue:[NSString stringWithFormat:@"%qu",srcsize]];
[olddate setStringValue:XeeDescribeDate([destattrs fileModificationDate])];
[newdate setStringValue:XeeDescribeDate(srcdate)];
if(destimage)
{
[oldformat setStringValue:[NSString stringWithFormat:@"%dx%d\n%@ %@",
[destimage width],[destimage height],[destimage depth],[destimage format]]];
}
else [oldformat setStringValue:@""];
if(srcimage)
{
[newformat setStringValue:[NSString stringWithFormat:@"%dx%d\n%@ %@",
[srcimage width],[srcimage height],[srcimage depth],[srcimage format]]];
}
else [newformat setStringValue:@""];
NSString *newpath;
NSString *destdir=[destpath stringByDeletingLastPathComponent];
NSString *destname=[[destpath lastPathComponent] stringByDeletingPathExtension];
NSString *destext=[destpath pathExtension];
int n=1;
do { newpath=[destdir stringByAppendingPathComponent:[[NSString stringWithFormat:@"%@-%d",destname,n++] stringByAppendingPathExtension:destext]]; }
while([[NSFileManager defaultManager] fileExistsAtPath:newpath]);
[namefield setStringValue:[[newpath lastPathComponent] stringByMappingColonToSlash]];
[self makeFirstResponder:namefield];
[[namefield currentEditor] setSelectedRange:NSMakeRange(0,[[[namefield stringValue] stringByDeletingPathExtension] length])];
[replacebutton setKeyEquivalent:@"\r"];
[renamebutton setKeyEquivalent:@""];
if(window)
{
sheet=YES;
[NSApp beginSheet:self modalForWindow:window modalDelegate:nil didEndSelector:nil contextInfo:nil];
}
else
{
sheet=NO;
[self makeKeyAndOrderFront:nil];
}
}
-(void)loadThumbnail:(XeeImage *)image
{
NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
[image retain];
[NSThread setThreadPriority:0.1];
[image runLoaderForThumbnail];
[image release];
[pool release];
}
-(IBAction)cancelClick:(id)sender
{
[self endWithReturnCode:0 path:nil];
}
-(IBAction)renameClick:(id)sender
{
[self endWithReturnCode:2 path:[[destinationpath stringByDeletingLastPathComponent]
stringByAppendingPathComponent:[[namefield stringValue] stringByMappingSlashToColon]]];
}
-(IBAction)replaceClick:(id)sender
{
[self endWithReturnCode:1 path:destinationpath];
}
-(void)endWithReturnCode:(int)res path:(NSString *)destination
{
if(sheet) [NSApp endSheet:self];
[self orderOut:nil];
[icon setImage:nil];
[destinationpath autorelease]; // Avoid releasing the destination string when clicking replace
NSInvocation *invocation=[NSInvocation invocationWithMethodSignature:[enddelegate methodSignatureForSelector:endselector]];
[invocation setSelector:endselector];
[invocation setArgument:&self atIndex:2];
[invocation setArgument:&res atIndex:3];
[invocation setArgument:&destination atIndex:4];
[invocation setArgument:&transfermode atIndex:5];
[invocation invokeWithTarget:enddelegate];
}
-(void)controlTextDidChange:(NSNotification *)notification
{
[replacebutton setKeyEquivalent:@""];
[renamebutton setKeyEquivalent:@"\r"];
}
@end