-
Notifications
You must be signed in to change notification settings - Fork 0
/
NetworkPreferencesViewController.m
274 lines (219 loc) · 11 KB
/
NetworkPreferencesViewController.m
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
//
// NetworkPreferencesViewController.m
// Telephone
//
// Copyright (c) 2008-2009 Alexei Kuznetsov. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holder nor the names of contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE THE COPYRIGHT HOLDER
// OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#import "NetworkPreferencesViewController.h"
#import "AppController.h"
#import "PreferencesController.h"
@interface NetworkPreferencesViewController ()
// Method to be called when an alert about network changes is dismissed.
- (void)networkSettingsChangeAlertDidEnd:(NSAlert *)alert
returnCode:(int)returnCode
contextInfo:(void *)contextInfo;
@end
@implementation NetworkPreferencesViewController
@synthesize preferencesController = preferencesController_;
@synthesize transportPortField = transportPortField_;
@synthesize STUNServerHostField = STUNServerHostField_;
@synthesize STUNServerPortField = STUNServerPortField_;
@synthesize useICECheckBox = useICECheckBox_;
@synthesize useDNSSRVCheckBox = useDNSSRVCheckBox_;
@synthesize outboundProxyHostField = outboundProxyHostField_;
@synthesize outboundProxyPortField = outboundProxyPortField_;
- (id)init {
self = [super initWithNibName:@"NetworkPreferencesView" bundle:nil];
if (self != nil) {
[self setTitle:NSLocalizedString(@"Network",
@"Network preferences window title.")];
}
return self;
}
- (void)awakeFromNib {
NSNotificationCenter *notificationCenter
= [NSNotificationCenter defaultCenter];
// Subscribe to User Agent start events.
[notificationCenter addObserver:self
selector:@selector(SIPUserAgentDidFinishStarting:)
name:AKSIPUserAgentDidFinishStartingNotification
object:nil];
// Show user agent's current transport port as a placeholder string.
if ([[[NSApp delegate] userAgent] isStarted]) {
[[[self transportPortField] cell] setPlaceholderString:
[[NSNumber numberWithUnsignedInteger:
[[[NSApp delegate] userAgent] transportPort]] stringValue]];
}
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults integerForKey:kTransportPort] > 0) {
[[self transportPortField] setIntegerValue:
[defaults integerForKey:kTransportPort]];
}
[[self STUNServerHostField] setStringValue:
[defaults stringForKey:kSTUNServerHost]];
if ([defaults integerForKey:kSTUNServerPort] > 0) {
[[self STUNServerPortField] setIntegerValue:
[defaults integerForKey:kSTUNServerPort]];
}
[[self useICECheckBox] setState:[defaults integerForKey:kUseICE]];
[[self useDNSSRVCheckBox] setState:[defaults integerForKey:kUseDNSSRV]];
[[self outboundProxyHostField] setStringValue:
[defaults stringForKey:kOutboundProxyHost]];
if ([defaults integerForKey:kOutboundProxyPort] > 0) {
[[self outboundProxyPortField] setIntegerValue:
[defaults integerForKey:kOutboundProxyPort]];
}
}
- (void)dealloc {
[transportPortField_ release];
[STUNServerHostField_ release];
[STUNServerPortField_ release];
[useICECheckBox_ release];
[useDNSSRVCheckBox_ release];
[outboundProxyHostField_ release];
[outboundProxyPortField_ release];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
- (BOOL)checkForNetworkSettingsChanges:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSCharacterSet *spacesSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSInteger newTransportPort = [[self transportPortField] integerValue];
NSString *newSTUNServerHost = [[[self STUNServerHostField] stringValue]
stringByTrimmingCharactersInSet:spacesSet];
NSInteger newSTUNServerPort = [[self STUNServerPortField] integerValue];
BOOL newUseICE = ([[self useICECheckBox] state] == NSOnState) ? YES : NO;
BOOL newUseDNSSRV = ([[self useDNSSRVCheckBox] state] == NSOnState) ? YES : NO;
NSString *newOutboundProxyHost = [[[self outboundProxyHostField] stringValue]
stringByTrimmingCharactersInSet:spacesSet];
NSInteger newOutboundProxyPort = [[self outboundProxyPortField] integerValue];
if ([defaults integerForKey:kTransportPort] != newTransportPort ||
![[defaults stringForKey:kSTUNServerHost] isEqualToString:newSTUNServerHost] ||
[defaults integerForKey:kSTUNServerPort] != newSTUNServerPort ||
[defaults boolForKey:kUseICE] != newUseICE ||
[defaults boolForKey:kUseDNSSRV] != newUseDNSSRV ||
![[defaults stringForKey:kOutboundProxyHost] isEqualToString:newOutboundProxyHost] ||
[defaults integerForKey:kOutboundProxyPort] != newOutboundProxyPort) {
// Explicitly select Network toolbar item.
[[[self preferencesController] toolbar] setSelectedItemIdentifier:
[[[self preferencesController] networkToolbarItem] itemIdentifier]];
NSAlert *alert = [[[NSAlert alloc] init] autorelease];
[alert addButtonWithTitle:NSLocalizedString(@"Save", @"Save button.")];
[alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"Cancel button.")];
[alert addButtonWithTitle:
NSLocalizedString(@"Don't Save", @"Don't save button.")];
[[[alert buttons] objectAtIndex:1] setKeyEquivalent:@"\033"];
[alert setMessageText:
NSLocalizedString(@"Save changes to the network settings?",
@"Network settings change confirmation.")];
[alert setInformativeText:
NSLocalizedString(@"New network settings will be applied immediately, all "
"accounts will be reconnected.",
@"Network settings change confirmation informative text.")];
[alert beginSheetModalForWindow:[[self preferencesController] window]
modalDelegate:self
didEndSelector:@selector(networkSettingsChangeAlertDidEnd:returnCode:contextInfo:)
contextInfo:sender];
return YES;
}
return NO;
}
- (void)networkSettingsChangeAlertDidEnd:(NSAlert *)alert
returnCode:(int)returnCode
contextInfo:(void *)contextInfo {
[[alert window] orderOut:nil];
if (returnCode == NSAlertSecondButtonReturn)
return;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSCharacterSet *spacesSet = [NSCharacterSet whitespaceAndNewlineCharacterSet];
id sender = (id)contextInfo;
if (returnCode == NSAlertFirstButtonReturn) {
[[[self transportPortField] cell] setPlaceholderString:
[[self transportPortField] stringValue]];
[defaults setInteger:[[self transportPortField] integerValue]
forKey:kTransportPort];
NSString *STUNServerHost = [[[self STUNServerHostField] stringValue]
stringByTrimmingCharactersInSet:spacesSet];
[defaults setObject:STUNServerHost forKey:kSTUNServerHost];
[defaults setInteger:[[self STUNServerPortField] integerValue]
forKey:kSTUNServerPort];
BOOL useICEFlag = ([[self useICECheckBox] state] == NSOnState) ? YES : NO;
[defaults setBool:useICEFlag forKey:kUseICE];
BOOL newUseDNSSRVFlag
= ([[self useDNSSRVCheckBox] state] == NSOnState) ? YES : NO;
[defaults setBool:newUseDNSSRVFlag forKey:kUseDNSSRV];
NSString *outboundProxyHost = [[[self outboundProxyHostField] stringValue]
stringByTrimmingCharactersInSet:spacesSet];
[defaults setObject:outboundProxyHost forKey:kOutboundProxyHost];
[defaults setInteger:[[self outboundProxyPortField] integerValue]
forKey:kOutboundProxyPort];
[[NSNotificationCenter defaultCenter]
postNotificationName:AKPreferencesControllerDidChangeNetworkSettingsNotification
object:[self preferencesController]];
} else if (returnCode == NSAlertThirdButtonReturn) {
if ([defaults integerForKey:kTransportPort] == 0) {
[[self transportPortField] setStringValue:@""];
} else {
[[self transportPortField] setIntegerValue:
[defaults integerForKey:kTransportPort]];
}
[[self STUNServerHostField] setStringValue:
[defaults stringForKey:kSTUNServerHost]];
if ([defaults integerForKey:kSTUNServerPort] == 0) {
[[self STUNServerPortField] setStringValue:@""];
} else {
[[self STUNServerPortField] setIntegerValue:
[defaults integerForKey:kSTUNServerPort]];
}
[[self useICECheckBox] setState:[defaults integerForKey:kUseICE]];
[[self useDNSSRVCheckBox] setState:[defaults integerForKey:kUseDNSSRV]];
[[self outboundProxyHostField] setStringValue:
[defaults stringForKey:kOutboundProxyHost]];
if ([defaults integerForKey:kOutboundProxyPort] == 0) {
[[self outboundProxyPortField] setStringValue:@""];
} else {
[[self outboundProxyPortField] setIntegerValue:
[defaults integerForKey:kOutboundProxyPort]];
}
}
if ([sender isMemberOfClass:[NSToolbarItem class]]) {
[[[self preferencesController] toolbar]
setSelectedItemIdentifier:[sender itemIdentifier]];
[[self preferencesController] changeView:sender];
} else if ([sender isMemberOfClass:[NSWindow class]])
[sender close];
}
#pragma mark -
#pragma mark AKSIPUserAgent notifications
- (void)SIPUserAgentDidFinishStarting:(NSNotification *)notification {
if (![[[NSApp delegate] userAgent] isStarted])
return;
// Show transport port in the network preferences as a placeholder string.
[[[self transportPortField] cell] setPlaceholderString:
[[NSNumber numberWithUnsignedInteger:
[[[NSApp delegate] userAgent] transportPort]] stringValue]];
}
@end