Merged [15040]: Trying some magic: 5 seconds after the last unreachable host is repor...
[adiumx.git] / Source / AIAccountProxySettings.m
blob4deed1faf47d268428d756d4556f77d5c7bd35f8
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 "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;
30 @end
32 @implementation AIAccountProxySettings
34 /*!
35  * @brief Init our account proxy settings
36  *
37  * Loads AccountProxy.nib and sets up menus
38  */
39 - (id)init
41         if((self = [super init])) {
42                 //Load our view
43                 [NSBundle loadNibNamed:@"AccountProxy" owner:self];
45                 //Setup our menu
46                 [popUpButton_proxy setMenu:[self _proxyMenu]];
47         }
49         return self;
52 /*!
53  * @brief Our view
54  */
55 - (NSView *)view
57         return(view_accountProxy);
60 /*!
61  * @brief Deallocate
62  */
63 - (void)dealloc
65         [view_accountProxy release];
67         [super dealloc];
71 /*!
72  * @brief Toggle proxy
73  *
74  * Called when proxy usage is turned on or off
75  */
76 - (IBAction)toggleProxy:(id)sender
78         [self configureControlDimming];
81 /*!
82  * @brief Change proxy type
83  *
84  * Called when the proxy type is changed
85  */
86 - (void)changeProxyType:(id)sender
88         [self configureControlDimming];
91 /*!
92  * @brief Configure the proxy view for the passed account
93  *
94  * @param inAccount The account for which to configure
95  */
96 - (void)configureForAccount:(AIAccount *)inAccount
98         if(account != inAccount){
99                 [account release];
100                 account = [inAccount retain];
102                 //Enabled & Type
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]];
107                 
108                 //Host & Port
109                 NSString        *proxyHost = [account preferenceForKey:KEY_ACCOUNT_PROXY_HOST group:GROUP_ACCOUNT_STATUS];
110                 [textField_proxyHostName setStringValue:(proxyHost ? proxyHost : @"")];
111                 
112                 NSString        *proxyPort = [account preferenceForKey:KEY_ACCOUNT_PROXY_PORT group:GROUP_ACCOUNT_STATUS];
113                 [textField_proxyPortNumber setStringValue:(proxyPort ? proxyPort : @"")];
114                 
115                 //Username
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];
121         }
125  * @brief Save current control values
126  */
127 - (void)saveConfiguration
129         NSString        *proxyHostName = [textField_proxyHostName stringValue];
130         NSString        *proxyUserName = [textField_proxyUserName stringValue];
132         //Password
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]]){
135                 
136                 [[adium accountController] setPassword:[textField_proxyPassword secureStringValue]
137                                                                 forProxyServer:proxyHostName
138                                                                           userName:proxyUserName];
139         }
141         //Enabled & Type
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];
146         
147         //Host & Port
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];
152         
153         //Username
154         [account setPreference:[textField_proxyUserName stringValue]
155                                         forKey:KEY_ACCOUNT_PROXY_USERNAME group:GROUP_ACCOUNT_STATUS];
159  * @brief Update password field
160  */
161 - (void)updatePasswordField
163         NSString        *proxyHostName = [textField_proxyHostName stringValue];
164         NSString        *proxyUserName = [textField_proxyUserName stringValue];
165         
166         if(proxyHostName && proxyUserName){
167                 NSString *proxyPassword = [[adium accountController] passwordForProxyServer:proxyHostName
168                                                                                                                                                    userName:proxyUserName];
169                 [textField_proxyPassword setStringValue:(proxyPassword ? proxyPassword : @"")];
170         }
171 }       
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.
178  */
179 - (void)controlTextDidChange:(NSNotification *)aNotification
181         NSTextField *sender = [aNotification object];
182         
183         if(sender == textField_proxyHostName){
184                 
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];
189                 
190         }else if(sender == textField_proxyUserName){
191                 NSString        *userName = [textField_proxyUserName stringValue];
192                 
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];
199                         
200                         //Update the password field
201                         [textField_proxyPassword setStringValue:@""];
202                         [textField_proxyPassword setEnabled:(userName && [userName length])];
203                 }
204         }
208  * @brief Configure dimming of proxy controls
209  */
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);
217         
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
232  */
233 - (NSMenu *)_proxyMenu
235     NSMenu                      *proxyMenu = [[NSMenu alloc] init];
236         
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]];
243         
244         return [proxyMenu autorelease];
248  * @brief Create a proxy menu menuItem
250  * Convenience method for _proxyMenu
251  */
252 - (NSMenuItem *)_proxyMenuItemWithTitle:(NSString *)title tag:(int)tag
254         NSMenuItem              *menuItem;
255     
256     menuItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:title
257                                                                                                                                         target:self
258                                                                                                                                         action:@selector(changeProxyType:)
259                                                                                                                          keyEquivalent:@""];
260     [menuItem setTag:tag];
261         
262         return([menuItem autorelease]);
265 @end