]> git.saurik.com Git - apple/security.git/blob - Keychain/KDAppDelegate.m
Security-55471.tar.gz
[apple/security.git] / Keychain / KDAppDelegate.m
1 //
2 // KDAppDelegate.m
3 // Keychain
4 //
5 // Created by J Osborne on 2/13/13.
6 //
7 //
8
9 #import "KDAppDelegate.h"
10 #import "KDCirclePeer.h"
11 #import "NSArray+mapWithBlock.h"
12 #include <notify.h>
13
14 #define kSecServerKeychainChangedNotification "com.apple.security.keychainchanged"
15
16 @implementation KDAppDelegate
17
18 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
19 {
20 self.stuffNotToLeak = [NSMutableArray new];
21 [self.stuffNotToLeak addObject:[[NSNotificationCenter defaultCenter] addObserverForName:kKDSecItemsUpdated object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
22 self.itemTableTitle.title = [NSString stringWithFormat:@"All Items (%ld)", (long)[self.itemDataSource numberOfRowsInTableView:self.itemTable]];
23 }]];
24
25 [self.syncSpinner setUsesThreadedAnimation:YES];
26 [self.syncSpinner startAnimation:nil];
27
28 self.itemDataSource = [[KDSecItems alloc] init];
29 self.itemTable.dataSource = self.itemDataSource;
30
31 int notificationToken;
32 uint32_t rc = notify_register_dispatch(kSecServerKeychainChangedNotification, &notificationToken, dispatch_get_main_queue(), ^(int token __unused) {
33 NSLog(@"Received %s", kSecServerKeychainChangedNotification);
34 [(KDSecItems*)self.itemDataSource loadItems];
35 [self.itemTable reloadData];
36 });
37 NSAssert(rc == 0, @"Can't register for %s", kSecServerKeychainChangedNotification);
38
39 self.circle = [KDSecCircle new];
40 [self.circle addChangeCallback:^{
41 self.circleStatusCell.stringValue = self.circle.status;
42
43 [self setCheckbox];
44
45 self.peerCountCell.objectValue = @(self.circle.peers.count);
46 NSString *peerNames = [[self.circle.peers mapWithBlock:^id(id obj) {
47 return ((KDCirclePeer*)obj).name;
48 }] componentsJoinedByString:@"\n"];
49 [self.peerTextList.textStorage replaceCharactersInRange:NSMakeRange(0, [self.peerTextList.textStorage length]) withString:peerNames];
50
51 self.applicantCountCell.objectValue = @(self.circle.applicants.count);
52 NSString *applicantNames = [[self.circle.applicants mapWithBlock:^id(id obj) {
53 return ((KDCirclePeer*)obj).name;
54 }] componentsJoinedByString:@"\n"];
55 [self.applicantTextList.textStorage replaceCharactersInRange:NSMakeRange(0, [self.applicantTextList.textStorage length]) withString:applicantNames];
56
57 [self.syncSpinner stopAnimation:nil];
58 }];
59 }
60
61 -(void)setCheckbox
62 {
63 if (self.circle.isInCircle) {
64 [self.enableKeychainSyncing setState:NSOnState];
65 } else if (self.circle.isOutOfCircle) {
66 [self.enableKeychainSyncing setState:NSOffState];
67 } else {
68 [self.enableKeychainSyncing setState:NSMixedState];
69 }
70 }
71
72 -(IBAction)enableKeychainSyncingClicked:(id)sender
73 {
74 [self.syncSpinner startAnimation:sender];
75 if (self.circle.isOutOfCircle) {
76 [self.circle enableSync];
77 } else {
78 [self.circle disableSync];
79 }
80 [self setCheckbox];
81 }
82
83 @end