Added `-[NSArray validateAsPropertyList]` and `-[NSDictionary validateAsPropertyList...
[adiumx.git] / Source / AIAccountProxySettings.m
blob2282a87a973d17593470cf677958e78a8419d0a8
1 /* 
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
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.
8  * 
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.
12  * 
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.
15  */
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;
30 @end
32 @implementation AIAccountProxySettings
34 + (void)initialize
36         if (self == [AIAccountProxySettings class]) {
37                 [self exposeBinding:@"showProxyDetailsControls"];
38         }
41 /*!
42  * @brief Init our account proxy settings
43  *
44  * Loads AccountProxy.nib and sets up menus
45  */
46 - (id)init
48         if ((self = [super init])) {
49                 //Load our view
50                 [NSBundle loadNibNamed:@"AccountProxy" owner:self];
52                 //Setup our menu
53                 [popUpButton_proxy setMenu:[self _proxyMenu]];
54         }
56         return self;
59 /*!
60  * @brief Our view
61  */
62 - (NSView *)view
64         return view_accountProxy;
67 /*!
68  * @brief Deallocate
69  */
70 - (void)dealloc
72         [view_accountProxy release];
74         [super dealloc];
78 /*!
79  * @brief Toggle proxy
80  *
81  * Called when proxy usage is turned on or off
82  */
83 - (IBAction)toggleProxy:(id)sender
85         [self configureControlDimming];
88 /*!
89  * @brief Change proxy type
90  *
91  * Called when the proxy type is changed
92  */
93 - (void)changeProxyType:(id)sender
95         [self configureControlDimming];
98 /*!
99  * @brief Configure the proxy view for the passed account
101  * @param inAccount The account for which to configure
102  */
103 - (void)configureForAccount:(AIAccount *)inAccount
105         if (account != inAccount) {
106                 [account release];
107                 account = [inAccount retain];
109                 //Enabled & Type
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]];
114                 
115                 //Host & Port
116                 NSString        *proxyHost = [account preferenceForKey:KEY_ACCOUNT_PROXY_HOST group:GROUP_ACCOUNT_STATUS];
117                 [textField_proxyHostName setStringValue:(proxyHost ? proxyHost : @"")];
118                 
119                 NSString        *proxyPort = [account preferenceForKey:KEY_ACCOUNT_PROXY_PORT group:GROUP_ACCOUNT_STATUS];
120                 [textField_proxyPortNumber setStringValue:(proxyPort ? proxyPort : @"")];
121                 
122                 //Username
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];
128         }
132  * @brief Save current control values
133  */
134 - (void)saveConfiguration
136         NSString        *proxyHostName = [textField_proxyHostName stringValue];
137         NSString        *proxyUserName = [textField_proxyUserName stringValue];
139         //Password
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]]) {
142                 
143                 [[adium accountController] setPassword:[textField_proxyPassword secureStringValue]
144                                                                 forProxyServer:proxyHostName
145                                                                           userName:proxyUserName];
146         }
148         //Enabled & Type
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];
153         
154         //Host & Port
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];
159         
160         //Username
161         [account setPreference:[textField_proxyUserName stringValue]
162                                         forKey:KEY_ACCOUNT_PROXY_USERNAME group:GROUP_ACCOUNT_STATUS];
166  * @brief Update password field
167  */
168 - (void)updatePasswordField
170         NSString        *proxyHostName = [textField_proxyHostName stringValue];
171         NSString        *proxyUserName = [textField_proxyUserName stringValue];
172         
173         if (proxyHostName && proxyUserName) {
174                 NSString *proxyPassword = [[adium accountController] passwordForProxyServer:proxyHostName
175                                                                                                                                                    userName:proxyUserName];
176                 [textField_proxyPassword setStringValue:(proxyPassword ? proxyPassword : @"")];
177         }
178 }       
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.
185  */
186 - (void)controlTextDidChange:(NSNotification *)aNotification
188         NSTextField *sender = [aNotification object];
189         
190         if (sender == textField_proxyHostName) {
191                 
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];
196                 
197         } else if (sender == textField_proxyUserName) {
198                 NSString        *userName = [textField_proxyUserName stringValue];
199                 
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];
206                         
207                         //Update the password field
208                         [textField_proxyPassword setStringValue:@""];
209                         [textField_proxyPassword setEnabled:(userName && [userName length])];
210                 }
211         }
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
226  */
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);
234         
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)];
240         
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
252  */
253 - (NSMenu *)_proxyMenu
255     NSMenu                      *proxyMenu = [[NSMenu alloc] init];
256         
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]];
263         
264         return [proxyMenu autorelease];
268  * @brief Create a proxy menu menuItem
270  * Convenience method for _proxyMenu
271  */
272 - (NSMenuItem *)_proxyMenuItemWithTitle:(NSString *)title tag:(int)tag
274         NSMenuItem              *menuItem;
275     
276     menuItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:title
277                                                                                                                                         target:self
278                                                                                                                                         action:@selector(changeProxyType:)
279                                                                                                                          keyEquivalent:@""];
280     [menuItem setTag:tag];
281         
282         return [menuItem autorelease];
285 @end