blob: b53d9d314d1fc9cd2c3d5102925784f74789834b [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
59 IBOutlet NSLayoutConstraint* titleHoverButtonConstraint;
60 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_);
150 if (bestName == bestId) {
151 bestId = @(model->owner.contactModel->getContact(conv->participants[0]).profileInfo.uri.c_str());
152 }
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éonard2382b562017-12-13 15:51:28 -0500155
Anthony Léonard2382b562017-12-13 15:51:28 -0500156 BOOL hideCMPopupButton = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)];
Anthony Léonard2382b562017-12-13 15:51:28 -0500157
158 [titleHoverButtonConstraint setActive:hideCMPopupButton];
159 [titleTopConstraint setActive:!hideCMPopupButton];
160}
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é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éonard2382b562017-12-13 15:51:28 -0500197 [messagesViewVC newMessageSent];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500198 }
199}
200
201- (IBAction)placeCall:(id)sender
202{
Anthony Léonard2382b562017-12-13 15:51:28 -0500203 auto* conv = [self getCurrentConversation];
204 convModel_->placeCall(conv->uid);
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500205}
206
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500207- (IBAction)backPressed:(id)sender {
Anthony Léonard8585cc02017-12-28 14:03:45 -0500208 [delegate rightPanelClosed];
Anthony Léonard2382b562017-12-13 15:51:28 -0500209 [self animateOut];
Kateryna Kostiuk0ba1baf2017-05-12 16:51:27 -0400210}
211
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500212# pragma mark private IN/OUT animations
213
214-(void) animateIn
215{
216 CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
217 [self.view setHidden:NO];
218
219 [CATransaction begin];
220 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
221 [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
222 [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
223 [animation setDuration:0.2f];
224 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
225 [self.view.layer addAnimation:animation forKey:animation.keyPath];
226
227 [CATransaction commit];
228}
229
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500230-(void) animateOut
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500231{
232 if(self.view.frame.origin.x < 0) {
233 return;
234 }
235
236 CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
237 [CATransaction begin];
238 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
239 [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
240 [animation setToValue:[NSValue valueWithPoint:frame.origin]];
241 [animation setDuration:0.2f];
242 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
243
244 [CATransaction setCompletionBlock:^{
245 [self.view setHidden:YES];
246 }];
247 [self.view.layer addAnimation:animation forKey:animation.keyPath];
248
249 [self.view.layer setPosition:frame.origin];
250 [CATransaction commit];
251}
252
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500253#pragma mark - NSTextFieldDelegate
254
255- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
256{
257 if (commandSelector == @selector(insertNewline:) && self.message.length > 0) {
258 [self sendMessage:nil];
259 return YES;
260 }
261 return NO;
262}
263
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500264
265@end