blob: 5de8e1524ced0eadcd27cb4de246c5374a062a66 [file] [log] [blame]
Alexandre Lision0f66bd32016-01-18 11:30:45 -05001/*
Sébastien Blin029ffa82019-01-02 17:43:48 -05002 * Copyright (C) 2016-2019 Savoir-faire Linux Inc.
Alexandre Lision0f66bd32016-01-18 11:30:45 -05003 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
Anthony Léonard2382b562017-12-13 15:51:28 -05004 * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
Alexandre Lision0f66bd32016-01-18 11:30:45 -05005 *
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 Lision0f66bd32016-01-18 11:30:45 -050023#import <qstring.h>
24#import <QPixmap>
25#import <QtMacExtras/qmacfunctions.h>
Kateryna Kostiuk1ba5a012018-12-12 17:07:38 -050026#import <QuartzCore/QuartzCore.h>
Kateryna Kostiuk6ea9ac12019-07-18 15:02:07 -040027#import <QuickLook/QuickLook.h>
28#import <Quartz/Quartz.h>
Alexandre Lision0f66bd32016-01-18 11:30:45 -050029
30#import "views/IconButton.h"
Kateryna Kostiukdb95b472018-04-17 16:35:57 -040031#import "views/HoverButton.h"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050032#import "views/IMTableCellView.h"
33#import "views/NSColor+RingTheme.h"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050034#import "delegates/ImageManipulationDelegate.h"
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040035#import "MessagesVC.h"
Anthony Léonard2382b562017-12-13 15:51:28 -050036#import "utils.h"
Anthony Léonard8585cc02017-12-28 14:03:45 -050037#import "RingWindowController.h"
Kateryna Kostiuka0f16862018-05-04 09:11:41 -040038#import "NSString+Extensions.h"
Kateryna Kostiukf6317422018-09-27 17:08:20 -040039#import "LeaveMessageVC.h"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050040
Kateryna Kostiukf6317422018-09-27 17:08:20 -040041@interface ConversationVC () <QLPreviewPanelDataSource, QLPreviewPanelDelegate>{
Alexandre Lision0f66bd32016-01-18 11:30:45 -050042
Alexandre Lision0f66bd32016-01-18 11:30:45 -050043 __unsafe_unretained IBOutlet NSTextField* conversationTitle;
Olivier Soldano13c69e32018-01-05 12:01:36 -050044 __unsafe_unretained IBOutlet NSTextField *conversationID;
Kateryna Kostiukdb95b472018-04-17 16:35:57 -040045 __unsafe_unretained IBOutlet HoverButton *addContactButton;
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -040046 __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth;
Kateryna Kostiuk4f37d952018-12-04 13:19:17 -050047
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -040048 __unsafe_unretained IBOutlet NSButton* sentContactRequestButton;
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040049 IBOutlet MessagesVC* messagesViewVC;
Kateryna Kostiukf6317422018-09-27 17:08:20 -040050 LeaveMessageVC* leaveMessageVC;
Anthony Léonardade9f9b2017-07-26 15:51:12 -040051
Olivier Soldanof563e152018-01-09 16:08:32 -050052 IBOutlet NSLayoutConstraint *titleCenteredConstraint;
Anthony Léonardade9f9b2017-07-26 15:51:12 -040053 IBOutlet NSLayoutConstraint* titleTopConstraint;
Anthony Léonard2382b562017-12-13 15:51:28 -050054
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040055 QString convUid_;
Anthony Léonard2382b562017-12-13 15:51:28 -050056 const lrc::api::conversation::Info* cachedConv_;
57 lrc::api::ConversationModel* convModel_;
58
Anthony Léonard8585cc02017-12-28 14:03:45 -050059 RingWindowController* delegate;
Kateryna Kostiukf6317422018-09-27 17:08:20 -040060 NSMutableArray* leaveMessageConversations;
Anthony Léonard8585cc02017-12-28 14:03:45 -050061
Anthony Léonard01634102017-12-27 16:37:16 -050062 // All those connections are needed to invalidate cached conversation as pointer
Anthony Léonard2382b562017-12-13 15:51:28 -050063 // may not be referencing the same conversation anymore
Anthony Léonard01634102017-12-27 16:37:16 -050064 QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050065}
66
67@end
68
Kateryna Kostiuk4f37d952018-12-04 13:19:17 -050069NSInteger const MEESAGE_MARGIN = 21;
70NSInteger const SEND_PANEL_DEFAULT_HEIGHT = 60;
71NSInteger const SEND_PANEL_MAX_HEIGHT = 120;
72
Alexandre Lision0f66bd32016-01-18 11:30:45 -050073@implementation ConversationVC
74
Kateryna Kostiukf6317422018-09-27 17:08:20 -040075- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil delegate:(RingWindowController*) mainWindow aVModel:(lrc::api::AVModel*) avModel
Anthony Léonard8585cc02017-12-28 14:03:45 -050076{
77 if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
78 {
79 delegate = mainWindow;
Kateryna Kostiukf6317422018-09-27 17:08:20 -040080 leaveMessageVC = [[LeaveMessageVC alloc] initWithNibName:@"LeaveMessageVC" bundle:nil];
81 [[leaveMessageVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
82 [self.view addSubview:[leaveMessageVC view] positioned:NSWindowAbove relativeTo:nil];
83 [leaveMessageVC initFrame];
84 [leaveMessageVC setAVModel: avModel];
85 leaveMessageConversations = [[NSMutableArray alloc] init];
86 leaveMessageVC.delegate = self;
Kateryna Kostiukfbe1b2f2019-10-07 17:32:26 -040087 [messagesViewVC setAVModel: avModel];
Anthony Léonard8585cc02017-12-28 14:03:45 -050088 }
89 return self;
90}
91
Kateryna Kostiukf6317422018-09-27 17:08:20 -040092-(NSViewController*) getMessagesView {
93 return messagesViewVC;
94}
95
Andreas Traczyk252a94a2018-04-20 16:36:20 -040096-(void) clearData {
97 cachedConv_ = nil;
98 convUid_ = "";
99 convModel_ = nil;
100
101 [messagesViewVC clearData];
102 QObject::disconnect(modelSortedConnection_);
103 QObject::disconnect(filterChangedConnection_);
104 QObject::disconnect(newConversationConnection_);
105 QObject::disconnect(conversationRemovedConnection_);
106}
107
Anthony Léonard2382b562017-12-13 15:51:28 -0500108-(const lrc::api::conversation::Info*) getCurrentConversation
109{
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400110 if (convModel_ == nil || convUid_.isEmpty())
Anthony Léonard2382b562017-12-13 15:51:28 -0500111 return nil;
112
113 if (cachedConv_ != nil)
114 return cachedConv_;
115
Kateryna Kostiukae660fd2018-04-24 14:10:41 -0400116 auto convQueue = convModel_->allFilteredConversations();
Anthony Léonard2382b562017-12-13 15:51:28 -0500117
Anthony Léonard6f819752018-01-05 09:53:40 -0500118 auto it = getConversationFromUid(convUid_, *convModel_);
Anthony Léonard2382b562017-12-13 15:51:28 -0500119
120 if (it != convQueue.end())
121 cachedConv_ = &(*it);
122
123 return cachedConv_;
124}
125
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400126-(void) setConversationUid:(const QString&)convUid model:(lrc::api::ConversationModel *)model {
Anthony Léonard2382b562017-12-13 15:51:28 -0500127 if (convUid_ == convUid && convModel_ == model)
128 return;
Kateryna Kostiuk0f0ba992018-06-07 14:22:58 -0400129 [self clearData];
Anthony Léonard2382b562017-12-13 15:51:28 -0500130 cachedConv_ = nil;
131 convUid_ = convUid;
132 convModel_ = model;
133
134 [messagesViewVC setConversationUid:convUid_ model:convModel_];
135
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400136 if (convUid_.isEmpty() || convModel_ == nil)
Anthony Léonard2382b562017-12-13 15:51:28 -0500137 return;
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400138 if([leaveMessageConversations containsObject:convUid_.toNSString()]) {
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400139 [leaveMessageVC setConversationUID: convUid_ conversationModel: convModel_];
140 } else {
141 [leaveMessageVC hide];
142 }
Anthony Léonard2382b562017-12-13 15:51:28 -0500143
144 // Signals tracking changes in conversation list, we need them as cached conversation can be invalid
145 // after a reordering.
Anthony Léonard01634102017-12-27 16:37:16 -0500146 QObject::disconnect(modelSortedConnection_);
147 QObject::disconnect(filterChangedConnection_);
148 QObject::disconnect(newConversationConnection_);
149 QObject::disconnect(conversationRemovedConnection_);
150 modelSortedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted,
Anthony Léonard2382b562017-12-13 15:51:28 -0500151 [self](){
152 cachedConv_ = nil;
153 });
Anthony Léonard01634102017-12-27 16:37:16 -0500154 filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged,
Anthony Léonard2382b562017-12-13 15:51:28 -0500155 [self](){
156 cachedConv_ = nil;
157 });
Kateryna Kostiuk557f5ea2018-03-29 13:41:53 -0400158 newConversationConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newConversation,
Anthony Léonard01634102017-12-27 16:37:16 -0500159 [self](){
160 cachedConv_ = nil;
161 });
Kateryna Kostiuk557f5ea2018-03-29 13:41:53 -0400162 conversationRemovedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationRemoved,
Anthony Léonard01634102017-12-27 16:37:16 -0500163 [self](){
164 cachedConv_ = nil;
165 });
Anthony Léonard2382b562017-12-13 15:51:28 -0500166
167 auto* conv = [self getCurrentConversation];
168
169 if (conv == nil)
170 return;
171
172 // Setup UI elements according to new conversation
173 NSString* bestName = bestNameForConversation(*conv, *convModel_);
Olivier Soldano13c69e32018-01-05 12:01:36 -0500174 NSString* bestId = bestIDForConversation(*conv, *convModel_);
Anthony Léonard2382b562017-12-13 15:51:28 -0500175 [conversationTitle setStringValue: bestName];
Olivier Soldano13c69e32018-01-05 12:01:36 -0500176 [conversationID setStringValue: bestId];
Anthony Léonard2382b562017-12-13 15:51:28 -0500177
Olivier Soldanof563e152018-01-09 16:08:32 -0500178 BOOL hideBestId = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)];
Anthony Léonard2382b562017-12-13 15:51:28 -0500179
Olivier Soldanof563e152018-01-09 16:08:32 -0500180 [conversationID setHidden:hideBestId];
181 [titleCenteredConstraint setActive:hideBestId];
182 [titleTopConstraint setActive:!hideBestId];
Kateryna Kostiukdb95b472018-04-17 16:35:57 -0400183 auto accountType = convModel_->owner.profileInfo.type;
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400184 try {
185 [addContactButton setHidden:((convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type != lrc::api::profile::Type::TEMPORARY) || accountType == lrc::api::profile::Type::SIP)];
186 } catch (std::out_of_range& e) {
187 NSLog(@"contact out of range");
188 }
Anthony Léonard2382b562017-12-13 15:51:28 -0500189}
190
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500191- (void) initFrame
192{
193 [self.view setFrame:self.view.superview.bounds];
194 [self.view setHidden:YES];
195 self.view.layer.position = self.view.frame.origin;
196}
197
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500198- (IBAction)placeCall:(id)sender
199{
Anthony Léonard2382b562017-12-13 15:51:28 -0500200 auto* conv = [self getCurrentConversation];
201 convModel_->placeCall(conv->uid);
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500202}
203
Kateryna Kostiukdb95b472018-04-17 16:35:57 -0400204- (IBAction)placeAudioCall:(id)sender
205{
206 auto* conv = [self getCurrentConversation];
207 convModel_->placeAudioOnlyCall(conv->uid);
208}
209
210- (IBAction)addContact:(id)sender
211{
212 auto* conv = [self getCurrentConversation];
213 convModel_->makePermanent(conv->uid);
214}
215
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500216- (IBAction)backPressed:(id)sender {
Anthony Léonard8585cc02017-12-28 14:03:45 -0500217 [delegate rightPanelClosed];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400218 [self hideWithAnimation:false];
Kateryna Kostiukfbe1b2f2019-10-07 17:32:26 -0400219 [messagesViewVC clearData];
Kateryna Kostiuk0ba1baf2017-05-12 16:51:27 -0400220}
221
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500222# pragma mark private IN/OUT animations
223
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400224-(void) showWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500225{
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400226 if (!animate) {
227 [self.view setHidden:NO];
228 return;
229 }
230
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500231 CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
232 [self.view setHidden:NO];
233
234 [CATransaction begin];
235 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
236 [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
237 [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
238 [animation setDuration:0.2f];
239 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
240 [self.view.layer addAnimation:animation forKey:animation.keyPath];
241
242 [CATransaction commit];
243}
244
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400245-(void) hideWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500246{
247 if(self.view.frame.origin.x < 0) {
248 return;
249 }
250
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400251 [self clearData];
252
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400253 if (!animate) {
254 [self.view setHidden:YES];
255 return;
256 }
257
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500258 CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
259 [CATransaction begin];
260 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
261 [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
262 [animation setToValue:[NSValue valueWithPoint:frame.origin]];
263 [animation setDuration:0.2f];
264 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
265
266 [CATransaction setCompletionBlock:^{
267 [self.view setHidden:YES];
268 }];
269 [self.view.layer addAnimation:animation forKey:animation.keyPath];
270
271 [self.view.layer setPosition:frame.origin];
272 [CATransaction commit];
273}
274
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400275- (void) presentLeaveMessageView {
276 [leaveMessageVC setConversationUID: convUid_ conversationModel: convModel_];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400277 [leaveMessageConversations addObject:convUid_.toNSString()];
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400278}
279
280-(void) messageCompleted {
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400281 [leaveMessageConversations removeObject:convUid_.toNSString()];
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400282}
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500283
284@end