forked from the-kenny/AdiumChatQuickLook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatlogRenderer.m
309 lines (244 loc) · 12.6 KB
/
ChatlogRenderer.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//
// ChatlogRenderer.m
// AdiumChatQuickLook
//
// Created by Moritz Ulrich on 11.08.11.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#include <QuickLook/QuickLook.h>
#import "ChatlogRenderer.h"
@implementation ChatlogRenderer
#define PROJECT_ID @"im.adium.quicklookImporter"
@synthesize url=_url;
@synthesize account=_account;
@synthesize service=_service;
@synthesize attachments=_attachments;
@synthesize ISO8601Formatter=_ISO8601Formatter;
@synthesize hoursFormatter=_hoursFormatter;
- (id)init
{
if (self=[super init]) {
self.ISO8601Formatter = [[NSDateFormatter alloc] init];
[self.ISO8601Formatter setTimeStyle:NSDateFormatterFullStyle];
[self.ISO8601Formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZ"];
self.hoursFormatter = [[NSDateFormatter alloc] init];
[self.hoursFormatter setTimeStyle:NSDateFormatterShortStyle];
[self.hoursFormatter setDateFormat:@"HH:mm:ss"];
}
return self;
}
- (void)dealloc {
[rendererBundle release];
[_ISO8601Formatter release];
[_hoursFormatter release];
[super dealloc];
}
- (NSString *)generateHTMLForURL:(NSURL *)url attachments:(NSDictionary * __autoreleasing *)attachmentsDict
{
self.url = url;
NSDictionary* userDefaults = [[NSUserDefaults standardUserDefaults] persistentDomainForName:PROJECT_ID];
debugLog = [[userDefaults valueForKey:@"debugLog"] boolValue];
stripFontStyles = [[userDefaults valueForKey:@"stripStyles"] boolValue];
//NSUInteger messageLimit = [[userDefaults valueForKey:@"messageLimit"] unsignedIntegerValue];
#warning This is a workaround for getting a working NSBundle. When included in Adium, [NSBundle mainBundle] should work
rendererBundle = [[NSBundle bundleWithIdentifier:PROJECT_ID] retain];
NSError* error = nil;
NSXMLDocument *document = [[[NSXMLDocument alloc] initWithContentsOfURL:url
options:0
error:&error] autorelease];
if (!document || error) {
return [NSString stringWithFormat:@"Error reading the XML, %@", error];
}
if(debugLog)
NSLog(@"wholeTree: %@", document);
NSXMLElement* chatNode = [[document nodesForXPath:@"/chat" error:&error] objectAtIndex:0];
self.account = [[chatNode attributeForName:@"account"] stringValue];
self.service = [[chatNode attributeForName:@"service"] stringValue];
self.attachments = [NSMutableDictionary dictionary];
NSXMLElement* bodyElement = [NSXMLElement
elementWithName:@"body"
children:[NSArray arrayWithObject:[self generateTableFromChatElement:chatNode]]
attributes:nil];
NSXMLDocument* htmlElement = [NSXMLElement elementWithName:@"html"
children:[NSArray arrayWithObjects:
[self generateHead],
bodyElement, nil]
attributes:nil];
if(debugLog)
NSLog(@"html: %@", htmlElement);
if (attachmentsDict) {
*attachmentsDict = [NSDictionary dictionaryWithDictionary:self.attachments];
}
return [NSString stringWithFormat:@"%@", htmlElement];
}
#pragma mark - Methods to generate HTML
- (NSXMLElement*)generateHead {
NSError* error = nil;
NSString* cssStyle = [NSString stringWithContentsOfURL:[rendererBundle URLForResource:@"chatlog" withExtension:@"css"]
encoding:NSUTF8StringEncoding
error:&error];
if(error) {
NSLog(@"Error loading style: %@", [error description]);
error = nil;
}
return [NSXMLElement elementWithName:@"head"
children:[NSArray arrayWithObject:[NSXMLElement elementWithName:@"style" stringValue:cssStyle]]
attributes:nil];
}
- (NSXMLElement*)generateTableFromChatElement:(NSXMLElement*)chatElement {
NSXMLElement* table = [NSXMLElement elementWithName:@"table"];
for(NSXMLElement* node in [chatElement children]) {
if([node.name caseInsensitiveCompare:@"message"] == NSOrderedSame) {
[table addChild:[self generateMessageRow:node]];
} else if([node.name caseInsensitiveCompare:@"event"] == NSOrderedSame) {
[table addChild:[self generateEventRow:node]];
} else if([node.name caseInsensitiveCompare:@"status"] == NSOrderedSame) {
[table addChild:[self generateStatusRow:node]];
}
}
return table;
}
- (NSXMLElement*)generateTimestampFromMessage:(NSXMLElement*)message {
NSString* timeString = [[message attributeForName:@"time"] stringValue];
if(!timeString)
return nil;
return [NSXMLElement elementWithName:@"td"
children:[NSArray arrayWithObject:[NSXMLElement textWithStringValue:[self
formatDate:timeString]]]
attributes:[NSArray arrayWithObject:[NSXMLElement attributeWithName:@"class" stringValue:@"time"]]];
}
- (NSXMLElement*)generateNameFromMessage:(NSXMLElement*)message {
NSString* sender = [[message attributeForName:@"sender"] stringValue];
NSString* name = [[message attributeForName:@"alias"] stringValue];
if(!name) name = sender;
NSString *spanstyle = [sender caseInsensitiveCompare:self.account] == NSOrderedSame ? @"me" : @"other";
NSXMLElement* span = [NSXMLElement elementWithName:@"span"
children:[NSArray arrayWithObject:[NSXMLElement textWithStringValue:name]]
attributes:[NSArray arrayWithObject:[NSXMLElement attributeWithName:@"class"
stringValue:spanstyle]]];
return [NSXMLElement elementWithName:@"td"
children:[NSArray arrayWithObjects:span, [NSXMLElement textWithStringValue:@":"], nil]
attributes:[NSArray arrayWithObject:[NSXMLElement attributeWithName:@"class" stringValue:@"who"]]];
}
- (NSXMLElement*)generateTextFromMessage:(NSXMLElement*)message {
NSXMLElement *content = [[[message objectsForXQuery:@".//div" error:NULL] objectAtIndex:0] copy];
if (stripFontStyles == YES) {
[ChatlogRenderer removeStyleRecursive:content];
}
else {
[ChatlogRenderer removeInlineWhiteStyleRecursive:content];
}
for (NSUInteger i = 0; i < content.childCount; i++) {
NSXMLElement *img = (NSXMLElement *)[content childAtIndex:i];
if ([img.name caseInsensitiveCompare:@"img"] == NSOrderedSame) {
NSXMLNode *srcNode = [img attributeForName:@"src"];
NSString *imgFilename = srcNode.stringValue;
NSString *imgExt = imgFilename.pathExtension;
NSString *mimeType = nil;
if ([imgExt caseInsensitiveCompare:@"jpg"] == NSOrderedSame
|| [imgExt caseInsensitiveCompare:@"jpeg"] == NSOrderedSame) {
mimeType = @"image/jpg";
}
else if ([imgExt caseInsensitiveCompare:@"png"] == NSOrderedSame) {
mimeType = @"image/png";
}
else if ([imgExt caseInsensitiveCompare:@"tiff"] == NSOrderedSame) {
mimeType = @"image/tiff";
}
else {
continue;
}
[srcNode setStringValue:[NSString stringWithFormat:@"cid:%@", imgFilename]];
NSURL *imgUrl = [[self.url URLByDeletingLastPathComponent] URLByAppendingPathComponent:imgFilename];
// Fails probably due to sandbox
// if (![NSFileManager.defaultManager fileExistsAtPath:imgUrl.absoluteString]) {
// NSLog(@"error: file did not exist at path specified by <img> - %@ ;; xml: %@", imgUrl.absoluteString, self.url);
//
// continue;
// }
NSData *imgData = [NSData dataWithContentsOfURL:imgUrl];
self.attachments[imgFilename] = @{
(NSString *)kQLPreviewPropertyMIMETypeKey: mimeType,
(NSString *)kQLPreviewPropertyAttachmentDataKey: imgData
};
}
}
return [NSXMLElement elementWithName:@"td"
children:[NSArray arrayWithObject:content]
attributes:[NSArray arrayWithObject:[NSXMLElement attributeWithName:@"class" stringValue:@"what"]]];
}
- (NSXMLElement*)generateMessageRow:(NSXMLElement*)message {
return [NSXMLElement elementWithName:@"tr"
children:[NSArray arrayWithObjects:
[self generateTimestampFromMessage:message],
[self generateNameFromMessage:message],
[self generateTextFromMessage:message], nil]
attributes:nil];
}
- (NSXMLElement*)generateEventRow:(NSXMLElement*)event {
return nil;
}
- (NSXMLElement*)generateStatusRow:(NSXMLElement*)status {
// NSLog(@"statusRow: %@:", status);
NSString* sender = [[status attributeForName:@"sender"] stringValue];
if([sender isEqual:self.account]) {
//My own event!
NSXMLElement* content = [[[status objectsForXQuery:@".//div" error:NULL] objectAtIndex:0] copy];
if (stripFontStyles == YES) {
[ChatlogRenderer removeStyleRecursive:content];
}
else {
[ChatlogRenderer removeInlineWhiteStyleRecursive:content];
}
NSString* eventString = [NSString stringWithFormat:@"%@ (%@)",
[[content childAtIndex:0] stringValue],
[self formatDate:[[status attributeForName:@"time"] stringValue]]];
content = [NSXMLElement elementWithName:@"div"
children:[NSArray arrayWithObject:[NSXMLElement textWithStringValue:eventString]]
attributes:nil];
NSXMLElement* row = [NSXMLElement elementWithName:@"td"
children:[NSArray arrayWithObject:content]
attributes:[NSArray arrayWithObjects:
[NSXMLElement attributeWithName:@"class"
stringValue:@"event"],
[NSXMLElement attributeWithName:@"colspan"
stringValue:@"3"], nil]];
return [NSXMLElement elementWithName:@"tr"
children:[NSArray arrayWithObject:row]
attributes:nil];
} else {
return nil;
}
}
#pragma mark - Utility Methods
- (NSString*)formatDate:(NSString*)s {
// Remove : of time zone
NSMutableString *dateString = [[s mutableCopy] autorelease];
if ([dateString characterAtIndex: [dateString length] - 3] == ':')
[dateString deleteCharactersInRange: NSMakeRange([dateString length] - 3, 1)];
// Create NSDate
NSDate *date = [self.ISO8601Formatter dateFromString:dateString];
// Extract the hours
return [self.hoursFormatter stringFromDate:date];
}
+ (void)removeStyleRecursive:(NSXMLElement*)el {
if(el.kind == NSXMLElementKind) {
[el removeAttributeForName:@"class"];
[el removeAttributeForName:@"style"];
}
for(NSXMLElement* child in [el children]) {
[self removeStyleRecursive:child];
}
}
+ (void)removeInlineWhiteStyleRecursive:(NSXMLElement*)el {
if (el.kind == NSXMLElementKind) {
NSXMLNode *style = [el attributeForName:@"style"];
NSString *notWhiteStyle = [style.stringValue stringByReplacingOccurrencesOfString:@"color: #ffffff"
withString:@"color: #000000"];
[style setStringValue:notWhiteStyle];
}
for (NSXMLElement* child in el.children) {
[self removeInlineWhiteStyleRecursive:child];
}
}
@end