forked from saptarshiguha/ReplyWithHeaders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMailHeaderString.m
160 lines (131 loc) · 5.68 KB
/
MailHeaderString.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// replyWitHeaders MailBundle - compose reply with message headers as in forwards
// Copyright (c) 2013 Saptarshi Guha and Jason Schroth
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// MIT License for more details.
//
// You should have received a copy of the MIT License along with this
// program. If not, see <http://opensource.org/licenses/MIT>.
//
// MailHeaderString.m
// ReplyWithHeader
//
// Created by Jason Schroth on 8/15/12.
//
//
#import "MailHeaderString.h"
@implementation MailHeaderString
-(id)init
{
if (self = [super init]) {
//good stuff...
}
else {
RWH_LOG(@"MailHeaderString: Init failed");
}
return self;
}
-(id)initWithStr: (NSAttributedString *)str
{
//initialze the value with a mutable copy of the attributed string
if( self = [super init] )
{
// NSAttributedString *headerString =[[self originalMessageHeaders] attributedStringShowingHeaderDetailLevel:1];
// useHeadIndents:NO
// useBold:boldhead
// includeBCC:YES];
headstr = [str mutableCopy];
//remove the color attribute so that the text is black instead of gray
//also remove paragraph style included in the header to avoid spacing issues when received by some mail clients
[headstr removeAttribute:@"NSColor" range:NSMakeRange(0,[headstr length])];
[headstr removeAttribute:@"NSParagraphStyle" range:NSMakeRange(0,[headstr length])];
}
else {
RWH_LOG(@"MailHeaderString: Init failed");
}
return self;
}
-(id)initWithBackEnd:(id)backend
{
//initialze the value with a mutable copy of the attributed string
if( self = [super init] )
{
//Mountain Lion call
//headstr =[[backend originalMessageHeaders] attributedStringShowingHeaderDetailLevel:1
// useHeadIndents:NO
// useBold:YES
// includeBCC:YES];
//new call for Lion
headstr = [[[backend originalMessageHeaders] attributedStringShowingHeaderDetailLevel:1] mutableCopy];
//remove the color attribute so that the text is black instead of gray
//also remove paragraph style included in the header to avoid spacing issues when received by some mail clients
[headstr removeAttribute:@"NSColor" range:NSMakeRange(0,[headstr length])];
[headstr removeAttribute:@"NSParagraphStyle" range:NSMakeRange(0,[headstr length])];
RWH_LOG(@"MailHeaderString: created headstr: %@",headstr);
}
else {
RWH_LOG(@"MailHeaderString: Init Backend failed");
}
return self;
}
/*
* descr: Bolds the lables for header elements (i.e., From:, To:, Subject:, Date:, etc.).
*/
-(void)boldHeaderLabels
{
//get all of the fonts in use, but only use the first one
NSDictionary *dict = [headstr fontAttributesInRange:NSMakeRange(0,[headstr length])];
NSEnumerator *enumer = [dict objectEnumerator];
NSFont *basicFont = (NSFont *) [enumer nextObject]; //[dict objectForKey:key];
RWH_LOG(@"font = %@",basicFont);
NSString *fontName = [basicFont fontName];
RWH_LOG(@"orig font name is: %@",fontName);
const CGFloat *mat = [basicFont matrix];
//check if the font is already bold before making it bold
NSRange boldRangeLoc;
boldRangeLoc =[fontName rangeOfString:@"-Bold"];
if( boldRangeLoc.location == NSNotFound )
{
fontName = [[fontName autorelease] stringByAppendingString:@"-Bold"];
}
RWH_LOG(@"font name is: %@",fontName);
NSFont *boldFont = [NSFont fontWithName:fontName matrix:mat];
//setup a regular expression to find a word followed by a colon and then space
// should get the first item (e.g. "From:").
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\w+:\\s" options: NSRegularExpressionCaseInsensitive error:&error];
NSRange rangeOfFirstMatch = [regex rangeOfFirstMatchInString:[headstr string] options:0 range:NSMakeRange(0, [headstr length])];
RWH_LOG(@"Match Range is: %@", NSStringFromRange(rangeOfFirstMatch));
[headstr addAttribute:@"NSFont" value:boldFont range:rangeOfFirstMatch];
//new regex and for loop to process the rest of the attribute names (e.g. Subject:, To:, Cc:, etc.)
regex = [NSRegularExpression regularExpressionWithPattern:@"(\\n|\\r)[\\w\\-\\s]+:\\s" options: NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches=[regex matchesInString:[headstr string] options:0 range:NSMakeRange(0,[headstr length])];
for (NSTextCheckingResult *match in matches)
{
NSRange matchRange = [match range];
[headstr addAttribute:@"NSFont" value:boldFont range:matchRange];
}
}
-(WebArchive *)getWebArch
{
WebArchive *arch = [headstr webArchiveForRange:NSMakeRange(0,[headstr length]) fixUpNewlines:YES];
return arch;
}
-(NSMutableAttributedString *)string
{
return headstr;
}
@end