Source/AIDefaultFontRemovalPlugin.m
author Zachary West <zacw@adium.im>
Wed Oct 28 10:44:02 2009 -0400 (2009-10-28)
changeset 2666 a81e3542ed8f
permissions -rw-r--r--
Add a "Default font remover" plugin. Refs #12906 (though really fixes it).

This restores our pre-Snow Leopard behavior of sending plaintext when our defualt default font is specified. This also removes the white background Snow Leopard gives us for the attributed string, which would cause it to be sent for every message as well.

The only 'gotcha' here is that, for example, Helvetica-Oblique (italics) is a different actual font than Helvetica. At this point I'm more or less okay with this behavior.
zacw@2666
     1
//
zacw@2666
     2
//  AIDefaultFontRemoval.m
zacw@2666
     3
//  Adium
zacw@2666
     4
//
zacw@2666
     5
//  Created by Zachary West on 2009-10-28.
zacw@2666
     6
//  Copyright 2009  . All rights reserved.
zacw@2666
     7
//
zacw@2666
     8
zacw@2666
     9
#import "AIDefaultFontRemovalPlugin.h"
zacw@2666
    10
#import <AIUtilities/AIFontAdditions.h>
zacw@2666
    11
#import <AIUtilities/AIColorAdditions.h>
zacw@2666
    12
zacw@2666
    13
@implementation AIDefaultFontRemovalPlugin
zacw@2666
    14
- (void)installPlugin
zacw@2666
    15
{
zacw@2666
    16
	// We only monitor outgoing messages.
zacw@2666
    17
	[adium.contentController registerContentFilter:self ofType:AIFilterContent direction:AIFilterOutgoing];	
zacw@2666
    18
}
zacw@2666
    19
zacw@2666
    20
- (void)uninstallPlugin
zacw@2666
    21
{
zacw@2666
    22
	[adium.contentController unregisterContentFilter:self];
zacw@2666
    23
}
zacw@2666
    24
zacw@2666
    25
- (void)dealloc
zacw@2666
    26
{
zacw@2666
    27
	[defaultRemovedAttributes release];
zacw@2666
    28
	[super dealloc];
zacw@2666
    29
}
zacw@2666
    30
zacw@2666
    31
- (NSAttributedString *)filterAttributedString:(NSAttributedString *)inAttributedString context:(id)context
zacw@2666
    32
{
zacw@2666
    33
	if (!inAttributedString || ![inAttributedString length]) return inAttributedString;
zacw@2666
    34
	
zacw@2666
    35
	NSMutableAttributedString *mutableString = [inAttributedString mutableCopy];
zacw@2666
    36
		
zacw@2666
    37
	if (!defaultRemovedAttributes) {
zacw@2666
    38
		NSFont *defaultFont = [[adium.preferenceController defaultPreferenceForKey:KEY_FORMATTING_FONT
zacw@2666
    39
																			 group:PREF_GROUP_FORMATTING
zacw@2666
    40
																			object:nil] representedFont];
zacw@2666
    41
		
zacw@2666
    42
		defaultRemovedAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:
zacw@2666
    43
									 defaultFont, NSFontAttributeName,
zacw@2666
    44
									 [NSColor colorWithCalibratedRed:1.0 green:1.0 blue:1.0 alpha:1.0], NSBackgroundColorAttributeName,
zacw@2666
    45
									 nil] retain];
zacw@2666
    46
	}
zacw@2666
    47
	
zacw@2666
    48
	for (NSString *attributeName in defaultRemovedAttributes.allKeys) {
zacw@2666
    49
		NSUInteger position = 0;
zacw@2666
    50
		
zacw@2666
    51
		while (position < mutableString.length) {
zacw@2666
    52
			NSRange attributeRange;
zacw@2666
    53
			id attributeValue = [mutableString attribute:attributeName
zacw@2666
    54
												 atIndex:position
zacw@2666
    55
										  effectiveRange:&attributeRange];
zacw@2666
    56
zacw@2666
    57
			if (attributeValue && [attributeValue isEqualTo:[defaultRemovedAttributes objectForKey:attributeName]]) {
zacw@2666
    58
				[mutableString removeAttribute:attributeName range:attributeRange];
zacw@2666
    59
			}
zacw@2666
    60
			
zacw@2666
    61
			position += attributeRange.length;
zacw@2666
    62
		}
zacw@2666
    63
	}
zacw@2666
    64
	
zacw@2666
    65
	return [mutableString autorelease];
zacw@2666
    66
}
zacw@2666
    67
zacw@2666
    68
/*!
zacw@2666
    69
 * @brief When should this run?
zacw@2666
    70
 *
zacw@2666
    71
 * We want this font removal to occur as early as possible, in case any other filters try and modify the fonts.
zacw@2666
    72
 */
zacw@2666
    73
- (CGFloat)filterPriority
zacw@2666
    74
{
zacw@2666
    75
	return HIGHEST_FILTER_PRIORITY;
zacw@2666
    76
}
zacw@2666
    77
zacw@2666
    78
@end