2 * Adium is the legal property of its developers, whose names are listed in the copyright file included
3 * with this source distribution.
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11 * Public License for more details.
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 #import <Adium/AIAccountControllerProtocol.h>
18 #import "AIAccountProxySettings.h"
19 #import <AIUtilities/AIMenuAdditions.h>
20 #import <AIUtilities/AIPopUpButtonAdditions.h>
21 #import <AIUtilities/AITextFieldAdditions.h>
22 #import <Adium/AIAccount.h>
24 @interface AIAccountProxySettings (PRIVATE)
25 - (void)configureControlDimming;
26 - (void)updatePasswordField;
28 - (NSMenu *)_proxyMenu;
29 - (NSMenuItem *)_proxyMenuItemWithTitle:(NSString *)title tag:(int)tag;
32 @implementation AIAccountProxySettings
36 if (self == [AIAccountProxySettings class]) {
37 [self exposeBinding:@"showProxyDetailsControls"];
42 * @brief Init our account proxy settings
44 * Loads AccountProxy.nib and sets up menus
48 if ((self = [super init])) {
50 [NSBundle loadNibNamed:@"AccountProxy" owner:self];
53 [popUpButton_proxy setMenu:[self _proxyMenu]];
64 return view_accountProxy;
72 [view_accountProxy release];
81 * Called when proxy usage is turned on or off
83 - (IBAction)toggleProxy:(id)sender
85 [self configureControlDimming];
89 * @brief Change proxy type
91 * Called when the proxy type is changed
93 - (void)changeProxyType:(id)sender
95 [self configureControlDimming];
99 * @brief Configure the proxy view for the passed account
101 * @param inAccount The account for which to configure
103 - (void)configureForAccount:(AIAccount *)inAccount
105 if (account != inAccount) {
107 account = [inAccount retain];
110 [checkBox_useProxy setState:[[account preferenceForKey:KEY_ACCOUNT_PROXY_ENABLED
111 group:GROUP_ACCOUNT_STATUS] boolValue]];
112 [popUpButton_proxy compatibleSelectItemWithTag:[[account preferenceForKey:KEY_ACCOUNT_PROXY_TYPE
113 group:GROUP_ACCOUNT_STATUS] intValue]];
116 NSString *proxyHost = [account preferenceForKey:KEY_ACCOUNT_PROXY_HOST group:GROUP_ACCOUNT_STATUS];
117 [textField_proxyHostName setStringValue:(proxyHost ? proxyHost : @"")];
119 NSString *proxyPort = [account preferenceForKey:KEY_ACCOUNT_PROXY_PORT group:GROUP_ACCOUNT_STATUS];
120 [textField_proxyPortNumber setStringValue:(proxyPort ? proxyPort : @"")];
123 NSString *proxyUser = [account preferenceForKey:KEY_ACCOUNT_PROXY_USERNAME group:GROUP_ACCOUNT_STATUS];
124 [textField_proxyUserName setStringValue:(proxyUser ? proxyUser : @"")];
126 [self updatePasswordField];
127 [self configureControlDimming];
132 * @brief Save current control values
134 - (void)saveConfiguration
136 NSString *proxyHostName = [textField_proxyHostName stringValue];
137 NSString *proxyUserName = [textField_proxyUserName stringValue];
140 if (![proxyUserName isEqualToString:[account preferenceForKey:KEY_ACCOUNT_PROXY_USERNAME group:GROUP_ACCOUNT_STATUS]] ||
141 ![proxyHostName isEqualToString:[account preferenceForKey:KEY_ACCOUNT_PROXY_HOST group:GROUP_ACCOUNT_STATUS]]) {
143 [[adium accountController] setPassword:[textField_proxyPassword secureStringValue]
144 forProxyServer:proxyHostName
145 userName:proxyUserName];
149 [account setPreference:[NSNumber numberWithInt:[checkBox_useProxy state]]
150 forKey:KEY_ACCOUNT_PROXY_ENABLED group:GROUP_ACCOUNT_STATUS];
151 [account setPreference:[NSNumber numberWithInt:[[popUpButton_proxy selectedItem] tag]]
152 forKey:KEY_ACCOUNT_PROXY_TYPE group:GROUP_ACCOUNT_STATUS];
155 [account setPreference:[textField_proxyHostName stringValue]
156 forKey:KEY_ACCOUNT_PROXY_HOST group:GROUP_ACCOUNT_STATUS];
157 [account setPreference:[textField_proxyPortNumber stringValue]
158 forKey:KEY_ACCOUNT_PROXY_PORT group:GROUP_ACCOUNT_STATUS];
161 [account setPreference:[textField_proxyUserName stringValue]
162 forKey:KEY_ACCOUNT_PROXY_USERNAME group:GROUP_ACCOUNT_STATUS];
166 * @brief Update password field
168 - (void)updatePasswordField
170 NSString *proxyHostName = [textField_proxyHostName stringValue];
171 NSString *proxyUserName = [textField_proxyUserName stringValue];
173 if (proxyHostName && proxyUserName) {
174 NSString *proxyPassword = [[adium accountController] passwordForProxyServer:proxyHostName
175 userName:proxyUserName];
176 [textField_proxyPassword setStringValue:(proxyPassword ? proxyPassword : @"")];
181 * @brief User changed proxy preference
183 * We set to nil instead of the @"" a stringValue would return because we want to return to the global (default) value
184 * if the user clears the field.
186 - (void)controlTextDidChange:(NSNotification *)aNotification
188 NSTextField *sender = [aNotification object];
190 if (sender == textField_proxyHostName) {
192 } else if (sender == textField_proxyPortNumber) {
193 [account setPreference:[NSNumber numberWithInt:[textField_proxyPortNumber intValue]]
194 forKey:KEY_ACCOUNT_PROXY_PORT
195 group:GROUP_ACCOUNT_STATUS];
197 } else if (sender == textField_proxyUserName) {
198 NSString *userName = [textField_proxyUserName stringValue];
200 //If the username changed, save the new username and clear the password field
201 if (![userName isEqualToString:[account preferenceForKey:KEY_ACCOUNT_PROXY_USERNAME
202 group:GROUP_ACCOUNT_STATUS]]) {
203 [account setPreference:userName
204 forKey:KEY_ACCOUNT_PROXY_USERNAME
205 group:GROUP_ACCOUNT_STATUS];
207 //Update the password field
208 [textField_proxyPassword setStringValue:@""];
209 [textField_proxyPassword setEnabled:(userName && [userName length])];
214 - (BOOL)showProxyDetailsControls
216 AdiumProxyType proxyType = [[popUpButton_proxy selectedItem] tag];
217 BOOL usingSystemwide = (proxyType == Adium_Proxy_Default_SOCKS5 ||
218 proxyType == Adium_Proxy_Default_HTTP ||
219 proxyType == Adium_Proxy_Default_SOCKS4);
221 return !usingSystemwide;
225 * @brief Configure dimming of proxy controls
227 - (void)configureControlDimming
229 AdiumProxyType proxyType = [[popUpButton_proxy selectedItem] tag];
230 BOOL proxyEnabled = [checkBox_useProxy state];
231 BOOL usingSystemwide = (proxyType == Adium_Proxy_Default_SOCKS5 ||
232 proxyType == Adium_Proxy_Default_HTTP ||
233 proxyType == Adium_Proxy_Default_SOCKS4);
235 [popUpButton_proxy setEnabled:proxyEnabled];
236 [textField_proxyHostName setEnabled:(proxyEnabled && !usingSystemwide)];
237 [textField_proxyPortNumber setEnabled:(proxyEnabled && !usingSystemwide)];
238 [textField_proxyUserName setEnabled:(proxyEnabled && !usingSystemwide)];
239 [textField_proxyPassword setEnabled:(proxyEnabled && !usingSystemwide)];
241 [self willChangeValueForKey:@"showProxyDetailsControls"];
242 [self didChangeValueForKey:@"showProxyDetailsControls"];
246 //Proxy type menu ------------------------------------------------------------------------------------------------------
247 #pragma mark Proxy type menu
249 * @brief Build the proxy type menu
251 * @result An NSMenu of supported proxy settings
253 - (NSMenu *)_proxyMenu
255 NSMenu *proxyMenu = [[NSMenu alloc] init];
257 [proxyMenu addItem:[self _proxyMenuItemWithTitle:AILocalizedString(@"Systemwide SOCKS4 Settings",nil) tag:Adium_Proxy_Default_SOCKS4]];
258 [proxyMenu addItem:[self _proxyMenuItemWithTitle:AILocalizedString(@"Systemwide SOCKS5 Settings",nil) tag:Adium_Proxy_Default_SOCKS5]];
259 [proxyMenu addItem:[self _proxyMenuItemWithTitle:AILocalizedString(@"Systemwide HTTP Settings",nil) tag:Adium_Proxy_Default_HTTP]];
260 [proxyMenu addItem:[self _proxyMenuItemWithTitle:@"SOCKS4" tag:Adium_Proxy_SOCKS4]];
261 [proxyMenu addItem:[self _proxyMenuItemWithTitle:@"SOCKS5" tag:Adium_Proxy_SOCKS5]];
262 [proxyMenu addItem:[self _proxyMenuItemWithTitle:@"HTTP" tag:Adium_Proxy_HTTP]];
264 return [proxyMenu autorelease];
268 * @brief Create a proxy menu menuItem
270 * Convenience method for _proxyMenu
272 - (NSMenuItem *)_proxyMenuItemWithTitle:(NSString *)title tag:(int)tag
274 NSMenuItem *menuItem;
276 menuItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:title
278 action:@selector(changeProxyType:)
280 [menuItem setTag:tag];
282 return [menuItem autorelease];