OSDN Git Service

FIX: ABEND when encrypting.
[tombo/Tombo.git] / iOS / Tombo / Tombo / PasswordManager.m
1 #import "PasswordManager.h"
2 #import "SinglePasswordDialog.h"
3
4 @implementation PasswordManager {
5     NSTimer *timer;
6 }
7
8 @synthesize password = _password;
9
10 - (id)init {
11     id obj = [super init];
12     self.password = nil;
13     return obj;
14 }
15
16 - (BOOL)preparePassword {
17     if (!self.password) {
18         SinglePasswordDialog *dialog = [[SinglePasswordDialog alloc] initWithTitle:@"Password" 
19                                                                            message:@"Please input password"];
20         NSString *pass = [dialog showAndWait];
21         if (pass == nil) return NO;
22         
23         self.password = pass;
24     }
25
26     [self resetTimer];
27     return YES;
28 }
29
30 - (BOOL)preparePasswordConfirm {
31     if (!self.password) {
32         SinglePasswordDialog *dialog = [[SinglePasswordDialog alloc] initWithTitle:@"Password" 
33                                                                            message:@"Please input password"];
34         NSString *pass1 = [dialog showAndWait];
35         if (pass1 == nil) return NO;
36         
37         dialog = [[SinglePasswordDialog alloc] initWithTitle:@"Confirm" message:@"Input password again"];
38         NSString *pass2 = [dialog showAndWait];
39         if (pass2 == nil) return NO;
40         
41         if (![pass1 isEqualToString:pass2]) {
42             UIAlertView *mismatch = [[UIAlertView alloc] initWithTitle:@"Warn"
43                                                                message:@"Password mismatch."
44                                                               delegate:nil cancelButtonTitle:@"OK"
45                                                      otherButtonTitles:nil];
46             [mismatch show];
47             return NO;
48         }
49         self.password = pass1;
50     }
51
52     [self resetTimer];
53
54     return YES;
55 }
56
57 - (void)resetTimer {
58     if (timer) [timer invalidate];
59     timer = [NSTimer scheduledTimerWithTimeInterval:60
60                                              target:self 
61                                            selector:@selector(fireTimer:) 
62                                            userInfo:nil 
63                                             repeats:NO];
64 }
65
66 - (void)fireTimer:(NSTimer *)timer {
67     self.password = nil;
68 }
69 @end