Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 1 | /* |
Anthony Léonard | e7d62ed | 2018-01-25 10:51:47 -0500 | [diff] [blame] | 2 | * Copyright (C) 2016-2018 Savoir-faire Linux Inc. |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 3 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 4 | * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com> |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 5 | * |
| 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 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 23 | #import <qstring.h> |
| 24 | #import <QPixmap> |
| 25 | #import <QtMacExtras/qmacfunctions.h> |
Kateryna Kostiuk | 1ba5a01 | 2018-12-12 17:07:38 -0500 | [diff] [blame] | 26 | #import <QuartzCore/QuartzCore.h> |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 27 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 28 | // LRC |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 29 | |
| 30 | #import "views/IconButton.h" |
Kateryna Kostiuk | db95b47 | 2018-04-17 16:35:57 -0400 | [diff] [blame] | 31 | #import "views/HoverButton.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 32 | #import "views/IMTableCellView.h" |
| 33 | #import "views/NSColor+RingTheme.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 34 | #import "delegates/ImageManipulationDelegate.h" |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 35 | #import "MessagesVC.h" |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 36 | #import "utils.h" |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 37 | #import "RingWindowController.h" |
Kateryna Kostiuk | a0f1686 | 2018-05-04 09:11:41 -0400 | [diff] [blame] | 38 | #import "NSString+Extensions.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 39 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 40 | @interface ConversationVC () { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 41 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 42 | __unsafe_unretained IBOutlet NSTextField* conversationTitle; |
Olivier Soldano | 13c69e3 | 2018-01-05 12:01:36 -0500 | [diff] [blame] | 43 | __unsafe_unretained IBOutlet NSTextField *conversationID; |
Kateryna Kostiuk | db95b47 | 2018-04-17 16:35:57 -0400 | [diff] [blame] | 44 | __unsafe_unretained IBOutlet HoverButton *addContactButton; |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 45 | __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth; |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 46 | |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 47 | __unsafe_unretained IBOutlet NSButton* sentContactRequestButton; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 48 | IBOutlet MessagesVC* messagesViewVC; |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame] | 49 | |
Olivier Soldano | f563e15 | 2018-01-09 16:08:32 -0500 | [diff] [blame] | 50 | IBOutlet NSLayoutConstraint *titleCenteredConstraint; |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame] | 51 | IBOutlet NSLayoutConstraint* titleTopConstraint; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 52 | |
| 53 | std::string convUid_; |
| 54 | const lrc::api::conversation::Info* cachedConv_; |
| 55 | lrc::api::ConversationModel* convModel_; |
| 56 | |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 57 | RingWindowController* delegate; |
| 58 | |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 59 | // All those connections are needed to invalidate cached conversation as pointer |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 60 | // may not be referencing the same conversation anymore |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 61 | QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @end |
| 65 | |
Kateryna Kostiuk | 4f37d95 | 2018-12-04 13:19:17 -0500 | [diff] [blame] | 66 | NSInteger const MEESAGE_MARGIN = 21; |
| 67 | NSInteger const SEND_PANEL_DEFAULT_HEIGHT = 60; |
| 68 | NSInteger const SEND_PANEL_MAX_HEIGHT = 120; |
| 69 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 70 | @implementation ConversationVC |
| 71 | |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 72 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil delegate:(RingWindowController*) mainWindow |
| 73 | { |
| 74 | if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) |
| 75 | { |
| 76 | delegate = mainWindow; |
| 77 | } |
| 78 | return self; |
| 79 | } |
| 80 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 81 | -(void) clearData { |
| 82 | cachedConv_ = nil; |
| 83 | convUid_ = ""; |
| 84 | convModel_ = nil; |
| 85 | |
| 86 | [messagesViewVC clearData]; |
| 87 | QObject::disconnect(modelSortedConnection_); |
| 88 | QObject::disconnect(filterChangedConnection_); |
| 89 | QObject::disconnect(newConversationConnection_); |
| 90 | QObject::disconnect(conversationRemovedConnection_); |
| 91 | } |
| 92 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 93 | -(const lrc::api::conversation::Info*) getCurrentConversation |
| 94 | { |
| 95 | if (convModel_ == nil || convUid_.empty()) |
| 96 | return nil; |
| 97 | |
| 98 | if (cachedConv_ != nil) |
| 99 | return cachedConv_; |
| 100 | |
Kateryna Kostiuk | ae660fd | 2018-04-24 14:10:41 -0400 | [diff] [blame] | 101 | auto convQueue = convModel_->allFilteredConversations(); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 102 | |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 103 | auto it = getConversationFromUid(convUid_, *convModel_); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 104 | |
| 105 | if (it != convQueue.end()) |
| 106 | cachedConv_ = &(*it); |
| 107 | |
| 108 | return cachedConv_; |
| 109 | } |
| 110 | |
| 111 | -(void) setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model { |
| 112 | if (convUid_ == convUid && convModel_ == model) |
| 113 | return; |
Kateryna Kostiuk | 0f0ba99 | 2018-06-07 14:22:58 -0400 | [diff] [blame] | 114 | [self clearData]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 115 | cachedConv_ = nil; |
| 116 | convUid_ = convUid; |
| 117 | convModel_ = model; |
| 118 | |
| 119 | [messagesViewVC setConversationUid:convUid_ model:convModel_]; |
| 120 | |
| 121 | if (convUid_.empty() || convModel_ == nil) |
| 122 | return; |
| 123 | |
| 124 | // Signals tracking changes in conversation list, we need them as cached conversation can be invalid |
| 125 | // after a reordering. |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 126 | QObject::disconnect(modelSortedConnection_); |
| 127 | QObject::disconnect(filterChangedConnection_); |
| 128 | QObject::disconnect(newConversationConnection_); |
| 129 | QObject::disconnect(conversationRemovedConnection_); |
| 130 | modelSortedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted, |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 131 | [self](){ |
| 132 | cachedConv_ = nil; |
| 133 | }); |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 134 | filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged, |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 135 | [self](){ |
| 136 | cachedConv_ = nil; |
| 137 | }); |
Kateryna Kostiuk | 557f5ea | 2018-03-29 13:41:53 -0400 | [diff] [blame] | 138 | newConversationConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newConversation, |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 139 | [self](){ |
| 140 | cachedConv_ = nil; |
| 141 | }); |
Kateryna Kostiuk | 557f5ea | 2018-03-29 13:41:53 -0400 | [diff] [blame] | 142 | conversationRemovedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationRemoved, |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 143 | [self](){ |
| 144 | cachedConv_ = nil; |
| 145 | }); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 146 | |
| 147 | auto* conv = [self getCurrentConversation]; |
| 148 | |
| 149 | if (conv == nil) |
| 150 | return; |
| 151 | |
| 152 | // Setup UI elements according to new conversation |
| 153 | NSString* bestName = bestNameForConversation(*conv, *convModel_); |
Olivier Soldano | 13c69e3 | 2018-01-05 12:01:36 -0500 | [diff] [blame] | 154 | NSString* bestId = bestIDForConversation(*conv, *convModel_); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 155 | [conversationTitle setStringValue: bestName]; |
Olivier Soldano | 13c69e3 | 2018-01-05 12:01:36 -0500 | [diff] [blame] | 156 | [conversationID setStringValue: bestId]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 157 | |
Olivier Soldano | f563e15 | 2018-01-09 16:08:32 -0500 | [diff] [blame] | 158 | BOOL hideBestId = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 159 | |
Olivier Soldano | f563e15 | 2018-01-09 16:08:32 -0500 | [diff] [blame] | 160 | [conversationID setHidden:hideBestId]; |
| 161 | [titleCenteredConstraint setActive:hideBestId]; |
| 162 | [titleTopConstraint setActive:!hideBestId]; |
Kateryna Kostiuk | db95b47 | 2018-04-17 16:35:57 -0400 | [diff] [blame] | 163 | auto accountType = convModel_->owner.profileInfo.type; |
| 164 | [addContactButton setHidden:((convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type != lrc::api::profile::Type::TEMPORARY) || accountType == lrc::api::profile::Type::SIP)]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 165 | } |
| 166 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 167 | - (void) initFrame |
| 168 | { |
| 169 | [self.view setFrame:self.view.superview.bounds]; |
| 170 | [self.view setHidden:YES]; |
| 171 | self.view.layer.position = self.view.frame.origin; |
| 172 | } |
| 173 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 174 | - (IBAction)placeCall:(id)sender |
| 175 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 176 | auto* conv = [self getCurrentConversation]; |
| 177 | convModel_->placeCall(conv->uid); |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 178 | } |
| 179 | |
Kateryna Kostiuk | db95b47 | 2018-04-17 16:35:57 -0400 | [diff] [blame] | 180 | - (IBAction)placeAudioCall:(id)sender |
| 181 | { |
| 182 | auto* conv = [self getCurrentConversation]; |
| 183 | convModel_->placeAudioOnlyCall(conv->uid); |
| 184 | } |
| 185 | |
| 186 | - (IBAction)addContact:(id)sender |
| 187 | { |
| 188 | auto* conv = [self getCurrentConversation]; |
| 189 | convModel_->makePermanent(conv->uid); |
| 190 | } |
| 191 | |
Alexandre Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 192 | - (IBAction)backPressed:(id)sender { |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 193 | [delegate rightPanelClosed]; |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 194 | [self hideWithAnimation:false]; |
Kateryna Kostiuk | 0ba1baf | 2017-05-12 16:51:27 -0400 | [diff] [blame] | 195 | } |
| 196 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 197 | # pragma mark private IN/OUT animations |
| 198 | |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 199 | -(void) showWithAnimation:(BOOL)animate |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 200 | { |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 201 | if (!animate) { |
| 202 | [self.view setHidden:NO]; |
| 203 | return; |
| 204 | } |
| 205 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 206 | CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0); |
| 207 | [self.view setHidden:NO]; |
| 208 | |
| 209 | [CATransaction begin]; |
| 210 | CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; |
| 211 | [animation setFromValue:[NSValue valueWithPoint:frame.origin]]; |
| 212 | [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]]; |
| 213 | [animation setDuration:0.2f]; |
| 214 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; |
| 215 | [self.view.layer addAnimation:animation forKey:animation.keyPath]; |
| 216 | |
| 217 | [CATransaction commit]; |
| 218 | } |
| 219 | |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 220 | -(void) hideWithAnimation:(BOOL)animate |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 221 | { |
| 222 | if(self.view.frame.origin.x < 0) { |
| 223 | return; |
| 224 | } |
| 225 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 226 | [self clearData]; |
| 227 | |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 228 | if (!animate) { |
| 229 | [self.view setHidden:YES]; |
| 230 | return; |
| 231 | } |
| 232 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 233 | CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0); |
| 234 | [CATransaction begin]; |
| 235 | CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; |
| 236 | [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]]; |
| 237 | [animation setToValue:[NSValue valueWithPoint:frame.origin]]; |
| 238 | [animation setDuration:0.2f]; |
| 239 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
| 240 | |
| 241 | [CATransaction setCompletionBlock:^{ |
| 242 | [self.view setHidden:YES]; |
| 243 | }]; |
| 244 | [self.view.layer addAnimation:animation forKey:animation.keyPath]; |
| 245 | |
| 246 | [self.view.layer setPosition:frame.origin]; |
| 247 | [CATransaction commit]; |
| 248 | } |
| 249 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 250 | |
| 251 | @end |