Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 1 | /* |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2018 Savoir-faire Linux Inc. |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 3 | * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com> |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 4 | * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com> |
| 5 | * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com> |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 3 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #import "SmartViewVC.h" |
| 23 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 24 | //std |
| 25 | #import <sstream> |
| 26 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 27 | //Qt |
| 28 | #import <QtMacExtras/qmacfunctions.h> |
| 29 | #import <QPixmap> |
| 30 | #import <QIdentityProxyModel> |
| 31 | #import <QItemSelectionModel> |
| 32 | |
| 33 | //LRC |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 34 | #import <globalinstances.h> |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 35 | #import <api/newaccountmodel.h> |
| 36 | #import <api/conversationmodel.h> |
| 37 | #import <api/account.h> |
| 38 | #import <api/contact.h> |
| 39 | #import <api/contactmodel.h> |
| 40 | #import <api/newcallmodel.h> |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 41 | |
| 42 | #import "QNSTreeController.h" |
| 43 | #import "delegates/ImageManipulationDelegate.h" |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 44 | #import "views/HoverTableRowView.h" |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 45 | #import "PersonLinkerVC.h" |
Alexandre Lision | 4baba4c | 2016-02-11 13:00:57 -0500 | [diff] [blame] | 46 | #import "views/IconButton.h" |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 47 | #import "views/RingTableView.h" |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 48 | #import "views/ContextualTableCellView.h" |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 49 | #import "utils.h" |
Anthony Léonard | bee94cc | 2018-01-16 11:42:40 -0500 | [diff] [blame] | 50 | #import "RingWindowController.h" |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 51 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 52 | @interface SmartViewVC () <NSTableViewDelegate, NSTableViewDataSource, NSPopoverDelegate, ContextMenuDelegate, ContactLinkedDelegate, KeyboardShortcutDelegate> { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 53 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 54 | NSPopover* addToContactPopover; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 55 | |
| 56 | //UI elements |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 57 | __unsafe_unretained IBOutlet RingTableView* smartView; |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 58 | __unsafe_unretained IBOutlet NSSearchField* searchField; |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame] | 59 | __strong IBOutlet NSSegmentedControl *listTypeSelector; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 60 | __strong IBOutlet NSLayoutConstraint *listTypeSelectorHeight; |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame] | 61 | bool selectorIsPresent; |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 62 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 63 | QMetaObject::Connection modelSortedConnection_, modelUpdatedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_, newInteractionConnection_, interactionStatusUpdatedConnection_, conversationClearedConnection; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 64 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 65 | lrc::api::ConversationModel* convModel_; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 66 | std::string selectedUid_; |
Anthony Léonard | 4bfd68b | 2018-01-15 16:23:50 -0500 | [diff] [blame] | 67 | lrc::api::profile::Type currentFilterType; |
Anthony Léonard | bee94cc | 2018-01-16 11:42:40 -0500 | [diff] [blame] | 68 | |
| 69 | __unsafe_unretained IBOutlet RingWindowController *delegate; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @end |
| 73 | |
| 74 | @implementation SmartViewVC |
| 75 | |
Kateryna Kostiuk | 882cbac | 2017-07-05 17:29:00 -0400 | [diff] [blame] | 76 | @synthesize tabbar; |
| 77 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 78 | // Tags for views |
Alexandre Lision | 57914fa | 2016-02-10 14:52:24 -0500 | [diff] [blame] | 79 | NSInteger const IMAGE_TAG = 100; |
| 80 | NSInteger const DISPLAYNAME_TAG = 200; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 81 | NSInteger const NOTIFICATONS_TAG = 300; |
| 82 | NSInteger const RING_ID_LABEL = 400; |
| 83 | NSInteger const PRESENCE_TAG = 500; |
| 84 | NSInteger const TOTALMSGS_TAG = 600; |
| 85 | NSInteger const TOTALINVITES_TAG = 700; |
| 86 | NSInteger const DATE_TAG = 800; |
| 87 | NSInteger const SNIPPET_TAG = 900; |
| 88 | NSInteger const ADD_BUTTON_TAG = 1000; |
| 89 | NSInteger const REFUSE_BUTTON_TAG = 1100; |
| 90 | NSInteger const BLOCK_BUTTON_TAG = 1200; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 91 | |
Anthony Léonard | e6a01fa | 2018-01-12 10:29:58 -0500 | [diff] [blame] | 92 | // Segment indices for smartlist selector |
| 93 | NSInteger const CONVERSATION_SEG = 0; |
| 94 | NSInteger const REQUEST_SEG = 1; |
| 95 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 96 | - (void)awakeFromNib |
| 97 | { |
| 98 | NSLog(@"INIT SmartView VC"); |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 99 | //get selected account |
| 100 | //encapsulate conversationmodel in local version |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 101 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 102 | [smartView setTarget:self]; |
| 103 | [smartView setDoubleAction:@selector(placeCall:)]; |
| 104 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 105 | [smartView setContextMenuDelegate:self]; |
| 106 | [smartView setShortcutsDelegate:self]; |
| 107 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 108 | [smartView setDataSource: self]; |
Kateryna Kostiuk | 1f705ab | 2017-04-18 12:45:59 -0400 | [diff] [blame] | 109 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 110 | [self.view setWantsLayer:YES]; |
| 111 | [self.view setLayer:[CALayer layer]]; |
| 112 | [self.view.layer setBackgroundColor:[NSColor whiteColor].CGColor]; |
| 113 | |
| 114 | [searchField setWantsLayer:YES]; |
| 115 | [searchField setLayer:[CALayer layer]]; |
| 116 | [searchField.layer setBackgroundColor:[NSColor colorWithCalibratedRed:0.949 green:0.949 blue:0.949 alpha:0.9].CGColor]; |
Anthony Léonard | 4bfd68b | 2018-01-15 16:23:50 -0500 | [diff] [blame] | 117 | |
| 118 | currentFilterType = lrc::api::profile::Type::RING; |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame] | 119 | selectorIsPresent = true; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 120 | |
| 121 | smartView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleNone; |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 122 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | - (void)placeCall:(id)sender |
| 126 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 127 | NSInteger row; |
| 128 | if (sender != nil && [sender clickedRow] != -1) |
| 129 | row = [sender clickedRow]; |
| 130 | else if ([smartView selectedRow] != -1) |
| 131 | row = [smartView selectedRow]; |
| 132 | else |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 133 | return; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 134 | if (convModel_ == nil) |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 135 | return; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 136 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 137 | auto conv = convModel_->filteredConversation(row); |
| 138 | convModel_->placeCall(conv.uid); |
| 139 | } |
| 140 | |
| 141 | -(void) reloadSelectorNotifications |
| 142 | { |
| 143 | NSTextField* totalMsgsCount = [self.view viewWithTag:TOTALMSGS_TAG]; |
| 144 | NSTextField* totalInvites = [self.view viewWithTag:TOTALINVITES_TAG]; |
| 145 | |
| 146 | if (!selectorIsPresent) { |
| 147 | [totalMsgsCount setHidden:true]; |
| 148 | [totalInvites setHidden:true]; |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | auto ringConversations = convModel_->getFilteredConversations(lrc::api::profile::Type::RING); |
| 153 | int totalUnreadMessages = 0; |
| 154 | std::for_each(ringConversations.begin(), ringConversations.end(), |
| 155 | [&totalUnreadMessages, self] (const auto& conversation) { |
| 156 | totalUnreadMessages += convModel_->getNumberOfUnreadMessagesFor(conversation.uid); |
| 157 | }); |
| 158 | [totalMsgsCount setHidden:(totalUnreadMessages == 0)]; |
| 159 | [totalMsgsCount setIntValue:totalUnreadMessages]; |
| 160 | |
| 161 | auto totalRequests = [self chosenAccount].contactModel->pendingRequestCount(); |
| 162 | [totalInvites setHidden:(totalRequests == 0)]; |
| 163 | [totalInvites setIntValue:totalRequests]; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | -(void) reloadData |
| 167 | { |
Andreas Traczyk | 3b98eb8 | 2018-03-29 17:54:42 -0400 | [diff] [blame] | 168 | NSLog(@"reload"); |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 169 | [smartView deselectAll:nil]; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 170 | if (convModel_ == nil) |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 171 | return; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 172 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 173 | [self reloadSelectorNotifications]; |
| 174 | |
| 175 | if (!convModel_->owner.contactModel->hasPendingRequests()) { |
Anthony Léonard | 4bfd68b | 2018-01-15 16:23:50 -0500 | [diff] [blame] | 176 | if (currentFilterType == lrc::api::profile::Type::PENDING) { |
| 177 | [self selectConversationList]; |
| 178 | } |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame] | 179 | if (selectorIsPresent) { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 180 | listTypeSelectorHeight.constant = 0.0; |
| 181 | [listTypeSelector setHidden:YES]; |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame] | 182 | selectorIsPresent = false; |
| 183 | } |
Anthony Léonard | 4bfd68b | 2018-01-15 16:23:50 -0500 | [diff] [blame] | 184 | } else { |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame] | 185 | if (!selectorIsPresent) { |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame] | 186 | [listTypeSelector setSelected:YES forSegment:CONVERSATION_SEG]; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 187 | listTypeSelectorHeight.constant = 18.0; |
| 188 | [listTypeSelector setHidden:NO]; |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame] | 189 | selectorIsPresent = true; |
| 190 | } |
Anthony Léonard | 4bfd68b | 2018-01-15 16:23:50 -0500 | [diff] [blame] | 191 | } |
| 192 | |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame] | 193 | [smartView reloadData]; |
| 194 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 195 | if (!selectedUid_.empty() && convModel_ != nil) { |
| 196 | auto it = getConversationFromUid(selectedUid_, *convModel_); |
| 197 | if (it != convModel_->allFilteredConversations().end()) { |
| 198 | NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - convModel_->allFilteredConversations().begin())]; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 199 | [smartView selectRowIndexes:indexSet byExtendingSelection:NO]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 203 | [smartView scrollToBeginningOfDocument:nil]; |
| 204 | } |
| 205 | |
Andreas Traczyk | 3b98eb8 | 2018-03-29 17:54:42 -0400 | [diff] [blame] | 206 | -(void) reloadConversationWithUid:(NSString *)uid |
| 207 | { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 208 | if (convModel_ == nil) { |
| 209 | return; |
| 210 | } |
| 211 | |
| 212 | auto it = getConversationFromUid(std::string([uid UTF8String]), *convModel_); |
| 213 | if (it != convModel_->allFilteredConversations().end()) { |
| 214 | NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - convModel_->allFilteredConversations().begin())]; |
| 215 | NSLog(@"reloadConversationWithUid: %@", uid); |
| 216 | [smartView reloadDataForRowIndexes:indexSet |
| 217 | columnIndexes:[NSIndexSet indexSetWithIndex:0]]; |
Andreas Traczyk | 3b98eb8 | 2018-03-29 17:54:42 -0400 | [diff] [blame] | 218 | } |
| 219 | } |
Kateryna Kostiuk | d51a325 | 2018-04-04 09:20:37 -0400 | [diff] [blame] | 220 | |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 221 | - (BOOL)setConversationModel:(lrc::api::ConversationModel *)conversationModel |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 222 | { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 223 | if (convModel_ == conversationModel) { |
| 224 | return false; |
| 225 | } |
| 226 | |
| 227 | convModel_ = conversationModel; |
| 228 | selectedUid_.clear(); // Clear selected conversation as the selected account is being changed |
| 229 | QObject::disconnect(modelSortedConnection_); |
| 230 | QObject::disconnect(modelUpdatedConnection_); |
| 231 | QObject::disconnect(filterChangedConnection_); |
| 232 | QObject::disconnect(newConversationConnection_); |
| 233 | QObject::disconnect(conversationRemovedConnection_); |
| 234 | QObject::disconnect(conversationClearedConnection); |
| 235 | QObject::disconnect(interactionStatusUpdatedConnection_); |
| 236 | QObject::disconnect(newInteractionConnection_); |
| 237 | [self reloadData]; |
| 238 | if (convModel_ != nil) { |
| 239 | modelSortedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted, |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 240 | [self] (){ |
| 241 | [self reloadData]; |
| 242 | }); |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 243 | modelUpdatedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationUpdated, |
| 244 | [self] (const std::string& convUid){ |
| 245 | [self reloadConversationWithUid: [NSString stringWithUTF8String:convUid.c_str()]]; |
| 246 | }); |
| 247 | filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged, |
| 248 | [self] (){ |
| 249 | [self reloadData]; |
| 250 | }); |
| 251 | newConversationConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newConversation, |
| 252 | [self] (const std::string& convUid) { |
| 253 | [self reloadData]; |
| 254 | [self updateConversationForNewContact:[NSString stringWithUTF8String:convUid.c_str()]]; |
| 255 | }); |
| 256 | conversationRemovedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationRemoved, |
| 257 | [self] (){ |
| 258 | [delegate listTypeChanged]; |
| 259 | [self reloadData]; |
| 260 | }); |
| 261 | conversationClearedConnection = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationCleared, |
| 262 | [self] (const std::string& convUid){ |
| 263 | [self deselect]; |
| 264 | [delegate listTypeChanged]; |
| 265 | }); |
| 266 | interactionStatusUpdatedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::interactionStatusUpdated, |
| 267 | [self] (const std::string& convUid) { |
| 268 | if (convUid != selectedUid_) |
| 269 | return; |
| 270 | [self reloadConversationWithUid: [NSString stringWithUTF8String:convUid.c_str()]]; |
| 271 | }); |
| 272 | newInteractionConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newInteraction, |
| 273 | [self](const std::string& convUid, uint64_t interactionId, const lrc::api::interaction::Info& interaction){ |
| 274 | if (convUid == selectedUid_) { |
| 275 | convModel_->clearUnreadInteractions(convUid); |
| 276 | } |
| 277 | }); |
| 278 | convModel_->setFilter(""); // Reset the filter |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 279 | } |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 280 | [searchField setStringValue:@""]; |
| 281 | return true; |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | -(void)selectConversation:(const lrc::api::conversation::Info&)conv model:(lrc::api::ConversationModel*)model; |
| 285 | { |
| 286 | auto& uid = conv.uid; |
| 287 | if (selectedUid_ == uid) |
| 288 | return; |
| 289 | |
| 290 | [self setConversationModel:model]; |
| 291 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 292 | if (convModel_ == nil) { |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | auto it = getConversationFromUid(selectedUid_, *convModel_); |
| 297 | if (it != convModel_->allFilteredConversations().end()) { |
| 298 | NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - convModel_->allFilteredConversations().begin())]; |
| 299 | [smartView selectRowIndexes:indexSet byExtendingSelection:NO]; |
| 300 | selectedUid_ = uid; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 304 | -(void)deselect |
| 305 | { |
| 306 | selectedUid_.clear(); |
| 307 | [smartView deselectAll:nil]; |
| 308 | } |
| 309 | |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 310 | -(void) clearConversationModel { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 311 | convModel_ = nil; |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 312 | [self deselect]; |
| 313 | [smartView reloadData]; |
Andreas Traczyk | 4fdb2d9 | 2018-04-19 15:57:46 -0400 | [diff] [blame] | 314 | if (selectorIsPresent) { |
| 315 | [listTypeSelector removeFromSuperview]; |
| 316 | selectorIsPresent = false; |
| 317 | } |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 318 | } |
| 319 | |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 320 | - (IBAction) listTypeChanged:(id)sender |
| 321 | { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 322 | selectedUid_.clear(); |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 323 | NSInteger selectedItem = [sender selectedSegment]; |
Anthony Léonard | e6a01fa | 2018-01-12 10:29:58 -0500 | [diff] [blame] | 324 | switch (selectedItem) { |
| 325 | case CONVERSATION_SEG: |
Anthony Léonard | bee94cc | 2018-01-16 11:42:40 -0500 | [diff] [blame] | 326 | if (currentFilterType != lrc::api::profile::Type::RING) { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 327 | convModel_->setFilter(lrc::api::profile::Type::RING); |
Anthony Léonard | bee94cc | 2018-01-16 11:42:40 -0500 | [diff] [blame] | 328 | [delegate listTypeChanged]; |
| 329 | currentFilterType = lrc::api::profile::Type::RING; |
| 330 | } |
Anthony Léonard | e6a01fa | 2018-01-12 10:29:58 -0500 | [diff] [blame] | 331 | break; |
| 332 | case REQUEST_SEG: |
Anthony Léonard | bee94cc | 2018-01-16 11:42:40 -0500 | [diff] [blame] | 333 | if (currentFilterType != lrc::api::profile::Type::PENDING) { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 334 | convModel_->setFilter(lrc::api::profile::Type::PENDING); |
Anthony Léonard | bee94cc | 2018-01-16 11:42:40 -0500 | [diff] [blame] | 335 | [delegate listTypeChanged]; |
| 336 | currentFilterType = lrc::api::profile::Type::PENDING; |
| 337 | } |
Anthony Léonard | e6a01fa | 2018-01-12 10:29:58 -0500 | [diff] [blame] | 338 | break; |
| 339 | default: |
| 340 | NSLog(@"Invalid item selected in list selector: %d", selectedItem); |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | -(void) selectConversationList |
| 345 | { |
Anthony Léonard | d00cd18 | 2018-01-17 09:21:27 -0500 | [diff] [blame] | 346 | if (currentFilterType == lrc::api::profile::Type::RING) |
| 347 | return; |
Anthony Léonard | e6a01fa | 2018-01-12 10:29:58 -0500 | [diff] [blame] | 348 | [listTypeSelector setSelectedSegment:CONVERSATION_SEG]; |
Anthony Léonard | 4bfd68b | 2018-01-15 16:23:50 -0500 | [diff] [blame] | 349 | |
| 350 | // Do not invert order of the next two lines or stack overflow |
| 351 | // may happen on -(void) reloadData call if filter is currently set to PENDING |
| 352 | currentFilterType = lrc::api::profile::Type::RING; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 353 | convModel_->setFilter(lrc::api::profile::Type::RING); |
| 354 | convModel_->setFilter(""); |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 355 | } |
| 356 | |
Anthony Léonard | d00cd18 | 2018-01-17 09:21:27 -0500 | [diff] [blame] | 357 | -(void) selectPendingList |
| 358 | { |
| 359 | if (currentFilterType == lrc::api::profile::Type::PENDING) |
| 360 | return; |
| 361 | [listTypeSelector setSelectedSegment:REQUEST_SEG]; |
| 362 | |
| 363 | currentFilterType = lrc::api::profile::Type::PENDING; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 364 | convModel_->setFilter(lrc::api::profile::Type::PENDING); |
| 365 | convModel_->setFilter(""); |
Anthony Léonard | d00cd18 | 2018-01-17 09:21:27 -0500 | [diff] [blame] | 366 | } |
| 367 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 368 | #pragma mark - NSTableViewDelegate methods |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 369 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 370 | - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 371 | { |
| 372 | return YES; |
| 373 | } |
| 374 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 375 | - (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 376 | { |
| 377 | return NO; |
| 378 | } |
| 379 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 380 | - (void)tableViewSelectionDidChange:(NSNotification *)notification |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 381 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 382 | NSInteger row = [notification.object selectedRow]; |
Kateryna Kostiuk | 82c0d53 | 2018-05-09 16:49:06 -0400 | [diff] [blame] | 383 | NSInteger rows = [smartView numberOfRows]; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 384 | |
Kateryna Kostiuk | 82c0d53 | 2018-05-09 16:49:06 -0400 | [diff] [blame] | 385 | for (int i = 0; i< rows; i++) { |
| 386 | NSTableRowView* cellRowView = [smartView rowViewAtRow:i makeIfNecessary:YES]; |
| 387 | if (i == row) { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 388 | cellRowView.backgroundColor = [NSColor controlColor]; |
Kateryna Kostiuk | 82c0d53 | 2018-05-09 16:49:06 -0400 | [diff] [blame] | 389 | } else { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 390 | cellRowView.backgroundColor = [NSColor whiteColor]; |
| 391 | } |
Kateryna Kostiuk | 82c0d53 | 2018-05-09 16:49:06 -0400 | [diff] [blame] | 392 | } |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 393 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 394 | if (row == -1) |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 395 | return; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 396 | if (convModel_ == nil) |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 397 | return; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 398 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 399 | auto uid = convModel_->filteredConversation(row).uid; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 400 | if (selectedUid_ != uid) { |
| 401 | selectedUid_ = uid; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 402 | convModel_->selectConversation(uid); |
| 403 | convModel_->clearUnreadInteractions(uid); |
| 404 | [self reloadSelectorNotifications]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 405 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 406 | } |
| 407 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 408 | - (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 409 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 410 | return [tableView makeViewWithIdentifier:@"HoverRowView" owner:nil]; |
| 411 | } |
Alexandre Lision | a4bb051 | 2016-04-19 18:04:31 -0400 | [diff] [blame] | 412 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 413 | - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row |
| 414 | { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 415 | if (convModel_ == nil) |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 416 | return nil; |
| 417 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 418 | auto conversation = convModel_->filteredConversation(row); |
Alexandre Lision | a4bb051 | 2016-04-19 18:04:31 -0400 | [diff] [blame] | 419 | NSTableCellView* result; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 420 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 421 | result = [tableView makeViewWithIdentifier:@"MainCell" owner:tableView]; |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 422 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 423 | NSTextField* unreadCount = [result viewWithTag:NOTIFICATONS_TAG]; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 424 | [unreadCount setHidden:(conversation.unreadMessages == 0)]; |
| 425 | [unreadCount setIntValue:conversation.unreadMessages]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 426 | NSTextField* displayName = [result viewWithTag:DISPLAYNAME_TAG]; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 427 | NSTextField* displayRingID = [result viewWithTag:RING_ID_LABEL]; |
Kateryna Kostiuk | 0bf0375 | 2018-05-09 13:57:15 -0400 | [diff] [blame] | 428 | NSTextField* lastInteractionDate = [result viewWithTag:DATE_TAG]; |
| 429 | NSTextField* interactionSnippet = [result viewWithTag:SNIPPET_TAG]; |
| 430 | [displayName setStringValue:@""]; |
| 431 | [displayRingID setStringValue:@""]; |
| 432 | [lastInteractionDate setStringValue:@""]; |
| 433 | [interactionSnippet setStringValue:@""]; |
| 434 | NSImageView* photoView = [result viewWithTag:IMAGE_TAG]; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 435 | NSString* displayNameString = bestNameForConversation(conversation, *convModel_); |
| 436 | NSString* displayIDString = bestIDForConversation(conversation, *convModel_); |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 437 | if(displayNameString.length == 0 || [displayNameString isEqualToString:displayIDString]) { |
| 438 | [displayName setStringValue:displayIDString]; |
Anthony Léonard | 4adc3f0 | 2017-07-21 10:23:04 -0400 | [diff] [blame] | 439 | [displayRingID setHidden:YES]; |
Kateryna Kostiuk | 9d6657a | 2017-04-25 13:42:25 -0400 | [diff] [blame] | 440 | } |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 441 | else { |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 442 | [displayName setStringValue:displayNameString]; |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 443 | [displayRingID setStringValue:displayIDString]; |
Anthony Léonard | 4adc3f0 | 2017-07-21 10:23:04 -0400 | [diff] [blame] | 444 | [displayRingID setHidden:NO]; |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 445 | } |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 446 | auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator()); |
Kateryna Kostiuk | 0bf0375 | 2018-05-09 13:57:15 -0400 | [diff] [blame] | 447 | NSImage* image = QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(conversation, convModel_->owner))); |
| 448 | if(image) { |
| 449 | [NSLayoutConstraint deactivateConstraints:[photoView constraints]]; |
| 450 | NSArray* constraints = [NSLayoutConstraint |
| 451 | constraintsWithVisualFormat:@"H:[photoView(54)]" |
| 452 | options:0 |
| 453 | metrics:nil views:NSDictionaryOfVariableBindings(photoView)]; |
| 454 | [NSLayoutConstraint activateConstraints:constraints]; |
| 455 | } else { |
| 456 | [NSLayoutConstraint deactivateConstraints:[photoView constraints]]; |
| 457 | NSArray* constraints = [NSLayoutConstraint |
| 458 | constraintsWithVisualFormat:@"H:[photoView(0)]" |
| 459 | options:0 |
| 460 | metrics:nil views:NSDictionaryOfVariableBindings(photoView)]; |
| 461 | [NSLayoutConstraint activateConstraints:constraints]; |
| 462 | } |
| 463 | [photoView setImage: image]; |
Kateryna Kostiuk | d9039e9 | 2017-05-24 14:29:54 -0400 | [diff] [blame] | 464 | |
| 465 | NSView* presenceView = [result viewWithTag:PRESENCE_TAG]; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 466 | auto contact = convModel_->owner.contactModel->getContact(conversation.participants[0]); |
| 467 | if (contact.isPresent) { |
Kateryna Kostiuk | d9039e9 | 2017-05-24 14:29:54 -0400 | [diff] [blame] | 468 | [presenceView setHidden:NO]; |
| 469 | } else { |
| 470 | [presenceView setHidden:YES]; |
| 471 | } |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 472 | NSButton* addContactButton = [result viewWithTag:ADD_BUTTON_TAG]; |
| 473 | NSButton* refuseContactButton = [result viewWithTag:REFUSE_BUTTON_TAG]; |
| 474 | NSButton* blockContactButton = [result viewWithTag:BLOCK_BUTTON_TAG]; |
| 475 | [addContactButton setHidden:YES]; |
| 476 | [refuseContactButton setHidden:YES]; |
| 477 | [blockContactButton setHidden:YES]; |
| 478 | |
| 479 | if (profileType(conversation, *convModel_) == lrc::api::profile::Type::PENDING) { |
| 480 | [lastInteractionDate setHidden:true]; |
| 481 | [interactionSnippet setHidden:true]; |
| 482 | [addContactButton setHidden:NO]; |
| 483 | [refuseContactButton setHidden:NO]; |
| 484 | [blockContactButton setHidden:NO]; |
| 485 | [addContactButton setAction:@selector(acceptInvitation:)]; |
| 486 | [addContactButton setTarget:self]; |
| 487 | [refuseContactButton setAction:@selector(refuseInvitation:)]; |
| 488 | [refuseContactButton setTarget:self]; |
| 489 | [blockContactButton setAction:@selector(blockPendingContact:)]; |
| 490 | [blockContactButton setTarget:self]; |
| 491 | return result; |
| 492 | } |
| 493 | |
| 494 | [lastInteractionDate setHidden:false]; |
| 495 | |
| 496 | [interactionSnippet setHidden:false]; |
| 497 | |
| 498 | auto lastUid = conversation.lastMessageUid; |
| 499 | if (conversation.interactions.find(lastUid) != conversation.interactions.end()) { |
| 500 | // last interaction snippet |
| 501 | std::string lastInteractionSnippet = conversation.interactions[lastUid].body; |
| 502 | std::stringstream ss(lastInteractionSnippet); |
| 503 | std::getline(ss, lastInteractionSnippet); |
| 504 | NSString* lastInteractionSnippetFixedString = [[NSString stringWithUTF8String:lastInteractionSnippet.c_str()] |
| 505 | stringByReplacingOccurrencesOfString:@"🕽" withString:@""]; |
| 506 | lastInteractionSnippetFixedString = [lastInteractionSnippetFixedString stringByReplacingOccurrencesOfString:@"📞" withString:@""]; |
| 507 | if (conversation.interactions[lastUid].type == lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER |
| 508 | || conversation.interactions[lastUid].type == lrc::api::interaction::Type::INCOMING_DATA_TRANSFER) { |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 509 | lastInteractionSnippetFixedString = [lastInteractionSnippetFixedString lastPathComponent]; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 510 | } |
| 511 | [interactionSnippet setStringValue:lastInteractionSnippetFixedString]; |
| 512 | |
| 513 | // last interaction date/time |
| 514 | std::time_t lastInteractionTimestamp = conversation.interactions[lastUid].timestamp; |
| 515 | std::time_t now = std::time(nullptr); |
| 516 | char interactionDay[64]; |
| 517 | char nowDay[64]; |
| 518 | std::strftime(interactionDay, sizeof(interactionDay), "%D", std::localtime(&lastInteractionTimestamp)); |
| 519 | std::strftime(nowDay, sizeof(nowDay), "%D", std::localtime(&now)); |
| 520 | if (std::string(interactionDay) == std::string(nowDay)) { |
| 521 | char interactionTime[64]; |
| 522 | std::strftime(interactionTime, sizeof(interactionTime), "%R", std::localtime(&lastInteractionTimestamp)); |
| 523 | [lastInteractionDate setStringValue:[NSString stringWithUTF8String:interactionTime]]; |
| 524 | } else { |
| 525 | [lastInteractionDate setStringValue:[NSString stringWithUTF8String:interactionDay]]; |
| 526 | } |
| 527 | } |
| 528 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 529 | return result; |
| 530 | } |
| 531 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 532 | - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 533 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 534 | return 60.0; |
| 535 | } |
| 536 | |
| 537 | #pragma mark - NSTableDataSource methods |
| 538 | |
| 539 | - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView |
| 540 | { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 541 | if (tableView == smartView && convModel_ != nullptr) { |
| 542 | return convModel_->allFilteredConversations().size(); |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | return 0; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 546 | } |
| 547 | |
Kateryna Kostiuk | 882cbac | 2017-07-05 17:29:00 -0400 | [diff] [blame] | 548 | - (void)startCallForRow:(id)sender { |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 549 | NSInteger row = [smartView rowForView:sender]; |
| 550 | [smartView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO]; |
| 551 | [self placeCall:nil]; |
| 552 | } |
| 553 | |
| 554 | - (IBAction)hangUpClickedAtRow:(id)sender { |
| 555 | NSInteger row = [smartView rowForView:sender]; |
Alexandre Lision | 57914fa | 2016-02-10 14:52:24 -0500 | [diff] [blame] | 556 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 557 | if (row == -1) |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 558 | return; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 559 | if (convModel_ == nil) |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 560 | return; |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 561 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 562 | auto conv = convModel_->filteredConversation(row); |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 563 | auto& callId = conv.callId; |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 564 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 565 | if (callId.empty()) |
| 566 | return; |
| 567 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 568 | auto* callModel = convModel_->owner.callModel.get(); |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 569 | callModel->hangUp(callId); |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 570 | } |
| 571 | |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 572 | - (void) displayErrorModalWithTitle:(NSString*) title WithMessage:(NSString*) message |
| 573 | { |
| 574 | NSAlert* alert = [NSAlert alertWithMessageText:title |
| 575 | defaultButton:@"Ok" |
| 576 | alternateButton:nil |
| 577 | otherButton:nil |
| 578 | informativeTextWithFormat:message]; |
| 579 | |
| 580 | [alert beginSheetModalForWindow:self.view.window modalDelegate:nil didEndSelector:NULL contextInfo:NULL]; |
| 581 | } |
| 582 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 583 | - (void) processSearchFieldInput |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 584 | { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 585 | if (convModel_ == nil) { |
| 586 | return; |
| 587 | } |
| 588 | |
| 589 | convModel_->setFilter(std::string([[searchField stringValue] UTF8String])); |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 590 | } |
| 591 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 592 | -(const lrc::api::account::Info&) chosenAccount |
Kateryna Kostiuk | d2f7b5d | 2017-06-09 15:30:49 -0400 | [diff] [blame] | 593 | { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 594 | return convModel_->owner; |
Kateryna Kostiuk | d2f7b5d | 2017-06-09 15:30:49 -0400 | [diff] [blame] | 595 | } |
| 596 | |
Alexandre Lision | f909fb1 | 2016-11-03 12:35:36 -0400 | [diff] [blame] | 597 | - (void) clearSearchField |
| 598 | { |
| 599 | [searchField setStringValue:@""]; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 600 | [self processSearchFieldInput]; |
Kateryna Kostiuk | 9dd759c | 2017-07-11 12:06:55 -0400 | [diff] [blame] | 601 | } |
| 602 | |
Kateryna Kostiuk | 22ce936 | 2018-04-17 13:12:50 -0400 | [diff] [blame] | 603 | -(void)updateConversationForNewContact:(NSString *)uId { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 604 | if (convModel_ == nil) { |
Kateryna Kostiuk | 22ce936 | 2018-04-17 13:12:50 -0400 | [diff] [blame] | 605 | return; |
| 606 | } |
| 607 | [self clearSearchField]; |
| 608 | auto uid = std::string([uId UTF8String]); |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 609 | auto it = getConversationFromUid(uid, *convModel_); |
| 610 | if (it != convModel_->allFilteredConversations().end()) { |
Kateryna Kostiuk | 22ce936 | 2018-04-17 13:12:50 -0400 | [diff] [blame] | 611 | @try { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 612 | auto contact = convModel_->owner.contactModel->getContact(it->participants[0]); |
Kateryna Kostiuk | 22ce936 | 2018-04-17 13:12:50 -0400 | [diff] [blame] | 613 | if (!contact.profileInfo.uri.empty() && contact.profileInfo.uri.compare(selectedUid_) == 0) { |
| 614 | selectedUid_ = uid; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 615 | convModel_->selectConversation(uid); |
Kateryna Kostiuk | 22ce936 | 2018-04-17 13:12:50 -0400 | [diff] [blame] | 616 | } |
| 617 | } @catch (NSException *exception) { |
| 618 | return; |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 623 | /** |
| 624 | Copy a NSString in the general Pasteboard |
| 625 | |
| 626 | @param sender the NSObject containing the represented object to copy |
| 627 | */ |
| 628 | - (void) copyStringToPasteboard:(id) sender |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 629 | { |
| 630 | NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard]; |
| 631 | [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; |
| 632 | [pasteBoard setString:[sender representedObject] forType:NSStringPboardType]; |
| 633 | } |
| 634 | |
Alexandre Lision | bf0385e | 2015-10-22 17:36:28 -0400 | [diff] [blame] | 635 | #pragma NSTextFieldDelegate |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 636 | |
| 637 | - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector |
| 638 | { |
Kateryna Kostiuk | 0febcfa | 2018-04-04 11:14:51 -0400 | [diff] [blame] | 639 | if (commandSelector != @selector(insertNewline:) || [[searchField stringValue] isEqual:@""]) { |
| 640 | return NO; |
| 641 | } |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 642 | if (convModel_ == nil) { |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 643 | [self displayErrorModalWithTitle:NSLocalizedString(@"No account available", @"Displayed as RingID when no accounts are available for selection") WithMessage:NSLocalizedString(@"Navigate to preferences to create a new account", @"Allert message when no accounts are available")]; |
| 644 | return NO; |
| 645 | } |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 646 | if (convModel_->allFilteredConversations().size() <= 0) { |
Kateryna Kostiuk | 0febcfa | 2018-04-04 11:14:51 -0400 | [diff] [blame] | 647 | return YES; |
| 648 | } |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 649 | auto model = convModel_->filteredConversation(0); |
Kateryna Kostiuk | 0febcfa | 2018-04-04 11:14:51 -0400 | [diff] [blame] | 650 | auto uid = model.uid; |
| 651 | if (selectedUid_ == uid) { |
| 652 | return YES; |
| 653 | } |
| 654 | @try { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 655 | auto contact = convModel_->owner.contactModel->getContact(model.participants[0]); |
Kateryna Kostiuk | 0febcfa | 2018-04-04 11:14:51 -0400 | [diff] [blame] | 656 | if ((contact.profileInfo.uri.empty() && contact.profileInfo.type != lrc::api::profile::Type::SIP) || contact.profileInfo.type == lrc::api::profile::Type::INVALID) { |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 657 | return YES; |
| 658 | } |
Kateryna Kostiuk | 0febcfa | 2018-04-04 11:14:51 -0400 | [diff] [blame] | 659 | selectedUid_ = uid; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 660 | convModel_->selectConversation(uid); |
Kateryna Kostiuk | 0febcfa | 2018-04-04 11:14:51 -0400 | [diff] [blame] | 661 | [self.view.window makeFirstResponder: smartView]; |
Kateryna Kostiuk | 0febcfa | 2018-04-04 11:14:51 -0400 | [diff] [blame] | 662 | return YES; |
| 663 | } @catch (NSException *exception) { |
| 664 | return YES; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 665 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 666 | } |
| 667 | |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 668 | - (void)controlTextDidChange:(NSNotification *) notification |
| 669 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 670 | [self processSearchFieldInput]; |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 671 | } |
| 672 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 673 | #pragma mark - NSPopOverDelegate |
| 674 | |
| 675 | - (void)popoverDidClose:(NSNotification *)notification |
| 676 | { |
| 677 | if (addToContactPopover != nullptr) { |
| 678 | [addToContactPopover performClose:self]; |
| 679 | addToContactPopover = NULL; |
| 680 | } |
| 681 | } |
| 682 | |
Alexandre Lision | bf0385e | 2015-10-22 17:36:28 -0400 | [diff] [blame] | 683 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 684 | #pragma mark - ContactLinkedDelegate |
| 685 | |
| 686 | - (void)contactLinked |
| 687 | { |
| 688 | if (addToContactPopover != nullptr) { |
| 689 | [addToContactPopover performClose:self]; |
| 690 | addToContactPopover = NULL; |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | #pragma mark - KeyboardShortcutDelegate |
| 695 | |
| 696 | - (void) onAddShortcut |
| 697 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 698 | if ([smartView selectedRow] == -1) |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 699 | return; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 700 | if (convModel_ == nil) |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 701 | return ; |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 702 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 703 | auto uid = convModel_->filteredConversation([smartView selectedRow]).uid; |
| 704 | convModel_->makePermanent(uid); |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | #pragma mark - ContextMenuDelegate |
| 708 | |
Kateryna Kostiuk | d51a325 | 2018-04-04 09:20:37 -0400 | [diff] [blame] | 709 | - (NSMenu*) contextualMenuForRow:(int) index |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 710 | { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 711 | if (convModel_ == nil) |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 712 | return nil; |
| 713 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 714 | auto conversation = convModel_->filteredConversation(NSInteger(index)); |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 715 | |
Kateryna Kostiuk | d51a325 | 2018-04-04 09:20:37 -0400 | [diff] [blame] | 716 | @try { |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 717 | auto contact = convModel_->owner.contactModel->getContact(conversation.participants[0]); |
Kateryna Kostiuk | d51a325 | 2018-04-04 09:20:37 -0400 | [diff] [blame] | 718 | if (contact.profileInfo.type == lrc::api::profile::Type::INVALID) { |
| 719 | return nil; |
| 720 | } |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 721 | |
Kateryna Kostiuk | d51a325 | 2018-04-04 09:20:37 -0400 | [diff] [blame] | 722 | BOOL isSIP = false; |
| 723 | BOOL isRingContact = false; |
| 724 | /* for SIP contact show only call menu options |
| 725 | * if contact does not have uri that is not RING contact |
| 726 | * for trusted Ring contact show option block contact |
| 727 | * for untrasted contact show option add contact |
| 728 | */ |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 729 | |
Kateryna Kostiuk | d51a325 | 2018-04-04 09:20:37 -0400 | [diff] [blame] | 730 | if (contact.profileInfo.type == lrc::api::profile::Type::SIP) { |
| 731 | isSIP = true; |
| 732 | } else if (contact.profileInfo.uri.empty()) { |
| 733 | return nil; |
| 734 | } |
| 735 | |
| 736 | else if (contact.profileInfo.type == lrc::api::profile::Type::RING && contact.isTrusted == true) { |
| 737 | isRingContact = true; |
| 738 | } |
| 739 | auto conversationUD = conversation.uid; |
| 740 | NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@""]; |
| 741 | NSString* conversationUID = @(conversationUD.c_str()); |
| 742 | NSMenuItem* separator = [NSMenuItem separatorItem]; |
| 743 | NSMenuItem* videoCallItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Place video call", |
| 744 | @"Contextual menu action") |
| 745 | action:@selector(videoCall:) |
| 746 | keyEquivalent:@""]; |
| 747 | NSMenuItem* audioCallItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Place audio call", |
| 748 | @"Contextual menu action") |
| 749 | action:@selector(audioCall:) |
| 750 | keyEquivalent:@""]; |
| 751 | [videoCallItem setRepresentedObject: conversationUID]; |
| 752 | [audioCallItem setRepresentedObject: conversationUID]; |
| 753 | [theMenu addItem:videoCallItem]; |
| 754 | [theMenu addItem:audioCallItem]; |
| 755 | if (isSIP == false) { |
| 756 | [theMenu addItem:separator]; |
| 757 | NSMenuItem* clearConversationItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Clear conversation", @"Contextual menu action") |
| 758 | action:@selector(clearConversation:) |
| 759 | keyEquivalent:@""]; |
| 760 | [clearConversationItem setRepresentedObject: conversationUID]; |
| 761 | [theMenu addItem:clearConversationItem]; |
| 762 | if(isRingContact) { |
| 763 | NSMenuItem* blockContactItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Block contact", @"Contextual menu action") |
| 764 | action:@selector(blockContact:) |
| 765 | keyEquivalent:@""]; |
| 766 | [blockContactItem setRepresentedObject: conversationUID]; |
| 767 | [theMenu addItem:blockContactItem]; |
| 768 | } else { |
| 769 | NSMenuItem* addContactItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Add to contacts", @"Contextual menu action") |
| 770 | action:@selector(addContact:) |
| 771 | keyEquivalent:@"A"]; |
| 772 | [addContactItem setRepresentedObject: conversationUID]; |
| 773 | [theMenu addItem:addContactItem]; |
| 774 | } |
| 775 | } |
| 776 | return theMenu; |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 777 | } |
Kateryna Kostiuk | d51a325 | 2018-04-04 09:20:37 -0400 | [diff] [blame] | 778 | @catch (NSException *exception) { |
| 779 | return nil; |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 780 | } |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 781 | } |
Kateryna Kostiuk | d51a325 | 2018-04-04 09:20:37 -0400 | [diff] [blame] | 782 | |
| 783 | - (void) addContact: (NSMenuItem* ) item { |
| 784 | auto menuObject = item.representedObject; |
| 785 | if(menuObject == nil) { |
| 786 | return; |
| 787 | } |
| 788 | NSString * convUId = (NSString*)menuObject; |
| 789 | std::string conversationID = std::string([convUId UTF8String]); |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 790 | convModel_->makePermanent(conversationID); |
Kateryna Kostiuk | d51a325 | 2018-04-04 09:20:37 -0400 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | - (void) blockContact: (NSMenuItem* ) item { |
| 794 | auto menuObject = item.representedObject; |
| 795 | if(menuObject == nil) { |
| 796 | return; |
| 797 | } |
| 798 | NSString * convUId = (NSString*)menuObject; |
| 799 | std::string conversationID = std::string([convUId UTF8String]); |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 800 | //convModel_->clearHistory(conversationID); |
| 801 | convModel_->removeConversation(conversationID, true); |
Kateryna Kostiuk | d51a325 | 2018-04-04 09:20:37 -0400 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | - (void) audioCall: (NSMenuItem* ) item { |
| 805 | auto menuObject = item.representedObject; |
| 806 | if(menuObject == nil) { |
| 807 | return; |
| 808 | } |
| 809 | NSString * convUId = (NSString*)menuObject; |
| 810 | std::string conversationID = std::string([convUId UTF8String]); |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 811 | convModel_->placeAudioOnlyCall(conversationID); |
Kateryna Kostiuk | d51a325 | 2018-04-04 09:20:37 -0400 | [diff] [blame] | 812 | |
| 813 | } |
| 814 | |
| 815 | - (void) videoCall: (NSMenuItem* ) item { |
| 816 | auto menuObject = item.representedObject; |
| 817 | if(menuObject == nil) { |
| 818 | return; |
| 819 | } |
| 820 | NSString * convUId = (NSString*)menuObject; |
| 821 | std::string conversationID = std::string([convUId UTF8String]); |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 822 | convModel_->placeCall(conversationID); |
Kateryna Kostiuk | d51a325 | 2018-04-04 09:20:37 -0400 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | - (void) clearConversation:(NSMenuItem* ) item { |
| 826 | auto menuObject = item.representedObject; |
| 827 | if(menuObject == nil) { |
| 828 | return; |
| 829 | } |
| 830 | NSString * convUId = (NSString*)menuObject; |
| 831 | std::string conversationID = std::string([convUId UTF8String]); |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 832 | convModel_->clearHistory(conversationID); |
| 833 | } |
| 834 | |
| 835 | - (void)acceptInvitation:(id)sender { |
| 836 | NSInteger row = [smartView rowForView:sender]; |
| 837 | |
| 838 | if (row == -1) |
| 839 | return; |
| 840 | if (convModel_ == nil) |
| 841 | return; |
| 842 | |
| 843 | auto conv = convModel_->filteredConversation(row); |
| 844 | auto& convID = conv.Info::uid; |
| 845 | |
| 846 | if (convID.empty()) |
| 847 | return; |
| 848 | convModel_->makePermanent(convID); |
| 849 | } |
| 850 | |
| 851 | - (void)refuseInvitation:(id)sender { |
| 852 | NSInteger row = [smartView rowForView:sender]; |
| 853 | |
| 854 | if (row == -1) |
| 855 | return; |
| 856 | if (convModel_ == nil) |
| 857 | return; |
| 858 | |
| 859 | auto conv = convModel_->filteredConversation(row); |
| 860 | auto& convID = conv.Info::uid; |
| 861 | |
| 862 | if (convID.empty()) |
| 863 | return; |
| 864 | convModel_->removeConversation(convID); |
| 865 | } |
| 866 | |
| 867 | - (void)blockPendingContact:(id)sender { |
| 868 | NSInteger row = [smartView rowForView:sender]; |
| 869 | |
| 870 | if (row == -1) |
| 871 | return; |
| 872 | if (convModel_ == nil) |
| 873 | return; |
| 874 | |
| 875 | auto conv = convModel_->filteredConversation(row); |
| 876 | auto& convID = conv.Info::uid; |
| 877 | |
| 878 | if (convID.empty()) |
| 879 | return; |
| 880 | convModel_->removeConversation(convID, true); |
| 881 | [self deselect]; |
| 882 | [delegate listTypeChanged]; |
Kateryna Kostiuk | d51a325 | 2018-04-04 09:20:37 -0400 | [diff] [blame] | 883 | } |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 884 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 885 | @end |