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" |
| 32 | #import "INDSequentialTextSelectionManager.h" |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 33 | #import "delegates/ImageManipulationDelegate.h" |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 34 | #import "utils.h" |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 35 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 36 | @interface MessagesVC () <NSTableViewDelegate, NSTableViewDataSource> { |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 37 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 38 | __unsafe_unretained IBOutlet NSTableView* conversationView; |
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 | std::string convUid_; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 41 | lrc::api::ConversationModel* convModel_; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 42 | const lrc::api::conversation::Info* cachedConv_; |
| 43 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 44 | QMetaObject::Connection newInteractionSignal_; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 45 | |
| 46 | // Both are needed to invalidate cached conversation as pointer |
| 47 | // may not be referencing the same conversation anymore |
| 48 | QMetaObject::Connection modelSortedSignal_; |
| 49 | QMetaObject::Connection filterChangedSignal_; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 50 | QMetaObject::Connection interactionStatusUpdatedSignal_; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @property (nonatomic, strong, readonly) INDSequentialTextSelectionManager* selectionManager; |
| 54 | |
| 55 | @end |
| 56 | |
| 57 | @implementation MessagesVC |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 58 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 59 | -(const lrc::api::conversation::Info*) getCurrentConversation |
| 60 | { |
| 61 | if (convModel_ == nil || convUid_.empty()) |
| 62 | return nil; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 63 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 64 | if (cachedConv_ != nil) |
| 65 | return cachedConv_; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 66 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 67 | auto& convQueue = convModel_->allFilteredConversations(); |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 68 | |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 69 | auto it = getConversationFromUid(convUid_, *convModel_); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 70 | |
| 71 | if (it != convQueue.end()) |
| 72 | cachedConv_ = &(*it); |
| 73 | |
| 74 | return cachedConv_; |
| 75 | } |
| 76 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 77 | -(void)setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 78 | { |
| 79 | if (convUid_ == convUid && convModel_ == model) |
| 80 | return; |
| 81 | |
| 82 | cachedConv_ = nil; |
| 83 | convUid_ = convUid; |
| 84 | convModel_ = model; |
| 85 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 86 | // Signal triggered when messages are received or their status updated |
| 87 | QObject::disconnect(newInteractionSignal_); |
| 88 | QObject::disconnect(interactionStatusUpdatedSignal_); |
| 89 | newInteractionSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newInteraction, |
Anthony Léonard | ff0cb03 | 2018-01-25 10:52:10 -0500 | [diff] [blame] | 90 | [self](const std::string& uid, uint64_t interactionId, const lrc::api::interaction::Info& interaction){ |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 91 | if (uid != convUid_) |
| 92 | return; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 93 | cachedConv_ = nil; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 94 | [conversationView reloadData]; |
| 95 | [conversationView scrollToEndOfDocument:nil]; |
| 96 | }); |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 97 | interactionStatusUpdatedSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::interactionStatusUpdated, |
| 98 | [self](const std::string& uid, uint64_t interactionId, const lrc::api::interaction::Info& interaction){ |
| 99 | if (uid != convUid_) |
| 100 | return; |
| 101 | cachedConv_ = nil; |
| 102 | [conversationView reloadData]; |
| 103 | [conversationView scrollToEndOfDocument:nil]; |
| 104 | }); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 105 | |
| 106 | // Signals tracking changes in conversation list, we need them as cached conversation can be invalid |
| 107 | // after a reordering. |
| 108 | QObject::disconnect(modelSortedSignal_); |
| 109 | QObject::disconnect(filterChangedSignal_); |
| 110 | modelSortedSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted, |
| 111 | [self](){ |
| 112 | cachedConv_ = nil; |
| 113 | }); |
| 114 | filterChangedSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged, |
| 115 | [self](){ |
| 116 | cachedConv_ = nil; |
| 117 | }); |
| 118 | |
| 119 | |
| 120 | |
| 121 | [conversationView reloadData]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 122 | [conversationView scrollToEndOfDocument:nil]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 123 | } |
| 124 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 125 | -(void)newMessageSent |
| 126 | { |
| 127 | [conversationView reloadData]; |
| 128 | [conversationView scrollToEndOfDocument:nil]; |
| 129 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 130 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 131 | -(IMTableCellView*) makeViewforTransferStatus:(lrc::api::interaction::Status)status type:(lrc::api::interaction::Type)type tableView:(NSTableView*)tableView |
| 132 | { |
| 133 | IMTableCellView* result; |
| 134 | |
| 135 | // First, view is created |
| 136 | if (type == lrc::api::interaction::Type::INCOMING_DATA_TRANSFER) { |
| 137 | switch (status) { |
| 138 | case lrc::api::interaction::Status::TRANSFER_CREATED: |
| 139 | case lrc::api::interaction::Status::TRANSFER_AWAITING: |
| 140 | result = [tableView makeViewWithIdentifier:@"LeftIncomingFileView" owner:self]; |
| 141 | break; |
| 142 | case lrc::api::interaction::Status::TRANSFER_ACCEPTED: |
| 143 | case lrc::api::interaction::Status::TRANSFER_ONGOING: |
| 144 | result = [tableView makeViewWithIdentifier:@"LeftOngoingFileView" owner:self]; |
| 145 | [result.progressIndicator startAnimation:nil]; |
| 146 | break; |
| 147 | case lrc::api::interaction::Status::TRANSFER_FINISHED: |
| 148 | case lrc::api::interaction::Status::TRANSFER_CANCELED: |
| 149 | case lrc::api::interaction::Status::TRANSFER_ERROR: |
| 150 | result = [tableView makeViewWithIdentifier:@"LeftFinishedFileView" owner:self]; |
| 151 | } |
| 152 | } else if (type == lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER) { |
| 153 | switch (status) { |
| 154 | case lrc::api::interaction::Status::TRANSFER_CREATED: |
| 155 | case lrc::api::interaction::Status::TRANSFER_AWAITING: |
| 156 | case lrc::api::interaction::Status::TRANSFER_ONGOING: |
| 157 | case lrc::api::interaction::Status::TRANSFER_ACCEPTED: |
| 158 | result = [tableView makeViewWithIdentifier:@"RightOngoingFileView" owner:self]; |
| 159 | [result.progressIndicator startAnimation:nil]; |
| 160 | break; |
| 161 | case lrc::api::interaction::Status::TRANSFER_FINISHED: |
| 162 | case lrc::api::interaction::Status::TRANSFER_CANCELED: |
| 163 | case lrc::api::interaction::Status::TRANSFER_ERROR: |
| 164 | result = [tableView makeViewWithIdentifier:@"RightFinishedFileView" owner:self]; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // Then status label is updated if needed |
| 169 | switch (status) { |
| 170 | case lrc::api::interaction::Status::TRANSFER_FINISHED: |
| 171 | [result.statusLabel setStringValue:@"Success"]; |
| 172 | break; |
| 173 | case lrc::api::interaction::Status::TRANSFER_CANCELED: |
| 174 | [result.statusLabel setStringValue:@"Canceled"]; |
| 175 | break; |
| 176 | case lrc::api::interaction::Status::TRANSFER_ERROR: |
| 177 | [result.statusLabel setStringValue:@"Failed"]; |
| 178 | } |
| 179 | return result; |
| 180 | } |
| 181 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 182 | #pragma mark - NSTableViewDelegate methods |
| 183 | - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 184 | { |
| 185 | return YES; |
| 186 | } |
| 187 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 188 | - (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 189 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 190 | return NO; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 191 | } |
| 192 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 193 | - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 194 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 195 | auto* conv = [self getCurrentConversation]; |
| 196 | |
| 197 | if (conv == nil) |
| 198 | return nil; |
| 199 | |
| 200 | // HACK HACK HACK HACK HACK |
| 201 | // The following code has to be replaced when every views are implemented for every interaction types |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 202 | // This is an iterator which "jumps over" any interaction which is not a text or datatransfer one. |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 203 | // It behaves as if interaction list was only containing text interactions. |
| 204 | std::map<uint64_t, lrc::api::interaction::Info>::const_iterator it; |
| 205 | |
| 206 | { |
| 207 | int msgCount = 0; |
| 208 | it = std::find_if(conv->interactions.begin(), conv->interactions.end(), [&msgCount, row](const std::pair<uint64_t, lrc::api::interaction::Info>& inter) { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 209 | if (inter.second.type == lrc::api::interaction::Type::TEXT |
| 210 | || inter.second.type == lrc::api::interaction::Type::INCOMING_DATA_TRANSFER |
| 211 | || inter.second.type == lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER) { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 212 | if (msgCount == row) { |
| 213 | return true; |
| 214 | } else { |
| 215 | msgCount++; |
| 216 | return false; |
| 217 | } |
| 218 | } |
| 219 | return false; |
| 220 | }); |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 221 | } |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 222 | |
| 223 | if (it == conv->interactions.end()) |
| 224 | return nil; |
| 225 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 226 | IMTableCellView* result; |
| 227 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 228 | auto& interaction = it->second; |
| 229 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 230 | bool isOutgoing = lrc::api::interaction::isOutgoing(interaction); |
| 231 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 232 | switch (interaction.type) { |
| 233 | case lrc::api::interaction::Type::TEXT: |
| 234 | if (isOutgoing) { |
| 235 | result = [tableView makeViewWithIdentifier:@"RightMessageView" owner:self]; |
| 236 | } else { |
| 237 | result = [tableView makeViewWithIdentifier:@"LeftMessageView" owner:self]; |
| 238 | } |
| 239 | break; |
| 240 | case lrc::api::interaction::Type::INCOMING_DATA_TRANSFER: |
| 241 | case lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER: |
| 242 | result = [self makeViewforTransferStatus:interaction.status type:interaction.type tableView:tableView]; |
| 243 | break; |
| 244 | default: // If interaction is not of a known type |
| 245 | return nil; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | // check if the message first in incoming or outgoing messages sequence |
| 249 | Boolean isFirstInSequence = true; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 250 | if (it != conv->interactions.begin()) { |
| 251 | auto previousIt = it; |
| 252 | previousIt--; |
| 253 | auto& previousInteraction = previousIt->second; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 254 | if ((previousInteraction.type == lrc::api::interaction::Type::TEXT |
| 255 | || previousInteraction.type == lrc::api::interaction::Type::INCOMING_DATA_TRANSFER |
| 256 | || previousInteraction.type == lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER) && (isOutgoing == lrc::api::interaction::isOutgoing(previousInteraction))) |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 257 | isFirstInSequence = false; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 258 | } |
| 259 | [result.photoView setHidden:!isFirstInSequence]; |
| 260 | result.msgBackground.needPointer = isFirstInSequence; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 261 | [result setupForInteraction:it->first]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 262 | |
| 263 | NSMutableAttributedString* msgAttString = |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 264 | [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n",@(interaction.body.c_str())] |
Kateryna Kostiuk | 3308987 | 2017-07-14 16:43:59 -0400 | [diff] [blame] | 265 | attributes:[self messageAttributes]]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 266 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 267 | NSDate *msgTime = [NSDate dateWithTimeIntervalSince1970:interaction.timestamp]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 268 | NSAttributedString* timestampAttrString = |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 269 | [[NSAttributedString alloc] initWithString:[NSDateFormatter localizedStringFromDate:msgTime dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle] |
Kateryna Kostiuk | 3308987 | 2017-07-14 16:43:59 -0400 | [diff] [blame] | 270 | attributes:[self timestampAttributes]]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 271 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 272 | CGFloat finalWidth = MAX(msgAttString.size.width, timestampAttrString.size.width); |
| 273 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 274 | finalWidth = MIN(finalWidth + 30, tableView.frame.size.width * 0.7); |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 275 | |
| 276 | [msgAttString appendAttributedString:timestampAttrString]; |
| 277 | [[result.msgView textStorage] appendAttributedString:msgAttString]; |
| 278 | [result.msgView checkTextInDocument:nil]; |
| 279 | [result updateWidthConstraint:finalWidth]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 280 | |
| 281 | auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator()); |
Anthony Léonard | 64e1967 | 2018-01-18 16:40:34 -0500 | [diff] [blame] | 282 | if (isOutgoing) { |
| 283 | [result.photoView setImage:[NSImage imageNamed:@"default_user_icon"]]; |
| 284 | } else { |
| 285 | [result.photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(*conv, convModel_->owner)))]; |
| 286 | } |
| 287 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 288 | return result; |
| 289 | } |
| 290 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 291 | - (void)tableView:(NSTableView *)tableView didAddRowView:(NSTableRowView *)rowView forRow:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 292 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 293 | if (IMTableCellView* cellView = [tableView viewAtColumn:0 row:row makeIfNecessary:NO]) { |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 294 | [self.selectionManager registerTextView:cellView.msgView withUniqueIdentifier:@(row).stringValue]; |
| 295 | } |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 296 | } |
| 297 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 298 | - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 299 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 300 | double someWidth = tableView.frame.size.width * 0.7; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 301 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 302 | auto* conv = [self getCurrentConversation]; |
| 303 | |
| 304 | if (conv == nil) |
| 305 | return 0; |
| 306 | |
| 307 | // HACK HACK HACK HACK HACK |
| 308 | // The following code has to be replaced when every views are implemented for every interaction types |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 309 | // This is an iterator which "jumps over" any interaction which is not a text or datatransfer one. |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 310 | // It behaves as if interaction list was only containing text interactions. |
| 311 | std::map<uint64_t, lrc::api::interaction::Info>::const_iterator it; |
| 312 | |
| 313 | { |
| 314 | int msgCount = 0; |
| 315 | it = std::find_if(conv->interactions.begin(), conv->interactions.end(), [&msgCount, row](const std::pair<uint64_t, lrc::api::interaction::Info>& inter) { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 316 | if (inter.second.type == lrc::api::interaction::Type::TEXT |
| 317 | || inter.second.type == lrc::api::interaction::Type::INCOMING_DATA_TRANSFER |
| 318 | || inter.second.type == lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER) { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 319 | if (msgCount == row) { |
| 320 | return true; |
| 321 | } else { |
| 322 | msgCount++; |
| 323 | return false; |
| 324 | } |
| 325 | } |
| 326 | return false; |
| 327 | }); |
| 328 | } |
| 329 | |
| 330 | if (it == conv->interactions.end()) |
| 331 | return 0; |
| 332 | |
| 333 | auto& interaction = it->second; |
| 334 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 335 | if(interaction.type == lrc::api::interaction::Type::INCOMING_DATA_TRANSFER || interaction.type == lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER) |
| 336 | return 52.0; |
| 337 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 338 | // TODO Implement interactions other than messages |
| 339 | if(interaction.type != lrc::api::interaction::Type::TEXT) { |
| 340 | return 0; |
| 341 | } |
| 342 | |
| 343 | NSMutableAttributedString* msgAttString = |
| 344 | [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@\n",@(interaction.body.c_str())] |
| 345 | attributes:[self messageAttributes]]; |
| 346 | |
| 347 | NSDate *msgTime = [NSDate dateWithTimeIntervalSince1970:interaction.timestamp]; |
| 348 | NSAttributedString* timestampAttrString = |
| 349 | [[NSAttributedString alloc] initWithString:[NSDateFormatter localizedStringFromDate:msgTime dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle] |
| 350 | attributes:[self timestampAttributes]]; |
| 351 | |
| 352 | [msgAttString appendAttributedString:timestampAttrString]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 353 | |
| 354 | [msgAttString appendAttributedString:timestampAttrString]; |
| 355 | |
| 356 | NSRect frame = NSMakeRect(0, 0, someWidth, MAXFLOAT); |
| 357 | NSTextView *tv = [[NSTextView alloc] initWithFrame:frame]; |
| 358 | [tv setEnabledTextCheckingTypes:NSTextCheckingTypeLink]; |
| 359 | [tv setAutomaticLinkDetectionEnabled:YES]; |
| 360 | [[tv textStorage] setAttributedString:msgAttString]; |
| 361 | [tv sizeToFit]; |
| 362 | |
| 363 | double height = tv.frame.size.height + 10; |
| 364 | return MAX(height, 50.0f); |
| 365 | } |
| 366 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 367 | #pragma mark - NSTableViewDataSource |
| 368 | |
| 369 | - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView |
| 370 | { |
| 371 | auto* conv = [self getCurrentConversation]; |
| 372 | |
| 373 | if (conv) { |
| 374 | int count; |
| 375 | count = std::count_if(conv->interactions.begin(), conv->interactions.end(), [](const std::pair<uint64_t, lrc::api::interaction::Info>& inter) { |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 376 | return inter.second.type == lrc::api::interaction::Type::TEXT |
| 377 | || inter.second.type == lrc::api::interaction::Type::INCOMING_DATA_TRANSFER |
| 378 | || inter.second.type == lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 379 | }); |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 380 | NSLog(@"$$$ Interaction count: %d", count); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 381 | return count; |
| 382 | } |
| 383 | return 0; |
| 384 | |
| 385 | #if 0 |
| 386 | // TODO: Replace above code by the following one when every interaction types implemented |
| 387 | if (conv_) { |
| 388 | return conv_->interactions.size(); |
| 389 | } |
| 390 | #endif |
| 391 | } |
| 392 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 393 | #pragma mark - Text formatting |
| 394 | |
Kateryna Kostiuk | 3308987 | 2017-07-14 16:43:59 -0400 | [diff] [blame] | 395 | - (NSMutableDictionary*) timestampAttributes |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 396 | { |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 397 | NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; |
Kateryna Kostiuk | 3308987 | 2017-07-14 16:43:59 -0400 | [diff] [blame] | 398 | attrs[NSForegroundColorAttributeName] = [NSColor grayColor]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 399 | NSFont* systemFont = [NSFont systemFontOfSize:12.0f]; |
| 400 | attrs[NSFontAttributeName] = systemFont; |
| 401 | attrs[NSParagraphStyleAttributeName] = [self paragraphStyle]; |
| 402 | |
| 403 | return attrs; |
| 404 | } |
| 405 | |
Kateryna Kostiuk | 3308987 | 2017-07-14 16:43:59 -0400 | [diff] [blame] | 406 | - (NSMutableDictionary*) messageAttributes |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 407 | { |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 408 | NSMutableDictionary* attrs = [NSMutableDictionary dictionary]; |
Kateryna Kostiuk | 3308987 | 2017-07-14 16:43:59 -0400 | [diff] [blame] | 409 | attrs[NSForegroundColorAttributeName] = [NSColor blackColor]; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 410 | NSFont* systemFont = [NSFont systemFontOfSize:14.0f]; |
| 411 | attrs[NSFontAttributeName] = systemFont; |
| 412 | attrs[NSParagraphStyleAttributeName] = [self paragraphStyle]; |
| 413 | |
| 414 | return attrs; |
| 415 | } |
| 416 | |
| 417 | - (NSParagraphStyle*) paragraphStyle |
| 418 | { |
| 419 | /* |
| 420 | The only way to instantiate an NSMutableParagraphStyle is to mutably copy an |
| 421 | NSParagraphStyle. And since we don't have an existing NSParagraphStyle available |
| 422 | to copy, we use the default one. |
| 423 | |
| 424 | The default values supplied by the default NSParagraphStyle are: |
| 425 | Alignment NSNaturalTextAlignment |
| 426 | Tab stops 12 left-aligned tabs, spaced by 28.0 points |
| 427 | Line break mode NSLineBreakByWordWrapping |
| 428 | All others 0.0 |
| 429 | */ |
| 430 | NSMutableParagraphStyle* aMutableParagraphStyle = |
| 431 | [[NSParagraphStyle defaultParagraphStyle] mutableCopy]; |
| 432 | |
| 433 | // Now adjust our NSMutableParagraphStyle formatting to be whatever we want. |
| 434 | // The numeric values below are in points (72 points per inch) |
| 435 | [aMutableParagraphStyle setLineSpacing:1.5]; |
| 436 | [aMutableParagraphStyle setParagraphSpacing:5.0]; |
| 437 | [aMutableParagraphStyle setHeadIndent:5.0]; |
| 438 | [aMutableParagraphStyle setTailIndent:-5.0]; |
| 439 | [aMutableParagraphStyle setFirstLineHeadIndent:5.0]; |
| 440 | return aMutableParagraphStyle; |
| 441 | } |
| 442 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame^] | 443 | #pragma mark - Actions |
| 444 | |
| 445 | - (IBAction)acceptIncomingFile:(id)sender { |
| 446 | auto interId = [(IMTableCellView*)[[sender superview] superview] interaction]; |
| 447 | auto& inter = [self getCurrentConversation]->interactions.find(interId)->second; |
| 448 | if (convModel_ && !convUid_.empty()) { |
| 449 | NSSavePanel* filePicker = [NSSavePanel savePanel]; |
| 450 | [filePicker setNameFieldStringValue:@(inter.body.c_str())]; |
| 451 | |
| 452 | if ([filePicker runModal] == NSFileHandlingPanelOKButton) { |
| 453 | const char* fullPath = [[filePicker URL] fileSystemRepresentation]; |
| 454 | convModel_->acceptTransfer(convUid_, interId, fullPath); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | - (IBAction)declineIncomingFile:(id)sender { |
| 460 | auto inter = [(IMTableCellView*)[[sender superview] superview] interaction]; |
| 461 | if (convModel_ && !convUid_.empty()) { |
| 462 | convModel_->cancelTransfer(convUid_, inter); |
| 463 | } |
| 464 | } |
| 465 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 466 | @end |