blob: 9d2fe89c1d7a8554fcfeeda983047ae8eb4eda35 [file] [log] [blame]
Alexandre Lision0f66bd32016-01-18 11:30:45 -05001/*
Anthony Léonarde7d62ed2018-01-25 10:51:47 -05002 * Copyright (C) 2016-2018 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"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050039
Anthony Léonard2382b562017-12-13 15:51:28 -050040@interface ConversationVC () {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050041
Alexandre Lision0f66bd32016-01-18 11:30:45 -050042 __unsafe_unretained IBOutlet NSTextField* conversationTitle;
Olivier Soldano13c69e32018-01-05 12:01:36 -050043 __unsafe_unretained IBOutlet NSTextField *conversationID;
Kateryna Kostiukdb95b472018-04-17 16:35:57 -040044 __unsafe_unretained IBOutlet HoverButton *addContactButton;
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -040045 __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth;
Kateryna Kostiuk4f37d952018-12-04 13:19:17 -050046
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -040047 __unsafe_unretained IBOutlet NSButton* sentContactRequestButton;
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040048 IBOutlet MessagesVC* messagesViewVC;
Anthony Léonardade9f9b2017-07-26 15:51:12 -040049
Olivier Soldanof563e152018-01-09 16:08:32 -050050 IBOutlet NSLayoutConstraint *titleCenteredConstraint;
Anthony Léonardade9f9b2017-07-26 15:51:12 -040051 IBOutlet NSLayoutConstraint* titleTopConstraint;
Anthony Léonard2382b562017-12-13 15:51:28 -050052
53 std::string convUid_;
54 const lrc::api::conversation::Info* cachedConv_;
55 lrc::api::ConversationModel* convModel_;
56
Anthony Léonard8585cc02017-12-28 14:03:45 -050057 RingWindowController* delegate;
58
Anthony Léonard01634102017-12-27 16:37:16 -050059 // All those connections are needed to invalidate cached conversation as pointer
Anthony Léonard2382b562017-12-13 15:51:28 -050060 // may not be referencing the same conversation anymore
Anthony Léonard01634102017-12-27 16:37:16 -050061 QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050062}
63
64@end
65
Kateryna Kostiuk4f37d952018-12-04 13:19:17 -050066NSInteger const MEESAGE_MARGIN = 21;
67NSInteger const SEND_PANEL_DEFAULT_HEIGHT = 60;
68NSInteger const SEND_PANEL_MAX_HEIGHT = 120;
69
Alexandre Lision0f66bd32016-01-18 11:30:45 -050070@implementation ConversationVC
71
Anthony Léonard8585cc02017-12-28 14:03:45 -050072- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil delegate:(RingWindowController*) mainWindow
73{
74 if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
75 {
76 delegate = mainWindow;
77 }
78 return self;
79}
80
Andreas Traczyk252a94a2018-04-20 16:36:20 -040081-(void) clearData {
82 cachedConv_ = nil;
83 convUid_ = "";
84 convModel_ = nil;
85
86 [messagesViewVC clearData];
87 QObject::disconnect(modelSortedConnection_);
88 QObject::disconnect(filterChangedConnection_);
89 QObject::disconnect(newConversationConnection_);
90 QObject::disconnect(conversationRemovedConnection_);
91}
92
Anthony Léonard2382b562017-12-13 15:51:28 -050093-(const lrc::api::conversation::Info*) getCurrentConversation
94{
95 if (convModel_ == nil || convUid_.empty())
96 return nil;
97
98 if (cachedConv_ != nil)
99 return cachedConv_;
100
Kateryna Kostiukae660fd2018-04-24 14:10:41 -0400101 auto convQueue = convModel_->allFilteredConversations();
Anthony Léonard2382b562017-12-13 15:51:28 -0500102
Anthony Léonard6f819752018-01-05 09:53:40 -0500103 auto it = getConversationFromUid(convUid_, *convModel_);
Anthony Léonard2382b562017-12-13 15:51:28 -0500104
105 if (it != convQueue.end())
106 cachedConv_ = &(*it);
107
108 return cachedConv_;
109}
110
111-(void) setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model {
112 if (convUid_ == convUid && convModel_ == model)
113 return;
Kateryna Kostiuk0f0ba992018-06-07 14:22:58 -0400114 [self clearData];
Anthony Léonard2382b562017-12-13 15:51:28 -0500115 cachedConv_ = nil;
116 convUid_ = convUid;
117 convModel_ = model;
118
119 [messagesViewVC setConversationUid:convUid_ model:convModel_];
120
121 if (convUid_.empty() || convModel_ == nil)
122 return;
123
124 // Signals tracking changes in conversation list, we need them as cached conversation can be invalid
125 // after a reordering.
Anthony Léonard01634102017-12-27 16:37:16 -0500126 QObject::disconnect(modelSortedConnection_);
127 QObject::disconnect(filterChangedConnection_);
128 QObject::disconnect(newConversationConnection_);
129 QObject::disconnect(conversationRemovedConnection_);
130 modelSortedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted,
Anthony Léonard2382b562017-12-13 15:51:28 -0500131 [self](){
132 cachedConv_ = nil;
133 });
Anthony Léonard01634102017-12-27 16:37:16 -0500134 filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged,
Anthony Léonard2382b562017-12-13 15:51:28 -0500135 [self](){
136 cachedConv_ = nil;
137 });
Kateryna Kostiuk557f5ea2018-03-29 13:41:53 -0400138 newConversationConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newConversation,
Anthony Léonard01634102017-12-27 16:37:16 -0500139 [self](){
140 cachedConv_ = nil;
141 });
Kateryna Kostiuk557f5ea2018-03-29 13:41:53 -0400142 conversationRemovedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationRemoved,
Anthony Léonard01634102017-12-27 16:37:16 -0500143 [self](){
144 cachedConv_ = nil;
145 });
Anthony Léonard2382b562017-12-13 15:51:28 -0500146
147 auto* conv = [self getCurrentConversation];
148
149 if (conv == nil)
150 return;
151
152 // Setup UI elements according to new conversation
153 NSString* bestName = bestNameForConversation(*conv, *convModel_);
Olivier Soldano13c69e32018-01-05 12:01:36 -0500154 NSString* bestId = bestIDForConversation(*conv, *convModel_);
Anthony Léonard2382b562017-12-13 15:51:28 -0500155 [conversationTitle setStringValue: bestName];
Olivier Soldano13c69e32018-01-05 12:01:36 -0500156 [conversationID setStringValue: bestId];
Anthony Léonard2382b562017-12-13 15:51:28 -0500157
Olivier Soldanof563e152018-01-09 16:08:32 -0500158 BOOL hideBestId = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)];
Anthony Léonard2382b562017-12-13 15:51:28 -0500159
Olivier Soldanof563e152018-01-09 16:08:32 -0500160 [conversationID setHidden:hideBestId];
161 [titleCenteredConstraint setActive:hideBestId];
162 [titleTopConstraint setActive:!hideBestId];
Kateryna Kostiukdb95b472018-04-17 16:35:57 -0400163 auto accountType = convModel_->owner.profileInfo.type;
164 [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 -0500165}
166
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500167- (void) initFrame
168{
169 [self.view setFrame:self.view.superview.bounds];
170 [self.view setHidden:YES];
171 self.view.layer.position = self.view.frame.origin;
172}
173
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500174- (IBAction)placeCall:(id)sender
175{
Anthony Léonard2382b562017-12-13 15:51:28 -0500176 auto* conv = [self getCurrentConversation];
177 convModel_->placeCall(conv->uid);
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500178}
179
Kateryna Kostiukdb95b472018-04-17 16:35:57 -0400180- (IBAction)placeAudioCall:(id)sender
181{
182 auto* conv = [self getCurrentConversation];
183 convModel_->placeAudioOnlyCall(conv->uid);
184}
185
186- (IBAction)addContact:(id)sender
187{
188 auto* conv = [self getCurrentConversation];
189 convModel_->makePermanent(conv->uid);
190}
191
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500192- (IBAction)backPressed:(id)sender {
Anthony Léonard8585cc02017-12-28 14:03:45 -0500193 [delegate rightPanelClosed];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400194 [self hideWithAnimation:false];
Kateryna Kostiuk0ba1baf2017-05-12 16:51:27 -0400195}
196
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500197# pragma mark private IN/OUT animations
198
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400199-(void) showWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500200{
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400201 if (!animate) {
202 [self.view setHidden:NO];
203 return;
204 }
205
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500206 CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
207 [self.view setHidden:NO];
208
209 [CATransaction begin];
210 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
211 [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
212 [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
213 [animation setDuration:0.2f];
214 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
215 [self.view.layer addAnimation:animation forKey:animation.keyPath];
216
217 [CATransaction commit];
218}
219
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400220-(void) hideWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500221{
222 if(self.view.frame.origin.x < 0) {
223 return;
224 }
225
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400226 [self clearData];
227
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400228 if (!animate) {
229 [self.view setHidden:YES];
230 return;
231 }
232
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500233 CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
234 [CATransaction begin];
235 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
236 [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
237 [animation setToValue:[NSValue valueWithPoint:frame.origin]];
238 [animation setDuration:0.2f];
239 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
240
241 [CATransaction setCompletionBlock:^{
242 [self.view setHidden:YES];
243 }];
244 [self.view.layer addAnimation:animation forKey:animation.keyPath];
245
246 [self.view.layer setPosition:frame.origin];
247 [CATransaction commit];
248}
249
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500250
251@end