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 "AILoginWindowController.h"
20 #import "AILoginController.h"
21 #import <AIUtilities/AIDictionaryAdditions.h>
24 #define NEW_USER_NAME @"New User" //Default name of a new user
25 #define LOGIN_WINDOW_NIB @"LoginSelect" //Filename of the login window nib
27 #define LOGIN_TIMEOUT 10.0
29 @interface AILoginWindowController (PRIVATE)
30 - (id)initWithOwner:(id)inOwner windowNibName:(NSString *)windowNibName;
32 - (int)numberOfRowsInTableView:(NSTableView *)tableView;
33 - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row;
34 - (IBAction)login:(id)sender;
35 - (IBAction)editUsers:(id)sender;
36 - (IBAction)doneEditing:(id)sender;
37 - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo;
38 - (void)updateUserList;
39 - (IBAction)newUser:(id)sender;
40 - (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row;
41 - (IBAction)deleteUser:(id)sender;
42 - (void)windowDidLoad;
43 - (void)disableLoginTimeout;
46 @implementation AILoginWindowController
47 // return an instance of AILoginController
48 + (AILoginWindowController *)loginWindowControllerWithOwner:(id)inOwner
50 return([[[self alloc] initWithOwner:inOwner windowNibName:LOGIN_WINDOW_NIB] autorelease]);
54 // Internal --------------------------------------------------------------------------------
55 // init the login controller
56 - (id)initWithOwner:(id)inOwner windowNibName:(NSString *)windowNibName
58 if((self = [super initWithWindowNibName:windowNibName])) {
60 owner = [inOwner retain];
63 [self updateUserList];
68 // deallocate the login controller
71 [owner release]; owner = nil;
72 [userArray release]; userArray = nil;
77 // TableView Delegate methods - Return the number of items in the table
78 - (int)numberOfRowsInTableView:(NSTableView *)tableView
80 if(tableView == tableView_userList){
81 return([userArray count]);
82 }else if(tableView == tableView_editableUserList){
83 return([userArray count]);
89 // TableView Delegate methods - Return the requested item in the table
90 - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
92 if(tableView == tableView_userList){
93 return([userArray objectAtIndex:row]);
94 }else if(tableView == tableView_editableUserList){
95 return([userArray objectAtIndex:row]);
102 // Log in with the selected user
103 - (IBAction)login:(id)sender
105 NSMutableDictionary *loginDict;
106 NSString *selectedUserName = [userArray objectAtIndex:[tableView_userList selectedRow]];
108 //Open the login preferences
109 loginDict = [NSMutableDictionary dictionaryAtPath:[AIAdium applicationSupportDirectory]
110 withName:LOGIN_PREFERENCES_FILE_NAME
113 //Save the 'display on launch' checkbox state
114 [loginDict setObject:[NSNumber numberWithBool:[checkbox_displayOnStartup state]] forKey:LOGIN_SHOW_WINDOW];
116 //Save the login they used
117 [loginDict setObject:selectedUserName forKey:LOGIN_LAST_USER];
119 //Save the login preferences
120 [loginDict writeToPath:[AIAdium applicationSupportDirectory]
121 withName:LOGIN_PREFERENCES_FILE_NAME];
124 [owner loginAsUser:selectedUserName];
127 // Display the user list edit sheet
128 - (IBAction)editUsers:(id)sender
130 [self disableLoginTimeout];
132 [NSApp beginSheet:panel_userListEditor modalForWindow:[self window] modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:nil];
135 // Close the user list edit sheet
136 - (IBAction)doneEditing:(id)sender
138 [NSApp endSheet:panel_userListEditor];
141 // Called as the user list edit sheet closes, dismisses the sheet
142 - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
144 [sheet orderOut:nil];
147 //Update/Refresh our user list and outline views
148 - (void)updateUserList
150 //Update the reference
151 [userArray release]; userArray = nil;
152 userArray = [[owner userArray] retain];
154 //Refresh the tables (if the window is loaded)
155 if(tableView_userList != nil && tableView_editableUserList != nil){
156 [tableView_userList reloadData];
157 [tableView_editableUserList reloadData];
162 - (IBAction)newUser:(id)sender
166 //Force the table view to end editing
167 [tableView_editableUserList reloadData];
170 [owner addUser:NEW_USER_NAME];
172 //Refresh our user list and outline views
173 [self updateUserList];
175 //Select, scroll to, and 'edit' the new user
176 newRow = [userArray indexOfObject:NEW_USER_NAME];
177 [tableView_editableUserList selectRow:newRow byExtendingSelection:NO];
178 [tableView_editableUserList scrollRowToVisible:newRow];
179 [tableView_editableUserList editColumn:0 row:newRow withEvent:nil select:YES];
181 [self disableLoginTimeout];
185 - (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
187 if(tableView == tableView_editableUserList){
189 [owner renameUser:[userArray objectAtIndex:row] to:object];
191 //Refresh our user list
192 [self updateUserList];
195 [loginTimer invalidate]; [loginTimer release]; loginTimer = nil;
200 - (void)tableViewSelectionDidChange:(NSNotification *)inNotification
202 [self disableLoginTimeout];
205 // Delete the selected user
206 - (IBAction)deleteUser:(id)sender
208 //Force the table view to end editing
209 [tableView_editableUserList reloadData];
212 [owner deleteUser:[userArray objectAtIndex:[tableView_editableUserList selectedRow]]];
214 //Refresh our user list
215 [self updateUserList];
217 [self disableLoginTimeout];
220 // set up the window before it is displayed
221 - (void)windowDidLoad
223 NSDictionary *loginDict;
226 //Open the login preferences
227 loginDict = [NSDictionary dictionaryAtPath:[AIAdium applicationSupportDirectory]
228 withName:LOGIN_PREFERENCES_FILE_NAME
232 [[self window] center];
234 //Setup the 'display on launch' checkbox
235 [checkbox_displayOnStartup setState:[[loginDict objectForKey:LOGIN_SHOW_WINDOW] boolValue]];
237 //Select the login they used last
238 lastLogin = [loginDict objectForKey:LOGIN_LAST_USER];
239 if(lastLogin != nil && [lastLogin length] != 0 && [userArray indexOfObject:lastLogin] != NSNotFound){
240 [tableView_userList selectRow:[userArray indexOfObject:lastLogin] byExtendingSelection:NO];
242 [tableView_userList selectRow:0 byExtendingSelection:NO];
245 //Set login so it's called when the user double clicks a name
246 [tableView_userList setDoubleAction:@selector(login:)];
248 loginTimer = [[NSTimer scheduledTimerWithTimeInterval:LOGIN_TIMEOUT
250 selector:@selector(login:)
254 [tableView_userList setDelegate:self];
255 [tableView_userList setDataSource:self];
259 // called as the window closes
260 - (void)windowWillClose:(id)sender
262 [super windowWillClose:sender];
263 [loginTimer invalidate]; [loginTimer release]; loginTimer = nil;
266 - (void)disableLoginTimeout
269 [loginTimer invalidate]; [loginTimer release]; loginTimer = nil;