Imported Upstream version 878.70.2
[platform/upstream/mdnsresponder.git] / mDNSMacOSX / SettingsBundle / HostnameController.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 "HostnameController.h"
19 #import "BonjourSCStore.h"
20 #import <AssertMacros.h>
21 #import <Preferences/Preferences.h>
22
23 #define LocalizedStringFromMyBundle(key, comment)     \
24     NSLocalizedStringFromTableInBundle(key, @"Localizable", [NSBundle bundleForClass: [self class]], comment)
25
26 @interface HostnameController ()
27
28 @property (strong) UITextField *            textField;
29
30 @end
31
32 @implementation HostnameController
33
34 - (void)viewDidLoad
35 {
36     [super viewDidLoad];
37     
38     self.tableView.dataSource = (id<UITableViewDataSource>)self;
39     self.tableView.delegate = (id<UITableViewDelegate>)self;
40 }
41
42 - (void)viewWillDisappear:(BOOL)animated
43 {
44     [super viewWillDisappear: animated];
45     if (self.isMovingFromParentViewController)
46     {
47         [[self firstResponder] resignFirstResponder];       //  Ends any outstanding edits
48         self.bonjourHostname = _textField.attributedText.string;
49         [self savePreferences];    }
50 }
51
52 -(void)savePreferences
53 {
54     [BonjourSCStore setObject: _bonjourHostname.length ? @[@{
55                                                                (NSString *)SC_DYNDNS_DOMAIN_KEY  : _bonjourHostname,
56                                                                (NSString *)SC_DYNDNS_ENABLED_KEY : @YES
57                                                             }] : nil
58                        forKey: (NSString *)SC_DYNDNS_HOSTNAMES_KEY];
59 }
60
61 - (void)_setHostname:(NSString *)value
62 {
63     self.bonjourHostname = value;
64 }
65
66 #pragma mark - TableView Delegates
67
68 - (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
69 {
70     (void)tableView;    // Unused
71     (void)indexPath;    // Unused
72     return UITableViewAutomaticDimension;
73 }
74
75 - (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
76 {
77     (void)tableView;    // Unused
78     (void)section;      // Unused
79     return(LocalizedStringFromMyBundle(@"_bonjour.hostname.desc", nil));
80 }
81
82 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
83 {
84     (void)tableView;    // Unused
85     (void)section;      // Unused
86     return(1);
87 }
88
89 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
90 {
91     PSEditableTableCell *cell = nil;
92     
93     if (tableView == self.tableView && indexPath.section == 0)
94     {
95         static NSString *MyIdentifier = @"hostname_cell_id";
96         cell = (PSEditableTableCell *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier];
97         if (!cell)
98         {
99             cell = [[PSEditableTableCell alloc] initWithStyle: [PSEditableTableCell cellStyle]  reuseIdentifier: MyIdentifier];
100         }
101         cell.placeholderText = LocalizedStringFromMyBundle(@"_bonjour.hostname.placeholder", nil);
102         cell.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
103         cell.textField.clearButtonMode = UITextFieldViewModeAlways;
104         cell.textField.autocorrectionType = UITextAutocorrectionTypeNo;
105         cell.textField.keyboardType = UIKeyboardTypeURL;
106         cell.title = @"";
107         cell.value = _bonjourHostname;
108         self.textField = cell.textField;
109     }
110     
111     return(cell);
112 }
113
114 - (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
115 {
116     (void)tableView;    // Unused
117     BOOL    result = YES;
118     
119     if (indexPath.section == 0 && indexPath.row == 0) result = NO;
120     
121     return(result);
122 }
123
124 @end
125