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