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 "AIAccountController.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
35 * @brief Init our account proxy settings
37 * Loads AccountProxy.nib and sets up menus
41 if((self = [super init])) {
43 [NSBundle loadNibNamed:@"AccountProxy" owner:self];
46 [popUpButton_proxy setMenu:[self _proxyMenu]];
57 return(view_accountProxy);
65 [view_accountProxy release];
74 * Called when proxy usage is turned on or off
76 - (IBAction)toggleProxy:(id)sender
78 [self configureControlDimming];
82 * @brief Change proxy type
84 * Called when the proxy type is changed
86 - (void)changeProxyType:(id)sender
88 [self configureControlDimming];
92 * @brief Configure the proxy view for the passed account
94 * @param inAccount The account for which to configure
96 - (void)configureForAccount:(AIAccount *)inAccount
98 if(account != inAccount){
100 account = [inAccount retain];
103 [checkBox_useProxy setState:[[account preferenceForKey:KEY_ACCOUNT_PROXY_ENABLED
104 group:GROUP_ACCOUNT_STATUS] boolValue]];
105 [popUpButton_proxy compatibleSelectItemWithTag:[[account preferenceForKey:KEY_ACCOUNT_PROXY_TYPE
106 group:GROUP_ACCOUNT_STATUS] intValue]];
109 NSString *proxyHost = [account preferenceForKey:KEY_ACCOUNT_PROXY_HOST group:GROUP_ACCOUNT_STATUS];
110 [textField_proxyHostName setStringValue:(proxyHost ? proxyHost : @"")];
112 NSString *proxyPort = [account preferenceForKey:KEY_ACCOUNT_PROXY_PORT group:GROUP_ACCOUNT_STATUS];
113 [textField_proxyPortNumber setStringValue:(proxyPort ? proxyPort : @"")];
116 NSString *proxyUser = [account preferenceForKey:KEY_ACCOUNT_PROXY_USERNAME group:GROUP_ACCOUNT_STATUS];
117 [textField_proxyUserName setStringValue:(proxyUser ? proxyUser : @"")];
119 [self updatePasswordField];
120 [self configureControlDimming];
125 * @brief Save current control values
127 - (void)saveConfiguration
129 NSString *proxyHostName = [textField_proxyHostName stringValue];
130 NSString *proxyUserName = [textField_proxyUserName stringValue];
133 if(![proxyUserName isEqualToString:[account preferenceForKey:KEY_ACCOUNT_PROXY_USERNAME group:GROUP_ACCOUNT_STATUS]] ||
134 ![proxyHostName isEqualToString:[account preferenceForKey:KEY_ACCOUNT_PROXY_HOST group:GROUP_ACCOUNT_STATUS]]){
136 [[adium accountController] setPassword:[textField_proxyPassword secureStringValue]
137 forProxyServer:proxyHostName
138 userName:proxyUserName];
142 [account setPreference:[NSNumber numberWithInt:[checkBox_useProxy state]]
143 forKey:KEY_ACCOUNT_PROXY_ENABLED group:GROUP_ACCOUNT_STATUS];
144 [account setPreference:[NSNumber numberWithInt:[[popUpButton_proxy selectedItem] tag]]
145 forKey:KEY_ACCOUNT_PROXY_TYPE group:GROUP_ACCOUNT_STATUS];
148 [account setPreference:[textField_proxyHostName stringValue]
149 forKey:KEY_ACCOUNT_PROXY_HOST group:GROUP_ACCOUNT_STATUS];
150 [account setPreference:[textField_proxyPortNumber stringValue]
151 forKey:KEY_ACCOUNT_PROXY_PORT group:GROUP_ACCOUNT_STATUS];
154 [account setPreference:[textField_proxyUserName stringValue]
155 forKey:KEY_ACCOUNT_PROXY_USERNAME group:GROUP_ACCOUNT_STATUS];
159 * @brief Update password field
161 - (void)updatePasswordField
163 NSString *proxyHostName = [textField_proxyHostName stringValue];
164 NSString *proxyUserName = [textField_proxyUserName stringValue];
166 if(proxyHostName && proxyUserName){
167 NSString *proxyPassword = [[adium accountController] passwordForProxyServer:proxyHostName
168 userName:proxyUserName];
169 [textField_proxyPassword setStringValue:(proxyPassword ? proxyPassword : @"")];
174 * @brief User changed proxy preference
176 * We set to nil instead of the @"" a stringValue would return because we want to return to the global (default) value
177 * if the user clears the field.
179 - (void)controlTextDidChange:(NSNotification *)aNotification
181 NSTextField *sender = [aNotification object];
183 if(sender == textField_proxyHostName){
185 }else if(sender == textField_proxyPortNumber){
186 [account setPreference:[NSNumber numberWithInt:[textField_proxyPortNumber intValue]]
187 forKey:KEY_ACCOUNT_PROXY_PORT
188 group:GROUP_ACCOUNT_STATUS];
190 }else if(sender == textField_proxyUserName){
191 NSString *userName = [textField_proxyUserName stringValue];
193 //If the username changed, save the new username and clear the password field
194 if(![userName isEqualToString:[account preferenceForKey:KEY_ACCOUNT_PROXY_USERNAME
195 group:GROUP_ACCOUNT_STATUS]]){
196 [account setPreference:userName
197 forKey:KEY_ACCOUNT_PROXY_USERNAME
198 group:GROUP_ACCOUNT_STATUS];
200 //Update the password field
201 [textField_proxyPassword setStringValue:@""];
202 [textField_proxyPassword setEnabled:(userName && [userName length])];
208 * @brief Configure dimming of proxy controls
210 - (void)configureControlDimming
212 AdiumProxyType proxyType = [[popUpButton_proxy selectedItem] tag];
213 BOOL proxyEnabled = [checkBox_useProxy state];
214 BOOL usingSystemwide = (proxyType == Adium_Proxy_Default_SOCKS5 ||
215 proxyType == Adium_Proxy_Default_HTTP ||
216 proxyType == Adium_Proxy_Default_SOCKS4);
218 [popUpButton_proxy setEnabled:proxyEnabled];
219 [textField_proxyHostName setEnabled:(proxyEnabled && !usingSystemwide)];
220 [textField_proxyPortNumber setEnabled:(proxyEnabled && !usingSystemwide)];
221 [textField_proxyUserName setEnabled:(proxyEnabled && !usingSystemwide)];
222 [textField_proxyPassword setEnabled:(proxyEnabled && !usingSystemwide)];
226 //Proxy type menu ------------------------------------------------------------------------------------------------------
227 #pragma mark Proxy type menu
229 * @brief Build the proxy type menu
231 * @result An NSMenu of supported proxy settings
233 - (NSMenu *)_proxyMenu
235 NSMenu *proxyMenu = [[NSMenu alloc] init];
237 [proxyMenu addItem:[self _proxyMenuItemWithTitle:AILocalizedString(@"Systemwide SOCKS4 Settings",nil) tag:Adium_Proxy_Default_SOCKS4]];
238 [proxyMenu addItem:[self _proxyMenuItemWithTitle:AILocalizedString(@"Systemwide SOCKS5 Settings",nil) tag:Adium_Proxy_Default_SOCKS5]];
239 [proxyMenu addItem:[self _proxyMenuItemWithTitle:AILocalizedString(@"Systemwide HTTP Settings",nil) tag:Adium_Proxy_Default_HTTP]];
240 [proxyMenu addItem:[self _proxyMenuItemWithTitle:@"SOCKS4" tag:Adium_Proxy_SOCKS4]];
241 [proxyMenu addItem:[self _proxyMenuItemWithTitle:@"SOCKS5" tag:Adium_Proxy_SOCKS5]];
242 [proxyMenu addItem:[self _proxyMenuItemWithTitle:@"HTTP" tag:Adium_Proxy_HTTP]];
244 return [proxyMenu autorelease];
248 * @brief Create a proxy menu menuItem
250 * Convenience method for _proxyMenu
252 - (NSMenuItem *)_proxyMenuItemWithTitle:(NSString *)title tag:(int)tag
254 NSMenuItem *menuItem;
256 menuItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:title
258 action:@selector(changeProxyType:)
260 [menuItem setTag:tag];
262 return([menuItem autorelease]);