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
27 #define PATH_TRASH @"~/.Trash" //Path to the trash
29 #define DEFAULT_USER_NAME @"Default" //The default user name
31 @implementation AILoginController
33 // Init this controller
34 - (void)initController
39 // Close this controller
40 - (void)closeController
48 [userDirectory release];
49 [currentUser release];
54 // Prompts for a user, or automatically selects one
55 - (void)requestUserNotifyingTarget:(id)inTarget selector:(SEL)inSelector
57 NSMutableDictionary *loginDict;
59 unsigned argumentIndex;
60 NSString *userName = nil;
62 //Retain the target and selector
64 selector = inSelector;
66 //Open the login preferences
67 loginDict = [NSMutableDictionary dictionaryAtPath:[AIAdium applicationSupportDirectory] withName:LOGIN_PREFERENCES_FILE_NAME create:YES];
69 //Make sure that atleast 1 login name is available. If not, create the name 'default'
70 if([[self userArray] count] == 0){
71 //Create a 'default' user
72 [self addUser:DEFAULT_USER_NAME];
74 //Set 'default' as the login of choice
75 [loginDict setObject:DEFAULT_USER_NAME forKey:LOGIN_LAST_USER];
76 [loginDict writeToPath:[AIAdium applicationSupportDirectory] withName:LOGIN_PREFERENCES_FILE_NAME];
79 //Retrieve the desired user from the command line if possible
80 arguments = [[NSProcessInfo processInfo] arguments];
81 if(arguments && ([arguments count] > 1)){
83 argumentIndex = [arguments indexOfObject:@"--user"];
84 if((argumentIndex != NSNotFound) && ([arguments count] > argumentIndex + 1)){
85 userName = [[[arguments objectAtIndex:argumentIndex+1] copy] autorelease];
90 If we don't have a userName yet, show the login select window if:
92 - We should always show it
94 - LOGIN_LAST_USER does not indicate a valid user
97 BOOL userRequestedShowWindow = NO;
99 if(([NSEvent optionKey]) ||
100 (userRequestedShowWindow = [[loginDict objectForKey:LOGIN_SHOW_WINDOW] boolValue]) ||
101 (!(userName = [loginDict objectForKey:LOGIN_LAST_USER]))){
103 //Prompt for the user
104 loginWindowController = [[AILoginWindowController loginWindowControllerWithOwner:self] retain];
105 [loginWindowController showWindow:nil];
107 //If the user always wants to see the window, disable the login timeout
108 if(userRequestedShowWindow) [loginWindowController disableLoginTimeout];
114 [self loginAsUser:userName];
118 // Returns the current user's Adium home directory
119 - (NSString *)userDirectory
121 return(userDirectory);
125 - (NSString *)currentUser
130 // Sets the correct user directory and sends out a login message
131 - (void)loginAsUser:(NSString *)userName
133 NSParameterAssert(userName != nil);
135 //Close the login panel
136 if(loginWindowController){
137 [loginWindowController closeWindow:nil];
138 [loginWindowController release]; loginWindowController = nil;
141 //Save the user directory
142 currentUser = [userName retain];
143 userDirectory = [[[[AIAdium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:userName] retain];
145 //Tell Adium to complete login
146 [target performSelector:selector];
149 // Switches users: logs out, provides user choosing dialog
152 // Log out previous user
153 [target applicationWillTerminate:nil];
156 loginWindowController = [[AILoginWindowController loginWindowControllerWithOwner:self] retain];
157 [loginWindowController showWindow:nil];
160 // Creates and returns a mutable array of the login users
161 - (NSArray *)userArray
164 NSArray *directoryContents;
165 NSMutableArray *userArray;
171 userPath = [[AIAdium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS];
173 //Build the user array
174 userArray = [[NSMutableArray alloc] init];
176 directoryContents = [[NSFileManager defaultManager] directoryContentsAtPath:userPath];
177 count = [directoryContents count];
178 for(loop = 0;loop < count;loop++){
179 NSString *path = [directoryContents objectAtIndex:loop];
181 //Fetch the names of all directories
182 if([[NSFileManager defaultManager] fileExistsAtPath:[userPath stringByAppendingPathComponent:path] isDirectory:&isDirectory]){
184 [userArray addObject:[path lastPathComponent]];
189 return([userArray autorelease]);
193 - (void)deleteUser:(NSString *)inUserName
195 NSString *sourcePath;
197 NSParameterAssert(inUserName != nil);
199 //Create the source and dest paths
200 sourcePath = [[[AIAdium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:inUserName];
201 [[NSFileManager defaultManager] trashFileAtPath:sourcePath];
204 // Add a user with the specified name
205 - (void)addUser:(NSString *)inUserName
209 NSParameterAssert(inUserName != nil);
211 //Create the user path
212 userPath = [[[AIAdium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:inUserName];
214 //Create a folder for the new user
215 [[NSFileManager defaultManager] createDirectoriesForPath:userPath];
218 // Rename an existing user
219 - (void)renameUser:(NSString *)oldName to:(NSString *)newName
221 NSString *sourcePath, *destPath;
223 NSParameterAssert(oldName != nil);
224 NSParameterAssert(newName != nil);
226 //Create the source and dest paths
227 sourcePath = [[[AIAdium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:oldName];
228 destPath = [[[AIAdium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:newName];
230 //Rename the user's folder (by moving it to a path with a different name)
231 [[NSFileManager defaultManager] movePath:sourcePath toPath:destPath handler:nil];