blob: e529a3950ce99efb1732259817330c8462dfd501 [file] [log] [blame]
Alexandre Lision4dfcafc2015-08-20 12:43:23 -04001/*
Anthony Léonardcb73a2c2018-01-18 11:45:56 -05002 * Copyright (C) 2015-2018 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"
Anthony Léonardbee94cc2018-01-16 11:42:40 -050047#import "RingWindowController.h"
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040048
Olivier Soldanod4311552017-11-20 15:09:53 -050049@interface SmartViewVC () <NSTableViewDelegate, NSTableViewDataSource, NSPopoverDelegate, ContextMenuDelegate, ContactLinkedDelegate, KeyboardShortcutDelegate> {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050050
Alexandre Lision61db3552015-10-22 19:12:52 -040051 NSPopover* addToContactPopover;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040052
53 //UI elements
Olivier Soldanod4311552017-11-20 15:09:53 -050054 __unsafe_unretained IBOutlet RingTableView* smartView;
Alexandre Lision61db3552015-10-22 19:12:52 -040055 __unsafe_unretained IBOutlet NSSearchField* searchField;
Anthony Léonardcb73a2c2018-01-18 11:45:56 -050056 __strong IBOutlet NSSegmentedControl *listTypeSelector;
57 bool selectorIsPresent;
Alexandre Lision45c18672016-11-02 15:24:45 -040058
Anthony Léonard01634102017-12-27 16:37:16 -050059 QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_;
Olivier Soldanod4311552017-11-20 15:09:53 -050060
61 lrc::api::ConversationModel* model_;
62 std::string selectedUid_;
Anthony Léonard4bfd68b2018-01-15 16:23:50 -050063 lrc::api::profile::Type currentFilterType;
Anthony Léonardbee94cc2018-01-16 11:42:40 -050064
65 __unsafe_unretained IBOutlet RingWindowController *delegate;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040066}
67
68@end
69
70@implementation SmartViewVC
71
Kateryna Kostiuk882cbac2017-07-05 17:29:00 -040072@synthesize tabbar;
73
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040074// Tags for views
Alexandre Lision57914fa2016-02-10 14:52:24 -050075NSInteger const IMAGE_TAG = 100;
76NSInteger const DISPLAYNAME_TAG = 200;
77NSInteger const DETAILS_TAG = 300;
78NSInteger const CALL_BUTTON_TAG = 400;
79NSInteger const TXT_BUTTON_TAG = 500;
80NSInteger const CANCEL_BUTTON_TAG = 600;
Kateryna Kostiuk6a536362017-05-02 14:26:12 -040081NSInteger const RING_ID_LABEL = 700;
Kateryna Kostiukd9039e92017-05-24 14:29:54 -040082NSInteger const PRESENCE_TAG = 800;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040083
Anthony Léonarde6a01fa2018-01-12 10:29:58 -050084// Segment indices for smartlist selector
85NSInteger const CONVERSATION_SEG = 0;
86NSInteger const REQUEST_SEG = 1;
87
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040088- (void)awakeFromNib
89{
90 NSLog(@"INIT SmartView VC");
Olivier Soldanod4311552017-11-20 15:09:53 -050091 //get selected account
92 //encapsulate conversationmodel in local version
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040093
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040094 [smartView setTarget:self];
95 [smartView setDoubleAction:@selector(placeCall:)];
96
Alexandre Lision61db3552015-10-22 19:12:52 -040097 [smartView setContextMenuDelegate:self];
98 [smartView setShortcutsDelegate:self];
99
Olivier Soldanod4311552017-11-20 15:09:53 -0500100 [smartView setDataSource: self];
Kateryna Kostiuk1f705ab2017-04-18 12:45:59 -0400101
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400102 [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éonard4bfd68b2018-01-15 16:23:50 -0500109
110 currentFilterType = lrc::api::profile::Type::RING;
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500111 selectorIsPresent = true;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400112}
113
114- (void)placeCall:(id)sender
115{
Olivier Soldanod4311552017-11-20 15:09:53 -0500116 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 Lision4dfcafc2015-08-20 12:43:23 -0400122 return;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400123
Olivier Soldanod4311552017-11-20 15:09:53 -0500124 auto conv = model_->filteredConversation(row);
125 model_->placeCall(conv.uid);
126}
127
128-(void) reloadData
129{
130 [smartView deselectAll:nil];
Olivier Soldanod4311552017-11-20 15:09:53 -0500131
Anthony Léonard4bfd68b2018-01-15 16:23:50 -0500132 if (!model_->owner.contactModel->hasPendingRequests()) {
133 if (currentFilterType == lrc::api::profile::Type::PENDING) {
134 [self selectConversationList];
135 }
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500136 if (selectorIsPresent) {
137 [listTypeSelector removeFromSuperview];
138 selectorIsPresent = false;
139 }
Anthony Léonard4bfd68b2018-01-15 16:23:50 -0500140 } else {
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500141 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éonard4bfd68b2018-01-15 16:23:50 -0500161 }
162
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500163 [smartView reloadData];
164
Olivier Soldanod4311552017-11-20 15:09:53 -0500165 if (!selectedUid_.empty() && model_ != nil) {
Anthony Léonard6f819752018-01-05 09:53:40 -0500166 auto it = getConversationFromUid(selectedUid_, *model_);
Olivier Soldanod4311552017-11-20 15:09:53 -0500167 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 Lision4dfcafc2015-08-20 12:43:23 -0400172 }
173 }
174
Olivier Soldanod4311552017-11-20 15:09:53 -0500175 [smartView scrollToBeginningOfDocument:nil];
176}
177
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500178- (BOOL)setConversationModel:(lrc::api::ConversationModel *)conversationModel
Olivier Soldanod4311552017-11-20 15:09:53 -0500179{
180 if (model_ != conversationModel) {
181 model_ = conversationModel;
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500182 selectedUid_.clear(); // Clear selected conversation as the selected account is being changed
Olivier Soldanod4311552017-11-20 15:09:53 -0500183 QObject::disconnect(modelSortedConnection_);
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500184 QObject::disconnect(filterChangedConnection_);
Anthony Léonard01634102017-12-27 16:37:16 -0500185 QObject::disconnect(newConversationConnection_);
186 QObject::disconnect(conversationRemovedConnection_);
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500187 [self reloadData];
Olivier Soldanod4311552017-11-20 15:09:53 -0500188 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éonard01634102017-12-27 16:37:16 -0500197 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éonard9bebf1d2017-12-21 14:33:51 -0500205 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éonard6f819752018-01-05 09:53:40 -0500222 auto it = getConversationFromUid(selectedUid_, *model_);
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500223 if (it != model_->allFilteredConversations().end()) {
224 NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - model_->allFilteredConversations().begin())];
225 [smartView selectRowIndexes:indexSet byExtendingSelection:NO];
226 selectedUid_ = uid;
Olivier Soldanod4311552017-11-20 15:09:53 -0500227 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400228 }
229}
230
Anthony Léonard8585cc02017-12-28 14:03:45 -0500231-(void)deselect
232{
233 selectedUid_.clear();
234 [smartView deselectAll:nil];
235}
236
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500237- (IBAction) listTypeChanged:(id)sender
238{
239 NSInteger selectedItem = [sender selectedSegment];
Anthony Léonarde6a01fa2018-01-12 10:29:58 -0500240 switch (selectedItem) {
241 case CONVERSATION_SEG:
Anthony Léonardbee94cc2018-01-16 11:42:40 -0500242 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éonarde6a01fa2018-01-12 10:29:58 -0500247 break;
248 case REQUEST_SEG:
Anthony Léonardbee94cc2018-01-16 11:42:40 -0500249 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éonarde6a01fa2018-01-12 10:29:58 -0500254 break;
255 default:
256 NSLog(@"Invalid item selected in list selector: %d", selectedItem);
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500257 }
258}
259
260-(void) selectConversationList
261{
Anthony Léonardd00cd182018-01-17 09:21:27 -0500262 if (currentFilterType == lrc::api::profile::Type::RING)
263 return;
Anthony Léonarde6a01fa2018-01-12 10:29:58 -0500264 [listTypeSelector setSelectedSegment:CONVERSATION_SEG];
Anthony Léonard4bfd68b2018-01-15 16:23:50 -0500265
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éonard0a9904c2018-01-11 16:43:47 -0500269 model_->setFilter(lrc::api::profile::Type::RING);
Anthony Léonard4bfd68b2018-01-15 16:23:50 -0500270 model_->setFilter("");
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500271}
272
Anthony Léonardd00cd182018-01-17 09:21:27 -0500273-(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 Soldanod4311552017-11-20 15:09:53 -0500284#pragma mark - NSTableViewDelegate methods
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400285
Olivier Soldanod4311552017-11-20 15:09:53 -0500286- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400287{
288 return YES;
289}
290
Olivier Soldanod4311552017-11-20 15:09:53 -0500291- (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400292{
293 return NO;
294}
295
Olivier Soldanod4311552017-11-20 15:09:53 -0500296- (void)tableViewSelectionDidChange:(NSNotification *)notification
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400297{
Olivier Soldanod4311552017-11-20 15:09:53 -0500298 NSInteger row = [notification.object selectedRow];
299
300 if (row == -1)
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400301 return;
Olivier Soldanod4311552017-11-20 15:09:53 -0500302
303 auto uid = model_->filteredConversation(row).uid;
304 if (selectedUid_ != uid) {
305 selectedUid_ = uid;
306 model_->selectConversation(uid);
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400307 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400308}
309
Olivier Soldanod4311552017-11-20 15:09:53 -0500310- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400311{
Olivier Soldanod4311552017-11-20 15:09:53 -0500312 return [tableView makeViewWithIdentifier:@"HoverRowView" owner:nil];
313}
Alexandre Lisiona4bb0512016-04-19 18:04:31 -0400314
Olivier Soldanod4311552017-11-20 15:09:53 -0500315- (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 Lisiona4bb0512016-04-19 18:04:31 -0400321 NSTableCellView* result;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400322
Olivier Soldanod4311552017-11-20 15:09:53 -0500323 result = [tableView makeViewWithIdentifier:@"MainCell" owner:tableView];
324// NSTextField* details = [result viewWithTag:DETAILS_TAG];
Alexandre Lision4e280d62015-09-09 15:56:30 -0400325
Olivier Soldanod4311552017-11-20 15:09:53 -0500326 NSMutableArray* controls = [NSMutableArray arrayWithObject:[result viewWithTag:CALL_BUTTON_TAG]];
327 [((ContextualTableCellView*) result) setContextualsControls:controls];
328 [((ContextualTableCellView*) result) setShouldBlurParentView:YES];
Alexandre Lision21666f32015-09-22 17:04:36 -0400329
Olivier Soldanod4311552017-11-20 15:09:53 -0500330// 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 Lision0f66bd32016-01-18 11:30:45 -0500337
Olivier Soldanod4311552017-11-20 15:09:53 -0500338 NSTextField* unreadCount = [result viewWithTag:TXT_BUTTON_TAG];
339 [unreadCount setHidden:(conversation.unreadMessages == 0)];
340 [unreadCount setIntValue:conversation.unreadMessages];
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400341
342 NSTextField* displayName = [result viewWithTag:DISPLAYNAME_TAG];
Olivier Soldanod4311552017-11-20 15:09:53 -0500343 NSString* displayNameString = bestNameForConversation(conversation, *model_);
344 NSString* displayIDString = bestIDForConversation(conversation, *model_);
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400345 if(displayNameString.length == 0 || [displayNameString isEqualToString:displayIDString]) {
Anthony Léonard4adc3f02017-07-21 10:23:04 -0400346 NSTextField* displayRingID = [result viewWithTag:RING_ID_LABEL];
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400347 [displayName setStringValue:displayIDString];
Anthony Léonard4adc3f02017-07-21 10:23:04 -0400348 [displayRingID setHidden:YES];
Kateryna Kostiuk9d6657a2017-04-25 13:42:25 -0400349 }
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400350 else {
351 NSTextField* displayRingID = [result viewWithTag:RING_ID_LABEL];
352 [displayName setStringValue:displayNameString];
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400353 [displayRingID setStringValue:displayIDString];
Anthony Léonard4adc3f02017-07-21 10:23:04 -0400354 [displayRingID setHidden:NO];
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400355 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400356 NSImageView* photoView = [result viewWithTag:IMAGE_TAG];
Alexandre Lision43e91bc2016-04-19 18:04:52 -0400357
Olivier Soldanod4311552017-11-20 15:09:53 -0500358 auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator());
359 [photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(conversation, model_->owner)))];
Kateryna Kostiukd9039e92017-05-24 14:29:54 -0400360
361 NSView* presenceView = [result viewWithTag:PRESENCE_TAG];
Olivier Soldanod4311552017-11-20 15:09:53 -0500362 if (model_->owner.contactModel->getContact(conversation.participants[0]).isPresent) {
Kateryna Kostiukd9039e92017-05-24 14:29:54 -0400363 [presenceView setHidden:NO];
364 } else {
365 [presenceView setHidden:YES];
366 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400367 return result;
368}
369
Olivier Soldanod4311552017-11-20 15:09:53 -0500370- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500371{
Olivier Soldanod4311552017-11-20 15:09:53 -0500372 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 Lision0f66bd32016-01-18 11:30:45 -0500385}
386
Kateryna Kostiuk882cbac2017-07-05 17:29:00 -0400387- (void)startCallForRow:(id)sender {
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400388 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 Lision57914fa2016-02-10 14:52:24 -0500395
Olivier Soldanod4311552017-11-20 15:09:53 -0500396 if (row == -1)
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500397 return;
Alexandre Lision45c18672016-11-02 15:24:45 -0400398
Olivier Soldanod4311552017-11-20 15:09:53 -0500399 auto conv = model_->filteredConversation(row);
400 auto& callId = conv.callId;
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500401
Olivier Soldanod4311552017-11-20 15:09:53 -0500402 if (callId.empty())
403 return;
404
405 auto* callModel = model_->owner.callModel.get();
406 callModel->hangUp(callId);
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400407}
408
Alexandre Lision45c18672016-11-02 15:24:45 -0400409- (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 Soldanod4311552017-11-20 15:09:53 -0500420- (void) processSearchFieldInput
Alexandre Lision45c18672016-11-02 15:24:45 -0400421{
Olivier Soldanod4311552017-11-20 15:09:53 -0500422 model_->setFilter(std::string([[searchField stringValue] UTF8String]));
Alexandre Lision45c18672016-11-02 15:24:45 -0400423}
424
Olivier Soldanod4311552017-11-20 15:09:53 -0500425-(const lrc::api::account::Info&) chosenAccount
Kateryna Kostiukd2f7b5d2017-06-09 15:30:49 -0400426{
Olivier Soldanod4311552017-11-20 15:09:53 -0500427 return model_->owner;
Kateryna Kostiukd2f7b5d2017-06-09 15:30:49 -0400428}
429
Alexandre Lisionf909fb12016-11-03 12:35:36 -0400430- (void) clearSearchField
431{
432 [searchField setStringValue:@""];
Olivier Soldanod4311552017-11-20 15:09:53 -0500433 [self processSearchFieldInput];
Kateryna Kostiuk9dd759c2017-07-11 12:06:55 -0400434}
435
Alexandre Lision72a669e2016-09-14 17:52:16 -0400436/**
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 Lisiond7bf2882015-10-22 22:54:32 -0400442{
443 NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
444 [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
445 [pasteBoard setString:[sender representedObject] forType:NSStringPboardType];
446}
447
Alexandre Lisionbf0385e2015-10-22 17:36:28 -0400448#pragma NSTextFieldDelegate
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400449
450- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
451{
452 if (commandSelector == @selector(insertNewline:)) {
453 if([[searchField stringValue] isNotEqualTo:@""]) {
Olivier Soldanod4311552017-11-20 15:09:53 -0500454 [self processSearchFieldInput];
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400455 return YES;
456 }
457 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400458 return NO;
459}
460
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500461- (void)controlTextDidChange:(NSNotification *) notification
462{
Olivier Soldanod4311552017-11-20 15:09:53 -0500463 [self processSearchFieldInput];
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500464}
465
Alexandre Lision61db3552015-10-22 19:12:52 -0400466#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 Lisionbf0385e2015-10-22 17:36:28 -0400476
Alexandre Lision61db3552015-10-22 19:12:52 -0400477#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 Soldanod4311552017-11-20 15:09:53 -0500491 if ([smartView selectedRow] == -1)
Alexandre Lision61db3552015-10-22 19:12:52 -0400492 return;
493
Olivier Soldanod4311552017-11-20 15:09:53 -0500494 auto uid = model_->filteredConversation([smartView selectedRow]).uid;
495 model_->makePermanent(uid);
Alexandre Lision61db3552015-10-22 19:12:52 -0400496}
497
498#pragma mark - ContextMenuDelegate
499
Olivier Soldanod4311552017-11-20 15:09:53 -0500500#if 0
501// TODO: Reimplement contextual menu with new models and behaviors
Alexandre Lision12946a72016-03-08 13:39:34 -0500502- (NSMenu*) contextualMenuForIndex:(NSTreeNode*) item
Alexandre Lision61db3552015-10-22 19:12:52 -0400503{
Alexandre Lision12946a72016-03-08 13:39:34 -0500504 auto qIdx = [treeController toQIdx:item];
505
506 if (!qIdx.isValid()) {
507 return nil;
508 }
509
Alexandre Lision61db3552015-10-22 19:12:52 -0400510 auto originIdx = RecentModel::instance().peopleProxy()->mapToSource(qIdx);
511 auto contactmethods = RecentModel::instance().getContactMethods(originIdx);
512 if (contactmethods.isEmpty())
513 return nil;
514
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400515 NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@""];
516
517 if (contactmethods.size() == 1
518 && !contactmethods.first()->contact()
519 || contactmethods.first()->contact()->isPlaceHolder()) {
520
Kateryna Kostiuk9dd759c2017-07-11 12:06:55 -0400521 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 Lision72a669e2016-09-14 17:52:16 -0400526 } 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 Lision61db3552015-10-22 19:12:52 -0400533 }
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400534
535 NSMenu* copySubmenu = [[NSMenu alloc] init];
536 NSMenu* callSubmenu = [[NSMenu alloc] init];
537
Alexandre Lision72a669e2016-09-14 17:52:16 -0400538 for (auto cm : contactmethods) {
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400539 NSMenuItem* tmpCopyItem = [[NSMenuItem alloc] initWithTitle:cm->uri().toNSString()
Alexandre Lision72a669e2016-09-14 17:52:16 -0400540 action:@selector(copyStringToPasteboard:)
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400541 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 Lision72a669e2016-09-14 17:52:16 -0400553 NSMenuItem* copyNumberItem = [[NSMenuItem alloc] init];
554 [copyNumberItem setTitle:NSLocalizedString(@"Copy number", @"Contextual menu action")];
555 [copyNumberItem setSubmenu:copySubmenu];
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400556
557 NSMenuItem* callItems = [[NSMenuItem alloc] init];
558 [callItems setTitle:NSLocalizedString(@"Call number", @"Contextual menu action")];
559 [callItems setSubmenu:callSubmenu];
560
Alexandre Lision72a669e2016-09-14 17:52:16 -0400561 [theMenu insertItem:copyNumberItem atIndex:theMenu.itemArray.count];
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400562 [theMenu insertItem:[NSMenuItem separatorItem] atIndex:theMenu.itemArray.count];
563 [theMenu insertItem:callItems atIndex:theMenu.itemArray.count];
564
565 return theMenu;
Alexandre Lision61db3552015-10-22 19:12:52 -0400566}
Olivier Soldanod4311552017-11-20 15:09:53 -0500567#endif
Alexandre Lision61db3552015-10-22 19:12:52 -0400568
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400569@end