William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | /*
* Adium is the legal property of its developers, whose names are listed in the copyright file included
* with this source distribution.
*
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not,
* write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// $Id$
#import "AILoginWindowController.h"
#import <Adium/AILoginControllerProtocol.h>
#import <AIUtilities/AIDictionaryAdditions.h>
//Preference Keys
#define NEW_USER_NAME @"New User" //Default name of a new user
#define LOGIN_WINDOW_NIB @"LoginSelect" //Filename of the login window nib
#define LOGIN_TIMEOUT 10.0
@interface AILoginWindowController ()
- (id)initWithOwner:(id)inOwner windowNibName:(NSString *)windowNibName;
- (void)dealloc;
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView;
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;
- (IBAction)login:(id)sender;
- (IBAction)editUsers:(id)sender;
- (IBAction)doneEditing:(id)sender;
- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo;
- (void)updateUserList;
- (IBAction)newUser:(id)sender;
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row;
- (IBAction)deleteUser:(id)sender;
- (void)windowDidLoad;
- (void)disableLoginTimeout;
@end
@implementation AILoginWindowController
// return an instance of AILoginController
+ (AILoginWindowController *)loginWindowControllerWithOwner:(id)inOwner
{
/* Release self in windowWillClose: */
return [[self alloc] initWithOwner:inOwner windowNibName:LOGIN_WINDOW_NIB];
}
// Internal --------------------------------------------------------------------------------
// init the login controller
- (id)initWithOwner:(id)inOwner windowNibName:(NSString *)windowNibName
{
if ((self = [super initWithWindowNibName:windowNibName])) {
//Retain our owner
owner = [inOwner retain];
//Get the user list
[self updateUserList];
}
return self;
}
// deallocate the login controller
- (void)dealloc
{
[owner release]; owner = nil;
[userArray release]; userArray = nil;
[super dealloc];
}
// TableView Delegate methods - Return the number of items in the table
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
{
if (tableView == tableView_userList) {
return [userArray count];
} else if (tableView == tableView_editableUserList) {
return [userArray count];
} else {
return 0;
}
}
// TableView Delegate methods - Return the requested item in the table
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
if (tableView == tableView_userList) {
return [userArray objectAtIndex:row];
} else if (tableView == tableView_editableUserList) {
return [userArray objectAtIndex:row];
} else {
return nil;
}
}
// Log in with the selected user
- (IBAction)login:(id)sender
{
NSMutableDictionary *loginDict;
NSString *selectedUserName = [userArray objectAtIndex:[tableView_userList selectedRow]];
//Open the login preferences
loginDict = [NSMutableDictionary dictionaryAtPath:[adium applicationSupportDirectory]
withName:LOGIN_PREFERENCES_FILE_NAME
create:YES];
//Save the 'display on launch' checkbox state
[loginDict setObject:[NSNumber numberWithBool:[checkbox_displayOnStartup state]] forKey:LOGIN_SHOW_WINDOW];
//Save the login they used
#ifdef DEBUG_BUILD
[loginDict setObject:selectedUserName forKey:LOGIN_LAST_USER_DEBUG];
#else
[loginDict setObject:selectedUserName forKey:LOGIN_LAST_USER];
#endif
//Save the login preferences
[loginDict asyncWriteToPath:[adium applicationSupportDirectory]
withName:LOGIN_PREFERENCES_FILE_NAME];
//Login
[owner loginAsUser:selectedUserName];
}
// Display the user list edit sheet
- (IBAction)editUsers:(id)sender
{
[self disableLoginTimeout];
[NSApp beginSheet:panel_userListEditor modalForWindow:[self window] modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:nil];
}
// Close the user list edit sheet
- (IBAction)doneEditing:(id)sender
{
[NSApp endSheet:panel_userListEditor];
}
// Called as the user list edit sheet closes, dismisses the sheet
- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo
{
[sheet orderOut:nil];
}
//Update/Refresh our user list and outline views
- (void)updateUserList
{
//Update the reference
[userArray release]; userArray = nil;
userArray = [[owner userArray] retain];
[tableView_editableUserList reloadData];
[tableView_userList reloadData];
}
// Add a new user
- (IBAction)newUser:(id)sender
{
NSInteger newRow;
//Force the table view to end editing
[tableView_editableUserList reloadData];
//Add a new user
[owner addUser:NEW_USER_NAME];
//Refresh our user list and outline views
[self updateUserList];
//Select, scroll to, and 'edit' the new user
newRow = [userArray indexOfObject:NEW_USER_NAME];
[tableView_editableUserList selectRowIndexes:[NSIndexSet indexSetWithIndex:newRow] byExtendingSelection:NO];
[tableView_editableUserList scrollRowToVisible:newRow];
[tableView_editableUserList editColumn:0 row:newRow withEvent:nil select:YES];
[self disableLoginTimeout];
}
// Rename a user
- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
if (tableView == tableView_editableUserList) {
//Rename the user
[owner renameUser:[userArray objectAtIndex:row] to:object];
//Refresh our user list
[self updateUserList];
if (loginTimer) {
[loginTimer invalidate]; [loginTimer release]; loginTimer = nil;
}
}
}
- (void)tableViewSelectionDidChange:(NSNotification *)inNotification
{
[self disableLoginTimeout];
}
// Delete the selected user
- (IBAction)deleteUser:(id)sender
{
//Force the table view to end editing
[tableView_editableUserList reloadData];
//Delete the user
[owner deleteUser:[userArray objectAtIndex:[tableView_editableUserList selectedRow]]];
//Refresh our user list
[self updateUserList];
[self disableLoginTimeout];
}
// set up the window before it is displayed
- (void)windowDidLoad
{
NSDictionary *loginDict;
NSString *lastLogin;
//Open the login preferences
loginDict = [NSDictionary dictionaryAtPath:[adium applicationSupportDirectory]
withName:LOGIN_PREFERENCES_FILE_NAME
create:YES];
//Center the window
[[self window] center];
//Setup the 'display on launch' checkbox
[checkbox_displayOnStartup setState:[[loginDict objectForKey:LOGIN_SHOW_WINDOW] boolValue]];
//Select the login they used last
#if DEBUG_BUILD
lastLogin = [loginDict objectForKey:LOGIN_LAST_USER_DEBUG];
#else
lastLogin = [loginDict objectForKey:LOGIN_LAST_USER];
#endif
NSIndexSet *rowIndex;
if (lastLogin != nil && [lastLogin length] != 0 && [userArray indexOfObject:lastLogin] != NSNotFound) {
rowIndex = [NSIndexSet indexSetWithIndex:[userArray indexOfObject:lastLogin]];
} else {
rowIndex = [NSIndexSet indexSetWithIndex:0];
}
[tableView_userList selectRowIndexes:rowIndex byExtendingSelection:NO];
//Set login so it's called when the user double clicks a name
[tableView_userList setDoubleAction:@selector(login:)];
loginTimer = [[NSTimer scheduledTimerWithTimeInterval:LOGIN_TIMEOUT
target:self
selector:@selector(login:)
userInfo:nil
repeats:NO] retain];
[tableView_userList setDelegate:self];
[tableView_userList setDataSource:self];
[self updateUserList];
}
// called as the window closes
- (void)windowWillClose:(id)sender
{
[super windowWillClose:sender];
[loginTimer invalidate]; [loginTimer release]; loginTimer = nil;
[self autorelease];
}
- (void)disableLoginTimeout
{
if (loginTimer) {
[loginTimer invalidate]; [loginTimer release]; loginTimer = nil;
}
}
@end
|