]> git.saurik.com Git - apple/security.git/blob - OSX/Keychain/KDAppDelegate.m
Security-59306.101.1.tar.gz
[apple/security.git] / OSX / Keychain / KDAppDelegate.m
1 /*
2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 #import "KDAppDelegate.h"
26 #import "KDCirclePeer.h"
27 #import "NSArray+mapWithBlock.h"
28 #include <notify.h>
29
30 #include <Security/SecItemInternal.h>
31
32 @implementation KDAppDelegate
33
34 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
35 {
36 self.stuffNotToLeak = [NSMutableArray new];
37 [self.stuffNotToLeak addObject:[[NSNotificationCenter defaultCenter] addObserverForName:kKDSecItemsUpdated object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
38 self.itemTableTitle.title = [NSString stringWithFormat:@"All Items (%ld)", (long)[self.itemDataSource numberOfRowsInTableView:self.itemTable]];
39 }]];
40
41 [self.syncSpinner setUsesThreadedAnimation:YES];
42 [self.syncSpinner startAnimation:nil];
43
44 self.itemDataSource = [[KDSecItems alloc] init];
45 self.itemTable.dataSource = self.itemDataSource;
46
47 #pragma clang diagnostic push
48 #pragma clang diagnostic ignored "-Wunused-variable"
49 int notificationToken;
50 uint32_t rc = notify_register_dispatch(kSecServerKeychainChangedNotification, &notificationToken, dispatch_get_main_queue(), ^(int token __unused) {
51 NSLog(@"Received %s", kSecServerKeychainChangedNotification);
52 [(KDSecItems*)self.itemDataSource loadItems];
53 [self.itemTable reloadData];
54 });
55 NSAssert(rc == 0, @"Can't register for %s", kSecServerKeychainChangedNotification);
56 #pragma clang diagnostic pop
57
58 self.circle = [KDSecCircle new];
59
60 __weak typeof(self) weakSelf = self;
61 [self.circle addChangeCallback:^{
62 __strong typeof(self) strongSelf = weakSelf;
63 if(strongSelf) {
64 strongSelf.circleStatusCell.stringValue = strongSelf.circle.status;
65
66 [strongSelf setCheckbox];
67
68 strongSelf.peerCountCell.objectValue = @(strongSelf.circle.peers.count);
69 NSString *peerNames = [[strongSelf.circle.peers mapWithBlock:^id(id obj) {
70 return ((KDCirclePeer*)obj).name;
71 }] componentsJoinedByString:@"\n"];
72 [strongSelf.peerTextList.textStorage replaceCharactersInRange:NSMakeRange(0, [strongSelf.peerTextList.textStorage length]) withString:peerNames];
73
74 strongSelf.applicantCountCell.objectValue = @(strongSelf.circle.applicants.count);
75 NSString *applicantNames = [[strongSelf.circle.applicants mapWithBlock:^id(id obj) {
76 return ((KDCirclePeer*)obj).name;
77 }] componentsJoinedByString:@"\n"];
78 [strongSelf.applicantTextList.textStorage replaceCharactersInRange:NSMakeRange(0, [strongSelf.applicantTextList.textStorage length]) withString:applicantNames];
79
80 [strongSelf.syncSpinner stopAnimation:nil];
81 }
82 }];
83 }
84
85 -(void)setCheckbox
86 {
87 if (self.circle.isInCircle) {
88 [self.enableKeychainSyncing setState:NSControlStateValueOn];
89 } else if (self.circle.isOutOfCircle) {
90 [self.enableKeychainSyncing setState:NSControlStateValueOff];
91 } else {
92 [self.enableKeychainSyncing setState:NSControlStateValueMixed];
93 }
94 }
95
96 -(IBAction)enableKeychainSyncingClicked:(id)sender
97 {
98 [self.syncSpinner startAnimation:sender];
99 if (self.circle.isOutOfCircle) {
100 [self.circle enableSync];
101 } else {
102 [self.circle disableSync];
103 }
104 [self setCheckbox];
105 }
106
107 @end