French updates
[adiumx.git] / Source / AILoginController.m
blobcb563a55d2c0a089213caf12080d8220d71a8091
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
27 #define PATH_TRASH                      @"~/.Trash"             //Path to the trash
28 //Other
29 #define DEFAULT_USER_NAME               @"Default"              //The default user name
31 @implementation AILoginController
33 // Init this controller
34 - (void)initController
36     userDirectory = nil;
39 // Close this controller
40 - (void)closeController
45 // Dealloc
46 - (void)dealloc
48     [userDirectory release];
49     [currentUser release];
51     [super dealloc];
54 // Prompts for a user, or automatically selects one
55 - (void)requestUserNotifyingTarget:(id)inTarget selector:(SEL)inSelector
57     NSMutableDictionary *loginDict;
58         NSArray                         *arguments;
59         unsigned                        argumentIndex;
60         NSString                        *userName = nil;
61         
62     //Retain the target and selector
63     target = inTarget;
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];
77     }
78         
79         //Retrieve the desired user from the command line if possible
80         arguments = [[NSProcessInfo processInfo] arguments];
81         if(arguments && ([arguments count] > 1)){
82                 
83                 argumentIndex = [arguments indexOfObject:@"--user"];
84                 if((argumentIndex != NSNotFound) && ([arguments count] > argumentIndex + 1)){
85                         userName = [[[arguments objectAtIndex:argumentIndex+1] copy] autorelease];
86                 }
87         }
89     /*
90          If we don't have a userName yet, show the login select window if:
91                 - Option is held down
92                 - We should always show it
93                         or
94                 - LOGIN_LAST_USER does not indicate a valid user
95          */
96         if(!userName){
97                 BOOL userRequestedShowWindow = NO;
98                 
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];
106                         
107                         //If the user always wants to see the window, disable the login timeout
108                         if(userRequestedShowWindow) [loginWindowController disableLoginTimeout];
109                 }
110     }
112         
113         if(userName){
114                 [self loginAsUser:userName];
115         }
118 // Returns the current user's Adium home directory
119 - (NSString *)userDirectory
121     return(userDirectory);
125 - (NSString *)currentUser
127     return(currentUser);
130 // Sets the correct user directory and sends out a login message
131 - (void)loginAsUser:(NSString *)userName
133     NSParameterAssert(userName != nil);
134     
135     //Close the login panel
136     if(loginWindowController){
137         [loginWindowController closeWindow:nil];
138         [loginWindowController release]; loginWindowController = nil;
139     }
141     //Save the user directory
142     currentUser = [userName retain];
143     userDirectory = [[[[AIAdium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:userName] retain];
144     
145     //Tell Adium to complete login
146     [target performSelector:selector];
149 // Switches users: logs out, provides user choosing dialog
150 - (void)switchUsers
152     // Log out previous user
153     [target applicationWillTerminate:nil];
154     
155     // Open login panel
156     loginWindowController = [[AILoginWindowController loginWindowControllerWithOwner:self] retain];
157     [loginWindowController showWindow:nil];
160 // Creates and returns a mutable array of the login users
161 - (NSArray *)userArray
163     NSString            *userPath;
164     NSArray                     *directoryContents;
165     NSMutableArray      *userArray;
166     int                         loop;
167         unsigned                count;
168     BOOL                        isDirectory;
170     //Get the users path
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]){
183             if(isDirectory){
184                 [userArray addObject:[path lastPathComponent]];
185             }
186         }
187     }
189     return([userArray autorelease]);
192 // Delete a user
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
207     NSString    *userPath;
208     
209     NSParameterAssert(inUserName != nil);
211     //Create the user path
212     userPath = [[[AIAdium applicationSupportDirectory] stringByAppendingPathComponent:PATH_USERS] stringByAppendingPathComponent:inUserName];
213     
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];
234 @end