blob: 8fbea93fe9dadfa984c9a1ba0b02c9d16b5122af [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
23#import <QItemSelectionModel>
24#import <qstring.h>
25#import <QPixmap>
26#import <QtMacExtras/qmacfunctions.h>
27
Anthony Léonard2382b562017-12-13 15:51:28 -050028// LRC
Alexandre Lision0f66bd32016-01-18 11:30:45 -050029#import <globalinstances.h>
30
31#import "views/IconButton.h"
Kateryna Kostiukdb95b472018-04-17 16:35:57 -040032#import "views/HoverButton.h"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050033#import "views/IMTableCellView.h"
34#import "views/NSColor+RingTheme.h"
35#import "QNSTreeController.h"
Alexandre Lision83180df2016-01-18 11:32:20 -050036#import "INDSequentialTextSelectionManager.h"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050037#import "delegates/ImageManipulationDelegate.h"
Kateryna Kostiuk0ba1baf2017-05-12 16:51:27 -040038#import "PhoneDirectoryModel.h"
39#import "account.h"
40#import "AvailableAccountModel.h"
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040041#import "MessagesVC.h"
Anthony Léonard2382b562017-12-13 15:51:28 -050042#import "utils.h"
Anthony Léonard8585cc02017-12-28 14:03:45 -050043#import "RingWindowController.h"
Kateryna Kostiuka0f16862018-05-04 09:11:41 -040044#import "NSString+Extensions.h"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050045
46#import <QuartzCore/QuartzCore.h>
47
Anthony Léonard2382b562017-12-13 15:51:28 -050048@interface ConversationVC () {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050049
Alexandre Lision0f66bd32016-01-18 11:30:45 -050050 __unsafe_unretained IBOutlet NSTextField* messageField;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050051 NSMutableString* textSelection;
52
Alexandre Lision0f66bd32016-01-18 11:30:45 -050053 __unsafe_unretained IBOutlet NSView* sendPanel;
54 __unsafe_unretained IBOutlet NSTextField* conversationTitle;
Olivier Soldano13c69e32018-01-05 12:01:36 -050055 __unsafe_unretained IBOutlet NSTextField *conversationID;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050056 __unsafe_unretained IBOutlet IconButton* sendButton;
Anthony Léonarde7d62ed2018-01-25 10:51:47 -050057 __unsafe_unretained IBOutlet IconButton *sendFileButton;
Kateryna Kostiukdb95b472018-04-17 16:35:57 -040058 __unsafe_unretained IBOutlet HoverButton *addContactButton;
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -040059 __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth;
60 __unsafe_unretained IBOutlet NSButton* sentContactRequestButton;
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040061 IBOutlet MessagesVC* messagesViewVC;
Anthony Léonardade9f9b2017-07-26 15:51:12 -040062
Olivier Soldanof563e152018-01-09 16:08:32 -050063 IBOutlet NSLayoutConstraint *titleCenteredConstraint;
Anthony Léonardade9f9b2017-07-26 15:51:12 -040064 IBOutlet NSLayoutConstraint* titleTopConstraint;
Anthony Léonard2382b562017-12-13 15:51:28 -050065
66 std::string convUid_;
67 const lrc::api::conversation::Info* cachedConv_;
68 lrc::api::ConversationModel* convModel_;
69
Anthony Léonard8585cc02017-12-28 14:03:45 -050070 RingWindowController* delegate;
71
Anthony Léonard01634102017-12-27 16:37:16 -050072 // All those connections are needed to invalidate cached conversation as pointer
Anthony Léonard2382b562017-12-13 15:51:28 -050073 // may not be referencing the same conversation anymore
Anthony Léonard01634102017-12-27 16:37:16 -050074 QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_;
75
Alexandre Lision0f66bd32016-01-18 11:30:45 -050076}
77
Alexandre Lision83180df2016-01-18 11:32:20 -050078
Alexandre Lision0f66bd32016-01-18 11:30:45 -050079@end
80
81@implementation ConversationVC
82
Anthony Léonard8585cc02017-12-28 14:03:45 -050083- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil delegate:(RingWindowController*) mainWindow
84{
85 if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
86 {
87 delegate = mainWindow;
88 }
89 return self;
90}
91
Kateryna Kostiuka0f16862018-05-04 09:11:41 -040092- (void)setMessage:(NSString *)newValue {
93 _message = [newValue removeEmptyLinesAtBorders];
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;
129
130 cachedConv_ = nil;
131 convUid_ = convUid;
132 convModel_ = model;
133
134 [messagesViewVC setConversationUid:convUid_ model:convModel_];
135
136 if (convUid_.empty() || convModel_ == nil)
137 return;
138
139 // Signals tracking changes in conversation list, we need them as cached conversation can be invalid
140 // after a reordering.
Anthony Léonard01634102017-12-27 16:37:16 -0500141 QObject::disconnect(modelSortedConnection_);
142 QObject::disconnect(filterChangedConnection_);
143 QObject::disconnect(newConversationConnection_);
144 QObject::disconnect(conversationRemovedConnection_);
145 modelSortedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted,
Anthony Léonard2382b562017-12-13 15:51:28 -0500146 [self](){
147 cachedConv_ = nil;
148 });
Anthony Léonard01634102017-12-27 16:37:16 -0500149 filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged,
Anthony Léonard2382b562017-12-13 15:51:28 -0500150 [self](){
151 cachedConv_ = nil;
152 });
Kateryna Kostiuk557f5ea2018-03-29 13:41:53 -0400153 newConversationConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newConversation,
Anthony Léonard01634102017-12-27 16:37:16 -0500154 [self](){
155 cachedConv_ = nil;
156 });
Kateryna Kostiuk557f5ea2018-03-29 13:41:53 -0400157 conversationRemovedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationRemoved,
Anthony Léonard01634102017-12-27 16:37:16 -0500158 [self](){
159 cachedConv_ = nil;
160 });
Anthony Léonard2382b562017-12-13 15:51:28 -0500161
162 auto* conv = [self getCurrentConversation];
163
164 if (conv == nil)
165 return;
166
167 // Setup UI elements according to new conversation
168 NSString* bestName = bestNameForConversation(*conv, *convModel_);
Olivier Soldano13c69e32018-01-05 12:01:36 -0500169 NSString* bestId = bestIDForConversation(*conv, *convModel_);
Anthony Léonard2382b562017-12-13 15:51:28 -0500170 [conversationTitle setStringValue: bestName];
Olivier Soldano13c69e32018-01-05 12:01:36 -0500171 [conversationID setStringValue: bestId];
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500172 [sendFileButton setEnabled:(convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type != lrc::api::profile::Type::SIP)];
Anthony Léonard2382b562017-12-13 15:51:28 -0500173
Olivier Soldanof563e152018-01-09 16:08:32 -0500174 BOOL hideBestId = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)];
Anthony Léonard2382b562017-12-13 15:51:28 -0500175
Olivier Soldanof563e152018-01-09 16:08:32 -0500176 [conversationID setHidden:hideBestId];
177 [titleCenteredConstraint setActive:hideBestId];
178 [titleTopConstraint setActive:!hideBestId];
Kateryna Kostiukdb95b472018-04-17 16:35:57 -0400179 auto accountType = convModel_->owner.profileInfo.type;
180 [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 -0500181}
182
Alexandre Lision4baba4c2016-02-11 13:00:57 -0500183- (void)loadView {
184 [super loadView];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500185 // Do view setup here.
186 [self.view setWantsLayer:YES];
187 [self.view setLayer:[CALayer layer]];
188 [self.view.layer setBackgroundColor:[NSColor ringGreyHighlight].CGColor];
189 [self.view.layer setCornerRadius:5.0f];
190
Kateryna Kostiuk64d025a2017-07-14 11:30:44 -0400191 [messageField setFocusRingType:NSFocusRingTypeNone];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500192}
193
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -0400194-(Account* ) chosenAccount
195{
196 QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex();
197 if(!index.isValid()) {
198 return nullptr;
199 }
200 Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>();
201 return account;
202}
203
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500204- (void) initFrame
205{
206 [self.view setFrame:self.view.superview.bounds];
207 [self.view setHidden:YES];
208 self.view.layer.position = self.view.frame.origin;
209}
210
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500211- (IBAction)sendMessage:(id)sender
212{
213 /* make sure there is text to send */
214 NSString* text = self.message;
215 if (text && text.length > 0) {
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500216 auto* conv = [self getCurrentConversation];
217 bool isPending = convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING;
Anthony Léonard4b0935f2017-12-26 12:05:22 -0500218 convModel_->sendMessage(convUid_, std::string([text UTF8String]));
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500219 self.message = @"";
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500220 if (isPending)
221 [delegate currentConversationTrusted];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500222 }
223}
224
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500225- (IBAction)sendFile:(id)sender
226{
227 NSOpenPanel* filePicker = [NSOpenPanel openPanel];
228 [filePicker setCanChooseFiles:YES];
229 [filePicker setCanChooseDirectories:NO];
230 [filePicker setAllowsMultipleSelection:NO];
231
232 if ([filePicker runModal] == NSFileHandlingPanelOKButton) {
233 if ([[filePicker URLs] count] == 1) {
234 NSURL* url = [[filePicker URLs] objectAtIndex:0];
235 const char* fullPath = [url fileSystemRepresentation];
236 NSString* fileName = [url lastPathComponent];
237 if (convModel_) {
Anthony Léonard0c65bd12018-02-08 16:47:19 -0500238 auto* conv = [self getCurrentConversation];
239 bool isPending = convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING;
240
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500241 convModel_->sendFile(convUid_, std::string(fullPath), std::string([fileName UTF8String]));
Anthony Léonard0c65bd12018-02-08 16:47:19 -0500242
243 if (isPending)
244 [delegate currentConversationTrusted];
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500245 }
246 }
247 }
248}
249
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500250- (IBAction)placeCall:(id)sender
251{
Anthony Léonard2382b562017-12-13 15:51:28 -0500252 auto* conv = [self getCurrentConversation];
253 convModel_->placeCall(conv->uid);
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500254}
255
Kateryna Kostiukdb95b472018-04-17 16:35:57 -0400256- (IBAction)placeAudioCall:(id)sender
257{
258 auto* conv = [self getCurrentConversation];
259 convModel_->placeAudioOnlyCall(conv->uid);
260}
261
262- (IBAction)addContact:(id)sender
263{
264 auto* conv = [self getCurrentConversation];
265 convModel_->makePermanent(conv->uid);
266}
267
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500268- (IBAction)backPressed:(id)sender {
Anthony Léonard8585cc02017-12-28 14:03:45 -0500269 [delegate rightPanelClosed];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400270 [self hideWithAnimation:false];
Kateryna Kostiuk0ba1baf2017-05-12 16:51:27 -0400271}
272
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500273# pragma mark private IN/OUT animations
274
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400275-(void) showWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500276{
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400277 if (!animate) {
278 [self.view setHidden:NO];
279 return;
280 }
281
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500282 CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
283 [self.view setHidden:NO];
284
285 [CATransaction begin];
286 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
287 [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
288 [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
289 [animation setDuration:0.2f];
290 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
291 [self.view.layer addAnimation:animation forKey:animation.keyPath];
292
293 [CATransaction commit];
294}
295
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400296-(void) hideWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500297{
298 if(self.view.frame.origin.x < 0) {
299 return;
300 }
301
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400302 [self clearData];
303
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400304 if (!animate) {
305 [self.view setHidden:YES];
306 return;
307 }
308
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500309 CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
310 [CATransaction begin];
311 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
312 [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
313 [animation setToValue:[NSValue valueWithPoint:frame.origin]];
314 [animation setDuration:0.2f];
315 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
316
317 [CATransaction setCompletionBlock:^{
318 [self.view setHidden:YES];
319 }];
320 [self.view.layer addAnimation:animation forKey:animation.keyPath];
321
322 [self.view.layer setPosition:frame.origin];
323 [CATransaction commit];
324}
325
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500326#pragma mark - NSTextFieldDelegate
327
328- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
329{
330 if (commandSelector == @selector(insertNewline:) && self.message.length > 0) {
331 [self sendMessage:nil];
332 return YES;
333 }
334 return NO;
335}
336
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500337
338@end