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