blob: 5e55e2fa4b041634e5c42a4f16811649505953f3 [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
55 std::string convUid_;
56 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{
110 if (convModel_ == nil || convUid_.empty())
111 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
126-(void) setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model {
127 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
136 if (convUid_.empty() || convModel_ == nil)
137 return;
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400138 if([leaveMessageConversations containsObject:@(convUid_.c_str())]) {
139 [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;
184 [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 -0500185}
186
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500187- (void) initFrame
188{
189 [self.view setFrame:self.view.superview.bounds];
190 [self.view setHidden:YES];
191 self.view.layer.position = self.view.frame.origin;
192}
193
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500194- (IBAction)placeCall:(id)sender
195{
Anthony Léonard2382b562017-12-13 15:51:28 -0500196 auto* conv = [self getCurrentConversation];
197 convModel_->placeCall(conv->uid);
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500198}
199
Kateryna Kostiukdb95b472018-04-17 16:35:57 -0400200- (IBAction)placeAudioCall:(id)sender
201{
202 auto* conv = [self getCurrentConversation];
203 convModel_->placeAudioOnlyCall(conv->uid);
204}
205
206- (IBAction)addContact:(id)sender
207{
208 auto* conv = [self getCurrentConversation];
209 convModel_->makePermanent(conv->uid);
210}
211
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500212- (IBAction)backPressed:(id)sender {
Anthony Léonard8585cc02017-12-28 14:03:45 -0500213 [delegate rightPanelClosed];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400214 [self hideWithAnimation:false];
Kateryna Kostiukfbe1b2f2019-10-07 17:32:26 -0400215 [messagesViewVC clearData];
Kateryna Kostiuk0ba1baf2017-05-12 16:51:27 -0400216}
217
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500218# pragma mark private IN/OUT animations
219
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400220-(void) showWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500221{
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400222 if (!animate) {
223 [self.view setHidden:NO];
224 return;
225 }
226
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500227 CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
228 [self.view setHidden:NO];
229
230 [CATransaction begin];
231 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
232 [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
233 [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
234 [animation setDuration:0.2f];
235 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
236 [self.view.layer addAnimation:animation forKey:animation.keyPath];
237
238 [CATransaction commit];
239}
240
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400241-(void) hideWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500242{
243 if(self.view.frame.origin.x < 0) {
244 return;
245 }
246
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400247 [self clearData];
248
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400249 if (!animate) {
250 [self.view setHidden:YES];
251 return;
252 }
253
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500254 CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
255 [CATransaction begin];
256 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
257 [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
258 [animation setToValue:[NSValue valueWithPoint:frame.origin]];
259 [animation setDuration:0.2f];
260 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
261
262 [CATransaction setCompletionBlock:^{
263 [self.view setHidden:YES];
264 }];
265 [self.view.layer addAnimation:animation forKey:animation.keyPath];
266
267 [self.view.layer setPosition:frame.origin];
268 [CATransaction commit];
269}
270
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400271- (void) presentLeaveMessageView {
272 [leaveMessageVC setConversationUID: convUid_ conversationModel: convModel_];
273 [leaveMessageConversations addObject:@(convUid_.c_str())];
274}
275
276-(void) messageCompleted {
277 [leaveMessageConversations removeObject:@(convUid_.c_str())];
278}
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500279
280@end