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 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 192 | #pragma mark - NSTableViewDelegate methods |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 193 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 194 | - (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 195 | { |
| 196 | return YES; |
| 197 | } |
| 198 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 199 | - (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 200 | { |
| 201 | return NO; |
| 202 | } |
| 203 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 204 | - (void)tableViewSelectionDidChange:(NSNotification *)notification |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 205 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 206 | NSInteger row = [notification.object selectedRow]; |
| 207 | |
| 208 | if (row == -1) |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 209 | return; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 210 | |
| 211 | auto uid = model_->filteredConversation(row).uid; |
| 212 | if (selectedUid_ != uid) { |
| 213 | selectedUid_ = uid; |
| 214 | model_->selectConversation(uid); |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 215 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 216 | } |
| 217 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 218 | - (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 219 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 220 | return [tableView makeViewWithIdentifier:@"HoverRowView" owner:nil]; |
| 221 | } |
Alexandre Lision | a4bb051 | 2016-04-19 18:04:31 -0400 | [diff] [blame] | 222 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 223 | - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row |
| 224 | { |
| 225 | if (model_ == nil) |
| 226 | return nil; |
| 227 | |
| 228 | auto conversation = model_->filteredConversation(row); |
Alexandre Lision | a4bb051 | 2016-04-19 18:04:31 -0400 | [diff] [blame] | 229 | NSTableCellView* result; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 230 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 231 | result = [tableView makeViewWithIdentifier:@"MainCell" owner:tableView]; |
| 232 | // NSTextField* details = [result viewWithTag:DETAILS_TAG]; |
Alexandre Lision | 4e280d6 | 2015-09-09 15:56:30 -0400 | [diff] [blame] | 233 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 234 | NSMutableArray* controls = [NSMutableArray arrayWithObject:[result viewWithTag:CALL_BUTTON_TAG]]; |
| 235 | [((ContextualTableCellView*) result) setContextualsControls:controls]; |
| 236 | [((ContextualTableCellView*) result) setShouldBlurParentView:YES]; |
Alexandre Lision | 21666f3 | 2015-09-22 17:04:36 -0400 | [diff] [blame] | 237 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 238 | // if (auto call = RecentModel::instance().getActiveCall(qIdx)) { |
| 239 | // [details setStringValue:call->roleData((int)Ring::Role::FormattedState).toString().toNSString()]; |
| 240 | // [((ContextualTableCellView*) result) setActiveState:YES]; |
| 241 | // } else { |
| 242 | // [details setStringValue:qIdx.data((int)Ring::Role::FormattedLastUsed).toString().toNSString()]; |
| 243 | // [((ContextualTableCellView*) result) setActiveState:NO]; |
| 244 | // } |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 245 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 246 | NSTextField* unreadCount = [result viewWithTag:TXT_BUTTON_TAG]; |
| 247 | [unreadCount setHidden:(conversation.unreadMessages == 0)]; |
| 248 | [unreadCount setIntValue:conversation.unreadMessages]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 249 | |
| 250 | NSTextField* displayName = [result viewWithTag:DISPLAYNAME_TAG]; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 251 | NSString* displayNameString = bestNameForConversation(conversation, *model_); |
| 252 | NSString* displayIDString = bestIDForConversation(conversation, *model_); |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 253 | if(displayNameString.length == 0 || [displayNameString isEqualToString:displayIDString]) { |
Anthony Léonard | 4adc3f0 | 2017-07-21 10:23:04 -0400 | [diff] [blame] | 254 | NSTextField* displayRingID = [result viewWithTag:RING_ID_LABEL]; |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 255 | [displayName setStringValue:displayIDString]; |
Anthony Léonard | 4adc3f0 | 2017-07-21 10:23:04 -0400 | [diff] [blame] | 256 | [displayRingID setHidden:YES]; |
Kateryna Kostiuk | 9d6657a | 2017-04-25 13:42:25 -0400 | [diff] [blame] | 257 | } |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 258 | else { |
| 259 | NSTextField* displayRingID = [result viewWithTag:RING_ID_LABEL]; |
| 260 | [displayName setStringValue:displayNameString]; |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 261 | [displayRingID setStringValue:displayIDString]; |
Anthony Léonard | 4adc3f0 | 2017-07-21 10:23:04 -0400 | [diff] [blame] | 262 | [displayRingID setHidden:NO]; |
Kateryna Kostiuk | 6a53636 | 2017-05-02 14:26:12 -0400 | [diff] [blame] | 263 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 264 | NSImageView* photoView = [result viewWithTag:IMAGE_TAG]; |
Alexandre Lision | 43e91bc | 2016-04-19 18:04:52 -0400 | [diff] [blame] | 265 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 266 | auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator()); |
| 267 | [photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(conversation, model_->owner)))]; |
Kateryna Kostiuk | d9039e9 | 2017-05-24 14:29:54 -0400 | [diff] [blame] | 268 | |
| 269 | NSView* presenceView = [result viewWithTag:PRESENCE_TAG]; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 270 | if (model_->owner.contactModel->getContact(conversation.participants[0]).isPresent) { |
Kateryna Kostiuk | d9039e9 | 2017-05-24 14:29:54 -0400 | [diff] [blame] | 271 | [presenceView setHidden:NO]; |
| 272 | } else { |
| 273 | [presenceView setHidden:YES]; |
| 274 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 275 | return result; |
| 276 | } |
| 277 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 278 | - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 279 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 280 | return 60.0; |
| 281 | } |
| 282 | |
| 283 | #pragma mark - NSTableDataSource methods |
| 284 | |
| 285 | - (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView |
| 286 | { |
| 287 | if (tableView == smartView) { |
| 288 | if (model_ != nullptr) |
| 289 | return model_->allFilteredConversations().size(); |
| 290 | } |
| 291 | |
| 292 | return 0; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 293 | } |
| 294 | |
Kateryna Kostiuk | 882cbac | 2017-07-05 17:29:00 -0400 | [diff] [blame] | 295 | - (void)startCallForRow:(id)sender { |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 296 | NSInteger row = [smartView rowForView:sender]; |
| 297 | [smartView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO]; |
| 298 | [self placeCall:nil]; |
| 299 | } |
| 300 | |
| 301 | - (IBAction)hangUpClickedAtRow:(id)sender { |
| 302 | NSInteger row = [smartView rowForView:sender]; |
Alexandre Lision | 57914fa | 2016-02-10 14:52:24 -0500 | [diff] [blame] | 303 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 304 | if (row == -1) |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 305 | return; |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 306 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 307 | auto conv = model_->filteredConversation(row); |
| 308 | auto& callId = conv.callId; |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 309 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 310 | if (callId.empty()) |
| 311 | return; |
| 312 | |
| 313 | auto* callModel = model_->owner.callModel.get(); |
| 314 | callModel->hangUp(callId); |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 315 | } |
| 316 | |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 317 | - (void) displayErrorModalWithTitle:(NSString*) title WithMessage:(NSString*) message |
| 318 | { |
| 319 | NSAlert* alert = [NSAlert alertWithMessageText:title |
| 320 | defaultButton:@"Ok" |
| 321 | alternateButton:nil |
| 322 | otherButton:nil |
| 323 | informativeTextWithFormat:message]; |
| 324 | |
| 325 | [alert beginSheetModalForWindow:self.view.window modalDelegate:nil didEndSelector:NULL contextInfo:NULL]; |
| 326 | } |
| 327 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 328 | - (void) processSearchFieldInput |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 329 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 330 | model_->setFilter(std::string([[searchField stringValue] UTF8String])); |
Alexandre Lision | 45c1867 | 2016-11-02 15:24:45 -0400 | [diff] [blame] | 331 | } |
| 332 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 333 | -(const lrc::api::account::Info&) chosenAccount |
Kateryna Kostiuk | d2f7b5d | 2017-06-09 15:30:49 -0400 | [diff] [blame] | 334 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 335 | return model_->owner; |
Kateryna Kostiuk | d2f7b5d | 2017-06-09 15:30:49 -0400 | [diff] [blame] | 336 | } |
| 337 | |
Alexandre Lision | f909fb1 | 2016-11-03 12:35:36 -0400 | [diff] [blame] | 338 | - (void) clearSearchField |
| 339 | { |
| 340 | [searchField setStringValue:@""]; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 341 | [self processSearchFieldInput]; |
Kateryna Kostiuk | 9dd759c | 2017-07-11 12:06:55 -0400 | [diff] [blame] | 342 | } |
| 343 | |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 344 | /** |
| 345 | Copy a NSString in the general Pasteboard |
| 346 | |
| 347 | @param sender the NSObject containing the represented object to copy |
| 348 | */ |
| 349 | - (void) copyStringToPasteboard:(id) sender |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 350 | { |
| 351 | NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard]; |
| 352 | [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil]; |
| 353 | [pasteBoard setString:[sender representedObject] forType:NSStringPboardType]; |
| 354 | } |
| 355 | |
Alexandre Lision | bf0385e | 2015-10-22 17:36:28 -0400 | [diff] [blame] | 356 | #pragma NSTextFieldDelegate |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 357 | |
| 358 | - (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector |
| 359 | { |
| 360 | if (commandSelector == @selector(insertNewline:)) { |
| 361 | if([[searchField stringValue] isNotEqualTo:@""]) { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 362 | [self processSearchFieldInput]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 363 | return YES; |
| 364 | } |
| 365 | } |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 366 | return NO; |
| 367 | } |
| 368 | |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 369 | - (void)controlTextDidChange:(NSNotification *) notification |
| 370 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 371 | [self processSearchFieldInput]; |
Alexandre Lision | ac4f5b6 | 2016-02-17 13:53:12 -0500 | [diff] [blame] | 372 | } |
| 373 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 374 | #pragma mark - NSPopOverDelegate |
| 375 | |
| 376 | - (void)popoverDidClose:(NSNotification *)notification |
| 377 | { |
| 378 | if (addToContactPopover != nullptr) { |
| 379 | [addToContactPopover performClose:self]; |
| 380 | addToContactPopover = NULL; |
| 381 | } |
| 382 | } |
| 383 | |
Alexandre Lision | bf0385e | 2015-10-22 17:36:28 -0400 | [diff] [blame] | 384 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 385 | #pragma mark - ContactLinkedDelegate |
| 386 | |
| 387 | - (void)contactLinked |
| 388 | { |
| 389 | if (addToContactPopover != nullptr) { |
| 390 | [addToContactPopover performClose:self]; |
| 391 | addToContactPopover = NULL; |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | #pragma mark - KeyboardShortcutDelegate |
| 396 | |
| 397 | - (void) onAddShortcut |
| 398 | { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 399 | if ([smartView selectedRow] == -1) |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 400 | return; |
| 401 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 402 | auto uid = model_->filteredConversation([smartView selectedRow]).uid; |
| 403 | model_->makePermanent(uid); |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 404 | } |
| 405 | |
| 406 | #pragma mark - ContextMenuDelegate |
| 407 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 408 | #if 0 |
| 409 | // TODO: Reimplement contextual menu with new models and behaviors |
Alexandre Lision | 12946a7 | 2016-03-08 13:39:34 -0500 | [diff] [blame] | 410 | - (NSMenu*) contextualMenuForIndex:(NSTreeNode*) item |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 411 | { |
Alexandre Lision | 12946a7 | 2016-03-08 13:39:34 -0500 | [diff] [blame] | 412 | auto qIdx = [treeController toQIdx:item]; |
| 413 | |
| 414 | if (!qIdx.isValid()) { |
| 415 | return nil; |
| 416 | } |
| 417 | |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 418 | auto originIdx = RecentModel::instance().peopleProxy()->mapToSource(qIdx); |
| 419 | auto contactmethods = RecentModel::instance().getContactMethods(originIdx); |
| 420 | if (contactmethods.isEmpty()) |
| 421 | return nil; |
| 422 | |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 423 | NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@""]; |
| 424 | |
| 425 | if (contactmethods.size() == 1 |
| 426 | && !contactmethods.first()->contact() |
| 427 | || contactmethods.first()->contact()->isPlaceHolder()) { |
| 428 | |
Kateryna Kostiuk | 9dd759c | 2017-07-11 12:06:55 -0400 | [diff] [blame] | 429 | NSMenuItem* addContactItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Add to contacts", @"Contextual menu action") |
| 430 | action:@selector(addContactForRow:) |
| 431 | keyEquivalent:@""]; |
| 432 | [addContactItem setRepresentedObject:item]; |
| 433 | [theMenu addItem:addContactItem]; |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 434 | } else if (auto person = contactmethods.first()->contact()) { |
| 435 | NSMenuItem* copyNameItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Copy name", @"Contextual menu action") |
| 436 | action:@selector(copyStringToPasteboard:) |
| 437 | keyEquivalent:@""]; |
| 438 | |
| 439 | [copyNameItem setRepresentedObject:person->formattedName().toNSString()]; |
| 440 | [theMenu addItem:copyNameItem]; |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 441 | } |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 442 | |
| 443 | NSMenu* copySubmenu = [[NSMenu alloc] init]; |
| 444 | NSMenu* callSubmenu = [[NSMenu alloc] init]; |
| 445 | |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 446 | for (auto cm : contactmethods) { |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 447 | NSMenuItem* tmpCopyItem = [[NSMenuItem alloc] initWithTitle:cm->uri().toNSString() |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 448 | action:@selector(copyStringToPasteboard:) |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 449 | keyEquivalent:@""]; |
| 450 | |
| 451 | [tmpCopyItem setRepresentedObject:cm->uri().toNSString()]; |
| 452 | [copySubmenu addItem:tmpCopyItem]; |
| 453 | |
| 454 | NSMenuItem* tmpCallItem = [[NSMenuItem alloc] initWithTitle:cm->uri().toNSString() |
| 455 | action:@selector(callNumber:) |
| 456 | keyEquivalent:@""]; |
| 457 | [tmpCallItem setRepresentedObject:cm->uri().toNSString()]; |
| 458 | [callSubmenu addItem:tmpCallItem]; |
| 459 | } |
| 460 | |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 461 | NSMenuItem* copyNumberItem = [[NSMenuItem alloc] init]; |
| 462 | [copyNumberItem setTitle:NSLocalizedString(@"Copy number", @"Contextual menu action")]; |
| 463 | [copyNumberItem setSubmenu:copySubmenu]; |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 464 | |
| 465 | NSMenuItem* callItems = [[NSMenuItem alloc] init]; |
| 466 | [callItems setTitle:NSLocalizedString(@"Call number", @"Contextual menu action")]; |
| 467 | [callItems setSubmenu:callSubmenu]; |
| 468 | |
Alexandre Lision | 72a669e | 2016-09-14 17:52:16 -0400 | [diff] [blame] | 469 | [theMenu insertItem:copyNumberItem atIndex:theMenu.itemArray.count]; |
Alexandre Lision | d7bf288 | 2015-10-22 22:54:32 -0400 | [diff] [blame] | 470 | [theMenu insertItem:[NSMenuItem separatorItem] atIndex:theMenu.itemArray.count]; |
| 471 | [theMenu insertItem:callItems atIndex:theMenu.itemArray.count]; |
| 472 | |
| 473 | return theMenu; |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 474 | } |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 475 | #endif |
Alexandre Lision | 61db355 | 2015-10-22 19:12:52 -0400 | [diff] [blame] | 476 | |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 477 | @end |