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