blob: c42559d576dc491ecf09c1cefeaa1ca2bb666ba3 [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"
32#import "views/IMTableCellView.h"
33#import "views/NSColor+RingTheme.h"
34#import "QNSTreeController.h"
Alexandre Lision83180df2016-01-18 11:32:20 -050035#import "INDSequentialTextSelectionManager.h"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050036#import "delegates/ImageManipulationDelegate.h"
Kateryna Kostiuk0ba1baf2017-05-12 16:51:27 -040037#import "PhoneDirectoryModel.h"
38#import "account.h"
39#import "AvailableAccountModel.h"
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040040#import "MessagesVC.h"
Anthony Léonard2382b562017-12-13 15:51:28 -050041#import "utils.h"
Anthony Léonard8585cc02017-12-28 14:03:45 -050042#import "RingWindowController.h"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050043
44#import <QuartzCore/QuartzCore.h>
45
Anthony Léonard2382b562017-12-13 15:51:28 -050046@interface ConversationVC () {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050047
Alexandre Lision0f66bd32016-01-18 11:30:45 -050048 __unsafe_unretained IBOutlet NSTextField* messageField;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050049 NSMutableString* textSelection;
50
Alexandre Lision0f66bd32016-01-18 11:30:45 -050051 __unsafe_unretained IBOutlet NSView* sendPanel;
52 __unsafe_unretained IBOutlet NSTextField* conversationTitle;
Olivier Soldano13c69e32018-01-05 12:01:36 -050053 __unsafe_unretained IBOutlet NSTextField *conversationID;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050054 __unsafe_unretained IBOutlet IconButton* sendButton;
Anthony Léonarde7d62ed2018-01-25 10:51:47 -050055 __unsafe_unretained IBOutlet IconButton *sendFileButton;
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -040056 __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth;
57 __unsafe_unretained IBOutlet NSButton* sentContactRequestButton;
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040058 IBOutlet MessagesVC* messagesViewVC;
Anthony Léonardade9f9b2017-07-26 15:51:12 -040059
Olivier Soldanof563e152018-01-09 16:08:32 -050060 IBOutlet NSLayoutConstraint *titleCenteredConstraint;
Anthony Léonardade9f9b2017-07-26 15:51:12 -040061 IBOutlet NSLayoutConstraint* titleTopConstraint;
Anthony Léonard2382b562017-12-13 15:51:28 -050062
63 std::string convUid_;
64 const lrc::api::conversation::Info* cachedConv_;
65 lrc::api::ConversationModel* convModel_;
66
Anthony Léonard8585cc02017-12-28 14:03:45 -050067 RingWindowController* delegate;
68
Anthony Léonard01634102017-12-27 16:37:16 -050069 // All those connections are needed to invalidate cached conversation as pointer
Anthony Léonard2382b562017-12-13 15:51:28 -050070 // may not be referencing the same conversation anymore
Anthony Léonard01634102017-12-27 16:37:16 -050071 QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_;
72
Alexandre Lision0f66bd32016-01-18 11:30:45 -050073}
74
Alexandre Lision83180df2016-01-18 11:32:20 -050075
Alexandre Lision0f66bd32016-01-18 11:30:45 -050076@end
77
78@implementation ConversationVC
79
Anthony Léonard8585cc02017-12-28 14:03:45 -050080- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil delegate:(RingWindowController*) mainWindow
81{
82 if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
83 {
84 delegate = mainWindow;
85 }
86 return self;
87}
88
Anthony Léonard2382b562017-12-13 15:51:28 -050089-(const lrc::api::conversation::Info*) getCurrentConversation
90{
91 if (convModel_ == nil || convUid_.empty())
92 return nil;
93
94 if (cachedConv_ != nil)
95 return cachedConv_;
96
97 auto& convQueue = convModel_->allFilteredConversations();
98
Anthony Léonard6f819752018-01-05 09:53:40 -050099 auto it = getConversationFromUid(convUid_, *convModel_);
Anthony Léonard2382b562017-12-13 15:51:28 -0500100
101 if (it != convQueue.end())
102 cachedConv_ = &(*it);
103
104 return cachedConv_;
105}
106
107-(void) setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model {
108 if (convUid_ == convUid && convModel_ == model)
109 return;
110
111 cachedConv_ = nil;
112 convUid_ = convUid;
113 convModel_ = model;
114
115 [messagesViewVC setConversationUid:convUid_ model:convModel_];
116
117 if (convUid_.empty() || convModel_ == nil)
118 return;
119
120 // Signals tracking changes in conversation list, we need them as cached conversation can be invalid
121 // after a reordering.
Anthony Léonard01634102017-12-27 16:37:16 -0500122 QObject::disconnect(modelSortedConnection_);
123 QObject::disconnect(filterChangedConnection_);
124 QObject::disconnect(newConversationConnection_);
125 QObject::disconnect(conversationRemovedConnection_);
126 modelSortedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted,
Anthony Léonard2382b562017-12-13 15:51:28 -0500127 [self](){
128 cachedConv_ = nil;
129 });
Anthony Léonard01634102017-12-27 16:37:16 -0500130 filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged,
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::newConversation,
135 [self](){
136 cachedConv_ = nil;
137 });
138 filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationRemoved,
139 [self](){
140 cachedConv_ = nil;
141 });
Anthony Léonard2382b562017-12-13 15:51:28 -0500142
143 auto* conv = [self getCurrentConversation];
144
145 if (conv == nil)
146 return;
147
148 // Setup UI elements according to new conversation
149 NSString* bestName = bestNameForConversation(*conv, *convModel_);
Olivier Soldano13c69e32018-01-05 12:01:36 -0500150 NSString* bestId = bestIDForConversation(*conv, *convModel_);
Anthony Léonard2382b562017-12-13 15:51:28 -0500151 [conversationTitle setStringValue: bestName];
Olivier Soldano13c69e32018-01-05 12:01:36 -0500152 [conversationID setStringValue: bestId];
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500153 [sendFileButton setEnabled:(convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type != lrc::api::profile::Type::SIP)];
Anthony Léonard2382b562017-12-13 15:51:28 -0500154
Olivier Soldanof563e152018-01-09 16:08:32 -0500155 BOOL hideBestId = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)];
Anthony Léonard2382b562017-12-13 15:51:28 -0500156
Olivier Soldanof563e152018-01-09 16:08:32 -0500157 [conversationID setHidden:hideBestId];
158 [titleCenteredConstraint setActive:hideBestId];
159 [titleTopConstraint setActive:!hideBestId];
Anthony Léonard2382b562017-12-13 15:51:28 -0500160}
161
Alexandre Lision4baba4c2016-02-11 13:00:57 -0500162- (void)loadView {
163 [super loadView];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500164 // Do view setup here.
165 [self.view setWantsLayer:YES];
166 [self.view setLayer:[CALayer layer]];
167 [self.view.layer setBackgroundColor:[NSColor ringGreyHighlight].CGColor];
168 [self.view.layer setCornerRadius:5.0f];
169
Kateryna Kostiuk64d025a2017-07-14 11:30:44 -0400170 [messageField setFocusRingType:NSFocusRingTypeNone];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500171}
172
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -0400173-(Account* ) chosenAccount
174{
175 QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex();
176 if(!index.isValid()) {
177 return nullptr;
178 }
179 Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>();
180 return account;
181}
182
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500183- (void) initFrame
184{
185 [self.view setFrame:self.view.superview.bounds];
186 [self.view setHidden:YES];
187 self.view.layer.position = self.view.frame.origin;
188}
189
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500190- (IBAction)sendMessage:(id)sender
191{
192 /* make sure there is text to send */
193 NSString* text = self.message;
194 if (text && text.length > 0) {
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500195 auto* conv = [self getCurrentConversation];
196 bool isPending = convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING;
Anthony Léonard4b0935f2017-12-26 12:05:22 -0500197 convModel_->sendMessage(convUid_, std::string([text UTF8String]));
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500198 self.message = @"";
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500199 if (isPending)
200 [delegate currentConversationTrusted];
Anthony Léonard2382b562017-12-13 15:51:28 -0500201 [messagesViewVC newMessageSent];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500202 }
203}
204
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500205- (IBAction)sendFile:(id)sender
206{
207 NSOpenPanel* filePicker = [NSOpenPanel openPanel];
208 [filePicker setCanChooseFiles:YES];
209 [filePicker setCanChooseDirectories:NO];
210 [filePicker setAllowsMultipleSelection:NO];
211
212 if ([filePicker runModal] == NSFileHandlingPanelOKButton) {
213 if ([[filePicker URLs] count] == 1) {
214 NSURL* url = [[filePicker URLs] objectAtIndex:0];
215 const char* fullPath = [url fileSystemRepresentation];
216 NSString* fileName = [url lastPathComponent];
217 if (convModel_) {
Anthony Léonard0c65bd12018-02-08 16:47:19 -0500218 auto* conv = [self getCurrentConversation];
219 bool isPending = convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING;
220
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500221 convModel_->sendFile(convUid_, std::string(fullPath), std::string([fileName UTF8String]));
Anthony Léonard0c65bd12018-02-08 16:47:19 -0500222
223 if (isPending)
224 [delegate currentConversationTrusted];
Anthony Léonarde7d62ed2018-01-25 10:51:47 -0500225 }
226 }
227 }
228}
229
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500230- (IBAction)placeCall:(id)sender
231{
Anthony Léonard2382b562017-12-13 15:51:28 -0500232 auto* conv = [self getCurrentConversation];
233 convModel_->placeCall(conv->uid);
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500234}
235
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500236- (IBAction)backPressed:(id)sender {
Anthony Léonard8585cc02017-12-28 14:03:45 -0500237 [delegate rightPanelClosed];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400238 [self hideWithAnimation:false];
Kateryna Kostiuk0ba1baf2017-05-12 16:51:27 -0400239}
240
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500241# pragma mark private IN/OUT animations
242
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400243-(void) showWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500244{
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400245 if (!animate) {
246 [self.view setHidden:NO];
247 return;
248 }
249
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500250 CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
251 [self.view setHidden:NO];
252
253 [CATransaction begin];
254 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
255 [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
256 [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
257 [animation setDuration:0.2f];
258 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
259 [self.view.layer addAnimation:animation forKey:animation.keyPath];
260
261 [CATransaction commit];
262}
263
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400264-(void) hideWithAnimation:(BOOL)animate
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500265{
266 if(self.view.frame.origin.x < 0) {
267 return;
268 }
269
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400270 if (!animate) {
271 [self.view setHidden:YES];
272 return;
273 }
274
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500275 CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
276 [CATransaction begin];
277 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
278 [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
279 [animation setToValue:[NSValue valueWithPoint:frame.origin]];
280 [animation setDuration:0.2f];
281 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
282
283 [CATransaction setCompletionBlock:^{
284 [self.view setHidden:YES];
285 }];
286 [self.view.layer addAnimation:animation forKey:animation.keyPath];
287
288 [self.view.layer setPosition:frame.origin];
289 [CATransaction commit];
290}
291
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500292#pragma mark - NSTextFieldDelegate
293
294- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
295{
296 if (commandSelector == @selector(insertNewline:) && self.message.length > 0) {
297 [self sendMessage:nil];
298 return YES;
299 }
300 return NO;
301}
302
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500303
304@end