forked from ttscoff/nv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPassphraseRetriever.m
executable file
·129 lines (95 loc) · 4.23 KB
/
PassphraseRetriever.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
/*Copyright (c) 2010, Zachary Schneirov. All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of conditions
and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list of
conditions and the following disclaimer in the documentation and/or other materials provided with
the distribution.
- Neither the name of Notational Velocity nor the names of its contributors may be used to endorse
or promote products derived from this software without specific prior written permission. */
#import "PassphraseRetriever.h"
#import "GlobalPrefs.h"
#import "NotationPrefs.h"
#import "NSData_transformations.h"
#import "NSString_NV.h"
#import "NSFileManager_NV.h"
@implementation PassphraseRetriever
+ (PassphraseRetriever *)retrieverWithNotationPrefs:(NotationPrefs*)prefs {
PassphraseRetriever *retriever = [[PassphraseRetriever alloc] initWithNotationPrefs:prefs];
return [retriever autorelease];
}
- (id)initWithNotationPrefs:(NotationPrefs*)prefs {
if (self=[super init]) {
notationPrefs = [prefs retain];
return self;
}
return nil;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[notationPrefs release];
[super dealloc];
}
//1 for OK, 0 for cancelled, some other number for something else
- (int)loadedUserPassphraseData {
if (!window) {
if (![NSBundle loadNibNamed:@"PassphraseRetriever" owner:self]) {
NSLog(@"Failed to load PassphraseRetriever.nib");
NSBeep();
return 0;
}
}
NSString *startingDirectory = NSLocalizedString(@"the current notes directory",nil);
FSRef notesDirectoryRef;
if ([[[notationPrefs delegate] aliasDataForNoteDirectory] fsRefAsAlias:¬esDirectoryRef]) {
NSString *resolvedPath = [[NSFileManager defaultManager] pathWithFSRef:¬esDirectoryRef];
if (resolvedPath) startingDirectory = resolvedPath;
}
[helpStringField setStringValue:[NSString stringWithFormat:NSLocalizedString(@"Please enter the passphrase to access notes in %@.",nil),
[startingDirectory stringByAbbreviatingWithTildeInPath]]];
BOOL notationExists = [[GlobalPrefs defaultPrefs] notationPrefs] != nil;
[cancelButton setKeyEquivalent: notationExists ? @"\033" : @"q"];
[cancelButton setKeyEquivalentModifierMask: notationExists ? 0 : NSCommandKeyMask];
[cancelButton setTitle: notationExists ? NSLocalizedString(@"Cancel",nil) : NSLocalizedString(@"Quit",nil)];
[cancelButton setTarget: notationExists ? self : NSApp];
[cancelButton setAction: notationExists ? @selector(cancelAction:) : @selector(terminate:)];
[differentFolderButton setHidden: notationExists];
[rememberKeychainButton setState:[notationPrefs storesPasswordInKeychain]];
int result = [NSApp runModalForWindow:window];
[passphraseField setStringValue:@""];
[self textDidChange:nil];
return result;
}
- (void)awakeFromNib {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChange:)
name:NSControlTextDidChangeNotification object:passphraseField];
}
- (void)textDidChange:(NSNotification *)aNotification {
[okButton setEnabled:([[passphraseField stringValue] length] > 0)];
}
- (IBAction)cancelAction:(id)sender {
[NSApp stopModalWithCode:0];
[window close];
}
- (IBAction)differentNotes:(id)sender {
//ask the user to choose a different folder
[NSApp stopModalWithCode:0];
[window close];
}
- (IBAction)okAction:(id)sender {
NSData *passData = [[passphraseField stringValue] dataUsingEncoding:NSUTF8StringEncoding];
if ([notationPrefs canLoadPassphraseData:passData]) {
if ([rememberKeychainButton state])
[notationPrefs setKeychainData:passData];
[notationPrefs setStoresPasswordInKeychain:[rememberKeychainButton state]];
[NSApp stopModalWithCode:1];
[window close];
} else {
NSBeginAlertSheet(NSLocalizedString(@"Sorry, you entered an incorrect passphrase.",nil), NSLocalizedString(@"OK",nil),
nil, nil, window, nil, NULL, NULL, NULL, NSLocalizedString(@"Please try again.",nil));
[passphraseField setStringValue:@""];
[self textDidChange:nil];
}
}
@end