3 * Copyright (c) 2016 Apple Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #import "BonjourSettingsController.h"
19 #import "HostnameController.h"
20 #import "BonjourSCStore.h"
21 #import "CNBrowseDomainsController.h"
22 #import <AssertMacros.h>
24 #define LocalizedStringFromMyBundle(key, comment) \
25 NSLocalizedStringFromTableInBundle(key, @"Localizable", [NSBundle bundleForClass: [self class]], comment)
27 @interface BonjourSettingsController ()
29 @property (strong) NSString * bonjourHostname;
30 @property (strong) NSArray * browseDomainsA;
34 @implementation BonjourSettingsController
36 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
38 if (self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil])
49 - (void)viewWillAppear:(BOOL)animated
51 [super viewWillAppear: animated];
52 [self readPreferences];
53 [self reloadSpecifiers];
56 - (id) getDomainCount:(PSSpecifier *)specifier
58 (void)specifier; // Unused
59 return [NSNumber numberWithInteger: [_browseDomainsA filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: @"(%K == %@)", (NSString *)SC_DYNDNS_ENABLED_KEY, @YES]].count].stringValue;
62 - (id) getHostname:(PSSpecifier *)specifier
64 (void)specifier; // Unused
65 return _bonjourHostname.length ? _bonjourHostname : LocalizedStringFromMyBundle(@"_bonjour.hostname.unset", nil);
68 - (NSArray *)specifiers
71 PSSpecifier * specifier;
72 NSMutableArray * specifiers = [NSMutableArray array];
74 specifier = [PSSpecifier groupSpecifierWithName: LocalizedStringFromMyBundle(@"_bonjour.hostname.groupname", nil)];
75 [specifiers addObject: specifier];
76 specifier = [PSSpecifier preferenceSpecifierNamed: LocalizedStringFromMyBundle(@"_bonjour.hostname.name", nil)
79 get: @selector(getHostname:)
80 detail: [HostnameController class]
83 [specifier setProperty: @"hostnameID"
85 [specifiers addObject: specifier];
87 specifier = [PSSpecifier groupSpecifierWithName: LocalizedStringFromMyBundle(@"_bonjour.browse.groupname", nil)];
88 [specifiers addObject: specifier];
89 specifier = [PSSpecifier preferenceSpecifierNamed: LocalizedStringFromMyBundle(@"_bonjour.browse.name", nil)
92 get: @selector(getDomainCount:)
93 detail: [CNBrowseDomainsController class]
96 [specifier setProperty: @"browseID"
98 [specifiers addObject: specifier];
100 _specifiers = specifiers;
105 -(void)readPreferences
107 self.browseDomainsA = [BonjourSCStore objectForKey: (NSString *)SC_DYNDNS_BROWSEDOMAINS_KEY];
110 hostArray = [BonjourSCStore objectForKey: (NSString *)SC_DYNDNS_HOSTNAMES_KEY];
111 if (hostArray && [hostArray count] > 0)
113 self.bonjourHostname = hostArray[0][(NSString *)SC_DYNDNS_DOMAIN_KEY];
115 else self.bonjourHostname = nil;
117 if (!_browseDomainsA) self.browseDomainsA = [NSMutableArray array];
118 if (!_bonjourHostname) self.bonjourHostname = [NSString string];
121 #pragma mark - TableView Delegates
123 - (void)listItemSelected:(NSIndexPath *)indexPath //sender is NSIndexPath of selection
125 if (indexPath.section == 0)
127 HostnameController * c = [[HostnameController alloc] initWithStyle: UITableViewStyleGrouped];
128 c.bonjourHostname = _bonjourHostname;
129 c.title = LocalizedStringFromMyBundle(@"_bonjour.hostname.name", nil);
130 [self.navigationController pushViewController: c animated: YES];
132 else if (indexPath.section == 1)
134 CNBrowseDomainsController * c = [[CNBrowseDomainsController alloc] initWithStyle: UITableViewStyleGrouped];
135 c.browseDomainsA = _browseDomainsA;
136 c.title = LocalizedStringFromMyBundle(@"_bonjour.browse.name", nil);
137 [self.navigationController pushViewController: c animated: YES];
140 [_table deselectRowAtIndexPath: indexPath animated: YES];