blob: e5bfcc6a40b37f7d60e7717c19b4b20805822968 [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"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050044
45#import <QuartzCore/QuartzCore.h>
46
Anthony Léonard2382b562017-12-13 15:51:28 -050047@interface ConversationVC () {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050048
Alexandre Lision0f66bd32016-01-18 11:30:45 -050049 __unsafe_unretained IBOutlet NSTextField* messageField;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050050 NSMutableString* textSelection;
51
Alexandre Lision0f66bd32016-01-18 11:30:45 -050052 __unsafe_unretained IBOutlet NSView* sendPanel;
53 __unsafe_unretained IBOutlet NSTextField* conversationTitle;
Olivier Soldano13c69e32018-01-05 12:01:36 -050054 __unsafe_unretained IBOutlet NSTextField *conversationID;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050055 __unsafe_unretained IBOutlet IconButton* sendButton;
Anthony Léonarde7d62ed2018-01-25 10:51:47 -050056 __unsafe_unretained IBOutlet IconButton *sendFileButton;
Kateryna Kostiukdb95b472018-04-17 16:35:57 -040057 __unsafe_unretained IBOutlet HoverButton *addContactButton;
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -040058 __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth;
59 __unsafe_unretained IBOutlet NSButton* sentContactRequestButton;
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040060 IBOutlet MessagesVC* messagesViewVC;
Anthony Léonardade9f9b2017-07-26 15:51:12 -040061
Olivier Soldanof563e152018-01-09 16:08:32 -050062 IBOutlet NSLayoutConstraint *titleCenteredConstraint;
Anthony Léonardade9f9b2017-07-26 15:51:12 -040063 IBOutlet NSLayoutConstraint* titleTopConstraint;
Anthony Léonard2382b562017-12-13 15:51:28 -050064
65 std::string convUid_;
66 const lrc::api::conversation::Info* cachedConv_;
67 lrc::api::ConversationModel* convModel_;
68
Anthony Léonard8585cc02017-12-28 14:03:45 -050069 RingWindowController* delegate;
70
Anthony Léonard01634102017-12-27 16:37:16 -050071 // All those connections are needed to invalidate cached conversation as pointer
Anthony Léonard2382b562017-12-13 15:51:28 -050072 // may not be referencing the same conversation anymore
Anthony Léonard01634102017-12-27 16:37:16 -050073 QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_;
74
Alexandre Lision0f66bd32016-01-18 11:30:45 -050075}
76
Alexandre Lision83180df2016-01-18 11:32:20 -050077
Alexandre Lision0f66bd32016-01-18 11:30:45 -050078@end
79
80@implementation ConversationVC
81
Anthony Léonard8585cc02017-12-28 14:03:45 -050082- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil delegate:(RingWindowController*) mainWindow
83{
84 if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
85 {
86 delegate = mainWindow;
87 }
88 return self;
89}
90
Anthony Léonard2382b562017-12-13 15:51:28 -050091-(const lrc::api::conversation::Info*) getCurrentConversation
92{
93 if (convModel_ == nil || convUid_.empty())
94 return nil;
95
96 if (cachedConv_ != nil)
97 return cachedConv_;
98
99 auto& convQueue = convModel_->allFilteredConversations();
100
Anthony Léonard6f819752018-01-05 09:53:40 -0500101 auto it = getConversationFromUid(convUid_, *convModel_);
Anthony Léonard2382b562017-12-13 15:51:28 -0500102
103 if (it != convQueue.end())
104 cachedConv_ = &(*it);
105
106 return cachedConv_;
107}
108
109-(void) setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model {
110 if (convUid_ == convUid && convModel_ == model)
111 return;
112
113 cachedConv_ = nil;
114 convUid_ = convUid;
115 convModel_ = model;
116
117 [messagesViewVC setConversationUid:convUid_ model:convModel_];
118
119 if (convUid_.empty() || convModel_ == nil)
120 return;
121
122 // Signals tracking changes in conversation list, we need them as cached conversation can be invalid
123 // after a reordering.
Anthony Léonard01634102017-12-27 16:37:16 -0500124 QObject::disconnect(modelSortedConnection_);
125 QObject::disconnect(filterChangedConnection_);
126 QObject::disconnect(newConversationConnection_);
127 QObject::disconnect(conversationRemovedConnection_);
128 modelSortedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted,
Anthony Léonard2382b562017-12-13 15:51:28 -0500129 [self](){
130 cachedConv_ = nil;
131 });
Anthony Léonard01634102017-12-27 16:37:16 -0500132 filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged,
Anthony Léonard2382b562017-12-13 15:51:28 -0500133 [self](){
134 cachedConv_ = nil;
135 });
Kateryna Kostiuk557f5ea2018-03-29 13:41:53 -0400136 newConversationConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newConversation,
Anthony Léonard01634102017-12-27 16:37:16 -0500137 [self](){
138 cachedConv_ = nil;
139 });
Kateryna Kostiuk557f5ea2018-03-29 13:41:53 -0400140 conversationRemovedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationRemoved,
Anthony Léonard01634102017-12-27 16:37:16 -0500141 [self](){
142 cachedConv_ = nil;
143 });
Anthony Léonard2382b562017-12-13 15:51:28 -0500144
145 auto* conv = [self getCurrentConversation];
146
147 if (conv == nil)
148 return;
149
150 // Setup UI elements according to new conversation
151 NSString* bestName = bestNameForConversation(*conv, *convModel_);
Olivier Soldano13c69e32018-01-05 12:01:36 -0500152 NSString* bestId = bestIDForConversation(*conv, *convModel_);
Anthony Léonard2382b562017-12-13 15:51:28 -0500153 [conversationTitle setStringValue: bestName];
Olivier Soldano13c69e32018-01-05 12:01:36 -0500154 [conversationID setStringValue: bestId];
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500155 [sendFileButton setEnabled:(convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type != lrc::api::profile::Type::SIP)];
Anthony Léonard2382b562017-12-13 15:51:28 -0500156
Olivier Soldanof563e152018-01-09 16:08:32 -0500157 BOOL hideBestId = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)];
Anthony Léonard2382b562017-12-13 15:51:28 -0500158
Olivier Soldanof563e152018-01-09 16:08:32 -0500159 [conversationID setHidden:hideBestId];
160 [titleCenteredConstraint setActive:hideBestId];
161 [titleTopConstraint setActive:!hideBestId];
Kateryna Kostiukdb95b472018-04-17 16:35:57 -0400162 auto accountType = convModel_->owner.profileInfo.type;
163 [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 -0500164}
165
Alexandre Lision4baba4c2016-02-11 13:00:57 -0500166- (void)loadView {
167 [super loadView];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500168 // Do view setup here.
169 [self.view setWantsLayer:YES];
170 [self.view setLayer:[CALayer layer]];
171 [self.view.layer setBackgroundColor:[NSColor ringGreyHighlight].CGColor];
172 [self.view.layer setCornerRadius:5.0f];
173
Kateryna Kostiuk64d025a2017-07-14 11:30:44 -0400174 [messageField setFocusRingType:NSFocusRingTypeNone];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500175}
176
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -0400177-(Account* ) chosenAccount
178{
179 QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex();
180 if(!index.isValid()) {
181 return nullptr;
182 }
183 Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>();
184 return account;
185}
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)sendMessage:(id)sender
195{
196 /* make sure there is text to send */
197 NSString* text = self.message;
198 if (text && text.length > 0) {
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500199 auto* conv = [self getCurrentConversation];
200 bool isPending = convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING;
Anthony Léonard4b0935f2017-12-26 12:05:22 -0500201 convModel_->sendMessage(convUid_, std::string([text UTF8String]));
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500202 self.message = @"";
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500203 if (isPending)
204 [delegate currentConversationTrusted];
Anthony Léonard2382b562017-12-13 15:51:28 -0500205 [messagesViewVC newMessageSent];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500206 }
207}
208
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500209- (IBAction)sendFile:(id)sender
210{
211 NSOpenPanel* filePicker = [NSOpenPanel openPanel];
212 [filePicker setCanChooseFiles:YES];
213 [filePicker setCanChooseDirectories:NO];
214 [filePicker setAllowsMultipleSelection:NO];
215
216 if ([filePicker runModal] == NSFileHandlingPanelOKButton) {
217 if ([[filePicker URLs] count] == 1) {
218 NSURL* url = [[filePicker URLs] objectAtIndex:0];
219 const char* fullPath = [url fileSystemRepresentation];
220 NSString* fileName = [url lastPathComponent];
221 if (convModel_) {
Anthony Léonard0c65bd12018-02-08 16:47:19 -0500222 auto* conv = [self getCurrentConversation];
223 bool isPending = convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING;
224
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500225 convModel_->sendFile(convUid_, std::string(fullPath), std::string([fileName UTF8String]));
Anthony Léonard0c65bd12018-02-08 16:47:19 -0500226
227 if (isPending)
228 [delegate currentConversationTrusted];
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500229 }
230 }
231 }
232}
233
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500234- (IBAction)placeCall:(id)sender
235{
Anthony Léonard2382b562017-12-13 15:51:28 -0500236 auto* conv = [self getCurrentConversation];
237 convModel_->placeCall(conv->uid);
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500238}
239
Kateryna Kostiukdb95b472018-04-17 16:35:57 -0400240- (IBAction)placeAudioCall:(id)sender
241{
242 auto* conv = [self getCurrentConversation];
243 convModel_->placeAudioOnlyCall(conv->uid);
244}
245
246- (IBAction)addContact:(id)sender
247{
248 auto* conv = [self getCurrentConversation];
249 convModel_->makePermanent(conv->uid);
250}
251
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500252- (IBAction)backPressed:(id)sender {
Anthony Léonard8585cc02017-12-28 14:03:45 -0500253 [delegate rightPanelClosed];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400254 [self hideWithAnimation:false];
Kateryna Kostiuk0ba1baf2017-05-12 16:51:27 -0400255}
256
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500257# pragma mark private IN/OUT animations
258
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400259-(void) showWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500260{
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400261 if (!animate) {
262 [self.view setHidden:NO];
263 return;
264 }
265
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500266 CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
267 [self.view setHidden:NO];
268
269 [CATransaction begin];
270 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
271 [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
272 [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
273 [animation setDuration:0.2f];
274 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
275 [self.view.layer addAnimation:animation forKey:animation.keyPath];
276
277 [CATransaction commit];
278}
279
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400280-(void) hideWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500281{
282 if(self.view.frame.origin.x < 0) {
283 return;
284 }
285
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400286 if (!animate) {
287 [self.view setHidden:YES];
288 return;
289 }
290
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500291 CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
292 [CATransaction begin];
293 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
294 [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
295 [animation setToValue:[NSValue valueWithPoint:frame.origin]];
296 [animation setDuration:0.2f];
297 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
298
299 [CATransaction setCompletionBlock:^{
300 [self.view setHidden:YES];
301 }];
302 [self.view.layer addAnimation:animation forKey:animation.keyPath];
303
304 [self.view.layer setPosition:frame.origin];
305 [CATransaction commit];
306}
307
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500308#pragma mark - NSTextFieldDelegate
309
310- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
311{
312 if (commandSelector == @selector(insertNewline:) && self.message.length > 0) {
313 [self sendMessage:nil];
314 return YES;
315 }
316 return NO;
317}
318
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500319
320@end