blob: 4b3e97c7c6505da2a98813b6e36d38f784fe0a34 [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>
Alexandre Lision0f66bd32016-01-18 11:30:45 -050027
Anthony Léonard2382b562017-12-13 15:51:28 -050028// LRC
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"
40#import <QuickLook/QuickLook.h>
41#import <Quartz/Quartz.h>
42#import "LeaveMessageVC.h"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050043
Kateryna Kostiukf6317422018-09-27 17:08:20 -040044@interface ConversationVC () <QLPreviewPanelDataSource, QLPreviewPanelDelegate>{
Alexandre Lision0f66bd32016-01-18 11:30:45 -050045
Alexandre Lision0f66bd32016-01-18 11:30:45 -050046 __unsafe_unretained IBOutlet NSTextField* conversationTitle;
Olivier Soldano13c69e32018-01-05 12:01:36 -050047 __unsafe_unretained IBOutlet NSTextField *conversationID;
Kateryna Kostiukdb95b472018-04-17 16:35:57 -040048 __unsafe_unretained IBOutlet HoverButton *addContactButton;
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -040049 __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth;
Kateryna Kostiuk4f37d952018-12-04 13:19:17 -050050
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -040051 __unsafe_unretained IBOutlet NSButton* sentContactRequestButton;
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040052 IBOutlet MessagesVC* messagesViewVC;
Kateryna Kostiukf6317422018-09-27 17:08:20 -040053 LeaveMessageVC* leaveMessageVC;
Anthony Léonardade9f9b2017-07-26 15:51:12 -040054
Olivier Soldanof563e152018-01-09 16:08:32 -050055 IBOutlet NSLayoutConstraint *titleCenteredConstraint;
Anthony Léonardade9f9b2017-07-26 15:51:12 -040056 IBOutlet NSLayoutConstraint* titleTopConstraint;
Anthony Léonard2382b562017-12-13 15:51:28 -050057
58 std::string convUid_;
59 const lrc::api::conversation::Info* cachedConv_;
60 lrc::api::ConversationModel* convModel_;
61
Anthony Léonard8585cc02017-12-28 14:03:45 -050062 RingWindowController* delegate;
Kateryna Kostiukf6317422018-09-27 17:08:20 -040063 NSMutableArray* leaveMessageConversations;
Anthony Léonard8585cc02017-12-28 14:03:45 -050064
Anthony Léonard01634102017-12-27 16:37:16 -050065 // All those connections are needed to invalidate cached conversation as pointer
Anthony Léonard2382b562017-12-13 15:51:28 -050066 // may not be referencing the same conversation anymore
Anthony Léonard01634102017-12-27 16:37:16 -050067 QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050068}
69
70@end
71
Kateryna Kostiuk4f37d952018-12-04 13:19:17 -050072NSInteger const MEESAGE_MARGIN = 21;
73NSInteger const SEND_PANEL_DEFAULT_HEIGHT = 60;
74NSInteger const SEND_PANEL_MAX_HEIGHT = 120;
75
Alexandre Lision0f66bd32016-01-18 11:30:45 -050076@implementation ConversationVC
77
Kateryna Kostiukf6317422018-09-27 17:08:20 -040078- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil delegate:(RingWindowController*) mainWindow aVModel:(lrc::api::AVModel*) avModel
Anthony Léonard8585cc02017-12-28 14:03:45 -050079{
80 if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
81 {
82 delegate = mainWindow;
Kateryna Kostiukf6317422018-09-27 17:08:20 -040083 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éonard8585cc02017-12-28 14:03:45 -050090 }
91 return self;
92}
93
Kateryna Kostiukf6317422018-09-27 17:08:20 -040094-(NSViewController*) getMessagesView {
95 return messagesViewVC;
96}
97
Andreas Traczyk252a94a2018-04-20 16:36:20 -040098-(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éonard2382b562017-12-13 15:51:28 -0500110-(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 Kostiukae660fd2018-04-24 14:10:41 -0400118 auto convQueue = convModel_->allFilteredConversations();
Anthony Léonard2382b562017-12-13 15:51:28 -0500119
Anthony Léonard6f819752018-01-05 09:53:40 -0500120 auto it = getConversationFromUid(convUid_, *convModel_);
Anthony Léonard2382b562017-12-13 15:51:28 -0500121
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 Kostiuk0f0ba992018-06-07 14:22:58 -0400131 [self clearData];
Anthony Léonard2382b562017-12-13 15:51:28 -0500132 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 Kostiukf6317422018-09-27 17:08:20 -0400140 if([leaveMessageConversations containsObject:@(convUid_.c_str())]) {
141 [leaveMessageVC setConversationUID: convUid_ conversationModel: convModel_];
142 } else {
143 [leaveMessageVC hide];
144 }
Anthony Léonard2382b562017-12-13 15:51:28 -0500145
146 // Signals tracking changes in conversation list, we need them as cached conversation can be invalid
147 // after a reordering.
Anthony Léonard01634102017-12-27 16:37:16 -0500148 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éonard2382b562017-12-13 15:51:28 -0500153 [self](){
154 cachedConv_ = nil;
155 });
Anthony Léonard01634102017-12-27 16:37:16 -0500156 filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged,
Anthony Léonard2382b562017-12-13 15:51:28 -0500157 [self](){
158 cachedConv_ = nil;
159 });
Kateryna Kostiuk557f5ea2018-03-29 13:41:53 -0400160 newConversationConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newConversation,
Anthony Léonard01634102017-12-27 16:37:16 -0500161 [self](){
162 cachedConv_ = nil;
163 });
Kateryna Kostiuk557f5ea2018-03-29 13:41:53 -0400164 conversationRemovedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationRemoved,
Anthony Léonard01634102017-12-27 16:37:16 -0500165 [self](){
166 cachedConv_ = nil;
167 });
Anthony Léonard2382b562017-12-13 15:51:28 -0500168
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 Soldano13c69e32018-01-05 12:01:36 -0500176 NSString* bestId = bestIDForConversation(*conv, *convModel_);
Anthony Léonard2382b562017-12-13 15:51:28 -0500177 [conversationTitle setStringValue: bestName];
Olivier Soldano13c69e32018-01-05 12:01:36 -0500178 [conversationID setStringValue: bestId];
Anthony Léonard2382b562017-12-13 15:51:28 -0500179
Olivier Soldanof563e152018-01-09 16:08:32 -0500180 BOOL hideBestId = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)];
Anthony Léonard2382b562017-12-13 15:51:28 -0500181
Olivier Soldanof563e152018-01-09 16:08:32 -0500182 [conversationID setHidden:hideBestId];
183 [titleCenteredConstraint setActive:hideBestId];
184 [titleTopConstraint setActive:!hideBestId];
Kateryna Kostiukdb95b472018-04-17 16:35:57 -0400185 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éonard2382b562017-12-13 15:51:28 -0500187}
188
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500189- (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 Lision0f66bd32016-01-18 11:30:45 -0500196- (IBAction)placeCall:(id)sender
197{
Anthony Léonard2382b562017-12-13 15:51:28 -0500198 auto* conv = [self getCurrentConversation];
199 convModel_->placeCall(conv->uid);
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500200}
201
Kateryna Kostiukdb95b472018-04-17 16:35:57 -0400202- (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 Lision01cf5e32016-01-21 15:54:30 -0500214- (IBAction)backPressed:(id)sender {
Anthony Léonard8585cc02017-12-28 14:03:45 -0500215 [delegate rightPanelClosed];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400216 [self hideWithAnimation:false];
Kateryna Kostiuk0ba1baf2017-05-12 16:51:27 -0400217}
218
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500219# pragma mark private IN/OUT animations
220
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400221-(void) showWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500222{
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400223 if (!animate) {
224 [self.view setHidden:NO];
225 return;
226 }
227
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500228 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 Traczykecdc4fa2018-03-22 16:11:47 -0400242-(void) hideWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500243{
244 if(self.view.frame.origin.x < 0) {
245 return;
246 }
247
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400248 [self clearData];
249
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400250 if (!animate) {
251 [self.view setHidden:YES];
252 return;
253 }
254
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500255 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 Kostiukf6317422018-09-27 17:08:20 -0400272- (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 Lision0f66bd32016-01-18 11:30:45 -0500280
281@end