// // AccountSetupWindowController.m // Proteus // // Created by Julian Cain on 4/14/10. // Copyright 2010 __MyCompanyName__. All rights reserved. // #import "AccountSetupWindowController.h" #import "Proteus_AppDelegate.h" @implementation AccountSetupWindowController @synthesize accountsPopupButton = _accountsPopupButton; @synthesize usernameTextField = _usernameTextField; @synthesize passwordSecureTextField = _passwordSecureTextField; - (id)init { if (self = [super initWithWindowNibName:@"AccountSetup"]) { // ... } return self; } - (void)awakeFromNib { Proteus_AppDelegate * appDelegate = (Proteus_AppDelegate *)[NSApp delegate]; [self.accountsPopupButton removeAllItems]; /** * Load supported protocols. * plugin_info_name * plugin_info_id */ NSUInteger i = 0; for (NSDictionary * dict in [appDelegate supportedProtocols]) { NSString * pluginInfoName = [dict objectForKey:@"plugin_info_name"]; [self.accountsPopupButton insertItemWithTitle:pluginInfoName atIndex:i++ ]; } } - (IBAction)cancelAction:(id)sender { [self.window close]; } - (IBAction)doneAction:(id)sender { Proteus_AppDelegate * appDelegate = [NSApp delegate]; NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults]; NSMutableArray * accounts = [userDefaults mutableArrayValueForKey:@"accounts" ]; NSUInteger accountType = [self.accountsPopupButton indexOfSelectedItem]; NSString * username = [self.usernameTextField stringValue]; NSString * password = [self.passwordSecureTextField stringValue]; BOOL found = NO; for (NSDictionary * dict in accounts) { if ([[dict objectForKey:@"username"] isEqualToString:username]) { found = YES; break; } } if (found) { NSBeep(); return; } if (username.length && password.length) { PurpAccount * purpAccount = [appDelegate.purple enableAccountType:accountType username:username password:password ]; if (purpAccount) { /** * Add account to NSUserDefaults. */ NSMutableDictionary * account = [NSMutableDictionary dictionary]; /** * Set user default values for this account. */ [account setObject:[NSNumber numberWithInt:accountType] forKey:@"protocol" ]; [account setObject:username forKey:@"username"]; [account setObject:password forKey:@"password"]; if (accounts) { // ... } else { accounts = [NSMutableArray array]; } [accounts addObject:account]; [userDefaults synchronize]; [self.window close]; } else { NSAssert(0, @"Unable to allocate account."); } } else { NSBeep(); } } - (void)dealloc { [_accountsPopupButton release], _accountsPopupButton = nil; [_usernameTextField release], _usernameTextField = nil; [_passwordSecureTextField release], _passwordSecureTextField = nil; [super dealloc]; } @end