2 // AIGuestAccountWindowController.m
5 // Created by Evan Schoenberg on 4/9/06.
8 #import "AIGuestAccountWindowController.h"
9 #import "AIEditAccountWindowController.h"
10 #import <Adium/AIAccountControllerProtocol.h>
11 #import <Adium/AIAccount.h>
12 #import <Adium/AIService.h>
13 #import "AIServiceMenu.h"
14 #import <AIUtilities/AIStringAdditions.h>
15 #import <AIUtilities/AIStringFormatter.h>
16 #import <AIUtilities/AITextFieldAdditions.h>
18 @interface AIGuestAccountWindowController (PRIVATE)
19 - (void)selectServiceType:(id)sender;
22 static AIGuestAccountWindowController *sharedGuestAccountWindowController = nil;
24 @implementation AIGuestAccountWindowController
25 + (void)showGuestAccountWindow
28 if (!sharedGuestAccountWindowController) {
29 sharedGuestAccountWindowController = [[self alloc] initWithWindowNibName:@"GuestAccountWindow"];
32 [[sharedGuestAccountWindowController window] makeKeyAndOrderFront:nil];
35 - (NSString *)adiumFrameAutosaveName
37 return @"GuestAccountWindow";
42 [[self window] setTitle:AILocalizedString(@"Connect Guest Account", "Title for the window shown when adding a guest (temporary) account")];
54 [super windowDidLoad];
56 [popUp_service setMenu:[AIServiceMenu menuOfServicesWithTarget:self
60 [self selectServiceType:nil];
61 [label_password setLocalizedString:AILocalizedString(@"Password:", nil)];
62 [label_service setLocalizedString:AILocalizedString(@"Service:", nil)];
63 [button_okay setLocalizedString:AILocalizedString(@"Connect", nil)];
64 [button_cancel setLocalizedString:AILocalizedString(@"Cancel", nil)];
65 [button_advanced setLocalizedString:[AILocalizedString(@"Advanced", nil) stringByAppendingEllipsis]];
68 - (void)windowWillClose:(id)sender
70 [super windowWillClose:sender];
72 [sharedGuestAccountWindowController autorelease]; sharedGuestAccountWindowController = nil;
75 - (AIService *)service
77 return [[popUp_service selectedItem] representedObject];
82 NSString *UID = [textField_name stringValue];
84 //Use the default user name if possible, if no UID is specified
85 if (!UID || ![UID length]) UID = [[self service] defaultUserName];
90 - (AIAccount *)account
93 account = [[[adium accountController] createAccountWithService:[self service]
94 UID:[self UID]] retain];
96 if (([self service] != [account service]) ||
97 (![[self UID] isEqualToString:[account UID]])) {
100 account = [[[adium accountController] createAccountWithService:[self service]
101 UID:[self UID]] retain];
108 - (void)selectServiceType:(id)sender
110 AIService *service = [self service];
111 [label_name setStringValue:[[service userNameLabel] stringByAppendingString:AILocalizedString(@":", "Colon which will be appended after a label such as 'User Name', before an input field")]];
113 [textField_name setFormatter:
114 [AIStringFormatter stringFormatterAllowingCharacters:[service allowedCharactersForAccountName]
115 length:[service allowedLengthForAccountName]
116 caseSensitive:[service caseSensitive]
117 errorMessage:AILocalizedString(@"The characters you're entering are not valid for an account name on this service.", nil)]];
119 NSString *placeholder = [service defaultUserName];
120 if (!placeholder || ![placeholder length]) placeholder = [service UIDPlaceholder];
121 [[textField_name cell] setPlaceholderString:(placeholder ? placeholder : @"")];
124 - (IBAction)okay:(id)sender
126 AIAccount *theAccount = [self account];
127 [theAccount setIsTemporary:YES];
129 [[adium accountController] addAccount:theAccount];
130 [theAccount setPasswordTemporarily:[textField_password secureStringValue]];
132 //Connect the account
133 [theAccount setPreference:[NSNumber numberWithBool:YES] forKey:@"Online" group:GROUP_ACCOUNT_STATUS];
135 [[self window] performClose:nil];
138 - (IBAction)displayAdvanced:(id)sender
140 [AIEditAccountWindowController editAccount:[self account]
141 onWindow:[self window]
142 notifyingTarget:self];
145 - (void)editAccountSheetDidEndForAccount:(AIAccount *)inAccount withSuccess:(BOOL)inSuccess
147 //If the AIEditAccountWindowController changes the account object, update to follow suit
148 if (inAccount != account) {
150 account = [inAccount retain];
153 //Make sure our UID is still accurate
154 if (![[inAccount UID] isEqualToString:[self UID]]) {
155 [textField_name setStringValue:[inAccount UID]];