header, schmeader
[adiumx.git] / Source / AIGuestAccountWindowController.m
blob7cb1b0e700958d351827728e3ab44869fa3ec580
1 //
2 //  AIGuestAccountWindowController.m
3 //  Adium
4 //
5 //  Created by Evan Schoenberg on 4/9/06.
6 //
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;
20 @end
22 static AIGuestAccountWindowController *sharedGuestAccountWindowController = nil;
24 @implementation AIGuestAccountWindowController
25 + (void)showGuestAccountWindow
27         //Create the window
28         if (!sharedGuestAccountWindowController) {
29                 sharedGuestAccountWindowController = [[self alloc] initWithWindowNibName:@"GuestAccountWindow"];
30         }
32         [[sharedGuestAccountWindowController window] makeKeyAndOrderFront:nil];
35 - (NSString *)adiumFrameAutosaveName
37         return @"GuestAccountWindow";
40 - (void)awakeFromNib
42         [[self window] setTitle:AILocalizedString(@"Connect Guest Account",nil)];
45 - (void)windowDidLoad
47         [super windowDidLoad];
49         [popUp_service setMenu:[AIServiceMenu menuOfServicesWithTarget:self
50                                                                                                 activeServicesOnly:NO
51                                                                                                    longDescription:YES
52                                                                                                                         format:nil]];
53         [self selectServiceType:nil];
54         [label_password setLocalizedString:AILocalizedString(@"Password:", nil)];
55         [label_service setLocalizedString:AILocalizedString(@"Service:", nil)];
56         [button_okay setLocalizedString:AILocalizedString(@"Connect", nil)];
57         [button_cancel setLocalizedString:AILocalizedString(@"Cancel", nil)];
58         [button_advanced setLocalizedString:[AILocalizedString(@"Advanced", nil) stringByAppendingEllipsis]];
61 - (void)windowWillClose:(id)sender
63         [super windowWillClose:sender];
64         
65         [sharedGuestAccountWindowController autorelease]; sharedGuestAccountWindowController = nil;
68 - (AIService *)service
70         return [[popUp_service selectedItem] representedObject];
73 - (NSString *)UID
75         NSString *UID = [textField_name stringValue];
76         
77         //Use the default user name if possible, if no UID is specified
78         if (!UID || ![UID length]) UID = [[self service] defaultUserName];
80         return UID;
83 - (AIAccount *)account
85         if (!account) {
86                 account = [[[adium accountController] createAccountWithService:[self service]
87                                                                                                                                    UID:[self UID]] retain];
88         } else {
89                 if (([self service] != [account service]) ||
90                         (![[self UID] isEqualToString:[account UID]])) {
91                         [account release];
93                         account = [[adium accountController] createAccountWithService:[self service]
94                                                                                                                                           UID:[self UID]];
95                 }
96         }
97         
98         return account;
101 - (void)selectServiceType:(id)sender
103         AIService *service = [self service];
104         [label_name setStringValue:[[service userNameLabel] stringByAppendingString:AILocalizedString(@":", "Colon which will be appended after a label such as 'User Name', before an input field")]];
105         
106         [textField_name setFormatter:
107                 [AIStringFormatter stringFormatterAllowingCharacters:[service allowedCharactersForAccountName]
108                                                                                                           length:[service allowedLengthForAccountName]
109                                                                                            caseSensitive:[service caseSensitive]
110                                                                                                 errorMessage:AILocalizedString(@"The characters you're entering are not valid for an account name on this service.", nil)]];
111         
112         NSString *placeholder = [service defaultUserName];
113         if (!placeholder || ![placeholder length]) placeholder = [service UIDPlaceholder];
114         [[textField_name cell] setPlaceholderString:(placeholder ? placeholder : @"")];
117 - (IBAction)okay:(id)sender
119         AIAccount       *theAccount = [self account];
120         [theAccount setIsTemporary:YES];
121         
122         [[adium accountController] addAccount:theAccount];
123         [theAccount setPasswordTemporarily:[textField_password secureStringValue]];
125         //Connect the account
126         [theAccount setPreference:[NSNumber numberWithBool:YES] forKey:@"Online" group:GROUP_ACCOUNT_STATUS];
127         
128         [[self window] performClose:nil];
131 - (IBAction)displayAdvanced:(id)sender
133         [AIEditAccountWindowController editAccount:[self account]
134                                                                           onWindow:[self window]
135                                                            notifyingTarget:self];       
138 - (void)editAccountSheetDidEndForAccount:(AIAccount *)inAccount withSuccess:(BOOL)inSuccess
140         //If the AIEditAccountWindowController changes the account object, update to follow suit
141         if (inAccount != account) {
142                 [account release];
143                 account = [inAccount retain];
144         }
145         
146         //Make sure our UID is still accurate
147         if (![[inAccount UID] isEqualToString:[self UID]]) {
148                 [textField_name setStringValue:[inAccount UID]];
149         }
152 @end