blob: 6b01470245c33c5f58241327c7904557b032444b [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 Kostiuka7b909c2020-10-19 11:46:26 -0400116 auto convOpt = getConversationFromUid(convUid_, *convModel_);
117 if (convOpt.has_value()) {
kkostiukf81c6372021-01-11 18:51:28 -0500118 lrc::api::conversation::Info& conversation = *convOpt;
Kateryna Kostiuka7b909c2020-10-19 11:46:26 -0400119 cachedConv_ = &conversation;
Kateryna Kostiuk3541ae22020-08-17 12:26:14 -0400120 }
Anthony Léonard2382b562017-12-13 15:51:28 -0500121 return cachedConv_;
122}
123
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400124-(void) setConversationUid:(const QString&)convUid model:(lrc::api::ConversationModel *)model {
Anthony Léonard2382b562017-12-13 15:51:28 -0500125 if (convUid_ == convUid && convModel_ == model)
126 return;
Kateryna Kostiuk0f0ba992018-06-07 14:22:58 -0400127 [self clearData];
Anthony Léonard2382b562017-12-13 15:51:28 -0500128 cachedConv_ = nil;
129 convUid_ = convUid;
130 convModel_ = model;
131
132 [messagesViewVC setConversationUid:convUid_ model:convModel_];
133
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400134 if (convUid_.isEmpty() || convModel_ == nil)
Anthony Léonard2382b562017-12-13 15:51:28 -0500135 return;
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400136 if([leaveMessageConversations containsObject:convUid_.toNSString()]) {
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400137 [leaveMessageVC setConversationUID: convUid_ conversationModel: convModel_];
138 } else {
139 [leaveMessageVC hide];
140 }
Anthony Léonard2382b562017-12-13 15:51:28 -0500141
142 // Signals tracking changes in conversation list, we need them as cached conversation can be invalid
143 // after a reordering.
Anthony Léonard01634102017-12-27 16:37:16 -0500144 QObject::disconnect(modelSortedConnection_);
145 QObject::disconnect(filterChangedConnection_);
146 QObject::disconnect(newConversationConnection_);
147 QObject::disconnect(conversationRemovedConnection_);
Kateryna Kostiuka7b909c2020-10-19 11:46:26 -0400148 modelSortedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelChanged,
Anthony Léonard2382b562017-12-13 15:51:28 -0500149 [self](){
150 cachedConv_ = nil;
151 });
Anthony Léonard01634102017-12-27 16:37:16 -0500152 filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged,
Anthony Léonard2382b562017-12-13 15:51:28 -0500153 [self](){
154 cachedConv_ = nil;
155 });
Kateryna Kostiuk557f5ea2018-03-29 13:41:53 -0400156 newConversationConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newConversation,
Anthony Léonard01634102017-12-27 16:37:16 -0500157 [self](){
158 cachedConv_ = nil;
159 });
Kateryna Kostiuk557f5ea2018-03-29 13:41:53 -0400160 conversationRemovedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationRemoved,
Anthony Léonard01634102017-12-27 16:37:16 -0500161 [self](){
162 cachedConv_ = nil;
163 });
Anthony Léonard2382b562017-12-13 15:51:28 -0500164
165 auto* conv = [self getCurrentConversation];
166
167 if (conv == nil)
168 return;
169
170 // Setup UI elements according to new conversation
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400171 NSLog(@"account info, %@", conv->accountId.toNSString());
172 NSLog(@"conv info, %@", conv->uid.toNSString());
173 NSLog(@"paricipant info, %@", conv->participants[0].toNSString());
Anthony Léonard2382b562017-12-13 15:51:28 -0500174 NSString* bestName = bestNameForConversation(*conv, *convModel_);
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400175 NSLog(@"account info, %@", conv->accountId.toNSString());
176 NSLog(@"conv info, %@", conv->uid.toNSString());
177 NSLog(@"paricipant info, %@", conv->participants[0].toNSString());
Olivier Soldano13c69e32018-01-05 12:01:36 -0500178 NSString* bestId = bestIDForConversation(*conv, *convModel_);
Anthony Léonard2382b562017-12-13 15:51:28 -0500179 [conversationTitle setStringValue: bestName];
Olivier Soldano13c69e32018-01-05 12:01:36 -0500180 [conversationID setStringValue: bestId];
Anthony Léonard2382b562017-12-13 15:51:28 -0500181
Olivier Soldanof563e152018-01-09 16:08:32 -0500182 BOOL hideBestId = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)];
Anthony Léonard2382b562017-12-13 15:51:28 -0500183
Olivier Soldanof563e152018-01-09 16:08:32 -0500184 [conversationID setHidden:hideBestId];
185 [titleCenteredConstraint setActive:hideBestId];
186 [titleTopConstraint setActive:!hideBestId];
Kateryna Kostiukdb95b472018-04-17 16:35:57 -0400187 auto accountType = convModel_->owner.profileInfo.type;
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400188 try {
189 [addContactButton setHidden:((convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type != lrc::api::profile::Type::TEMPORARY) || accountType == lrc::api::profile::Type::SIP)];
190 } catch (std::out_of_range& e) {
191 NSLog(@"contact out of range");
192 }
Anthony Léonard2382b562017-12-13 15:51:28 -0500193}
194
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500195- (void) initFrame
196{
197 [self.view setFrame:self.view.superview.bounds];
198 [self.view setHidden:YES];
199 self.view.layer.position = self.view.frame.origin;
200}
201
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500202- (IBAction)placeCall:(id)sender
203{
Anthony Léonard2382b562017-12-13 15:51:28 -0500204 auto* conv = [self getCurrentConversation];
205 convModel_->placeCall(conv->uid);
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500206}
207
Kateryna Kostiukdb95b472018-04-17 16:35:57 -0400208- (IBAction)placeAudioCall:(id)sender
209{
210 auto* conv = [self getCurrentConversation];
211 convModel_->placeAudioOnlyCall(conv->uid);
212}
213
214- (IBAction)addContact:(id)sender
215{
216 auto* conv = [self getCurrentConversation];
217 convModel_->makePermanent(conv->uid);
218}
219
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500220- (IBAction)backPressed:(id)sender {
Anthony Léonard8585cc02017-12-28 14:03:45 -0500221 [delegate rightPanelClosed];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400222 [self hideWithAnimation:false];
Kateryna Kostiukfbe1b2f2019-10-07 17:32:26 -0400223 [messagesViewVC clearData];
Kateryna Kostiuk0ba1baf2017-05-12 16:51:27 -0400224}
225
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500226# pragma mark private IN/OUT animations
227
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400228-(void) showWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500229{
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400230 if (!animate) {
231 [self.view setHidden:NO];
232 return;
233 }
234
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500235 CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
236 [self.view setHidden:NO];
237
238 [CATransaction begin];
239 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
240 [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
241 [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
242 [animation setDuration:0.2f];
243 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
244 [self.view.layer addAnimation:animation forKey:animation.keyPath];
245
246 [CATransaction commit];
247}
248
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400249-(void) hideWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500250{
251 if(self.view.frame.origin.x < 0) {
252 return;
253 }
254
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400255 [self clearData];
256
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400257 if (!animate) {
258 [self.view setHidden:YES];
259 return;
260 }
261
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500262 CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
263 [CATransaction begin];
264 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
265 [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
266 [animation setToValue:[NSValue valueWithPoint:frame.origin]];
267 [animation setDuration:0.2f];
268 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
269
270 [CATransaction setCompletionBlock:^{
271 [self.view setHidden:YES];
272 }];
273 [self.view.layer addAnimation:animation forKey:animation.keyPath];
274
275 [self.view.layer setPosition:frame.origin];
276 [CATransaction commit];
277}
278
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400279- (void) presentLeaveMessageView {
280 [leaveMessageVC setConversationUID: convUid_ conversationModel: convModel_];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400281 [leaveMessageConversations addObject:convUid_.toNSString()];
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400282}
283
284-(void) messageCompleted {
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400285 [leaveMessageConversations removeObject:convUid_.toNSString()];
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400286}
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500287
288@end