Use initialize, not load, wherever possible.
[adiumx.git] / Source / ESAuthorizationRequestWindowController.m
blobf6033fdb4538227b6d7c9be0f9cc9f716b8a8c2b
1 //
2 //  ESAuthorizationRequestWindowController.m
3 //  Adium
4 //
5 //  Created by Evan Schoenberg on 5/18/05.
6 //
8 #import "ESAuthorizationRequestWindowController.h"
9 #import <Adium/AIAccountControllerProtocol.h>
10 #import <Adium/AIContactControllerProtocol.h>
11 #import <Adium/AIAccount.h>
12 #import <AIUtilities/AIStringAdditions.h>
14 @interface ESAuthorizationRequestWindowController (PRIVATE)
15 - (id)initWithWindowNibName:(NSString *)windowNibName withDict:(NSDictionary *)inInfoDict forAccount:(AIAccount *)inAccount;
16 @end
18 @implementation ESAuthorizationRequestWindowController
20 + (ESAuthorizationRequestWindowController *)showAuthorizationRequestWithDict:(NSDictionary *)inInfoDict  forAccount:(AIAccount *)inAccount
22         ESAuthorizationRequestWindowController  *controller;
23         
24         if ((controller = [[self alloc] initWithWindowNibName:@"AuthorizationRequestWindow"
25                                                                                                  withDict:inInfoDict
26                                                                                            forAccount:inAccount])) {
27                 [controller showWindow:nil];
28                 [[controller window] makeKeyAndOrderFront:nil];
29         }
30         
31         return controller;
34 //Init
35 - (id)initWithWindowNibName:(NSString *)windowNibName withDict:(NSDictionary *)inInfoDict forAccount:(AIAccount *)inAccount
37     if ((self = [super initWithWindowNibName:windowNibName])) {
38                 infoDict = [inInfoDict retain];
39                 account = [inAccount retain];
40         }
41         
42     return self;
45 - (void)dealloc
47         [infoDict release]; infoDict = nil;
48         [account release];
50         [super dealloc];
53 - (void)windowDidLoad
54 {       
55         NSString        *message;
57         [super windowDidLoad];
59         [textField_header setStringValue:AILocalizedString(@"Authorization Requested",nil)];
60         [checkBox_addToList setLocalizedString:AILocalizedString(@"Add to my Contact List", "Checkbox in the Authorizatoin Request window to add the contact to the contact list if authorization is granted")]; 
61         [button_authorize setLocalizedString:AILocalizedString(@"Authorize", nil)];
62         [button_deny setLocalizedString:AILocalizedString(@"Deny", nil)];
64         // Hide the "Add to my Contact List" checkbox if the contact already exists in the list
65         AIListContact *contact = [[adium contactController] existingContactWithService:[account service] account:account UID:[infoDict objectForKey:@"Remote Name"]];
66         if (contact && [contact isIntentionallyNotAStranger]) {
67                 [checkBox_addToList setState:NSOffState];
68                 [checkBox_addToList setEnabled:NO];
69                 [checkBox_addToList setHidden:YES];
70         }
72         if ([infoDict objectForKey:@"Reason"]) {
73                 message = [NSString stringWithFormat:
74                         AILocalizedString(@"The contact %@ wants to add %@ to his or her contact list for the following reason:\n%@",nil),
75                         [infoDict objectForKey:@"Remote Name"],
76                         [account formattedUID],
77                         [infoDict objectForKey:@"Reason"]];
79         } else {
80                 message = [NSString stringWithFormat:
81                         AILocalizedString(@"The contact %@ wants to add %@ to his or her contact list.",nil),
82                         [infoDict objectForKey:@"Remote Name"],
83                         [account formattedUID]];
84         }
86         NSScrollView *scrollView_message = [textView_message enclosingScrollView];
87         
88         [textView_message setVerticallyResizable:YES];
89         [textView_message setHorizontallyResizable:NO];
90         [textView_message setDrawsBackground:NO];
91         [textView_message setTextContainerInset:NSZeroSize];
92         [scrollView_message setDrawsBackground:NO];
93         
94         [textView_message setString:(message ? message : @"")];
95         
96         //Resize the window frame to fit the error title
97         [textView_message sizeToFit];
98         float heightChange = [textView_message frame].size.height - [scrollView_message documentVisibleRect].size.height;
100         NSRect windowFrame = [[self window] frame];
101         windowFrame.size.height += heightChange;
102         windowFrame.origin.y -= heightChange;
103         [[self window] setFrame:windowFrame display:YES animate:NO];
104         
105         [[self window] center];
108 - (IBAction)authorize:(id)sender
110         //Do the authorization serverside
111         [account authorizationWindowController:self
112                                          authorizationWithDict:infoDict
113                                                           didAuthorize:YES];
114         
115         //Now handle the Add To Contact List checkbox
116         AILog(@"Authorize: (%i) %@",[checkBox_addToList state],infoDict);
118         if ([checkBox_addToList state] == NSOnState) {
119                 [[adium contactController] requestAddContactWithUID:[infoDict objectForKey:@"Remote Name"]
120                                                                                                         service:[account service]
121                                                                                                         account:account];
122         }
123         
124         [infoDict release]; infoDict = nil;
125         
126         [self closeWindow:nil];
129 - (IBAction)deny:(id)sender
131         [account authorizationWindowController:self
132                                          authorizationWithDict:infoDict
133                                                           didAuthorize:NO];     
135         [infoDict release]; infoDict = nil;
137         [self closeWindow:nil];
140 @end