Updating svn:mergeinfo
[adiumx.git] / Source / AILoginController.m
blob0e3f1a9291162bba9d7fa26e1596caa49060eb16
1 /* 
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
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.
8  * 
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.
12  * 
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.
15  */
17 // $Id$
19 #import "AILoginController.h"
20 #import "AILoginWindowController.h"
21 #import <AIUtilities/AIDictionaryAdditions.h>
22 #import <AIUtilities/AIFileManagerAdditions.h>
23 #import <AIUtilities/AIEventAdditions.h>
25 //Paths & Filenames
26 #define PATH_USERS                      @"/Users"               //Path of the users folder
28 //Other
29 #define DEFAULT_USER_NAME               @"Default"              //The default user name
31 @implementation AILoginController
33 // Init this controller
34 - (id)init
36         if ((self = [super init])) { 
37                 userDirectory = nil;
38         }
39         
40         return self;
43 - (void)controllerDidLoad
47 // Close this controller
48 - (void)controllerWillClose
53 // Dealloc
54 - (void)dealloc
56     [userDirectory release];
57     [currentUser release];
59     [super dealloc];
62 // Prompts for a user, or automatically selects one
63 - (void)requestUserNotifyingTarget:(id)inTarget selector:(SEL)inSelector
65     NSMutableDictionary *loginDict;
66         NSArray                         *arguments;
67         unsigned                        argumentIndex;
68         NSString                        *userName = nil;
69         
70     //Retain the target and selector
71     target = inTarget;
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];
85     }
86         
87         //Retrieve the desired user from the command line if possible
88         arguments = [[NSProcessInfo processInfo] arguments];
89         if (arguments && ([arguments count] > 1)) {
90                 
91                 argumentIndex = [arguments indexOfObject:@"--user"];
92                 if ((argumentIndex != NSNotFound) && ([arguments count] > argumentIndex + 1)) {
93                         userName = [[[arguments objectAtIndex:argumentIndex+1] copy] autorelease];
94                 }
95         }
97     /*
98          If we don't have a userName yet, show the login select window if:
99                 - Option is held down
100                 - We should always show it
101                         or
102                 - LOGIN_LAST_USER does not indicate a valid user
103          */
104         if (!userName) {
105                 BOOL userRequestedShowWindow = NO;
106                 
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];
114                         
115                         //If the user always wants to see the window, disable the login timeout
116                         if (userRequestedShowWindow) [loginWindowController disableLoginTimeout];
117                 }
118     }
120         
121         if (userName) {
122                 [self loginAsUser:userName];
123         }
126 // Returns the current user's Adium home directory
127 - (NSString *)userDirectory
129     return userDirectory;
133 - (NSString *)currentUser
135     return currentUser;
138 // Sets the correct user directory and sends out a login message
139 - (void)loginAsUser:(NSString *)userName
141     NSParameterAssert(userName != nil);
142     
143     //Close the login panel
144     if (loginWindowController) {
145         [loginWindowController closeWindow:nil];
146         [loginWindowController release]; loginWindowController = nil;
147     }
149     //Save the user directory
150     currentUser = [userName retain];
151     userDirectory = [[[[adium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:userName] retain];
152     
153     //Tell Adium to complete login
154     [target performSelector:selector];
157 // Creates and returns a mutable array of the login users
158 - (NSArray *)userArray
160     NSString            *userPath;
161     NSArray                     *directoryContents;
162     NSMutableArray      *userArray;
163     int                         loop;
164         unsigned                count;
165     BOOL                        isDirectory;
167     //Get the users path
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]) {
180             if (isDirectory) {
181                 [userArray addObject:[path lastPathComponent]];
182             }
183         }
184     }
186     return [userArray autorelease];
189 // Delete a user
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
204     NSString    *userPath;
205     
206     NSParameterAssert(inUserName != nil);
208     //Create the user path
209     userPath = [[[adium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:inUserName];
210     
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];
231 @end