blob: b996a2bc7865b2eb2a9a173a6cd9f414520cfe6a [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
Andreas Traczyk3b98eb82018-03-29 17:54:42 -040059 QMetaObject::Connection modelSortedConnection_, modelUpdatedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_, interactionStatusUpdatedConnection_, conversationClearedConnection;
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;
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400123 if (model_ == nil)
124 return;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400125
Olivier Soldanod4311552017-11-20 15:09:53 -0500126 auto conv = model_->filteredConversation(row);
127 model_->placeCall(conv.uid);
128}
129
130-(void) reloadData
131{
Andreas Traczyk3b98eb82018-03-29 17:54:42 -0400132 NSLog(@"reload");
Olivier Soldanod4311552017-11-20 15:09:53 -0500133 [smartView deselectAll:nil];
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400134 if (model_ == nil)
135 return;
Olivier Soldanod4311552017-11-20 15:09:53 -0500136
Anthony Léonard4bfd68b2018-01-15 16:23:50 -0500137 if (!model_->owner.contactModel->hasPendingRequests()) {
138 if (currentFilterType == lrc::api::profile::Type::PENDING) {
139 [self selectConversationList];
140 }
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500141 if (selectorIsPresent) {
142 [listTypeSelector removeFromSuperview];
143 selectorIsPresent = false;
144 }
Anthony Léonard4bfd68b2018-01-15 16:23:50 -0500145 } else {
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500146 if (!selectorIsPresent) {
147 // First we restore the selector with selection on "Conversations"
148 [self.view addSubview:listTypeSelector];
149 [listTypeSelector setSelected:YES forSegment:CONVERSATION_SEG];
150
151 // Then constraints are recreated (as these are lost when calling removeFromSuperview)
152 [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[searchField]-8-[listTypeSelector]"
153 options:0
154 metrics:nil
155 views:NSDictionaryOfVariableBindings(searchField, listTypeSelector)]];
156 [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[listTypeSelector]-8-[tabbar]"
157 options:0
158 metrics:nil
159 views:NSDictionaryOfVariableBindings(listTypeSelector, tabbar)]];
160 [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[listTypeSelector]-20-|"
161 options:0
162 metrics:nil
163 views:NSDictionaryOfVariableBindings(listTypeSelector)]];
164 selectorIsPresent = true;
165 }
Anthony Léonard4bfd68b2018-01-15 16:23:50 -0500166 }
167
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500168 [smartView reloadData];
169
Olivier Soldanod4311552017-11-20 15:09:53 -0500170 if (!selectedUid_.empty() && model_ != nil) {
Anthony Léonard6f819752018-01-05 09:53:40 -0500171 auto it = getConversationFromUid(selectedUid_, *model_);
Olivier Soldanod4311552017-11-20 15:09:53 -0500172 if (it != model_->allFilteredConversations().end()) {
173 NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - model_->allFilteredConversations().begin())];
174 [smartView selectRowIndexes:indexSet byExtendingSelection:NO];
175 } else {
176 selectedUid_.clear();
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400177 }
178 }
179
Olivier Soldanod4311552017-11-20 15:09:53 -0500180 [smartView scrollToBeginningOfDocument:nil];
181}
182
Andreas Traczyk3b98eb82018-03-29 17:54:42 -0400183-(void) reloadConversationWithUid:(NSString *)uid
184{
185 if (model_ != nil) {
186 auto it = getConversationFromUid(std::string([uid UTF8String]), *model_);
187 if (it != model_->allFilteredConversations().end()) {
188 NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - model_->allFilteredConversations().begin())];
189 NSLog(@"reloadConversationWithUid: %@", uid);
190 [smartView reloadDataForRowIndexes:indexSet
191 columnIndexes:[NSIndexSet indexSetWithIndex:0]];
192 }
193 }
194}
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400195
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500196- (BOOL)setConversationModel:(lrc::api::ConversationModel *)conversationModel
Olivier Soldanod4311552017-11-20 15:09:53 -0500197{
198 if (model_ != conversationModel) {
199 model_ = conversationModel;
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500200 selectedUid_.clear(); // Clear selected conversation as the selected account is being changed
Olivier Soldanod4311552017-11-20 15:09:53 -0500201 QObject::disconnect(modelSortedConnection_);
Andreas Traczyk3b98eb82018-03-29 17:54:42 -0400202 QObject::disconnect(modelUpdatedConnection_);
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500203 QObject::disconnect(filterChangedConnection_);
Anthony Léonard01634102017-12-27 16:37:16 -0500204 QObject::disconnect(newConversationConnection_);
205 QObject::disconnect(conversationRemovedConnection_);
Anthony Léonarddd85dee2018-02-19 14:34:35 -0500206 QObject::disconnect(interactionStatusUpdatedConnection_);
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400207 QObject::disconnect(conversationClearedConnection);
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500208 [self reloadData];
Olivier Soldanod4311552017-11-20 15:09:53 -0500209 if (model_ != nil) {
210 modelSortedConnection_ = QObject::connect(model_, &lrc::api::ConversationModel::modelSorted,
211 [self] (){
212 [self reloadData];
213 });
Andreas Traczyk3b98eb82018-03-29 17:54:42 -0400214 modelUpdatedConnection_ = QObject::connect(model_, &lrc::api::ConversationModel::conversationUpdated,
215 [self] (const std::string& uid){
216 [self reloadConversationWithUid: [NSString stringWithUTF8String:uid.c_str()]];
217 });
Olivier Soldanod4311552017-11-20 15:09:53 -0500218 filterChangedConnection_ = QObject::connect(model_, &lrc::api::ConversationModel::filterChanged,
219 [self] (){
220 [self reloadData];
221 });
Anthony Léonard01634102017-12-27 16:37:16 -0500222 newConversationConnection_ = QObject::connect(model_, &lrc::api::ConversationModel::newConversation,
223 [self] (){
224 [self reloadData];
225 });
226 conversationRemovedConnection_ = QObject::connect(model_, &lrc::api::ConversationModel::conversationRemoved,
227 [self] (){
228 [self reloadData];
229 });
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400230 conversationClearedConnection = QObject::connect(model_, &lrc::api::ConversationModel::conversationCleared,
231 [self] (const std::string& id){
232 [self deselect];
233 [delegate listTypeChanged];
234 });
Anthony Léonarddd85dee2018-02-19 14:34:35 -0500235 interactionStatusUpdatedConnection_ = QObject::connect(model_, &lrc::api::ConversationModel::interactionStatusUpdated,
236 [self] (const std::string& convUid) {
237 if (convUid != selectedUid_)
238 return;
Andreas Traczyk46d53af2018-04-13 16:00:17 -0400239 [self reloadConversationWithUid: [NSString stringWithUTF8String:convUid.c_str()]];
Anthony Léonarddd85dee2018-02-19 14:34:35 -0500240 });
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500241 model_->setFilter(""); // Reset the filter
242 }
243 [searchField setStringValue:@""];
244 return YES;
245 }
246 return NO;
247}
248
249-(void)selectConversation:(const lrc::api::conversation::Info&)conv model:(lrc::api::ConversationModel*)model;
250{
251 auto& uid = conv.uid;
252 if (selectedUid_ == uid)
253 return;
254
255 [self setConversationModel:model];
256
257 if (model_ != nil) {
Anthony Léonard6f819752018-01-05 09:53:40 -0500258 auto it = getConversationFromUid(selectedUid_, *model_);
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500259 if (it != model_->allFilteredConversations().end()) {
260 NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - model_->allFilteredConversations().begin())];
261 [smartView selectRowIndexes:indexSet byExtendingSelection:NO];
262 selectedUid_ = uid;
Olivier Soldanod4311552017-11-20 15:09:53 -0500263 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400264 }
265}
266
Anthony Léonard8585cc02017-12-28 14:03:45 -0500267-(void)deselect
268{
269 selectedUid_.clear();
270 [smartView deselectAll:nil];
271}
272
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400273-(void) clearConversationModel {
274 model_ = nil;
275 [self deselect];
276 [smartView reloadData];
277}
278
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500279- (IBAction) listTypeChanged:(id)sender
280{
281 NSInteger selectedItem = [sender selectedSegment];
Anthony Léonarde6a01fa2018-01-12 10:29:58 -0500282 switch (selectedItem) {
283 case CONVERSATION_SEG:
Anthony Léonardbee94cc2018-01-16 11:42:40 -0500284 if (currentFilterType != lrc::api::profile::Type::RING) {
285 model_->setFilter(lrc::api::profile::Type::RING);
286 [delegate listTypeChanged];
287 currentFilterType = lrc::api::profile::Type::RING;
288 }
Anthony Léonarde6a01fa2018-01-12 10:29:58 -0500289 break;
290 case REQUEST_SEG:
Anthony Léonardbee94cc2018-01-16 11:42:40 -0500291 if (currentFilterType != lrc::api::profile::Type::PENDING) {
292 model_->setFilter(lrc::api::profile::Type::PENDING);
293 [delegate listTypeChanged];
294 currentFilterType = lrc::api::profile::Type::PENDING;
295 }
Anthony Léonarde6a01fa2018-01-12 10:29:58 -0500296 break;
297 default:
298 NSLog(@"Invalid item selected in list selector: %d", selectedItem);
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500299 }
300}
301
302-(void) selectConversationList
303{
Anthony Léonardd00cd182018-01-17 09:21:27 -0500304 if (currentFilterType == lrc::api::profile::Type::RING)
305 return;
Anthony Léonarde6a01fa2018-01-12 10:29:58 -0500306 [listTypeSelector setSelectedSegment:CONVERSATION_SEG];
Anthony Léonard4bfd68b2018-01-15 16:23:50 -0500307
308 // Do not invert order of the next two lines or stack overflow
309 // may happen on -(void) reloadData call if filter is currently set to PENDING
310 currentFilterType = lrc::api::profile::Type::RING;
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500311 model_->setFilter(lrc::api::profile::Type::RING);
Anthony Léonard4bfd68b2018-01-15 16:23:50 -0500312 model_->setFilter("");
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500313}
314
Anthony Léonardd00cd182018-01-17 09:21:27 -0500315-(void) selectPendingList
316{
317 if (currentFilterType == lrc::api::profile::Type::PENDING)
318 return;
319 [listTypeSelector setSelectedSegment:REQUEST_SEG];
320
321 currentFilterType = lrc::api::profile::Type::PENDING;
322 model_->setFilter(lrc::api::profile::Type::PENDING);
323 model_->setFilter("");
324}
325
Olivier Soldanod4311552017-11-20 15:09:53 -0500326#pragma mark - NSTableViewDelegate methods
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400327
Olivier Soldanod4311552017-11-20 15:09:53 -0500328- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400329{
330 return YES;
331}
332
Olivier Soldanod4311552017-11-20 15:09:53 -0500333- (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400334{
335 return NO;
336}
337
Olivier Soldanod4311552017-11-20 15:09:53 -0500338- (void)tableViewSelectionDidChange:(NSNotification *)notification
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400339{
Olivier Soldanod4311552017-11-20 15:09:53 -0500340 NSInteger row = [notification.object selectedRow];
341
342 if (row == -1)
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400343 return;
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400344 if (model_ == nil)
345 return;
Olivier Soldanod4311552017-11-20 15:09:53 -0500346
347 auto uid = model_->filteredConversation(row).uid;
348 if (selectedUid_ != uid) {
349 selectedUid_ = uid;
350 model_->selectConversation(uid);
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400351 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400352}
353
Olivier Soldanod4311552017-11-20 15:09:53 -0500354- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400355{
Olivier Soldanod4311552017-11-20 15:09:53 -0500356 return [tableView makeViewWithIdentifier:@"HoverRowView" owner:nil];
357}
Alexandre Lisiona4bb0512016-04-19 18:04:31 -0400358
Olivier Soldanod4311552017-11-20 15:09:53 -0500359- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
360{
361 if (model_ == nil)
362 return nil;
363
364 auto conversation = model_->filteredConversation(row);
Alexandre Lisiona4bb0512016-04-19 18:04:31 -0400365 NSTableCellView* result;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400366
Olivier Soldanod4311552017-11-20 15:09:53 -0500367 result = [tableView makeViewWithIdentifier:@"MainCell" owner:tableView];
368// NSTextField* details = [result viewWithTag:DETAILS_TAG];
Alexandre Lision4e280d62015-09-09 15:56:30 -0400369
Olivier Soldanod4311552017-11-20 15:09:53 -0500370 NSMutableArray* controls = [NSMutableArray arrayWithObject:[result viewWithTag:CALL_BUTTON_TAG]];
371 [((ContextualTableCellView*) result) setContextualsControls:controls];
372 [((ContextualTableCellView*) result) setShouldBlurParentView:YES];
Alexandre Lision21666f32015-09-22 17:04:36 -0400373
Andreas Traczyk3b98eb82018-03-29 17:54:42 -0400374 // if (auto call = RecentModel::instance().getActiveCall(qIdx)) {
375 // [details setStringValue:call->roleData((int)Ring::Role::FormattedState).toString().toNSString()];
376 // [((ContextualTableCellView*) result) setActiveState:YES];
377 // } else {
378 // [details setStringValue:qIdx.data((int)Ring::Role::FormattedLastUsed).toString().toNSString()];
379 // [((ContextualTableCellView*) result) setActiveState:NO];
380 // }
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500381
Olivier Soldanod4311552017-11-20 15:09:53 -0500382 NSTextField* unreadCount = [result viewWithTag:TXT_BUTTON_TAG];
383 [unreadCount setHidden:(conversation.unreadMessages == 0)];
384 [unreadCount setIntValue:conversation.unreadMessages];
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400385
386 NSTextField* displayName = [result viewWithTag:DISPLAYNAME_TAG];
Olivier Soldanod4311552017-11-20 15:09:53 -0500387 NSString* displayNameString = bestNameForConversation(conversation, *model_);
388 NSString* displayIDString = bestIDForConversation(conversation, *model_);
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400389 if(displayNameString.length == 0 || [displayNameString isEqualToString:displayIDString]) {
Anthony Léonard4adc3f02017-07-21 10:23:04 -0400390 NSTextField* displayRingID = [result viewWithTag:RING_ID_LABEL];
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400391 [displayName setStringValue:displayIDString];
Anthony Léonard4adc3f02017-07-21 10:23:04 -0400392 [displayRingID setHidden:YES];
Kateryna Kostiuk9d6657a2017-04-25 13:42:25 -0400393 }
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400394 else {
395 NSTextField* displayRingID = [result viewWithTag:RING_ID_LABEL];
396 [displayName setStringValue:displayNameString];
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400397 [displayRingID setStringValue:displayIDString];
Anthony Léonard4adc3f02017-07-21 10:23:04 -0400398 [displayRingID setHidden:NO];
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400399 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400400 NSImageView* photoView = [result viewWithTag:IMAGE_TAG];
Alexandre Lision43e91bc2016-04-19 18:04:52 -0400401
Olivier Soldanod4311552017-11-20 15:09:53 -0500402 auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator());
403 [photoView setImage:QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(conversation, model_->owner)))];
Kateryna Kostiukd9039e92017-05-24 14:29:54 -0400404
405 NSView* presenceView = [result viewWithTag:PRESENCE_TAG];
Olivier Soldanod4311552017-11-20 15:09:53 -0500406 if (model_->owner.contactModel->getContact(conversation.participants[0]).isPresent) {
Kateryna Kostiukd9039e92017-05-24 14:29:54 -0400407 [presenceView setHidden:NO];
408 } else {
409 [presenceView setHidden:YES];
410 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400411 return result;
412}
413
Olivier Soldanod4311552017-11-20 15:09:53 -0500414- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500415{
Olivier Soldanod4311552017-11-20 15:09:53 -0500416 return 60.0;
417}
418
419#pragma mark - NSTableDataSource methods
420
421- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
422{
423 if (tableView == smartView) {
424 if (model_ != nullptr)
425 return model_->allFilteredConversations().size();
426 }
427
428 return 0;
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500429}
430
Kateryna Kostiuk882cbac2017-07-05 17:29:00 -0400431- (void)startCallForRow:(id)sender {
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400432 NSInteger row = [smartView rowForView:sender];
433 [smartView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
434 [self placeCall:nil];
435}
436
437- (IBAction)hangUpClickedAtRow:(id)sender {
438 NSInteger row = [smartView rowForView:sender];
Alexandre Lision57914fa2016-02-10 14:52:24 -0500439
Olivier Soldanod4311552017-11-20 15:09:53 -0500440 if (row == -1)
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500441 return;
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400442 if (model_ == nil)
443 return;
Alexandre Lision45c18672016-11-02 15:24:45 -0400444
Olivier Soldanod4311552017-11-20 15:09:53 -0500445 auto conv = model_->filteredConversation(row);
446 auto& callId = conv.callId;
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500447
Olivier Soldanod4311552017-11-20 15:09:53 -0500448 if (callId.empty())
449 return;
450
451 auto* callModel = model_->owner.callModel.get();
452 callModel->hangUp(callId);
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400453}
454
Alexandre Lision45c18672016-11-02 15:24:45 -0400455- (void) displayErrorModalWithTitle:(NSString*) title WithMessage:(NSString*) message
456{
457 NSAlert* alert = [NSAlert alertWithMessageText:title
458 defaultButton:@"Ok"
459 alternateButton:nil
460 otherButton:nil
461 informativeTextWithFormat:message];
462
463 [alert beginSheetModalForWindow:self.view.window modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
464}
465
Olivier Soldanod4311552017-11-20 15:09:53 -0500466- (void) processSearchFieldInput
Alexandre Lision45c18672016-11-02 15:24:45 -0400467{
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400468 if (model_ == nil)
469 return;
Olivier Soldanod4311552017-11-20 15:09:53 -0500470 model_->setFilter(std::string([[searchField stringValue] UTF8String]));
Alexandre Lision45c18672016-11-02 15:24:45 -0400471}
472
Olivier Soldanod4311552017-11-20 15:09:53 -0500473-(const lrc::api::account::Info&) chosenAccount
Kateryna Kostiukd2f7b5d2017-06-09 15:30:49 -0400474{
Olivier Soldanod4311552017-11-20 15:09:53 -0500475 return model_->owner;
Kateryna Kostiukd2f7b5d2017-06-09 15:30:49 -0400476}
477
Alexandre Lisionf909fb12016-11-03 12:35:36 -0400478- (void) clearSearchField
479{
480 [searchField setStringValue:@""];
Olivier Soldanod4311552017-11-20 15:09:53 -0500481 [self processSearchFieldInput];
Kateryna Kostiuk9dd759c2017-07-11 12:06:55 -0400482}
483
Alexandre Lision72a669e2016-09-14 17:52:16 -0400484/**
485 Copy a NSString in the general Pasteboard
486
487 @param sender the NSObject containing the represented object to copy
488 */
489- (void) copyStringToPasteboard:(id) sender
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400490{
491 NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
492 [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
493 [pasteBoard setString:[sender representedObject] forType:NSStringPboardType];
494}
495
Alexandre Lisionbf0385e2015-10-22 17:36:28 -0400496#pragma NSTextFieldDelegate
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400497
498- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
499{
Kateryna Kostiuk0febcfa2018-04-04 11:14:51 -0400500 if (commandSelector != @selector(insertNewline:) || [[searchField stringValue] isEqual:@""]) {
501 return NO;
502 }
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400503 if (model_ == nil) {
504 [self displayErrorModalWithTitle:NSLocalizedString(@"No account available", @"Displayed as RingID when no accounts are available for selection") WithMessage:NSLocalizedString(@"Navigate to preferences to create a new account", @"Allert message when no accounts are available")];
505 return NO;
506 }
Kateryna Kostiuk0febcfa2018-04-04 11:14:51 -0400507 if (model_->allFilteredConversations().size() <= 0) {
508 return YES;
509 }
510 auto model = model_->filteredConversation(0);
511 auto uid = model.uid;
512 if (selectedUid_ == uid) {
513 return YES;
514 }
515 @try {
516 auto contact = model_->owner.contactModel->getContact(model.participants[0]);
517 if ((contact.profileInfo.uri.empty() && contact.profileInfo.type != lrc::api::profile::Type::SIP) || contact.profileInfo.type == lrc::api::profile::Type::INVALID) {
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400518 return YES;
519 }
Kateryna Kostiuk0febcfa2018-04-04 11:14:51 -0400520 selectedUid_ = uid;
521 model_->selectConversation(uid);
522 [self.view.window makeFirstResponder: smartView];
523 searchField.stringValue = @"";
524 return YES;
525 } @catch (NSException *exception) {
526 return YES;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400527 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400528}
529
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500530- (void)controlTextDidChange:(NSNotification *) notification
531{
Olivier Soldanod4311552017-11-20 15:09:53 -0500532 [self processSearchFieldInput];
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500533}
534
Alexandre Lision61db3552015-10-22 19:12:52 -0400535#pragma mark - NSPopOverDelegate
536
537- (void)popoverDidClose:(NSNotification *)notification
538{
539 if (addToContactPopover != nullptr) {
540 [addToContactPopover performClose:self];
541 addToContactPopover = NULL;
542 }
543}
544
Alexandre Lisionbf0385e2015-10-22 17:36:28 -0400545
Alexandre Lision61db3552015-10-22 19:12:52 -0400546#pragma mark - ContactLinkedDelegate
547
548- (void)contactLinked
549{
550 if (addToContactPopover != nullptr) {
551 [addToContactPopover performClose:self];
552 addToContactPopover = NULL;
553 }
554}
555
556#pragma mark - KeyboardShortcutDelegate
557
558- (void) onAddShortcut
559{
Olivier Soldanod4311552017-11-20 15:09:53 -0500560 if ([smartView selectedRow] == -1)
Alexandre Lision61db3552015-10-22 19:12:52 -0400561 return;
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400562 if (model_ == nil)
563 return ;
Alexandre Lision61db3552015-10-22 19:12:52 -0400564
Olivier Soldanod4311552017-11-20 15:09:53 -0500565 auto uid = model_->filteredConversation([smartView selectedRow]).uid;
566 model_->makePermanent(uid);
Alexandre Lision61db3552015-10-22 19:12:52 -0400567}
568
569#pragma mark - ContextMenuDelegate
570
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400571- (NSMenu*) contextualMenuForRow:(int) index
Alexandre Lision61db3552015-10-22 19:12:52 -0400572{
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400573 if (model_ == nil)
Alexandre Lision61db3552015-10-22 19:12:52 -0400574 return nil;
575
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400576 auto conversation = model_->filteredConversation(NSInteger(index));
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400577
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400578 @try {
579 auto contact = model_->owner.contactModel->getContact(conversation.participants[0]);
580 if (contact.profileInfo.type == lrc::api::profile::Type::INVALID) {
581 return nil;
582 }
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400583
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400584 BOOL isSIP = false;
585 BOOL isRingContact = false;
586 /* for SIP contact show only call menu options
587 * if contact does not have uri that is not RING contact
588 * for trusted Ring contact show option block contact
589 * for untrasted contact show option add contact
590 */
Alexandre Lision72a669e2016-09-14 17:52:16 -0400591
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400592 if (contact.profileInfo.type == lrc::api::profile::Type::SIP) {
593 isSIP = true;
594 } else if (contact.profileInfo.uri.empty()) {
595 return nil;
596 }
597
598 else if (contact.profileInfo.type == lrc::api::profile::Type::RING && contact.isTrusted == true) {
599 isRingContact = true;
600 }
601 auto conversationUD = conversation.uid;
602 NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@""];
603 NSString* conversationUID = @(conversationUD.c_str());
604 NSMenuItem* separator = [NSMenuItem separatorItem];
605 NSMenuItem* videoCallItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Place video call",
606 @"Contextual menu action")
607 action:@selector(videoCall:)
608 keyEquivalent:@""];
609 NSMenuItem* audioCallItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Place audio call",
610 @"Contextual menu action")
611 action:@selector(audioCall:)
612 keyEquivalent:@""];
613 [videoCallItem setRepresentedObject: conversationUID];
614 [audioCallItem setRepresentedObject: conversationUID];
615 [theMenu addItem:videoCallItem];
616 [theMenu addItem:audioCallItem];
617 if (isSIP == false) {
618 [theMenu addItem:separator];
619 NSMenuItem* clearConversationItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Clear conversation", @"Contextual menu action")
620 action:@selector(clearConversation:)
621 keyEquivalent:@""];
622 [clearConversationItem setRepresentedObject: conversationUID];
623 [theMenu addItem:clearConversationItem];
624 if(isRingContact) {
625 NSMenuItem* blockContactItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Block contact", @"Contextual menu action")
626 action:@selector(blockContact:)
627 keyEquivalent:@""];
628 [blockContactItem setRepresentedObject: conversationUID];
629 [theMenu addItem:blockContactItem];
630 } else {
631 NSMenuItem* addContactItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Add to contacts", @"Contextual menu action")
632 action:@selector(addContact:)
633 keyEquivalent:@"A"];
634 [addContactItem setRepresentedObject: conversationUID];
635 [theMenu addItem:addContactItem];
636 }
637 }
638 return theMenu;
Alexandre Lision61db3552015-10-22 19:12:52 -0400639 }
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400640 @catch (NSException *exception) {
641 return nil;
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400642 }
Alexandre Lision61db3552015-10-22 19:12:52 -0400643}
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400644
645- (void) addContact: (NSMenuItem* ) item {
646 auto menuObject = item.representedObject;
647 if(menuObject == nil) {
648 return;
649 }
650 NSString * convUId = (NSString*)menuObject;
651 std::string conversationID = std::string([convUId UTF8String]);
652 model_->makePermanent(conversationID);
653}
654
655- (void) blockContact: (NSMenuItem* ) item {
656 auto menuObject = item.representedObject;
657 if(menuObject == nil) {
658 return;
659 }
660 NSString * convUId = (NSString*)menuObject;
661 std::string conversationID = std::string([convUId UTF8String]);
662 model_->clearHistory(conversationID);
663 model_->removeConversation(conversationID, true);
664}
665
666- (void) audioCall: (NSMenuItem* ) item {
667 auto menuObject = item.representedObject;
668 if(menuObject == nil) {
669 return;
670 }
671 NSString * convUId = (NSString*)menuObject;
672 std::string conversationID = std::string([convUId UTF8String]);
673 model_->placeAudioOnlyCall(conversationID);
674
675}
676
677- (void) videoCall: (NSMenuItem* ) item {
678 auto menuObject = item.representedObject;
679 if(menuObject == nil) {
680 return;
681 }
682 NSString * convUId = (NSString*)menuObject;
683 std::string conversationID = std::string([convUId UTF8String]);
684 model_->placeCall(conversationID);
685}
686
687- (void) clearConversation:(NSMenuItem* ) item {
688 auto menuObject = item.representedObject;
689 if(menuObject == nil) {
690 return;
691 }
692 NSString * convUId = (NSString*)menuObject;
693 std::string conversationID = std::string([convUId UTF8String]);
694 model_->clearHistory(conversationID);
695}
Alexandre Lision61db3552015-10-22 19:12:52 -0400696
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400697@end