Merged [21073]: When encoding an icon to JPEG, start off at 100% quality. If that...
[adiumx.git] / Source / AIAutoLinkingPlugin.m
blobd5950a2220c459cfb731b159e316f0881ae21513
1 /* 
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
4  * 
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.
8  * 
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.
12  * 
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.
15  */
17 #import "AIAutoLinkingPlugin.h"
18 #import <Adium/AIContentControllerProtocol.h>
19 #import <AIHyperlinks/AIHyperlinks.h>
21 /*!
22  * @class AIAutoLinkingPlugin
23  * @brief Filter component ta automatically create links within attributed strings as appropriate
24  *
25  * The bulk of this component's work is accomplished by SHHyperlinkScanner
26  */
27 @implementation AIAutoLinkingPlugin
29 /*!
30  * @brief Install
31  */
32 - (void)installPlugin
34         hyperlinkScanner = [[SHHyperlinkScanner alloc] initWithStrictChecking:NO];
36         [[adium contentController] registerContentFilter:self ofType:AIFilterDisplay direction:AIFilterIncoming];
37         [[adium contentController] registerContentFilter:self ofType:AIFilterMessageDisplay direction:AIFilterIncoming];
38         [[adium contentController] registerContentFilter:self ofType:AIFilterMessageDisplay direction:AIFilterOutgoing];
41 - (void)uninstallPlugin
43         [[adium contentController] unregisterContentFilter:self];
46 /*!
47  * @brief Deallocate
48  */
49 - (void)dealloc
51         [hyperlinkScanner release];
52         
53         [super dealloc];
56 /*!
57  * @brief Filter an attributed string to add links as appropriate
58  */
59 - (NSAttributedString *)filterAttributedString:(NSAttributedString *)inAttributedString context:(id)context
61         if(!inAttributedString || ![inAttributedString length]) return inAttributedString;
63         NSMutableAttributedString       *replacementMessage = [inAttributedString mutableCopy];
64         NSRange                                         linkRange = NSMakeRange(0,0);
65         unsigned                                        stringLength = [replacementMessage length];
67         for (int i = 0; i < stringLength; i += linkRange.length) {
68                 if (![replacementMessage attribute:NSLinkAttributeName atIndex:i longestEffectiveRange:&linkRange inRange:NSMakeRange(i, stringLength - i)]) {
69                         /* If there's no link at this index already, process it via the hyperlinkScanner to see if there should be one.
70                          * We don't process existing links because (a) it would be duplicative effort and (b) we might mess up a link which had
71                          * a linkable item within its text, such as "Check out the new story at adiumx.com" linked to an adiumx.com page.
72                          */
73                         NSAttributedString      *replacementPart = [hyperlinkScanner linkifyString:[inAttributedString attributedSubstringFromRange:linkRange]];
74                         [replacementMessage replaceCharactersInRange:linkRange
75                                                                         withAttributedString:replacementPart];
76                         stringLength -= linkRange.length;
77                         linkRange.length = [replacementPart length];
78                         stringLength += linkRange.length;
79                 }
80         }
81         
82     return [replacementMessage autorelease];
85 /*!
86  * @brief Filter priority
87  *
88  * Auto linking overrides other potential filters; do it first
89  */
90 - (float)filterPriority
92         return HIGHEST_FILTER_PRIORITY;
95 @end