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 "AILoginController.h"
20 #import "AILoginWindowController.h"
21 #import <AIUtilities/AIDictionaryAdditions.h>
22 #import <AIUtilities/AIFileManagerAdditions.h>
23 #import <AIUtilities/AIEventAdditions.h>
26 #define PATH_USERS @"/Users" //Path of the users folder
29 #define DEFAULT_USER_NAME @"Default" //The default user name
31 @implementation AILoginController
33 // Init this controller
36 if ((self = [super init])) {
43 - (void)controllerDidLoad
47 // Close this controller
48 - (void)controllerWillClose
56 [userDirectory release];
57 [currentUser release];
62 // Prompts for a user, or automatically selects one
63 - (void)requestUserNotifyingTarget:(id)inTarget selector:(SEL)inSelector
65 NSMutableDictionary *loginDict;
67 unsigned argumentIndex;
68 NSString *userName = nil;
70 //Retain the target and selector
72 selector = inSelector;
74 //Open the login preferences
75 loginDict = [NSMutableDictionary dictionaryAtPath:[adium applicationSupportDirectory] withName:LOGIN_PREFERENCES_FILE_NAME create:YES];
77 //Make sure that atleast 1 login name is available. If not, create the name 'default'
78 if ([[self userArray] count] == 0) {
79 //Create a 'default' user
80 [self addUser:DEFAULT_USER_NAME];
82 //Set 'default' as the login of choice
83 [loginDict setObject:DEFAULT_USER_NAME forKey:LOGIN_LAST_USER];
84 [loginDict writeToPath:[adium applicationSupportDirectory] withName:LOGIN_PREFERENCES_FILE_NAME];
87 //Retrieve the desired user from the command line if possible
88 arguments = [[NSProcessInfo processInfo] arguments];
89 if (arguments && ([arguments count] > 1)) {
91 argumentIndex = [arguments indexOfObject:@"--user"];
92 if ((argumentIndex != NSNotFound) && ([arguments count] > argumentIndex + 1)) {
93 userName = [[[arguments objectAtIndex:argumentIndex+1] copy] autorelease];
98 If we don't have a userName yet, show the login select window if:
100 - We should always show it
102 - LOGIN_LAST_USER does not indicate a valid user
105 BOOL userRequestedShowWindow = NO;
107 if (([NSEvent optionKey]) ||
108 (userRequestedShowWindow = [[loginDict objectForKey:LOGIN_SHOW_WINDOW] boolValue]) ||
109 (!(userName = [loginDict objectForKey:LOGIN_LAST_USER]))) {
111 //Prompt for the user
112 loginWindowController = [[AILoginWindowController loginWindowControllerWithOwner:self] retain];
113 [loginWindowController showWindow:nil];
115 //If the user always wants to see the window, disable the login timeout
116 if (userRequestedShowWindow) [loginWindowController disableLoginTimeout];
122 [self loginAsUser:userName];
126 // Returns the current user's Adium home directory
127 - (NSString *)userDirectory
129 return userDirectory;
133 - (NSString *)currentUser
138 // Sets the correct user directory and sends out a login message
139 - (void)loginAsUser:(NSString *)userName
141 NSParameterAssert(userName != nil);
143 //Close the login panel
144 if (loginWindowController) {
145 [loginWindowController closeWindow:nil];
146 [loginWindowController release]; loginWindowController = nil;
149 //Save the user directory
150 currentUser = [userName retain];
151 userDirectory = [[[[adium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:userName] retain];
153 //Tell Adium to complete login
154 [target performSelector:selector];
157 // Creates and returns a mutable array of the login users
158 - (NSArray *)userArray
161 NSArray *directoryContents;
162 NSMutableArray *userArray;
168 userPath = [[adium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS];
170 //Build the user array
171 userArray = [[NSMutableArray alloc] init];
173 directoryContents = [[NSFileManager defaultManager] directoryContentsAtPath:userPath];
174 count = [directoryContents count];
175 for (loop = 0;loop < count;loop++) {
176 NSString *path = [directoryContents objectAtIndex:loop];
178 //Fetch the names of all directories
179 if ([[NSFileManager defaultManager] fileExistsAtPath:[userPath stringByAppendingPathComponent:path] isDirectory:&isDirectory]) {
181 [userArray addObject:[path lastPathComponent]];
186 return [userArray autorelease];
190 - (void)deleteUser:(NSString *)inUserName
192 NSString *sourcePath;
194 NSParameterAssert(inUserName != nil);
196 //Create the source and dest paths
197 sourcePath = [[[adium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:inUserName];
198 [[NSFileManager defaultManager] trashFileAtPath:sourcePath];
201 // Add a user with the specified name
202 - (void)addUser:(NSString *)inUserName
206 NSParameterAssert(inUserName != nil);
208 //Create the user path
209 userPath = [[[adium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:inUserName];
211 //Create a folder for the new user
212 [[NSFileManager defaultManager] createDirectoriesForPath:userPath];
215 // Rename an existing user
216 - (void)renameUser:(NSString *)oldName to:(NSString *)newName
218 NSString *sourcePath, *destPath;
220 NSParameterAssert(oldName != nil);
221 NSParameterAssert(newName != nil);
223 //Create the source and dest paths
224 sourcePath = [[[adium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:oldName];
225 destPath = [[[adium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:newName];
227 //Rename the user's folder (by moving it to a path with a different name)
228 [[NSFileManager defaultManager] movePath:sourcePath toPath:destPath handler:nil];