header, schmeader
[adiumx.git] / Source / ESPersonalPreferences.m
blob251c0f0cbfd6d4a1e96ba62ac9c966ecd67e215e
1 //
2 //  ESPersonalPreferences.m
3 //  Adium
4 //
5 //  Created by Evan Schoenberg on 12/18/05.
6 //
8 #import "ESPersonalPreferences.h"
9 #import <Adium/AIPreferenceControllerProtocol.h>
10 #import <Adium/AIContactControllerProtocol.h>
11 #import <Adium/AIAccount.h>
12 #import <AIUtilities/AIAttributedStringAdditions.h>
13 #import <AIUtilities/AIImageViewWithImagePicker.h>
14 #import <AIUtilities/AIAutoScrollView.h>
15 #import <AIUtilities/AIImageViewWithImagePicker.h>
16 #import <AIUtilities/AIDelayedTextField.h>
17 #import <Adium/AIMessageEntryTextView.h>
19 @interface ESPersonalPreferences (PRIVATE)
20 - (void)fireProfileChangesImmediately;
21 - (void)configureProfile;
22 - (void)configureImageView;
23 - (void)configureTooltips;
24 @end
26 @implementation ESPersonalPreferences
28 /*!
29  * @brief Preference pane properties
30  */
31 - (AIPreferenceCategory)category{
32     return AIPref_Personal;
34 - (NSString *)label{
35     return AILocalizedString(@"Personal","Personal preferences label");
37 - (NSString *)nibName{
38     return @"PersonalPreferences";
41 /*!
42  * @brief Configure the view initially
43  */
44 - (void)viewDidLoad
46         NSString *displayName = [[[[adium preferenceController] preferenceForKey:KEY_ACCOUNT_DISPLAY_NAME
47                                                                                                                                            group:GROUP_ACCOUNT_STATUS] attributedString] string];
48         [textField_displayName setStringValue:(displayName ? displayName : @"")];
49         
50         //Set the default local alias (address book name) as the placeholder for the local alias
51         NSString *defaultAlias = [[[[adium preferenceController] defaultPreferenceForKey:KEY_ACCOUNT_DISPLAY_NAME
52                                                                                                                                                            group:GROUP_ACCOUNT_STATUS
53                                                                                                                                                           object:nil] attributedString] string];
54         [[textField_displayName cell] setPlaceholderString:(defaultAlias ? defaultAlias : @"")];
56         [self configureProfile];
57         [self configureImageView];
58         [self configureTooltips];
59         
60         if ([[[adium preferenceController] preferenceForKey:KEY_USE_USER_ICON
61                                                                                                   group:GROUP_ACCOUNT_STATUS] boolValue]) {
62                 [matrix_userIcon selectCellWithTag:1];
63         } else {
64                 [matrix_userIcon selectCellWithTag:0];          
65         }
67         [self configureControlDimming];
69         [[adium preferenceController] registerPreferenceObserver:self forGroup:GROUP_ACCOUNT_STATUS];
71         [super viewDidLoad];
74 - (void)viewWillClose
76         [[adium preferenceController] unregisterPreferenceObserver:self];
77         [self fireProfileChangesImmediately];
79         [[NSFontPanel sharedFontPanel] setDelegate:nil];
81         [super viewWillClose];
84 - (void)changePreference:(id)sender
85 {       
86         if (sender == textField_displayName) {
87                 NSString *displayName = [textField_displayName stringValue];
88                 
89                 [[adium preferenceController] setPreference:((displayName && [displayName length]) ?
90                                                                                                          [[NSAttributedString stringWithString:displayName] dataRepresentation] :
91                                                                                                          nil)
92                                                                                          forKey:KEY_ACCOUNT_DISPLAY_NAME
93                                                                                           group:GROUP_ACCOUNT_STATUS];
95         } else if (sender == textView_profile) {
96                 [[adium preferenceController] setPreference:[[textView_profile textStorage] dataRepresentation] 
97                                                                                          forKey:@"TextProfile"
98                                                                                           group:GROUP_ACCOUNT_STATUS];
100         } else if (sender == matrix_userIcon) {
101                 BOOL enableUserIcon = ([[matrix_userIcon selectedCell] tag] == 1);
103                 [[adium preferenceController] setPreference:[NSNumber numberWithBool:enableUserIcon]
104                                                                                          forKey:KEY_USE_USER_ICON
105                                                                                           group:GROUP_ACCOUNT_STATUS];  
106         }else if (sender == button_enableMusicProfile) {
107                 BOOL enableUserIcon = ([button_enableMusicProfile state] == NSOnState);
108                 
109                 [[adium preferenceController] setPreference:[NSNumber numberWithBool:enableUserIcon]
110                                                                                          forKey:KEY_USE_USER_ICON
111                                                                                           group:GROUP_ACCOUNT_STATUS];  
112         }
113         
114         
115         [super changePreference:nil];
118 - (void)configureControlDimming
120         BOOL enableUserIcon = ([[matrix_userIcon selectedCell] tag] == 1);
122         [button_chooseIcon setEnabled:enableUserIcon];
123         [imageView_userIcon setEnabled:enableUserIcon]; 
126 - (void)preferencesChangedForGroup:(NSString *)group key:(NSString *)key
127                                                         object:(AIListObject *)object preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime
129         if (object) return;
130         
131         if ([key isEqualToString:KEY_ACCOUNT_DISPLAY_NAME]) {
132                 NSString *displayName = [textField_displayName stringValue];
133                 NSString *newDisplayName = [[[prefDict objectForKey:KEY_ACCOUNT_DISPLAY_NAME] attributedString] string];
134                 if (![displayName isEqualToString:newDisplayName]) {
135                         [textField_displayName setStringValue:newDisplayName];
136                 }
137         }
140 #pragma mark Profile
141 - (void)configureProfile
143         NSScrollView    *scrollView = [textView_profile enclosingScrollView];
144         if (scrollView && [scrollView isKindOfClass:[AIAutoScrollView class]]) {
145                 [(AIAutoScrollView *)scrollView setAlwaysDrawFocusRingIfFocused:YES];
146         }
147         
148         if ([textView_profile isKindOfClass:[AIMessageEntryTextView class]]) {
149                 /* We use the AIMessageEntryTextView to get nifty features for our text view, but we don't want to attempt
150                 * to 'send' to a target on Enter or Return.
151                 */
152                 [(AIMessageEntryTextView *)textView_profile setSendingEnabled:NO];
153         }
155         [[NSFontPanel sharedFontPanel] setDelegate:textView_profile];
157         NSData                          *profileData = [[adium preferenceController] preferenceForKey:@"TextProfile"
158                                                                                                                                                                 group:GROUP_ACCOUNT_STATUS];
159         NSAttributedString      *profile = (profileData ? [NSAttributedString stringWithData:profileData] : nil);
160         
161         if (profile && [profile length]) {
162                 [[textView_profile textStorage] setAttributedString:profile];
163         } else {
164                 [textView_profile setString:@""];
165         }       
168 - (void)fireProfileChangesImmediately
170         [NSObject cancelPreviousPerformRequestsWithTarget:self
171                                                                                          selector:@selector(changePreference:)
172                                                                                            object:textView_profile];    
173         [self changePreference:textView_profile];
176 - (void)textDidChange:(NSNotification *)aNotification
178         if ([aNotification object] == textView_profile) {               
179                 [NSObject cancelPreviousPerformRequestsWithTarget:self
180                                                                                                  selector:@selector(changePreference:)
181                                                                                                    object:textView_profile];
182                 [self performSelector:@selector(changePreference:)
183                                    withObject:textView_profile
184                                    afterDelay:1.0];
185         }
188 // AIImageViewWithImagePicker Delegate ---------------------------------------------------------------------
189 #pragma mark AIImageViewWithImagePicker Delegate
190 - (void)imageViewWithImagePicker:(AIImageViewWithImagePicker *)sender didChangeToImageData:(NSData *)imageData
192         [[adium preferenceController] setPreference:imageData
193                                                                                  forKey:KEY_USER_ICON
194                                                                                   group:GROUP_ACCOUNT_STATUS];
197 - (void)deleteInImageViewWithImagePicker:(AIImageViewWithImagePicker *)sender
199         [[adium preferenceController] setPreference:nil
200                                                                                  forKey:KEY_USER_ICON
201                                                                                   group:GROUP_ACCOUNT_STATUS];
203         //User icon - restore to the default icon
204         [self configureImageView];
207 - (NSString *)fileNameForImageInImagePicker:(AIImageViewWithImagePicker *)picker
209         return AILocalizedString(@"Adium Icon", nil);
212 - (void)configureImageView
214         NSData *imageData = [[adium preferenceController] preferenceForKey:KEY_USER_ICON
215                                                                                                                                  group:GROUP_ACCOUNT_STATUS];
216         if (!imageData) {
217                 imageData = [[adium preferenceController] preferenceForKey:KEY_DEFAULT_USER_ICON
218                                                                                                                          group:GROUP_ACCOUNT_STATUS];
219         }
221         [imageView_userIcon setImage:(imageData ? [[[NSImage alloc] initWithData:imageData] autorelease] : nil)];
224 - (void)configureTooltips
226         [matrix_userIcon setToolTip:AILocalizedString(@"Do not use an icon to represent you.", nil)
227                                                 forCell:[matrix_userIcon cellWithTag:0]];
228         [matrix_userIcon setToolTip:AILocalizedString(@"Use the icon below to represent you.", nil)
229                                                 forCell:[matrix_userIcon cellWithTag:1]];
231 #define DISPLAY_NAME_TOOLTIP AILocalizedString(@"Your name, which on supported services will be sent to remote contacts. Substitutions from the Edit->Scripts and Edit->iTunes menus may be used here.", nil)
232         [label_remoteAlias  setToolTip:DISPLAY_NAME_TOOLTIP];
233         [textField_displayName setToolTip:DISPLAY_NAME_TOOLTIP];
235 #define PROFILE_TOOLTIP AILocalizedString(@"Profile to display when contacts request information about you (not supported by all services). Text may be formatted using the Edit and Format menus.", nil)
236         [label_profile setToolTip:PROFILE_TOOLTIP];
237         [textView_profile setToolTip:PROFILE_TOOLTIP];
240 @end