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