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.
19 #import "AIPasswordPromptController.h"
20 #import <AIUtilities/AITextFieldAdditions.h>
21 #import <objc/objc-runtime.h>
23 #define PASSWORD_PROMPT_NIB @"PasswordPrompt"
24 #define KEY_PASSWORD_WINDOW_FRAME @"Password Prompt Frame"
26 @interface AIPasswordPromptController (PRIVATE)
27 - (void)setPassword:(NSString *)password;
30 @implementation AIPasswordPromptController
32 - (id)initWithWindowNibName:(NSString *)windowNibName password:(NSString *)inPassword notifyingTarget:(id)inTarget selector:(SEL)inSelector context:(id)inContext
34 if ((self = [super initWithWindowNibName:windowNibName])) {
35 [self setTarget:inTarget selector:inSelector context:inContext];
36 [self setPassword:inPassword];
42 - (void)setTarget:(id)inTarget selector:(SEL)inSelector context:(id)inContext
44 if (inTarget != target) {
46 target = [inTarget retain];
49 selector = inSelector;
51 if (inContext != context) {
53 context = [inContext retain];
57 - (void)setPassword:(NSString *)inPassword
59 if (password != inPassword) {
61 password = [inPassword copy];
76 [[self window] center];
78 [textField_password setStringValue:(password ? password : @"")];
80 [super windowDidLoad];
83 - (NSString *)savedPasswordKey
88 - (IBAction)cancel:(id)sender
90 //close up and notify our caller (pass nil to signify no password)
91 [self closeWindow:nil];
93 void (*targetMethodSender)(id, SEL, id, AIPasswordPromptReturn, id) = (void (*)(id, SEL, id, AIPasswordPromptReturn, id)) objc_msgSend;
94 targetMethodSender(target, selector, nil, AIPasswordPromptCancelReturn, context);
97 - (IBAction)okay:(id)sender
99 NSString *thePassword = [textField_password secureStringValue];
100 BOOL savePassword = [checkBox_savePassword state];
103 if (savePassword && thePassword && [thePassword length]) {
104 [self savePassword:thePassword];
107 //close up and notify our caller
108 [self closeWindow:nil];
110 void (*targetMethodSender)(id, SEL, id, AIPasswordPromptReturn, id) = (void (*)(id, SEL, id, AIPasswordPromptReturn, id)) objc_msgSend;
111 targetMethodSender(target, selector, thePassword, AIPasswordPromptOKReturn, context);
114 - (IBAction)togglePasswordSaved:(id)sender
116 if ([sender state] == NSOffState) {
117 //Forget any saved passwords
118 [self savePassword:nil];
122 - (void)savePassword:(NSString *)password
124 //abstract method. subclasses can do things here.
127 - (void)textDidChange:(NSNotification *)notification
129 //if the password field is empty, disable the OK button.
130 //otherwise, enable it.
131 [button_OK setEnabled:([[textField_password secureStringValue] length] != 0)];
134 // called as the window closes
135 - (void)windowWillClose:(id)sender
137 [NSObject cancelPreviousPerformRequestsWithTarget:[self window]
138 selector:@selector(makeFirstResponder:)
139 object:textField_password];
141 [super windowWillClose:sender];
145 - (void)windowDidBecomeKey:(NSNotification *)aNotification
147 [[self window] performSelector:@selector(makeFirstResponder:)
148 withObject:textField_password
152 - (BOOL)shouldResignKeyWindowWithoutUserInput
154 /* We override this to ensure that password windows
155 * don't have their focus pulled away programmatically.