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 | /* |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 3 | * Copyright (C) 2015-2018 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 | |
| 22 | #import <QItemSelectionModel> |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 23 | #import <QPixmap> |
| 24 | #import <QtMacExtras/qmacfunctions.h> |
| 25 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 26 | // LRC |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 27 | #import <globalinstances.h> |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 28 | #import <api/interaction.h> |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 29 | |
| 30 | #import "MessagesVC.h" |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 31 | #import "views/IMTableCellView.h" |
| 32 | #import "views/MessageBubbleView.h" |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 33 | #import "views/NSImage+Extensions.h" |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 34 | #import "INDSequentialTextSelectionManager.h" |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 35 | #import "delegates/ImageManipulationDelegate.h" |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 36 | #import "utils.h" |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 37 | #import "views/NSColor+RingTheme.h" |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame^] | 38 | #import "NSString+Extensions.h" |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 39 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 40 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 41 | @interface MessagesVC () <NSTableViewDelegate, NSTableViewDataSource> { |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 42 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 43 | __unsafe_unretained IBOutlet NSTableView* conversationView; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 44 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 45 | std::string convUid_; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 46 | lrc::api::ConversationModel* convModel_; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 47 | const lrc::api::conversation::Info* cachedConv_; |
| 48 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 49 | QMetaObject::Connection newInteractionSignal_; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 50 | |
| 51 | // Both are needed to invalidate cached conversation as pointer |
| 52 | // may not be referencing the same conversation anymore |
| 53 | QMetaObject::Connection modelSortedSignal_; |
| 54 | QMetaObject::Connection filterChangedSignal_; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 55 | QMetaObject::Connection interactionStatusUpdatedSignal_; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @property (nonatomic, strong, readonly) INDSequentialTextSelectionManager* selectionManager; |
| 59 | |
| 60 | @end |
| 61 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 62 | // Tags for view |
| 63 | NSInteger const GENERIC_INT_TEXT_TAG = 100; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 64 | NSInteger const GENERIC_INT_TIME_TAG = 200; |
| 65 | |
| 66 | // views size |
| 67 | CGFloat const GENERIC_CELL_HEIGHT = 60; |
| 68 | CGFloat const TIME_BOX_HEIGHT = 34; |
| 69 | CGFloat const MESSAGE_TEXT_PADDING = 10; |
| 70 | CGFloat const MAX_TRANSFERED_IMAGE_SIZE = 250; |
| 71 | CGFloat const BUBBLE_HEIGHT_FOR_TRANSFERED_FILE = 87; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 72 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 73 | @implementation MessagesVC |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 74 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 75 | //MessageBuble type |
| 76 | typedef NS_ENUM(NSInteger, MessageSequencing) { |
| 77 | SINGLE_WITH_TIME = 0, |
| 78 | SINGLE_WITHOUT_TIME = 1, |
| 79 | FIRST_WITH_TIME = 2, |
| 80 | FIRST_WITHOUT_TIME = 3, |
| 81 | MIDDLE_IN_SEQUENCE = 5, |
| 82 | LAST_IN_SEQUENCE = 6, |
| 83 | }; |
| 84 | |
| 85 | - (void)awakeFromNib |
| 86 | { |
| 87 | NSNib *cellNib = [[NSNib alloc] initWithNibNamed:@"MessageCells" bundle:nil]; |
| 88 | [conversationView registerNib:cellNib forIdentifier:@"LeftIncomingFileView"]; |
| 89 | [conversationView registerNib:cellNib forIdentifier:@"LeftOngoingFileView"]; |
| 90 | [conversationView registerNib:cellNib forIdentifier:@"LeftFinishedFileView"]; |
| 91 | [conversationView registerNib:cellNib forIdentifier:@"RightOngoingFileView"]; |
| 92 | [conversationView registerNib:cellNib forIdentifier:@"RightFinishedFileView"]; |
| 93 | } |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 94 | -(void) clearData { |
| 95 | cachedConv_ = nil; |
| 96 | convUid_ = ""; |
| 97 | convModel_ = nil; |
| 98 | |
| 99 | QObject::disconnect(modelSortedSignal_); |
| 100 | QObject::disconnect(filterChangedSignal_); |
| 101 | QObject::disconnect(interactionStatusUpdatedSignal_); |
| 102 | QObject::disconnect(newInteractionSignal_); |
| 103 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 104 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 105 | -(const lrc::api::conversation::Info*) getCurrentConversation |
| 106 | { |
| 107 | if (convModel_ == nil || convUid_.empty()) |
| 108 | return nil; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 109 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 110 | if (cachedConv_ != nil) |
| 111 | return cachedConv_; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 112 | |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 113 | auto it = getConversationFromUid(convUid_, *convModel_); |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 114 | if (it != convModel_->allFilteredConversations().end()) |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 115 | cachedConv_ = &(*it); |
| 116 | |
| 117 | return cachedConv_; |
| 118 | } |
| 119 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 120 | -(void) reloadConversationForMessage:(uint64_t) uid shouldUpdateHeight:(bool)update { |
| 121 | auto* conv = [self getCurrentConversation]; |
| 122 | |
| 123 | if (conv == nil) |
| 124 | return; |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 125 | auto it = conv->interactions.find(uid); |
| 126 | if (it == conv->interactions.end()) { |
| 127 | return; |
| 128 | } |
| 129 | auto itIndex = distance(conv->interactions.begin(),it); |
| 130 | NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:itIndex]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 131 | //reload previous message to update bubbleview |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 132 | if (itIndex > 0) { |
| 133 | auto previousIt = it; |
| 134 | previousIt--; |
| 135 | auto previousInteraction = previousIt->second; |
| 136 | if (previousInteraction.type == lrc::api::interaction::Type::TEXT) { |
| 137 | NSRange range = NSMakeRange(itIndex - 1, 2); |
| 138 | indexSet = [NSIndexSet indexSetWithIndexesInRange:range]; |
| 139 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 140 | } |
| 141 | if (update) { |
| 142 | [conversationView noteHeightOfRowsWithIndexesChanged:indexSet]; |
| 143 | } |
| 144 | [conversationView reloadDataForRowIndexes: indexSet |
| 145 | columnIndexes:[NSIndexSet indexSetWithIndex:0]]; |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 146 | CGRect visibleRect = [conversationView enclosingScrollView].contentView.visibleRect; |
| 147 | NSRange range = [conversationView rowsInRect:visibleRect]; |
| 148 | NSIndexSet* visibleIndexes = [NSIndexSet indexSetWithIndexesInRange:range]; |
| 149 | NSUInteger lastvisibleRow = [visibleIndexes lastIndex]; |
| 150 | if (([conversationView numberOfRows] > 0) && |
| 151 | lastvisibleRow == ([conversationView numberOfRows] -1)) { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 152 | [conversationView scrollToEndOfDocument:nil]; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | -(void) reloadConversationForMessage:(uint64_t) uid shouldUpdateHeight:(bool)update updateConversation:(bool) updateConversation { |
| 157 | auto* conv = [self getCurrentConversation]; |
| 158 | |
| 159 | if (conv == nil) |
| 160 | return; |
| 161 | auto it = distance(conv->interactions.begin(),conv->interactions.find(uid)); |
| 162 | NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:it]; |
| 163 | //reload previous message to update bubbleview |
| 164 | if (it > 0) { |
| 165 | NSRange range = NSMakeRange(it - 1, it); |
| 166 | indexSet = [NSIndexSet indexSetWithIndexesInRange:range]; |
| 167 | } |
| 168 | if (update) { |
| 169 | [conversationView noteHeightOfRowsWithIndexesChanged:indexSet]; |
| 170 | } |
| 171 | [conversationView reloadDataForRowIndexes: indexSet |
| 172 | columnIndexes:[NSIndexSet indexSetWithIndex:0]]; |
| 173 | if (update) { |
| 174 | [conversationView scrollToEndOfDocument:nil]; |
| 175 | } |
| 176 | } |
| 177 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 178 | -(void)setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 179 | { |
| 180 | if (convUid_ == convUid && convModel_ == model) |
| 181 | return; |
| 182 | |
| 183 | cachedConv_ = nil; |
| 184 | convUid_ = convUid; |
| 185 | convModel_ = model; |
| 186 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 187 | // Signal triggered when messages are received or their status updated |
| 188 | QObject::disconnect(newInteractionSignal_); |
| 189 | QObject::disconnect(interactionStatusUpdatedSignal_); |
| 190 | newInteractionSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newInteraction, |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 191 | [self](const std::string& uid, uint64_t interactionId, const lrc::api::interaction::Info& interaction){ |
| 192 | if (uid != convUid_) |
| 193 | return; |
| 194 | cachedConv_ = nil; |
| 195 | [conversationView noteNumberOfRowsChanged]; |
| 196 | [self reloadConversationForMessage:interactionId shouldUpdateHeight:YES]; |
| 197 | }); |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 198 | interactionStatusUpdatedSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::interactionStatusUpdated, |
| 199 | [self](const std::string& uid, uint64_t interactionId, const lrc::api::interaction::Info& interaction){ |
| 200 | if (uid != convUid_) |
| 201 | return; |
| 202 | cachedConv_ = nil; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 203 | bool isOutgoing = lrc::api::interaction::isOutgoing(interaction); |
| 204 | if (interaction.type == lrc::api::interaction::Type::TEXT && isOutgoing) { |
| 205 | convModel_->refreshFilter(); |
| 206 | } |
| 207 | [self reloadConversationForMessage:interactionId shouldUpdateHeight:YES]; |
| 208 | //accept incoming transfer |
| 209 | if (interaction.type == lrc::api::interaction::Type::INCOMING_DATA_TRANSFER && |
| 210 | (interaction.status == lrc::api::interaction::Status::TRANSFER_AWAITING_HOST || |
| 211 | interaction.status == lrc::api::interaction::Status::TRANSFER_CREATED)) { |
| 212 | lrc::api::datatransfer::Info info = {}; |
| 213 | convModel_->getTransferInfo(interactionId, info); |
| 214 | double convertData = static_cast<double>(info.totalSize); |
| 215 | NSString* pathUrl = @(info.displayName.c_str()); |
| 216 | |
| 217 | NSString* fileExtension = pathUrl.pathExtension; |
| 218 | |
| 219 | CFStringRef utiType = UTTypeCreatePreferredIdentifierForTag( |
| 220 | kUTTagClassFilenameExtension, (__bridge CFStringRef)fileExtension, NULL); |
| 221 | |
| 222 | bool isImage = UTTypeConformsTo(utiType, kUTTypeImage); |
| 223 | if( convertData <= 10485760 && isImage) { |
| 224 | [self acceptFile:interactionId]; |
| 225 | } |
| 226 | } |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 227 | }); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 228 | |
| 229 | // Signals tracking changes in conversation list, we need them as cached conversation can be invalid |
| 230 | // after a reordering. |
| 231 | QObject::disconnect(modelSortedSignal_); |
| 232 | QObject::disconnect(filterChangedSignal_); |
| 233 | modelSortedSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted, |
| 234 | [self](){ |
| 235 | cachedConv_ = nil; |
| 236 | }); |
| 237 | filterChangedSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged, |
| 238 | [self](){ |
| 239 | cachedConv_ = nil; |
| 240 | }); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 241 | [conversationView reloadData]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 242 | [conversationView scrollToEndOfDocument:nil]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 243 | } |
| 244 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 245 | #pragma mark - configure cells |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 246 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 247 | -(NSTableCellView*) makeGenericInteractionViewForTableView:(NSTableView*)tableView withText:(NSString*)text andTime:(NSString*) time |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 248 | { |
| 249 | NSTableCellView* result = [tableView makeViewWithIdentifier:@"GenericInteractionView" owner:self]; |
| 250 | NSTextField* textField = [result viewWithTag:GENERIC_INT_TEXT_TAG]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 251 | NSTextField* timeField = [result viewWithTag:GENERIC_INT_TIME_TAG]; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 252 | |
| 253 | // TODO: Fix symbol in LRC |
| 254 | NSString* fixedString = [text stringByReplacingOccurrencesOfString:@"🕽" withString:@"📞"]; |
| 255 | [textField setStringValue:fixedString]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 256 | [timeField setStringValue:time]; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 257 | |
| 258 | return result; |
| 259 | } |
| 260 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 261 | -(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] | 262 | { |
| 263 | IMTableCellView* result; |
| 264 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 265 | auto type = interaction.type; |
| 266 | auto status = interaction.status; |
| 267 | |
| 268 | NSString* fileName = @"incoming file"; |
| 269 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 270 | // First, view is created |
| 271 | if (type == lrc::api::interaction::Type::INCOMING_DATA_TRANSFER) { |
| 272 | switch (status) { |
| 273 | case lrc::api::interaction::Status::TRANSFER_CREATED: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 274 | case lrc::api::interaction::Status::TRANSFER_AWAITING_HOST: { |
| 275 | result = [tableView makeViewWithIdentifier:@"LeftIncomingFileView" owner: conversationView]; |
| 276 | [result.acceptButton setAction:@selector(acceptIncomingFile:)]; |
| 277 | [result.acceptButton setTarget:self]; |
| 278 | [result.declineButton setAction:@selector(declineIncomingFile:)]; |
| 279 | [result.declineButton setTarget:self]; |
| 280 | break;} |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 281 | case lrc::api::interaction::Status::TRANSFER_ACCEPTED: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 282 | case lrc::api::interaction::Status::TRANSFER_ONGOING: { |
| 283 | result = [tableView makeViewWithIdentifier:@"LeftOngoingFileView" owner:conversationView]; |
| 284 | [result.progressIndicator startAnimation:conversationView]; |
| 285 | [result.declineButton setAction:@selector(declineIncomingFile:)]; |
| 286 | [result.declineButton setTarget:self]; |
| 287 | break;} |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 288 | case lrc::api::interaction::Status::TRANSFER_FINISHED: |
| 289 | case lrc::api::interaction::Status::TRANSFER_CANCELED: |
| 290 | case lrc::api::interaction::Status::TRANSFER_ERROR: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 291 | result = [tableView makeViewWithIdentifier:@"LeftFinishedFileView" owner:conversationView]; |
| 292 | break; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 293 | } |
| 294 | } else if (type == lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER) { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 295 | NSString* fileName = @"sent file"; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 296 | switch (status) { |
| 297 | case lrc::api::interaction::Status::TRANSFER_CREATED: |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 298 | case lrc::api::interaction::Status::TRANSFER_ONGOING: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 299 | case lrc::api::interaction::Status::TRANSFER_AWAITING_PEER: |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 300 | case lrc::api::interaction::Status::TRANSFER_ACCEPTED: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 301 | result = [tableView makeViewWithIdentifier:@"RightOngoingFileView" owner:conversationView]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 302 | [result.progressIndicator startAnimation:nil]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 303 | [result.declineButton setAction:@selector(declineIncomingFile:)]; |
| 304 | [result.declineButton setTarget:self]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 305 | break; |
| 306 | case lrc::api::interaction::Status::TRANSFER_FINISHED: |
| 307 | case lrc::api::interaction::Status::TRANSFER_CANCELED: |
| 308 | case lrc::api::interaction::Status::TRANSFER_ERROR: |
Olivier Soldano | e521a18 | 2018-02-26 16:55:19 -0500 | [diff] [blame] | 309 | case lrc::api::interaction::Status::TRANSFER_UNJOINABLE_PEER: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 310 | result = [tableView makeViewWithIdentifier:@"RightFinishedFileView" owner:conversationView]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | // Then status label is updated if needed |
| 315 | switch (status) { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 316 | [result.statusLabel setTextColor:[NSColor textColor]]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 317 | case lrc::api::interaction::Status::TRANSFER_FINISHED: |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame^] | 318 | [result.statusLabel setTextColor:[NSColor greenSuccessColor]]; |
Anthony Léonard | 70638f0 | 2018-02-05 11:10:19 -0500 | [diff] [blame] | 319 | [result.statusLabel setStringValue:NSLocalizedString(@"Success", @"File transfer successful label")]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 320 | break; |
| 321 | case lrc::api::interaction::Status::TRANSFER_CANCELED: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 322 | [result.statusLabel setTextColor:[NSColor orangeColor]]; |
Anthony Léonard | 70638f0 | 2018-02-05 11:10:19 -0500 | [diff] [blame] | 323 | [result.statusLabel setStringValue:NSLocalizedString(@"Canceled", @"File transfer canceled label")]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 324 | break; |
| 325 | case lrc::api::interaction::Status::TRANSFER_ERROR: |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame^] | 326 | [result.statusLabel setTextColor:[NSColor errorTransferColor]]; |
Anthony Léonard | 70638f0 | 2018-02-05 11:10:19 -0500 | [diff] [blame] | 327 | [result.statusLabel setStringValue:NSLocalizedString(@"Failed", @"File transfer failed label")]; |
Olivier Soldano | e521a18 | 2018-02-26 16:55:19 -0500 | [diff] [blame] | 328 | break; |
| 329 | case lrc::api::interaction::Status::TRANSFER_UNJOINABLE_PEER: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 330 | [result.statusLabel setTextColor:[NSColor textColor]]; |
Olivier Soldano | e521a18 | 2018-02-26 16:55:19 -0500 | [diff] [blame] | 331 | [result.statusLabel setStringValue:NSLocalizedString(@"Unjoinable", @"File transfer peer unjoinable label")]; |
| 332 | break; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 333 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 334 | result.transferedImage.image = nil; |
| 335 | [result.msgBackground setHidden:NO]; |
| 336 | [result invalidateImageConstraints]; |
| 337 | NSString* name = @(interaction.body.c_str()); |
| 338 | if (name.length > 0) { |
| 339 | if (([name rangeOfString:@"/"].location != NSNotFound)) { |
| 340 | NSArray *listItems = [name componentsSeparatedByString:@"/"]; |
| 341 | NSString* name1 = listItems.lastObject; |
| 342 | fileName = name1; |
| 343 | } else { |
| 344 | fileName = name; |
| 345 | } |
| 346 | } |
| 347 | result.transferedFileName.stringValue = fileName; |
| 348 | if (status == lrc::api::interaction::Status::TRANSFER_FINISHED) { |
| 349 | NSImage* image = [self getImageForFilePath:name]; |
| 350 | if (([name rangeOfString:@"/"].location == NSNotFound)) { |
| 351 | image = [self getImageForFilePath:[self getDataTransferPath:interactionID]]; |
| 352 | } |
| 353 | if(image != nil) { |
| 354 | result.transferedImage.image = [image roundCorners:14]; |
| 355 | [result updateImageConstraint:image.size.width andHeight:image.size.height]; |
| 356 | } |
| 357 | } |
| 358 | [result setupForInteraction:interactionID]; |
| 359 | NSDate* msgTime = [NSDate dateWithTimeIntervalSince1970:interaction.timestamp]; |
| 360 | NSString* timeString = [self timeForMessage: msgTime]; |
| 361 | result.timeLabel.stringValue = timeString; |
| 362 | bool isOutgoing = lrc::api::interaction::isOutgoing(interaction); |
| 363 | if (!isOutgoing) { |
| 364 | auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator()); |
| 365 | auto* conv = [self getCurrentConversation]; |
| 366 | [result.photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(*conv, convModel_->owner)))]; |
| 367 | } |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 368 | return result; |
| 369 | } |
| 370 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 371 | #pragma mark - NSTableViewDelegate methods |
| 372 | - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 373 | { |
| 374 | return YES; |
| 375 | } |
| 376 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 377 | - (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 378 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 379 | return NO; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 380 | } |
| 381 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 382 | - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 383 | { |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 384 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 385 | auto* conv = [self getCurrentConversation]; |
| 386 | |
| 387 | if (conv == nil) |
| 388 | return nil; |
| 389 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 390 | auto it = conv->interactions.begin(); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 391 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 392 | std::advance(it, row); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 393 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 394 | IMTableCellView* result; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 395 | auto interaction = it->second; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 396 | bool isOutgoing = lrc::api::interaction::isOutgoing(interaction); |
| 397 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 398 | switch (interaction.type) { |
| 399 | case lrc::api::interaction::Type::TEXT: |
| 400 | if (isOutgoing) { |
| 401 | result = [tableView makeViewWithIdentifier:@"RightMessageView" owner:self]; |
| 402 | } else { |
| 403 | result = [tableView makeViewWithIdentifier:@"LeftMessageView" owner:self]; |
| 404 | } |
| 405 | break; |
| 406 | case lrc::api::interaction::Type::INCOMING_DATA_TRANSFER: |
| 407 | case lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 408 | return [self configureViewforTransfer:interaction interactionID: it->first tableView:tableView]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 409 | break; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 410 | case lrc::api::interaction::Type::CONTACT: |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 411 | case lrc::api::interaction::Type::CALL: { |
| 412 | NSDate* msgTime = [NSDate dateWithTimeIntervalSince1970:interaction.timestamp]; |
| 413 | NSString* timeString = [self timeForMessage: msgTime]; |
| 414 | return [self makeGenericInteractionViewForTableView:tableView withText:@(interaction.body.c_str()) andTime:timeString]; |
| 415 | } |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 416 | default: // If interaction is not of a known type |
| 417 | return nil; |
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 | MessageSequencing sequence = [self computeSequencingFor:row]; |
| 420 | BubbleType type = SINGLE; |
| 421 | if (sequence == FIRST_WITHOUT_TIME || sequence == FIRST_WITH_TIME) { |
| 422 | type = FIRST; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 423 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 424 | if (sequence == MIDDLE_IN_SEQUENCE) { |
| 425 | type = MIDDLE; |
| 426 | } |
| 427 | if (sequence == LAST_IN_SEQUENCE) { |
| 428 | type = LAST; |
| 429 | } |
| 430 | result.msgBackground.type = type; |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame^] | 431 | bool sendingFail = false; |
| 432 | [result.messageStatus setHidden:YES]; |
| 433 | if (interaction.type == lrc::api::interaction::Type::TEXT && isOutgoing) { |
| 434 | if (interaction.status == lrc::api::interaction::Status::SENDING) { |
| 435 | [result.messageStatus setHidden:NO]; |
| 436 | [result.sendingMessageIndicator startAnimation:nil]; |
| 437 | [result.messageFailed setHidden:YES]; |
| 438 | } else if (interaction.status == lrc::api::interaction::Status::FAILED) { |
| 439 | [result.messageStatus setHidden:NO]; |
| 440 | [result.sendingMessageIndicator setHidden:YES]; |
| 441 | [result.messageFailed setHidden:NO]; |
| 442 | sendingFail = true; |
| 443 | } |
| 444 | } |
| 445 | [result setupForInteraction:it->first isFailed: sendingFail]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 446 | bool shouldDisplayTime = (sequence == FIRST_WITH_TIME || sequence == SINGLE_WITH_TIME) ? YES : NO; |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 447 | bool shouldApplyPadding = (sequence == FIRST_WITHOUT_TIME || sequence == SINGLE_WITHOUT_TIME) ? YES : NO; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 448 | [result.msgBackground setNeedsDisplay:YES]; |
| 449 | [result setNeedsDisplay:YES]; |
| 450 | [result.timeBox setNeedsDisplay:YES]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 451 | |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame^] | 452 | NSString *text = @(interaction.body.c_str()); |
| 453 | text = [text removeEmptyLinesAtBorders]; |
| 454 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 455 | NSMutableAttributedString* msgAttString = |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame^] | 456 | [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:text] |
Kateryna Kostiuk | 3308987 | 2017-07-14 16:43:59 -0400 | [diff] [blame] | 457 | attributes:[self messageAttributes]]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 458 | |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame^] | 459 | CGSize messageSize = [self sizeFor: text maxWidth:tableView.frame.size.width * 0.7]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 460 | |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 461 | [result updateMessageConstraint:messageSize.width andHeight:messageSize.height timeIsVisible:shouldDisplayTime isTopPadding: shouldApplyPadding]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 462 | [[result.msgView textStorage] appendAttributedString:msgAttString]; |
| 463 | [result.msgView checkTextInDocument:nil]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 464 | |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame^] | 465 | NSDataDetector *linkDetector = [NSDataDetector dataDetectorWithTypes:NSTextCheckingTypeLink error:nil]; |
| 466 | NSArray *matches = [linkDetector matchesInString:result.msgView.string options:0 range:NSMakeRange(0, result.msgView.string.length)]; |
| 467 | |
| 468 | [result.msgView.textStorage beginEditing]; |
| 469 | |
| 470 | for (NSTextCheckingResult *match in matches) { |
| 471 | if (!match.URL) continue; |
| 472 | |
| 473 | NSDictionary *linkAttributes = @{ |
| 474 | NSLinkAttributeName: match.URL, |
| 475 | }; |
| 476 | [result.msgView.textStorage addAttributes:linkAttributes range:match.range]; |
| 477 | } |
| 478 | |
| 479 | [result.msgView.textStorage endEditing]; |
| 480 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 481 | if (shouldDisplayTime) { |
| 482 | NSDate* msgTime = [NSDate dateWithTimeIntervalSince1970:interaction.timestamp]; |
| 483 | NSString* timeString = [self timeForMessage: msgTime]; |
| 484 | result.timeLabel.stringValue = timeString; |
Anthony Léonard | 64e1967 | 2018-01-18 16:40:34 -0500 | [diff] [blame] | 485 | } |
| 486 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 487 | bool shouldDisplayAvatar = (sequence != MIDDLE_IN_SEQUENCE && sequence != FIRST_WITHOUT_TIME |
| 488 | && sequence != FIRST_WITH_TIME) ? YES : NO; |
| 489 | [result.photoView setHidden:!shouldDisplayAvatar]; |
| 490 | if (!isOutgoing && shouldDisplayAvatar) { |
| 491 | auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator()); |
| 492 | [result.photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(*conv, convModel_->owner)))]; |
| 493 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 494 | return result; |
| 495 | } |
| 496 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 497 | - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 498 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 499 | double someWidth = tableView.frame.size.width * 0.7; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 500 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 501 | auto* conv = [self getCurrentConversation]; |
| 502 | |
| 503 | if (conv == nil) |
| 504 | return 0; |
| 505 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 506 | auto it = conv->interactions.begin(); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 507 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 508 | std::advance(it, row); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 509 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 510 | auto interaction = it->second; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 511 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 512 | MessageSequencing sequence = [self computeSequencingFor:row]; |
| 513 | |
| 514 | bool shouldDisplayTime = (sequence == FIRST_WITH_TIME || sequence == SINGLE_WITH_TIME) ? YES : NO; |
| 515 | |
| 516 | |
| 517 | if(interaction.type == lrc::api::interaction::Type::INCOMING_DATA_TRANSFER || interaction.type == lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER) { |
| 518 | |
| 519 | if( interaction.status == lrc::api::interaction::Status::TRANSFER_FINISHED) { |
| 520 | NSString* name = @(interaction.body.c_str()); |
| 521 | NSImage* image = [self getImageForFilePath:name]; |
| 522 | if (([name rangeOfString:@"/"].location == NSNotFound)) { |
| 523 | image = [self getImageForFilePath:[self getDataTransferPath:it->first]]; |
| 524 | } |
| 525 | if (image != nil) { |
| 526 | return image.size.height + TIME_BOX_HEIGHT; |
| 527 | } |
| 528 | } |
| 529 | return BUBBLE_HEIGHT_FOR_TRANSFERED_FILE + TIME_BOX_HEIGHT; |
| 530 | } |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 531 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 532 | if(interaction.type == lrc::api::interaction::Type::CONTACT || interaction.type == lrc::api::interaction::Type::CALL) |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 533 | return GENERIC_CELL_HEIGHT; |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 534 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 535 | // TODO Implement interactions other than messages |
| 536 | if(interaction.type != lrc::api::interaction::Type::TEXT) { |
| 537 | return 0; |
| 538 | } |
| 539 | |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame^] | 540 | NSString *text = @(interaction.body.c_str()); |
| 541 | text = [text removeEmptyLinesAtBorders]; |
| 542 | |
| 543 | CGSize messageSize = [self sizeFor: text maxWidth:tableView.frame.size.width * 0.7]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 544 | CGFloat singleLignMessageHeight = 15; |
| 545 | |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 546 | bool shouldApplyPadding = (sequence == FIRST_WITHOUT_TIME || sequence == SINGLE_WITHOUT_TIME) ? YES : NO; |
| 547 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 548 | if (shouldDisplayTime) { |
| 549 | return MAX(messageSize.height + TIME_BOX_HEIGHT + MESSAGE_TEXT_PADDING * 2, |
| 550 | TIME_BOX_HEIGHT + MESSAGE_TEXT_PADDING * 2 + singleLignMessageHeight); |
| 551 | } |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 552 | if(shouldApplyPadding) { |
| 553 | return MAX(messageSize.height + MESSAGE_TEXT_PADDING * 2 + 15, |
| 554 | singleLignMessageHeight + MESSAGE_TEXT_PADDING * 2 + 15); |
| 555 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 556 | return MAX(messageSize.height + MESSAGE_TEXT_PADDING * 2, |
| 557 | singleLignMessageHeight + MESSAGE_TEXT_PADDING * 2); |
| 558 | } |
| 559 | |
| 560 | #pragma mark - message view parameters |
| 561 | |
| 562 | -(NSString *) getDataTransferPath:(uint64_t)interactionId { |
| 563 | lrc::api::datatransfer::Info info = {}; |
| 564 | convModel_->getTransferInfo(interactionId, info); |
| 565 | double convertData = static_cast<double>(info.totalSize); |
| 566 | return @(info.path.c_str()); |
| 567 | } |
| 568 | |
| 569 | -(NSImage*) getImageForFilePath: (NSString *) path { |
| 570 | if (path.length <= 0) {return nil;} |
| 571 | if (![[NSFileManager defaultManager] fileExistsAtPath: path]) {return nil;} |
| 572 | NSImage* transferedImage = [[NSImage alloc] initWithContentsOfFile: path]; |
| 573 | if(transferedImage != nil) { |
| 574 | return [transferedImage imageResizeInsideMax: MAX_TRANSFERED_IMAGE_SIZE]; |
| 575 | } |
| 576 | return nil; |
| 577 | } |
| 578 | |
| 579 | -(CGSize) sizeFor:(NSString *) message maxWidth:(CGFloat) width { |
| 580 | CGFloat horizaontalMargin = 6; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 581 | NSMutableAttributedString* msgAttString = |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 582 | [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@", message] |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 583 | attributes:[self messageAttributes]]; |
| 584 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 585 | CGFloat finalWidth = MIN(msgAttString.size.width + horizaontalMargin * 2, width); |
| 586 | NSRect frame = NSMakeRect(0, 0, finalWidth, msgAttString.size.height); |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 587 | NSTextView *tv = [[NSTextView alloc] initWithFrame:frame]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 588 | [[tv textStorage] setAttributedString:msgAttString]; |
| 589 | [tv sizeToFit]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 590 | return tv.frame.size; |
| 591 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 592 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 593 | -(MessageSequencing) computeSequencingFor:(NSInteger) row { |
| 594 | auto* conv = [self getCurrentConversation]; |
| 595 | if (conv == nil) |
| 596 | return SINGLE_WITHOUT_TIME; |
| 597 | auto it = conv->interactions.begin(); |
| 598 | std::advance(it, row); |
| 599 | auto interaction = it->second; |
| 600 | if (interaction.type != lrc::api::interaction::Type::TEXT) { |
| 601 | return SINGLE_WITH_TIME; |
| 602 | } |
| 603 | if (row == 0) { |
| 604 | if (it == conv->interactions.end()) { |
| 605 | return SINGLE_WITH_TIME; |
| 606 | } |
| 607 | auto nextIt = it; |
| 608 | nextIt++; |
| 609 | auto nextInteraction = nextIt->second; |
| 610 | if ([self sequenceChangedFrom:interaction to: nextInteraction]) { |
| 611 | return SINGLE_WITH_TIME; |
| 612 | } |
| 613 | return FIRST_WITH_TIME; |
| 614 | } |
| 615 | |
| 616 | if (row == conversationView.numberOfRows - 1) { |
| 617 | if(it == conv->interactions.begin()) { |
| 618 | return SINGLE_WITH_TIME; |
| 619 | } |
| 620 | auto previousIt = it; |
| 621 | previousIt--; |
| 622 | auto previousInteraction = previousIt->second; |
| 623 | bool timeChanged = [self sequenceTimeChangedFrom:interaction to:previousInteraction]; |
| 624 | bool authorChanged = [self sequenceAuthorChangedFrom:interaction to:previousInteraction]; |
| 625 | if (!timeChanged && !authorChanged) { |
| 626 | return LAST_IN_SEQUENCE; |
| 627 | } |
| 628 | if (!timeChanged && authorChanged) { |
| 629 | return SINGLE_WITHOUT_TIME; |
| 630 | } |
| 631 | return SINGLE_WITH_TIME; |
| 632 | } |
| 633 | if(it == conv->interactions.begin() || it == conv->interactions.end()) { |
| 634 | return SINGLE_WITH_TIME; |
| 635 | } |
| 636 | auto previousIt = it; |
| 637 | previousIt--; |
| 638 | auto previousInteraction = previousIt->second; |
| 639 | auto nextIt = it; |
| 640 | nextIt++; |
| 641 | auto nextInteraction = nextIt->second; |
| 642 | |
| 643 | bool timeChanged = [self sequenceTimeChangedFrom:interaction to:previousInteraction]; |
| 644 | bool authorChanged = [self sequenceAuthorChangedFrom:interaction to:previousInteraction]; |
| 645 | bool sequenceWillChange = [self sequenceChangedFrom:interaction to: nextInteraction]; |
Kateryna Kostiuk | 9d8b792 | 2018-05-02 12:52:53 -0400 | [diff] [blame] | 646 | if (previousInteraction.type == lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER || |
| 647 | previousInteraction.type == lrc::api::interaction::Type::INCOMING_DATA_TRANSFER) { |
| 648 | if(!sequenceWillChange) { |
| 649 | return FIRST_WITH_TIME; |
| 650 | } |
| 651 | return SINGLE_WITH_TIME; |
| 652 | } |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 653 | if (!sequenceWillChange) { |
| 654 | if (!timeChanged && !authorChanged) { |
| 655 | return MIDDLE_IN_SEQUENCE; |
| 656 | } |
| 657 | if (timeChanged) { |
| 658 | return FIRST_WITH_TIME; |
| 659 | } |
| 660 | return FIRST_WITHOUT_TIME; |
| 661 | } if (!timeChanged && !authorChanged) { |
| 662 | return LAST_IN_SEQUENCE; |
| 663 | } if (timeChanged) { |
| 664 | return SINGLE_WITH_TIME; |
| 665 | } |
| 666 | return SINGLE_WITHOUT_TIME; |
| 667 | } |
| 668 | |
| 669 | -(bool) sequenceChangedFrom:(lrc::api::interaction::Info) firstInteraction to:(lrc::api::interaction::Info) secondInteraction { |
| 670 | return ([self sequenceTimeChangedFrom:firstInteraction to:secondInteraction] || [self sequenceAuthorChangedFrom:firstInteraction to:secondInteraction]); |
| 671 | } |
| 672 | |
| 673 | -(bool) sequenceTimeChangedFrom:(lrc::api::interaction::Info) firstInteraction to:(lrc::api::interaction::Info) secondInteraction { |
| 674 | bool timeChanged = NO; |
| 675 | NSDate* firstMessageTime = [NSDate dateWithTimeIntervalSince1970:firstInteraction.timestamp]; |
| 676 | NSDate* secondMessageTime = [NSDate dateWithTimeIntervalSince1970:secondInteraction.timestamp]; |
| 677 | bool hourComp = [[NSCalendar currentCalendar] compareDate:firstMessageTime toDate:secondMessageTime toUnitGranularity:NSCalendarUnitHour]; |
| 678 | bool minutComp = [[NSCalendar currentCalendar] compareDate:firstMessageTime toDate:secondMessageTime toUnitGranularity:NSCalendarUnitMinute]; |
| 679 | if(hourComp != NSOrderedSame || minutComp != NSOrderedSame) { |
| 680 | timeChanged = YES; |
| 681 | } |
| 682 | return timeChanged; |
| 683 | } |
| 684 | |
| 685 | -(bool) sequenceAuthorChangedFrom:(lrc::api::interaction::Info) firstInteraction to:(lrc::api::interaction::Info) secondInteraction { |
| 686 | bool authorChanged = YES; |
| 687 | bool isOutgoing = lrc::api::interaction::isOutgoing(firstInteraction); |
| 688 | if ((secondInteraction.type == lrc::api::interaction::Type::TEXT) && (isOutgoing == lrc::api::interaction::isOutgoing(secondInteraction))) { |
| 689 | authorChanged = NO; |
| 690 | } |
| 691 | return authorChanged; |
| 692 | } |
| 693 | |
| 694 | -(NSString *)timeForMessage:(NSDate*) msgTime { |
| 695 | NSDate *today = [NSDate date]; |
| 696 | NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init]; |
| 697 | if ([[NSCalendar currentCalendar] compareDate:today |
| 698 | toDate:msgTime |
| 699 | toUnitGranularity:NSCalendarUnitYear]!= NSOrderedSame) { |
| 700 | return [NSDateFormatter localizedStringFromDate:msgTime dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle]; |
| 701 | } |
| 702 | |
| 703 | if ([[NSCalendar currentCalendar] compareDate:today |
| 704 | toDate:msgTime |
| 705 | toUnitGranularity:NSCalendarUnitDay]!= NSOrderedSame || |
| 706 | [[NSCalendar currentCalendar] compareDate:today |
| 707 | toDate:msgTime |
| 708 | toUnitGranularity:NSCalendarUnitMonth]!= NSOrderedSame) { |
| 709 | [dateFormatter setDateFormat:@"MMM dd, HH:mm"]; |
| 710 | return [dateFormatter stringFromDate:msgTime]; |
| 711 | } |
| 712 | |
| 713 | [dateFormatter setDateFormat:@"HH:mm"]; |
| 714 | return [dateFormatter stringFromDate:msgTime]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 715 | } |
| 716 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 717 | #pragma mark - NSTableViewDataSource |
| 718 | |
| 719 | - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView |
| 720 | { |
| 721 | auto* conv = [self getCurrentConversation]; |
| 722 | |
Anthony Léonard | f2bb17d | 2018-02-15 17:18:09 -0500 | [diff] [blame] | 723 | if (conv) |
| 724 | return conv->interactions.size(); |
| 725 | else |
| 726 | return 0; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 727 | } |
| 728 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 729 | #pragma mark - Text formatting |
| 730 | |
Kateryna Kostiuk | 3308987 | 2017-07-14 16:43:59 -0400 | [diff] [blame] | 731 | - (NSMutableDictionary*) messageAttributes |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 732 | { |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 733 | NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 734 | attrs[NSForegroundColorAttributeName] = [NSColor labelColor]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 735 | attrs[NSParagraphStyleAttributeName] = [self paragraphStyle]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 736 | return attrs; |
| 737 | } |
| 738 | |
| 739 | - (NSParagraphStyle*) paragraphStyle |
| 740 | { |
| 741 | /* |
| 742 | The only way to instantiate an NSMutableParagraphStyle is to mutably copy an |
| 743 | NSParagraphStyle. And since we don't have an existing NSParagraphStyle available |
| 744 | to copy, we use the default one. |
| 745 | |
| 746 | The default values supplied by the default NSParagraphStyle are: |
| 747 | Alignment NSNaturalTextAlignment |
| 748 | Tab stops 12 left-aligned tabs, spaced by 28.0 points |
| 749 | Line break mode NSLineBreakByWordWrapping |
| 750 | All others 0.0 |
| 751 | */ |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 752 | NSMutableParagraphStyle* aMutableParagraphStyle = |
| 753 | [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
| 754 | [aMutableParagraphStyle setHeadIndent:1.0]; |
| 755 | [aMutableParagraphStyle setFirstLineHeadIndent:1.0]; |
| 756 | return aMutableParagraphStyle; |
| 757 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 758 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 759 | - (void)acceptFile:(uint64_t)interactionID { |
| 760 | NSURL *downloadsURL = [[NSFileManager defaultManager] |
| 761 | URLForDirectory:NSDownloadsDirectory |
| 762 | inDomain:NSUserDomainMask appropriateForURL:nil |
| 763 | create:YES error:nil]; |
| 764 | auto& inter = [self getCurrentConversation]->interactions.find(interactionID)->second; |
| 765 | if (convModel_ && !convUid_.empty()) { |
| 766 | NSURL *bUrl = [downloadsURL URLByAppendingPathComponent:@(inter.body.c_str())]; |
| 767 | const char* fullPath = [bUrl fileSystemRepresentation]; |
| 768 | convModel_->acceptTransfer(convUid_, interactionID, fullPath); |
| 769 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 770 | } |
| 771 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 772 | #pragma mark - Actions |
| 773 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 774 | - (void)acceptIncomingFile:(id)sender { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 775 | auto interId = [(IMTableCellView*)[[sender superview] superview] interaction]; |
| 776 | auto& inter = [self getCurrentConversation]->interactions.find(interId)->second; |
| 777 | if (convModel_ && !convUid_.empty()) { |
| 778 | NSSavePanel* filePicker = [NSSavePanel savePanel]; |
| 779 | [filePicker setNameFieldStringValue:@(inter.body.c_str())]; |
| 780 | |
| 781 | if ([filePicker runModal] == NSFileHandlingPanelOKButton) { |
| 782 | const char* fullPath = [[filePicker URL] fileSystemRepresentation]; |
| 783 | convModel_->acceptTransfer(convUid_, interId, fullPath); |
| 784 | } |
| 785 | } |
| 786 | } |
| 787 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 788 | - (void)declineIncomingFile:(id)sender { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 789 | auto inter = [(IMTableCellView*)[[sender superview] superview] interaction]; |
| 790 | if (convModel_ && !convUid_.empty()) { |
| 791 | convModel_->cancelTransfer(convUid_, inter); |
| 792 | } |
| 793 | } |
| 794 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 795 | @end |