-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathPassEntryViewController.mm
121 lines (101 loc) · 3.25 KB
/
PassEntryViewController.mm
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
/*
* Copyright (C) 2012 Brian A. Mattern <[email protected]>.
* All Rights Reserved.
* This file is licensed under the GPLv2+.
* Please see COPYING for more information
*/
#import "PassEntryViewController.h"
#import "PassEntry.h"
#import "PDKeychainBindings.h"
@interface PassEntryViewController()
@property (nonatomic,retain) NSString *passphrase;
//-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;
- (void)requestPassphrase;
- (void)copyName;
- (BOOL)copyPass;
- (PDKeychainBindings *)keychain;
@end
@implementation PassEntryViewController
@synthesize entry;
@synthesize passphrase;
- (PDKeychainBindings *)keychain {
return [PDKeychainBindings sharedKeychainBindings];
}
- (void)viewDidLoad {
[super viewDidLoad];
// self.title = NSLocalizedString(@"Passwords", @"Password title");
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"EntryDetailCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
}
switch(indexPath.row) {
case 0:
cell.textLabel.text = @"Name";
cell.detailTextLabel.text = self.entry.name;
break;
case 1:
cell.textLabel.text = @"Password";
cell.detailTextLabel.text = @"********";
break;
default:
break;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
switch(indexPath.row) {
case 0:
[self copyName];
break;
case 1:
self.passphrase = [[self keychain] stringForKey:@"passphrase"];
if (self.passphrase == nil) {
[self requestPassphrase];
} else {
[self copyPass];
}
break;
default:
break;
}
}
- (void)copyName {
[UIPasteboard generalPasteboard].string = self.entry.name;
}
- (BOOL)copyPass {
NSString *pass = [self.entry passWithPassphrase:self.passphrase];
if (pass == nil) {
[[self keychain] removeObjectForKey:@"passphrase"];
return NO;
} else {
[UIPasteboard generalPasteboard].string = pass;
return YES;
}
}
- (void)requestPassphrase {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Passphrase" message:@"Enter passphrase for gpg key" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil];
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
[alert show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
self.passphrase = [alertView textFieldAtIndex:0].text;
[[self keychain] setObject:self.passphrase forKey:@"passphrase"];
if (![self copyPass]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Passphrase" message:@"Passphrase invalid" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
}
}
@end