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 | /*
* 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 "AILoginController.h"
#import "AILoginWindowController.h"
#import <AIUtilities/AIDictionaryAdditions.h>
#import <AIUtilities/AIFileManagerAdditions.h>
#import <AIUtilities/AIEventAdditions.h>
//Paths & Filenames
#define PATH_USERS @"/Users" //Path of the users folder
//Other
#define DEFAULT_USER_NAME @"Default" //The default user name
@implementation AILoginController
// Init this controller
- (id)init
{
if ((self = [super init])) {
userDirectory = nil;
}
return self;
}
- (void)controllerDidLoad
{
}
// Close this controller
- (void)controllerWillClose
{
}
// Dealloc
- (void)dealloc
{
[userDirectory release];
[currentUser release];
[super dealloc];
}
// Prompts for a user, or automatically selects one
- (void)requestUserNotifyingTarget:(id)inTarget selector:(SEL)inSelector
{
NSMutableDictionary *loginDict;
NSArray *arguments;
NSUInteger argumentIndex;
NSString *userName = nil;
//Retain the target and selector
target = inTarget;
selector = inSelector;
//Open the login preferences
loginDict = [NSMutableDictionary dictionaryAtPath:[adium applicationSupportDirectory] withName:LOGIN_PREFERENCES_FILE_NAME create:YES];
//Make sure that atleast 1 login name is available. If not, create the name 'default'
if ([[self userArray] count] == 0) {
//Create a 'default' user
[self addUser:DEFAULT_USER_NAME];
//Set 'default' as the login of choice
[loginDict setObject:DEFAULT_USER_NAME forKey:LOGIN_LAST_USER];
[loginDict asyncWriteToPath:[adium applicationSupportDirectory] withName:LOGIN_PREFERENCES_FILE_NAME];
}
//Retrieve the desired user from the command line if possible
arguments = [[NSProcessInfo processInfo] arguments];
if (arguments && ([arguments count] > 1)) {
argumentIndex = [arguments indexOfObject:@"--user"];
if ((argumentIndex != NSNotFound) && ([arguments count] > argumentIndex + 1)) {
userName = [[[arguments objectAtIndex:argumentIndex+1] copy] autorelease];
}
}
/*
If we don't have a userName yet, show the login select window if:
- Option is held down
- We should always show it
or
- LOGIN_LAST_USER does not indicate a valid user
*/
if (!userName) {
BOOL userRequestedShowWindow = NO;
BOOL shouldShowWindow;
shouldShowWindow = [NSEvent optionKey];
if (!shouldShowWindow)
shouldShowWindow = (userRequestedShowWindow = [[loginDict objectForKey:LOGIN_SHOW_WINDOW] boolValue]);
if (!shouldShowWindow) {
#ifdef DEBUG_BUILD
userName = [loginDict objectForKey:LOGIN_LAST_USER_DEBUG];
if (!userName)
#endif
shouldShowWindow = ((userName = [loginDict objectForKey:LOGIN_LAST_USER]) == nil);
}
if (shouldShowWindow) {
//Prompt for the user
loginWindowController = [[AILoginWindowController loginWindowControllerWithOwner:self] retain];
[loginWindowController showWindow:nil];
//If the user always wants to see the window, disable the login timeout
if (userRequestedShowWindow) [loginWindowController disableLoginTimeout];
}
}
if (userName) {
[self loginAsUser:userName];
}
}
// Returns the current user's Adium home directory
- (NSString *)userDirectory
{
return userDirectory;
}
- (NSString *)currentUser
{
return currentUser;
}
// Sets the correct user directory and sends out a login message
- (void)loginAsUser:(NSString *)userName
{
NSParameterAssert(userName != nil);
//Close the login panel
if (loginWindowController) {
[loginWindowController closeWindow:nil];
[loginWindowController release]; loginWindowController = nil;
}
//Save the user directory
currentUser = [userName retain];
userDirectory = [[[[adium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:userName] retain];
//Tell Adium to complete login
[target performSelector:selector];
}
// Creates and returns a mutable array of the login users
- (NSArray *)userArray
{
BOOL isDirectory;
//Get the users path
NSString *userPath = [[adium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS];
//Build the user array
NSMutableArray *userArray = [NSMutableArray array];
for (NSString *path in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:userPath error:NULL]) {
//Fetch the names of all directories
if ([[NSFileManager defaultManager] fileExistsAtPath:[userPath stringByAppendingPathComponent:path] isDirectory:&isDirectory]) {
if (isDirectory) {
[userArray addObject:[path lastPathComponent]];
}
}
}
return userArray;
}
// Delete a user
- (void)deleteUser:(NSString *)inUserName
{
NSString *sourcePath;
NSParameterAssert(inUserName != nil);
//Create the source and dest paths
sourcePath = [[[adium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:inUserName];
[[NSFileManager defaultManager] trashFileAtPath:sourcePath];
}
// Add a user with the specified name
- (void)addUser:(NSString *)inUserName
{
NSString *userPath;
NSParameterAssert(inUserName != nil);
//Create the user path
userPath = [[[adium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:inUserName];
//Create a folder for the new user
[[NSFileManager defaultManager] createDirectoryAtPath:userPath withIntermediateDirectories:YES attributes:nil error:NULL];
}
// Rename an existing user
- (void)renameUser:(NSString *)oldName to:(NSString *)newName
{
NSString *sourcePath, *destPath;
NSParameterAssert(oldName != nil);
NSParameterAssert(newName != nil);
//Create the source and dest paths
sourcePath = [[[adium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:oldName];
destPath = [[[adium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:newName];
//Rename the user's folder (by moving it to a path with a different name)
[[NSFileManager defaultManager] moveItemAtPath:sourcePath toPath:destPath error:NULL];
}
@end
|