-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathWCConversationController.m
359 lines (231 loc) · 10.5 KB
/
WCConversationController.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/* $Id$ */
/*
* Copyright (c) 2009 Axel Andersson
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#import "WCApplicationController.h"
#import "WCChatController.h"
#import "WCPublicChat.h"
#import "WCConversation.h"
#import "WCConversationController.h"
#import "WCMessage.h"
#import "WCMessages.h"
#import "WCFiles.h"
#import "WCFile.h"
#import "WCTransfers.h"
#import "WDWiredModel.h"
#import "WCDatabaseController.h"
#import "WCMessageTableCellView.h"
#import "NSManagedObjectContext+Fetch.h"
#import "NSString+Emoji.h"
#import "NSImage+Data.h"
#import "NSDate_TimeAgo/NSDate+TimeAgo.h"
@interface WCConversationController (Private)
- (void)_configureCell:(WCMessageTableCellView *)cell forRow:(NSInteger)row;
@end
@implementation WCConversationController (Private)
- (void)_configureCell:(WCMessageTableCellView *)cell forRow:(NSInteger)row {
NSSortDescriptor *descriptor;
WDMessage *message;
descriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
message = [[[[self conversation] messages] sortedArrayUsingDescriptors:@[descriptor]] objectAtIndex:row];
if (message != nil) {
cell.nickTextField.stringValue = [message nick];
cell.serverNameTextField.stringValue = [[self conversation] serverName];
cell.timeTextField.stringValue = [message.date timeAgoWithLimit:(3600*24*30) dateFormatter:[[WCApplicationController sharedController] dateFormatter]];
cell.iconImageView.image = [[message user] icon];
if(![[message messageString] hasPrefix:@"<img src='data:image/png;base64,"]) {
NSAttributedString *attrString = [self _attributedStringWithClickableURLs:[[message messageString] stringByReplacingEmojiCheatCodesWithUnicode]];
cell.messageTestField.attributedStringValue = attrString;
cell.messageTestField.font = [self font];
cell.messageTestField.allowsEditingTextAttributes = YES;
} else if ([[message messageString] hasPrefix:@"<img src='http"]) {
NSData *imageData = [[message messageString] dataUsingEncoding:NSUTF8StringEncoding];
NSAttributedString *imageString = [[NSAttributedString alloc] initWithHTML:imageData documentAttributes:nil];
if (imageString) {
cell.messageTestField.attributedStringValue = imageString;
}
} else {
NSArray *comps = [[message messageString] componentsSeparatedByString:@"base64,"];
NSString *base64String = [[comps lastObject] substringToIndex:[[comps lastObject] length] - 3];
NSImage *image = [NSImage imageWithData:[NSData dataWithBase64EncodedString:base64String]];
id <NSTextAttachmentCell> attachmentCell = [[[NSTextAttachmentCell alloc] initImageCell:image] autorelease];
NSTextAttachment *attachment = [[[NSTextAttachment alloc] initWithData:nil ofType:nil] autorelease];
[attachment setAttachmentCell:attachmentCell];
NSAttributedString *attrString = [NSAttributedString attributedStringWithAttachment:attachment];
cell.messageTestField.attributedStringValue = attrString;
}
if (message.directionValue == WDMessageFrom) {
cell.iconImageView.image = [[self conversation] userIcon];
} else {
cell.iconImageView.image = [NSImage imageWithData:
[NSData dataWithBase64EncodedString:[[WCSettings settings] objectForKey:WCIcon]]];
}
}
}
- (void)_sendLocalImage:(NSURL *)url {
NSString *html;
NSString *base64ImageString;
NSData *imageData;
imageData = [NSData dataWithContentsOfURL:url];
base64ImageString = [imageData base64EncodedString];
html = [NSSWF:@"<img src='data:image/png;base64, %@'/>", base64ImageString];
if(html && [html length] > 0) {
[[WCMessages messages] sendMessage:html toUser:self.conversation.user];
}
}
- (NSAttributedString *)_attributedStringWithClickableURLs:(NSString *)text {
NSMutableAttributedString *string;
NSRange range;
string = [NSMutableAttributedString attributedStringWithString:text];
[string addAttribute:NSFontAttributeName value:[self font] range:NSMakeRange(0, text.length-1)];
range = [text rangeOfRegex:[NSSWF:@"(^|\\s)(%@)(\\.|,|:|\\?|!)?(\\s|$)", [NSString URLRegex]]
options:RKLCaseless | RKLMultiline
capture:0];
if (range.location != NSNotFound) {
[string addAttribute:NSLinkAttributeName value:[text substringWithRange:range] range:range];
}
range = [text rangeOfRegex:[NSSWF:@"(^|\\s)(%@)(\\.|,|:|\\?|!)?(\\s|$)", [NSString schemelessURLRegex]]
options:RKLCaseless | RKLMultiline
capture:0];
if (range.location != NSNotFound) {
[string addAttribute:NSLinkAttributeName value:[text substringWithRange:range] range:range];
}
range = [text rangeOfRegex:[NSSWF:@"(^|\\s)(%@)(\\.|,|:|\\?|!)?(\\s|$)", [NSString mailtoURLRegex]]
options:RKLCaseless | RKLMultiline
capture:0];
if (range.location != NSNotFound) {
[string addAttribute:NSLinkAttributeName value:[text substringWithRange:range] range:range];
}
return string;
}
@end
@implementation WCConversationController
- (id)init {
self = [super init];
_conversation = nil;
return self;
}
- (void)awakeFromNib {
[_conversationTableView registerForDraggedTypes:@[NSFilenamesPboardType]];
[_conversationTableView setDraggingDestinationFeedbackStyle:NSTableViewDraggingDestinationFeedbackStyleNone];
}
- (void)dealloc {
[_font release];
[_textColor release];
[_URLTextColor release];
[_backgroundColor release];
[_conversation release];
[super dealloc];
}
#pragma mark -
- (void)setConversation:(WDConversation *)conversation {
[conversation retain];
[_conversation release];
_conversation = conversation;
[self reloadData];
}
- (WDConversation *)conversation {
return _conversation;
}
- (void)setFont:(NSFont *)font {
[font retain];
[_font release];
_font = font;
}
- (NSFont *)font {
return _font;
}
- (void)setTextColor:(NSColor *)textColor {
[textColor retain];
[_textColor release];
_textColor = textColor;
}
- (NSColor *)textColor {
return _textColor;
}
- (void)setURLTextColor:(NSColor *)textColor {
[textColor retain];
[_URLTextColor release];
_URLTextColor = textColor;
}
- (NSColor *)URLTextColor {
return _URLTextColor;
}
- (void)setBackgroundColor:(NSColor *)backgroundColor {
[backgroundColor retain];
[_backgroundColor release];
_backgroundColor = backgroundColor;
}
- (NSColor *)backgroundColor {
return _backgroundColor;
}
#pragma mark -
- (void)appendMessage:(WDMessage *)message {
NSInteger lastRow = [[[self conversation] messages] count] == 0 ? 0 : [[[self conversation] messages] count] - 1;
NSIndexSet *lastRowIndexSet = [NSIndexSet indexSetWithIndex:lastRow];
[_conversationTableView insertRowsAtIndexes:[NSIndexSet indexSetWithIndex:lastRow]
withAnimation:NSTableViewAnimationEffectFade];
[_conversationTableView scrollToBottomAnimated];
lastRow = [_conversationTableView numberOfRows] - 1;
lastRowIndexSet = [NSIndexSet indexSetWithIndex:lastRow];
[_conversationTableView reloadDataForRowIndexes:lastRowIndexSet columnIndexes:[NSIndexSet indexSetWithIndex:0]];
[_conversationTableView scrollToBottomAnimated];
}
#pragma mark -
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [[_conversation messages] count];
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
WCMessageTableCellView *cell;
cell = [tableView makeViewWithIdentifier:@"WCMessageTableCellView" owner:self];
[self _configureCell:cell forRow:row];
return cell;
}
#pragma mark -
- (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id<NSDraggingInfo>)info row:(NSInteger)row dropOperation:(NSTableViewDropOperation)dropOperation {
NSArray *draggedFilenames = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
NSString *filepath = [draggedFilenames firstObject];
NSImage *image = [NSImage imageWithContentsOfFile:filepath];
if (!image) return NO;
NSURL *url = [NSURL fileURLWithPath:filepath];
[self _sendLocalImage:url];
return YES;
}
- (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id<NSDraggingInfo>)info proposedRow:(NSInteger)row proposedDropOperation:(NSTableViewDropOperation)dropOperation {
NSArray *draggedFilenames = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
if (![NSImage isImageAtPath:[draggedFilenames firstObject]]) {
return NSDragOperationNone;
}
return NSDragOperationCopy;
}
#pragma mark -
- (void)reloadData {
[_conversationTableView reloadData];
[_conversationTableView performSelector:@selector(scrollToBottomAnimated) afterDelay:0.1];
}
- (void)reloadView {
[_conversationTableView setNeedsDisplay];
}
@end