1 |
// |
2 |
// BSBeSAAPAnchorComposer.m |
3 |
// BathyScaphe |
4 |
// |
5 |
// Created by Tsutomu Sawada on 08/10/12. |
6 |
// Copyright 2008 BathyScaphe Project. All rights reserved. |
7 |
// encoding="UTF-8" |
8 |
// |
9 |
|
10 |
#import "BSBeSAAPAnchorComposer.h" |
11 |
|
12 |
|
13 |
@implementation BSBeSAAPAnchorComposer |
14 |
static BOOL g_showsSAAPIcon = YES; |
15 |
|
16 |
static inline NSString *convertedLinkString(NSString *saapString) |
17 |
{ |
18 |
NSMutableString *string = [NSMutableString stringWithString:saapString]; |
19 |
[string replaceCharactersInRange:NSMakeRange(0, 4) withString:@"http"]; |
20 |
return string; |
21 |
} |
22 |
|
23 |
static inline NSAttributedString *beIconAttachment(NSURL *adjustedUrl) |
24 |
{ |
25 |
NSImage *image_; |
26 |
NSTextAttachment *attachment_; |
27 |
NSAttributedString *attrs_; |
28 |
NSTextAttachmentCell *cell_; |
29 |
|
30 |
if (!adjustedUrl) return nil; |
31 |
image_ = [[NSImage alloc] initWithContentsOfURL:adjustedUrl]; |
32 |
|
33 |
if (!image_) return nil; |
34 |
|
35 |
attachment_ = [[NSTextAttachment alloc] init]; |
36 |
cell_ = [[NSTextAttachmentCell alloc] initImageCell:image_]; |
37 |
[image_ release]; |
38 |
|
39 |
[attachment_ setAttachmentCell:cell_]; |
40 |
[cell_ release]; |
41 |
|
42 |
if (!attachment_ || !cell_) { |
43 |
[attachment_ release]; |
44 |
return nil; |
45 |
} |
46 |
attrs_ = [NSAttributedString attributedStringWithAttachment:attachment_]; |
47 |
[attachment_ release]; |
48 |
if (!attrs_) return nil; |
49 |
|
50 |
return attrs_; |
51 |
} |
52 |
|
53 |
+ (BOOL)showsSAAPIcon |
54 |
{ |
55 |
return g_showsSAAPIcon; |
56 |
} |
57 |
|
58 |
+ (void)setShowsSAAPIcon:(BOOL)flag |
59 |
{ |
60 |
g_showsSAAPIcon = flag; |
61 |
} |
62 |
|
63 |
- (id)initWithRange:(NSRange)range saapLinkString:(NSString *)string |
64 |
{ |
65 |
if (self = [super init]) { |
66 |
m_replacingRange = range; |
67 |
m_httpLinkString = [convertedLinkString(string) retain]; |
68 |
} |
69 |
return self; |
70 |
} |
71 |
|
72 |
- (void)dealloc |
73 |
{ |
74 |
[m_httpLinkString release]; |
75 |
m_httpLinkString = nil; |
76 |
[super dealloc]; |
77 |
} |
78 |
|
79 |
- (void)composeSAAPAnchorIfNeeded:(NSMutableAttributedString *)message |
80 |
{ |
81 |
if ([[self class] showsSAAPIcon]) { |
82 |
NSURL *url = [NSURL URLWithString:m_httpLinkString]; |
83 |
NSAttributedString *attrStr = beIconAttachment(url); |
84 |
if (attrStr) { |
85 |
[message replaceCharactersInRange:m_replacingRange withAttributedString:attrStr]; |
86 |
return; |
87 |
} |
88 |
} |
89 |
|
90 |
[message replaceCharactersInRange:m_replacingRange withString:@""]; |
91 |
} |
92 |
@end |