Automatically disable the Delete button in the Xtras Manager if no item is selected...
[adiumx.git] / Source / AIEditAccountWindowController.m
blob171d8e5e82c0a68db9aa5e1a27d68e45476c79b4
1 /*
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  *
5  * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6  * General Public License as published by the Free Software Foundation; either version 2 of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11  * Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along with this program; if not,
14  * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15  */
17 #import "AIAccountController.h"
18 #import "AIAccountProxySettings.h"
19 #import "AIContactController.h"
20 #import "AIPreferenceController.h"
21 #import "AIEditAccountWindowController.h"
22 #import <AIUtilities/AIStringAdditions.h>
23 #import <AIUtilities/AITabViewAdditions.h>
24 #import <AIUtilities/AIViewAdditions.h>
25 #import <AIUtilities/AIImageAdditions.h>
26 #import <AIUtilities/AIImageViewWithImagePicker.h>
27 #import <Adium/AIAccount.h>
28 #import <Adium/AIAccountViewController.h>
29 #import <Adium/AIService.h>
30 #import <Adium/AIService.h>
31 #import <Adium/AIServiceIcons.h>
33 @interface AIEditAccountWindowController (PRIVATE)
34 - (id)initWithWindowNibName:(NSString *)windowNibName account:(AIAccount *)inAccount notifyingTarget:(id)inTarget;
35 - (void)_addCustomViewAndTabsForAccount:(AIAccount *)inAccount;
36 - (int)_addCustomView:(NSView *)customView toView:(NSView *)setupView tabViewItemIdentifier:(NSString *)identifier
37           availableHeight:(int)height;
38 - (void)_configureResponderChain:(NSTimer *)inTimer;
39 - (void)_removeCustomViewAndTabs;
40 - (void)_localizeTabViewItemLabels;
41 - (void)saveConfiguration;
42 - (void)configureControlDimming;
43 @end
45 /*!
46  * @class AIEditAccountWindowController
47  * @brief Window controller for configuring an <tt>AIAccount</tt>
48  */
49 @implementation AIEditAccountWindowController
51 /*!
52  * @brief Begin editing
53  *
54  * @param inAccount The account to edit
55  * @param parentWindow A window on which to show the edit account window as a sheet.  If nil, account editing takes place in an independent window.
56  * @param notifyingTarget Target to notify when editing is complete.
57  */
58 + (void)editAccount:(AIAccount *)inAccount onWindow:(id)parentWindow notifyingTarget:(id)inTarget
60         AIEditAccountWindowController   *controller;
62         controller = [[self alloc] initWithWindowNibName:@"EditAccountSheet"
63                                                                                          account:inAccount
64                                                                          notifyingTarget:inTarget];
66         if (parentWindow) {
67                 [NSApp beginSheet:[controller window]
68                    modalForWindow:parentWindow
69                         modalDelegate:controller
70                    didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
71                           contextInfo:nil];
72         } else {
73                 [controller showWindow:nil];
74         }
77 /*!
78  * @brief Init the window controller
79  */
80 - (id)initWithWindowNibName:(NSString *)windowNibName account:(AIAccount *)inAccount notifyingTarget:(id)inTarget
82         if ((self = [super initWithWindowNibName:windowNibName])) {
83                 account = [inAccount retain];
84                 notifyTarget = inTarget;
85                 userIconData = nil;
86                 didDeleteUserIcon = NO;
87         }
88         return self;
91 /*!
92  * @brief Deallocate
93  */
94 - (void)dealloc
96         [account release];
97         [userIconData release]; userIconData = nil;
99         [super dealloc];
103  * @brief Setup the window before it is displayed
104  */
105 - (void)windowDidLoad
107         //Center our window if we're not a sheet (or opening a sheet failed)
108         [[self window] center];
110         //Account Overview
111         [textField_serviceName setStringValue:[[account service] longDescription]];
112         [textField_accountDescription setStringValue:[account UID]];
113         [button_chooseIcon setLocalizedString:[AILocalizedString(@"Choose Icon",nil) stringByAppendingEllipsis]];
114         [button_OK setLocalizedString:AILocalizedString(@"OK",nil)];
115         [button_cancel setLocalizedString:AILocalizedString(@"Cancel",nil)];
117         //User icon
118         if ([account preferenceForKey:KEY_USER_ICON group:GROUP_ACCOUNT_STATUS ignoreInheritedValues:YES]) {
119                 //If this account has a icon set directly on it, then it has its own icon
120                 [matrix_userIcon selectCellWithTag:1];
122         } else {
123                 //Otherwise it is using the global icon
124                 [matrix_userIcon selectCellWithTag:0];
125         }
127         [imageView_userIcon setImage:[account userIcon]];
129         //Insert the custom controls for this account
130         [self _removeCustomViewAndTabs];
131         [self _addCustomViewAndTabsForAccount:account];
132         [self _localizeTabViewItemLabels];
133         
134         [self configureControlDimming];
138  * @brief Window is closing
139  */
140 - (void)windowWillClose:(id)sender
142         [super windowWillClose:sender];
143         [self autorelease];
147  * @brief Called as the user list edit sheet closes, dismisses the sheet
148  */
149 - (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
151     [sheet orderOut:nil];
154 - (void)configureControlDimming
156         BOOL enableUserIcon = ([[matrix_userIcon selectedCell] tag] == 1);
157         
158         [imageView_userIcon setEnabled:enableUserIcon];
159         [button_chooseIcon setEnabled:enableUserIcon];
163  * @brief The user changed the selection in the icon setting matrix which determines availability of the icon controls
164  */
165 - (IBAction)changedIconSetting:(id)sender
167         [self configureControlDimming];
171  * @brief Cancel
173  * Close without saving changes.
174  */
175 - (IBAction)cancel:(id)sender
177         if (notifyTarget) [notifyTarget editAccountSheetDidEndForAccount:account withSuccess:NO];
178         [self closeWindow:nil];
182  * @brief Okay.
184  * Save changes and close.
185  */
186 - (IBAction)okay:(id)sender
188         [self saveConfiguration];
189         [accountViewController saveConfiguration];
190         [accountProxyController saveConfiguration];
192         if (notifyTarget) [notifyTarget editAccountSheetDidEndForAccount:account withSuccess:YES];
193         [self closeWindow:nil];
197  * @brief Save any configuration managed by the window controller
199  * Most configuration is handled by the custom view controllers.  Save any other configuration, such as the user icon.
200  */
201 - (void)saveConfiguration
203         BOOL enableUserIcon = ([[matrix_userIcon selectedCell] tag] == 1);
205         if (!enableUserIcon) {
206                 [userIconData release]; userIconData = nil;
207                 didDeleteUserIcon = YES;
208         }
210         /* User icon - save if we have data or we deleted
211          * (so if we don't have data that's the desired thing to set as the pref) */
212         if (userIconData || didDeleteUserIcon) {
213                 [account setPreference:userIconData
214                                                 forKey:KEY_USER_ICON
215                                                  group:GROUP_ACCOUNT_STATUS];
216         }       
220  * @brief Add the custom views for an account
221  */
222 - (void)_addCustomViewAndTabsForAccount:(AIAccount *)inAccount
224         NSRect  windowFrame = [[self window] frame];
225         int             baseHeight = [view_accountSetup frame].size.height;
226         int             newHeight = baseHeight;
228         //Configure our account and proxy view controllers
229         accountViewController = [[[inAccount service] accountViewController] retain];
230         [accountViewController configureForAccount:inAccount];
232         accountProxyController = ([[inAccount service] supportsProxySettings] ?
233                                                           [[AIAccountProxySettings alloc] init] :
234                                                           nil);
235         [accountProxyController configureForAccount:inAccount];
237         //Account setup view
238         newHeight = [self _addCustomView:[accountViewController setupView]
239                                                    toView:view_accountSetup
240                         tabViewItemIdentifier:@"account"
241                                   availableHeight:newHeight];
242         
243         //Account Profile View
244         newHeight = [self _addCustomView:[accountViewController profileView]
245                                                    toView:view_accountProfile
246                         tabViewItemIdentifier:@"profile"
247                                   availableHeight:newHeight];
248         
249         //Account Options view
250         newHeight = [self _addCustomView:[accountViewController optionsView]
251                                                    toView:view_accountOptions
252                         tabViewItemIdentifier:@"options"
253                                   availableHeight:newHeight];
254         
255         //Account Privacy view
256         newHeight = [self _addCustomView:[accountViewController privacyView]
257                                                    toView:view_accountPrivacy
258                         tabViewItemIdentifier:@"privacy"
259                                   availableHeight:newHeight];
260         
261         //Add proxy view
262         newHeight = [self _addCustomView:[accountProxyController view]
263                                                    toView:view_accountProxy
264                         tabViewItemIdentifier:@"proxy"
265                                   availableHeight:newHeight];
266         
267         //Resize our window as necessary to make room for the custom views
268         windowFrame.size.height += newHeight - [view_accountSetup frame].size.height;
269         [[self window] setFrame:windowFrame display:YES];
270         
271         //Responder chains are a pain in 10.3.  The tab view will set them up correctly when we switch tabs, but doesn't
272         //get a chance to setup the responder chain for our default tab.  A quick hack to get the tab view to set things
273         //up correctly is to switch tabs away and then back to our default.  This causes little harm, since our window
274         //isn't visible at this point anyway.
275         //XXX - I believe we're getting a method that will avoid the need for this hack in 10.4 -ai
276         [tabView_auxiliary selectLastTabViewItem:nil];
277         [tabView_auxiliary selectFirstTabViewItem:nil];
281  * @brief Used when configuring to add custom views and remove tabs as necessary
283  * Add customView to setupView and return the height difference between the two if customView is taller than setupView.
284  * Remove the tabViewItem with the passed identifier if no customView exists, avoiding empty tabs.
286  * @param customView The view to add
287  * @param setupView The view within our nib which will be filled by customView
288  * @param identifier Identifier of the <tt>NSTabViewItem</tt> which will be removed from tabView_auxiliary if customView == nil
289  * @param requiredHeight The current required view height to display all our views
290  * @result The new required window height to display our existing views and the newly added view
291  */
292 - (int)_addCustomView:(NSView *)customView toView:(NSView *)setupView tabViewItemIdentifier:(NSString *)identifier
293           availableHeight:(int)height
295         if (customView) {
296                 //Adjust height as necessary if our view needs more room
297                 if ([customView frame].size.height > height) {
298                         height = [customView frame].size.height;
299                 }
301                 //Align our view to the top and insert it into the window
302                 [customView setFrameOrigin:NSMakePoint(0, [setupView frame].size.height - [customView frame].size.height)];
303                 [customView setAutoresizingMask:NSViewMinYMargin];
304                 [setupView addSubview:customView];
306         } else {
307                 //If no view is available, remove the corresponding tab
308                 [tabView_auxiliary removeTabViewItem:[tabView_auxiliary tabViewItemWithIdentifier:identifier]];
309         }
311         return height;
315  * @brief Remove any existing custom views
316  */
317 - (void)_removeCustomViewAndTabs
319     //Close any currently open controllers
320     [view_accountSetup removeAllSubviews];
321     [accountViewController release]; accountViewController = nil;
325  * @brief Localization
326  */
327 - (void)_localizeTabViewItemLabels
329         [[tabView_auxiliary tabViewItemWithIdentifier:@"account"] setLabel:AILocalizedString(@"Account",nil)];
330         [[tabView_auxiliary tabViewItemWithIdentifier:@"profile"] setLabel:AILocalizedString(@"Personal",nil)];
331         [[tabView_auxiliary tabViewItemWithIdentifier:@"options"] setLabel:AILocalizedString(@"Options",nil)];
332         [[tabView_auxiliary tabViewItemWithIdentifier:@"privacy"] setLabel:AILocalizedString(@"Privacy",nil)];
333         [[tabView_auxiliary tabViewItemWithIdentifier:@"proxy"] setLabel:AILocalizedString(@"Proxy",nil)];
337 // AIImageViewWithImagePicker Delegate ---------------------------------------------------------------------
338 #pragma mark AIImageViewWithImagePicker Delegate
339 - (void)imageViewWithImagePicker:(AIImageViewWithImagePicker *)sender didChangeToImageData:(NSData *)imageData
341         [userIconData release];
342         userIconData = [imageData retain];
345 - (void)deleteInImageViewWithImagePicker:(AIImageViewWithImagePicker *)sender
347         [userIconData release]; userIconData = nil;
348         didDeleteUserIcon = YES;
350         //User icon - restore to the default icon
351         [imageView_userIcon setImage:[account userIcon]];
352         
353         //We're now using the global icon
354         [matrix_userIcon selectCellWithTag:0];
357 - (NSString *)fileNameForImageInImagePicker:(AIImageViewWithImagePicker *)picker
359         return [[account displayName] safeFilenameString];
362 @end