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#define kSecServerKeychainChangedNotification "com.apple.security.keychainchanged"
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    int notificationToken;
48    uint32_t rc = notify_register_dispatch(kSecServerKeychainChangedNotification, &notificationToken, dispatch_get_main_queue(), ^(int token __unused) {
49            NSLog(@"Received %s", kSecServerKeychainChangedNotification);
50            [(KDSecItems*)self.itemDataSource loadItems];
51            [self.itemTable reloadData];
52         });
53    NSAssert(rc == 0, @"Can't register for %s", kSecServerKeychainChangedNotification);
54
55	self.circle = [KDSecCircle new];
56	[self.circle addChangeCallback:^{
57		self.circleStatusCell.stringValue = self.circle.status;
58
59        [self setCheckbox];
60
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];
66
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];
72
73        [self.syncSpinner stopAnimation:nil];
74	}];
75}
76
77-(void)setCheckbox
78{
79    if (self.circle.isInCircle) {
80        [self.enableKeychainSyncing setState:NSOnState];
81    } else if (self.circle.isOutOfCircle) {
82        [self.enableKeychainSyncing setState:NSOffState];
83    } else {
84        [self.enableKeychainSyncing setState:NSMixedState];
85    }
86}
87
88-(IBAction)enableKeychainSyncingClicked:(id)sender
89{
90    [self.syncSpinner startAnimation:sender];
91    if (self.circle.isOutOfCircle) {
92        [self.circle enableSync];
93    } else {
94        [self.circle disableSync];
95    }
96    [self setCheckbox];
97}
98
99@end
100