blob: cd110f33c3573f978d47153ae2b202d1d6189765 [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"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050042
43#import <QuartzCore/QuartzCore.h>
44
Anthony Léonard2382b562017-12-13 15:51:28 -050045@interface ConversationVC () {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050046
Alexandre Lision0f66bd32016-01-18 11:30:45 -050047 __unsafe_unretained IBOutlet NSTextField* messageField;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050048 NSMutableString* textSelection;
49
Alexandre Lision0f66bd32016-01-18 11:30:45 -050050 __unsafe_unretained IBOutlet NSView* sendPanel;
51 __unsafe_unretained IBOutlet NSTextField* conversationTitle;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050052 __unsafe_unretained IBOutlet IconButton* sendButton;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050053 __unsafe_unretained IBOutlet NSPopUpButton* contactMethodsPopupButton;
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -040054 __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth;
55 __unsafe_unretained IBOutlet NSButton* sentContactRequestButton;
Kateryna Kostiuk58276bc2017-06-07 08:50:48 -040056 IBOutlet MessagesVC* messagesViewVC;
Anthony Léonardade9f9b2017-07-26 15:51:12 -040057
58 IBOutlet NSLayoutConstraint* titleHoverButtonConstraint;
59 IBOutlet NSLayoutConstraint* titleTopConstraint;
Anthony Léonard2382b562017-12-13 15:51:28 -050060
61 std::string convUid_;
62 const lrc::api::conversation::Info* cachedConv_;
63 lrc::api::ConversationModel* convModel_;
64
65 // Both are needed to invalidate cached conversation as pointer
66 // may not be referencing the same conversation anymore
67 QMetaObject::Connection modelSortedSignal_;
68 QMetaObject::Connection filterChangedSignal_;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050069}
70
Alexandre Lision83180df2016-01-18 11:32:20 -050071
Alexandre Lision0f66bd32016-01-18 11:30:45 -050072@end
73
74@implementation ConversationVC
75
Anthony Léonard2382b562017-12-13 15:51:28 -050076-(const lrc::api::conversation::Info*) getCurrentConversation
77{
78 if (convModel_ == nil || convUid_.empty())
79 return nil;
80
81 if (cachedConv_ != nil)
82 return cachedConv_;
83
84 auto& convQueue = convModel_->allFilteredConversations();
85
86 auto it = std::find_if(convQueue.begin(), convQueue.end(), [self](const lrc::api::conversation::Info& conv) {return conv.uid == convUid_;});
87
88 if (it != convQueue.end())
89 cachedConv_ = &(*it);
90
91 return cachedConv_;
92}
93
94-(void) setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model {
95 if (convUid_ == convUid && convModel_ == model)
96 return;
97
98 cachedConv_ = nil;
99 convUid_ = convUid;
100 convModel_ = model;
101
102 [messagesViewVC setConversationUid:convUid_ model:convModel_];
103
104 if (convUid_.empty() || convModel_ == nil)
105 return;
106
107 // Signals tracking changes in conversation list, we need them as cached conversation can be invalid
108 // after a reordering.
109 QObject::disconnect(modelSortedSignal_);
110 QObject::disconnect(filterChangedSignal_);
111 modelSortedSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted,
112 [self](){
113 cachedConv_ = nil;
114 });
115 filterChangedSignal_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged,
116 [self](){
117 cachedConv_ = nil;
118 });
119
120 auto* conv = [self getCurrentConversation];
121
122 if (conv == nil)
123 return;
124
125 // Setup UI elements according to new conversation
126 NSString* bestName = bestNameForConversation(*conv, *convModel_);
127 [conversationTitle setStringValue: bestName];
128
129 [contactMethodsPopupButton setEnabled:NO];
130 [contactMethodsPopupButton setBordered:NO];
131 BOOL hideCMPopupButton = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)];
132 [contactMethodsPopupButton setHidden:hideCMPopupButton];
133
134 [titleHoverButtonConstraint setActive:hideCMPopupButton];
135 [titleTopConstraint setActive:!hideCMPopupButton];
136}
137
Alexandre Lision4baba4c2016-02-11 13:00:57 -0500138- (void)loadView {
139 [super loadView];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500140 // Do view setup here.
141 [self.view setWantsLayer:YES];
142 [self.view setLayer:[CALayer layer]];
143 [self.view.layer setBackgroundColor:[NSColor ringGreyHighlight].CGColor];
144 [self.view.layer setCornerRadius:5.0f];
145
Kateryna Kostiuk64d025a2017-07-14 11:30:44 -0400146 [messageField setFocusRingType:NSFocusRingTypeNone];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500147}
148
Kateryna Kostiuk78e1f122017-06-14 14:52:34 -0400149-(Account* ) chosenAccount
150{
151 QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex();
152 if(!index.isValid()) {
153 return nullptr;
154 }
155 Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>();
156 return account;
157}
158
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500159- (void) initFrame
160{
161 [self.view setFrame:self.view.superview.bounds];
162 [self.view setHidden:YES];
163 self.view.layer.position = self.view.frame.origin;
164}
165
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500166- (IBAction)sendMessage:(id)sender
167{
168 /* make sure there is text to send */
169 NSString* text = self.message;
170 if (text && text.length > 0) {
Anthony Léonard4b0935f2017-12-26 12:05:22 -0500171 convModel_->sendMessage(convUid_, std::string([text UTF8String]));
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500172 self.message = @"";
Anthony Léonard2382b562017-12-13 15:51:28 -0500173 [messagesViewVC newMessageSent];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500174 }
175}
176
177- (IBAction)placeCall:(id)sender
178{
Anthony Léonard2382b562017-12-13 15:51:28 -0500179 auto* conv = [self getCurrentConversation];
180 convModel_->placeCall(conv->uid);
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500181}
182
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500183- (IBAction)backPressed:(id)sender {
Anthony Léonard2382b562017-12-13 15:51:28 -0500184 [self animateOut];
Kateryna Kostiuk0ba1baf2017-05-12 16:51:27 -0400185}
186
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500187# pragma mark private IN/OUT animations
188
189-(void) animateIn
190{
191 CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
192 [self.view setHidden:NO];
193
194 [CATransaction begin];
195 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
196 [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
197 [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
198 [animation setDuration:0.2f];
199 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
200 [self.view.layer addAnimation:animation forKey:animation.keyPath];
201
202 [CATransaction commit];
203}
204
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500205-(void) animateOut
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500206{
207 if(self.view.frame.origin.x < 0) {
208 return;
209 }
210
211 CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
212 [CATransaction begin];
213 CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
214 [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
215 [animation setToValue:[NSValue valueWithPoint:frame.origin]];
216 [animation setDuration:0.2f];
217 [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
218
219 [CATransaction setCompletionBlock:^{
220 [self.view setHidden:YES];
221 }];
222 [self.view.layer addAnimation:animation forKey:animation.keyPath];
223
224 [self.view.layer setPosition:frame.origin];
225 [CATransaction commit];
226}
227
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500228#pragma mark - NSTextFieldDelegate
229
230- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
231{
232 if (commandSelector == @selector(insertNewline:) && self.message.length > 0) {
233 [self sendMessage:nil];
234 return YES;
235 }
236 return NO;
237}
238
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500239
240@end