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 | /* |
Sébastien Blin | 029ffa8 | 2019-01-02 17:43:48 -0500 | [diff] [blame] | 3 | * Copyright (C) 2015-2019 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 | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 39 | #import <AVFoundation/AVFoundation.h> |
| 40 | |
| 41 | #import "RecordFileVC.h" |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 42 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 43 | |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 44 | @interface MessagesVC () <NSTableViewDelegate, NSTableViewDataSource, QLPreviewPanelDataSource> { |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 45 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 46 | __unsafe_unretained IBOutlet NSTableView* conversationView; |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 47 | __unsafe_unretained IBOutlet NSView* containerView; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 48 | __unsafe_unretained IBOutlet NSTextField* messageField; |
| 49 | __unsafe_unretained IBOutlet IconButton *sendFileButton; |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 50 | __unsafe_unretained IBOutlet IconButton *recordVideoButton; |
| 51 | __unsafe_unretained IBOutlet IconButton *recordAudioButton; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 52 | __unsafe_unretained IBOutlet NSLayoutConstraint* sendPanelHeight; |
| 53 | __unsafe_unretained IBOutlet NSLayoutConstraint* messagesBottomMargin; |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 54 | IBOutlet NSPopover *recordMessagePopover; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 55 | |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 56 | QString convUid_; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 57 | lrc::api::ConversationModel* convModel_; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 58 | const lrc::api::conversation::Info* cachedConv_; |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 59 | lrc::api::AVModel* avModel; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 60 | QMetaObject::Connection newInteractionSignal_; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 61 | |
| 62 | // Both are needed to invalidate cached conversation as pointer |
| 63 | // may not be referencing the same conversation anymore |
| 64 | QMetaObject::Connection modelSortedSignal_; |
| 65 | QMetaObject::Connection filterChangedSignal_; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 66 | QMetaObject::Connection interactionStatusUpdatedSignal_; |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 67 | QMetaObject::Connection peerComposingMsgSignal_; |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 68 | NSString* previewImage; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 69 | NSMutableDictionary *pendingMessagesToSend; |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 70 | RecordFileVC * recordingController; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 73 | @end |
| 74 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 75 | // Tags for view |
| 76 | NSInteger const GENERIC_INT_TEXT_TAG = 100; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 77 | NSInteger const GENERIC_INT_TIME_TAG = 200; |
| 78 | |
| 79 | // views size |
| 80 | CGFloat const GENERIC_CELL_HEIGHT = 60; |
| 81 | CGFloat const TIME_BOX_HEIGHT = 34; |
| 82 | CGFloat const MESSAGE_TEXT_PADDING = 10; |
| 83 | CGFloat const MAX_TRANSFERED_IMAGE_SIZE = 250; |
| 84 | CGFloat const BUBBLE_HEIGHT_FOR_TRANSFERED_FILE = 87; |
Kateryna Kostiuk | f5eb58c | 2020-03-25 16:50:35 -0400 | [diff] [blame] | 85 | CGFloat const HEIGHT_FOR_COMPOSING_INDICATOR = 37; |
Kateryna Kostiuk | a120192 | 2020-04-20 11:59:35 -0400 | [diff] [blame] | 86 | CGFloat const HEIGHT_DEFAULT = 34; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 87 | NSInteger const MEESAGE_MARGIN = 21; |
| 88 | NSInteger const SEND_PANEL_DEFAULT_HEIGHT = 60; |
| 89 | NSInteger const SEND_PANEL_MAX_HEIGHT = 120; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 90 | |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 91 | BOOL peerComposingMessage = false; |
| 92 | BOOL composingMessage = false; |
| 93 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 94 | @implementation MessagesVC |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 95 | |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 96 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 97 | //MessageBuble type |
| 98 | typedef NS_ENUM(NSInteger, MessageSequencing) { |
| 99 | SINGLE_WITH_TIME = 0, |
| 100 | SINGLE_WITHOUT_TIME = 1, |
| 101 | FIRST_WITH_TIME = 2, |
| 102 | FIRST_WITHOUT_TIME = 3, |
| 103 | MIDDLE_IN_SEQUENCE = 5, |
| 104 | LAST_IN_SEQUENCE = 6, |
| 105 | }; |
| 106 | |
| 107 | - (void)awakeFromNib |
| 108 | { |
| 109 | NSNib *cellNib = [[NSNib alloc] initWithNibNamed:@"MessageCells" bundle:nil]; |
| 110 | [conversationView registerNib:cellNib forIdentifier:@"LeftIncomingFileView"]; |
| 111 | [conversationView registerNib:cellNib forIdentifier:@"LeftOngoingFileView"]; |
| 112 | [conversationView registerNib:cellNib forIdentifier:@"LeftFinishedFileView"]; |
| 113 | [conversationView registerNib:cellNib forIdentifier:@"RightOngoingFileView"]; |
| 114 | [conversationView registerNib:cellNib forIdentifier:@"RightFinishedFileView"]; |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 115 | [conversationView registerNib:cellNib forIdentifier:@"PeerComposingMsgView"]; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 116 | [[conversationView.enclosingScrollView contentView] setCopiesOnScroll:NO]; |
| 117 | [messageField setFocusRingType:NSFocusRingTypeNone]; |
| 118 | [conversationView setWantsLayer:YES]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 119 | } |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 120 | |
| 121 | - (instancetype)initWithCoder:(NSCoder *)coder |
| 122 | { |
| 123 | self = [super initWithCoder:coder]; |
| 124 | if (self) { |
| 125 | pendingMessagesToSend = [[NSMutableDictionary alloc] init]; |
| 126 | } |
| 127 | return self; |
| 128 | } |
| 129 | |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 130 | -(void) setAVModel: (lrc::api::AVModel*) avmodel { |
| 131 | avModel = avmodel; |
| 132 | if (recordingController == nil) { |
| 133 | recordingController = [[RecordFileVC alloc] initWithNibName:@"RecordFileVC" bundle:nil avModel: self->avModel]; |
| 134 | recordingController.delegate = self; |
| 135 | } |
| 136 | } |
| 137 | |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 138 | - (void)setMessage:(NSString *)newValue { |
| 139 | _message = [newValue removeEmptyLinesAtBorders]; |
| 140 | } |
| 141 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 142 | -(void) clearData { |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 143 | if (!convUid_.isEmpty()) { |
| 144 | pendingMessagesToSend[convUid_.toNSString()] = messageField.stringValue; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 145 | } |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 146 | cachedConv_ = nil; |
| 147 | convUid_ = ""; |
| 148 | convModel_ = nil; |
| 149 | |
| 150 | QObject::disconnect(modelSortedSignal_); |
| 151 | QObject::disconnect(filterChangedSignal_); |
| 152 | QObject::disconnect(interactionStatusUpdatedSignal_); |
| 153 | QObject::disconnect(newInteractionSignal_); |
Kateryna Kostiuk | 5acaefd | 2020-03-25 11:14:25 -0400 | [diff] [blame] | 154 | QObject::disconnect(peerComposingMsgSignal_); |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 155 | [self closeRecordingView]; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 156 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 157 | |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 158 | -(void) scrollToBottom { |
| 159 | CGRect visibleRect = [conversationView enclosingScrollView].contentView.visibleRect; |
| 160 | NSRange range = [conversationView rowsInRect:visibleRect]; |
| 161 | NSIndexSet* visibleIndexes = [NSIndexSet indexSetWithIndexesInRange:range]; |
| 162 | NSUInteger lastvisibleRow = [visibleIndexes lastIndex]; |
| 163 | if (([conversationView numberOfRows] > 0) && |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 164 | lastvisibleRow > ([conversationView numberOfRows] - 3)) { |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 165 | [conversationView scrollToEndOfDocument:nil]; |
| 166 | } |
| 167 | } |
| 168 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 169 | -(const lrc::api::conversation::Info*) getCurrentConversation |
| 170 | { |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 171 | if (convModel_ == nil || convUid_.isEmpty()) |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 172 | return nil; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 173 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 174 | if (cachedConv_ != nil) |
| 175 | return cachedConv_; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 176 | |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 177 | auto it = getConversationFromUid(convUid_, *convModel_); |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 178 | if (it != convModel_->allFilteredConversations().end()) |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 179 | cachedConv_ = &(*it); |
| 180 | |
| 181 | return cachedConv_; |
| 182 | } |
| 183 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 184 | -(void) reloadConversationForMessage:(uint64_t) uid shouldUpdateHeight:(bool)update { |
| 185 | auto* conv = [self getCurrentConversation]; |
| 186 | |
| 187 | if (conv == nil) |
| 188 | return; |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 189 | auto it = conv->interactions.find(uid); |
| 190 | if (it == conv->interactions.end()) { |
| 191 | return; |
| 192 | } |
| 193 | auto itIndex = distance(conv->interactions.begin(),it); |
Kateryna Kostiuk | a120192 | 2020-04-20 11:59:35 -0400 | [diff] [blame] | 194 | if (itIndex >= ([conversationView numberOfRows] - 1)) { |
| 195 | return; |
| 196 | } |
Kateryna Kostiuk | f5eb58c | 2020-03-25 16:50:35 -0400 | [diff] [blame] | 197 | NSRange rangeToUpdate = NSMakeRange(itIndex, 2); |
| 198 | NSIndexSet* indexSet = [NSIndexSet indexSetWithIndexesInRange:rangeToUpdate]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 199 | //reload previous message to update bubbleview |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 200 | if (itIndex > 0) { |
| 201 | auto previousIt = it; |
| 202 | previousIt--; |
| 203 | auto previousInteraction = previousIt->second; |
| 204 | if (previousInteraction.type == lrc::api::interaction::Type::TEXT) { |
Kateryna Kostiuk | f5eb58c | 2020-03-25 16:50:35 -0400 | [diff] [blame] | 205 | NSRange range = NSMakeRange(itIndex - 1, 3); |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 206 | indexSet = [NSIndexSet indexSetWithIndexesInRange:range]; |
| 207 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 208 | } |
| 209 | if (update) { |
| 210 | [conversationView noteHeightOfRowsWithIndexesChanged:indexSet]; |
| 211 | } |
| 212 | [conversationView reloadDataForRowIndexes: indexSet |
| 213 | columnIndexes:[NSIndexSet indexSetWithIndex:0]]; |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 214 | CGRect visibleRect = [conversationView enclosingScrollView].contentView.visibleRect; |
| 215 | NSRange range = [conversationView rowsInRect:visibleRect]; |
| 216 | NSIndexSet* visibleIndexes = [NSIndexSet indexSetWithIndexesInRange:range]; |
| 217 | NSUInteger lastvisibleRow = [visibleIndexes lastIndex]; |
| 218 | if (([conversationView numberOfRows] > 0) && |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 219 | lastvisibleRow > ([conversationView numberOfRows] - 3)) { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 220 | [conversationView scrollToEndOfDocument:nil]; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | -(void) reloadConversationForMessage:(uint64_t) uid shouldUpdateHeight:(bool)update updateConversation:(bool) updateConversation { |
| 225 | auto* conv = [self getCurrentConversation]; |
| 226 | |
| 227 | if (conv == nil) |
| 228 | return; |
| 229 | auto it = distance(conv->interactions.begin(),conv->interactions.find(uid)); |
| 230 | NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:it]; |
| 231 | //reload previous message to update bubbleview |
| 232 | if (it > 0) { |
| 233 | NSRange range = NSMakeRange(it - 1, it); |
| 234 | indexSet = [NSIndexSet indexSetWithIndexesInRange:range]; |
| 235 | } |
| 236 | if (update) { |
| 237 | [conversationView noteHeightOfRowsWithIndexesChanged:indexSet]; |
| 238 | } |
| 239 | [conversationView reloadDataForRowIndexes: indexSet |
| 240 | columnIndexes:[NSIndexSet indexSetWithIndex:0]]; |
| 241 | if (update) { |
| 242 | [conversationView scrollToEndOfDocument:nil]; |
| 243 | } |
| 244 | } |
| 245 | |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 246 | -(void)setConversationUid:(const QString&)convUid model:(lrc::api::ConversationModel *)model |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 247 | { |
| 248 | if (convUid_ == convUid && convModel_ == model) |
| 249 | return; |
| 250 | |
| 251 | cachedConv_ = nil; |
| 252 | convUid_ = convUid; |
| 253 | convModel_ = model; |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 254 | peerComposingMessage = false; |
| 255 | composingMessage = false; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 256 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 257 | // Signal triggered when messages are received or their status updated |
| 258 | QObject::disconnect(newInteractionSignal_); |
| 259 | QObject::disconnect(interactionStatusUpdatedSignal_); |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 260 | QObject::disconnect(peerComposingMsgSignal_); |
| 261 | |
| 262 | peerComposingMsgSignal_ = QObject::connect(convModel_, |
| 263 | &lrc::api::ConversationModel::composingStatusChanged, |
| 264 | [self](const QString &uid, |
| 265 | const QString &contactUri, |
| 266 | bool isComposing) { |
| 267 | if (uid != convUid_) |
| 268 | return; |
| 269 | bool shouldUpdate = isComposing != peerComposingMessage; |
| 270 | if (!shouldUpdate) { |
| 271 | return; |
| 272 | } |
| 273 | // reload and update height for composing indicator |
| 274 | peerComposingMessage = isComposing; |
| 275 | auto* conv = [self getCurrentConversation]; |
| 276 | if (conv == nil) |
| 277 | return; |
| 278 | auto row = [conversationView numberOfRows] - 1; |
| 279 | if (row < 0) { |
| 280 | return; |
| 281 | } |
| 282 | NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:row]; |
Kateryna Kostiuk | f5eb58c | 2020-03-25 16:50:35 -0400 | [diff] [blame] | 283 | if(peerComposingMessage) { |
| 284 | [conversationView noteHeightOfRowsWithIndexesChanged:indexSet]; |
| 285 | } else { |
| 286 | //whait for possible incoming message to avoid view jumping |
| 287 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ |
| 288 | if (!peerComposingMessage) { |
| 289 | [conversationView noteHeightOfRowsWithIndexesChanged:indexSet]; |
| 290 | } |
| 291 | }); |
| 292 | } |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 293 | [conversationView reloadDataForRowIndexes: indexSet |
| 294 | columnIndexes:[NSIndexSet indexSetWithIndex:0]]; |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 295 | CGRect visibleRect = [conversationView enclosingScrollView].contentView.visibleRect; |
| 296 | NSRange range = [conversationView rowsInRect:visibleRect]; |
| 297 | NSIndexSet* visibleIndexes = [NSIndexSet indexSetWithIndexesInRange:range]; |
| 298 | NSUInteger lastvisibleRow = [visibleIndexes lastIndex]; |
| 299 | if (([conversationView numberOfRows] > 0) && |
| 300 | lastvisibleRow > ([conversationView numberOfRows] - 3)) { |
| 301 | [conversationView scrollToEndOfDocument:nil]; |
| 302 | } |
| 303 | }); |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 304 | newInteractionSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newInteraction, |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 305 | [self](const QString& uid, uint64_t interactionId, const lrc::api::interaction::Info& interaction){ |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 306 | if (uid != convUid_) |
| 307 | return; |
| 308 | cachedConv_ = nil; |
Kateryna Kostiuk | f5eb58c | 2020-03-25 16:50:35 -0400 | [diff] [blame] | 309 | peerComposingMessage = false; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 310 | [conversationView noteNumberOfRowsChanged]; |
| 311 | [self reloadConversationForMessage:interactionId shouldUpdateHeight:YES]; |
| 312 | }); |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 313 | interactionStatusUpdatedSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::interactionStatusUpdated, |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 314 | [self](const QString& uid, uint64_t interactionId, const lrc::api::interaction::Info& interaction){ |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 315 | if (uid != convUid_) |
| 316 | return; |
| 317 | cachedConv_ = nil; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 318 | bool isOutgoing = lrc::api::interaction::isOutgoing(interaction); |
| 319 | if (interaction.type == lrc::api::interaction::Type::TEXT && isOutgoing) { |
| 320 | convModel_->refreshFilter(); |
| 321 | } |
| 322 | [self reloadConversationForMessage:interactionId shouldUpdateHeight:YES]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 323 | }); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 324 | |
| 325 | // Signals tracking changes in conversation list, we need them as cached conversation can be invalid |
| 326 | // after a reordering. |
| 327 | QObject::disconnect(modelSortedSignal_); |
| 328 | QObject::disconnect(filterChangedSignal_); |
| 329 | modelSortedSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted, |
| 330 | [self](){ |
| 331 | cachedConv_ = nil; |
| 332 | }); |
| 333 | filterChangedSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged, |
| 334 | [self](){ |
| 335 | cachedConv_ = nil; |
| 336 | }); |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 337 | if (pendingMessagesToSend[convUid_.toNSString()]) { |
| 338 | self.message = pendingMessagesToSend[convUid_.toNSString()]; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 339 | [self updateSendMessageHeight]; |
| 340 | } else { |
| 341 | self.message = @""; |
| 342 | if(messagesBottomMargin.constant != SEND_PANEL_DEFAULT_HEIGHT) { |
| 343 | sendPanelHeight.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 344 | messagesBottomMargin.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 345 | [self scrollToBottom]; |
| 346 | } |
| 347 | } |
| 348 | conversationView.alphaValue = 0.0; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 349 | [conversationView reloadData]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 350 | [conversationView scrollToEndOfDocument:nil]; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 351 | CABasicAnimation *fadeIn = [CABasicAnimation animationWithKeyPath:@"opacity"]; |
| 352 | fadeIn.fromValue = [NSNumber numberWithFloat:0.0]; |
| 353 | fadeIn.toValue = [NSNumber numberWithFloat:1.0]; |
| 354 | fadeIn.duration = 0.4f; |
| 355 | |
| 356 | [conversationView.layer addAnimation:fadeIn forKey:fadeIn.keyPath]; |
| 357 | conversationView.alphaValue = 1; |
| 358 | auto* conv = [self getCurrentConversation]; |
| 359 | |
| 360 | if (conv == nil) |
| 361 | return; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 362 | try { |
| 363 | [sendFileButton setEnabled:(convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type != lrc::api::profile::Type::SIP)]; |
| 364 | } catch (std::out_of_range& e) { |
| 365 | NSLog(@"contact out of range"); |
| 366 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 367 | } |
| 368 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 369 | #pragma mark - configure cells |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 370 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 371 | -(NSTableCellView*) makeGenericInteractionViewForTableView:(NSTableView*)tableView withText:(NSString*)text andTime:(NSString*) time |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 372 | { |
| 373 | NSTableCellView* result = [tableView makeViewWithIdentifier:@"GenericInteractionView" owner:self]; |
| 374 | NSTextField* textField = [result viewWithTag:GENERIC_INT_TEXT_TAG]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 375 | NSTextField* timeField = [result viewWithTag:GENERIC_INT_TIME_TAG]; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 376 | |
| 377 | // TODO: Fix symbol in LRC |
Kateryna Kostiuk | 6640643 | 2019-11-09 17:20:34 -0500 | [diff] [blame] | 378 | NSString* fixedString = [[text stringByReplacingOccurrencesOfString:@"🕽" withString:@""] stringByReplacingOccurrencesOfString:@"📞" withString:@""]; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 379 | [textField setStringValue:fixedString]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 380 | [timeField setStringValue:time]; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 381 | |
| 382 | return result; |
| 383 | } |
| 384 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 385 | -(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] | 386 | { |
| 387 | IMTableCellView* result; |
| 388 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 389 | auto type = interaction.type; |
| 390 | auto status = interaction.status; |
| 391 | |
| 392 | NSString* fileName = @"incoming file"; |
| 393 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 394 | // First, view is created |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 395 | if (!interaction.authorUri.isEmpty()) { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 396 | switch (status) { |
| 397 | case lrc::api::interaction::Status::TRANSFER_CREATED: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 398 | case lrc::api::interaction::Status::TRANSFER_AWAITING_HOST: { |
| 399 | result = [tableView makeViewWithIdentifier:@"LeftIncomingFileView" owner: conversationView]; |
| 400 | [result.acceptButton setAction:@selector(acceptIncomingFile:)]; |
| 401 | [result.acceptButton setTarget:self]; |
| 402 | [result.declineButton setAction:@selector(declineIncomingFile:)]; |
| 403 | [result.declineButton setTarget:self]; |
| 404 | break;} |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 405 | case lrc::api::interaction::Status::TRANSFER_ACCEPTED: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 406 | case lrc::api::interaction::Status::TRANSFER_ONGOING: { |
| 407 | result = [tableView makeViewWithIdentifier:@"LeftOngoingFileView" owner:conversationView]; |
| 408 | [result.progressIndicator startAnimation:conversationView]; |
| 409 | [result.declineButton setAction:@selector(declineIncomingFile:)]; |
| 410 | [result.declineButton setTarget:self]; |
| 411 | break;} |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 412 | case lrc::api::interaction::Status::TRANSFER_FINISHED: |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 413 | result = [tableView makeViewWithIdentifier:@"LeftFinishedFileView" owner:conversationView]; |
| 414 | [result.transferedFileName setAction:@selector(imagePreview:)]; |
| 415 | [result.transferedFileName setTarget:self]; |
| 416 | [result.transferedFileName.cell setHighlightsBy:NSContentsCellMask]; |
| 417 | break; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 418 | case lrc::api::interaction::Status::TRANSFER_CANCELED: |
| 419 | case lrc::api::interaction::Status::TRANSFER_ERROR: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 420 | result = [tableView makeViewWithIdentifier:@"LeftFinishedFileView" owner:conversationView]; |
| 421 | break; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 422 | } |
Kateryna Kostiuk | 209a630 | 2019-08-14 16:46:21 -0400 | [diff] [blame] | 423 | } else { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 424 | NSString* fileName = @"sent file"; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 425 | switch (status) { |
| 426 | case lrc::api::interaction::Status::TRANSFER_CREATED: |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 427 | case lrc::api::interaction::Status::TRANSFER_ONGOING: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 428 | case lrc::api::interaction::Status::TRANSFER_AWAITING_PEER: |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 429 | case lrc::api::interaction::Status::TRANSFER_ACCEPTED: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 430 | result = [tableView makeViewWithIdentifier:@"RightOngoingFileView" owner:conversationView]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 431 | [result.progressIndicator startAnimation:nil]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 432 | [result.declineButton setAction:@selector(declineIncomingFile:)]; |
| 433 | [result.declineButton setTarget:self]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 434 | break; |
| 435 | case lrc::api::interaction::Status::TRANSFER_FINISHED: |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 436 | result = [tableView makeViewWithIdentifier:@"RightFinishedFileView" owner:conversationView]; |
| 437 | [result.transferedFileName setAction:@selector(imagePreview:)]; |
| 438 | [result.transferedFileName setTarget:self]; |
| 439 | [result.transferedFileName.cell setHighlightsBy:NSContentsCellMask]; |
| 440 | break; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 441 | case lrc::api::interaction::Status::TRANSFER_CANCELED: |
| 442 | case lrc::api::interaction::Status::TRANSFER_ERROR: |
Olivier Soldano | e521a18 | 2018-02-26 16:55:19 -0500 | [diff] [blame] | 443 | case lrc::api::interaction::Status::TRANSFER_UNJOINABLE_PEER: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 444 | result = [tableView makeViewWithIdentifier:@"RightFinishedFileView" owner:conversationView]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
| 448 | // Then status label is updated if needed |
| 449 | switch (status) { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 450 | [result.statusLabel setTextColor:[NSColor textColor]]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 451 | case lrc::api::interaction::Status::TRANSFER_FINISHED: |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 452 | [result.statusLabel setTextColor:[NSColor greenSuccessColor]]; |
Anthony Léonard | 70638f0 | 2018-02-05 11:10:19 -0500 | [diff] [blame] | 453 | [result.statusLabel setStringValue:NSLocalizedString(@"Success", @"File transfer successful label")]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 454 | break; |
| 455 | case lrc::api::interaction::Status::TRANSFER_CANCELED: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 456 | [result.statusLabel setTextColor:[NSColor orangeColor]]; |
Anthony Léonard | 70638f0 | 2018-02-05 11:10:19 -0500 | [diff] [blame] | 457 | [result.statusLabel setStringValue:NSLocalizedString(@"Canceled", @"File transfer canceled label")]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 458 | break; |
| 459 | case lrc::api::interaction::Status::TRANSFER_ERROR: |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 460 | [result.statusLabel setTextColor:[NSColor errorTransferColor]]; |
Anthony Léonard | 70638f0 | 2018-02-05 11:10:19 -0500 | [diff] [blame] | 461 | [result.statusLabel setStringValue:NSLocalizedString(@"Failed", @"File transfer failed label")]; |
Olivier Soldano | e521a18 | 2018-02-26 16:55:19 -0500 | [diff] [blame] | 462 | break; |
| 463 | case lrc::api::interaction::Status::TRANSFER_UNJOINABLE_PEER: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 464 | [result.statusLabel setTextColor:[NSColor textColor]]; |
Olivier Soldano | e521a18 | 2018-02-26 16:55:19 -0500 | [diff] [blame] | 465 | [result.statusLabel setStringValue:NSLocalizedString(@"Unjoinable", @"File transfer peer unjoinable label")]; |
| 466 | break; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 467 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 468 | result.transferedImage.image = nil; |
Kateryna Kostiuk | eaf1bc8 | 2018-10-12 14:33:50 -0400 | [diff] [blame] | 469 | [result.openImagebutton setHidden:YES]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 470 | [result.msgBackground setHidden:NO]; |
| 471 | [result invalidateImageConstraints]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 472 | NSString* name = interaction.body.toNSString(); |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 473 | if (name.length > 0) { |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 474 | fileName = [name lastPathComponent]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 475 | } |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 476 | NSFont *nameFont = [NSFont userFontOfSize:14.0]; |
| 477 | NSColor *nameColor = [NSColor textColor]; |
| 478 | NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; |
| 479 | paragraphStyle.lineBreakMode = NSLineBreakByTruncatingTail; |
| 480 | paragraphStyle.alignment = NSTextAlignmentLeft; |
| 481 | NSDictionary *nameAttr = [NSDictionary dictionaryWithObjectsAndKeys:nameFont,NSFontAttributeName, |
| 482 | nameColor,NSForegroundColorAttributeName, |
| 483 | paragraphStyle,NSParagraphStyleAttributeName, nil]; |
| 484 | NSAttributedString* nameAttributedString = [[NSAttributedString alloc] initWithString:fileName attributes:nameAttr]; |
| 485 | result.transferedFileName.attributedTitle = nameAttributedString; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 486 | if (status == lrc::api::interaction::Status::TRANSFER_FINISHED) { |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 487 | NSColor *higlightColor = [NSColor grayColor]; |
| 488 | NSDictionary *alternativeNametAttr = [NSDictionary dictionaryWithObjectsAndKeys:nameFont,NSFontAttributeName, |
| 489 | higlightColor,NSForegroundColorAttributeName, |
| 490 | paragraphStyle,NSParagraphStyleAttributeName, nil]; |
| 491 | NSAttributedString* alternativeString = [[NSAttributedString alloc] initWithString:fileName attributes:alternativeNametAttr]; |
| 492 | result.transferedFileName.attributedAlternateTitle = alternativeString; |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 493 | NSImage* image = [self getImageForFilePath:name]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 494 | if (([name rangeOfString:@"/"].location == NSNotFound)) { |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 495 | image = [self getImageForFilePath:[self getDataTransferPath:interactionID]]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 496 | } |
| 497 | if(image != nil) { |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 498 | result.transferedImage.image = image; |
| 499 | [result updateImageConstraintWithMax: MAX_TRANSFERED_IMAGE_SIZE]; |
| 500 | [result.openImagebutton setAction:@selector(imagePreview:)]; |
| 501 | [result.openImagebutton setTarget:self]; |
Kateryna Kostiuk | eaf1bc8 | 2018-10-12 14:33:50 -0400 | [diff] [blame] | 502 | [result.openImagebutton setHidden:NO]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | [result setupForInteraction:interactionID]; |
| 506 | NSDate* msgTime = [NSDate dateWithTimeIntervalSince1970:interaction.timestamp]; |
| 507 | NSString* timeString = [self timeForMessage: msgTime]; |
| 508 | result.timeLabel.stringValue = timeString; |
| 509 | bool isOutgoing = lrc::api::interaction::isOutgoing(interaction); |
| 510 | if (!isOutgoing) { |
| 511 | auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator()); |
| 512 | auto* conv = [self getCurrentConversation]; |
| 513 | [result.photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(*conv, convModel_->owner)))]; |
| 514 | } |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 515 | return result; |
| 516 | } |
| 517 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 518 | #pragma mark - NSTableViewDelegate methods |
| 519 | - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 520 | { |
| 521 | return YES; |
| 522 | } |
| 523 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 524 | - (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 525 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 526 | return NO; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 527 | } |
| 528 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 529 | - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 530 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 531 | auto* conv = [self getCurrentConversation]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 532 | if (conv == nil) |
| 533 | return nil; |
| 534 | |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 535 | IMTableCellView* result; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 536 | auto it = conv->interactions.begin(); |
Kateryna Kostiuk | a120192 | 2020-04-20 11:59:35 -0400 | [diff] [blame] | 537 | auto size = [conversationView numberOfRows] - 1; |
Kateryna Kostiuk | 0c06855 | 2020-03-30 09:48:17 -0400 | [diff] [blame] | 538 | if (row > size) { |
| 539 | return [[NSView alloc] init]; |
| 540 | } |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 541 | |
| 542 | if (row == size) { |
Kateryna Kostiuk | b39ca19 | 2020-03-27 14:01:00 -0400 | [diff] [blame] | 543 | if (size < 1) { |
| 544 | return [[NSView alloc] init]; |
| 545 | } |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 546 | //last row peer composing view |
| 547 | result = [tableView makeViewWithIdentifier:@"PeerComposingMsgView" owner:conversationView]; |
Kateryna Kostiuk | f5eb58c | 2020-03-25 16:50:35 -0400 | [diff] [blame] | 548 | std::advance(it, row-1); |
Kateryna Kostiuk | 0c06855 | 2020-03-30 09:48:17 -0400 | [diff] [blame] | 549 | if (it != conv->interactions.end()) { |
| 550 | //if previous message not from peer display avatar |
| 551 | auto interaction = it->second; |
| 552 | bool isOutgoing = lrc::api::interaction::isOutgoing(interaction); |
| 553 | [result.photoView setHidden: YES]; |
| 554 | if(isOutgoing) { |
| 555 | auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator()); |
| 556 | auto* conv = [self getCurrentConversation]; |
| 557 | [result.photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(*conv, convModel_->owner)))]; |
| 558 | [result.photoView setHidden: NO]; |
| 559 | } |
Kateryna Kostiuk | f5eb58c | 2020-03-25 16:50:35 -0400 | [diff] [blame] | 560 | } |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 561 | CGFloat alpha = peerComposingMessage ? 1 : 0; |
| 562 | result.alphaValue = 0; |
| 563 | [result animateCompozingIndicator: NO]; |
| 564 | if (alpha == 1) { |
| 565 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ |
| 566 | if (peerComposingMessage) { |
| 567 | result.alphaValue = alpha; |
| 568 | [result animateCompozingIndicator: YES]; |
| 569 | } |
| 570 | }); |
| 571 | } |
| 572 | return result; |
| 573 | } |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 574 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 575 | std::advance(it, row); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 576 | |
Kateryna Kostiuk | 0c06855 | 2020-03-30 09:48:17 -0400 | [diff] [blame] | 577 | if (it == conv->interactions.end()) { |
| 578 | return [[NSView alloc] init]; |
| 579 | } |
| 580 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 581 | auto interaction = it->second; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 582 | bool isOutgoing = lrc::api::interaction::isOutgoing(interaction); |
| 583 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 584 | switch (interaction.type) { |
| 585 | case lrc::api::interaction::Type::TEXT: |
| 586 | if (isOutgoing) { |
| 587 | result = [tableView makeViewWithIdentifier:@"RightMessageView" owner:self]; |
| 588 | } else { |
| 589 | result = [tableView makeViewWithIdentifier:@"LeftMessageView" owner:self]; |
| 590 | } |
| 591 | break; |
Kateryna Kostiuk | 209a630 | 2019-08-14 16:46:21 -0400 | [diff] [blame] | 592 | case lrc::api::interaction::Type::DATA_TRANSFER: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 593 | return [self configureViewforTransfer:interaction interactionID: it->first tableView:tableView]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 594 | break; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 595 | case lrc::api::interaction::Type::CONTACT: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 596 | case lrc::api::interaction::Type::CALL: { |
| 597 | NSDate* msgTime = [NSDate dateWithTimeIntervalSince1970:interaction.timestamp]; |
| 598 | NSString* timeString = [self timeForMessage: msgTime]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 599 | return [self makeGenericInteractionViewForTableView:tableView withText:interaction.body.toNSString() andTime:timeString]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 600 | } |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 601 | default: // If interaction is not of a known type |
| 602 | return nil; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 603 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 604 | MessageSequencing sequence = [self computeSequencingFor:row]; |
| 605 | BubbleType type = SINGLE; |
| 606 | if (sequence == FIRST_WITHOUT_TIME || sequence == FIRST_WITH_TIME) { |
| 607 | type = FIRST; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 608 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 609 | if (sequence == MIDDLE_IN_SEQUENCE) { |
| 610 | type = MIDDLE; |
| 611 | } |
| 612 | if (sequence == LAST_IN_SEQUENCE) { |
| 613 | type = LAST; |
| 614 | } |
| 615 | result.msgBackground.type = type; |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 616 | bool sendingFail = false; |
| 617 | [result.messageStatus setHidden:YES]; |
| 618 | if (interaction.type == lrc::api::interaction::Type::TEXT && isOutgoing) { |
| 619 | if (interaction.status == lrc::api::interaction::Status::SENDING) { |
| 620 | [result.messageStatus setHidden:NO]; |
| 621 | [result.sendingMessageIndicator startAnimation:nil]; |
| 622 | [result.messageFailed setHidden:YES]; |
Kateryna Kostiuk | 209a630 | 2019-08-14 16:46:21 -0400 | [diff] [blame] | 623 | } else if (interaction.status == lrc::api::interaction::Status::FAILURE) { |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 624 | [result.messageStatus setHidden:NO]; |
| 625 | [result.sendingMessageIndicator setHidden:YES]; |
| 626 | [result.messageFailed setHidden:NO]; |
| 627 | sendingFail = true; |
| 628 | } |
| 629 | } |
| 630 | [result setupForInteraction:it->first isFailed: sendingFail]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 631 | bool shouldDisplayTime = (sequence == FIRST_WITH_TIME || sequence == SINGLE_WITH_TIME) ? YES : NO; |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 632 | bool shouldApplyPadding = (sequence == FIRST_WITHOUT_TIME || sequence == SINGLE_WITHOUT_TIME) ? YES : NO; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 633 | [result.msgBackground setNeedsDisplay:YES]; |
| 634 | [result setNeedsDisplay:YES]; |
| 635 | [result.timeBox setNeedsDisplay:YES]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 636 | |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 637 | NSString *text = interaction.body.toNSString(); |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 638 | text = [text removeEmptyLinesAtBorders]; |
| 639 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 640 | NSMutableAttributedString* msgAttString = |
Kateryna Kostiuk | 5a41a8d | 2020-05-13 09:05:56 -0400 | [diff] [blame^] | 641 | [[NSMutableAttributedString alloc] initWithString:text |
Kateryna Kostiuk | 3308987 | 2017-07-14 16:43:59 -0400 | [diff] [blame] | 642 | attributes:[self messageAttributes]]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 643 | |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 644 | CGSize messageSize = [self sizeFor: text maxWidth:tableView.frame.size.width * 0.7]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 645 | |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 646 | [result updateMessageConstraint:messageSize.width andHeight:messageSize.height timeIsVisible:shouldDisplayTime isTopPadding: shouldApplyPadding]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 647 | [[result.msgView textStorage] appendAttributedString:msgAttString]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 648 | |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 649 | NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil]; |
| 650 | NSArray *matches = [linkDetector matchesInString:result.msgView.string options:0 range:NSMakeRange(0, result.msgView.string.length)]; |
| 651 | |
| 652 | [result.msgView.textStorage beginEditing]; |
| 653 | |
| 654 | for (NSTextCheckingResult *match in matches) { |
| 655 | if (!match.URL) continue; |
| 656 | |
| 657 | NSDictionary *linkAttributes = @{ |
| 658 | NSLinkAttributeName: match.URL, |
| 659 | }; |
| 660 | [result.msgView.textStorage addAttributes:linkAttributes range:match.range]; |
| 661 | } |
| 662 | |
| 663 | [result.msgView.textStorage endEditing]; |
| 664 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 665 | if (shouldDisplayTime) { |
| 666 | NSDate* msgTime = [NSDate dateWithTimeIntervalSince1970:interaction.timestamp]; |
| 667 | NSString* timeString = [self timeForMessage: msgTime]; |
| 668 | result.timeLabel.stringValue = timeString; |
Anthony Léonard | 64e1967 | 2018-01-18 16:40:34 -0500 | [diff] [blame] | 669 | } |
| 670 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 671 | bool shouldDisplayAvatar = (sequence != MIDDLE_IN_SEQUENCE && sequence != FIRST_WITHOUT_TIME |
| 672 | && sequence != FIRST_WITH_TIME) ? YES : NO; |
| 673 | [result.photoView setHidden:!shouldDisplayAvatar]; |
| 674 | if (!isOutgoing && shouldDisplayAvatar) { |
| 675 | auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator()); |
| 676 | [result.photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(*conv, convModel_->owner)))]; |
| 677 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 678 | return result; |
| 679 | } |
| 680 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 681 | - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 682 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 683 | double someWidth = tableView.frame.size.width * 0.7; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 684 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 685 | auto* conv = [self getCurrentConversation]; |
| 686 | |
| 687 | if (conv == nil) |
Kateryna Kostiuk | 0c06855 | 2020-03-30 09:48:17 -0400 | [diff] [blame] | 688 | return HEIGHT_DEFAULT; |
| 689 | |
Kateryna Kostiuk | a120192 | 2020-04-20 11:59:35 -0400 | [diff] [blame] | 690 | auto size = [conversationView numberOfRows] - 1; |
| 691 | if (row >= size) { |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 692 | //last item peer composing view |
| 693 | if (peerComposingMessage) { |
| 694 | return HEIGHT_FOR_COMPOSING_INDICATOR; |
| 695 | } |
Kateryna Kostiuk | a120192 | 2020-04-20 11:59:35 -0400 | [diff] [blame] | 696 | return 1; |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 697 | } |
| 698 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 699 | auto it = conv->interactions.begin(); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 700 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 701 | std::advance(it, row); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 702 | |
Kateryna Kostiuk | 0c06855 | 2020-03-30 09:48:17 -0400 | [diff] [blame] | 703 | if (it == conv->interactions.end()) { |
| 704 | return HEIGHT_DEFAULT; |
| 705 | } |
| 706 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 707 | auto interaction = it->second; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 708 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 709 | MessageSequencing sequence = [self computeSequencingFor:row]; |
| 710 | |
| 711 | bool shouldDisplayTime = (sequence == FIRST_WITH_TIME || sequence == SINGLE_WITH_TIME) ? YES : NO; |
| 712 | |
| 713 | |
Kateryna Kostiuk | 209a630 | 2019-08-14 16:46:21 -0400 | [diff] [blame] | 714 | if(interaction.type == lrc::api::interaction::Type::DATA_TRANSFER) { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 715 | |
| 716 | if( interaction.status == lrc::api::interaction::Status::TRANSFER_FINISHED) { |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 717 | NSString* name = interaction.body.toNSString(); |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 718 | NSImage* image = [self getImageForFilePath:name]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 719 | if (([name rangeOfString:@"/"].location == NSNotFound)) { |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 720 | image = [self getImageForFilePath:[self getDataTransferPath:it->first]]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 721 | } |
| 722 | if (image != nil) { |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 723 | CGFloat widthScaleFactor = MAX_TRANSFERED_IMAGE_SIZE / image.size.width; |
| 724 | CGFloat heightScaleFactor = MAX_TRANSFERED_IMAGE_SIZE / image.size.height; |
| 725 | CGFloat heigt = 0; |
| 726 | if((widthScaleFactor >= 1) && (heightScaleFactor >= 1)) { |
| 727 | heigt = image.size.height; |
| 728 | } else { |
| 729 | CGFloat scale = MIN(widthScaleFactor, heightScaleFactor); |
| 730 | heigt = image.size.height * scale; |
| 731 | } |
| 732 | return heigt + TIME_BOX_HEIGHT; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 733 | } |
| 734 | } |
| 735 | return BUBBLE_HEIGHT_FOR_TRANSFERED_FILE + TIME_BOX_HEIGHT; |
| 736 | } |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 737 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 738 | 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] | 739 | return GENERIC_CELL_HEIGHT; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 740 | |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 741 | NSString *text = interaction.body.toNSString(); |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 742 | text = [text removeEmptyLinesAtBorders]; |
| 743 | |
| 744 | CGSize messageSize = [self sizeFor: text maxWidth:tableView.frame.size.width * 0.7]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 745 | CGFloat singleLignMessageHeight = 15; |
| 746 | |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 747 | bool shouldApplyPadding = (sequence == FIRST_WITHOUT_TIME || sequence == SINGLE_WITHOUT_TIME) ? YES : NO; |
| 748 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 749 | if (shouldDisplayTime) { |
| 750 | return MAX(messageSize.height + TIME_BOX_HEIGHT + MESSAGE_TEXT_PADDING * 2, |
| 751 | TIME_BOX_HEIGHT + MESSAGE_TEXT_PADDING * 2 + singleLignMessageHeight); |
| 752 | } |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 753 | if(shouldApplyPadding) { |
| 754 | return MAX(messageSize.height + MESSAGE_TEXT_PADDING * 2 + 15, |
| 755 | singleLignMessageHeight + MESSAGE_TEXT_PADDING * 2 + 15); |
| 756 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 757 | return MAX(messageSize.height + MESSAGE_TEXT_PADDING * 2, |
| 758 | singleLignMessageHeight + MESSAGE_TEXT_PADDING * 2); |
| 759 | } |
| 760 | |
| 761 | #pragma mark - message view parameters |
| 762 | |
| 763 | -(NSString *) getDataTransferPath:(uint64_t)interactionId { |
| 764 | lrc::api::datatransfer::Info info = {}; |
| 765 | convModel_->getTransferInfo(interactionId, info); |
| 766 | double convertData = static_cast<double>(info.totalSize); |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 767 | return info.path.toNSString(); |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 768 | } |
| 769 | |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 770 | -(NSImage*) getImageForFilePath: (NSString *) path { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 771 | if (path.length <= 0) {return nil;} |
| 772 | if (![[NSFileManager defaultManager] fileExistsAtPath: path]) {return nil;} |
| 773 | NSImage* transferedImage = [[NSImage alloc] initWithContentsOfFile: path]; |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 774 | return transferedImage; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 775 | } |
| 776 | |
| 777 | -(CGSize) sizeFor:(NSString *) message maxWidth:(CGFloat) width { |
| 778 | CGFloat horizaontalMargin = 6; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 779 | NSMutableAttributedString* msgAttString = |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 780 | [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", message] |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 781 | attributes:[self messageAttributes]]; |
| 782 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 783 | CGFloat finalWidth = MIN(msgAttString.size.width + horizaontalMargin * 2, width); |
| 784 | NSRect frame = NSMakeRect(0, 0, finalWidth, msgAttString.size.height); |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 785 | NSTextView *tv = [[NSTextView alloc] initWithFrame:frame]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 786 | [[tv textStorage] setAttributedString:msgAttString]; |
| 787 | [tv sizeToFit]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 788 | return tv.frame.size; |
| 789 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 790 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 791 | -(MessageSequencing) computeSequencingFor:(NSInteger) row { |
Kateryna Kostiuk | cd9397f | 2020-04-13 09:40:53 -0400 | [diff] [blame] | 792 | if (row >= conversationView.numberOfRows - 1) { |
| 793 | return SINGLE_WITHOUT_TIME; |
| 794 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 795 | auto* conv = [self getCurrentConversation]; |
| 796 | if (conv == nil) |
Kateryna Kostiuk | cd9397f | 2020-04-13 09:40:53 -0400 | [diff] [blame] | 797 | return SINGLE_WITHOUT_TIME; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 798 | auto it = conv->interactions.begin(); |
| 799 | std::advance(it, row); |
Kateryna Kostiuk | 0c06855 | 2020-03-30 09:48:17 -0400 | [diff] [blame] | 800 | if (it == conv->interactions.end()) { |
| 801 | return SINGLE_WITHOUT_TIME; |
| 802 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 803 | auto interaction = it->second; |
| 804 | if (interaction.type != lrc::api::interaction::Type::TEXT) { |
| 805 | return SINGLE_WITH_TIME; |
| 806 | } |
Kateryna Kostiuk | cd9397f | 2020-04-13 09:40:53 -0400 | [diff] [blame] | 807 | // first message in comversation |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 808 | if (row == 0) { |
| 809 | if (it == conv->interactions.end()) { |
| 810 | return SINGLE_WITH_TIME; |
| 811 | } |
| 812 | auto nextIt = it; |
| 813 | nextIt++; |
Kateryna Kostiuk | cd9397f | 2020-04-13 09:40:53 -0400 | [diff] [blame] | 814 | if (nextIt == conv->interactions.end()) { |
| 815 | return SINGLE_WITH_TIME; |
| 816 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 817 | auto nextInteraction = nextIt->second; |
| 818 | if ([self sequenceChangedFrom:interaction to: nextInteraction]) { |
| 819 | return SINGLE_WITH_TIME; |
| 820 | } |
| 821 | return FIRST_WITH_TIME; |
| 822 | } |
Kateryna Kostiuk | cd9397f | 2020-04-13 09:40:53 -0400 | [diff] [blame] | 823 | // last message in comversation |
| 824 | if (row == conversationView.numberOfRows - 2) { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 825 | if(it == conv->interactions.begin()) { |
| 826 | return SINGLE_WITH_TIME; |
| 827 | } |
| 828 | auto previousIt = it; |
| 829 | previousIt--; |
| 830 | auto previousInteraction = previousIt->second; |
| 831 | bool timeChanged = [self sequenceTimeChangedFrom:interaction to:previousInteraction]; |
| 832 | bool authorChanged = [self sequenceAuthorChangedFrom:interaction to:previousInteraction]; |
| 833 | if (!timeChanged && !authorChanged) { |
| 834 | return LAST_IN_SEQUENCE; |
| 835 | } |
| 836 | if (!timeChanged && authorChanged) { |
| 837 | return SINGLE_WITHOUT_TIME; |
| 838 | } |
| 839 | return SINGLE_WITH_TIME; |
| 840 | } |
Kateryna Kostiuk | cd9397f | 2020-04-13 09:40:53 -0400 | [diff] [blame] | 841 | // single message in comversation |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 842 | if(it == conv->interactions.begin() || it == conv->interactions.end()) { |
| 843 | return SINGLE_WITH_TIME; |
| 844 | } |
Kateryna Kostiuk | cd9397f | 2020-04-13 09:40:53 -0400 | [diff] [blame] | 845 | // message in the middle of conversation |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 846 | auto previousIt = it; |
| 847 | previousIt--; |
| 848 | auto previousInteraction = previousIt->second; |
| 849 | auto nextIt = it; |
| 850 | nextIt++; |
Kateryna Kostiuk | cd9397f | 2020-04-13 09:40:53 -0400 | [diff] [blame] | 851 | if (nextIt == conv->interactions.end()) { |
| 852 | return SINGLE_WITHOUT_TIME; |
| 853 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 854 | auto nextInteraction = nextIt->second; |
| 855 | |
| 856 | bool timeChanged = [self sequenceTimeChangedFrom:interaction to:previousInteraction]; |
| 857 | bool authorChanged = [self sequenceAuthorChangedFrom:interaction to:previousInteraction]; |
| 858 | bool sequenceWillChange = [self sequenceChangedFrom:interaction to: nextInteraction]; |
Kateryna Kostiuk | 209a630 | 2019-08-14 16:46:21 -0400 | [diff] [blame] | 859 | if (previousInteraction.type == lrc::api::interaction::Type::DATA_TRANSFER) { |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 860 | if(!sequenceWillChange) { |
| 861 | return FIRST_WITH_TIME; |
| 862 | } |
| 863 | return SINGLE_WITH_TIME; |
| 864 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 865 | if (!sequenceWillChange) { |
| 866 | if (!timeChanged && !authorChanged) { |
| 867 | return MIDDLE_IN_SEQUENCE; |
| 868 | } |
| 869 | if (timeChanged) { |
| 870 | return FIRST_WITH_TIME; |
| 871 | } |
| 872 | return FIRST_WITHOUT_TIME; |
| 873 | } if (!timeChanged && !authorChanged) { |
| 874 | return LAST_IN_SEQUENCE; |
| 875 | } if (timeChanged) { |
| 876 | return SINGLE_WITH_TIME; |
| 877 | } |
| 878 | return SINGLE_WITHOUT_TIME; |
| 879 | } |
| 880 | |
| 881 | -(bool) sequenceChangedFrom:(lrc::api::interaction::Info) firstInteraction to:(lrc::api::interaction::Info) secondInteraction { |
| 882 | return ([self sequenceTimeChangedFrom:firstInteraction to:secondInteraction] || [self sequenceAuthorChangedFrom:firstInteraction to:secondInteraction]); |
| 883 | } |
| 884 | |
| 885 | -(bool) sequenceTimeChangedFrom:(lrc::api::interaction::Info) firstInteraction to:(lrc::api::interaction::Info) secondInteraction { |
| 886 | bool timeChanged = NO; |
| 887 | NSDate* firstMessageTime = [NSDate dateWithTimeIntervalSince1970:firstInteraction.timestamp]; |
| 888 | NSDate* secondMessageTime = [NSDate dateWithTimeIntervalSince1970:secondInteraction.timestamp]; |
| 889 | bool hourComp = [[NSCalendar currentCalendar] compareDate:firstMessageTime toDate:secondMessageTime toUnitGranularity:NSCalendarUnitHour]; |
| 890 | bool minutComp = [[NSCalendar currentCalendar] compareDate:firstMessageTime toDate:secondMessageTime toUnitGranularity:NSCalendarUnitMinute]; |
| 891 | if(hourComp != NSOrderedSame || minutComp != NSOrderedSame) { |
| 892 | timeChanged = YES; |
| 893 | } |
| 894 | return timeChanged; |
| 895 | } |
| 896 | |
| 897 | -(bool) sequenceAuthorChangedFrom:(lrc::api::interaction::Info) firstInteraction to:(lrc::api::interaction::Info) secondInteraction { |
| 898 | bool authorChanged = YES; |
| 899 | bool isOutgoing = lrc::api::interaction::isOutgoing(firstInteraction); |
| 900 | if ((secondInteraction.type == lrc::api::interaction::Type::TEXT) && (isOutgoing == lrc::api::interaction::isOutgoing(secondInteraction))) { |
| 901 | authorChanged = NO; |
| 902 | } |
| 903 | return authorChanged; |
| 904 | } |
| 905 | |
| 906 | -(NSString *)timeForMessage:(NSDate*) msgTime { |
| 907 | NSDate *today = [NSDate date]; |
| 908 | NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; |
Kateryna Kostiuk | af6d5e2 | 2018-06-12 15:00:00 -0400 | [diff] [blame] | 909 | [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale currentLocale] localeIdentifier]]]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 910 | if ([[NSCalendar currentCalendar] compareDate:today |
| 911 | toDate:msgTime |
| 912 | toUnitGranularity:NSCalendarUnitYear]!= NSOrderedSame) { |
Kateryna Kostiuk | 6640643 | 2019-11-09 17:20:34 -0500 | [diff] [blame] | 913 | return [NSDateFormatter localizedStringFromDate:msgTime dateStyle:NSDateFormatterLongStyle timeStyle:NSDateFormatterMediumStyle]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 914 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 915 | if ([[NSCalendar currentCalendar] compareDate:today |
| 916 | toDate:msgTime |
| 917 | toUnitGranularity:NSCalendarUnitDay]!= NSOrderedSame || |
| 918 | [[NSCalendar currentCalendar] compareDate:today |
| 919 | toDate:msgTime |
| 920 | toUnitGranularity:NSCalendarUnitMonth]!= NSOrderedSame) { |
Kateryna Kostiuk | 6640643 | 2019-11-09 17:20:34 -0500 | [diff] [blame] | 921 | return [NSDateFormatter localizedStringFromDate:msgTime dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]; |
| 922 | } |
| 923 | return [NSDateFormatter localizedStringFromDate:msgTime dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterShortStyle]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 924 | } |
| 925 | |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 926 | - (void) updateSendMessageHeight { |
| 927 | NSAttributedString *msgAttString = messageField.attributedStringValue; |
| 928 | NSRect frame = NSMakeRect(0, 0, messageField.frame.size.width, msgAttString.size.height); |
| 929 | NSTextView *tv = [[NSTextView alloc] initWithFrame:frame]; |
| 930 | [[tv textStorage] setAttributedString:msgAttString]; |
| 931 | [tv sizeToFit]; |
| 932 | CGFloat height = tv.frame.size.height + MEESAGE_MARGIN * 2; |
| 933 | CGFloat newHeight = MIN(SEND_PANEL_MAX_HEIGHT, MAX(SEND_PANEL_DEFAULT_HEIGHT, height)); |
| 934 | if(messagesBottomMargin.constant == newHeight) { |
| 935 | return; |
| 936 | } |
| 937 | messagesBottomMargin.constant = newHeight; |
| 938 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.05 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ |
| 939 | [self scrollToBottom]; |
| 940 | sendPanelHeight.constant = newHeight; |
| 941 | }); |
| 942 | } |
| 943 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 944 | #pragma mark - NSTableViewDataSource |
| 945 | |
| 946 | - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView |
| 947 | { |
| 948 | auto* conv = [self getCurrentConversation]; |
| 949 | |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 950 | // return conversation +1 view for composing indicator |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 951 | if (conv) |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 952 | return conv->interactions.size() + 1; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 953 | else |
| 954 | return 0; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 955 | } |
| 956 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 957 | #pragma mark - Text formatting |
| 958 | |
Kateryna Kostiuk | 3308987 | 2017-07-14 16:43:59 -0400 | [diff] [blame] | 959 | - (NSMutableDictionary*) messageAttributes |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 960 | { |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 961 | NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 962 | attrs[NSForegroundColorAttributeName] = [NSColor labelColor]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 963 | attrs[NSParagraphStyleAttributeName] = [self paragraphStyle]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 964 | return attrs; |
| 965 | } |
| 966 | |
| 967 | - (NSParagraphStyle*) paragraphStyle |
| 968 | { |
| 969 | /* |
| 970 | The only way to instantiate an NSMutableParagraphStyle is to mutably copy an |
| 971 | NSParagraphStyle. And since we don't have an existing NSParagraphStyle available |
| 972 | to copy, we use the default one. |
| 973 | |
| 974 | The default values supplied by the default NSParagraphStyle are: |
| 975 | Alignment NSNaturalTextAlignment |
| 976 | Tab stops 12 left-aligned tabs, spaced by 28.0 points |
| 977 | Line break mode NSLineBreakByWordWrapping |
| 978 | All others 0.0 |
| 979 | */ |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 980 | NSMutableParagraphStyle* aMutableParagraphStyle = |
| 981 | [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
| 982 | [aMutableParagraphStyle setHeadIndent:1.0]; |
| 983 | [aMutableParagraphStyle setFirstLineHeadIndent:1.0]; |
| 984 | return aMutableParagraphStyle; |
| 985 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 986 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 987 | #pragma mark - Actions |
| 988 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 989 | - (void)acceptIncomingFile:(id)sender { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 990 | auto interId = [(IMTableCellView*)[[sender superview] superview] interaction]; |
| 991 | auto& inter = [self getCurrentConversation]->interactions.find(interId)->second; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 992 | if (convModel_ && !convUid_.isEmpty()) { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 993 | NSSavePanel* filePicker = [NSSavePanel savePanel]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 994 | [filePicker setNameFieldStringValue:inter.body.toNSString()]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 995 | |
| 996 | if ([filePicker runModal] == NSFileHandlingPanelOKButton) { |
| 997 | const char* fullPath = [[filePicker URL] fileSystemRepresentation]; |
| 998 | convModel_->acceptTransfer(convUid_, interId, fullPath); |
| 999 | } |
| 1000 | } |
| 1001 | } |
| 1002 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 1003 | - (void)declineIncomingFile:(id)sender { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 1004 | auto inter = [(IMTableCellView*)[[sender superview] superview] interaction]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 1005 | if (convModel_ && !convUid_.isEmpty()) { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 1006 | convModel_->cancelTransfer(convUid_, inter); |
| 1007 | } |
| 1008 | } |
| 1009 | |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 1010 | - (void)imagePreview:(id)sender { |
| 1011 | uint64_t interId; |
| 1012 | if ([[sender superview] isKindOfClass:[IMTableCellView class]]) { |
| 1013 | interId = [(IMTableCellView*)[sender superview] interaction]; |
| 1014 | } else if ([[[sender superview] superview] isKindOfClass:[IMTableCellView class]]) { |
| 1015 | interId = [(IMTableCellView*)[[sender superview] superview] interaction]; |
| 1016 | } else { |
| 1017 | return; |
| 1018 | } |
| 1019 | auto it = [self getCurrentConversation]->interactions.find(interId); |
| 1020 | if (it == [self getCurrentConversation]->interactions.end()) { |
| 1021 | return; |
| 1022 | } |
| 1023 | auto& interaction = it->second; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 1024 | NSString* name = interaction.body.toNSString(); |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 1025 | if (([name rangeOfString:@"/"].location == NSNotFound)) { |
| 1026 | name = [self getDataTransferPath:interId]; |
| 1027 | } |
| 1028 | previewImage = name; |
Kateryna Kostiuk | 30c6ac2 | 2020-05-06 17:42:59 -0400 | [diff] [blame] | 1029 | if (!previewImage || previewImage.length <= 0) { |
| 1030 | return; |
| 1031 | } |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 1032 | if ([QLPreviewPanel sharedPreviewPanelExists] && [[QLPreviewPanel sharedPreviewPanel] isVisible]) { |
| 1033 | [[QLPreviewPanel sharedPreviewPanel] orderOut:nil]; |
| 1034 | } else { |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 1035 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 1036 | [[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFront:self]; |
| 1037 | }); |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 1038 | } |
| 1039 | } |
| 1040 | |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 1041 | - (IBAction)sendMessage:(id)sender { |
| 1042 | NSString* text = self.message; |
Kateryna Kostiuk | a8b6b56 | 2019-02-01 13:26:18 -0500 | [diff] [blame] | 1043 | unichar separatorChar = NSLineSeparatorCharacter; |
| 1044 | NSString *separatorString = [NSString stringWithCharacters:&separatorChar length:1]; |
| 1045 | text = [text stringByReplacingOccurrencesOfString: separatorString withString: @"\n"]; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 1046 | if (text && text.length > 0) { |
| 1047 | auto* conv = [self getCurrentConversation]; |
Kateryna Kostiuk | 0c06855 | 2020-03-30 09:48:17 -0400 | [diff] [blame] | 1048 | if (conv == nil) |
| 1049 | return; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 1050 | convModel_->sendMessage(convUid_, QString::fromNSString(text)); |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 1051 | self.message = @""; |
| 1052 | if(sendPanelHeight.constant != SEND_PANEL_DEFAULT_HEIGHT) { |
| 1053 | sendPanelHeight.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 1054 | messagesBottomMargin.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 1055 | [self scrollToBottom]; |
| 1056 | } |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 1057 | if (composingMessage) { |
| 1058 | composingMessage = false; |
| 1059 | convModel_->setIsComposing(convUid_, composingMessage); |
| 1060 | } |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | |
| 1064 | - (IBAction)openEmojy:(id)sender { |
| 1065 | [messageField.window makeFirstResponder: messageField]; |
| 1066 | [[messageField currentEditor] moveToEndOfLine:nil]; |
| 1067 | [NSApp orderFrontCharacterPalette: messageField]; |
| 1068 | } |
| 1069 | |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 1070 | - (IBAction)startVideoMessage:(id)sender |
| 1071 | { |
| 1072 | [self startRecording:NO]; |
| 1073 | } |
| 1074 | |
| 1075 | - (IBAction)startAudioMessage:(id)sender |
| 1076 | { |
| 1077 | [self startRecording:YES]; |
| 1078 | } |
| 1079 | -(void) startRecording:(BOOL)isAudio { |
| 1080 | if (recordingController == nil) { |
| 1081 | recordingController = [[RecordFileVC alloc] initWithNibName:@"RecordFileVC" bundle:nil avModel: self->avModel]; |
| 1082 | recordingController.delegate = self; |
| 1083 | } |
| 1084 | if(recordMessagePopover != nil) |
| 1085 | { |
| 1086 | [self closeRecordingView]; |
| 1087 | return; |
| 1088 | } |
| 1089 | recordMessagePopover = [[NSPopover alloc] init]; |
| 1090 | [recordingController prepareRecordingView: isAudio]; |
| 1091 | [recordMessagePopover setContentSize: recordingController.view.frame.size]; |
| 1092 | [recordMessagePopover setContentViewController:recordingController]; |
| 1093 | [recordMessagePopover setAnimates:YES]; |
| 1094 | NSButton *anchorButton = isAudio ? recordAudioButton : recordVideoButton; |
| 1095 | [recordMessagePopover showRelativeToRect: anchorButton.bounds |
| 1096 | ofView: anchorButton |
| 1097 | preferredEdge: NSMaxYEdge]; |
| 1098 | } |
| 1099 | |
| 1100 | -(void) sendFile:(NSString *) name withFilePath:(NSString *) path { |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 1101 | convModel_->sendFile(convUid_, QString::fromNSString(path), QString::fromNSString(name)); |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 1102 | } |
| 1103 | |
| 1104 | -(void) closeRecordingView { |
| 1105 | if(recordMessagePopover != nil) { |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 1106 | [recordMessagePopover close]; |
| 1107 | recordMessagePopover = nil; |
Kateryna Kostiuk | a740481 | 2019-10-28 12:24:46 -0400 | [diff] [blame] | 1108 | recordingController.stopRecordingView; |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 1109 | } |
| 1110 | } |
| 1111 | |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 1112 | - (IBAction)sendFile:(id)sender { |
| 1113 | NSOpenPanel* filePicker = [NSOpenPanel openPanel]; |
| 1114 | [filePicker setCanChooseFiles:YES]; |
| 1115 | [filePicker setCanChooseDirectories:NO]; |
| 1116 | [filePicker setAllowsMultipleSelection:NO]; |
| 1117 | |
| 1118 | if ([filePicker runModal] == NSFileHandlingPanelOKButton) { |
| 1119 | if ([[filePicker URLs] count] == 1) { |
| 1120 | NSURL* url = [[filePicker URLs] objectAtIndex:0]; |
| 1121 | const char* fullPath = [url fileSystemRepresentation]; |
| 1122 | NSString* fileName = [url lastPathComponent]; |
| 1123 | if (convModel_) { |
| 1124 | auto* conv = [self getCurrentConversation]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 1125 | convModel_->sendFile(convUid_, QString::fromStdString(fullPath), QString::fromNSString(fileName)); |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | |
| 1132 | #pragma mark - NSTextFieldDelegate |
| 1133 | |
| 1134 | - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector |
| 1135 | { |
| 1136 | if (commandSelector == @selector(insertNewline:)) { |
| 1137 | if(self.message.length > 0) { |
| 1138 | [self sendMessage: nil]; |
| 1139 | } else if(messagesBottomMargin.constant != SEND_PANEL_DEFAULT_HEIGHT) { |
| 1140 | sendPanelHeight.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 1141 | messagesBottomMargin.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 1142 | [self scrollToBottom]; |
| 1143 | } |
| 1144 | return YES; |
| 1145 | } |
| 1146 | return NO; |
| 1147 | } |
| 1148 | |
| 1149 | - (void)controlTextDidChange:(NSNotification *)aNotification { |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 1150 | [self checkIfcomposingMsg]; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 1151 | [self updateSendMessageHeight]; |
| 1152 | } |
| 1153 | |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 1154 | - (void) checkIfcomposingMsg { |
| 1155 | [self updateSendMessageHeight]; |
| 1156 | BOOL haveText = [messageField.stringValue removeEmptyLinesAtBorders].length != 0; |
| 1157 | if (haveText != composingMessage) { |
| 1158 | composingMessage = haveText; |
| 1159 | convModel_->setIsComposing(convUid_, composingMessage); |
| 1160 | } |
| 1161 | } |
| 1162 | |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 1163 | #pragma mark - QLPreviewPanelDataSource |
| 1164 | |
| 1165 | -(void)beginPreviewPanelControl:(QLPreviewPanel *)panel |
| 1166 | { |
| 1167 | panel.dataSource = self; |
| 1168 | } |
| 1169 | |
| 1170 | - (void)endPreviewPanelControl:(QLPreviewPanel *)panel { |
| 1171 | panel.dataSource = nil; |
| 1172 | } |
| 1173 | |
| 1174 | -(BOOL)acceptsPreviewPanelControl:(QLPreviewPanel *)panel |
| 1175 | { |
| 1176 | return YES; |
| 1177 | } |
| 1178 | |
| 1179 | - (NSInteger)numberOfPreviewItemsInPreviewPanel:(QLPreviewPanel *)panel { |
| 1180 | return 1; |
| 1181 | } |
| 1182 | |
| 1183 | - (id <QLPreviewItem>)previewPanel:(QLPreviewPanel *)panel previewItemAtIndex:(NSInteger)index { |
Kateryna Kostiuk | 30c6ac2 | 2020-05-06 17:42:59 -0400 | [diff] [blame] | 1184 | try { |
| 1185 | return [NSURL fileURLWithPath: previewImage]; |
| 1186 | } catch (NSException *exception) { |
| 1187 | nil; |
| 1188 | } |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 1189 | } |
| 1190 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 1191 | @end |