Imported Upstream version 878.70.2
[platform/upstream/mdnsresponder.git] / mDNSMacOSX / SettingsBundle / BonjourSettingsController.m
1 /*
2  *
3  * Copyright (c) 2016 Apple Inc. All rights reserved.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 #import "BonjourSettingsController.h"
19 #import "HostnameController.h"
20 #import "BonjourSCStore.h"
21 #import "CNBrowseDomainsController.h"
22 #import <AssertMacros.h>
23
24 #define LocalizedStringFromMyBundle(key, comment)     \
25     NSLocalizedStringFromTableInBundle(key, @"Localizable", [NSBundle bundleForClass: [self class]], comment)
26
27 @interface BonjourSettingsController ()
28
29 @property (strong) NSString *               bonjourHostname;
30 @property (strong) NSArray *                browseDomainsA;
31
32 @end
33
34 @implementation BonjourSettingsController
35
36 - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
37 {
38     if (self = [super initWithNibName: nibNameOrNil bundle: nibBundleOrNil])
39     {
40         [self commonInit];
41     }
42     return(self);
43 }
44
45 - (void)commonInit
46 {
47 }
48
49 - (void)viewWillAppear:(BOOL)animated
50 {
51     [super viewWillAppear: animated];
52     [self readPreferences];
53     [self reloadSpecifiers];
54 }
55
56 - (id) getDomainCount:(PSSpecifier *)specifier
57 {
58     (void)specifier;    // Unused
59     return [NSNumber numberWithInteger: [_browseDomainsA filteredArrayUsingPredicate: [NSPredicate predicateWithFormat: @"(%K == %@)", (NSString *)SC_DYNDNS_ENABLED_KEY, @YES]].count].stringValue;
60 }
61
62 - (id) getHostname:(PSSpecifier *)specifier
63 {
64     (void)specifier;    // Unused
65     return _bonjourHostname.length ? _bonjourHostname : LocalizedStringFromMyBundle(@"_bonjour.hostname.unset", nil);
66 }
67
68 - (NSArray *)specifiers
69 {
70     if (!_specifiers) {
71         PSSpecifier * specifier;
72         NSMutableArray * specifiers = [NSMutableArray array];
73         
74         specifier = [PSSpecifier groupSpecifierWithName: LocalizedStringFromMyBundle(@"_bonjour.hostname.groupname", nil)];
75         [specifiers addObject: specifier];
76         specifier = [PSSpecifier preferenceSpecifierNamed: LocalizedStringFromMyBundle(@"_bonjour.hostname.name", nil)
77                                                    target: self
78                                                       set: nil
79                                                       get: @selector(getHostname:)
80                                                    detail: [HostnameController class]
81                                                      cell: PSLinkListCell
82                                                      edit: nil];
83         [specifier setProperty: @"hostnameID"
84                         forKey: PSIDKey];
85         [specifiers addObject: specifier];
86         
87         specifier = [PSSpecifier groupSpecifierWithName: LocalizedStringFromMyBundle(@"_bonjour.browse.groupname", nil)];
88         [specifiers addObject: specifier];
89         specifier = [PSSpecifier preferenceSpecifierNamed: LocalizedStringFromMyBundle(@"_bonjour.browse.name", nil)
90                                                    target: self
91                                                       set: nil
92                                                       get: @selector(getDomainCount:)
93                                                    detail: [CNBrowseDomainsController class]
94                                                      cell: PSLinkListCell
95                                                      edit: nil];
96         [specifier setProperty: @"browseID"
97                         forKey: PSIDKey];
98         [specifiers addObject: specifier];
99        
100         _specifiers = specifiers;
101     }
102     return _specifiers;
103 }
104
105 -(void)readPreferences
106 {
107     self.browseDomainsA = [BonjourSCStore objectForKey: (NSString *)SC_DYNDNS_BROWSEDOMAINS_KEY];
108     
109     NSArray * hostArray;
110     hostArray = [BonjourSCStore objectForKey: (NSString *)SC_DYNDNS_HOSTNAMES_KEY];
111     if (hostArray && [hostArray count] > 0)
112     {
113         self.bonjourHostname = hostArray[0][(NSString *)SC_DYNDNS_DOMAIN_KEY];
114     }
115     else self.bonjourHostname = nil;
116     
117     if (!_browseDomainsA) self.browseDomainsA = [NSMutableArray array];
118     if (!_bonjourHostname) self.bonjourHostname = [NSString string];
119 }
120
121 #pragma mark - TableView Delegates
122
123 - (void)listItemSelected:(NSIndexPath *)indexPath //sender is NSIndexPath of selection
124 {
125     if (indexPath.section == 0)
126     {
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];
131     }
132     else if (indexPath.section == 1)
133     {
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];
138     }
139     
140     [_table deselectRowAtIndexPath: indexPath animated: YES];
141 }
142
143 @end