blob: 31a0823aaba944b90e9a11d16b378cce8e7997ab [file] [log] [blame]
Alexandre Lision4dfcafc2015-08-20 12:43:23 -04001/*
Alexandre Lision9fe374b2016-01-06 10:17:31 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lision4dfcafc2015-08-20 12:43:23 -04003 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
Olivier Soldanod4311552017-11-20 15:09:53 -05004 * Author: Olivier Soldano <olivier.soldano@savoirfairelinux.com>
5 * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
Alexandre Lision4dfcafc2015-08-20 12:43:23 -04006 *
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 Lision4dfcafc2015-08-20 12:43:23 -040031#import <globalinstances.h>
Olivier Soldanod4311552017-11-20 15:09:53 -050032#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 Lision4dfcafc2015-08-20 12:43:23 -040038
39#import "QNSTreeController.h"
40#import "delegates/ImageManipulationDelegate.h"
Alexandre Lision4e280d62015-09-09 15:56:30 -040041#import "views/HoverTableRowView.h"
Alexandre Lision61db3552015-10-22 19:12:52 -040042#import "PersonLinkerVC.h"
Alexandre Lision4baba4c2016-02-11 13:00:57 -050043#import "views/IconButton.h"
Olivier Soldanod4311552017-11-20 15:09:53 -050044#import "views/RingTableView.h"
Alexandre Lision4e280d62015-09-09 15:56:30 -040045#import "views/ContextualTableCellView.h"
Olivier Soldanod4311552017-11-20 15:09:53 -050046#import "utils.h"
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040047
Olivier Soldanod4311552017-11-20 15:09:53 -050048@interface SmartViewVC () <NSTableViewDelegate, NSTableViewDataSource, NSPopoverDelegate, ContextMenuDelegate, ContactLinkedDelegate, KeyboardShortcutDelegate> {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050049
Alexandre Lision61db3552015-10-22 19:12:52 -040050 NSPopover* addToContactPopover;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040051
52 //UI elements
Olivier Soldanod4311552017-11-20 15:09:53 -050053 __unsafe_unretained IBOutlet RingTableView* smartView;
Alexandre Lision61db3552015-10-22 19:12:52 -040054 __unsafe_unretained IBOutlet NSSearchField* searchField;
Alexandre Lision45c18672016-11-02 15:24:45 -040055
Anthony Léonard01634102017-12-27 16:37:16 -050056 QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_;
Olivier Soldanod4311552017-11-20 15:09:53 -050057
58 lrc::api::ConversationModel* model_;
59 std::string selectedUid_;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040060}
61
62@end
63
64@implementation SmartViewVC
65
Kateryna Kostiuk882cbac2017-07-05 17:29:00 -040066@synthesize tabbar;
67
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040068// Tags for views
Alexandre Lision57914fa2016-02-10 14:52:24 -050069NSInteger const IMAGE_TAG = 100;
70NSInteger const DISPLAYNAME_TAG = 200;
71NSInteger const DETAILS_TAG = 300;
72NSInteger const CALL_BUTTON_TAG = 400;
73NSInteger const TXT_BUTTON_TAG = 500;
74NSInteger const CANCEL_BUTTON_TAG = 600;
Kateryna Kostiuk6a536362017-05-02 14:26:12 -040075NSInteger const RING_ID_LABEL = 700;
Kateryna Kostiukd9039e92017-05-24 14:29:54 -040076NSInteger const PRESENCE_TAG = 800;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040077
78- (void)awakeFromNib
79{
80 NSLog(@"INIT SmartView VC");
Olivier Soldanod4311552017-11-20 15:09:53 -050081 //get selected account
82 //encapsulate conversationmodel in local version
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040083
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040084 [smartView setTarget:self];
85 [smartView setDoubleAction:@selector(placeCall:)];
86
Alexandre Lision61db3552015-10-22 19:12:52 -040087 [smartView setContextMenuDelegate:self];
88 [smartView setShortcutsDelegate:self];
89
Olivier Soldanod4311552017-11-20 15:09:53 -050090 [smartView setDataSource: self];
Kateryna Kostiuk1f705ab2017-04-18 12:45:59 -040091
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040092 [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 Soldanod4311552017-11-20 15:09:53 -0500103 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 Lision4dfcafc2015-08-20 12:43:23 -0400109 return;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400110
Olivier Soldanod4311552017-11-20 15:09:53 -0500111 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 Lision4dfcafc2015-08-20 12:43:23 -0400130 }
131 }
132
Olivier Soldanod4311552017-11-20 15:09:53 -0500133 [smartView scrollToBeginningOfDocument:nil];
134}
135
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500136- (BOOL)setConversationModel:(lrc::api::ConversationModel *)conversationModel
Olivier Soldanod4311552017-11-20 15:09:53 -0500137{
138 if (model_ != conversationModel) {
139 model_ = conversationModel;
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500140 selectedUid_.clear(); // Clear selected conversation as the selected account is being changed
Olivier Soldanod4311552017-11-20 15:09:53 -0500141 [self reloadData];
142 QObject::disconnect(modelSortedConnection_);
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500143 QObject::disconnect(filterChangedConnection_);
Anthony Léonard01634102017-12-27 16:37:16 -0500144 QObject::disconnect(newConversationConnection_);
145 QObject::disconnect(conversationRemovedConnection_);
Olivier Soldanod4311552017-11-20 15:09:53 -0500146 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éonard01634102017-12-27 16:37:16 -0500155 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éonard9bebf1d2017-12-21 14:33:51 -0500163 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 Soldanod4311552017-11-20 15:09:53 -0500188 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400189 }
190}
191
Olivier Soldanod4311552017-11-20 15:09:53 -0500192#pragma mark - NSTableViewDelegate methods
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400193
Olivier Soldanod4311552017-11-20 15:09:53 -0500194- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400195{
196 return YES;
197}
198
Olivier Soldanod4311552017-11-20 15:09:53 -0500199- (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400200{
201 return NO;
202}
203
Olivier Soldanod4311552017-11-20 15:09:53 -0500204- (void)tableViewSelectionDidChange:(NSNotification *)notification
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400205{
Olivier Soldanod4311552017-11-20 15:09:53 -0500206 NSInteger row = [notification.object selectedRow];
207
208 if (row == -1)
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400209 return;
Olivier Soldanod4311552017-11-20 15:09:53 -0500210
211 auto uid = model_->filteredConversation(row).uid;
212 if (selectedUid_ != uid) {
213 selectedUid_ = uid;
214 model_->selectConversation(uid);
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400215 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400216}
217
Olivier Soldanod4311552017-11-20 15:09:53 -0500218- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400219{
Olivier Soldanod4311552017-11-20 15:09:53 -0500220 return [tableView makeViewWithIdentifier:@"HoverRowView" owner:nil];
221}
Alexandre Lisiona4bb0512016-04-19 18:04:31 -0400222
Olivier Soldanod4311552017-11-20 15:09:53 -0500223- (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 Lisiona4bb0512016-04-19 18:04:31 -0400229 NSTableCellView* result;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400230
Olivier Soldanod4311552017-11-20 15:09:53 -0500231 result = [tableView makeViewWithIdentifier:@"MainCell" owner:tableView];
232// NSTextField* details = [result viewWithTag:DETAILS_TAG];
Alexandre Lision4e280d62015-09-09 15:56:30 -0400233
Olivier Soldanod4311552017-11-20 15:09:53 -0500234 NSMutableArray* controls = [NSMutableArray arrayWithObject:[result viewWithTag:CALL_BUTTON_TAG]];
235 [((ContextualTableCellView*) result) setContextualsControls:controls];
236 [((ContextualTableCellView*) result) setShouldBlurParentView:YES];
Alexandre Lision21666f32015-09-22 17:04:36 -0400237
Olivier Soldanod4311552017-11-20 15:09:53 -0500238// 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 Lision0f66bd32016-01-18 11:30:45 -0500245
Olivier Soldanod4311552017-11-20 15:09:53 -0500246 NSTextField* unreadCount = [result viewWithTag:TXT_BUTTON_TAG];
247 [unreadCount setHidden:(conversation.unreadMessages == 0)];
248 [unreadCount setIntValue:conversation.unreadMessages];
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400249
250 NSTextField* displayName = [result viewWithTag:DISPLAYNAME_TAG];
Olivier Soldanod4311552017-11-20 15:09:53 -0500251 NSString* displayNameString = bestNameForConversation(conversation, *model_);
252 NSString* displayIDString = bestIDForConversation(conversation, *model_);
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400253 if(displayNameString.length == 0 || [displayNameString isEqualToString:displayIDString]) {
Anthony Léonard4adc3f02017-07-21 10:23:04 -0400254 NSTextField* displayRingID = [result viewWithTag:RING_ID_LABEL];
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400255 [displayName setStringValue:displayIDString];
Anthony Léonard4adc3f02017-07-21 10:23:04 -0400256 [displayRingID setHidden:YES];
Kateryna Kostiuk9d6657a2017-04-25 13:42:25 -0400257 }
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400258 else {
259 NSTextField* displayRingID = [result viewWithTag:RING_ID_LABEL];
260 [displayName setStringValue:displayNameString];
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400261 [displayRingID setStringValue:displayIDString];
Anthony Léonard4adc3f02017-07-21 10:23:04 -0400262 [displayRingID setHidden:NO];
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400263 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400264 NSImageView* photoView = [result viewWithTag:IMAGE_TAG];
Alexandre Lision43e91bc2016-04-19 18:04:52 -0400265
Olivier Soldanod4311552017-11-20 15:09:53 -0500266 auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator());
267 [photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(conversation, model_->owner)))];
Kateryna Kostiukd9039e92017-05-24 14:29:54 -0400268
269 NSView* presenceView = [result viewWithTag:PRESENCE_TAG];
Olivier Soldanod4311552017-11-20 15:09:53 -0500270 if (model_->owner.contactModel->getContact(conversation.participants[0]).isPresent) {
Kateryna Kostiukd9039e92017-05-24 14:29:54 -0400271 [presenceView setHidden:NO];
272 } else {
273 [presenceView setHidden:YES];
274 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400275 return result;
276}
277
Olivier Soldanod4311552017-11-20 15:09:53 -0500278- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500279{
Olivier Soldanod4311552017-11-20 15:09:53 -0500280 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 Lision0f66bd32016-01-18 11:30:45 -0500293}
294
Kateryna Kostiuk882cbac2017-07-05 17:29:00 -0400295- (void)startCallForRow:(id)sender {
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400296 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 Lision57914fa2016-02-10 14:52:24 -0500303
Olivier Soldanod4311552017-11-20 15:09:53 -0500304 if (row == -1)
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500305 return;
Alexandre Lision45c18672016-11-02 15:24:45 -0400306
Olivier Soldanod4311552017-11-20 15:09:53 -0500307 auto conv = model_->filteredConversation(row);
308 auto& callId = conv.callId;
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500309
Olivier Soldanod4311552017-11-20 15:09:53 -0500310 if (callId.empty())
311 return;
312
313 auto* callModel = model_->owner.callModel.get();
314 callModel->hangUp(callId);
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400315}
316
Alexandre Lision45c18672016-11-02 15:24:45 -0400317- (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 Soldanod4311552017-11-20 15:09:53 -0500328- (void) processSearchFieldInput
Alexandre Lision45c18672016-11-02 15:24:45 -0400329{
Olivier Soldanod4311552017-11-20 15:09:53 -0500330 model_->setFilter(std::string([[searchField stringValue] UTF8String]));
Alexandre Lision45c18672016-11-02 15:24:45 -0400331}
332
Olivier Soldanod4311552017-11-20 15:09:53 -0500333-(const lrc::api::account::Info&) chosenAccount
Kateryna Kostiukd2f7b5d2017-06-09 15:30:49 -0400334{
Olivier Soldanod4311552017-11-20 15:09:53 -0500335 return model_->owner;
Kateryna Kostiukd2f7b5d2017-06-09 15:30:49 -0400336}
337
Alexandre Lisionf909fb12016-11-03 12:35:36 -0400338- (void) clearSearchField
339{
340 [searchField setStringValue:@""];
Olivier Soldanod4311552017-11-20 15:09:53 -0500341 [self processSearchFieldInput];
Kateryna Kostiuk9dd759c2017-07-11 12:06:55 -0400342}
343
Alexandre Lision72a669e2016-09-14 17:52:16 -0400344/**
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 Lisiond7bf2882015-10-22 22:54:32 -0400350{
351 NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
352 [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
353 [pasteBoard setString:[sender representedObject] forType:NSStringPboardType];
354}
355
Alexandre Lisionbf0385e2015-10-22 17:36:28 -0400356#pragma NSTextFieldDelegate
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400357
358- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
359{
360 if (commandSelector == @selector(insertNewline:)) {
361 if([[searchField stringValue] isNotEqualTo:@""]) {
Olivier Soldanod4311552017-11-20 15:09:53 -0500362 [self processSearchFieldInput];
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400363 return YES;
364 }
365 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400366 return NO;
367}
368
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500369- (void)controlTextDidChange:(NSNotification *) notification
370{
Olivier Soldanod4311552017-11-20 15:09:53 -0500371 [self processSearchFieldInput];
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500372}
373
Alexandre Lision61db3552015-10-22 19:12:52 -0400374#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 Lisionbf0385e2015-10-22 17:36:28 -0400384
Alexandre Lision61db3552015-10-22 19:12:52 -0400385#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 Soldanod4311552017-11-20 15:09:53 -0500399 if ([smartView selectedRow] == -1)
Alexandre Lision61db3552015-10-22 19:12:52 -0400400 return;
401
Olivier Soldanod4311552017-11-20 15:09:53 -0500402 auto uid = model_->filteredConversation([smartView selectedRow]).uid;
403 model_->makePermanent(uid);
Alexandre Lision61db3552015-10-22 19:12:52 -0400404}
405
406#pragma mark - ContextMenuDelegate
407
Olivier Soldanod4311552017-11-20 15:09:53 -0500408#if 0
409// TODO: Reimplement contextual menu with new models and behaviors
Alexandre Lision12946a72016-03-08 13:39:34 -0500410- (NSMenu*) contextualMenuForIndex:(NSTreeNode*) item
Alexandre Lision61db3552015-10-22 19:12:52 -0400411{
Alexandre Lision12946a72016-03-08 13:39:34 -0500412 auto qIdx = [treeController toQIdx:item];
413
414 if (!qIdx.isValid()) {
415 return nil;
416 }
417
Alexandre Lision61db3552015-10-22 19:12:52 -0400418 auto originIdx = RecentModel::instance().peopleProxy()->mapToSource(qIdx);
419 auto contactmethods = RecentModel::instance().getContactMethods(originIdx);
420 if (contactmethods.isEmpty())
421 return nil;
422
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400423 NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@""];
424
425 if (contactmethods.size() == 1
426 && !contactmethods.first()->contact()
427 || contactmethods.first()->contact()->isPlaceHolder()) {
428
Kateryna Kostiuk9dd759c2017-07-11 12:06:55 -0400429 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 Lision72a669e2016-09-14 17:52:16 -0400434 } 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 Lision61db3552015-10-22 19:12:52 -0400441 }
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400442
443 NSMenu* copySubmenu = [[NSMenu alloc] init];
444 NSMenu* callSubmenu = [[NSMenu alloc] init];
445
Alexandre Lision72a669e2016-09-14 17:52:16 -0400446 for (auto cm : contactmethods) {
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400447 NSMenuItem* tmpCopyItem = [[NSMenuItem alloc] initWithTitle:cm->uri().toNSString()
Alexandre Lision72a669e2016-09-14 17:52:16 -0400448 action:@selector(copyStringToPasteboard:)
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400449 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 Lision72a669e2016-09-14 17:52:16 -0400461 NSMenuItem* copyNumberItem = [[NSMenuItem alloc] init];
462 [copyNumberItem setTitle:NSLocalizedString(@"Copy number", @"Contextual menu action")];
463 [copyNumberItem setSubmenu:copySubmenu];
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400464
465 NSMenuItem* callItems = [[NSMenuItem alloc] init];
466 [callItems setTitle:NSLocalizedString(@"Call number", @"Contextual menu action")];
467 [callItems setSubmenu:callSubmenu];
468
Alexandre Lision72a669e2016-09-14 17:52:16 -0400469 [theMenu insertItem:copyNumberItem atIndex:theMenu.itemArray.count];
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400470 [theMenu insertItem:[NSMenuItem separatorItem] atIndex:theMenu.itemArray.count];
471 [theMenu insertItem:callItems atIndex:theMenu.itemArray.count];
472
473 return theMenu;
Alexandre Lision61db3552015-10-22 19:12:52 -0400474}
Olivier Soldanod4311552017-11-20 15:09:53 -0500475#endif
Alexandre Lision61db3552015-10-22 19:12:52 -0400476
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400477@end