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