2 * Adium is the legal property of its developers, whose names are listed in the copyright file included
3 * with this source distribution.
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.
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.
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.
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;
46 * @class AIEditAccountWindowController
47 * @brief Window controller for configuring an <tt>AIAccount</tt>
49 @implementation AIEditAccountWindowController
52 * @brief Begin editing
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.
58 + (void)editAccount:(AIAccount *)inAccount onWindow:(id)parentWindow notifyingTarget:(id)inTarget
60 AIEditAccountWindowController *controller;
62 controller = [[self alloc] initWithWindowNibName:@"EditAccountSheet"
64 notifyingTarget:inTarget];
67 [NSApp beginSheet:[controller window]
68 modalForWindow:parentWindow
69 modalDelegate:controller
70 didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
73 [controller showWindow:nil];
78 * @brief Init the window controller
80 - (id)initWithWindowNibName:(NSString *)windowNibName account:(AIAccount *)inAccount notifyingTarget:(id)inTarget
82 if ((self = [super initWithWindowNibName:windowNibName])) {
83 account = [inAccount retain];
84 notifyTarget = inTarget;
86 didDeleteUserIcon = NO;
97 [userIconData release]; userIconData = nil;
103 * @brief Setup the window before it is displayed
105 - (void)windowDidLoad
107 //Center our window if we're not a sheet (or opening a sheet failed)
108 [[self window] center];
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)];
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];
123 //Otherwise it is using the global icon
124 [matrix_userIcon selectCellWithTag:0];
127 [imageView_userIcon setImage:[account userIcon]];
129 //Insert the custom controls for this account
130 [self _removeCustomViewAndTabs];
131 [self _addCustomViewAndTabsForAccount:account];
132 [self _localizeTabViewItemLabels];
134 [self configureControlDimming];
138 * @brief Window is closing
140 - (void)windowWillClose:(id)sender
142 [super windowWillClose:sender];
147 * @brief Called as the user list edit sheet closes, dismisses the sheet
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);
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
165 - (IBAction)changedIconSetting:(id)sender
167 [self configureControlDimming];
173 * Close without saving changes.
175 - (IBAction)cancel:(id)sender
177 if (notifyTarget) [notifyTarget editAccountSheetDidEndForAccount:account withSuccess:NO];
178 [self closeWindow:nil];
184 * Save changes and close.
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.
201 - (void)saveConfiguration
203 BOOL enableUserIcon = ([[matrix_userIcon selectedCell] tag] == 1);
205 if (!enableUserIcon) {
206 [userIconData release]; userIconData = nil;
207 didDeleteUserIcon = YES;
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
215 group:GROUP_ACCOUNT_STATUS];
220 * @brief Add the custom views for an account
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] :
235 [accountProxyController configureForAccount:inAccount];
238 newHeight = [self _addCustomView:[accountViewController setupView]
239 toView:view_accountSetup
240 tabViewItemIdentifier:@"account"
241 availableHeight:newHeight];
243 //Account Profile View
244 newHeight = [self _addCustomView:[accountViewController profileView]
245 toView:view_accountProfile
246 tabViewItemIdentifier:@"profile"
247 availableHeight:newHeight];
249 //Account Options view
250 newHeight = [self _addCustomView:[accountViewController optionsView]
251 toView:view_accountOptions
252 tabViewItemIdentifier:@"options"
253 availableHeight:newHeight];
255 //Account Privacy view
256 newHeight = [self _addCustomView:[accountViewController privacyView]
257 toView:view_accountPrivacy
258 tabViewItemIdentifier:@"privacy"
259 availableHeight:newHeight];
262 newHeight = [self _addCustomView:[accountProxyController view]
263 toView:view_accountProxy
264 tabViewItemIdentifier:@"proxy"
265 availableHeight:newHeight];
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];
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
292 - (int)_addCustomView:(NSView *)customView toView:(NSView *)setupView tabViewItemIdentifier:(NSString *)identifier
293 availableHeight:(int)height
296 //Adjust height as necessary if our view needs more room
297 if ([customView frame].size.height > height) {
298 height = [customView frame].size.height;
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];
307 //If no view is available, remove the corresponding tab
308 [tabView_auxiliary removeTabViewItem:[tabView_auxiliary tabViewItemWithIdentifier:identifier]];
315 * @brief Remove any existing custom views
317 - (void)_removeCustomViewAndTabs
319 //Close any currently open controllers
320 [view_accountSetup removeAllSubviews];
321 [accountViewController release]; accountViewController = nil;
325 * @brief Localization
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]];
353 //We're now using the global icon
354 [matrix_userIcon selectCellWithTag:0];
357 - (NSString *)fileNameForImageInImagePicker:(AIImageViewWithImagePicker *)picker
359 return [[account displayName] safeFilenameString];