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