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 | |
| 24 | //Qt |
| 25 | #import <QtMacExtras/qmacfunctions.h> |
| 26 | #import <QPixmap> |
| 27 | #import <QIdentityProxyModel> |
| 28 | #import <QItemSelectionModel> |
| 29 | |
| 30 | //LRC |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 31 | #import <globalinstances.h> |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 32 | #import <api/newaccountmodel.h> |
| 33 | #import <api/conversationmodel.h> |
| 34 | #import <api/account.h> |
| 35 | #import <api/contact.h> |
| 36 | #import <api/contactmodel.h> |
| 37 | #import <api/newcallmodel.h> |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 38 | |
| 39 | #import "QNSTreeController.h" |
| 40 | #import "delegates/ImageManipulationDelegate.h" |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 41 | #import "views/HoverTableRowView.h" |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 42 | #import "PersonLinkerVC.h" |
Alexandre Lision | 4baba4c | 2016-02-11 13:00:57 -0500 | [diff] [blame] | 43 | #import "views/IconButton.h" |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 44 | #import "views/RingTableView.h" |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 45 | #import "views/ContextualTableCellView.h" |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 46 | #import "utils.h" |
Anthony Léonard | bee94cc | 2018-01-16 11:42:40 -0500 | [diff] [blame] | 47 | #import "RingWindowController.h" |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 48 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 49 | @interface SmartViewVC () <NSTableViewDelegate, NSTableViewDataSource, NSPopoverDelegate, ContextMenuDelegate, ContactLinkedDelegate, KeyboardShortcutDelegate> { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 50 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 51 | NSPopover* addToContactPopover; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 52 | |
| 53 | //UI elements |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 54 | __unsafe_unretained IBOutlet RingTableView* smartView; |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 55 | __unsafe_unretained IBOutlet NSSearchField* searchField; |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame^] | 56 | __strong IBOutlet NSSegmentedControl *listTypeSelector; |
| 57 | bool selectorIsPresent; |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 58 | |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 59 | QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 60 | |
| 61 | lrc::api::ConversationModel* model_; |
| 62 | std::string selectedUid_; |
Anthony Léonard | 4bfd68b | 2018-01-15 16:23:50 -0500 | [diff] [blame] | 63 | lrc::api::profile::Type currentFilterType; |
Anthony Léonard | bee94cc | 2018-01-16 11:42:40 -0500 | [diff] [blame] | 64 | |
| 65 | __unsafe_unretained IBOutlet RingWindowController *delegate; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @end |
| 69 | |
| 70 | @implementation SmartViewVC |
| 71 | |
Kateryna Kostiuk | 882cbac | 2017-07-05 17:29:00 -0400 | [diff] [blame] | 72 | @synthesize tabbar; |
| 73 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 74 | // Tags for views |
Alexandre Lision | 57914fa | 2016-02-10 14:52:24 -0500 | [diff] [blame] | 75 | NSInteger const IMAGE_TAG = 100; |
| 76 | NSInteger const DISPLAYNAME_TAG = 200; |
| 77 | NSInteger const DETAILS_TAG = 300; |
| 78 | NSInteger const CALL_BUTTON_TAG = 400; |
| 79 | NSInteger const TXT_BUTTON_TAG = 500; |
| 80 | NSInteger const CANCEL_BUTTON_TAG = 600; |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 81 | NSInteger const RING_ID_LABEL = 700; |
Kateryna Kostiuk | d9039e9 | 2017-05-24 14:29:54 -0400 | [diff] [blame] | 82 | NSInteger const PRESENCE_TAG = 800; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 83 | |
Anthony Léonard | e6a01fa | 2018-01-12 10:29:58 -0500 | [diff] [blame] | 84 | // Segment indices for smartlist selector |
| 85 | NSInteger const CONVERSATION_SEG = 0; |
| 86 | NSInteger const REQUEST_SEG = 1; |
| 87 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 88 | - (void)awakeFromNib |
| 89 | { |
| 90 | NSLog(@"INIT SmartView VC"); |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 91 | //get selected account |
| 92 | //encapsulate conversationmodel in local version |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 93 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 94 | [smartView setTarget:self]; |
| 95 | [smartView setDoubleAction:@selector(placeCall:)]; |
| 96 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 97 | [smartView setContextMenuDelegate:self]; |
| 98 | [smartView setShortcutsDelegate:self]; |
| 99 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 100 | [smartView setDataSource: self]; |
Kateryna Kostiuk | 1f705ab | 2017-04-18 12:45:59 -0400 | [diff] [blame] | 101 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 102 | [self.view setWantsLayer:YES]; |
| 103 | [self.view setLayer:[CALayer layer]]; |
| 104 | [self.view.layer setBackgroundColor:[NSColor whiteColor].CGColor]; |
| 105 | |
| 106 | [searchField setWantsLayer:YES]; |
| 107 | [searchField setLayer:[CALayer layer]]; |
| 108 | [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] | 109 | |
| 110 | currentFilterType = lrc::api::profile::Type::RING; |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame^] | 111 | selectorIsPresent = true; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | - (void)placeCall:(id)sender |
| 115 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 116 | NSInteger row; |
| 117 | if (sender != nil && [sender clickedRow] != -1) |
| 118 | row = [sender clickedRow]; |
| 119 | else if ([smartView selectedRow] != -1) |
| 120 | row = [smartView selectedRow]; |
| 121 | else |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 122 | return; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 123 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 124 | auto conv = model_->filteredConversation(row); |
| 125 | model_->placeCall(conv.uid); |
| 126 | } |
| 127 | |
| 128 | -(void) reloadData |
| 129 | { |
| 130 | [smartView deselectAll:nil]; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 131 | |
Anthony Léonard | 4bfd68b | 2018-01-15 16:23:50 -0500 | [diff] [blame] | 132 | if (!model_->owner.contactModel->hasPendingRequests()) { |
| 133 | if (currentFilterType == lrc::api::profile::Type::PENDING) { |
| 134 | [self selectConversationList]; |
| 135 | } |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame^] | 136 | if (selectorIsPresent) { |
| 137 | [listTypeSelector removeFromSuperview]; |
| 138 | selectorIsPresent = false; |
| 139 | } |
Anthony Léonard | 4bfd68b | 2018-01-15 16:23:50 -0500 | [diff] [blame] | 140 | } else { |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame^] | 141 | if (!selectorIsPresent) { |
| 142 | // First we restore the selector with selection on "Conversations" |
| 143 | [self.view addSubview:listTypeSelector]; |
| 144 | [listTypeSelector setSelected:YES forSegment:CONVERSATION_SEG]; |
| 145 | |
| 146 | // Then constraints are recreated (as these are lost when calling removeFromSuperview) |
| 147 | [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[searchField]-8-[listTypeSelector]" |
| 148 | options:0 |
| 149 | metrics:nil |
| 150 | views:NSDictionaryOfVariableBindings(searchField, listTypeSelector)]]; |
| 151 | [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[listTypeSelector]-8-[tabbar]" |
| 152 | options:0 |
| 153 | metrics:nil |
| 154 | views:NSDictionaryOfVariableBindings(listTypeSelector, tabbar)]]; |
| 155 | [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[listTypeSelector]-20-|" |
| 156 | options:0 |
| 157 | metrics:nil |
| 158 | views:NSDictionaryOfVariableBindings(listTypeSelector)]]; |
| 159 | selectorIsPresent = true; |
| 160 | } |
Anthony Léonard | 4bfd68b | 2018-01-15 16:23:50 -0500 | [diff] [blame] | 161 | } |
| 162 | |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame^] | 163 | [smartView reloadData]; |
| 164 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 165 | if (!selectedUid_.empty() && model_ != nil) { |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 166 | auto it = getConversationFromUid(selectedUid_, *model_); |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 167 | if (it != model_->allFilteredConversations().end()) { |
| 168 | NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - model_->allFilteredConversations().begin())]; |
| 169 | [smartView selectRowIndexes:indexSet byExtendingSelection:NO]; |
| 170 | } else { |
| 171 | selectedUid_.clear(); |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 175 | [smartView scrollToBeginningOfDocument:nil]; |
| 176 | } |
| 177 | |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 178 | - (BOOL)setConversationModel:(lrc::api::ConversationModel *)conversationModel |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 179 | { |
| 180 | if (model_ != conversationModel) { |
| 181 | model_ = conversationModel; |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 182 | selectedUid_.clear(); // Clear selected conversation as the selected account is being changed |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 183 | QObject::disconnect(modelSortedConnection_); |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 184 | QObject::disconnect(filterChangedConnection_); |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 185 | QObject::disconnect(newConversationConnection_); |
| 186 | QObject::disconnect(conversationRemovedConnection_); |
Anthony Léonard | cb73a2c | 2018-01-18 11:45:56 -0500 | [diff] [blame^] | 187 | [self reloadData]; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 188 | if (model_ != nil) { |
| 189 | modelSortedConnection_ = QObject::connect(model_, &lrc::api::ConversationModel::modelSorted, |
| 190 | [self] (){ |
| 191 | [self reloadData]; |
| 192 | }); |
| 193 | filterChangedConnection_ = QObject::connect(model_, &lrc::api::ConversationModel::filterChanged, |
| 194 | [self] (){ |
| 195 | [self reloadData]; |
| 196 | }); |
Anthony Léonard | 0163410 | 2017-12-27 16:37:16 -0500 | [diff] [blame] | 197 | newConversationConnection_ = QObject::connect(model_, &lrc::api::ConversationModel::newConversation, |
| 198 | [self] (){ |
| 199 | [self reloadData]; |
| 200 | }); |
| 201 | conversationRemovedConnection_ = QObject::connect(model_, &lrc::api::ConversationModel::conversationRemoved, |
| 202 | [self] (){ |
| 203 | [self reloadData]; |
| 204 | }); |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 205 | model_->setFilter(""); // Reset the filter |
| 206 | } |
| 207 | [searchField setStringValue:@""]; |
| 208 | return YES; |
| 209 | } |
| 210 | return NO; |
| 211 | } |
| 212 | |
| 213 | -(void)selectConversation:(const lrc::api::conversation::Info&)conv model:(lrc::api::ConversationModel*)model; |
| 214 | { |
| 215 | auto& uid = conv.uid; |
| 216 | if (selectedUid_ == uid) |
| 217 | return; |
| 218 | |
| 219 | [self setConversationModel:model]; |
| 220 | |
| 221 | if (model_ != nil) { |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 222 | auto it = getConversationFromUid(selectedUid_, *model_); |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 223 | if (it != model_->allFilteredConversations().end()) { |
| 224 | NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - model_->allFilteredConversations().begin())]; |
| 225 | [smartView selectRowIndexes:indexSet byExtendingSelection:NO]; |
| 226 | selectedUid_ = uid; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 227 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 231 | -(void)deselect |
| 232 | { |
| 233 | selectedUid_.clear(); |
| 234 | [smartView deselectAll:nil]; |
| 235 | } |
| 236 | |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 237 | - (IBAction) listTypeChanged:(id)sender |
| 238 | { |
| 239 | NSInteger selectedItem = [sender selectedSegment]; |
Anthony Léonard | e6a01fa | 2018-01-12 10:29:58 -0500 | [diff] [blame] | 240 | switch (selectedItem) { |
| 241 | case CONVERSATION_SEG: |
Anthony Léonard | bee94cc | 2018-01-16 11:42:40 -0500 | [diff] [blame] | 242 | if (currentFilterType != lrc::api::profile::Type::RING) { |
| 243 | model_->setFilter(lrc::api::profile::Type::RING); |
| 244 | [delegate listTypeChanged]; |
| 245 | currentFilterType = lrc::api::profile::Type::RING; |
| 246 | } |
Anthony Léonard | e6a01fa | 2018-01-12 10:29:58 -0500 | [diff] [blame] | 247 | break; |
| 248 | case REQUEST_SEG: |
Anthony Léonard | bee94cc | 2018-01-16 11:42:40 -0500 | [diff] [blame] | 249 | if (currentFilterType != lrc::api::profile::Type::PENDING) { |
| 250 | model_->setFilter(lrc::api::profile::Type::PENDING); |
| 251 | [delegate listTypeChanged]; |
| 252 | currentFilterType = lrc::api::profile::Type::PENDING; |
| 253 | } |
Anthony Léonard | e6a01fa | 2018-01-12 10:29:58 -0500 | [diff] [blame] | 254 | break; |
| 255 | default: |
| 256 | NSLog(@"Invalid item selected in list selector: %d", selectedItem); |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | -(void) selectConversationList |
| 261 | { |
Anthony Léonard | d00cd18 | 2018-01-17 09:21:27 -0500 | [diff] [blame] | 262 | if (currentFilterType == lrc::api::profile::Type::RING) |
| 263 | return; |
Anthony Léonard | e6a01fa | 2018-01-12 10:29:58 -0500 | [diff] [blame] | 264 | [listTypeSelector setSelectedSegment:CONVERSATION_SEG]; |
Anthony Léonard | 4bfd68b | 2018-01-15 16:23:50 -0500 | [diff] [blame] | 265 | |
| 266 | // Do not invert order of the next two lines or stack overflow |
| 267 | // may happen on -(void) reloadData call if filter is currently set to PENDING |
| 268 | currentFilterType = lrc::api::profile::Type::RING; |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 269 | model_->setFilter(lrc::api::profile::Type::RING); |
Anthony Léonard | 4bfd68b | 2018-01-15 16:23:50 -0500 | [diff] [blame] | 270 | model_->setFilter(""); |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 271 | } |
| 272 | |
Anthony Léonard | d00cd18 | 2018-01-17 09:21:27 -0500 | [diff] [blame] | 273 | -(void) selectPendingList |
| 274 | { |
| 275 | if (currentFilterType == lrc::api::profile::Type::PENDING) |
| 276 | return; |
| 277 | [listTypeSelector setSelectedSegment:REQUEST_SEG]; |
| 278 | |
| 279 | currentFilterType = lrc::api::profile::Type::PENDING; |
| 280 | model_->setFilter(lrc::api::profile::Type::PENDING); |
| 281 | model_->setFilter(""); |
| 282 | } |
| 283 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 284 | #pragma mark - NSTableViewDelegate methods |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 285 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 286 | - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 287 | { |
| 288 | return YES; |
| 289 | } |
| 290 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 291 | - (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 292 | { |
| 293 | return NO; |
| 294 | } |
| 295 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 296 | - (void)tableViewSelectionDidChange:(NSNotification *)notification |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 297 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 298 | NSInteger row = [notification.object selectedRow]; |
| 299 | |
| 300 | if (row == -1) |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 301 | return; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 302 | |
| 303 | auto uid = model_->filteredConversation(row).uid; |
| 304 | if (selectedUid_ != uid) { |
| 305 | selectedUid_ = uid; |
| 306 | model_->selectConversation(uid); |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 307 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 308 | } |
| 309 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 310 | - (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 311 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 312 | return [tableView makeViewWithIdentifier:@"HoverRowView" owner:nil]; |
| 313 | } |
Alexandre Lision | a4bb051 | 2016-04-19 18:04:31 -0400 | [diff] [blame] | 314 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 315 | - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row |
| 316 | { |
| 317 | if (model_ == nil) |
| 318 | return nil; |
| 319 | |
| 320 | auto conversation = model_->filteredConversation(row); |
Alexandre Lision | a4bb051 | 2016-04-19 18:04:31 -0400 | [diff] [blame] | 321 | NSTableCellView* result; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 322 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 323 | result = [tableView makeViewWithIdentifier:@"MainCell" owner:tableView]; |
| 324 | // NSTextField* details = [result viewWithTag:DETAILS_TAG]; |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 325 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 326 | NSMutableArray* controls = [NSMutableArray arrayWithObject:[result viewWithTag:CALL_BUTTON_TAG]]; |
| 327 | [((ContextualTableCellView*) result) setContextualsControls:controls]; |
| 328 | [((ContextualTableCellView*) result) setShouldBlurParentView:YES]; |
Alexandre Lision | 21666f3 | 2015-09-22 17:04:36 -0400 | [diff] [blame] | 329 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 330 | // if (auto call = RecentModel::instance().getActiveCall(qIdx)) { |
| 331 | // [details setStringValue:call->roleData((int)Ring::Role::FormattedState).toString().toNSString()]; |
| 332 | // [((ContextualTableCellView*) result) setActiveState:YES]; |
| 333 | // } else { |
| 334 | // [details setStringValue:qIdx.data((int)Ring::Role::FormattedLastUsed).toString().toNSString()]; |
| 335 | // [((ContextualTableCellView*) result) setActiveState:NO]; |
| 336 | // } |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 337 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 338 | NSTextField* unreadCount = [result viewWithTag:TXT_BUTTON_TAG]; |
| 339 | [unreadCount setHidden:(conversation.unreadMessages == 0)]; |
| 340 | [unreadCount setIntValue:conversation.unreadMessages]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 341 | |
| 342 | NSTextField* displayName = [result viewWithTag:DISPLAYNAME_TAG]; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 343 | NSString* displayNameString = bestNameForConversation(conversation, *model_); |
| 344 | NSString* displayIDString = bestIDForConversation(conversation, *model_); |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 345 | if(displayNameString.length == 0 || [displayNameString isEqualToString:displayIDString]) { |
Anthony Léonard | 4adc3f0 | 2017-07-21 10:23:04 -0400 | [diff] [blame] | 346 | NSTextField* displayRingID = [result viewWithTag:RING_ID_LABEL]; |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 347 | [displayName setStringValue:displayIDString]; |
Anthony Léonard | 4adc3f0 | 2017-07-21 10:23:04 -0400 | [diff] [blame] | 348 | [displayRingID setHidden:YES]; |
Kateryna Kostiuk | 9d6657a | 2017-04-25 13:42:25 -0400 | [diff] [blame] | 349 | } |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 350 | else { |
| 351 | NSTextField* displayRingID = [result viewWithTag:RING_ID_LABEL]; |
| 352 | [displayName setStringValue:displayNameString]; |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 353 | [displayRingID setStringValue:displayIDString]; |
Anthony Léonard | 4adc3f0 | 2017-07-21 10:23:04 -0400 | [diff] [blame] | 354 | [displayRingID setHidden:NO]; |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 355 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 356 | NSImageView* photoView = [result viewWithTag:IMAGE_TAG]; |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 357 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 358 | auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator()); |
| 359 | [photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(conversation, model_->owner)))]; |
Kateryna Kostiuk | d9039e9 | 2017-05-24 14:29:54 -0400 | [diff] [blame] | 360 | |
| 361 | NSView* presenceView = [result viewWithTag:PRESENCE_TAG]; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 362 | if (model_->owner.contactModel->getContact(conversation.participants[0]).isPresent) { |
Kateryna Kostiuk | d9039e9 | 2017-05-24 14:29:54 -0400 | [diff] [blame] | 363 | [presenceView setHidden:NO]; |
| 364 | } else { |
| 365 | [presenceView setHidden:YES]; |
| 366 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 367 | return result; |
| 368 | } |
| 369 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 370 | - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 371 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 372 | return 60.0; |
| 373 | } |
| 374 | |
| 375 | #pragma mark - NSTableDataSource methods |
| 376 | |
| 377 | - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView |
| 378 | { |
| 379 | if (tableView == smartView) { |
| 380 | if (model_ != nullptr) |
| 381 | return model_->allFilteredConversations().size(); |
| 382 | } |
| 383 | |
| 384 | return 0; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 385 | } |
| 386 | |
Kateryna Kostiuk | 882cbac | 2017-07-05 17:29:00 -0400 | [diff] [blame] | 387 | - (void)startCallForRow:(id)sender { |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 388 | NSInteger row = [smartView rowForView:sender]; |
| 389 | [smartView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO]; |
| 390 | [self placeCall:nil]; |
| 391 | } |
| 392 | |
| 393 | - (IBAction)hangUpClickedAtRow:(id)sender { |
| 394 | NSInteger row = [smartView rowForView:sender]; |
Alexandre Lision | 57914fa | 2016-02-10 14:52:24 -0500 | [diff] [blame] | 395 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 396 | if (row == -1) |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 397 | return; |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 398 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 399 | auto conv = model_->filteredConversation(row); |
| 400 | auto& callId = conv.callId; |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 401 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 402 | if (callId.empty()) |
| 403 | return; |
| 404 | |
| 405 | auto* callModel = model_->owner.callModel.get(); |
| 406 | callModel->hangUp(callId); |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 407 | } |
| 408 | |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 409 | - (void) displayErrorModalWithTitle:(NSString*) title WithMessage:(NSString*) message |
| 410 | { |
| 411 | NSAlert* alert = [NSAlert alertWithMessageText:title |
| 412 | defaultButton:@"Ok" |
| 413 | alternateButton:nil |
| 414 | otherButton:nil |
| 415 | informativeTextWithFormat:message]; |
| 416 | |
| 417 | [alert beginSheetModalForWindow:self.view.window modalDelegate:nil didEndSelector:NULL contextInfo:NULL]; |
| 418 | } |
| 419 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 420 | - (void) processSearchFieldInput |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 421 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 422 | model_->setFilter(std::string([[searchField stringValue] UTF8String])); |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 423 | } |
| 424 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 425 | -(const lrc::api::account::Info&) chosenAccount |
Kateryna Kostiuk | d2f7b5d | 2017-06-09 15:30:49 -0400 | [diff] [blame] | 426 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 427 | return model_->owner; |
Kateryna Kostiuk | d2f7b5d | 2017-06-09 15:30:49 -0400 | [diff] [blame] | 428 | } |
| 429 | |
Alexandre Lision | f909fb1 | 2016-11-03 12:35:36 -0400 | [diff] [blame] | 430 | - (void) clearSearchField |
| 431 | { |
| 432 | [searchField setStringValue:@""]; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 433 | [self processSearchFieldInput]; |
Kateryna Kostiuk | 9dd759c | 2017-07-11 12:06:55 -0400 | [diff] [blame] | 434 | } |
| 435 | |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 436 | /** |
| 437 | Copy a NSString in the general Pasteboard |
| 438 | |
| 439 | @param sender the NSObject containing the represented object to copy |
| 440 | */ |
| 441 | - (void) copyStringToPasteboard:(id) sender |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 442 | { |
| 443 | NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard]; |
| 444 | [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; |
| 445 | [pasteBoard setString:[sender representedObject] forType:NSStringPboardType]; |
| 446 | } |
| 447 | |
Alexandre Lision | bf0385e | 2015-10-22 17:36:28 -0400 | [diff] [blame] | 448 | #pragma NSTextFieldDelegate |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 449 | |
| 450 | - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector |
| 451 | { |
| 452 | if (commandSelector == @selector(insertNewline:)) { |
| 453 | if([[searchField stringValue] isNotEqualTo:@""]) { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 454 | [self processSearchFieldInput]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 455 | return YES; |
| 456 | } |
| 457 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 458 | return NO; |
| 459 | } |
| 460 | |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 461 | - (void)controlTextDidChange:(NSNotification *) notification |
| 462 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 463 | [self processSearchFieldInput]; |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 464 | } |
| 465 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 466 | #pragma mark - NSPopOverDelegate |
| 467 | |
| 468 | - (void)popoverDidClose:(NSNotification *)notification |
| 469 | { |
| 470 | if (addToContactPopover != nullptr) { |
| 471 | [addToContactPopover performClose:self]; |
| 472 | addToContactPopover = NULL; |
| 473 | } |
| 474 | } |
| 475 | |
Alexandre Lision | bf0385e | 2015-10-22 17:36:28 -0400 | [diff] [blame] | 476 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 477 | #pragma mark - ContactLinkedDelegate |
| 478 | |
| 479 | - (void)contactLinked |
| 480 | { |
| 481 | if (addToContactPopover != nullptr) { |
| 482 | [addToContactPopover performClose:self]; |
| 483 | addToContactPopover = NULL; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | #pragma mark - KeyboardShortcutDelegate |
| 488 | |
| 489 | - (void) onAddShortcut |
| 490 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 491 | if ([smartView selectedRow] == -1) |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 492 | return; |
| 493 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 494 | auto uid = model_->filteredConversation([smartView selectedRow]).uid; |
| 495 | model_->makePermanent(uid); |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | #pragma mark - ContextMenuDelegate |
| 499 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 500 | #if 0 |
| 501 | // TODO: Reimplement contextual menu with new models and behaviors |
Alexandre Lision | 12946a7 | 2016-03-08 13:39:34 -0500 | [diff] [blame] | 502 | - (NSMenu*) contextualMenuForIndex:(NSTreeNode*) item |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 503 | { |
Alexandre Lision | 12946a7 | 2016-03-08 13:39:34 -0500 | [diff] [blame] | 504 | auto qIdx = [treeController toQIdx:item]; |
| 505 | |
| 506 | if (!qIdx.isValid()) { |
| 507 | return nil; |
| 508 | } |
| 509 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 510 | auto originIdx = RecentModel::instance().peopleProxy()->mapToSource(qIdx); |
| 511 | auto contactmethods = RecentModel::instance().getContactMethods(originIdx); |
| 512 | if (contactmethods.isEmpty()) |
| 513 | return nil; |
| 514 | |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 515 | NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@""]; |
| 516 | |
| 517 | if (contactmethods.size() == 1 |
| 518 | && !contactmethods.first()->contact() |
| 519 | || contactmethods.first()->contact()->isPlaceHolder()) { |
| 520 | |
Kateryna Kostiuk | 9dd759c | 2017-07-11 12:06:55 -0400 | [diff] [blame] | 521 | NSMenuItem* addContactItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Add to contacts", @"Contextual menu action") |
| 522 | action:@selector(addContactForRow:) |
| 523 | keyEquivalent:@""]; |
| 524 | [addContactItem setRepresentedObject:item]; |
| 525 | [theMenu addItem:addContactItem]; |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 526 | } else if (auto person = contactmethods.first()->contact()) { |
| 527 | NSMenuItem* copyNameItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Copy name", @"Contextual menu action") |
| 528 | action:@selector(copyStringToPasteboard:) |
| 529 | keyEquivalent:@""]; |
| 530 | |
| 531 | [copyNameItem setRepresentedObject:person->formattedName().toNSString()]; |
| 532 | [theMenu addItem:copyNameItem]; |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 533 | } |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 534 | |
| 535 | NSMenu* copySubmenu = [[NSMenu alloc] init]; |
| 536 | NSMenu* callSubmenu = [[NSMenu alloc] init]; |
| 537 | |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 538 | for (auto cm : contactmethods) { |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 539 | NSMenuItem* tmpCopyItem = [[NSMenuItem alloc] initWithTitle:cm->uri().toNSString() |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 540 | action:@selector(copyStringToPasteboard:) |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 541 | keyEquivalent:@""]; |
| 542 | |
| 543 | [tmpCopyItem setRepresentedObject:cm->uri().toNSString()]; |
| 544 | [copySubmenu addItem:tmpCopyItem]; |
| 545 | |
| 546 | NSMenuItem* tmpCallItem = [[NSMenuItem alloc] initWithTitle:cm->uri().toNSString() |
| 547 | action:@selector(callNumber:) |
| 548 | keyEquivalent:@""]; |
| 549 | [tmpCallItem setRepresentedObject:cm->uri().toNSString()]; |
| 550 | [callSubmenu addItem:tmpCallItem]; |
| 551 | } |
| 552 | |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 553 | NSMenuItem* copyNumberItem = [[NSMenuItem alloc] init]; |
| 554 | [copyNumberItem setTitle:NSLocalizedString(@"Copy number", @"Contextual menu action")]; |
| 555 | [copyNumberItem setSubmenu:copySubmenu]; |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 556 | |
| 557 | NSMenuItem* callItems = [[NSMenuItem alloc] init]; |
| 558 | [callItems setTitle:NSLocalizedString(@"Call number", @"Contextual menu action")]; |
| 559 | [callItems setSubmenu:callSubmenu]; |
| 560 | |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 561 | [theMenu insertItem:copyNumberItem atIndex:theMenu.itemArray.count]; |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 562 | [theMenu insertItem:[NSMenuItem separatorItem] atIndex:theMenu.itemArray.count]; |
| 563 | [theMenu insertItem:callItems atIndex:theMenu.itemArray.count]; |
| 564 | |
| 565 | return theMenu; |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 566 | } |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 567 | #endif |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 568 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 569 | @end |