Added Australian English translation to adium-0.8
[adiumx.git] / Source / AIAutoLinkingPlugin.m
blob78461e2caa79327390511afcb0b4a15d2087b072
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 "AIContentController.h"
19 #import <AIHyperlinks/AIHyperlinks.h>
21 /*!
22  * @class AIAutoLinkingPlugin
23  * @brief Filter component to 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 /*!
42  * @brief Deallocate
43  */
44 - (void)dealloc
46         [hyperlinkScanner release];
47         
48         [super dealloc];
51 /*!
52  * @brief Filter an attributed string to add links as appropriate
53  */
54 - (NSAttributedString *)filterAttributedString:(NSAttributedString *)inAttributedString context:(id)context
56         NSMutableAttributedString       *replacementMessage = nil;
58         if(inAttributedString){
59                 NSRange                                         linkRange = NSMakeRange(0,0);
60                 unsigned                                        index = 0;
61                 unsigned                                        stringLength = [inAttributedString length];
63                 replacementMessage = [[[NSMutableAttributedString alloc] initWithString:@""] autorelease];
65                 // do some quick scanning to avoid overwriting custom titled links that happen to have domain names in them.
66                 // e.g. if the entire string "check out the new story on adiumx.com" is linked to a specific story URI, then
67                 // adiumx.com should not link to only http://adiumx.com
68                 while(index < stringLength){
69                         if([inAttributedString attribute:NSLinkAttributeName atIndex:index effectiveRange:&linkRange]){
70                                 // if the link is found at that index, append the link's whole range to the replacement string
71                                 [replacementMessage appendAttributedString:[inAttributedString attributedSubstringFromRange:linkRange]];
72                         }else{
73                                 // if not, the range to the next link attribute is returned, and we linkify that range's substring
74                                 [replacementMessage appendAttributedString:[hyperlinkScanner linkifyString:[inAttributedString attributedSubstringFromRange:linkRange]]];
75                         }
76                         // increase the index
77                         index += linkRange.length;
78                 }
79         }
80         
81     return (replacementMessage);
84 /*!
85  * @brief Filter priority
86  *
87  * Auto linking overrides other potential filters; do it first
88  */
89 - (float)filterPriority
91         return HIGHEST_FILTER_PRIORITY;
94 @end