summaryrefslogtreecommitdiffstats
path: root/mDNSResponder/mDNSMacOSX/SettingsBundle/HostnameController.m
blob: 94edc3693f6c35b451debe2d21cd1cacc1264949 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 *
 * Copyright (c) 2016 Apple Inc. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import "HostnameController.h"
#import "BonjourSCStore.h"
#import <AssertMacros.h>
#import <Preferences/Preferences.h>

#define LocalizedStringFromMyBundle(key, comment)     \
    NSLocalizedStringFromTableInBundle(key, @"Localizable", [NSBundle bundleForClass: [self class]], comment)

@interface HostnameController ()

@property (strong) UITextField *            textField;

@end

@implementation HostnameController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.tableView.dataSource = (id<UITableViewDataSource>)self;
    self.tableView.delegate = (id<UITableViewDelegate>)self;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear: animated];
    if (self.isMovingFromParentViewController)
    {
        [[self firstResponder] resignFirstResponder];       //  Ends any outstanding edits
        self.bonjourHostname = _textField.attributedText.string;
        [self savePreferences];    }
}

-(void)savePreferences
{
    [BonjourSCStore setObject: _bonjourHostname.length ? @[@{
                                                               (NSString *)SC_DYNDNS_DOMAIN_KEY  : _bonjourHostname,
                                                               (NSString *)SC_DYNDNS_ENABLED_KEY : @YES
                                                            }] : nil
                       forKey: (NSString *)SC_DYNDNS_HOSTNAMES_KEY];
}

- (void)_setHostname:(NSString *)value
{
    self.bonjourHostname = value;
}

#pragma mark - TableView Delegates

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    (void)tableView;    // Unused
    (void)indexPath;    // Unused
    return UITableViewAutomaticDimension;
}

- (nullable NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    (void)tableView;    // Unused
    (void)section;      // Unused
    return(LocalizedStringFromMyBundle(@"_bonjour.hostname.desc", nil));
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    (void)tableView;    // Unused
    (void)section;      // Unused
    return(1);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PSEditableTableCell *cell = nil;
    
    if (tableView == self.tableView && indexPath.section == 0)
    {
        static NSString *MyIdentifier = @"hostname_cell_id";
        cell = (PSEditableTableCell *)[tableView dequeueReusableCellWithIdentifier: MyIdentifier];
        if (!cell)
        {
            cell = [[PSEditableTableCell alloc] initWithStyle: [PSEditableTableCell cellStyle]  reuseIdentifier: MyIdentifier];
        }
        cell.placeholderText = LocalizedStringFromMyBundle(@"_bonjour.hostname.placeholder", nil);
        cell.textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
        cell.textField.clearButtonMode = UITextFieldViewModeAlways;
        cell.textField.autocorrectionType = UITextAutocorrectionTypeNo;
        cell.textField.keyboardType = UIKeyboardTypeURL;
        cell.title = @"";
        cell.value = _bonjourHostname;
        self.textField = cell.textField;
    }
    
    return(cell);
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath
{
    (void)tableView;    // Unused
    BOOL    result = YES;
    
    if (indexPath.section == 0 && indexPath.row == 0) result = NO;
    
    return(result);
}

@end