-
Notifications
You must be signed in to change notification settings - Fork 1
/
SAController.m
213 lines (186 loc) · 6.35 KB
/
SAController.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
//
// SAController.m
// Safe
//
// Created by Matt Patenaude on 3/3/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "SAController.h"
#import "SAImageTool.h"
#import "NSWindow+SAAdditions.h"
#import "MPTidbits.h"
@implementation SAController
#pragma mark Initializers
- (id)init
{
if (self = [super init])
{
hasFinishedLaunching = NO;
fileTriggeredLaunch = NO;
}
return self;
}
#pragma mark Deallocator
- (void)dealloc
{
[openingFile release];
[super dealloc];
}
#pragma mark Application delegate methods
- (BOOL)application:(NSApplication *)theApplication openFile:(NSString *)filename
{
NSFileManager *fm = [NSFileManager defaultManager];
NSString *fileType = [[filename pathExtension] lowercaseString];
if (!hasFinishedLaunching)
fileTriggeredLaunch = YES;
if ([fileType isEqualToString:@"safe"])
{
// Safe file
NSString *infoPath = [filename stringByAppendingPathComponent:@"Info.plist"];
if ([fm fileExistsAtPath:infoPath])
{
NSDictionary *infoDict = [NSDictionary dictionaryWithContentsOfFile:infoPath];
if ([infoDict containsKey:@"volumeName"] && [infoDict containsKey:@"volumeFile"])
{
NSString *imagePath = [filename stringByAppendingPathComponent:[infoDict objectForKey:@"volumeFile"]];
NSString *imageName = [infoDict objectForKey:@"volumeName"];
[self mountImage:imagePath withName:imageName];
[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:[NSURL fileURLWithPath:filename]];
return YES;
}
}
}
else if ([fileType isEqualToString:@"exc"])
{
// Exces file
NSString *imagePath = [filename stringByAppendingPathComponent:@"Main.sparseimage"];
NSString *nameFile = [filename stringByAppendingPathComponent:@"mountName"];
if ([fm fileExistsAtPath:imagePath])
{
[self mountImage:imagePath withName:[NSString stringWithContentsOfFile:nameFile encoding:NSUTF8StringEncoding error:NULL]];
[[NSDocumentController sharedDocumentController] noteNewRecentDocumentURL:[NSURL fileURLWithPath:filename]];
return YES;
}
}
return NO;
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
hasFinishedLaunching = YES;
}
#pragma mark File methods
- (IBAction)openSafe:(id)sender
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setAllowsMultipleSelection:NO];
[openPanel setTreatsFilePackagesAsDirectories:NO];
if ([openPanel runModalForTypes:[NSArray arrayWithObjects:@"safe", @"exc", nil]])
[self application:[NSApplication sharedApplication] openFile:[[[openPanel URLs] objectAtIndex:0] path]];
}
- (void)mountImage:(NSString *)filename withName:(NSString *)theName
{
[instructionField setStringValue:[NSString stringWithFormat:NSLocalizedString(@"Enter password for \"%@\"", nil), theName]];
[passwordField setStringValue:@""];
if (openingFile)
{
[openingFile release];
openingFile = nil;
}
openingFile = [filename copy];
[passwordWindow makeFirstResponder:passwordField];
[passwordWindow dialogPositionWindowOnScreen:[NSScreen mainScreen]];
[passwordWindow makeKeyAndOrderFront:nil];
}
- (IBAction)completeMount:(id)sender
{
[passwordWindow orderOut:nil];
if (openingFile)
{
NSString *result = [SAImageTool mountImageWithPath:openingFile password:[passwordField stringValue]];
[openingFile release];
openingFile = nil;
if (result != nil)
{
[[NSWorkspace sharedWorkspace] openFile:result];
if (fileTriggeredLaunch)
[[NSApplication sharedApplication] terminate:nil];
}
}
// For a little extra security
[passwordField setStringValue:@""];
}
- (IBAction)cancelMount:(id)sender
{
[passwordWindow orderOut:nil];
if (fileTriggeredLaunch)
[[NSApplication sharedApplication] terminate:nil];
// For a little extra security
[passwordField setStringValue:@""];
}
- (IBAction)newSafe:(id)sender
{
[newSafeNameField setStringValue:@""];
[newSafeEncryptionType selectItemWithTag:1];
[newSafePasswordField setStringValue:@""];
[newSafePasswordConfirmField setStringValue:@""];
[newSafeWindow makeFirstResponder:newSafeNameField];
[newSafeWindow dialogPositionWindowOnScreen:[NSScreen mainScreen]];
[newSafeWindow makeKeyAndOrderFront:nil];
}
- (IBAction)completeCreate:(id)sender
{
if (![[newSafePasswordField stringValue] isEqualToString:[newSafePasswordConfirmField stringValue]])
return;
[newSafeWindow orderOut:nil];
NSSavePanel *savePanel = [NSSavePanel savePanel];
[savePanel setAllowedFileTypes:[NSArray arrayWithObject:@"safe"]];
[savePanel setAllowsOtherFileTypes:NO];
[savePanel setTreatsFilePackagesAsDirectories:NO];
[savePanel setCanSelectHiddenExtension:YES];
[savePanel setExtensionHidden:YES];
if ([savePanel runModalForDirectory:nil file:[NSString stringWithFormat:@"%@.safe", [newSafeNameField stringValue]]])
{
NSFileManager *fm = [NSFileManager defaultManager];
NSString *path = [[savePanel URL] path];
NSString *infoPath = [path stringByAppendingPathComponent:@"Info.plist"];
NSString *imagePath = [path stringByAppendingPathComponent:@"Contents.sparseimage"];
NSString *encType = nil;
switch ([newSafeEncryptionType selectedTag])
{
case 0:
encType = @"AES-128";
break;
case 1:
encType = @"AES-256";
break;
default:
break;
}
if ([fm fileExistsAtPath:path])
{
NSError *err;
if (![fm removeItemAtPath:path error:&err])
return;
}
[fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:[savePanel isExtensionHidden]] forKey:NSFileExtensionHidden] error:NULL];
NSDictionary *infoDict = [NSDictionary dictionaryWithObjectsAndKeys:[newSafeNameField stringValue], @"volumeName", [imagePath lastPathComponent], @"volumeFile", nil];
[infoDict writeToFile:infoPath atomically:YES];
if ([SAImageTool createEmptyImageAtPath:imagePath volumeName:[newSafeNameField stringValue] encryption:encType password:[newSafePasswordField stringValue]])
{
NSString *result = [SAImageTool mountImageWithPath:imagePath password:[newSafePasswordField stringValue]];
if (result != nil)
[[NSWorkspace sharedWorkspace] openFile:result];
}
}
// For a little extra security
[newSafePasswordField setStringValue:@""];
[newSafePasswordConfirmField setStringValue:@""];
}
- (IBAction)cancelCreate:(id)sender
{
[newSafeWindow orderOut:nil];
// For a little extra security
[newSafePasswordField setStringValue:@""];
[newSafePasswordConfirmField setStringValue:@""];
}
@end