// // AccountPreferencesController.m // Proteus // // Created by Julian Cain on 5/17/10. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import "AccountPreferencesController.h" @implementation AccountPreferencesController @synthesize accountsTableView = _accountsTableView; - (void)awakeFromNib { [_accountsTableView reloadData]; } - (int)numberOfRowsInTableView:(NSTableView *)aTableView { Proteus_AppDelegate * appDelegate = [NSApp delegate]; return appDelegate.accounts.count; } - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(int)rowIndex { Proteus_AppDelegate * appDelegate = [NSApp delegate]; return [[appDelegate.accounts objectAtIndex:rowIndex] objectForKey:@"username" ]; } - (IBAction)removeAccount:(id)sender { Proteus_AppDelegate * appDelegate = [NSApp delegate]; if ([[self.accountsTableView selectedRowIndexes] count]) { NSInteger rowIndex = [[self.accountsTableView selectedRowIndexes] firstIndex ]; NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; NSMutableArray * accounts = [userDefaults objectForKey:@"accounts"]; if (rowIndex != -1 && accounts.count) { NSString * username = [[accounts objectAtIndex:rowIndex] objectForKey:@"username" ]; [appDelegate.purple disableAccount:username]; /** * Remove object from NSUserDefault's accounts. */ for (NSDictionary * dict1 in accounts) { if ([[dict1 objectForKey:@"username"] isEqualToString:username]) { [accounts removeObject:dict1]; break; } } /** * Remove object from in memory accounts. */ for (NSDictionary * dict2 in appDelegate.accounts) { if ([[dict2 objectForKey:@"username"] isEqualToString:username]) { [appDelegate.accounts removeObject:dict2]; break; } } [userDefaults setObject:accounts forKey:@"accounts"]; [userDefaults synchronize]; [self.accountsTableView reloadData]; } } } - (IBAction)toggleAccount:(id)sender { NSLog(@":TODO: Toggle selected account, %d.", [sender state]); Proteus_AppDelegate * appDelegate = [NSApp delegate]; if ([[self.accountsTableView selectedRowIndexes] count]) { NSInteger rowIndex = [[self.accountsTableView selectedRowIndexes] firstIndex ]; NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; NSMutableArray * accounts = [userDefaults objectForKey:@"accounts"]; if (rowIndex != -1 && accounts.count) { NSString * username = [[accounts objectAtIndex:rowIndex] objectForKey:@"username" ]; if ([sender state] == NSOnState) { NSUInteger aType = [[[accounts objectAtIndex:rowIndex] objectForKey:@"protocol"] unsignedIntValue ]; NSString * password = [[accounts objectAtIndex:rowIndex] objectForKey:@"password" ]; [appDelegate.purple enableAccountType:aType username:username password:password ]; } else if ([sender state] == NSOffState) { [appDelegate.purple disableAccount:username]; } } } } - (void)dealloc { [_accountsTableView release], _accountsTableView = nil; [super dealloc]; } @end