Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 1 | /* |
Sébastien Blin | 029ffa8 | 2019-01-02 17:43:48 -0500 | [diff] [blame] | 2 | * Copyright (C) 2016-2019 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 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 23 | #import <qstring.h> |
| 24 | #import <QPixmap> |
| 25 | #import <QtMacExtras/qmacfunctions.h> |
Kateryna Kostiuk | 1ba5a01 | 2018-12-12 17:07:38 -0500 | [diff] [blame] | 26 | #import <QuartzCore/QuartzCore.h> |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 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 | |
| 30 | #import "views/IconButton.h" |
Kateryna Kostiuk | db95b47 | 2018-04-17 16:35:57 -0400 | [diff] [blame] | 31 | #import "views/HoverButton.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 32 | #import "views/IMTableCellView.h" |
| 33 | #import "views/NSColor+RingTheme.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 34 | #import "delegates/ImageManipulationDelegate.h" |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 35 | #import "MessagesVC.h" |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 36 | #import "utils.h" |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 37 | #import "RingWindowController.h" |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 38 | #import "NSString+Extensions.h" |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 39 | #import "LeaveMessageVC.h" |
| 40 | #import <QuickLook/QuickLook.h> |
| 41 | #import <Quartz/Quartz.h> |
| 42 | #import "LeaveMessageVC.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 43 | |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 44 | @interface ConversationVC () <QLPreviewPanelDataSource, QLPreviewPanelDelegate>{ |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 45 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 46 | __unsafe_unretained IBOutlet NSTextField* conversationTitle; |
Olivier Soldano | 13c69e3 | 2018-01-05 12:01:36 -0500 | [diff] [blame] | 47 | __unsafe_unretained IBOutlet NSTextField *conversationID; |
Kateryna Kostiuk | db95b47 | 2018-04-17 16:35:57 -0400 | [diff] [blame] | 48 | __unsafe_unretained IBOutlet HoverButton *addContactButton; |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 49 | __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 50 | |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 51 | __unsafe_unretained IBOutlet NSButton* sentContactRequestButton; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 52 | IBOutlet MessagesVC* messagesViewVC; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 53 | LeaveMessageVC* leaveMessageVC; |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame] | 54 | |
Olivier Soldano | f563e15 | 2018-01-09 16:08:32 -0500 | [diff] [blame] | 55 | IBOutlet NSLayoutConstraint *titleCenteredConstraint; |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame] | 56 | IBOutlet NSLayoutConstraint* titleTopConstraint; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 57 | |
| 58 | std::string convUid_; |
| 59 | const lrc::api::conversation::Info* cachedConv_; |
| 60 | lrc::api::ConversationModel* convModel_; |
| 61 | |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 62 | RingWindowController* delegate; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 63 | NSMutableArray* leaveMessageConversations; |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 64 | |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 65 | // All those connections are needed to invalidate cached conversation as pointer |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 66 | // may not be referencing the same conversation anymore |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 67 | QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @end |
| 71 | |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 72 | NSInteger const MEESAGE_MARGIN = 21; |
| 73 | NSInteger const SEND_PANEL_DEFAULT_HEIGHT = 60; |
| 74 | NSInteger const SEND_PANEL_MAX_HEIGHT = 120; |
| 75 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 76 | @implementation ConversationVC |
| 77 | |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 78 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil delegate:(RingWindowController*) mainWindow aVModel:(lrc::api::AVModel*) avModel |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 79 | { |
| 80 | if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) |
| 81 | { |
| 82 | delegate = mainWindow; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 83 | leaveMessageVC = [[LeaveMessageVC alloc] initWithNibName:@"LeaveMessageVC" bundle:nil]; |
| 84 | [[leaveMessageVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
| 85 | [self.view addSubview:[leaveMessageVC view] positioned:NSWindowAbove relativeTo:nil]; |
| 86 | [leaveMessageVC initFrame]; |
| 87 | [leaveMessageVC setAVModel: avModel]; |
| 88 | leaveMessageConversations = [[NSMutableArray alloc] init]; |
| 89 | leaveMessageVC.delegate = self; |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 90 | } |
| 91 | return self; |
| 92 | } |
| 93 | |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 94 | -(NSViewController*) getMessagesView { |
| 95 | return messagesViewVC; |
| 96 | } |
| 97 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 98 | -(void) clearData { |
| 99 | cachedConv_ = nil; |
| 100 | convUid_ = ""; |
| 101 | convModel_ = nil; |
| 102 | |
| 103 | [messagesViewVC clearData]; |
| 104 | QObject::disconnect(modelSortedConnection_); |
| 105 | QObject::disconnect(filterChangedConnection_); |
| 106 | QObject::disconnect(newConversationConnection_); |
| 107 | QObject::disconnect(conversationRemovedConnection_); |
| 108 | } |
| 109 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 110 | -(const lrc::api::conversation::Info*) getCurrentConversation |
| 111 | { |
| 112 | if (convModel_ == nil || convUid_.empty()) |
| 113 | return nil; |
| 114 | |
| 115 | if (cachedConv_ != nil) |
| 116 | return cachedConv_; |
| 117 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 118 | auto convQueue = convModel_->allFilteredConversations(); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 119 | |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 120 | auto it = getConversationFromUid(convUid_, *convModel_); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 121 | |
| 122 | if (it != convQueue.end()) |
| 123 | cachedConv_ = &(*it); |
| 124 | |
| 125 | return cachedConv_; |
| 126 | } |
| 127 | |
| 128 | -(void) setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model { |
| 129 | if (convUid_ == convUid && convModel_ == model) |
| 130 | return; |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 131 | [self clearData]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 132 | cachedConv_ = nil; |
| 133 | convUid_ = convUid; |
| 134 | convModel_ = model; |
| 135 | |
| 136 | [messagesViewVC setConversationUid:convUid_ model:convModel_]; |
| 137 | |
| 138 | if (convUid_.empty() || convModel_ == nil) |
| 139 | return; |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 140 | if([leaveMessageConversations containsObject:@(convUid_.c_str())]) { |
| 141 | [leaveMessageVC setConversationUID: convUid_ conversationModel: convModel_]; |
| 142 | } else { |
| 143 | [leaveMessageVC hide]; |
| 144 | } |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 145 | |
| 146 | // Signals tracking changes in conversation list, we need them as cached conversation can be invalid |
| 147 | // after a reordering. |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 148 | QObject::disconnect(modelSortedConnection_); |
| 149 | QObject::disconnect(filterChangedConnection_); |
| 150 | QObject::disconnect(newConversationConnection_); |
| 151 | QObject::disconnect(conversationRemovedConnection_); |
| 152 | modelSortedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted, |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 153 | [self](){ |
| 154 | cachedConv_ = nil; |
| 155 | }); |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 156 | filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged, |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 157 | [self](){ |
| 158 | cachedConv_ = nil; |
| 159 | }); |
Kateryna Kostiuk | 557f5ea | 2018-03-29 13:41:53 -0400 | [diff] [blame] | 160 | newConversationConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newConversation, |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 161 | [self](){ |
| 162 | cachedConv_ = nil; |
| 163 | }); |
Kateryna Kostiuk | 557f5ea | 2018-03-29 13:41:53 -0400 | [diff] [blame] | 164 | conversationRemovedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationRemoved, |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 165 | [self](){ |
| 166 | cachedConv_ = nil; |
| 167 | }); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 168 | |
| 169 | auto* conv = [self getCurrentConversation]; |
| 170 | |
| 171 | if (conv == nil) |
| 172 | return; |
| 173 | |
| 174 | // Setup UI elements according to new conversation |
| 175 | NSString* bestName = bestNameForConversation(*conv, *convModel_); |
Olivier Soldano | 13c69e3 | 2018-01-05 12:01:36 -0500 | [diff] [blame] | 176 | NSString* bestId = bestIDForConversation(*conv, *convModel_); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 177 | [conversationTitle setStringValue: bestName]; |
Olivier Soldano | 13c69e3 | 2018-01-05 12:01:36 -0500 | [diff] [blame] | 178 | [conversationID setStringValue: bestId]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 179 | |
Olivier Soldano | f563e15 | 2018-01-09 16:08:32 -0500 | [diff] [blame] | 180 | BOOL hideBestId = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 181 | |
Olivier Soldano | f563e15 | 2018-01-09 16:08:32 -0500 | [diff] [blame] | 182 | [conversationID setHidden:hideBestId]; |
| 183 | [titleCenteredConstraint setActive:hideBestId]; |
| 184 | [titleTopConstraint setActive:!hideBestId]; |
Kateryna Kostiuk | db95b47 | 2018-04-17 16:35:57 -0400 | [diff] [blame] | 185 | auto accountType = convModel_->owner.profileInfo.type; |
| 186 | [addContactButton setHidden:((convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type != lrc::api::profile::Type::TEMPORARY) || accountType == lrc::api::profile::Type::SIP)]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 187 | } |
| 188 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 189 | - (void) initFrame |
| 190 | { |
| 191 | [self.view setFrame:self.view.superview.bounds]; |
| 192 | [self.view setHidden:YES]; |
| 193 | self.view.layer.position = self.view.frame.origin; |
| 194 | } |
| 195 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 196 | - (IBAction)placeCall:(id)sender |
| 197 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 198 | auto* conv = [self getCurrentConversation]; |
| 199 | convModel_->placeCall(conv->uid); |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 200 | } |
| 201 | |
Kateryna Kostiuk | db95b47 | 2018-04-17 16:35:57 -0400 | [diff] [blame] | 202 | - (IBAction)placeAudioCall:(id)sender |
| 203 | { |
| 204 | auto* conv = [self getCurrentConversation]; |
| 205 | convModel_->placeAudioOnlyCall(conv->uid); |
| 206 | } |
| 207 | |
| 208 | - (IBAction)addContact:(id)sender |
| 209 | { |
| 210 | auto* conv = [self getCurrentConversation]; |
| 211 | convModel_->makePermanent(conv->uid); |
| 212 | } |
| 213 | |
Alexandre Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 214 | - (IBAction)backPressed:(id)sender { |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 215 | [delegate rightPanelClosed]; |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 216 | [self hideWithAnimation:false]; |
Kateryna Kostiuk | 0ba1baf | 2017-05-12 16:51:27 -0400 | [diff] [blame] | 217 | } |
| 218 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 219 | # pragma mark private IN/OUT animations |
| 220 | |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 221 | -(void) showWithAnimation:(BOOL)animate |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 222 | { |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 223 | if (!animate) { |
| 224 | [self.view setHidden:NO]; |
| 225 | return; |
| 226 | } |
| 227 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 228 | CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0); |
| 229 | [self.view setHidden:NO]; |
| 230 | |
| 231 | [CATransaction begin]; |
| 232 | CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; |
| 233 | [animation setFromValue:[NSValue valueWithPoint:frame.origin]]; |
| 234 | [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]]; |
| 235 | [animation setDuration:0.2f]; |
| 236 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; |
| 237 | [self.view.layer addAnimation:animation forKey:animation.keyPath]; |
| 238 | |
| 239 | [CATransaction commit]; |
| 240 | } |
| 241 | |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 242 | -(void) hideWithAnimation:(BOOL)animate |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 243 | { |
| 244 | if(self.view.frame.origin.x < 0) { |
| 245 | return; |
| 246 | } |
| 247 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 248 | [self clearData]; |
| 249 | |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 250 | if (!animate) { |
| 251 | [self.view setHidden:YES]; |
| 252 | return; |
| 253 | } |
| 254 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 255 | CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0); |
| 256 | [CATransaction begin]; |
| 257 | CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; |
| 258 | [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]]; |
| 259 | [animation setToValue:[NSValue valueWithPoint:frame.origin]]; |
| 260 | [animation setDuration:0.2f]; |
| 261 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
| 262 | |
| 263 | [CATransaction setCompletionBlock:^{ |
| 264 | [self.view setHidden:YES]; |
| 265 | }]; |
| 266 | [self.view.layer addAnimation:animation forKey:animation.keyPath]; |
| 267 | |
| 268 | [self.view.layer setPosition:frame.origin]; |
| 269 | [CATransaction commit]; |
| 270 | } |
| 271 | |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 272 | - (void) presentLeaveMessageView { |
| 273 | [leaveMessageVC setConversationUID: convUid_ conversationModel: convModel_]; |
| 274 | [leaveMessageConversations addObject:@(convUid_.c_str())]; |
| 275 | } |
| 276 | |
| 277 | -(void) messageCompleted { |
| 278 | [leaveMessageConversations removeObject:@(convUid_.c_str())]; |
| 279 | } |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 280 | |
| 281 | @end |