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 #define kSecServerKeychainChangedNotification "com.apple.security.keychainchanged"
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 int notificationToken;
48 uint32_t rc = notify_register_dispatch(kSecServerKeychainChangedNotification, ¬ificationToken, dispatch_get_main_queue(), ^(int token __unused) {
49 NSLog(@"Received %s", kSecServerKeychainChangedNotification);
50 [(KDSecItems*)self.itemDataSource loadItems];
51 [self.itemTable reloadData];
53 NSAssert(rc == 0, @"Can't register for %s", kSecServerKeychainChangedNotification);
55 self.circle = [KDSecCircle new];
56 [self.circle addChangeCallback:^{
57 self.circleStatusCell.stringValue = self.circle.status;
61 self.peerCountCell.objectValue = @(self.circle.peers.count);
62 NSString *peerNames = [[self.circle.peers mapWithBlock:^id(id obj) {
63 return ((KDCirclePeer*)obj).name;
64 }] componentsJoinedByString:@"\n"];
65 [self.peerTextList.textStorage replaceCharactersInRange:NSMakeRange(0, [self.peerTextList.textStorage length]) withString:peerNames];
67 self.applicantCountCell.objectValue = @(self.circle.applicants.count);
68 NSString *applicantNames = [[self.circle.applicants mapWithBlock:^id(id obj) {
69 return ((KDCirclePeer*)obj).name;
70 }] componentsJoinedByString:@"\n"];
71 [self.applicantTextList.textStorage replaceCharactersInRange:NSMakeRange(0, [self.applicantTextList.textStorage length]) withString:applicantNames];
73 [self.syncSpinner stopAnimation:nil];
79 if (self.circle.isInCircle) {
80 [self.enableKeychainSyncing setState:NSOnState];
81 } else if (self.circle.isOutOfCircle) {
82 [self.enableKeychainSyncing setState:NSOffState];
84 [self.enableKeychainSyncing setState:NSMixedState];
88 -(IBAction)enableKeychainSyncingClicked:(id)sender
90 [self.syncSpinner startAnimation:sender];
91 if (self.circle.isOutOfCircle) {
92 [self.circle enableSync];
94 [self.circle disableSync];