Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 1 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 2 | /* |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 3 | * Copyright (C) 2015-2018 Savoir-faire Linux Inc. |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 4 | * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com> |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 5 | * Anthony Léonard <anthony.leonard@savoirfairelinux.com> |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 22 | #import <QPixmap> |
| 23 | #import <QtMacExtras/qmacfunctions.h> |
| 24 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 25 | // LRC |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 26 | #import <globalinstances.h> |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 27 | #import <api/interaction.h> |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 28 | |
| 29 | #import "MessagesVC.h" |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 30 | #import "views/IMTableCellView.h" |
| 31 | #import "views/MessageBubbleView.h" |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 32 | #import "views/NSImage+Extensions.h" |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 33 | #import "delegates/ImageManipulationDelegate.h" |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 34 | #import "utils.h" |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 35 | #import "views/NSColor+RingTheme.h" |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 36 | #import "views/IconButton.h" |
| 37 | #import <QuickLook/QuickLook.h> |
| 38 | #import <Quartz/Quartz.h> |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 39 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 40 | |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 41 | @interface MessagesVC () <NSTableViewDelegate, NSTableViewDataSource, QLPreviewPanelDataSource> { |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 42 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 43 | __unsafe_unretained IBOutlet NSTableView* conversationView; |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 44 | __unsafe_unretained IBOutlet NSView* containerView; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 45 | __unsafe_unretained IBOutlet NSTextField* messageField; |
| 46 | __unsafe_unretained IBOutlet IconButton *sendFileButton; |
| 47 | __unsafe_unretained IBOutlet NSLayoutConstraint* sendPanelHeight; |
| 48 | __unsafe_unretained IBOutlet NSLayoutConstraint* messagesBottomMargin; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 49 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 50 | std::string convUid_; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 51 | lrc::api::ConversationModel* convModel_; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 52 | const lrc::api::conversation::Info* cachedConv_; |
| 53 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 54 | QMetaObject::Connection newInteractionSignal_; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 55 | |
| 56 | // Both are needed to invalidate cached conversation as pointer |
| 57 | // may not be referencing the same conversation anymore |
| 58 | QMetaObject::Connection modelSortedSignal_; |
| 59 | QMetaObject::Connection filterChangedSignal_; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 60 | QMetaObject::Connection interactionStatusUpdatedSignal_; |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 61 | NSString* previewImage; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 62 | NSMutableDictionary *pendingMessagesToSend; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 63 | } |
| 64 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 65 | |
| 66 | @end |
| 67 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 68 | // Tags for view |
| 69 | NSInteger const GENERIC_INT_TEXT_TAG = 100; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 70 | NSInteger const GENERIC_INT_TIME_TAG = 200; |
| 71 | |
| 72 | // views size |
| 73 | CGFloat const GENERIC_CELL_HEIGHT = 60; |
| 74 | CGFloat const TIME_BOX_HEIGHT = 34; |
| 75 | CGFloat const MESSAGE_TEXT_PADDING = 10; |
| 76 | CGFloat const MAX_TRANSFERED_IMAGE_SIZE = 250; |
| 77 | CGFloat const BUBBLE_HEIGHT_FOR_TRANSFERED_FILE = 87; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 78 | NSInteger const MEESAGE_MARGIN = 21; |
| 79 | NSInteger const SEND_PANEL_DEFAULT_HEIGHT = 60; |
| 80 | NSInteger const SEND_PANEL_MAX_HEIGHT = 120; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 81 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 82 | @implementation MessagesVC |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 83 | |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 84 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 85 | //MessageBuble type |
| 86 | typedef NS_ENUM(NSInteger, MessageSequencing) { |
| 87 | SINGLE_WITH_TIME = 0, |
| 88 | SINGLE_WITHOUT_TIME = 1, |
| 89 | FIRST_WITH_TIME = 2, |
| 90 | FIRST_WITHOUT_TIME = 3, |
| 91 | MIDDLE_IN_SEQUENCE = 5, |
| 92 | LAST_IN_SEQUENCE = 6, |
| 93 | }; |
| 94 | |
| 95 | - (void)awakeFromNib |
| 96 | { |
| 97 | NSNib *cellNib = [[NSNib alloc] initWithNibNamed:@"MessageCells" bundle:nil]; |
| 98 | [conversationView registerNib:cellNib forIdentifier:@"LeftIncomingFileView"]; |
| 99 | [conversationView registerNib:cellNib forIdentifier:@"LeftOngoingFileView"]; |
| 100 | [conversationView registerNib:cellNib forIdentifier:@"LeftFinishedFileView"]; |
| 101 | [conversationView registerNib:cellNib forIdentifier:@"RightOngoingFileView"]; |
| 102 | [conversationView registerNib:cellNib forIdentifier:@"RightFinishedFileView"]; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 103 | [[conversationView.enclosingScrollView contentView] setCopiesOnScroll:NO]; |
| 104 | [messageField setFocusRingType:NSFocusRingTypeNone]; |
| 105 | [conversationView setWantsLayer:YES]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 106 | } |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 107 | |
| 108 | - (instancetype)initWithCoder:(NSCoder *)coder |
| 109 | { |
| 110 | self = [super initWithCoder:coder]; |
| 111 | if (self) { |
| 112 | pendingMessagesToSend = [[NSMutableDictionary alloc] init]; |
| 113 | } |
| 114 | return self; |
| 115 | } |
| 116 | |
| 117 | - (void)setMessage:(NSString *)newValue { |
| 118 | _message = [newValue removeEmptyLinesAtBorders]; |
| 119 | } |
| 120 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 121 | -(void) clearData { |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 122 | if (!convUid_.empty()) { |
| 123 | pendingMessagesToSend[@(convUid_.c_str())] = messageField.stringValue; |
| 124 | } |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 125 | cachedConv_ = nil; |
| 126 | convUid_ = ""; |
| 127 | convModel_ = nil; |
| 128 | |
| 129 | QObject::disconnect(modelSortedSignal_); |
| 130 | QObject::disconnect(filterChangedSignal_); |
| 131 | QObject::disconnect(interactionStatusUpdatedSignal_); |
| 132 | QObject::disconnect(newInteractionSignal_); |
| 133 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 134 | |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 135 | -(void) scrollToBottom { |
| 136 | CGRect visibleRect = [conversationView enclosingScrollView].contentView.visibleRect; |
| 137 | NSRange range = [conversationView rowsInRect:visibleRect]; |
| 138 | NSIndexSet* visibleIndexes = [NSIndexSet indexSetWithIndexesInRange:range]; |
| 139 | NSUInteger lastvisibleRow = [visibleIndexes lastIndex]; |
| 140 | if (([conversationView numberOfRows] > 0) && |
| 141 | lastvisibleRow == ([conversationView numberOfRows] -1)) { |
| 142 | [conversationView scrollToEndOfDocument:nil]; |
| 143 | } |
| 144 | } |
| 145 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 146 | -(const lrc::api::conversation::Info*) getCurrentConversation |
| 147 | { |
| 148 | if (convModel_ == nil || convUid_.empty()) |
| 149 | return nil; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 150 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 151 | if (cachedConv_ != nil) |
| 152 | return cachedConv_; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 153 | |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 154 | auto it = getConversationFromUid(convUid_, *convModel_); |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 155 | if (it != convModel_->allFilteredConversations().end()) |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 156 | cachedConv_ = &(*it); |
| 157 | |
| 158 | return cachedConv_; |
| 159 | } |
| 160 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 161 | -(void) reloadConversationForMessage:(uint64_t) uid shouldUpdateHeight:(bool)update { |
| 162 | auto* conv = [self getCurrentConversation]; |
| 163 | |
| 164 | if (conv == nil) |
| 165 | return; |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 166 | auto it = conv->interactions.find(uid); |
| 167 | if (it == conv->interactions.end()) { |
| 168 | return; |
| 169 | } |
| 170 | auto itIndex = distance(conv->interactions.begin(),it); |
| 171 | NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:itIndex]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 172 | //reload previous message to update bubbleview |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 173 | if (itIndex > 0) { |
| 174 | auto previousIt = it; |
| 175 | previousIt--; |
| 176 | auto previousInteraction = previousIt->second; |
| 177 | if (previousInteraction.type == lrc::api::interaction::Type::TEXT) { |
| 178 | NSRange range = NSMakeRange(itIndex - 1, 2); |
| 179 | indexSet = [NSIndexSet indexSetWithIndexesInRange:range]; |
| 180 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 181 | } |
| 182 | if (update) { |
| 183 | [conversationView noteHeightOfRowsWithIndexesChanged:indexSet]; |
| 184 | } |
| 185 | [conversationView reloadDataForRowIndexes: indexSet |
| 186 | columnIndexes:[NSIndexSet indexSetWithIndex:0]]; |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 187 | CGRect visibleRect = [conversationView enclosingScrollView].contentView.visibleRect; |
| 188 | NSRange range = [conversationView rowsInRect:visibleRect]; |
| 189 | NSIndexSet* visibleIndexes = [NSIndexSet indexSetWithIndexesInRange:range]; |
| 190 | NSUInteger lastvisibleRow = [visibleIndexes lastIndex]; |
| 191 | if (([conversationView numberOfRows] > 0) && |
| 192 | lastvisibleRow == ([conversationView numberOfRows] -1)) { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 193 | [conversationView scrollToEndOfDocument:nil]; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | -(void) reloadConversationForMessage:(uint64_t) uid shouldUpdateHeight:(bool)update updateConversation:(bool) updateConversation { |
| 198 | auto* conv = [self getCurrentConversation]; |
| 199 | |
| 200 | if (conv == nil) |
| 201 | return; |
| 202 | auto it = distance(conv->interactions.begin(),conv->interactions.find(uid)); |
| 203 | NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:it]; |
| 204 | //reload previous message to update bubbleview |
| 205 | if (it > 0) { |
| 206 | NSRange range = NSMakeRange(it - 1, it); |
| 207 | indexSet = [NSIndexSet indexSetWithIndexesInRange:range]; |
| 208 | } |
| 209 | if (update) { |
| 210 | [conversationView noteHeightOfRowsWithIndexesChanged:indexSet]; |
| 211 | } |
| 212 | [conversationView reloadDataForRowIndexes: indexSet |
| 213 | columnIndexes:[NSIndexSet indexSetWithIndex:0]]; |
| 214 | if (update) { |
| 215 | [conversationView scrollToEndOfDocument:nil]; |
| 216 | } |
| 217 | } |
| 218 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 219 | -(void)setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 220 | { |
| 221 | if (convUid_ == convUid && convModel_ == model) |
| 222 | return; |
| 223 | |
| 224 | cachedConv_ = nil; |
| 225 | convUid_ = convUid; |
| 226 | convModel_ = model; |
| 227 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 228 | // Signal triggered when messages are received or their status updated |
| 229 | QObject::disconnect(newInteractionSignal_); |
| 230 | QObject::disconnect(interactionStatusUpdatedSignal_); |
| 231 | newInteractionSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newInteraction, |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 232 | [self](const std::string& uid, uint64_t interactionId, const lrc::api::interaction::Info& interaction){ |
| 233 | if (uid != convUid_) |
| 234 | return; |
| 235 | cachedConv_ = nil; |
| 236 | [conversationView noteNumberOfRowsChanged]; |
| 237 | [self reloadConversationForMessage:interactionId shouldUpdateHeight:YES]; |
| 238 | }); |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 239 | interactionStatusUpdatedSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::interactionStatusUpdated, |
| 240 | [self](const std::string& uid, uint64_t interactionId, const lrc::api::interaction::Info& interaction){ |
| 241 | if (uid != convUid_) |
| 242 | return; |
| 243 | cachedConv_ = nil; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 244 | bool isOutgoing = lrc::api::interaction::isOutgoing(interaction); |
| 245 | if (interaction.type == lrc::api::interaction::Type::TEXT && isOutgoing) { |
| 246 | convModel_->refreshFilter(); |
| 247 | } |
| 248 | [self reloadConversationForMessage:interactionId shouldUpdateHeight:YES]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 249 | }); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 250 | |
| 251 | // Signals tracking changes in conversation list, we need them as cached conversation can be invalid |
| 252 | // after a reordering. |
| 253 | QObject::disconnect(modelSortedSignal_); |
| 254 | QObject::disconnect(filterChangedSignal_); |
| 255 | modelSortedSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted, |
| 256 | [self](){ |
| 257 | cachedConv_ = nil; |
| 258 | }); |
| 259 | filterChangedSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged, |
| 260 | [self](){ |
| 261 | cachedConv_ = nil; |
| 262 | }); |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 263 | if (pendingMessagesToSend[@(convUid_.c_str())]) { |
| 264 | self.message = pendingMessagesToSend[@(convUid_.c_str())]; |
| 265 | [self updateSendMessageHeight]; |
| 266 | } else { |
| 267 | self.message = @""; |
| 268 | if(messagesBottomMargin.constant != SEND_PANEL_DEFAULT_HEIGHT) { |
| 269 | sendPanelHeight.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 270 | messagesBottomMargin.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 271 | [self scrollToBottom]; |
| 272 | } |
| 273 | } |
| 274 | conversationView.alphaValue = 0.0; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 275 | [conversationView reloadData]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 276 | [conversationView scrollToEndOfDocument:nil]; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 277 | CABasicAnimation *fadeIn = [CABasicAnimation animationWithKeyPath:@"opacity"]; |
| 278 | fadeIn.fromValue = [NSNumber numberWithFloat:0.0]; |
| 279 | fadeIn.toValue = [NSNumber numberWithFloat:1.0]; |
| 280 | fadeIn.duration = 0.4f; |
| 281 | |
| 282 | [conversationView.layer addAnimation:fadeIn forKey:fadeIn.keyPath]; |
| 283 | conversationView.alphaValue = 1; |
| 284 | auto* conv = [self getCurrentConversation]; |
| 285 | |
| 286 | if (conv == nil) |
| 287 | return; |
| 288 | [sendFileButton setEnabled:(convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type != lrc::api::profile::Type::SIP)]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 289 | } |
| 290 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 291 | #pragma mark - configure cells |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 292 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 293 | -(NSTableCellView*) makeGenericInteractionViewForTableView:(NSTableView*)tableView withText:(NSString*)text andTime:(NSString*) time |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 294 | { |
| 295 | NSTableCellView* result = [tableView makeViewWithIdentifier:@"GenericInteractionView" owner:self]; |
| 296 | NSTextField* textField = [result viewWithTag:GENERIC_INT_TEXT_TAG]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 297 | NSTextField* timeField = [result viewWithTag:GENERIC_INT_TIME_TAG]; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 298 | |
| 299 | // TODO: Fix symbol in LRC |
| 300 | NSString* fixedString = [text stringByReplacingOccurrencesOfString:@"🕽" withString:@"📞"]; |
| 301 | [textField setStringValue:fixedString]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 302 | [timeField setStringValue:time]; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 303 | |
| 304 | return result; |
| 305 | } |
| 306 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 307 | -(NSTableCellView*) configureViewforTransfer:(lrc::api::interaction::Info)interaction interactionID: (uint64_t) interactionID tableView:(NSTableView*)tableView |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 308 | { |
| 309 | IMTableCellView* result; |
| 310 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 311 | auto type = interaction.type; |
| 312 | auto status = interaction.status; |
| 313 | |
| 314 | NSString* fileName = @"incoming file"; |
| 315 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 316 | // First, view is created |
| 317 | if (type == lrc::api::interaction::Type::INCOMING_DATA_TRANSFER) { |
| 318 | switch (status) { |
| 319 | case lrc::api::interaction::Status::TRANSFER_CREATED: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 320 | case lrc::api::interaction::Status::TRANSFER_AWAITING_HOST: { |
| 321 | result = [tableView makeViewWithIdentifier:@"LeftIncomingFileView" owner: conversationView]; |
| 322 | [result.acceptButton setAction:@selector(acceptIncomingFile:)]; |
| 323 | [result.acceptButton setTarget:self]; |
| 324 | [result.declineButton setAction:@selector(declineIncomingFile:)]; |
| 325 | [result.declineButton setTarget:self]; |
| 326 | break;} |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 327 | case lrc::api::interaction::Status::TRANSFER_ACCEPTED: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 328 | case lrc::api::interaction::Status::TRANSFER_ONGOING: { |
| 329 | result = [tableView makeViewWithIdentifier:@"LeftOngoingFileView" owner:conversationView]; |
| 330 | [result.progressIndicator startAnimation:conversationView]; |
| 331 | [result.declineButton setAction:@selector(declineIncomingFile:)]; |
| 332 | [result.declineButton setTarget:self]; |
| 333 | break;} |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 334 | case lrc::api::interaction::Status::TRANSFER_FINISHED: |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 335 | result = [tableView makeViewWithIdentifier:@"LeftFinishedFileView" owner:conversationView]; |
| 336 | [result.transferedFileName setAction:@selector(imagePreview:)]; |
| 337 | [result.transferedFileName setTarget:self]; |
| 338 | [result.transferedFileName.cell setHighlightsBy:NSContentsCellMask]; |
| 339 | break; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 340 | case lrc::api::interaction::Status::TRANSFER_CANCELED: |
| 341 | case lrc::api::interaction::Status::TRANSFER_ERROR: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 342 | result = [tableView makeViewWithIdentifier:@"LeftFinishedFileView" owner:conversationView]; |
| 343 | break; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 344 | } |
| 345 | } else if (type == lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER) { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 346 | NSString* fileName = @"sent file"; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 347 | switch (status) { |
| 348 | case lrc::api::interaction::Status::TRANSFER_CREATED: |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 349 | case lrc::api::interaction::Status::TRANSFER_ONGOING: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 350 | case lrc::api::interaction::Status::TRANSFER_AWAITING_PEER: |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 351 | case lrc::api::interaction::Status::TRANSFER_ACCEPTED: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 352 | result = [tableView makeViewWithIdentifier:@"RightOngoingFileView" owner:conversationView]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 353 | [result.progressIndicator startAnimation:nil]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 354 | [result.declineButton setAction:@selector(declineIncomingFile:)]; |
| 355 | [result.declineButton setTarget:self]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 356 | break; |
| 357 | case lrc::api::interaction::Status::TRANSFER_FINISHED: |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 358 | result = [tableView makeViewWithIdentifier:@"RightFinishedFileView" owner:conversationView]; |
| 359 | [result.transferedFileName setAction:@selector(imagePreview:)]; |
| 360 | [result.transferedFileName setTarget:self]; |
| 361 | [result.transferedFileName.cell setHighlightsBy:NSContentsCellMask]; |
| 362 | break; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 363 | case lrc::api::interaction::Status::TRANSFER_CANCELED: |
| 364 | case lrc::api::interaction::Status::TRANSFER_ERROR: |
Olivier Soldano | e521a18 | 2018-02-26 16:55:19 -0500 | [diff] [blame] | 365 | case lrc::api::interaction::Status::TRANSFER_UNJOINABLE_PEER: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 366 | result = [tableView makeViewWithIdentifier:@"RightFinishedFileView" owner:conversationView]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
| 370 | // Then status label is updated if needed |
| 371 | switch (status) { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 372 | [result.statusLabel setTextColor:[NSColor textColor]]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 373 | case lrc::api::interaction::Status::TRANSFER_FINISHED: |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 374 | [result.statusLabel setTextColor:[NSColor greenSuccessColor]]; |
Anthony Léonard | 70638f0 | 2018-02-05 11:10:19 -0500 | [diff] [blame] | 375 | [result.statusLabel setStringValue:NSLocalizedString(@"Success", @"File transfer successful label")]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 376 | break; |
| 377 | case lrc::api::interaction::Status::TRANSFER_CANCELED: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 378 | [result.statusLabel setTextColor:[NSColor orangeColor]]; |
Anthony Léonard | 70638f0 | 2018-02-05 11:10:19 -0500 | [diff] [blame] | 379 | [result.statusLabel setStringValue:NSLocalizedString(@"Canceled", @"File transfer canceled label")]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 380 | break; |
| 381 | case lrc::api::interaction::Status::TRANSFER_ERROR: |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 382 | [result.statusLabel setTextColor:[NSColor errorTransferColor]]; |
Anthony Léonard | 70638f0 | 2018-02-05 11:10:19 -0500 | [diff] [blame] | 383 | [result.statusLabel setStringValue:NSLocalizedString(@"Failed", @"File transfer failed label")]; |
Olivier Soldano | e521a18 | 2018-02-26 16:55:19 -0500 | [diff] [blame] | 384 | break; |
| 385 | case lrc::api::interaction::Status::TRANSFER_UNJOINABLE_PEER: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 386 | [result.statusLabel setTextColor:[NSColor textColor]]; |
Olivier Soldano | e521a18 | 2018-02-26 16:55:19 -0500 | [diff] [blame] | 387 | [result.statusLabel setStringValue:NSLocalizedString(@"Unjoinable", @"File transfer peer unjoinable label")]; |
| 388 | break; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 389 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 390 | result.transferedImage.image = nil; |
Kateryna Kostiuk | eaf1bc8 | 2018-10-12 14:33:50 -0400 | [diff] [blame] | 391 | [result.openImagebutton setHidden:YES]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 392 | [result.msgBackground setHidden:NO]; |
| 393 | [result invalidateImageConstraints]; |
| 394 | NSString* name = @(interaction.body.c_str()); |
| 395 | if (name.length > 0) { |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 396 | fileName = [name lastPathComponent]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 397 | } |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 398 | NSFont *nameFont = [NSFont userFontOfSize:14.0]; |
| 399 | NSColor *nameColor = [NSColor textColor]; |
| 400 | NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; |
| 401 | paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail; |
| 402 | paragraphStyle.alignment = NSTextAlignmentLeft; |
| 403 | NSDictionary *nameAttr = [NSDictionary dictionaryWithObjectsAndKeys:nameFont,NSFontAttributeName, |
| 404 | nameColor,NSForegroundColorAttributeName, |
| 405 | paragraphStyle,NSParagraphStyleAttributeName, nil]; |
| 406 | NSAttributedString* nameAttributedString = [[NSAttributedString alloc] initWithString:fileName attributes:nameAttr]; |
| 407 | result.transferedFileName.attributedTitle = nameAttributedString; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 408 | if (status == lrc::api::interaction::Status::TRANSFER_FINISHED) { |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 409 | NSColor *higlightColor = [NSColor grayColor]; |
| 410 | NSDictionary *alternativeNametAttr = [NSDictionary dictionaryWithObjectsAndKeys:nameFont,NSFontAttributeName, |
| 411 | higlightColor,NSForegroundColorAttributeName, |
| 412 | paragraphStyle,NSParagraphStyleAttributeName, nil]; |
| 413 | NSAttributedString* alternativeString = [[NSAttributedString alloc] initWithString:fileName attributes:alternativeNametAttr]; |
| 414 | result.transferedFileName.attributedAlternateTitle = alternativeString; |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 415 | NSImage* image = [self getImageForFilePath:name]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 416 | if (([name rangeOfString:@"/"].location == NSNotFound)) { |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 417 | image = [self getImageForFilePath:[self getDataTransferPath:interactionID]]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 418 | } |
| 419 | if(image != nil) { |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 420 | result.transferedImage.image = image; |
| 421 | [result updateImageConstraintWithMax: MAX_TRANSFERED_IMAGE_SIZE]; |
| 422 | [result.openImagebutton setAction:@selector(imagePreview:)]; |
| 423 | [result.openImagebutton setTarget:self]; |
Kateryna Kostiuk | eaf1bc8 | 2018-10-12 14:33:50 -0400 | [diff] [blame] | 424 | [result.openImagebutton setHidden:NO]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | [result setupForInteraction:interactionID]; |
| 428 | NSDate* msgTime = [NSDate dateWithTimeIntervalSince1970:interaction.timestamp]; |
| 429 | NSString* timeString = [self timeForMessage: msgTime]; |
| 430 | result.timeLabel.stringValue = timeString; |
| 431 | bool isOutgoing = lrc::api::interaction::isOutgoing(interaction); |
| 432 | if (!isOutgoing) { |
| 433 | auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator()); |
| 434 | auto* conv = [self getCurrentConversation]; |
| 435 | [result.photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(*conv, convModel_->owner)))]; |
| 436 | } |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 437 | return result; |
| 438 | } |
| 439 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 440 | #pragma mark - NSTableViewDelegate methods |
| 441 | - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 442 | { |
| 443 | return YES; |
| 444 | } |
| 445 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 446 | - (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 447 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 448 | return NO; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 449 | } |
| 450 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 451 | - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 452 | { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 453 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 454 | auto* conv = [self getCurrentConversation]; |
| 455 | |
| 456 | if (conv == nil) |
| 457 | return nil; |
| 458 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 459 | auto it = conv->interactions.begin(); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 460 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 461 | std::advance(it, row); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 462 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 463 | IMTableCellView* result; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 464 | auto interaction = it->second; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 465 | bool isOutgoing = lrc::api::interaction::isOutgoing(interaction); |
| 466 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 467 | switch (interaction.type) { |
| 468 | case lrc::api::interaction::Type::TEXT: |
| 469 | if (isOutgoing) { |
| 470 | result = [tableView makeViewWithIdentifier:@"RightMessageView" owner:self]; |
| 471 | } else { |
| 472 | result = [tableView makeViewWithIdentifier:@"LeftMessageView" owner:self]; |
| 473 | } |
| 474 | break; |
| 475 | case lrc::api::interaction::Type::INCOMING_DATA_TRANSFER: |
| 476 | case lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 477 | return [self configureViewforTransfer:interaction interactionID: it->first tableView:tableView]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 478 | break; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 479 | case lrc::api::interaction::Type::CONTACT: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 480 | case lrc::api::interaction::Type::CALL: { |
| 481 | NSDate* msgTime = [NSDate dateWithTimeIntervalSince1970:interaction.timestamp]; |
| 482 | NSString* timeString = [self timeForMessage: msgTime]; |
| 483 | return [self makeGenericInteractionViewForTableView:tableView withText:@(interaction.body.c_str()) andTime:timeString]; |
| 484 | } |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 485 | default: // If interaction is not of a known type |
| 486 | return nil; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 487 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 488 | MessageSequencing sequence = [self computeSequencingFor:row]; |
| 489 | BubbleType type = SINGLE; |
| 490 | if (sequence == FIRST_WITHOUT_TIME || sequence == FIRST_WITH_TIME) { |
| 491 | type = FIRST; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 492 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 493 | if (sequence == MIDDLE_IN_SEQUENCE) { |
| 494 | type = MIDDLE; |
| 495 | } |
| 496 | if (sequence == LAST_IN_SEQUENCE) { |
| 497 | type = LAST; |
| 498 | } |
| 499 | result.msgBackground.type = type; |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 500 | bool sendingFail = false; |
| 501 | [result.messageStatus setHidden:YES]; |
| 502 | if (interaction.type == lrc::api::interaction::Type::TEXT && isOutgoing) { |
| 503 | if (interaction.status == lrc::api::interaction::Status::SENDING) { |
| 504 | [result.messageStatus setHidden:NO]; |
| 505 | [result.sendingMessageIndicator startAnimation:nil]; |
| 506 | [result.messageFailed setHidden:YES]; |
| 507 | } else if (interaction.status == lrc::api::interaction::Status::FAILED) { |
| 508 | [result.messageStatus setHidden:NO]; |
| 509 | [result.sendingMessageIndicator setHidden:YES]; |
| 510 | [result.messageFailed setHidden:NO]; |
| 511 | sendingFail = true; |
| 512 | } |
| 513 | } |
| 514 | [result setupForInteraction:it->first isFailed: sendingFail]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 515 | bool shouldDisplayTime = (sequence == FIRST_WITH_TIME || sequence == SINGLE_WITH_TIME) ? YES : NO; |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 516 | bool shouldApplyPadding = (sequence == FIRST_WITHOUT_TIME || sequence == SINGLE_WITHOUT_TIME) ? YES : NO; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 517 | [result.msgBackground setNeedsDisplay:YES]; |
| 518 | [result setNeedsDisplay:YES]; |
| 519 | [result.timeBox setNeedsDisplay:YES]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 520 | |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 521 | NSString *text = @(interaction.body.c_str()); |
| 522 | text = [text removeEmptyLinesAtBorders]; |
| 523 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 524 | NSMutableAttributedString* msgAttString = |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 525 | [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:text] |
Kateryna Kostiuk | 3308987 | 2017-07-14 16:43:59 -0400 | [diff] [blame] | 526 | attributes:[self messageAttributes]]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 527 | |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 528 | CGSize messageSize = [self sizeFor: text maxWidth:tableView.frame.size.width * 0.7]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 529 | |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 530 | [result updateMessageConstraint:messageSize.width andHeight:messageSize.height timeIsVisible:shouldDisplayTime isTopPadding: shouldApplyPadding]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 531 | [[result.msgView textStorage] appendAttributedString:msgAttString]; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 532 | // [result.msgView checkTextInDocument:nil]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 533 | |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 534 | NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil]; |
| 535 | NSArray *matches = [linkDetector matchesInString:result.msgView.string options:0 range:NSMakeRange(0, result.msgView.string.length)]; |
| 536 | |
| 537 | [result.msgView.textStorage beginEditing]; |
| 538 | |
| 539 | for (NSTextCheckingResult *match in matches) { |
| 540 | if (!match.URL) continue; |
| 541 | |
| 542 | NSDictionary *linkAttributes = @{ |
| 543 | NSLinkAttributeName: match.URL, |
| 544 | }; |
| 545 | [result.msgView.textStorage addAttributes:linkAttributes range:match.range]; |
| 546 | } |
| 547 | |
| 548 | [result.msgView.textStorage endEditing]; |
| 549 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 550 | if (shouldDisplayTime) { |
| 551 | NSDate* msgTime = [NSDate dateWithTimeIntervalSince1970:interaction.timestamp]; |
| 552 | NSString* timeString = [self timeForMessage: msgTime]; |
| 553 | result.timeLabel.stringValue = timeString; |
Anthony Léonard | 64e1967 | 2018-01-18 16:40:34 -0500 | [diff] [blame] | 554 | } |
| 555 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 556 | bool shouldDisplayAvatar = (sequence != MIDDLE_IN_SEQUENCE && sequence != FIRST_WITHOUT_TIME |
| 557 | && sequence != FIRST_WITH_TIME) ? YES : NO; |
| 558 | [result.photoView setHidden:!shouldDisplayAvatar]; |
| 559 | if (!isOutgoing && shouldDisplayAvatar) { |
| 560 | auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator()); |
| 561 | [result.photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(*conv, convModel_->owner)))]; |
| 562 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 563 | return result; |
| 564 | } |
| 565 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 566 | - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 567 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 568 | double someWidth = tableView.frame.size.width * 0.7; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 569 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 570 | auto* conv = [self getCurrentConversation]; |
| 571 | |
| 572 | if (conv == nil) |
| 573 | return 0; |
| 574 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 575 | auto it = conv->interactions.begin(); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 576 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 577 | std::advance(it, row); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 578 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 579 | auto interaction = it->second; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 580 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 581 | MessageSequencing sequence = [self computeSequencingFor:row]; |
| 582 | |
| 583 | bool shouldDisplayTime = (sequence == FIRST_WITH_TIME || sequence == SINGLE_WITH_TIME) ? YES : NO; |
| 584 | |
| 585 | |
| 586 | if(interaction.type == lrc::api::interaction::Type::INCOMING_DATA_TRANSFER || interaction.type == lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER) { |
| 587 | |
| 588 | if( interaction.status == lrc::api::interaction::Status::TRANSFER_FINISHED) { |
| 589 | NSString* name = @(interaction.body.c_str()); |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 590 | NSImage* image = [self getImageForFilePath:name]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 591 | if (([name rangeOfString:@"/"].location == NSNotFound)) { |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 592 | image = [self getImageForFilePath:[self getDataTransferPath:it->first]]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 593 | } |
| 594 | if (image != nil) { |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 595 | CGFloat widthScaleFactor = MAX_TRANSFERED_IMAGE_SIZE / image.size.width; |
| 596 | CGFloat heightScaleFactor = MAX_TRANSFERED_IMAGE_SIZE / image.size.height; |
| 597 | CGFloat heigt = 0; |
| 598 | if((widthScaleFactor >= 1) && (heightScaleFactor >= 1)) { |
| 599 | heigt = image.size.height; |
| 600 | } else { |
| 601 | CGFloat scale = MIN(widthScaleFactor, heightScaleFactor); |
| 602 | heigt = image.size.height * scale; |
| 603 | } |
| 604 | return heigt + TIME_BOX_HEIGHT; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 605 | } |
| 606 | } |
| 607 | return BUBBLE_HEIGHT_FOR_TRANSFERED_FILE + TIME_BOX_HEIGHT; |
| 608 | } |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 609 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 610 | if(interaction.type == lrc::api::interaction::Type::CONTACT || interaction.type == lrc::api::interaction::Type::CALL) |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 611 | return GENERIC_CELL_HEIGHT; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 612 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 613 | // TODO Implement interactions other than messages |
| 614 | if(interaction.type != lrc::api::interaction::Type::TEXT) { |
| 615 | return 0; |
| 616 | } |
| 617 | |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 618 | NSString *text = @(interaction.body.c_str()); |
| 619 | text = [text removeEmptyLinesAtBorders]; |
| 620 | |
| 621 | CGSize messageSize = [self sizeFor: text maxWidth:tableView.frame.size.width * 0.7]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 622 | CGFloat singleLignMessageHeight = 15; |
| 623 | |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 624 | bool shouldApplyPadding = (sequence == FIRST_WITHOUT_TIME || sequence == SINGLE_WITHOUT_TIME) ? YES : NO; |
| 625 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 626 | if (shouldDisplayTime) { |
| 627 | return MAX(messageSize.height + TIME_BOX_HEIGHT + MESSAGE_TEXT_PADDING * 2, |
| 628 | TIME_BOX_HEIGHT + MESSAGE_TEXT_PADDING * 2 + singleLignMessageHeight); |
| 629 | } |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 630 | if(shouldApplyPadding) { |
| 631 | return MAX(messageSize.height + MESSAGE_TEXT_PADDING * 2 + 15, |
| 632 | singleLignMessageHeight + MESSAGE_TEXT_PADDING * 2 + 15); |
| 633 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 634 | return MAX(messageSize.height + MESSAGE_TEXT_PADDING * 2, |
| 635 | singleLignMessageHeight + MESSAGE_TEXT_PADDING * 2); |
| 636 | } |
| 637 | |
| 638 | #pragma mark - message view parameters |
| 639 | |
| 640 | -(NSString *) getDataTransferPath:(uint64_t)interactionId { |
| 641 | lrc::api::datatransfer::Info info = {}; |
| 642 | convModel_->getTransferInfo(interactionId, info); |
| 643 | double convertData = static_cast<double>(info.totalSize); |
| 644 | return @(info.path.c_str()); |
| 645 | } |
| 646 | |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 647 | -(NSImage*) getImageForFilePath: (NSString *) path { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 648 | if (path.length <= 0) {return nil;} |
| 649 | if (![[NSFileManager defaultManager] fileExistsAtPath: path]) {return nil;} |
| 650 | NSImage* transferedImage = [[NSImage alloc] initWithContentsOfFile: path]; |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 651 | return transferedImage; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 652 | } |
| 653 | |
| 654 | -(CGSize) sizeFor:(NSString *) message maxWidth:(CGFloat) width { |
| 655 | CGFloat horizaontalMargin = 6; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 656 | NSMutableAttributedString* msgAttString = |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 657 | [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", message] |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 658 | attributes:[self messageAttributes]]; |
| 659 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 660 | CGFloat finalWidth = MIN(msgAttString.size.width + horizaontalMargin * 2, width); |
| 661 | NSRect frame = NSMakeRect(0, 0, finalWidth, msgAttString.size.height); |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 662 | NSTextView *tv = [[NSTextView alloc] initWithFrame:frame]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 663 | [[tv textStorage] setAttributedString:msgAttString]; |
| 664 | [tv sizeToFit]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 665 | return tv.frame.size; |
| 666 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 667 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 668 | -(MessageSequencing) computeSequencingFor:(NSInteger) row { |
| 669 | auto* conv = [self getCurrentConversation]; |
| 670 | if (conv == nil) |
| 671 | return SINGLE_WITHOUT_TIME; |
| 672 | auto it = conv->interactions.begin(); |
| 673 | std::advance(it, row); |
| 674 | auto interaction = it->second; |
| 675 | if (interaction.type != lrc::api::interaction::Type::TEXT) { |
| 676 | return SINGLE_WITH_TIME; |
| 677 | } |
| 678 | if (row == 0) { |
| 679 | if (it == conv->interactions.end()) { |
| 680 | return SINGLE_WITH_TIME; |
| 681 | } |
| 682 | auto nextIt = it; |
| 683 | nextIt++; |
| 684 | auto nextInteraction = nextIt->second; |
| 685 | if ([self sequenceChangedFrom:interaction to: nextInteraction]) { |
| 686 | return SINGLE_WITH_TIME; |
| 687 | } |
| 688 | return FIRST_WITH_TIME; |
| 689 | } |
| 690 | |
| 691 | if (row == conversationView.numberOfRows - 1) { |
| 692 | if(it == conv->interactions.begin()) { |
| 693 | return SINGLE_WITH_TIME; |
| 694 | } |
| 695 | auto previousIt = it; |
| 696 | previousIt--; |
| 697 | auto previousInteraction = previousIt->second; |
| 698 | bool timeChanged = [self sequenceTimeChangedFrom:interaction to:previousInteraction]; |
| 699 | bool authorChanged = [self sequenceAuthorChangedFrom:interaction to:previousInteraction]; |
| 700 | if (!timeChanged && !authorChanged) { |
| 701 | return LAST_IN_SEQUENCE; |
| 702 | } |
| 703 | if (!timeChanged && authorChanged) { |
| 704 | return SINGLE_WITHOUT_TIME; |
| 705 | } |
| 706 | return SINGLE_WITH_TIME; |
| 707 | } |
| 708 | if(it == conv->interactions.begin() || it == conv->interactions.end()) { |
| 709 | return SINGLE_WITH_TIME; |
| 710 | } |
| 711 | auto previousIt = it; |
| 712 | previousIt--; |
| 713 | auto previousInteraction = previousIt->second; |
| 714 | auto nextIt = it; |
| 715 | nextIt++; |
| 716 | auto nextInteraction = nextIt->second; |
| 717 | |
| 718 | bool timeChanged = [self sequenceTimeChangedFrom:interaction to:previousInteraction]; |
| 719 | bool authorChanged = [self sequenceAuthorChangedFrom:interaction to:previousInteraction]; |
| 720 | bool sequenceWillChange = [self sequenceChangedFrom:interaction to: nextInteraction]; |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 721 | if (previousInteraction.type == lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER || |
| 722 | previousInteraction.type == lrc::api::interaction::Type::INCOMING_DATA_TRANSFER) { |
| 723 | if(!sequenceWillChange) { |
| 724 | return FIRST_WITH_TIME; |
| 725 | } |
| 726 | return SINGLE_WITH_TIME; |
| 727 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 728 | if (!sequenceWillChange) { |
| 729 | if (!timeChanged && !authorChanged) { |
| 730 | return MIDDLE_IN_SEQUENCE; |
| 731 | } |
| 732 | if (timeChanged) { |
| 733 | return FIRST_WITH_TIME; |
| 734 | } |
| 735 | return FIRST_WITHOUT_TIME; |
| 736 | } if (!timeChanged && !authorChanged) { |
| 737 | return LAST_IN_SEQUENCE; |
| 738 | } if (timeChanged) { |
| 739 | return SINGLE_WITH_TIME; |
| 740 | } |
| 741 | return SINGLE_WITHOUT_TIME; |
| 742 | } |
| 743 | |
| 744 | -(bool) sequenceChangedFrom:(lrc::api::interaction::Info) firstInteraction to:(lrc::api::interaction::Info) secondInteraction { |
| 745 | return ([self sequenceTimeChangedFrom:firstInteraction to:secondInteraction] || [self sequenceAuthorChangedFrom:firstInteraction to:secondInteraction]); |
| 746 | } |
| 747 | |
| 748 | -(bool) sequenceTimeChangedFrom:(lrc::api::interaction::Info) firstInteraction to:(lrc::api::interaction::Info) secondInteraction { |
| 749 | bool timeChanged = NO; |
| 750 | NSDate* firstMessageTime = [NSDate dateWithTimeIntervalSince1970:firstInteraction.timestamp]; |
| 751 | NSDate* secondMessageTime = [NSDate dateWithTimeIntervalSince1970:secondInteraction.timestamp]; |
| 752 | bool hourComp = [[NSCalendar currentCalendar] compareDate:firstMessageTime toDate:secondMessageTime toUnitGranularity:NSCalendarUnitHour]; |
| 753 | bool minutComp = [[NSCalendar currentCalendar] compareDate:firstMessageTime toDate:secondMessageTime toUnitGranularity:NSCalendarUnitMinute]; |
| 754 | if(hourComp != NSOrderedSame || minutComp != NSOrderedSame) { |
| 755 | timeChanged = YES; |
| 756 | } |
| 757 | return timeChanged; |
| 758 | } |
| 759 | |
| 760 | -(bool) sequenceAuthorChangedFrom:(lrc::api::interaction::Info) firstInteraction to:(lrc::api::interaction::Info) secondInteraction { |
| 761 | bool authorChanged = YES; |
| 762 | bool isOutgoing = lrc::api::interaction::isOutgoing(firstInteraction); |
| 763 | if ((secondInteraction.type == lrc::api::interaction::Type::TEXT) && (isOutgoing == lrc::api::interaction::isOutgoing(secondInteraction))) { |
| 764 | authorChanged = NO; |
| 765 | } |
| 766 | return authorChanged; |
| 767 | } |
| 768 | |
| 769 | -(NSString *)timeForMessage:(NSDate*) msgTime { |
| 770 | NSDate *today = [NSDate date]; |
| 771 | NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; |
Kateryna Kostiuk | af6d5e2 | 2018-06-12 15:00:00 -0400 | [diff] [blame] | 772 | [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale currentLocale] localeIdentifier]]]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 773 | if ([[NSCalendar currentCalendar] compareDate:today |
| 774 | toDate:msgTime |
| 775 | toUnitGranularity:NSCalendarUnitYear]!= NSOrderedSame) { |
| 776 | return [NSDateFormatter localizedStringFromDate:msgTime dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle]; |
| 777 | } |
| 778 | |
| 779 | if ([[NSCalendar currentCalendar] compareDate:today |
| 780 | toDate:msgTime |
| 781 | toUnitGranularity:NSCalendarUnitDay]!= NSOrderedSame || |
| 782 | [[NSCalendar currentCalendar] compareDate:today |
| 783 | toDate:msgTime |
| 784 | toUnitGranularity:NSCalendarUnitMonth]!= NSOrderedSame) { |
| 785 | [dateFormatter setDateFormat:@"MMM dd, HH:mm"]; |
| 786 | return [dateFormatter stringFromDate:msgTime]; |
| 787 | } |
| 788 | |
| 789 | [dateFormatter setDateFormat:@"HH:mm"]; |
| 790 | return [dateFormatter stringFromDate:msgTime]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 791 | } |
| 792 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 793 | #pragma mark - NSTableViewDataSource |
| 794 | |
| 795 | - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView |
| 796 | { |
| 797 | auto* conv = [self getCurrentConversation]; |
| 798 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 799 | if (conv) |
| 800 | return conv->interactions.size(); |
| 801 | else |
| 802 | return 0; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 803 | } |
| 804 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 805 | #pragma mark - Text formatting |
| 806 | |
Kateryna Kostiuk | 3308987 | 2017-07-14 16:43:59 -0400 | [diff] [blame] | 807 | - (NSMutableDictionary*) messageAttributes |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 808 | { |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 809 | NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 810 | attrs[NSForegroundColorAttributeName] = [NSColor labelColor]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 811 | attrs[NSParagraphStyleAttributeName] = [self paragraphStyle]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 812 | return attrs; |
| 813 | } |
| 814 | |
| 815 | - (NSParagraphStyle*) paragraphStyle |
| 816 | { |
| 817 | /* |
| 818 | The only way to instantiate an NSMutableParagraphStyle is to mutably copy an |
| 819 | NSParagraphStyle. And since we don't have an existing NSParagraphStyle available |
| 820 | to copy, we use the default one. |
| 821 | |
| 822 | The default values supplied by the default NSParagraphStyle are: |
| 823 | Alignment NSNaturalTextAlignment |
| 824 | Tab stops 12 left-aligned tabs, spaced by 28.0 points |
| 825 | Line break mode NSLineBreakByWordWrapping |
| 826 | All others 0.0 |
| 827 | */ |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 828 | NSMutableParagraphStyle* aMutableParagraphStyle = |
| 829 | [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
| 830 | [aMutableParagraphStyle setHeadIndent:1.0]; |
| 831 | [aMutableParagraphStyle setFirstLineHeadIndent:1.0]; |
| 832 | return aMutableParagraphStyle; |
| 833 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 834 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 835 | #pragma mark - Actions |
| 836 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 837 | - (void)acceptIncomingFile:(id)sender { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 838 | auto interId = [(IMTableCellView*)[[sender superview] superview] interaction]; |
| 839 | auto& inter = [self getCurrentConversation]->interactions.find(interId)->second; |
| 840 | if (convModel_ && !convUid_.empty()) { |
| 841 | NSSavePanel* filePicker = [NSSavePanel savePanel]; |
| 842 | [filePicker setNameFieldStringValue:@(inter.body.c_str())]; |
| 843 | |
| 844 | if ([filePicker runModal] == NSFileHandlingPanelOKButton) { |
| 845 | const char* fullPath = [[filePicker URL] fileSystemRepresentation]; |
| 846 | convModel_->acceptTransfer(convUid_, interId, fullPath); |
| 847 | } |
| 848 | } |
| 849 | } |
| 850 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 851 | - (void)declineIncomingFile:(id)sender { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 852 | auto inter = [(IMTableCellView*)[[sender superview] superview] interaction]; |
| 853 | if (convModel_ && !convUid_.empty()) { |
| 854 | convModel_->cancelTransfer(convUid_, inter); |
| 855 | } |
| 856 | } |
| 857 | |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 858 | - (void)imagePreview:(id)sender { |
| 859 | uint64_t interId; |
| 860 | if ([[sender superview] isKindOfClass:[IMTableCellView class]]) { |
| 861 | interId = [(IMTableCellView*)[sender superview] interaction]; |
| 862 | } else if ([[[sender superview] superview] isKindOfClass:[IMTableCellView class]]) { |
| 863 | interId = [(IMTableCellView*)[[sender superview] superview] interaction]; |
| 864 | } else { |
| 865 | return; |
| 866 | } |
| 867 | auto it = [self getCurrentConversation]->interactions.find(interId); |
| 868 | if (it == [self getCurrentConversation]->interactions.end()) { |
| 869 | return; |
| 870 | } |
| 871 | auto& interaction = it->second; |
| 872 | NSString* name = @(interaction.body.c_str()); |
| 873 | if (([name rangeOfString:@"/"].location == NSNotFound)) { |
| 874 | name = [self getDataTransferPath:interId]; |
| 875 | } |
| 876 | previewImage = name; |
| 877 | if ([QLPreviewPanel sharedPreviewPanelExists] && [[QLPreviewPanel sharedPreviewPanel] isVisible]) { |
| 878 | [[QLPreviewPanel sharedPreviewPanel] orderOut:nil]; |
| 879 | } else { |
| 880 | [[QLPreviewPanel sharedPreviewPanel] updateController]; |
| 881 | [QLPreviewPanel sharedPreviewPanel].dataSource = self; |
| 882 | [[QLPreviewPanel sharedPreviewPanel] setAnimationBehavior:NSWindowAnimationBehaviorDocumentWindow]; |
| 883 | [[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFront:nil]; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | - (NSInteger)numberOfPreviewItemsInPreviewPanel:(QLPreviewPanel *)panel { |
| 888 | return 1; |
| 889 | } |
| 890 | |
| 891 | - (id <QLPreviewItem>)previewPanel:(QLPreviewPanel *)panel previewItemAtIndex:(NSInteger)index { |
| 892 | return [NSURL fileURLWithPath:previewImage]; |
| 893 | } |
| 894 | |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 895 | - (void) updateSendMessageHeight { |
| 896 | NSAttributedString *msgAttString = messageField.attributedStringValue; |
| 897 | NSRect frame = NSMakeRect(0, 0, messageField.frame.size.width, msgAttString.size.height); |
| 898 | NSTextView *tv = [[NSTextView alloc] initWithFrame:frame]; |
| 899 | [[tv textStorage] setAttributedString:msgAttString]; |
| 900 | [tv sizeToFit]; |
| 901 | CGFloat height = tv.frame.size.height + MEESAGE_MARGIN * 2; |
| 902 | CGFloat newHeight = MIN(SEND_PANEL_MAX_HEIGHT, MAX(SEND_PANEL_DEFAULT_HEIGHT, height)); |
| 903 | if(messagesBottomMargin.constant == newHeight) { |
| 904 | return; |
| 905 | } |
| 906 | messagesBottomMargin.constant = newHeight; |
| 907 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.05 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ |
| 908 | [self scrollToBottom]; |
| 909 | sendPanelHeight.constant = newHeight; |
| 910 | }); |
| 911 | } |
| 912 | |
| 913 | - (IBAction)sendMessage:(id)sender { |
| 914 | NSString* text = self.message; |
| 915 | if (text && text.length > 0) { |
| 916 | auto* conv = [self getCurrentConversation]; |
| 917 | convModel_->sendMessage(convUid_, std::string([text UTF8String])); |
| 918 | self.message = @""; |
| 919 | if(sendPanelHeight.constant != SEND_PANEL_DEFAULT_HEIGHT) { |
| 920 | sendPanelHeight.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 921 | messagesBottomMargin.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 922 | [self scrollToBottom]; |
| 923 | } |
| 924 | } |
| 925 | } |
| 926 | |
| 927 | - (IBAction)openEmojy:(id)sender { |
| 928 | [messageField.window makeFirstResponder: messageField]; |
| 929 | [[messageField currentEditor] moveToEndOfLine:nil]; |
| 930 | [NSApp orderFrontCharacterPalette: messageField]; |
| 931 | } |
| 932 | |
| 933 | - (IBAction)sendFile:(id)sender { |
| 934 | NSOpenPanel* filePicker = [NSOpenPanel openPanel]; |
| 935 | [filePicker setCanChooseFiles:YES]; |
| 936 | [filePicker setCanChooseDirectories:NO]; |
| 937 | [filePicker setAllowsMultipleSelection:NO]; |
| 938 | |
| 939 | if ([filePicker runModal] == NSFileHandlingPanelOKButton) { |
| 940 | if ([[filePicker URLs] count] == 1) { |
| 941 | NSURL* url = [[filePicker URLs] objectAtIndex:0]; |
| 942 | const char* fullPath = [url fileSystemRepresentation]; |
| 943 | NSString* fileName = [url lastPathComponent]; |
| 944 | if (convModel_) { |
| 945 | auto* conv = [self getCurrentConversation]; |
| 946 | convModel_->sendFile(convUid_, std::string(fullPath), std::string([fileName UTF8String])); |
| 947 | } |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | |
| 953 | #pragma mark - NSTextFieldDelegate |
| 954 | |
| 955 | - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector |
| 956 | { |
| 957 | if (commandSelector == @selector(insertNewline:)) { |
| 958 | if(self.message.length > 0) { |
| 959 | [self sendMessage: nil]; |
| 960 | } else if(messagesBottomMargin.constant != SEND_PANEL_DEFAULT_HEIGHT) { |
| 961 | sendPanelHeight.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 962 | messagesBottomMargin.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 963 | [self scrollToBottom]; |
| 964 | } |
| 965 | return YES; |
| 966 | } |
| 967 | return NO; |
| 968 | } |
| 969 | |
| 970 | - (void)controlTextDidChange:(NSNotification *)aNotification { |
| 971 | [self updateSendMessageHeight]; |
| 972 | } |
| 973 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 974 | @end |