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) { |
| 538 | //last row peer composing view |
| 539 | result = [tableView makeViewWithIdentifier:@"PeerComposingMsgView" owner:conversationView]; |
Kateryna Kostiuk | f5eb58c | 2020-03-25 16:50:35 -0400 | [diff] [blame] | 540 | std::advance(it, row-1); |
| 541 | //if previous message not from peer display avatar |
| 542 | auto interaction = it->second; |
| 543 | bool isOutgoing = lrc::api::interaction::isOutgoing(interaction); |
| 544 | [result.photoView setHidden: YES]; |
| 545 | if(isOutgoing) { |
| 546 | auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator()); |
| 547 | auto* conv = [self getCurrentConversation]; |
| 548 | [result.photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(*conv, convModel_->owner)))]; |
| 549 | [result.photoView setHidden: NO]; |
| 550 | } |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 551 | CGFloat alpha = peerComposingMessage ? 1 : 0; |
| 552 | result.alphaValue = 0; |
| 553 | [result animateCompozingIndicator: NO]; |
| 554 | if (alpha == 1) { |
| 555 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ |
| 556 | if (peerComposingMessage) { |
| 557 | result.alphaValue = alpha; |
| 558 | [result animateCompozingIndicator: YES]; |
| 559 | } |
| 560 | }); |
| 561 | } |
| 562 | return result; |
| 563 | } |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 564 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 565 | std::advance(it, row); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 566 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 567 | auto interaction = it->second; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 568 | bool isOutgoing = lrc::api::interaction::isOutgoing(interaction); |
| 569 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 570 | switch (interaction.type) { |
| 571 | case lrc::api::interaction::Type::TEXT: |
| 572 | if (isOutgoing) { |
| 573 | result = [tableView makeViewWithIdentifier:@"RightMessageView" owner:self]; |
| 574 | } else { |
| 575 | result = [tableView makeViewWithIdentifier:@"LeftMessageView" owner:self]; |
| 576 | } |
| 577 | break; |
Kateryna Kostiuk | 209a630 | 2019-08-14 16:46:21 -0400 | [diff] [blame] | 578 | case lrc::api::interaction::Type::DATA_TRANSFER: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 579 | return [self configureViewforTransfer:interaction interactionID: it->first tableView:tableView]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 580 | break; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 581 | case lrc::api::interaction::Type::CONTACT: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 582 | case lrc::api::interaction::Type::CALL: { |
| 583 | NSDate* msgTime = [NSDate dateWithTimeIntervalSince1970:interaction.timestamp]; |
| 584 | NSString* timeString = [self timeForMessage: msgTime]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 585 | return [self makeGenericInteractionViewForTableView:tableView withText:interaction.body.toNSString() andTime:timeString]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 586 | } |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 587 | default: // If interaction is not of a known type |
| 588 | return nil; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 589 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 590 | MessageSequencing sequence = [self computeSequencingFor:row]; |
| 591 | BubbleType type = SINGLE; |
| 592 | if (sequence == FIRST_WITHOUT_TIME || sequence == FIRST_WITH_TIME) { |
| 593 | type = FIRST; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 594 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 595 | if (sequence == MIDDLE_IN_SEQUENCE) { |
| 596 | type = MIDDLE; |
| 597 | } |
| 598 | if (sequence == LAST_IN_SEQUENCE) { |
| 599 | type = LAST; |
| 600 | } |
| 601 | result.msgBackground.type = type; |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 602 | bool sendingFail = false; |
| 603 | [result.messageStatus setHidden:YES]; |
| 604 | if (interaction.type == lrc::api::interaction::Type::TEXT && isOutgoing) { |
| 605 | if (interaction.status == lrc::api::interaction::Status::SENDING) { |
| 606 | [result.messageStatus setHidden:NO]; |
| 607 | [result.sendingMessageIndicator startAnimation:nil]; |
| 608 | [result.messageFailed setHidden:YES]; |
Kateryna Kostiuk | 209a630 | 2019-08-14 16:46:21 -0400 | [diff] [blame] | 609 | } else if (interaction.status == lrc::api::interaction::Status::FAILURE) { |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 610 | [result.messageStatus setHidden:NO]; |
| 611 | [result.sendingMessageIndicator setHidden:YES]; |
| 612 | [result.messageFailed setHidden:NO]; |
| 613 | sendingFail = true; |
| 614 | } |
| 615 | } |
| 616 | [result setupForInteraction:it->first isFailed: sendingFail]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 617 | bool shouldDisplayTime = (sequence == FIRST_WITH_TIME || sequence == SINGLE_WITH_TIME) ? YES : NO; |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 618 | bool shouldApplyPadding = (sequence == FIRST_WITHOUT_TIME || sequence == SINGLE_WITHOUT_TIME) ? YES : NO; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 619 | [result.msgBackground setNeedsDisplay:YES]; |
| 620 | [result setNeedsDisplay:YES]; |
| 621 | [result.timeBox setNeedsDisplay:YES]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 622 | |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 623 | NSString *text = interaction.body.toNSString(); |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 624 | text = [text removeEmptyLinesAtBorders]; |
| 625 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 626 | NSMutableAttributedString* msgAttString = |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 627 | [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:text] |
Kateryna Kostiuk | 3308987 | 2017-07-14 16:43:59 -0400 | [diff] [blame] | 628 | attributes:[self messageAttributes]]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 629 | |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 630 | CGSize messageSize = [self sizeFor: text maxWidth:tableView.frame.size.width * 0.7]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 631 | |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 632 | [result updateMessageConstraint:messageSize.width andHeight:messageSize.height timeIsVisible:shouldDisplayTime isTopPadding: shouldApplyPadding]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 633 | [[result.msgView textStorage] appendAttributedString:msgAttString]; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 634 | // [result.msgView checkTextInDocument:nil]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 635 | |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 636 | NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil]; |
| 637 | NSArray *matches = [linkDetector matchesInString:result.msgView.string options:0 range:NSMakeRange(0, result.msgView.string.length)]; |
| 638 | |
| 639 | [result.msgView.textStorage beginEditing]; |
| 640 | |
| 641 | for (NSTextCheckingResult *match in matches) { |
| 642 | if (!match.URL) continue; |
| 643 | |
| 644 | NSDictionary *linkAttributes = @{ |
| 645 | NSLinkAttributeName: match.URL, |
| 646 | }; |
| 647 | [result.msgView.textStorage addAttributes:linkAttributes range:match.range]; |
| 648 | } |
| 649 | |
| 650 | [result.msgView.textStorage endEditing]; |
| 651 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 652 | if (shouldDisplayTime) { |
| 653 | NSDate* msgTime = [NSDate dateWithTimeIntervalSince1970:interaction.timestamp]; |
| 654 | NSString* timeString = [self timeForMessage: msgTime]; |
| 655 | result.timeLabel.stringValue = timeString; |
Anthony Léonard | 64e1967 | 2018-01-18 16:40:34 -0500 | [diff] [blame] | 656 | } |
| 657 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 658 | bool shouldDisplayAvatar = (sequence != MIDDLE_IN_SEQUENCE && sequence != FIRST_WITHOUT_TIME |
| 659 | && sequence != FIRST_WITH_TIME) ? YES : NO; |
| 660 | [result.photoView setHidden:!shouldDisplayAvatar]; |
| 661 | if (!isOutgoing && shouldDisplayAvatar) { |
| 662 | auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator()); |
| 663 | [result.photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(*conv, convModel_->owner)))]; |
| 664 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 665 | return result; |
| 666 | } |
| 667 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 668 | - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 669 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 670 | double someWidth = tableView.frame.size.width * 0.7; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 671 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 672 | auto* conv = [self getCurrentConversation]; |
| 673 | |
| 674 | if (conv == nil) |
| 675 | return 0; |
| 676 | |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 677 | auto size = conv->interactions.size(); |
| 678 | if (row == size) { |
| 679 | //last item peer composing view |
| 680 | if (peerComposingMessage) { |
| 681 | return HEIGHT_FOR_COMPOSING_INDICATOR; |
| 682 | } |
| 683 | return 0.1; |
| 684 | } |
| 685 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 686 | auto it = conv->interactions.begin(); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 687 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 688 | std::advance(it, row); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 689 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 690 | auto interaction = it->second; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 691 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 692 | MessageSequencing sequence = [self computeSequencingFor:row]; |
| 693 | |
| 694 | bool shouldDisplayTime = (sequence == FIRST_WITH_TIME || sequence == SINGLE_WITH_TIME) ? YES : NO; |
| 695 | |
| 696 | |
Kateryna Kostiuk | 209a630 | 2019-08-14 16:46:21 -0400 | [diff] [blame] | 697 | if(interaction.type == lrc::api::interaction::Type::DATA_TRANSFER) { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 698 | |
| 699 | if( interaction.status == lrc::api::interaction::Status::TRANSFER_FINISHED) { |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 700 | NSString* name = interaction.body.toNSString(); |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 701 | NSImage* image = [self getImageForFilePath:name]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 702 | if (([name rangeOfString:@"/"].location == NSNotFound)) { |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 703 | image = [self getImageForFilePath:[self getDataTransferPath:it->first]]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 704 | } |
| 705 | if (image != nil) { |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 706 | CGFloat widthScaleFactor = MAX_TRANSFERED_IMAGE_SIZE / image.size.width; |
| 707 | CGFloat heightScaleFactor = MAX_TRANSFERED_IMAGE_SIZE / image.size.height; |
| 708 | CGFloat heigt = 0; |
| 709 | if((widthScaleFactor >= 1) && (heightScaleFactor >= 1)) { |
| 710 | heigt = image.size.height; |
| 711 | } else { |
| 712 | CGFloat scale = MIN(widthScaleFactor, heightScaleFactor); |
| 713 | heigt = image.size.height * scale; |
| 714 | } |
| 715 | return heigt + TIME_BOX_HEIGHT; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 716 | } |
| 717 | } |
| 718 | return BUBBLE_HEIGHT_FOR_TRANSFERED_FILE + TIME_BOX_HEIGHT; |
| 719 | } |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 720 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 721 | 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] | 722 | return GENERIC_CELL_HEIGHT; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 723 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 724 | // TODO Implement interactions other than messages |
| 725 | if(interaction.type != lrc::api::interaction::Type::TEXT) { |
| 726 | return 0; |
| 727 | } |
| 728 | |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 729 | NSString *text = interaction.body.toNSString(); |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 730 | text = [text removeEmptyLinesAtBorders]; |
| 731 | |
| 732 | CGSize messageSize = [self sizeFor: text maxWidth:tableView.frame.size.width * 0.7]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 733 | CGFloat singleLignMessageHeight = 15; |
| 734 | |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 735 | bool shouldApplyPadding = (sequence == FIRST_WITHOUT_TIME || sequence == SINGLE_WITHOUT_TIME) ? YES : NO; |
| 736 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 737 | if (shouldDisplayTime) { |
| 738 | return MAX(messageSize.height + TIME_BOX_HEIGHT + MESSAGE_TEXT_PADDING * 2, |
| 739 | TIME_BOX_HEIGHT + MESSAGE_TEXT_PADDING * 2 + singleLignMessageHeight); |
| 740 | } |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 741 | if(shouldApplyPadding) { |
| 742 | return MAX(messageSize.height + MESSAGE_TEXT_PADDING * 2 + 15, |
| 743 | singleLignMessageHeight + MESSAGE_TEXT_PADDING * 2 + 15); |
| 744 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 745 | return MAX(messageSize.height + MESSAGE_TEXT_PADDING * 2, |
| 746 | singleLignMessageHeight + MESSAGE_TEXT_PADDING * 2); |
| 747 | } |
| 748 | |
| 749 | #pragma mark - message view parameters |
| 750 | |
| 751 | -(NSString *) getDataTransferPath:(uint64_t)interactionId { |
| 752 | lrc::api::datatransfer::Info info = {}; |
| 753 | convModel_->getTransferInfo(interactionId, info); |
| 754 | double convertData = static_cast<double>(info.totalSize); |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 755 | return info.path.toNSString(); |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 756 | } |
| 757 | |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 758 | -(NSImage*) getImageForFilePath: (NSString *) path { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 759 | if (path.length <= 0) {return nil;} |
| 760 | if (![[NSFileManager defaultManager] fileExistsAtPath: path]) {return nil;} |
| 761 | NSImage* transferedImage = [[NSImage alloc] initWithContentsOfFile: path]; |
Kateryna Kostiuk | efc665d | 2018-09-17 15:42:43 -0400 | [diff] [blame] | 762 | return transferedImage; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | -(CGSize) sizeFor:(NSString *) message maxWidth:(CGFloat) width { |
| 766 | CGFloat horizaontalMargin = 6; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 767 | NSMutableAttributedString* msgAttString = |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 768 | [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", message] |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 769 | attributes:[self messageAttributes]]; |
| 770 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 771 | CGFloat finalWidth = MIN(msgAttString.size.width + horizaontalMargin * 2, width); |
| 772 | NSRect frame = NSMakeRect(0, 0, finalWidth, msgAttString.size.height); |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 773 | NSTextView *tv = [[NSTextView alloc] initWithFrame:frame]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 774 | [[tv textStorage] setAttributedString:msgAttString]; |
| 775 | [tv sizeToFit]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 776 | return tv.frame.size; |
| 777 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 778 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 779 | -(MessageSequencing) computeSequencingFor:(NSInteger) row { |
| 780 | auto* conv = [self getCurrentConversation]; |
| 781 | if (conv == nil) |
| 782 | return SINGLE_WITHOUT_TIME; |
| 783 | auto it = conv->interactions.begin(); |
| 784 | std::advance(it, row); |
| 785 | auto interaction = it->second; |
| 786 | if (interaction.type != lrc::api::interaction::Type::TEXT) { |
| 787 | return SINGLE_WITH_TIME; |
| 788 | } |
| 789 | if (row == 0) { |
| 790 | if (it == conv->interactions.end()) { |
| 791 | return SINGLE_WITH_TIME; |
| 792 | } |
| 793 | auto nextIt = it; |
| 794 | nextIt++; |
| 795 | auto nextInteraction = nextIt->second; |
| 796 | if ([self sequenceChangedFrom:interaction to: nextInteraction]) { |
| 797 | return SINGLE_WITH_TIME; |
| 798 | } |
| 799 | return FIRST_WITH_TIME; |
| 800 | } |
| 801 | |
| 802 | if (row == conversationView.numberOfRows - 1) { |
| 803 | if(it == conv->interactions.begin()) { |
| 804 | return SINGLE_WITH_TIME; |
| 805 | } |
| 806 | auto previousIt = it; |
| 807 | previousIt--; |
| 808 | auto previousInteraction = previousIt->second; |
| 809 | bool timeChanged = [self sequenceTimeChangedFrom:interaction to:previousInteraction]; |
| 810 | bool authorChanged = [self sequenceAuthorChangedFrom:interaction to:previousInteraction]; |
| 811 | if (!timeChanged && !authorChanged) { |
| 812 | return LAST_IN_SEQUENCE; |
| 813 | } |
| 814 | if (!timeChanged && authorChanged) { |
| 815 | return SINGLE_WITHOUT_TIME; |
| 816 | } |
| 817 | return SINGLE_WITH_TIME; |
| 818 | } |
| 819 | if(it == conv->interactions.begin() || it == conv->interactions.end()) { |
| 820 | return SINGLE_WITH_TIME; |
| 821 | } |
| 822 | auto previousIt = it; |
| 823 | previousIt--; |
| 824 | auto previousInteraction = previousIt->second; |
| 825 | auto nextIt = it; |
| 826 | nextIt++; |
| 827 | auto nextInteraction = nextIt->second; |
| 828 | |
| 829 | bool timeChanged = [self sequenceTimeChangedFrom:interaction to:previousInteraction]; |
| 830 | bool authorChanged = [self sequenceAuthorChangedFrom:interaction to:previousInteraction]; |
| 831 | bool sequenceWillChange = [self sequenceChangedFrom:interaction to: nextInteraction]; |
Kateryna Kostiuk | 209a630 | 2019-08-14 16:46:21 -0400 | [diff] [blame] | 832 | if (previousInteraction.type == lrc::api::interaction::Type::DATA_TRANSFER) { |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 833 | if(!sequenceWillChange) { |
| 834 | return FIRST_WITH_TIME; |
| 835 | } |
| 836 | return SINGLE_WITH_TIME; |
| 837 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 838 | if (!sequenceWillChange) { |
| 839 | if (!timeChanged && !authorChanged) { |
| 840 | return MIDDLE_IN_SEQUENCE; |
| 841 | } |
| 842 | if (timeChanged) { |
| 843 | return FIRST_WITH_TIME; |
| 844 | } |
| 845 | return FIRST_WITHOUT_TIME; |
| 846 | } if (!timeChanged && !authorChanged) { |
| 847 | return LAST_IN_SEQUENCE; |
| 848 | } if (timeChanged) { |
| 849 | return SINGLE_WITH_TIME; |
| 850 | } |
| 851 | return SINGLE_WITHOUT_TIME; |
| 852 | } |
| 853 | |
| 854 | -(bool) sequenceChangedFrom:(lrc::api::interaction::Info) firstInteraction to:(lrc::api::interaction::Info) secondInteraction { |
| 855 | return ([self sequenceTimeChangedFrom:firstInteraction to:secondInteraction] || [self sequenceAuthorChangedFrom:firstInteraction to:secondInteraction]); |
| 856 | } |
| 857 | |
| 858 | -(bool) sequenceTimeChangedFrom:(lrc::api::interaction::Info) firstInteraction to:(lrc::api::interaction::Info) secondInteraction { |
| 859 | bool timeChanged = NO; |
| 860 | NSDate* firstMessageTime = [NSDate dateWithTimeIntervalSince1970:firstInteraction.timestamp]; |
| 861 | NSDate* secondMessageTime = [NSDate dateWithTimeIntervalSince1970:secondInteraction.timestamp]; |
| 862 | bool hourComp = [[NSCalendar currentCalendar] compareDate:firstMessageTime toDate:secondMessageTime toUnitGranularity:NSCalendarUnitHour]; |
| 863 | bool minutComp = [[NSCalendar currentCalendar] compareDate:firstMessageTime toDate:secondMessageTime toUnitGranularity:NSCalendarUnitMinute]; |
| 864 | if(hourComp != NSOrderedSame || minutComp != NSOrderedSame) { |
| 865 | timeChanged = YES; |
| 866 | } |
| 867 | return timeChanged; |
| 868 | } |
| 869 | |
| 870 | -(bool) sequenceAuthorChangedFrom:(lrc::api::interaction::Info) firstInteraction to:(lrc::api::interaction::Info) secondInteraction { |
| 871 | bool authorChanged = YES; |
| 872 | bool isOutgoing = lrc::api::interaction::isOutgoing(firstInteraction); |
| 873 | if ((secondInteraction.type == lrc::api::interaction::Type::TEXT) && (isOutgoing == lrc::api::interaction::isOutgoing(secondInteraction))) { |
| 874 | authorChanged = NO; |
| 875 | } |
| 876 | return authorChanged; |
| 877 | } |
| 878 | |
| 879 | -(NSString *)timeForMessage:(NSDate*) msgTime { |
| 880 | NSDate *today = [NSDate date]; |
| 881 | NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; |
Kateryna Kostiuk | af6d5e2 | 2018-06-12 15:00:00 -0400 | [diff] [blame] | 882 | [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:[[NSLocale currentLocale] localeIdentifier]]]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 883 | if ([[NSCalendar currentCalendar] compareDate:today |
| 884 | toDate:msgTime |
| 885 | toUnitGranularity:NSCalendarUnitYear]!= NSOrderedSame) { |
Kateryna Kostiuk | 6640643 | 2019-11-09 17:20:34 -0500 | [diff] [blame] | 886 | return [NSDateFormatter localizedStringFromDate:msgTime dateStyle:NSDateFormatterLongStyle timeStyle:NSDateFormatterMediumStyle]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 887 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 888 | if ([[NSCalendar currentCalendar] compareDate:today |
| 889 | toDate:msgTime |
| 890 | toUnitGranularity:NSCalendarUnitDay]!= NSOrderedSame || |
| 891 | [[NSCalendar currentCalendar] compareDate:today |
| 892 | toDate:msgTime |
| 893 | toUnitGranularity:NSCalendarUnitMonth]!= NSOrderedSame) { |
Kateryna Kostiuk | 6640643 | 2019-11-09 17:20:34 -0500 | [diff] [blame] | 894 | return [NSDateFormatter localizedStringFromDate:msgTime dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterShortStyle]; |
| 895 | } |
| 896 | return [NSDateFormatter localizedStringFromDate:msgTime dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterShortStyle]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 897 | } |
| 898 | |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 899 | - (void) updateSendMessageHeight { |
| 900 | NSAttributedString *msgAttString = messageField.attributedStringValue; |
| 901 | NSRect frame = NSMakeRect(0, 0, messageField.frame.size.width, msgAttString.size.height); |
| 902 | NSTextView *tv = [[NSTextView alloc] initWithFrame:frame]; |
| 903 | [[tv textStorage] setAttributedString:msgAttString]; |
| 904 | [tv sizeToFit]; |
| 905 | CGFloat height = tv.frame.size.height + MEESAGE_MARGIN * 2; |
| 906 | CGFloat newHeight = MIN(SEND_PANEL_MAX_HEIGHT, MAX(SEND_PANEL_DEFAULT_HEIGHT, height)); |
| 907 | if(messagesBottomMargin.constant == newHeight) { |
| 908 | return; |
| 909 | } |
| 910 | messagesBottomMargin.constant = newHeight; |
| 911 | dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.05 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{ |
| 912 | [self scrollToBottom]; |
| 913 | sendPanelHeight.constant = newHeight; |
| 914 | }); |
| 915 | } |
| 916 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 917 | #pragma mark - NSTableViewDataSource |
| 918 | |
| 919 | - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView |
| 920 | { |
| 921 | auto* conv = [self getCurrentConversation]; |
| 922 | |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 923 | // return conversation +1 view for composing indicator |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 924 | if (conv) |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 925 | return conv->interactions.size() + 1; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 926 | else |
| 927 | return 0; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 928 | } |
| 929 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 930 | #pragma mark - Text formatting |
| 931 | |
Kateryna Kostiuk | 3308987 | 2017-07-14 16:43:59 -0400 | [diff] [blame] | 932 | - (NSMutableDictionary*) messageAttributes |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 933 | { |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 934 | NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 935 | attrs[NSForegroundColorAttributeName] = [NSColor labelColor]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 936 | attrs[NSParagraphStyleAttributeName] = [self paragraphStyle]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 937 | return attrs; |
| 938 | } |
| 939 | |
| 940 | - (NSParagraphStyle*) paragraphStyle |
| 941 | { |
| 942 | /* |
| 943 | The only way to instantiate an NSMutableParagraphStyle is to mutably copy an |
| 944 | NSParagraphStyle. And since we don't have an existing NSParagraphStyle available |
| 945 | to copy, we use the default one. |
| 946 | |
| 947 | The default values supplied by the default NSParagraphStyle are: |
| 948 | Alignment NSNaturalTextAlignment |
| 949 | Tab stops 12 left-aligned tabs, spaced by 28.0 points |
| 950 | Line break mode NSLineBreakByWordWrapping |
| 951 | All others 0.0 |
| 952 | */ |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 953 | NSMutableParagraphStyle* aMutableParagraphStyle = |
| 954 | [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
| 955 | [aMutableParagraphStyle setHeadIndent:1.0]; |
| 956 | [aMutableParagraphStyle setFirstLineHeadIndent:1.0]; |
| 957 | return aMutableParagraphStyle; |
| 958 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 959 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 960 | #pragma mark - Actions |
| 961 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 962 | - (void)acceptIncomingFile:(id)sender { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 963 | auto interId = [(IMTableCellView*)[[sender superview] superview] interaction]; |
| 964 | auto& inter = [self getCurrentConversation]->interactions.find(interId)->second; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 965 | if (convModel_ && !convUid_.isEmpty()) { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 966 | NSSavePanel* filePicker = [NSSavePanel savePanel]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 967 | [filePicker setNameFieldStringValue:inter.body.toNSString()]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 968 | |
| 969 | if ([filePicker runModal] == NSFileHandlingPanelOKButton) { |
| 970 | const char* fullPath = [[filePicker URL] fileSystemRepresentation]; |
| 971 | convModel_->acceptTransfer(convUid_, interId, fullPath); |
| 972 | } |
| 973 | } |
| 974 | } |
| 975 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 976 | - (void)declineIncomingFile:(id)sender { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 977 | auto inter = [(IMTableCellView*)[[sender superview] superview] interaction]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 978 | if (convModel_ && !convUid_.isEmpty()) { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 979 | convModel_->cancelTransfer(convUid_, inter); |
| 980 | } |
| 981 | } |
| 982 | |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 983 | - (void)imagePreview:(id)sender { |
| 984 | uint64_t interId; |
| 985 | if ([[sender superview] isKindOfClass:[IMTableCellView class]]) { |
| 986 | interId = [(IMTableCellView*)[sender superview] interaction]; |
| 987 | } else if ([[[sender superview] superview] isKindOfClass:[IMTableCellView class]]) { |
| 988 | interId = [(IMTableCellView*)[[sender superview] superview] interaction]; |
| 989 | } else { |
| 990 | return; |
| 991 | } |
| 992 | auto it = [self getCurrentConversation]->interactions.find(interId); |
| 993 | if (it == [self getCurrentConversation]->interactions.end()) { |
| 994 | return; |
| 995 | } |
| 996 | auto& interaction = it->second; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 997 | NSString* name = interaction.body.toNSString(); |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 998 | if (([name rangeOfString:@"/"].location == NSNotFound)) { |
| 999 | name = [self getDataTransferPath:interId]; |
| 1000 | } |
| 1001 | previewImage = name; |
| 1002 | if ([QLPreviewPanel sharedPreviewPanelExists] && [[QLPreviewPanel sharedPreviewPanel] isVisible]) { |
| 1003 | [[QLPreviewPanel sharedPreviewPanel] orderOut:nil]; |
| 1004 | } else { |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 1005 | dispatch_async(dispatch_get_main_queue(), ^{ |
| 1006 | [[QLPreviewPanel sharedPreviewPanel] makeKeyAndOrderFront:self]; |
| 1007 | }); |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 1008 | } |
| 1009 | } |
| 1010 | |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 1011 | - (IBAction)sendMessage:(id)sender { |
| 1012 | NSString* text = self.message; |
Kateryna Kostiuk | a8b6b56 | 2019-02-01 13:26:18 -0500 | [diff] [blame] | 1013 | unichar separatorChar = NSLineSeparatorCharacter; |
| 1014 | NSString *separatorString = [NSString stringWithCharacters:&separatorChar length:1]; |
| 1015 | text = [text stringByReplacingOccurrencesOfString: separatorString withString: @"\n"]; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 1016 | if (text && text.length > 0) { |
| 1017 | auto* conv = [self getCurrentConversation]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 1018 | convModel_->sendMessage(convUid_, QString::fromNSString(text)); |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 1019 | self.message = @""; |
| 1020 | if(sendPanelHeight.constant != SEND_PANEL_DEFAULT_HEIGHT) { |
| 1021 | sendPanelHeight.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 1022 | messagesBottomMargin.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 1023 | [self scrollToBottom]; |
| 1024 | } |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 1025 | if (composingMessage) { |
| 1026 | composingMessage = false; |
| 1027 | convModel_->setIsComposing(convUid_, composingMessage); |
| 1028 | } |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | - (IBAction)openEmojy:(id)sender { |
| 1033 | [messageField.window makeFirstResponder: messageField]; |
| 1034 | [[messageField currentEditor] moveToEndOfLine:nil]; |
| 1035 | [NSApp orderFrontCharacterPalette: messageField]; |
| 1036 | } |
| 1037 | |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 1038 | - (IBAction)startVideoMessage:(id)sender |
| 1039 | { |
| 1040 | [self startRecording:NO]; |
| 1041 | } |
| 1042 | |
| 1043 | - (IBAction)startAudioMessage:(id)sender |
| 1044 | { |
| 1045 | [self startRecording:YES]; |
| 1046 | } |
| 1047 | -(void) startRecording:(BOOL)isAudio { |
| 1048 | if (recordingController == nil) { |
| 1049 | recordingController = [[RecordFileVC alloc] initWithNibName:@"RecordFileVC" bundle:nil avModel: self->avModel]; |
| 1050 | recordingController.delegate = self; |
| 1051 | } |
| 1052 | if(recordMessagePopover != nil) |
| 1053 | { |
| 1054 | [self closeRecordingView]; |
| 1055 | return; |
| 1056 | } |
| 1057 | recordMessagePopover = [[NSPopover alloc] init]; |
| 1058 | [recordingController prepareRecordingView: isAudio]; |
| 1059 | [recordMessagePopover setContentSize: recordingController.view.frame.size]; |
| 1060 | [recordMessagePopover setContentViewController:recordingController]; |
| 1061 | [recordMessagePopover setAnimates:YES]; |
| 1062 | NSButton *anchorButton = isAudio ? recordAudioButton : recordVideoButton; |
| 1063 | [recordMessagePopover showRelativeToRect: anchorButton.bounds |
| 1064 | ofView: anchorButton |
| 1065 | preferredEdge: NSMaxYEdge]; |
| 1066 | } |
| 1067 | |
| 1068 | -(void) sendFile:(NSString *) name withFilePath:(NSString *) path { |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 1069 | convModel_->sendFile(convUid_, QString::fromNSString(path), QString::fromNSString(name)); |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 1070 | } |
| 1071 | |
| 1072 | -(void) closeRecordingView { |
| 1073 | if(recordMessagePopover != nil) { |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 1074 | [recordMessagePopover close]; |
| 1075 | recordMessagePopover = nil; |
Kateryna Kostiuk | a740481 | 2019-10-28 12:24:46 -0400 | [diff] [blame] | 1076 | recordingController.stopRecordingView; |
Kateryna Kostiuk | fbe1b2f | 2019-10-07 17:32:26 -0400 | [diff] [blame] | 1077 | } |
| 1078 | } |
| 1079 | |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 1080 | - (IBAction)sendFile:(id)sender { |
| 1081 | NSOpenPanel* filePicker = [NSOpenPanel openPanel]; |
| 1082 | [filePicker setCanChooseFiles:YES]; |
| 1083 | [filePicker setCanChooseDirectories:NO]; |
| 1084 | [filePicker setAllowsMultipleSelection:NO]; |
| 1085 | |
| 1086 | if ([filePicker runModal] == NSFileHandlingPanelOKButton) { |
| 1087 | if ([[filePicker URLs] count] == 1) { |
| 1088 | NSURL* url = [[filePicker URLs] objectAtIndex:0]; |
| 1089 | const char* fullPath = [url fileSystemRepresentation]; |
| 1090 | NSString* fileName = [url lastPathComponent]; |
| 1091 | if (convModel_) { |
| 1092 | auto* conv = [self getCurrentConversation]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 1093 | convModel_->sendFile(convUid_, QString::fromStdString(fullPath), QString::fromNSString(fileName)); |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | |
| 1100 | #pragma mark - NSTextFieldDelegate |
| 1101 | |
| 1102 | - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector |
| 1103 | { |
| 1104 | if (commandSelector == @selector(insertNewline:)) { |
| 1105 | if(self.message.length > 0) { |
| 1106 | [self sendMessage: nil]; |
| 1107 | } else if(messagesBottomMargin.constant != SEND_PANEL_DEFAULT_HEIGHT) { |
| 1108 | sendPanelHeight.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 1109 | messagesBottomMargin.constant = SEND_PANEL_DEFAULT_HEIGHT; |
| 1110 | [self scrollToBottom]; |
| 1111 | } |
| 1112 | return YES; |
| 1113 | } |
| 1114 | return NO; |
| 1115 | } |
| 1116 | |
| 1117 | - (void)controlTextDidChange:(NSNotification *)aNotification { |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 1118 | [self checkIfcomposingMsg]; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 1119 | [self updateSendMessageHeight]; |
| 1120 | } |
| 1121 | |
Kateryna Kostiuk | 10c896b | 2020-03-24 12:46:17 -0400 | [diff] [blame] | 1122 | - (void) checkIfcomposingMsg { |
| 1123 | [self updateSendMessageHeight]; |
| 1124 | BOOL haveText = [messageField.stringValue removeEmptyLinesAtBorders].length != 0; |
| 1125 | if (haveText != composingMessage) { |
| 1126 | composingMessage = haveText; |
| 1127 | convModel_->setIsComposing(convUid_, composingMessage); |
| 1128 | } |
| 1129 | } |
| 1130 | |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 1131 | #pragma mark - QLPreviewPanelDataSource |
| 1132 | |
| 1133 | -(void)beginPreviewPanelControl:(QLPreviewPanel *)panel |
| 1134 | { |
| 1135 | panel.dataSource = self; |
| 1136 | } |
| 1137 | |
| 1138 | - (void)endPreviewPanelControl:(QLPreviewPanel *)panel { |
| 1139 | panel.dataSource = nil; |
| 1140 | } |
| 1141 | |
| 1142 | -(BOOL)acceptsPreviewPanelControl:(QLPreviewPanel *)panel |
| 1143 | { |
| 1144 | return YES; |
| 1145 | } |
| 1146 | |
| 1147 | - (NSInteger)numberOfPreviewItemsInPreviewPanel:(QLPreviewPanel *)panel { |
| 1148 | return 1; |
| 1149 | } |
| 1150 | |
| 1151 | - (id <QLPreviewItem>)previewPanel:(QLPreviewPanel *)panel previewItemAtIndex:(NSInteger)index { |
| 1152 | return [NSURL fileURLWithPath:previewImage]; |
| 1153 | } |
| 1154 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 1155 | @end |