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 "AIContentController.h"
18 #import "AIDefaultFormattingPlugin.h"
19 #import "AIInterfaceController.h"
20 #import "AIMenuController.h"
21 #import "AIPreferenceController.h"
22 #import <AIUtilities/AIAttributedStringAdditions.h>
23 #import <AIUtilities/AIColorAdditions.h>
24 #import <AIUtilities/AIDictionaryAdditions.h>
25 #import <AIUtilities/AIFontAdditions.h>
26 #import <AIUtilities/AIMenuAdditions.h>
27 #import <AIUtilities/AITextAttributes.h>
28 #import <Adium/AIContentMessage.h>
30 #define DEFAULT_FORMATTING_DEFAULT_PREFS @"FormattingDefaults"
32 @protocol AITextEntryView;
34 @interface AIDefaultFormattingPlugin (PRIVATE)
35 - (void)preferencesChanged:(NSNotification *)notification;
36 - (void)_resetFormattingInView:(NSText<AITextEntryView> *)inTextEntryView;
39 @implementation AIDefaultFormattingPlugin
42 * @brief Install the default formatting plugin
47 AIPreferenceController *preferenceController = [adium preferenceController];
48 [preferenceController registerDefaults:[NSDictionary dictionaryNamed:DEFAULT_FORMATTING_DEFAULT_PREFS forClass:[self class]] forGroup:PREF_GROUP_FORMATTING];
49 [preferenceController registerPreferenceObserver:self forGroup:PREF_GROUP_FORMATTING];
51 //Reset formatting menu item
52 NSMenuItem *menuItem = [[[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:AILocalizedString(@"Restore Default Formatting",nil)
54 action:@selector(restoreDefaultFormat:)
55 keyEquivalent:@""] autorelease];
56 [[adium menuController] addMenuItem:menuItem toLocation:LOC_Format_Additions];
58 //Register as an entry filter, so we can prepare textviews as they open
59 [[adium contentController] registerTextEntryFilter:self];
61 //Observe content sending so we can save the user's formatting
62 [[adium notificationCenter] addObserver:self
63 selector:@selector(didSendContent:)
64 name:CONTENT_MESSAGE_SENT
68 [[adium notificationCenter] addObserver:self
69 selector:@selector(restoreDefaultFormat:)
70 name:@"Adium_RestoreDefaultFormatting"
75 * @brief Uninstall the default formatting plugin
77 - (void)uninstallPlugin
79 [[adium preferenceController] unregisterPreferenceObserver:self];
80 [[adium contentController] unregisterTextEntryFilter:self];
81 [[adium notificationCenter] removeObserver:self];
85 * @brief Invoked when formatting preferences change
87 - (void)preferencesChangedForGroup:(NSString *)group key:(NSString *)key
88 object:(AIListObject *)object preferenceDict:(NSDictionary *)prefDict firstTime:(BOOL)firstTime
90 NSMutableDictionary *attributes;
92 //Update our local preference cache for the new values
95 [backgroundColor release];
96 font = [[[prefDict objectForKey:KEY_FORMATTING_FONT] representedFont] retain];
97 textColor = [[[prefDict objectForKey:KEY_FORMATTING_TEXT_COLOR] representedColor] retain];
98 backgroundColor = [[[prefDict objectForKey:KEY_FORMATTING_BACKGROUND_COLOR] representedColor] retain];
100 //Apply the new preferences as Adium's default formatting attributes
101 attributes = [NSMutableDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil];
102 if(textColor && ![textColor equalToRGBColor:[NSColor textColor]]){
103 [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
105 if (backgroundColor && ![backgroundColor equalToRGBColor:[NSColor textBackgroundColor]]){
106 [attributes setObject:backgroundColor forKey:AIBodyColorAttributeName];
108 [[adium contentController] setDefaultFormattingAttributes:attributes];
112 * @brief Invoked when a text entry view opens, set it to our default formatting
114 - (void)didOpenTextEntryView:(NSText<AITextEntryView> *)inTextEntryView
116 [self _resetFormattingInView:inTextEntryView];
120 * @brief Invoked when a text entry view closes
122 - (void)willCloseTextEntryView:(NSText<AITextEntryView> *)inTextEntryView
128 * @brief Invoked when content is sent, remember the text formatting used
130 * Don't update the formatting when we send an autoreply.
132 - (void)didSendContent:(NSNotification *)notification
134 AIContentObject *content = [[notification userInfo] objectForKey:@"AIContentObject"];
137 [content isKindOfClass:[AIContentMessage class]] &&
138 ![(AIContentMessage *)content isAutoreply]){
139 NSAttributedString *message = [content message];
141 if([message length] > 0) {
142 NSDictionary *attributes = [message attributesAtIndex:[message length]-1 effectiveRange:nil];
143 NSString *newFont = [(NSFont *)[attributes objectForKey:NSFontAttributeName] stringRepresentation];
144 NSString *newTextColor = [(NSColor *)[attributes objectForKey:NSForegroundColorAttributeName] stringRepresentation];
145 NSString *newBodyColor = [(NSColor *)[attributes objectForKey:AIBodyColorAttributeName] stringRepresentation];
147 //If the attribute is nil for these values, substitute our defaults
148 if(!newFont) newFont = [[adium preferenceController] defaultPreferenceForKey:KEY_FORMATTING_FONT
149 group:PREF_GROUP_FORMATTING
151 if(!newTextColor) newTextColor = [[adium preferenceController] defaultPreferenceForKey:KEY_FORMATTING_TEXT_COLOR
152 group:PREF_GROUP_FORMATTING
154 if(!newBodyColor) newBodyColor = [[adium preferenceController] defaultPreferenceForKey:KEY_FORMATTING_BACKGROUND_COLOR
155 group:PREF_GROUP_FORMATTING
158 //Save the new formatting (if it's changed)
159 if(![[font stringRepresentation] isEqualToString:newFont]){
160 AILog(@"newfont:%@ (was %@)",newFont,[font stringRepresentation]);
161 [[adium preferenceController] setPreference:newFont
162 forKey:KEY_FORMATTING_FONT
163 group:PREF_GROUP_FORMATTING];
165 if(![[textColor stringRepresentation] isEqualToString:newTextColor]){
166 AILog(@"newcolor:%@ (was %@)",newTextColor,[textColor stringRepresentation]);
167 [[adium preferenceController] setPreference:newTextColor
168 forKey:KEY_FORMATTING_TEXT_COLOR
169 group:PREF_GROUP_FORMATTING];
171 if(![[backgroundColor stringRepresentation] isEqualToString:newBodyColor]){
172 AILog(@"newbackgroundcolor:%@ (was %@)",newBodyColor,[backgroundColor stringRepresentation]);
173 [[adium preferenceController] setPreference:newBodyColor
174 forKey:KEY_FORMATTING_TEXT_COLOR
175 group:PREF_GROUP_FORMATTING];
183 * @brief Restore text formatting to default in all message windows
185 - (void)restoreDefaultFormat:(id)sender
187 //Reset formatting to default
188 [[adium preferenceController] setPreference:nil
189 forKey:KEY_FORMATTING_FONT
190 group:PREF_GROUP_FORMATTING];
191 [[adium preferenceController] setPreference:nil
192 forKey:KEY_FORMATTING_TEXT_COLOR
193 group:PREF_GROUP_FORMATTING];
194 [[adium preferenceController] setPreference:nil
195 forKey:KEY_FORMATTING_BACKGROUND_COLOR
196 group:PREF_GROUP_FORMATTING];
198 //Update all open message windows to our default formatting
199 NSEnumerator *enumerator = [[[adium contentController] openTextEntryViews] objectEnumerator];
200 NSText<AITextEntryView> *textEntryView;
202 while(textEntryView = [enumerator nextObject]){
203 [self _resetFormattingInView:textEntryView];
208 * @brief Resets all the text in an entry view to the default values
210 - (void)_resetFormattingInView:(NSText<AITextEntryView> *)inTextEntryView
212 NSMutableAttributedString *contents;
213 NSDictionary *attributes;
215 attributes = [[adium contentController] defaultFormattingAttributes];
217 //Set them as the typing attributes for all new text
218 [inTextEntryView setTypingAttributes:attributes];
220 //Apply the attributes to the existing content
221 contents = [[inTextEntryView textStorage] mutableCopy];
222 [contents setAttributes:attributes range:NSMakeRange(0,[contents length])];
223 [inTextEntryView setAttributedString:contents];