Added `-[NSArray validateAsPropertyList]` and `-[NSDictionary validateAsPropertyList...
[adiumx.git] / Source / AIPasswordPromptController.m
blobc89c00385959f4a437461901a8349157e73bde7c
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 // $Id$
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;
28 @end
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];
37         }
39     return self;
42 - (void)setTarget:(id)inTarget selector:(SEL)inSelector context:(id)inContext
44         if (inTarget != target) {
45                 [target release];
46                 target = [inTarget retain];
47         }
48         
49         selector = inSelector;
50         
51         if (inContext != context) {
52                 [context release];
53                 context = [inContext retain];
54         }
57 - (void)setPassword:(NSString *)inPassword
59         if (password != inPassword) {
60                 [password release];
61                 password = [inPassword copy];
62         }
65 - (void)dealloc
67     [target release];
68         [context release];
69         [password release];
71     [super dealloc];
74 - (void)windowDidLoad
76         [[self window] center];
78         [textField_password setStringValue:(password ? password : @"")];
80         [super windowDidLoad];
83 - (NSString *)savedPasswordKey
85         return nil;
88 - (IBAction)cancel:(id)sender
90     //close up and notify our caller (pass nil to signify no password)
91     [self closeWindow:nil]; 
92         
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];
102         //save password?
103         if (savePassword && thePassword && [thePassword length]) {
104                 [self savePassword:thePassword];
105         }
107         //close up and notify our caller
108         [self closeWindow:nil];    
109         
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];
119     }
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];
142     [self autorelease];
145 - (void)windowDidBecomeKey:(NSNotification *)aNotification
147         [[self window] performSelector:@selector(makeFirstResponder:)
148                                                 withObject:textField_password
149                                                 afterDelay:0];
152 - (BOOL)shouldResignKeyWindowWithoutUserInput
154         /* We override this to ensure that password windows
155          * don't have their focus pulled away programmatically.
156          */
157         return NO;
160 @end