Merged [13645] and [13646]: Fixed one of the longest-standing Adium bugs: The Color...
[adiumx.git] / Source / ESAccountPasswordPromptController.m
blobdb87290c74930dedd39e0f6679e0702be8833e68
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 "ESAccountPasswordPromptController.h"
19 #import <Adium/AIAccount.h>
20 #import <Adium/AIService.h>
21 #import <Adium/AIServiceIcons.h>
23 #define ACCOUNT_PASSWORD_PROMPT_NIB             @"PasswordPrompt"
24 #define ACCOUNT_PASSWORD_REQUIRED               AILocalizedString(@"Connecting Account","Password prompt window title")
26 @interface ESAccountPasswordPromptController (PRIVATE)
27 - (id)initWithWindowNibName:(NSString *)windowNibName forAccount:(AIAccount *)inAccount notifyingTarget:(id)inTarget selector:(SEL)inSelector context:(id)inContext;
28 @end
31  * @class ESAccountPasswordPromptController
32  * @brief Account password prompt window controller
33  * 
34  * This AIPasswordPromptController subclass is responsible for requesting an account's password from the user when it
35  * attempts to connect and the password isn't saved.  The user has the option of saving the password for future use.
36  *
37  * Only one password prompt window per account is shown; an attempt to show a prompt for an account which already has
38  * an open account results in the existing account becoming key and front.
39  */
40 @implementation ESAccountPasswordPromptController
42 static NSMutableDictionary      *passwordPromptControllerDict = nil;
44 + (void)showPasswordPromptForAccount:(AIAccount *)inAccount notifyingTarget:(id)inTarget selector:(SEL)inSelector context:(id)inContext
45 {       
46         ESAccountPasswordPromptController       *controller = nil;
47         NSString                                                        *identifier = [inAccount internalObjectID];
48         
49         if (!passwordPromptControllerDict) passwordPromptControllerDict = [[NSMutableDictionary alloc] init];
50         
51         if ((controller = [passwordPromptControllerDict objectForKey:identifier])) {
52                 //Update the existing controller for this account to have the new target, selector, and context
53                 [controller setTarget:inTarget selector:inSelector context:inContext];
55         } else {
56                 if ((controller = [[self alloc] initWithWindowNibName:ACCOUNT_PASSWORD_PROMPT_NIB 
57                                                                                                    forAccount:inAccount 
58                                                                                           notifyingTarget:inTarget
59                                                                                                          selector:inSelector
60                                                                                                           context:inContext])) {
61                         [passwordPromptControllerDict setObject:controller
62                                                                                          forKey:identifier];
63                 }
64         }
65         
66     //bring the window front
67         [controller showWindow:nil];
68         [[controller window] makeKeyAndOrderFront:nil];
71 - (id)initWithWindowNibName:(NSString *)windowNibName forAccount:(AIAccount *)inAccount notifyingTarget:(id)inTarget selector:(SEL)inSelector context:(id)inContext
73     if((self = [super initWithWindowNibName:windowNibName notifyingTarget:inTarget selector:inSelector context:inContext])) {
74                 account = [inAccount retain];
75         }
77     return(self);
80 - (void)dealloc
82     [account release];
83         
84         [super dealloc];
88  * @brief Our window will close
89  *
90  * Remove this controller from our dictionary of controllers
91  */
92 - (void)windowWillClose:(id)sender
94         NSString        *identifier = [account internalObjectID];
96         [passwordPromptControllerDict removeObjectForKey:identifier];
98         [super windowWillClose:sender];
102  * @brief Window laoded
104  * Perform initial configuration
105  */
106 - (void)windowDidLoad
108         [[self window] setTitle:ACCOUNT_PASSWORD_REQUIRED];
109         
110     [textField_account setStringValue:[account formattedUID]];
111         [imageView_service setImage:[AIServiceIcons serviceIconForService:[account service]
112                                                                                                                                  type:AIServiceIconLarge
113                                                                                                                         direction:AIIconNormal]];
114         
115         [checkBox_savePassword setState:[[account preferenceForKey:[self savedPasswordKey] 
116                                                                                                                  group:GROUP_ACCOUNT_STATUS] boolValue]];
118         [super windowDidLoad];
122  * @brief The key 
123  */
124 - (NSString *)savedPasswordKey
126         return @"SavedPassword";
129 //Save a password; pass nil to forget the password
130 - (void)savePassword:(NSString *)password
132         if (password){
133                 [[adium accountController] setPassword:password forAccount:account];    
134         }else{
135                 [[adium accountController] forgetPasswordForAccount:account];   
136         }
139 @end