Put NSAutoreleasePool usage around other distributed notification observer methods
[adiumx.git] / Source / AILoginWindowController.m
blob8a66973c1d0cdc54cb93af5744c8ea1043f231ff
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 "AILoginWindowController.h"
20 #import <Adium/AILoginControllerProtocol.h>
21 #import <AIUtilities/AIDictionaryAdditions.h>
23 //Preference Keys
24 #define NEW_USER_NAME           @"New User"             //Default name of a new user
25 #define LOGIN_WINDOW_NIB        @"LoginSelect"          //Filename of the login window nib
27 #define LOGIN_TIMEOUT           10.0
29 @interface AILoginWindowController (PRIVATE)
30 - (id)initWithOwner:(id)inOwner windowNibName:(NSString *)windowNibName;
31 - (void)dealloc;
32 - (int)numberOfRowsInTableView:(NSTableView *)tableView;
33 - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row;
34 - (IBAction)login:(id)sender;
35 - (IBAction)editUsers:(id)sender;
36 - (IBAction)doneEditing:(id)sender;
37 - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo;
38 - (void)updateUserList;
39 - (IBAction)newUser:(id)sender;
40 - (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row;
41 - (IBAction)deleteUser:(id)sender;
42 - (void)windowDidLoad;
43 - (void)disableLoginTimeout;
44 @end
46 @implementation AILoginWindowController
47 // return an instance of AILoginController
48 + (AILoginWindowController *)loginWindowControllerWithOwner:(id)inOwner
50     return [[[self alloc] initWithOwner:inOwner windowNibName:LOGIN_WINDOW_NIB] autorelease];
54 // Internal --------------------------------------------------------------------------------
55 // init the login controller
56 - (id)initWithOwner:(id)inOwner windowNibName:(NSString *)windowNibName
58         if ((self = [super initWithWindowNibName:windowNibName])) {
59                 //Retain our owner
60                 owner = [inOwner retain];
62                 //Get the user list
63                 [self updateUserList];
64         }
65         return self;
68 // deallocate the login controller
69 - (void)dealloc
71     [owner release]; owner = nil;
72     [userArray release]; userArray = nil;
74     [super dealloc];
77 // TableView Delegate methods - Return the number of items in the table
78 - (int)numberOfRowsInTableView:(NSTableView *)tableView
80     if (tableView == tableView_userList) {
81         return [userArray count];
82     } else if (tableView == tableView_editableUserList) {
83         return [userArray count];
84     } else {
85         return 0;
86     }
89 // TableView Delegate methods - Return the requested item in the table
90 - (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(int)row
92     if (tableView == tableView_userList) {
93         return [userArray objectAtIndex:row];
94     } else if (tableView == tableView_editableUserList) {
95         return [userArray objectAtIndex:row];
96     } else {
97         return nil;
98     }
102 // Log in with the selected user
103 - (IBAction)login:(id)sender
105     NSMutableDictionary *loginDict;
106     NSString            *selectedUserName = [userArray objectAtIndex:[tableView_userList selectedRow]];
108     //Open the login preferences
109     loginDict = [NSMutableDictionary dictionaryAtPath:[adium applicationSupportDirectory]
110                                          withName:LOGIN_PREFERENCES_FILE_NAME
111                                            create:YES];
113     //Save the 'display on launch' checkbox state
114     [loginDict setObject:[NSNumber numberWithBool:[checkbox_displayOnStartup state]] forKey:LOGIN_SHOW_WINDOW];
116     //Save the login they used
117     [loginDict setObject:selectedUserName forKey:LOGIN_LAST_USER];
119     //Save the login preferences
120     [loginDict writeToPath:[adium applicationSupportDirectory]
121                            withName:LOGIN_PREFERENCES_FILE_NAME];
123     //Login
124     [owner loginAsUser:selectedUserName];
127 // Display the user list edit sheet
128 - (IBAction)editUsers:(id)sender
130         [self disableLoginTimeout];
132     [NSApp beginSheet:panel_userListEditor modalForWindow:[self window] modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:nil];
135 // Close the user list edit sheet
136 - (IBAction)doneEditing:(id)sender
138     [NSApp endSheet:panel_userListEditor];
141 // Called as the user list edit sheet closes, dismisses the sheet
142 - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
144     [sheet orderOut:nil];
147 //Update/Refresh our user list and outline views
148 - (void)updateUserList
150     //Update the reference
151     [userArray release]; userArray = nil;
152     userArray = [[owner userArray] retain];
154         [tableView_editableUserList reloadData];
155         [tableView_userList reloadData];
158 // Add a new user
159 - (IBAction)newUser:(id)sender
161     int         newRow;
163     //Force the table view to end editing
164     [tableView_editableUserList reloadData];
166     //Add a new user
167     [owner addUser:NEW_USER_NAME];
169     //Refresh our user list and outline views
170     [self updateUserList];
172     //Select, scroll to, and 'edit' the new user
173     newRow = [userArray indexOfObject:NEW_USER_NAME];
174     [tableView_editableUserList selectRow:newRow byExtendingSelection:NO];
175     [tableView_editableUserList scrollRowToVisible:newRow];
176     [tableView_editableUserList editColumn:0 row:newRow withEvent:nil select:YES];
178         [self disableLoginTimeout];
181 // Rename a user
182 - (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(int)row
184     if (tableView == tableView_editableUserList) {
185         //Rename the user
186         [owner renameUser:[userArray objectAtIndex:row] to:object];
188         //Refresh our user list
189         [self updateUserList];
191                 if (loginTimer) {
192                         [loginTimer invalidate]; [loginTimer release]; loginTimer = nil;
193                 }
194     }
197 - (void)tableViewSelectionDidChange:(NSNotification *)inNotification
199         [self disableLoginTimeout];
202 // Delete the selected user
203 - (IBAction)deleteUser:(id)sender
205     //Force the table view to end editing
206     [tableView_editableUserList reloadData];
208     //Delete the user
209     [owner deleteUser:[userArray objectAtIndex:[tableView_editableUserList selectedRow]]];
211     //Refresh our user list
212     [self updateUserList];
214         [self disableLoginTimeout];
217 // set up the window before it is displayed
218 - (void)windowDidLoad
220     NSDictionary        *loginDict;
221     NSString            *lastLogin;
223     //Open the login preferences
224     loginDict = [NSDictionary dictionaryAtPath:[adium applicationSupportDirectory]
225                                          withName:LOGIN_PREFERENCES_FILE_NAME
226                                            create:YES];
228     //Center the window
229     [[self window] center];
231     //Setup the 'display on launch' checkbox
232     [checkbox_displayOnStartup setState:[[loginDict objectForKey:LOGIN_SHOW_WINDOW] boolValue]];
234     //Select the login they used last
235     lastLogin = [loginDict objectForKey:LOGIN_LAST_USER];
236     if (lastLogin != nil && [lastLogin length] != 0 && [userArray indexOfObject:lastLogin] != NSNotFound) {
237         [tableView_userList selectRow:[userArray indexOfObject:lastLogin] byExtendingSelection:NO];
238     } else {
239         [tableView_userList selectRow:0 byExtendingSelection:NO];
240     }
242     //Set login so it's called when the user double clicks a name
243     [tableView_userList setDoubleAction:@selector(login:)];
245         loginTimer = [[NSTimer scheduledTimerWithTimeInterval:LOGIN_TIMEOUT
246                                                                                                    target:self
247                                                                                                  selector:@selector(login:)
248                                                                                                  userInfo:nil
249                                                                                                   repeats:NO] retain];
251         [tableView_userList setDelegate:self];
252         [tableView_userList setDataSource:self];
253         
254         [self updateUserList];
258 // called as the window closes
259 - (void)windowWillClose:(id)sender
261         [super windowWillClose:sender];
262         [loginTimer invalidate]; [loginTimer release]; loginTimer = nil;
265 - (void)disableLoginTimeout
267         if (loginTimer) {
268                 [loginTimer invalidate]; [loginTimer release]; loginTimer = nil;
269         }
272 @end