Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 1 | /* |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 2 | * Copyright (C) 2016-2018 Savoir-faire Linux Inc. |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 3 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 4 | * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com> |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [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 "ConversationVC.h" |
| 22 | |
| 23 | #import <QItemSelectionModel> |
| 24 | #import <qstring.h> |
| 25 | #import <QPixmap> |
| 26 | #import <QtMacExtras/qmacfunctions.h> |
| 27 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 28 | // LRC |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 29 | #import <globalinstances.h> |
| 30 | |
| 31 | #import "views/IconButton.h" |
| 32 | #import "views/IMTableCellView.h" |
| 33 | #import "views/NSColor+RingTheme.h" |
| 34 | #import "QNSTreeController.h" |
Alexandre Lision | 83180df | 2016-01-18 11:32:20 -0500 | [diff] [blame] | 35 | #import "INDSequentialTextSelectionManager.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 36 | #import "delegates/ImageManipulationDelegate.h" |
Kateryna Kostiuk | 0ba1baf | 2017-05-12 16:51:27 -0400 | [diff] [blame] | 37 | #import "PhoneDirectoryModel.h" |
| 38 | #import "account.h" |
| 39 | #import "AvailableAccountModel.h" |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 40 | #import "MessagesVC.h" |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 41 | #import "utils.h" |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 42 | #import "RingWindowController.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 43 | |
| 44 | #import <QuartzCore/QuartzCore.h> |
| 45 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 46 | @interface ConversationVC () { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 47 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 48 | __unsafe_unretained IBOutlet NSTextField* messageField; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 49 | NSMutableString* textSelection; |
| 50 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 51 | __unsafe_unretained IBOutlet NSView* sendPanel; |
| 52 | __unsafe_unretained IBOutlet NSTextField* conversationTitle; |
Olivier Soldano | 13c69e3 | 2018-01-05 12:01:36 -0500 | [diff] [blame] | 53 | __unsafe_unretained IBOutlet NSTextField *conversationID; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 54 | __unsafe_unretained IBOutlet IconButton* sendButton; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 55 | __unsafe_unretained IBOutlet IconButton *sendFileButton; |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 56 | __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth; |
| 57 | __unsafe_unretained IBOutlet NSButton* sentContactRequestButton; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 58 | IBOutlet MessagesVC* messagesViewVC; |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame] | 59 | |
Olivier Soldano | f563e15 | 2018-01-09 16:08:32 -0500 | [diff] [blame] | 60 | IBOutlet NSLayoutConstraint *titleCenteredConstraint; |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame] | 61 | IBOutlet NSLayoutConstraint* titleTopConstraint; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 62 | |
| 63 | std::string convUid_; |
| 64 | const lrc::api::conversation::Info* cachedConv_; |
| 65 | lrc::api::ConversationModel* convModel_; |
| 66 | |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 67 | RingWindowController* delegate; |
| 68 | |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 69 | // All those connections are needed to invalidate cached conversation as pointer |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 70 | // may not be referencing the same conversation anymore |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 71 | QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_; |
| 72 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 73 | } |
| 74 | |
Alexandre Lision | 83180df | 2016-01-18 11:32:20 -0500 | [diff] [blame] | 75 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 76 | @end |
| 77 | |
| 78 | @implementation ConversationVC |
| 79 | |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 80 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil delegate:(RingWindowController*) mainWindow |
| 81 | { |
| 82 | if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) |
| 83 | { |
| 84 | delegate = mainWindow; |
| 85 | } |
| 86 | return self; |
| 87 | } |
| 88 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 89 | -(const lrc::api::conversation::Info*) getCurrentConversation |
| 90 | { |
| 91 | if (convModel_ == nil || convUid_.empty()) |
| 92 | return nil; |
| 93 | |
| 94 | if (cachedConv_ != nil) |
| 95 | return cachedConv_; |
| 96 | |
| 97 | auto& convQueue = convModel_->allFilteredConversations(); |
| 98 | |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 99 | auto it = getConversationFromUid(convUid_, *convModel_); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 100 | |
| 101 | if (it != convQueue.end()) |
| 102 | cachedConv_ = &(*it); |
| 103 | |
| 104 | return cachedConv_; |
| 105 | } |
| 106 | |
| 107 | -(void) setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model { |
| 108 | if (convUid_ == convUid && convModel_ == model) |
| 109 | return; |
| 110 | |
| 111 | cachedConv_ = nil; |
| 112 | convUid_ = convUid; |
| 113 | convModel_ = model; |
| 114 | |
| 115 | [messagesViewVC setConversationUid:convUid_ model:convModel_]; |
| 116 | |
| 117 | if (convUid_.empty() || convModel_ == nil) |
| 118 | return; |
| 119 | |
| 120 | // Signals tracking changes in conversation list, we need them as cached conversation can be invalid |
| 121 | // after a reordering. |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 122 | QObject::disconnect(modelSortedConnection_); |
| 123 | QObject::disconnect(filterChangedConnection_); |
| 124 | QObject::disconnect(newConversationConnection_); |
| 125 | QObject::disconnect(conversationRemovedConnection_); |
| 126 | modelSortedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted, |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 127 | [self](){ |
| 128 | cachedConv_ = nil; |
| 129 | }); |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 130 | filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged, |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 131 | [self](){ |
| 132 | cachedConv_ = nil; |
| 133 | }); |
Kateryna Kostiuk | 557f5ea | 2018-03-29 13:41:53 -0400 | [diff] [blame] | 134 | newConversationConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newConversation, |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 135 | [self](){ |
| 136 | cachedConv_ = nil; |
| 137 | }); |
Kateryna Kostiuk | 557f5ea | 2018-03-29 13:41:53 -0400 | [diff] [blame] | 138 | conversationRemovedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationRemoved, |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 139 | [self](){ |
| 140 | cachedConv_ = nil; |
| 141 | }); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 142 | |
| 143 | auto* conv = [self getCurrentConversation]; |
| 144 | |
| 145 | if (conv == nil) |
| 146 | return; |
| 147 | |
| 148 | // Setup UI elements according to new conversation |
| 149 | NSString* bestName = bestNameForConversation(*conv, *convModel_); |
Olivier Soldano | 13c69e3 | 2018-01-05 12:01:36 -0500 | [diff] [blame] | 150 | NSString* bestId = bestIDForConversation(*conv, *convModel_); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 151 | [conversationTitle setStringValue: bestName]; |
Olivier Soldano | 13c69e3 | 2018-01-05 12:01:36 -0500 | [diff] [blame] | 152 | [conversationID setStringValue: bestId]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 153 | [sendFileButton setEnabled:(convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type != lrc::api::profile::Type::SIP)]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 154 | |
Olivier Soldano | f563e15 | 2018-01-09 16:08:32 -0500 | [diff] [blame] | 155 | BOOL hideBestId = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 156 | |
Olivier Soldano | f563e15 | 2018-01-09 16:08:32 -0500 | [diff] [blame] | 157 | [conversationID setHidden:hideBestId]; |
| 158 | [titleCenteredConstraint setActive:hideBestId]; |
| 159 | [titleTopConstraint setActive:!hideBestId]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 160 | } |
| 161 | |
Alexandre Lision | 4baba4c | 2016-02-11 13:00:57 -0500 | [diff] [blame] | 162 | - (void)loadView { |
| 163 | [super loadView]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 164 | // Do view setup here. |
| 165 | [self.view setWantsLayer:YES]; |
| 166 | [self.view setLayer:[CALayer layer]]; |
| 167 | [self.view.layer setBackgroundColor:[NSColor ringGreyHighlight].CGColor]; |
| 168 | [self.view.layer setCornerRadius:5.0f]; |
| 169 | |
Kateryna Kostiuk | 64d025a | 2017-07-14 11:30:44 -0400 | [diff] [blame] | 170 | [messageField setFocusRingType:NSFocusRingTypeNone]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 171 | } |
| 172 | |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 173 | -(Account* ) chosenAccount |
| 174 | { |
| 175 | QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex(); |
| 176 | if(!index.isValid()) { |
| 177 | return nullptr; |
| 178 | } |
| 179 | Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>(); |
| 180 | return account; |
| 181 | } |
| 182 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 183 | - (void) initFrame |
| 184 | { |
| 185 | [self.view setFrame:self.view.superview.bounds]; |
| 186 | [self.view setHidden:YES]; |
| 187 | self.view.layer.position = self.view.frame.origin; |
| 188 | } |
| 189 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 190 | - (IBAction)sendMessage:(id)sender |
| 191 | { |
| 192 | /* make sure there is text to send */ |
| 193 | NSString* text = self.message; |
| 194 | if (text && text.length > 0) { |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 195 | auto* conv = [self getCurrentConversation]; |
| 196 | bool isPending = convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING; |
Anthony Léonard | 4b0935f | 2017-12-26 12:05:22 -0500 | [diff] [blame] | 197 | convModel_->sendMessage(convUid_, std::string([text UTF8String])); |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 198 | self.message = @""; |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 199 | if (isPending) |
| 200 | [delegate currentConversationTrusted]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 201 | [messagesViewVC newMessageSent]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 205 | - (IBAction)sendFile:(id)sender |
| 206 | { |
| 207 | NSOpenPanel* filePicker = [NSOpenPanel openPanel]; |
| 208 | [filePicker setCanChooseFiles:YES]; |
| 209 | [filePicker setCanChooseDirectories:NO]; |
| 210 | [filePicker setAllowsMultipleSelection:NO]; |
| 211 | |
| 212 | if ([filePicker runModal] == NSFileHandlingPanelOKButton) { |
| 213 | if ([[filePicker URLs] count] == 1) { |
| 214 | NSURL* url = [[filePicker URLs] objectAtIndex:0]; |
| 215 | const char* fullPath = [url fileSystemRepresentation]; |
| 216 | NSString* fileName = [url lastPathComponent]; |
| 217 | if (convModel_) { |
Anthony Léonard | 0c65bd1 | 2018-02-08 16:47:19 -0500 | [diff] [blame] | 218 | auto* conv = [self getCurrentConversation]; |
| 219 | bool isPending = convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING; |
| 220 | |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 221 | convModel_->sendFile(convUid_, std::string(fullPath), std::string([fileName UTF8String])); |
Anthony Léonard | 0c65bd1 | 2018-02-08 16:47:19 -0500 | [diff] [blame] | 222 | |
| 223 | if (isPending) |
| 224 | [delegate currentConversationTrusted]; |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 230 | - (IBAction)placeCall:(id)sender |
| 231 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 232 | auto* conv = [self getCurrentConversation]; |
| 233 | convModel_->placeCall(conv->uid); |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 234 | } |
| 235 | |
Alexandre Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 236 | - (IBAction)backPressed:(id)sender { |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 237 | [delegate rightPanelClosed]; |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 238 | [self hideWithAnimation:false]; |
Kateryna Kostiuk | 0ba1baf | 2017-05-12 16:51:27 -0400 | [diff] [blame] | 239 | } |
| 240 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 241 | # pragma mark private IN/OUT animations |
| 242 | |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 243 | -(void) showWithAnimation:(BOOL)animate |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 244 | { |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 245 | if (!animate) { |
| 246 | [self.view setHidden:NO]; |
| 247 | return; |
| 248 | } |
| 249 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 250 | CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0); |
| 251 | [self.view setHidden:NO]; |
| 252 | |
| 253 | [CATransaction begin]; |
| 254 | CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; |
| 255 | [animation setFromValue:[NSValue valueWithPoint:frame.origin]]; |
| 256 | [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]]; |
| 257 | [animation setDuration:0.2f]; |
| 258 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; |
| 259 | [self.view.layer addAnimation:animation forKey:animation.keyPath]; |
| 260 | |
| 261 | [CATransaction commit]; |
| 262 | } |
| 263 | |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 264 | -(void) hideWithAnimation:(BOOL)animate |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 265 | { |
| 266 | if(self.view.frame.origin.x < 0) { |
| 267 | return; |
| 268 | } |
| 269 | |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 270 | if (!animate) { |
| 271 | [self.view setHidden:YES]; |
| 272 | return; |
| 273 | } |
| 274 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 275 | CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0); |
| 276 | [CATransaction begin]; |
| 277 | CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; |
| 278 | [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]]; |
| 279 | [animation setToValue:[NSValue valueWithPoint:frame.origin]]; |
| 280 | [animation setDuration:0.2f]; |
| 281 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
| 282 | |
| 283 | [CATransaction setCompletionBlock:^{ |
| 284 | [self.view setHidden:YES]; |
| 285 | }]; |
| 286 | [self.view.layer addAnimation:animation forKey:animation.keyPath]; |
| 287 | |
| 288 | [self.view.layer setPosition:frame.origin]; |
| 289 | [CATransaction commit]; |
| 290 | } |
| 291 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 292 | #pragma mark - NSTextFieldDelegate |
| 293 | |
| 294 | - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector |
| 295 | { |
| 296 | if (commandSelector == @selector(insertNewline:) && self.message.length > 0) { |
| 297 | [self sendMessage:nil]; |
| 298 | return YES; |
| 299 | } |
| 300 | return NO; |
| 301 | } |
| 302 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 303 | |
| 304 | @end |