Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 Savoir-faire Linux Inc. |
| 3 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #import "ConversationVC.h" |
| 21 | |
| 22 | #import <QItemSelectionModel> |
| 23 | #import <qstring.h> |
| 24 | #import <QPixmap> |
| 25 | #import <QtMacExtras/qmacfunctions.h> |
| 26 | |
| 27 | #import <media/media.h> |
| 28 | #import <recentmodel.h> |
| 29 | #import <person.h> |
| 30 | #import <contactmethod.h> |
| 31 | #import <media/text.h> |
| 32 | #import <media/textrecording.h> |
| 33 | #import <callmodel.h> |
| 34 | #import <globalinstances.h> |
| 35 | |
| 36 | #import "views/IconButton.h" |
| 37 | #import "views/IMTableCellView.h" |
| 38 | #import "views/NSColor+RingTheme.h" |
| 39 | #import "QNSTreeController.h" |
Alexandre Lision | 83180df | 2016-01-18 11:32:20 -0500 | [diff] [blame] | 40 | #import "INDSequentialTextSelectionManager.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 41 | #import "delegates/ImageManipulationDelegate.h" |
Kateryna Kostiuk | 0ba1baf | 2017-05-12 16:51:27 -0400 | [diff] [blame] | 42 | #import "PhoneDirectoryModel.h" |
| 43 | #import "account.h" |
| 44 | #import "AvailableAccountModel.h" |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 45 | #import "MessagesVC.h" |
Kateryna Kostiuk | 0ba1baf | 2017-05-12 16:51:27 -0400 | [diff] [blame] | 46 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 47 | |
| 48 | #import <QuartzCore/QuartzCore.h> |
| 49 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 50 | @interface ConversationVC () <NSOutlineViewDelegate, MessagesVCDelegate> { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 51 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 52 | __unsafe_unretained IBOutlet NSTextField* messageField; |
| 53 | QVector<ContactMethod*> contactMethods; |
| 54 | NSMutableString* textSelection; |
| 55 | |
Alexandre Lision | 05ca83c | 2016-11-24 19:31:08 -0500 | [diff] [blame] | 56 | QMetaObject::Connection contactMethodChanged; |
| 57 | ContactMethod* selectedContactMethod; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 58 | |
| 59 | __unsafe_unretained IBOutlet NSView* sendPanel; |
| 60 | __unsafe_unretained IBOutlet NSTextField* conversationTitle; |
| 61 | __unsafe_unretained IBOutlet NSTextField* emptyConversationPlaceHolder; |
| 62 | __unsafe_unretained IBOutlet IconButton* sendButton; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 63 | __unsafe_unretained IBOutlet NSPopUpButton* contactMethodsPopupButton; |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 64 | __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth; |
| 65 | __unsafe_unretained IBOutlet NSButton* sentContactRequestButton; |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 66 | IBOutlet MessagesVC* messagesViewVC; |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame^] | 67 | |
| 68 | IBOutlet NSLayoutConstraint* titleHoverButtonConstraint; |
| 69 | IBOutlet NSLayoutConstraint* titleTopConstraint; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 70 | } |
| 71 | |
Alexandre Lision | 83180df | 2016-01-18 11:32:20 -0500 | [diff] [blame] | 72 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 73 | @end |
| 74 | |
| 75 | @implementation ConversationVC |
| 76 | |
Alexandre Lision | 4baba4c | 2016-02-11 13:00:57 -0500 | [diff] [blame] | 77 | - (void)loadView { |
| 78 | [super loadView]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 79 | // Do view setup here. |
| 80 | [self.view setWantsLayer:YES]; |
| 81 | [self.view setLayer:[CALayer layer]]; |
| 82 | [self.view.layer setBackgroundColor:[NSColor ringGreyHighlight].CGColor]; |
| 83 | [self.view.layer setCornerRadius:5.0f]; |
| 84 | |
Kateryna Kostiuk | 64d025a | 2017-07-14 11:30:44 -0400 | [diff] [blame] | 85 | [messageField setFocusRingType:NSFocusRingTypeNone]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 86 | |
| 87 | [self setupChat]; |
| 88 | |
| 89 | } |
| 90 | |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 91 | -(Account* ) chosenAccount |
| 92 | { |
| 93 | QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex(); |
| 94 | if(!index.isValid()) { |
| 95 | return nullptr; |
| 96 | } |
| 97 | Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>(); |
| 98 | return account; |
| 99 | } |
| 100 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 101 | - (void) initFrame |
| 102 | { |
| 103 | [self.view setFrame:self.view.superview.bounds]; |
| 104 | [self.view setHidden:YES]; |
| 105 | self.view.layer.position = self.view.frame.origin; |
| 106 | } |
| 107 | |
| 108 | - (void) setupChat |
| 109 | { |
| 110 | QObject::connect(RecentModel::instance().selectionModel(), |
| 111 | &QItemSelectionModel::currentChanged, |
| 112 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
| 113 | |
| 114 | contactMethods = RecentModel::instance().getContactMethods(current); |
| 115 | if (contactMethods.isEmpty()) { |
| 116 | return ; |
| 117 | } |
| 118 | |
| 119 | [contactMethodsPopupButton removeAllItems]; |
| 120 | for (auto cm : contactMethods) { |
Anthony Léonard | a63112f | 2017-07-19 11:25:38 -0400 | [diff] [blame] | 121 | [contactMethodsPopupButton addItemWithTitle:cm->bestId().toNSString()]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Kateryna Kostiuk | 64d025a | 2017-07-14 11:30:44 -0400 | [diff] [blame] | 124 | BOOL isSMultipleCM = (contactMethods.length() > 1); |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame^] | 125 | BOOL hideCMPopupButton = !isSMultipleCM && (contactMethods.first()->bestId() == contactMethods.first()->bestName()); |
Kateryna Kostiuk | 64d025a | 2017-07-14 11:30:44 -0400 | [diff] [blame] | 126 | |
| 127 | [contactMethodsPopupButton setEnabled:isSMultipleCM]; |
| 128 | [contactMethodsPopupButton setBordered:isSMultipleCM]; |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame^] | 129 | [contactMethodsPopupButton setHidden:hideCMPopupButton]; |
Kateryna Kostiuk | 64d025a | 2017-07-14 11:30:44 -0400 | [diff] [blame] | 130 | [[contactMethodsPopupButton cell] setArrowPosition: !isSMultipleCM ? NSPopUpNoArrow : NSPopUpArrowAtBottom]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 131 | |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame^] | 132 | [titleHoverButtonConstraint setActive:hideCMPopupButton]; |
| 133 | [titleTopConstraint setActive:!hideCMPopupButton]; |
| 134 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 135 | [emptyConversationPlaceHolder setHidden:NO]; |
| 136 | // Select first cm |
| 137 | [contactMethodsPopupButton selectItemAtIndex:0]; |
| 138 | [self itemChanged:contactMethodsPopupButton]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 139 | }); |
| 140 | } |
| 141 | |
| 142 | - (IBAction)sendMessage:(id)sender |
| 143 | { |
| 144 | /* make sure there is text to send */ |
| 145 | NSString* text = self.message; |
| 146 | if (text && text.length > 0) { |
| 147 | QMap<QString, QString> messages; |
| 148 | messages["text/plain"] = QString::fromNSString(text); |
| 149 | contactMethods.at([contactMethodsPopupButton indexOfSelectedItem])->sendOfflineTextMessage(messages); |
| 150 | self.message = @""; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | - (IBAction)placeCall:(id)sender |
| 155 | { |
| 156 | if(auto cm = contactMethods.at([contactMethodsPopupButton indexOfSelectedItem])) { |
| 157 | auto c = CallModel::instance().dialingCall(); |
| 158 | c->setPeerContactMethod(cm); |
| 159 | c << Call::Action::ACCEPT; |
| 160 | CallModel::instance().selectCall(c); |
| 161 | } |
| 162 | } |
| 163 | |
Alexandre Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 164 | - (IBAction)backPressed:(id)sender { |
Alexandre Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 165 | RecentModel::instance().selectionModel()->clearCurrentIndex(); |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 166 | messagesViewVC.delegate = nil; |
Alexandre Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 167 | } |
| 168 | |
Kateryna Kostiuk | 8807191 | 2017-07-11 14:41:01 -0400 | [diff] [blame] | 169 | - (IBAction)sendContactRequest:(id)sender |
Kateryna Kostiuk | 0ba1baf | 2017-05-12 16:51:27 -0400 | [diff] [blame] | 170 | { |
Kateryna Kostiuk | 8807191 | 2017-07-11 14:41:01 -0400 | [diff] [blame] | 171 | auto cm = contactMethods.at([contactMethodsPopupButton indexOfSelectedItem]); |
| 172 | if(cm) { |
| 173 | if(cm->account() == nullptr) { |
| 174 | cm->setAccount([self chosenAccount]); |
| 175 | } |
| 176 | |
| 177 | if(cm->account() == nullptr) { |
| 178 | return; |
| 179 | } |
| 180 | cm->account()->sendContactRequest(cm); |
Kateryna Kostiuk | 0ba1baf | 2017-05-12 16:51:27 -0400 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 184 | # pragma mark private IN/OUT animations |
| 185 | |
| 186 | -(void) animateIn |
| 187 | { |
| 188 | CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0); |
| 189 | [self.view setHidden:NO]; |
| 190 | |
| 191 | [CATransaction begin]; |
| 192 | CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; |
| 193 | [animation setFromValue:[NSValue valueWithPoint:frame.origin]]; |
| 194 | [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]]; |
| 195 | [animation setDuration:0.2f]; |
| 196 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]]; |
| 197 | [self.view.layer addAnimation:animation forKey:animation.keyPath]; |
| 198 | |
| 199 | [CATransaction commit]; |
| 200 | } |
| 201 | |
Alexandre Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 202 | -(void) animateOut |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 203 | { |
| 204 | if(self.view.frame.origin.x < 0) { |
| 205 | return; |
| 206 | } |
| 207 | |
| 208 | CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0); |
| 209 | [CATransaction begin]; |
| 210 | CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"]; |
| 211 | [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]]; |
| 212 | [animation setToValue:[NSValue valueWithPoint:frame.origin]]; |
| 213 | [animation setDuration:0.2f]; |
| 214 | [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]]; |
| 215 | |
| 216 | [CATransaction setCompletionBlock:^{ |
| 217 | [self.view setHidden:YES]; |
| 218 | }]; |
| 219 | [self.view.layer addAnimation:animation forKey:animation.keyPath]; |
| 220 | |
| 221 | [self.view.layer setPosition:frame.origin]; |
| 222 | [CATransaction commit]; |
| 223 | } |
| 224 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 225 | #pragma mark - NSTextFieldDelegate |
| 226 | |
| 227 | - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector |
| 228 | { |
| 229 | if (commandSelector == @selector(insertNewline:) && self.message.length > 0) { |
| 230 | [self sendMessage:nil]; |
| 231 | return YES; |
| 232 | } |
| 233 | return NO; |
| 234 | } |
| 235 | |
Kateryna Kostiuk | 4f403b6 | 2017-07-11 16:15:32 -0400 | [diff] [blame] | 236 | -(BOOL)shouldHideSendRequestBtn { |
| 237 | /*to send contact request we need to meet thre condition: |
| 238 | 1)contact method has RING protocol |
| 239 | 2)accound is used to send request is also RING |
| 240 | 3)contact have not acceppt request yet*/ |
| 241 | if(selectedContactMethod->protocolHint() != URI::ProtocolHint::RING) { |
| 242 | return YES; |
| 243 | } |
| 244 | if(selectedContactMethod->isConfirmed()) { |
| 245 | return YES; |
| 246 | } |
| 247 | if(selectedContactMethod->account()) { |
| 248 | return selectedContactMethod->account()->protocol() != Account::Protocol::RING; |
| 249 | } |
| 250 | if([self chosenAccount]) { |
| 251 | return [self chosenAccount]->protocol() != Account::Protocol::RING; |
| 252 | } |
| 253 | return NO; |
| 254 | } |
| 255 | |
| 256 | -(void)updateSendButtonVisibility |
| 257 | { |
| 258 | [sentContactRequestButton setHidden:[self shouldHideSendRequestBtn]]; |
| 259 | sentContactRequestWidth.priority = [self shouldHideSendRequestBtn] ? 999: 250; |
| 260 | } |
| 261 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 262 | #pragma mark - NSPopUpButton item selection |
| 263 | |
| 264 | - (IBAction)itemChanged:(id)sender { |
| 265 | NSInteger index = [(NSPopUpButton *)sender indexOfSelectedItem]; |
| 266 | |
Alexandre Lision | 05ca83c | 2016-11-24 19:31:08 -0500 | [diff] [blame] | 267 | selectedContactMethod = contactMethods.at(index); |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 268 | |
Kateryna Kostiuk | 4f403b6 | 2017-07-11 16:15:32 -0400 | [diff] [blame] | 269 | [self updateSendButtonVisibility]; |
Kateryna Kostiuk | 78e1f12 | 2017-06-14 14:52:34 -0400 | [diff] [blame] | 270 | |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame^] | 271 | [conversationTitle setStringValue:selectedContactMethod->bestName().toNSString()]; |
Alexandre Lision | 05ca83c | 2016-11-24 19:31:08 -0500 | [diff] [blame] | 272 | QObject::disconnect(contactMethodChanged); |
| 273 | contactMethodChanged = QObject::connect(selectedContactMethod, |
| 274 | &ContactMethod::changed, |
| 275 | [self] { |
Anthony Léonard | ade9f9b | 2017-07-26 15:51:12 -0400 | [diff] [blame^] | 276 | [conversationTitle setStringValue:selectedContactMethod->bestName().toNSString()]; |
Kateryna Kostiuk | 4f403b6 | 2017-07-11 16:15:32 -0400 | [diff] [blame] | 277 | [self updateSendButtonVisibility]; |
Alexandre Lision | 05ca83c | 2016-11-24 19:31:08 -0500 | [diff] [blame] | 278 | }); |
| 279 | |
| 280 | if (auto txtRecording = selectedContactMethod->textRecording()) { |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 281 | messagesViewVC.delegate = self; |
| 282 | [messagesViewVC setUpViewWithModel:txtRecording->instantMessagingModel()]; |
Kateryna Kostiuk | b264547 | 2017-07-05 17:44:27 -0400 | [diff] [blame] | 283 | [self.view.window makeFirstResponder:messageField]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 284 | } |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 285 | } |
| 286 | |
Kateryna Kostiuk | 58276bc | 2017-06-07 08:50:48 -0400 | [diff] [blame] | 287 | #pragma mark - MessagesVC delegate |
| 288 | |
| 289 | -(void) newMessageAdded { |
| 290 | |
| 291 | if (auto txtRecording = contactMethods.at([contactMethodsPopupButton indexOfSelectedItem])->textRecording()) { |
| 292 | [emptyConversationPlaceHolder setHidden:txtRecording->instantMessagingModel()->rowCount() > 0]; |
| 293 | txtRecording->setAllRead(); |
| 294 | } |
| 295 | } |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 296 | |
| 297 | @end |