Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 1 | /* |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 2 | * Copyright (C) 2016-2017 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 | |
| 23 | #import <QItemSelectionModel> |
| 24 | #import <qstring.h> |
| 25 | #import <QPixmap> |
| 26 | #import <QtMacExtras/qmacfunctions.h> |
| 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 | #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 Lision | 83180df | 2016-01-18 11:32:20 -0500 | [diff] [blame] | 35 | #import "INDSequentialTextSelectionManager.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 36 | #import "delegates/ImageManipulationDelegate.h" |
Kateryna Kostiuk | 0ba1baf | 2017-05-12 16:51:27 -0400 | [diff] [blame] | 37 | #import "PhoneDirectoryModel.h" |
| 38 | #import "account.h" |
| 39 | #import "AvailableAccountModel.h" |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 40 | #import "MessagesVC.h" |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 41 | #import "utils.h" |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 42 | #import "RingWindowController.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 43 | |
| 44 | #import <QuartzCore/QuartzCore.h> |
| 45 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 46 | @interface ConversationVC () { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 47 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 48 | __unsafe_unretained IBOutlet NSTextField* messageField; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 49 | NSMutableString* textSelection; |
| 50 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 51 | __unsafe_unretained IBOutlet NSView* sendPanel; |
| 52 | __unsafe_unretained IBOutlet NSTextField* conversationTitle; |
Olivier Soldano | 13c69e3 | 2018-01-05 12:01:36 -0500 | [diff] [blame] | 53 | __unsafe_unretained IBOutlet NSTextField *conversationID; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 54 | __unsafe_unretained IBOutlet IconButton* sendButton; |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 55 | __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth; |
| 56 | __unsafe_unretained IBOutlet NSButton* sentContactRequestButton; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 57 | IBOutlet MessagesVC* messagesViewVC; |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame] | 58 | |
Olivier Soldano | f563e15 | 2018-01-09 16:08:32 -0500 | [diff] [blame] | 59 | IBOutlet NSLayoutConstraint *titleCenteredConstraint; |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame] | 60 | IBOutlet NSLayoutConstraint* titleTopConstraint; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 61 | |
| 62 | std::string convUid_; |
| 63 | const lrc::api::conversation::Info* cachedConv_; |
| 64 | lrc::api::ConversationModel* convModel_; |
| 65 | |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 66 | RingWindowController* delegate; |
| 67 | |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 68 | // All those connections are needed to invalidate cached conversation as pointer |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 69 | // may not be referencing the same conversation anymore |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 70 | QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_; |
| 71 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 72 | } |
| 73 | |
Alexandre Lision | 83180df | 2016-01-18 11:32:20 -0500 | [diff] [blame] | 74 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 75 | @end |
| 76 | |
| 77 | @implementation ConversationVC |
| 78 | |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 79 | - (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éonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 88 | -(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éonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 98 | auto it = getConversationFromUid(convUid_, *convModel_); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 99 | |
| 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éonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 121 | 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éonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 126 | [self](){ |
| 127 | cachedConv_ = nil; |
| 128 | }); |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 129 | filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged, |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 130 | [self](){ |
| 131 | cachedConv_ = nil; |
| 132 | }); |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 133 | 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éonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 141 | |
| 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 Soldano | 13c69e3 | 2018-01-05 12:01:36 -0500 | [diff] [blame] | 149 | NSString* bestId = bestIDForConversation(*conv, *convModel_); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 150 | [conversationTitle setStringValue: bestName]; |
Olivier Soldano | 13c69e3 | 2018-01-05 12:01:36 -0500 | [diff] [blame] | 151 | [conversationID setStringValue: bestId]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 152 | |
Olivier Soldano | f563e15 | 2018-01-09 16:08:32 -0500 | [diff] [blame] | 153 | BOOL hideBestId = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 154 | |
Olivier Soldano | f563e15 | 2018-01-09 16:08:32 -0500 | [diff] [blame] | 155 | [conversationID setHidden:hideBestId]; |
| 156 | [titleCenteredConstraint setActive:hideBestId]; |
| 157 | [titleTopConstraint setActive:!hideBestId]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 158 | } |
| 159 | |
Alexandre Lision | 4baba4c | 2016-02-11 13:00:57 -0500 | [diff] [blame] | 160 | - (void)loadView { |
| 161 | [super loadView]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 162 | // 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 Kostiuk | 64d025a | 2017-07-14 11:30:44 -0400 | [diff] [blame] | 168 | [messageField setFocusRingType:NSFocusRingTypeNone]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 169 | } |
| 170 | |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 171 | -(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 Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 181 | - (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 Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 188 | - (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éonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 193 | auto* conv = [self getCurrentConversation]; |
| 194 | bool isPending = convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING; |
Anthony Léonard | 4b0935f | 2017-12-26 12:05:22 -0500 | [diff] [blame] | 195 | convModel_->sendMessage(convUid_, std::string([text UTF8String])); |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 196 | self.message = @""; |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 197 | if (isPending) |
| 198 | [delegate currentConversationTrusted]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 199 | [messagesViewVC newMessageSent]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | - (IBAction)placeCall:(id)sender |
| 204 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 205 | auto* conv = [self getCurrentConversation]; |
| 206 | convModel_->placeCall(conv->uid); |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 207 | } |
| 208 | |
Alexandre Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 209 | - (IBAction)backPressed:(id)sender { |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 210 | [delegate rightPanelClosed]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 211 | [self animateOut]; |
Kateryna Kostiuk | 0ba1baf | 2017-05-12 16:51:27 -0400 | [diff] [blame] | 212 | } |
| 213 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 214 | # 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 Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 232 | -(void) animateOut |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 233 | { |
| 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 Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 255 | #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 Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 266 | |
| 267 | @end |