3 // SharedWebCredentialViewService
5 // Copyright (c) 2014 Apple Inc. All Rights Reserved.
8 #import <Foundation/NSXPCConnection.h>
9 #import "SWCViewController.h"
10 #import <UIKit/UIViewController_Private.h>
11 #import <UIKit/UIFont_Private.h>
13 #import <UIKit/UIAlertController_Private.h>
14 #import <UIKit/UITableViewCell_Private.h>
16 #include <bsm/libbsm.h>
17 #include <ipc/securityd_client.h>
19 #include "SharedWebCredential/swcagent_client.h"
21 #import "SWCViewController.h"
23 const NSString* SWC_PASSWORD_KEY = @"spwd";
24 const NSString* SWC_ACCOUNT_KEY = @"acct";
25 const NSString* SWC_SERVER_KEY = @"srvr";
28 // SWCDictionaryAdditions
31 @interface NSDictionary (SWCDictionaryAdditions)
32 - (NSComparisonResult) compareCredentialDictionaryAscending:(NSDictionary *)other;
35 @implementation NSDictionary (SWCDictionaryAdditions)
36 - (NSComparisonResult)compareCredentialDictionaryAscending:(NSDictionary *)other
38 NSComparisonResult result;
39 NSString *str1 = [self objectForKey:SWC_ACCOUNT_KEY], *str2 = [other objectForKey:SWC_ACCOUNT_KEY];
40 if (!str1) str1 = @"";
41 if (!str2) str2 = @"";
43 // primary sort by account name
44 result = [str1 localizedCaseInsensitiveCompare:str2];
45 if (result == NSOrderedSame) {
46 // secondary sort by domain name
47 NSString *str3 = [self objectForKey:SWC_SERVER_KEY], *str4 = [other objectForKey:SWC_SERVER_KEY];
48 if (!str3) str3 = @"";
49 if (!str4) str4 = @"";
51 result = [str3 localizedCaseInsensitiveCompare:str4];
62 @interface SWCItemCell : UITableViewCell
67 UIView *_bottomLineSelected;
69 UIView *_topLineSelected;
71 BOOL _showTopSeparator;
74 - (id)initWithDictionary:(NSDictionary *)dict;
75 @property (nonatomic, readonly) id userInfo;
76 @property (nonatomic, assign) BOOL showSeparator;
79 @implementation SWCItemCell
81 @synthesize showSeparator = _showSeparator;
83 - (id)initWithDictionary:(NSDictionary *)dict
85 if ((self = [super initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:nil]))
89 self.selectionStyle = UITableViewCellSelectionStyleNone;
91 self.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.005];
93 self.textLabel.textColor = [UIColor blackColor];
94 self.textLabel.textAlignment = NSTextAlignmentLeft;
95 self.textLabel.adjustsFontSizeToFitWidth = YES;
96 self.textLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
98 NSString *title = [dict objectForKey:SWC_ACCOUNT_KEY];
99 self.textLabel.text = title ? title : NSLocalizedString(@"--", nil);
101 self.detailTextLabel.textColor = [UIColor darkGrayColor];
102 self.detailTextLabel.textAlignment = NSTextAlignmentLeft;
103 self.detailTextLabel.adjustsFontSizeToFitWidth = YES;
104 self.detailTextLabel.baselineAdjustment = UIBaselineAdjustmentAlignCenters;
106 NSString *subtitle = [dict objectForKey:SWC_SERVER_KEY];
107 self.detailTextLabel.text = subtitle ? subtitle : NSLocalizedString(@"--", nil);
109 self.backgroundView = [[UIView alloc] init];
110 self.backgroundView.backgroundColor = self.backgroundColor;
112 self.imageView.image = [self _checkmarkImage: NO];
113 self.imageView.hidden = YES;
120 - (void)setTicked: (BOOL) selected
122 _isTicked = selected;
125 - (void)layoutSubviews
129 CGFloat scale = [[UIScreen mainScreen] scale];
130 [_bottomLine setFrame:CGRectMake(0, self.frame.size.height - (1 / scale), self.frame.size.width, 1 / scale)];
133 if (_bottomLineSelected) {
134 CGFloat scale = [[UIScreen mainScreen] scale];
135 [_bottomLineSelected setFrame:CGRectMake(0, self.frame.size.height - (1 / scale), self.frame.size.width, 1 / scale)];
139 CGFloat scale = [[UIScreen mainScreen] scale];
140 [_topLine setFrame:CGRectMake(0, 0, self.frame.size.width, 1 / scale)];
143 if (_topLineSelected) {
144 CGFloat scale = [[UIScreen mainScreen] scale];
145 [_topLineSelected setFrame:CGRectMake(0, 0, self.frame.size.width, 1 / scale)];
150 self.imageView.hidden = NO;
152 self.imageView.hidden = YES;
155 [super layoutSubviews];
159 - (void)setShowSeparator:(BOOL)showSeparator {
160 if (_showSeparator != showSeparator) {
161 _showSeparator = showSeparator;
163 if (_showSeparator) {
165 CGRect rectZero = CGRectMake(0, 0, 0, 0);
166 _bottomLine = [[UIView alloc] initWithFrame:rectZero];
167 _bottomLine.backgroundColor = [UIColor colorWithWhite:.5 alpha:.5];
168 [self.backgroundView addSubview:_bottomLine];
170 if (!_bottomLineSelected) {
171 CGRect rectZero = CGRectMake(0, 0, 0, 0);
172 _bottomLineSelected = [[UIView alloc] initWithFrame:rectZero];
173 _bottomLineSelected.backgroundColor = [UIColor colorWithWhite:.5 alpha:.5];
174 [self.selectedBackgroundView addSubview: _bottomLineSelected];
179 [_bottomLine removeFromSuperview];
182 if (_bottomLineSelected) {
183 [_bottomLineSelected removeFromSuperview];
184 _bottomLineSelected = nil;
190 - (void)setShowTopSeparator:(BOOL)showTopSeparator {
191 if (_showTopSeparator != showTopSeparator) {
192 _showTopSeparator = showTopSeparator;
194 if (_showTopSeparator) {
196 CGRect rectZero = CGRectMake(0, 0, 0, 0);
197 _topLine = [[UIView alloc] initWithFrame:rectZero];
198 _topLine.backgroundColor = [UIColor colorWithWhite:.5 alpha:.5];
199 [self.backgroundView addSubview:_topLine];
201 if (!_topLineSelected) {
202 CGRect rectZero = CGRectMake(0, 0, 0, 0);
203 _topLineSelected = [[UIView alloc] initWithFrame:rectZero];
204 _topLineSelected.backgroundColor = [UIColor colorWithWhite:.5 alpha:.5];
205 [self.selectedBackgroundView addSubview: _topLineSelected];
210 [_topLine removeFromSuperview];
213 if (_topLineSelected) {
214 [_topLineSelected removeFromSuperview];
215 _topLineSelected = nil;
228 @interface SWCViewController ()
230 NSMutableArray *_credentials; // array of NSDictionary
232 UILabel *_middleLabel;
234 NSDictionary *_selectedDict;
235 NSIndexPath *_selectedCell;
240 @implementation SWCViewController
242 - (NSDictionary *)selectedItem
244 return _selectedDict;
247 - (void)setCredentials:(NSArray *)inArray
249 NSMutableArray *credentials = [[NSMutableArray alloc] initWithArray:inArray];
250 [credentials sortUsingSelector:@selector(compareCredentialDictionaryAscending:)];
251 _credentials = credentials;
258 [_table setUserInteractionEnabled:YES];
261 - (UITableView *)tableView
264 _table = [[UITableView alloc] init];
265 [_table setTranslatesAutoresizingMaskIntoConstraints:NO];
266 [_table setAutoresizingMask:UIViewAutoresizingNone];
267 [_table setBackgroundColor:[UIColor clearColor]];
268 [_table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
272 return (UITableView *)_table;
278 UIView* view = [[UIView alloc] init];
280 UITableView* table = [self tableView];
281 [table setDelegate: self];
282 [table setDataSource: self];
284 [view addSubview: table];
286 CFErrorRef error = NULL;
287 audit_token_t auditToken = {};
288 memset(&auditToken, 0, sizeof(auditToken));
289 CFArrayRef credentialList = swca_copy_pairs(swca_copy_pairs_request_id, &auditToken, &error);
291 NSLog(@"Unable to get accounts: %@", [(__bridge NSError*)error localizedDescription]);
294 [self setCredentials:(__bridge NSArray*)credentialList];
295 if (credentialList) {
296 CFRelease(credentialList);
300 NSDictionary* views = NSDictionaryOfVariableBindings(table);
302 if ([_credentials count] > 2)
305 NSDictionary *metrics = @{@"height":@120.0};
306 [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-[table]-|" options:0 metrics:metrics views:views]];
307 [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[table(height)]-|" options:0 metrics:metrics views:views]];
309 CGFloat scale = [[UIScreen mainScreen] scale];
310 table.layer.borderWidth = 1.0 / scale;
311 table.layer.borderColor = [UIColor colorWithWhite:.5 alpha:.5].CGColor;
313 [self setPreferredContentSize:CGSizeMake(0,140)];
315 } else if ([_credentials count] == 2) {
317 NSDictionary *metrics = @{@"height":@90.0};
318 [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[table]|" options:0 metrics:metrics views:views]];
319 [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[table(height)]-|" options:0 metrics:metrics views:views]];
321 [self setPreferredContentSize:CGSizeMake(0,90)];
322 [table setScrollEnabled: NO];
324 } else { // [_credentials count] == 1
326 NSDictionary *metrics = @{@"height":@45.0};
327 [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[table]|" options:0 metrics:metrics views:views]];
328 [view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[table(height)]|" options:0 metrics:metrics views:views]];
330 [self setPreferredContentSize:CGSizeMake(0,45)];
331 [table setScrollEnabled: NO];
338 -(void)viewWillAppear:(BOOL)animated
341 // Select the first cell by default
343 NSDictionary *dict = [_credentials objectAtIndex: 0];
344 _selectedDict = dict;
345 _selectedCell = [NSIndexPath indexPathForItem:0 inSection: 0];
346 SWCItemCell *cell = (SWCItemCell *)[_table cellForRowAtIndexPath: _selectedCell];
347 [cell setTicked: YES];
348 [_table selectRowAtIndexPath: _selectedCell animated: NO scrollPosition: UITableViewScrollPositionTop];
350 CFErrorRef error = NULL;
351 audit_token_t auditToken = {};
352 memset(&auditToken, 0, sizeof(auditToken));
353 bool result = swca_set_selection(swca_set_selection_request_id,
354 &auditToken, (__bridge CFDictionaryRef)dict, &error);
356 NSLog(@"Unable to select item: %@", [(__bridge NSError*)error localizedDescription]);
359 [super viewWillAppear:animated];
364 // UITableView delegate methods
367 - (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section
369 return [_credentials count];
372 - (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
374 NSUInteger row = indexPath.row;
376 NSDictionary *dict = [_credentials objectAtIndex:row];
377 _selectedDict = dict;
379 CFErrorRef error = NULL;
380 audit_token_t auditToken = {};
381 memset(&auditToken, 0, sizeof(auditToken));
382 bool result = swca_set_selection(swca_set_selection_request_id,
383 &auditToken, (__bridge CFDictionaryRef)dict, &error);
385 NSLog(@"Unable to select item: %@", [(__bridge NSError*)error localizedDescription]);
388 _selectedCell = indexPath;
389 SWCItemCell *cell = (SWCItemCell *)[tableView cellForRowAtIndexPath: indexPath];
390 [cell setTicked: YES];
391 [cell layoutSubviews];
394 - (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
396 SWCItemCell *cell = (SWCItemCell *)[tableView cellForRowAtIndexPath: indexPath];
397 [cell setTicked: NO];
398 [cell layoutSubviews];
402 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
405 NSDictionary *dict = [_credentials objectAtIndex:[indexPath row]];
406 SWCItemCell *cell = [[SWCItemCell alloc] initWithDictionary:dict];
408 // show separator on top cell if there's only or or two items
409 if ([_credentials count] <= 2) {
410 cell.showTopSeparator = YES;
412 cell.showSeparator = YES;
414 if (indexPath.row == 0)
416 cell.showTopSeparator = YES;
421 if (_selectedCell == indexPath)
423 [cell setTicked: YES];
425 [cell setTicked: NO];