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