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