blob: 4061e663fe416da071c40790f750e2b3385d2bc6 [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
Kateryna Kostiukae660fd2018-04-24 14:10:41 -040099 auto convQueue = convModel_->allFilteredConversations();
Anthony Léonard2382b562017-12-13 15:51:28 -0500100
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];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500205 }
206}
207
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500208- (IBAction)sendFile:(id)sender
209{
210 NSOpenPanel* filePicker = [NSOpenPanel openPanel];
211 [filePicker setCanChooseFiles:YES];
212 [filePicker setCanChooseDirectories:NO];
213 [filePicker setAllowsMultipleSelection:NO];
214
215 if ([filePicker runModal] == NSFileHandlingPanelOKButton) {
216 if ([[filePicker URLs] count] == 1) {
217 NSURL* url = [[filePicker URLs] objectAtIndex:0];
218 const char* fullPath = [url fileSystemRepresentation];
219 NSString* fileName = [url lastPathComponent];
220 if (convModel_) {
Anthony Léonard0c65bd12018-02-08 16:47:19 -0500221 auto* conv = [self getCurrentConversation];
222 bool isPending = convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING;
223
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500224 convModel_->sendFile(convUid_, std::string(fullPath), std::string([fileName UTF8String]));
Anthony Léonard0c65bd12018-02-08 16:47:19 -0500225
226 if (isPending)
227 [delegate currentConversationTrusted];
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500228 }
229 }
230 }
231}
232
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500233- (IBAction)placeCall:(id)sender
234{
Anthony Léonard2382b562017-12-13 15:51:28 -0500235 auto* conv = [self getCurrentConversation];
236 convModel_->placeCall(conv->uid);
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500237}
238
Kateryna Kostiukdb95b472018-04-17 16:35:57 -0400239- (IBAction)placeAudioCall:(id)sender
240{
241 auto* conv = [self getCurrentConversation];
242 convModel_->placeAudioOnlyCall(conv->uid);
243}
244
245- (IBAction)addContact:(id)sender
246{
247 auto* conv = [self getCurrentConversation];
248 convModel_->makePermanent(conv->uid);
249}
250
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500251- (IBAction)backPressed:(id)sender {
Anthony Léonard8585cc02017-12-28 14:03:45 -0500252 [delegate rightPanelClosed];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400253 [self hideWithAnimation:false];
Kateryna Kostiuk0ba1baf2017-05-12 16:51:27 -0400254}
255
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500256# pragma mark private IN/OUT animations
257
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400258-(void) showWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500259{
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400260 if (!animate) {
261 [self.view setHidden:NO];
262 return;
263 }
264
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500265 CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
266 [self.view setHidden:NO];
267
268 [CATransaction begin];
269 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
270 [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
271 [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
272 [animation setDuration:0.2f];
273 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
274 [self.view.layer addAnimation:animation forKey:animation.keyPath];
275
276 [CATransaction commit];
277}
278
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400279-(void) hideWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500280{
281 if(self.view.frame.origin.x < 0) {
282 return;
283 }
284
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400285 if (!animate) {
286 [self.view setHidden:YES];
287 return;
288 }
289
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500290 CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
291 [CATransaction begin];
292 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
293 [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
294 [animation setToValue:[NSValue valueWithPoint:frame.origin]];
295 [animation setDuration:0.2f];
296 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
297
298 [CATransaction setCompletionBlock:^{
299 [self.view setHidden:YES];
300 }];
301 [self.view.layer addAnimation:animation forKey:animation.keyPath];
302
303 [self.view.layer setPosition:frame.origin];
304 [CATransaction commit];
305}
306
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500307#pragma mark - NSTextFieldDelegate
308
309- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
310{
311 if (commandSelector == @selector(insertNewline:) && self.message.length > 0) {
312 [self sendMessage:nil];
313 return YES;
314 }
315 return NO;
316}
317
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500318
319@end