2 * Copyright (c) 2013-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 #import "KDAppDelegate.h"
26 #import "KDCirclePeer.h"
27 #import "NSArray+mapWithBlock.h"
30 #include <Security/SecItemInternal.h>
32 @implementation KDAppDelegate
34 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
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]];
41 [self.syncSpinner setUsesThreadedAnimation:YES];
42 [self.syncSpinner startAnimation:nil];
44 self.itemDataSource = [[KDSecItems alloc] init];
45 self.itemTable.dataSource = self.itemDataSource;
47 #pragma clang diagnostic push
48 #pragma clang diagnostic ignored "-Wunused-variable"
49 int notificationToken;
50 uint32_t rc = notify_register_dispatch(kSecServerKeychainChangedNotification, ¬ificationToken, dispatch_get_main_queue(), ^(int token __unused) {
51 NSLog(@"Received %s", kSecServerKeychainChangedNotification);
52 [(KDSecItems*)self.itemDataSource loadItems];
53 [self.itemTable reloadData];
55 NSAssert(rc == 0, @"Can't register for %s", kSecServerKeychainChangedNotification);
56 #pragma clang diagnostic pop
58 self.circle = [KDSecCircle new];
60 __weak typeof(self) weakSelf = self;
61 [self.circle addChangeCallback:^{
62 __strong typeof(self) strongSelf = weakSelf;
64 strongSelf.circleStatusCell.stringValue = strongSelf.circle.status;
66 [strongSelf setCheckbox];
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];
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];
80 [strongSelf.syncSpinner stopAnimation:nil];
87 if (self.circle.isInCircle) {
88 [self.enableKeychainSyncing setState:NSControlStateValueOn];
89 } else if (self.circle.isOutOfCircle) {
90 [self.enableKeychainSyncing setState:NSControlStateValueOff];
92 [self.enableKeychainSyncing setState:NSControlStateValueMixed];
96 -(IBAction)enableKeychainSyncingClicked:(id)sender
98 [self.syncSpinner startAnimation:sender];
99 if (self.circle.isOutOfCircle) {
100 [self.circle enableSync];
102 [self.circle disableSync];