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