blob: f697506002e802b396a8491266627f68736cdf54 [file] [log] [blame]
Alexandre Lision0f66bd32016-01-18 11:30:45 -05001/*
Anthony Léonard2382b562017-12-13 15:51:28 -05002 * Copyright (C) 2016-2017 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;
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -040055 __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth;
56 __unsafe_unretained IBOutlet NSButton* sentContactRequestButton;
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040057 IBOutlet MessagesVC* messagesViewVC;
Anthony Léonardade9f9b2017-07-26 15:51:12 -040058
Olivier Soldanof563e152018-01-09 16:08:32 -050059 IBOutlet NSLayoutConstraint *titleCenteredConstraint;
Anthony Léonardade9f9b2017-07-26 15:51:12 -040060 IBOutlet NSLayoutConstraint* titleTopConstraint;
Anthony Léonard2382b562017-12-13 15:51:28 -050061
62 std::string convUid_;
63 const lrc::api::conversation::Info* cachedConv_;
64 lrc::api::ConversationModel* convModel_;
65
Anthony Léonard8585cc02017-12-28 14:03:45 -050066 RingWindowController* delegate;
67
Anthony Léonard01634102017-12-27 16:37:16 -050068 // All those connections are needed to invalidate cached conversation as pointer
Anthony Léonard2382b562017-12-13 15:51:28 -050069 // may not be referencing the same conversation anymore
Anthony Léonard01634102017-12-27 16:37:16 -050070 QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_;
71
Alexandre Lision0f66bd32016-01-18 11:30:45 -050072}
73
Alexandre Lision83180df2016-01-18 11:32:20 -050074
Alexandre Lision0f66bd32016-01-18 11:30:45 -050075@end
76
77@implementation ConversationVC
78
Anthony Léonard8585cc02017-12-28 14:03:45 -050079- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil delegate:(RingWindowController*) mainWindow
80{
81 if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
82 {
83 delegate = mainWindow;
84 }
85 return self;
86}
87
Anthony Léonard2382b562017-12-13 15:51:28 -050088-(const lrc::api::conversation::Info*) getCurrentConversation
89{
90 if (convModel_ == nil || convUid_.empty())
91 return nil;
92
93 if (cachedConv_ != nil)
94 return cachedConv_;
95
96 auto& convQueue = convModel_->allFilteredConversations();
97
Anthony Léonard6f819752018-01-05 09:53:40 -050098 auto it = getConversationFromUid(convUid_, *convModel_);
Anthony Léonard2382b562017-12-13 15:51:28 -050099
100 if (it != convQueue.end())
101 cachedConv_ = &(*it);
102
103 return cachedConv_;
104}
105
106-(void) setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model {
107 if (convUid_ == convUid && convModel_ == model)
108 return;
109
110 cachedConv_ = nil;
111 convUid_ = convUid;
112 convModel_ = model;
113
114 [messagesViewVC setConversationUid:convUid_ model:convModel_];
115
116 if (convUid_.empty() || convModel_ == nil)
117 return;
118
119 // Signals tracking changes in conversation list, we need them as cached conversation can be invalid
120 // after a reordering.
Anthony Léonard01634102017-12-27 16:37:16 -0500121 QObject::disconnect(modelSortedConnection_);
122 QObject::disconnect(filterChangedConnection_);
123 QObject::disconnect(newConversationConnection_);
124 QObject::disconnect(conversationRemovedConnection_);
125 modelSortedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted,
Anthony Léonard2382b562017-12-13 15:51:28 -0500126 [self](){
127 cachedConv_ = nil;
128 });
Anthony Léonard01634102017-12-27 16:37:16 -0500129 filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged,
Anthony Léonard2382b562017-12-13 15:51:28 -0500130 [self](){
131 cachedConv_ = nil;
132 });
Anthony Léonard01634102017-12-27 16:37:16 -0500133 filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newConversation,
134 [self](){
135 cachedConv_ = nil;
136 });
137 filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationRemoved,
138 [self](){
139 cachedConv_ = nil;
140 });
Anthony Léonard2382b562017-12-13 15:51:28 -0500141
142 auto* conv = [self getCurrentConversation];
143
144 if (conv == nil)
145 return;
146
147 // Setup UI elements according to new conversation
148 NSString* bestName = bestNameForConversation(*conv, *convModel_);
Olivier Soldano13c69e32018-01-05 12:01:36 -0500149 NSString* bestId = bestIDForConversation(*conv, *convModel_);
Anthony Léonard2382b562017-12-13 15:51:28 -0500150 [conversationTitle setStringValue: bestName];
Olivier Soldano13c69e32018-01-05 12:01:36 -0500151 [conversationID setStringValue: bestId];
Anthony Léonard2382b562017-12-13 15:51:28 -0500152
Olivier Soldanof563e152018-01-09 16:08:32 -0500153 BOOL hideBestId = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)];
Anthony Léonard2382b562017-12-13 15:51:28 -0500154
Olivier Soldanof563e152018-01-09 16:08:32 -0500155 [conversationID setHidden:hideBestId];
156 [titleCenteredConstraint setActive:hideBestId];
157 [titleTopConstraint setActive:!hideBestId];
Anthony Léonard2382b562017-12-13 15:51:28 -0500158}
159
Alexandre Lision4baba4c2016-02-11 13:00:57 -0500160- (void)loadView {
161 [super loadView];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500162 // Do view setup here.
163 [self.view setWantsLayer:YES];
164 [self.view setLayer:[CALayer layer]];
165 [self.view.layer setBackgroundColor:[NSColor ringGreyHighlight].CGColor];
166 [self.view.layer setCornerRadius:5.0f];
167
Kateryna Kostiuk64d025a2017-07-14 11:30:44 -0400168 [messageField setFocusRingType:NSFocusRingTypeNone];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500169}
170
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -0400171-(Account* ) chosenAccount
172{
173 QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex();
174 if(!index.isValid()) {
175 return nullptr;
176 }
177 Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>();
178 return account;
179}
180
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500181- (void) initFrame
182{
183 [self.view setFrame:self.view.superview.bounds];
184 [self.view setHidden:YES];
185 self.view.layer.position = self.view.frame.origin;
186}
187
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500188- (IBAction)sendMessage:(id)sender
189{
190 /* make sure there is text to send */
191 NSString* text = self.message;
192 if (text && text.length > 0) {
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500193 auto* conv = [self getCurrentConversation];
194 bool isPending = convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING;
Anthony Léonard4b0935f2017-12-26 12:05:22 -0500195 convModel_->sendMessage(convUid_, std::string([text UTF8String]));
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500196 self.message = @"";
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500197 if (isPending)
198 [delegate currentConversationTrusted];
Anthony Léonard2382b562017-12-13 15:51:28 -0500199 [messagesViewVC newMessageSent];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500200 }
201}
202
203- (IBAction)placeCall:(id)sender
204{
Anthony Léonard2382b562017-12-13 15:51:28 -0500205 auto* conv = [self getCurrentConversation];
206 convModel_->placeCall(conv->uid);
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500207}
208
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500209- (IBAction)backPressed:(id)sender {
Anthony Léonard8585cc02017-12-28 14:03:45 -0500210 [delegate rightPanelClosed];
Anthony Léonard2382b562017-12-13 15:51:28 -0500211 [self animateOut];
Kateryna Kostiuk0ba1baf2017-05-12 16:51:27 -0400212}
213
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500214# pragma mark private IN/OUT animations
215
216-(void) animateIn
217{
218 CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
219 [self.view setHidden:NO];
220
221 [CATransaction begin];
222 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
223 [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
224 [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
225 [animation setDuration:0.2f];
226 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
227 [self.view.layer addAnimation:animation forKey:animation.keyPath];
228
229 [CATransaction commit];
230}
231
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500232-(void) animateOut
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500233{
234 if(self.view.frame.origin.x < 0) {
235 return;
236 }
237
238 CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
239 [CATransaction begin];
240 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
241 [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
242 [animation setToValue:[NSValue valueWithPoint:frame.origin]];
243 [animation setDuration:0.2f];
244 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
245
246 [CATransaction setCompletionBlock:^{
247 [self.view setHidden:YES];
248 }];
249 [self.view.layer addAnimation:animation forKey:animation.keyPath];
250
251 [self.view.layer setPosition:frame.origin];
252 [CATransaction commit];
253}
254
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500255#pragma mark - NSTextFieldDelegate
256
257- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
258{
259 if (commandSelector == @selector(insertNewline:) && self.message.length > 0) {
260 [self sendMessage:nil];
261 return YES;
262 }
263 return NO;
264}
265
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500266
267@end