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" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 42 | |
| 43 | #import <QuartzCore/QuartzCore.h> |
| 44 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 45 | @interface ConversationVC () { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 46 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 47 | __unsafe_unretained IBOutlet NSTextField* messageField; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 48 | NSMutableString* textSelection; |
| 49 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 50 | __unsafe_unretained IBOutlet NSView* sendPanel; |
| 51 | __unsafe_unretained IBOutlet NSTextField* conversationTitle; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 52 | __unsafe_unretained IBOutlet IconButton* sendButton; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 53 | __unsafe_unretained IBOutlet NSPopUpButton* contactMethodsPopupButton; |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 54 | __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth; |
| 55 | __unsafe_unretained IBOutlet NSButton* sentContactRequestButton; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 56 | IBOutlet MessagesVC* messagesViewVC; |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame] | 57 | |
| 58 | IBOutlet NSLayoutConstraint* titleHoverButtonConstraint; |
| 59 | IBOutlet NSLayoutConstraint* titleTopConstraint; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 60 | |
| 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 Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 69 | } |
| 70 | |
Alexandre Lision | 83180df | 2016-01-18 11:32:20 -0500 | [diff] [blame] | 71 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 72 | @end |
| 73 | |
| 74 | @implementation ConversationVC |
| 75 | |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 76 | -(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 Lision | 4baba4c | 2016-02-11 13:00:57 -0500 | [diff] [blame] | 138 | - (void)loadView { |
| 139 | [super loadView]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 140 | // 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 Kostiuk | 64d025a | 2017-07-14 11:30:44 -0400 | [diff] [blame] | 146 | [messageField setFocusRingType:NSFocusRingTypeNone]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 147 | } |
| 148 | |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 149 | -(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 Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 159 | - (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 Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 166 | - (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éonard | 4b0935f | 2017-12-26 12:05:22 -0500 | [diff] [blame^] | 171 | convModel_->sendMessage(convUid_, std::string([text UTF8String])); |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 172 | self.message = @""; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 173 | [messagesViewVC newMessageSent]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
| 177 | - (IBAction)placeCall:(id)sender |
| 178 | { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 179 | auto* conv = [self getCurrentConversation]; |
| 180 | convModel_->placeCall(conv->uid); |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 181 | } |
| 182 | |
Alexandre Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 183 | - (IBAction)backPressed:(id)sender { |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 184 | [self animateOut]; |
Kateryna Kostiuk | 0ba1baf | 2017-05-12 16:51:27 -0400 | [diff] [blame] | 185 | } |
| 186 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 187 | # 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 Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 205 | -(void) animateOut |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 206 | { |
| 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 Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 228 | #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 Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 239 | |
| 240 | @end |