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