blob: 25d0fb2d69d78036072c3e0901eb34ad1bacce75 [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
Andreas Traczyk252a94a2018-04-20 16:36:20 -040024//std
25#import <sstream>
26
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040027//Qt
28#import <QtMacExtras/qmacfunctions.h>
29#import <QPixmap>
30#import <QIdentityProxyModel>
31#import <QItemSelectionModel>
32
33//LRC
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040034#import <globalinstances.h>
Olivier Soldanod4311552017-11-20 15:09:53 -050035#import <api/newaccountmodel.h>
36#import <api/conversationmodel.h>
37#import <api/account.h>
38#import <api/contact.h>
39#import <api/contactmodel.h>
40#import <api/newcallmodel.h>
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040041
42#import "QNSTreeController.h"
43#import "delegates/ImageManipulationDelegate.h"
Alexandre Lision4e280d62015-09-09 15:56:30 -040044#import "views/HoverTableRowView.h"
Alexandre Lision61db3552015-10-22 19:12:52 -040045#import "PersonLinkerVC.h"
Alexandre Lision4baba4c2016-02-11 13:00:57 -050046#import "views/IconButton.h"
Olivier Soldanod4311552017-11-20 15:09:53 -050047#import "views/RingTableView.h"
Alexandre Lision4e280d62015-09-09 15:56:30 -040048#import "views/ContextualTableCellView.h"
Olivier Soldanod4311552017-11-20 15:09:53 -050049#import "utils.h"
Anthony Léonardbee94cc2018-01-16 11:42:40 -050050#import "RingWindowController.h"
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040051
Olivier Soldanod4311552017-11-20 15:09:53 -050052@interface SmartViewVC () <NSTableViewDelegate, NSTableViewDataSource, NSPopoverDelegate, ContextMenuDelegate, ContactLinkedDelegate, KeyboardShortcutDelegate> {
Alexandre Lision0f66bd32016-01-18 11:30:45 -050053
Alexandre Lision61db3552015-10-22 19:12:52 -040054 NSPopover* addToContactPopover;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040055
56 //UI elements
Olivier Soldanod4311552017-11-20 15:09:53 -050057 __unsafe_unretained IBOutlet RingTableView* smartView;
Alexandre Lision61db3552015-10-22 19:12:52 -040058 __unsafe_unretained IBOutlet NSSearchField* searchField;
Anthony Léonardcb73a2c2018-01-18 11:45:56 -050059 __strong IBOutlet NSSegmentedControl *listTypeSelector;
Andreas Traczyk252a94a2018-04-20 16:36:20 -040060 __strong IBOutlet NSLayoutConstraint *listTypeSelectorHeight;
Anthony Léonardcb73a2c2018-01-18 11:45:56 -050061 bool selectorIsPresent;
Alexandre Lision45c18672016-11-02 15:24:45 -040062
Andreas Traczyk252a94a2018-04-20 16:36:20 -040063 QMetaObject::Connection modelSortedConnection_, modelUpdatedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_, newInteractionConnection_, interactionStatusUpdatedConnection_, conversationClearedConnection;
Olivier Soldanod4311552017-11-20 15:09:53 -050064
Andreas Traczyk252a94a2018-04-20 16:36:20 -040065 lrc::api::ConversationModel* convModel_;
Olivier Soldanod4311552017-11-20 15:09:53 -050066 std::string selectedUid_;
Anthony Léonard4bfd68b2018-01-15 16:23:50 -050067 lrc::api::profile::Type currentFilterType;
Anthony Léonardbee94cc2018-01-16 11:42:40 -050068
69 __unsafe_unretained IBOutlet RingWindowController *delegate;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040070}
71
72@end
73
74@implementation SmartViewVC
75
Kateryna Kostiuk882cbac2017-07-05 17:29:00 -040076@synthesize tabbar;
77
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040078// Tags for views
Alexandre Lision57914fa2016-02-10 14:52:24 -050079NSInteger const IMAGE_TAG = 100;
80NSInteger const DISPLAYNAME_TAG = 200;
Andreas Traczyk252a94a2018-04-20 16:36:20 -040081NSInteger const NOTIFICATONS_TAG = 300;
82NSInteger const RING_ID_LABEL = 400;
83NSInteger const PRESENCE_TAG = 500;
84NSInteger const TOTALMSGS_TAG = 600;
85NSInteger const TOTALINVITES_TAG = 700;
86NSInteger const DATE_TAG = 800;
87NSInteger const SNIPPET_TAG = 900;
88NSInteger const ADD_BUTTON_TAG = 1000;
89NSInteger const REFUSE_BUTTON_TAG = 1100;
90NSInteger const BLOCK_BUTTON_TAG = 1200;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040091
Anthony Léonarde6a01fa2018-01-12 10:29:58 -050092// Segment indices for smartlist selector
93NSInteger const CONVERSATION_SEG = 0;
94NSInteger const REQUEST_SEG = 1;
95
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040096- (void)awakeFromNib
97{
98 NSLog(@"INIT SmartView VC");
Olivier Soldanod4311552017-11-20 15:09:53 -050099 //get selected account
100 //encapsulate conversationmodel in local version
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400101
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400102 [smartView setTarget:self];
103 [smartView setDoubleAction:@selector(placeCall:)];
104
Alexandre Lision61db3552015-10-22 19:12:52 -0400105 [smartView setContextMenuDelegate:self];
106 [smartView setShortcutsDelegate:self];
107
Olivier Soldanod4311552017-11-20 15:09:53 -0500108 [smartView setDataSource: self];
Kateryna Kostiuk1f705ab2017-04-18 12:45:59 -0400109
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400110 [self.view setWantsLayer:YES];
111 [self.view setLayer:[CALayer layer]];
112 [self.view.layer setBackgroundColor:[NSColor whiteColor].CGColor];
113
114 [searchField setWantsLayer:YES];
115 [searchField setLayer:[CALayer layer]];
116 [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 -0500117
118 currentFilterType = lrc::api::profile::Type::RING;
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500119 selectorIsPresent = true;
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400120
121 smartView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleNone;
Kateryna Kostiuk67735232018-05-10 15:05:32 -0400122
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400123}
124
125- (void)placeCall:(id)sender
126{
Olivier Soldanod4311552017-11-20 15:09:53 -0500127 NSInteger row;
128 if (sender != nil && [sender clickedRow] != -1)
129 row = [sender clickedRow];
130 else if ([smartView selectedRow] != -1)
131 row = [smartView selectedRow];
132 else
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400133 return;
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400134 if (convModel_ == nil)
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400135 return;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400136
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400137 auto conv = convModel_->filteredConversation(row);
138 convModel_->placeCall(conv.uid);
139}
140
141-(void) reloadSelectorNotifications
142{
143 NSTextField* totalMsgsCount = [self.view viewWithTag:TOTALMSGS_TAG];
144 NSTextField* totalInvites = [self.view viewWithTag:TOTALINVITES_TAG];
145
146 if (!selectorIsPresent) {
147 [totalMsgsCount setHidden:true];
148 [totalInvites setHidden:true];
149 return;
150 }
151
152 auto ringConversations = convModel_->getFilteredConversations(lrc::api::profile::Type::RING);
153 int totalUnreadMessages = 0;
154 std::for_each(ringConversations.begin(), ringConversations.end(),
155 [&totalUnreadMessages, self] (const auto& conversation) {
156 totalUnreadMessages += convModel_->getNumberOfUnreadMessagesFor(conversation.uid);
157 });
158 [totalMsgsCount setHidden:(totalUnreadMessages == 0)];
159 [totalMsgsCount setIntValue:totalUnreadMessages];
160
161 auto totalRequests = [self chosenAccount].contactModel->pendingRequestCount();
162 [totalInvites setHidden:(totalRequests == 0)];
163 [totalInvites setIntValue:totalRequests];
Olivier Soldanod4311552017-11-20 15:09:53 -0500164}
165
166-(void) reloadData
167{
Andreas Traczyk3b98eb82018-03-29 17:54:42 -0400168 NSLog(@"reload");
Olivier Soldanod4311552017-11-20 15:09:53 -0500169 [smartView deselectAll:nil];
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400170 if (convModel_ == nil)
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400171 return;
Olivier Soldanod4311552017-11-20 15:09:53 -0500172
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400173 [self reloadSelectorNotifications];
174
175 if (!convModel_->owner.contactModel->hasPendingRequests()) {
Anthony Léonard4bfd68b2018-01-15 16:23:50 -0500176 if (currentFilterType == lrc::api::profile::Type::PENDING) {
177 [self selectConversationList];
178 }
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500179 if (selectorIsPresent) {
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400180 listTypeSelectorHeight.constant = 0.0;
181 [listTypeSelector setHidden:YES];
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500182 selectorIsPresent = false;
183 }
Anthony Léonard4bfd68b2018-01-15 16:23:50 -0500184 } else {
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500185 if (!selectorIsPresent) {
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500186 [listTypeSelector setSelected:YES forSegment:CONVERSATION_SEG];
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400187 listTypeSelectorHeight.constant = 18.0;
188 [listTypeSelector setHidden:NO];
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500189 selectorIsPresent = true;
190 }
Anthony Léonard4bfd68b2018-01-15 16:23:50 -0500191 }
192
Anthony Léonardcb73a2c2018-01-18 11:45:56 -0500193 [smartView reloadData];
194
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400195 if (!selectedUid_.empty() && convModel_ != nil) {
196 auto it = getConversationFromUid(selectedUid_, *convModel_);
197 if (it != convModel_->allFilteredConversations().end()) {
198 NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - convModel_->allFilteredConversations().begin())];
Olivier Soldanod4311552017-11-20 15:09:53 -0500199 [smartView selectRowIndexes:indexSet byExtendingSelection:NO];
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400200 }
201 }
202
Olivier Soldanod4311552017-11-20 15:09:53 -0500203 [smartView scrollToBeginningOfDocument:nil];
204}
205
Andreas Traczyk3b98eb82018-03-29 17:54:42 -0400206-(void) reloadConversationWithUid:(NSString *)uid
207{
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400208 if (convModel_ == nil) {
209 return;
210 }
211
212 auto it = getConversationFromUid(std::string([uid UTF8String]), *convModel_);
213 if (it != convModel_->allFilteredConversations().end()) {
214 NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - convModel_->allFilteredConversations().begin())];
215 NSLog(@"reloadConversationWithUid: %@", uid);
216 [smartView reloadDataForRowIndexes:indexSet
217 columnIndexes:[NSIndexSet indexSetWithIndex:0]];
Andreas Traczyk3b98eb82018-03-29 17:54:42 -0400218 }
219}
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400220
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500221- (BOOL)setConversationModel:(lrc::api::ConversationModel *)conversationModel
Olivier Soldanod4311552017-11-20 15:09:53 -0500222{
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400223 if (convModel_ == conversationModel) {
224 return false;
225 }
226
227 convModel_ = conversationModel;
228 selectedUid_.clear(); // Clear selected conversation as the selected account is being changed
229 QObject::disconnect(modelSortedConnection_);
230 QObject::disconnect(modelUpdatedConnection_);
231 QObject::disconnect(filterChangedConnection_);
232 QObject::disconnect(newConversationConnection_);
233 QObject::disconnect(conversationRemovedConnection_);
234 QObject::disconnect(conversationClearedConnection);
235 QObject::disconnect(interactionStatusUpdatedConnection_);
236 QObject::disconnect(newInteractionConnection_);
237 [self reloadData];
238 if (convModel_ != nil) {
239 modelSortedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted,
Olivier Soldanod4311552017-11-20 15:09:53 -0500240 [self] (){
241 [self reloadData];
242 });
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400243 modelUpdatedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationUpdated,
244 [self] (const std::string& convUid){
245 [self reloadConversationWithUid: [NSString stringWithUTF8String:convUid.c_str()]];
246 });
247 filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged,
248 [self] (){
249 [self reloadData];
250 });
251 newConversationConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newConversation,
252 [self] (const std::string& convUid) {
253 [self reloadData];
254 [self updateConversationForNewContact:[NSString stringWithUTF8String:convUid.c_str()]];
255 });
256 conversationRemovedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationRemoved,
257 [self] (){
258 [delegate listTypeChanged];
259 [self reloadData];
260 });
261 conversationClearedConnection = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationCleared,
262 [self] (const std::string& convUid){
263 [self deselect];
264 [delegate listTypeChanged];
265 });
266 interactionStatusUpdatedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::interactionStatusUpdated,
267 [self] (const std::string& convUid) {
268 if (convUid != selectedUid_)
269 return;
270 [self reloadConversationWithUid: [NSString stringWithUTF8String:convUid.c_str()]];
271 });
272 newInteractionConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newInteraction,
273 [self](const std::string& convUid, uint64_t interactionId, const lrc::api::interaction::Info& interaction){
274 if (convUid == selectedUid_) {
275 convModel_->clearUnreadInteractions(convUid);
276 }
277 });
278 convModel_->setFilter(""); // Reset the filter
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500279 }
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400280 [searchField setStringValue:@""];
281 return true;
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500282}
283
284-(void)selectConversation:(const lrc::api::conversation::Info&)conv model:(lrc::api::ConversationModel*)model;
285{
286 auto& uid = conv.uid;
287 if (selectedUid_ == uid)
288 return;
289
290 [self setConversationModel:model];
291
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400292 if (convModel_ == nil) {
293 return;
294 }
295
296 auto it = getConversationFromUid(selectedUid_, *convModel_);
297 if (it != convModel_->allFilteredConversations().end()) {
298 NSIndexSet* indexSet = [NSIndexSet indexSetWithIndex:(it - convModel_->allFilteredConversations().begin())];
299 [smartView selectRowIndexes:indexSet byExtendingSelection:NO];
300 selectedUid_ = uid;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400301 }
302}
303
Anthony Léonard8585cc02017-12-28 14:03:45 -0500304-(void)deselect
305{
306 selectedUid_.clear();
307 [smartView deselectAll:nil];
308}
309
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400310-(void) clearConversationModel {
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400311 convModel_ = nil;
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400312 [self deselect];
313 [smartView reloadData];
Andreas Traczyk4fdb2d92018-04-19 15:57:46 -0400314 if (selectorIsPresent) {
315 [listTypeSelector removeFromSuperview];
316 selectorIsPresent = false;
317 }
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400318}
319
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500320- (IBAction) listTypeChanged:(id)sender
321{
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400322 selectedUid_.clear();
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500323 NSInteger selectedItem = [sender selectedSegment];
Anthony Léonarde6a01fa2018-01-12 10:29:58 -0500324 switch (selectedItem) {
325 case CONVERSATION_SEG:
Anthony Léonardbee94cc2018-01-16 11:42:40 -0500326 if (currentFilterType != lrc::api::profile::Type::RING) {
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400327 convModel_->setFilter(lrc::api::profile::Type::RING);
Anthony Léonardbee94cc2018-01-16 11:42:40 -0500328 [delegate listTypeChanged];
329 currentFilterType = lrc::api::profile::Type::RING;
330 }
Anthony Léonarde6a01fa2018-01-12 10:29:58 -0500331 break;
332 case REQUEST_SEG:
Anthony Léonardbee94cc2018-01-16 11:42:40 -0500333 if (currentFilterType != lrc::api::profile::Type::PENDING) {
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400334 convModel_->setFilter(lrc::api::profile::Type::PENDING);
Anthony Léonardbee94cc2018-01-16 11:42:40 -0500335 [delegate listTypeChanged];
336 currentFilterType = lrc::api::profile::Type::PENDING;
337 }
Anthony Léonarde6a01fa2018-01-12 10:29:58 -0500338 break;
339 default:
340 NSLog(@"Invalid item selected in list selector: %d", selectedItem);
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500341 }
342}
343
344-(void) selectConversationList
345{
Anthony Léonardd00cd182018-01-17 09:21:27 -0500346 if (currentFilterType == lrc::api::profile::Type::RING)
347 return;
Anthony Léonarde6a01fa2018-01-12 10:29:58 -0500348 [listTypeSelector setSelectedSegment:CONVERSATION_SEG];
Anthony Léonard4bfd68b2018-01-15 16:23:50 -0500349
350 // Do not invert order of the next two lines or stack overflow
351 // may happen on -(void) reloadData call if filter is currently set to PENDING
352 currentFilterType = lrc::api::profile::Type::RING;
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400353 convModel_->setFilter(lrc::api::profile::Type::RING);
354 convModel_->setFilter("");
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500355}
356
Anthony Léonardd00cd182018-01-17 09:21:27 -0500357-(void) selectPendingList
358{
359 if (currentFilterType == lrc::api::profile::Type::PENDING)
360 return;
361 [listTypeSelector setSelectedSegment:REQUEST_SEG];
362
363 currentFilterType = lrc::api::profile::Type::PENDING;
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400364 convModel_->setFilter(lrc::api::profile::Type::PENDING);
365 convModel_->setFilter("");
Anthony Léonardd00cd182018-01-17 09:21:27 -0500366}
367
Olivier Soldanod4311552017-11-20 15:09:53 -0500368#pragma mark - NSTableViewDelegate methods
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400369
Olivier Soldanod4311552017-11-20 15:09:53 -0500370- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400371{
372 return YES;
373}
374
Olivier Soldanod4311552017-11-20 15:09:53 -0500375- (BOOL)tableView:(NSTableView *)tableView shouldEditTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400376{
377 return NO;
378}
379
Olivier Soldanod4311552017-11-20 15:09:53 -0500380- (void)tableViewSelectionDidChange:(NSNotification *)notification
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400381{
Olivier Soldanod4311552017-11-20 15:09:53 -0500382 NSInteger row = [notification.object selectedRow];
Kateryna Kostiuk82c0d532018-05-09 16:49:06 -0400383 NSInteger rows = [smartView numberOfRows];
Olivier Soldanod4311552017-11-20 15:09:53 -0500384
Kateryna Kostiuk82c0d532018-05-09 16:49:06 -0400385 for (int i = 0; i< rows; i++) {
386 NSTableRowView* cellRowView = [smartView rowViewAtRow:i makeIfNecessary:YES];
387 if (i == row) {
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400388 cellRowView.backgroundColor = [NSColor controlColor];
Kateryna Kostiuk82c0d532018-05-09 16:49:06 -0400389 } else {
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400390 cellRowView.backgroundColor = [NSColor whiteColor];
391 }
Kateryna Kostiuk82c0d532018-05-09 16:49:06 -0400392 }
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400393
Olivier Soldanod4311552017-11-20 15:09:53 -0500394 if (row == -1)
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400395 return;
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400396 if (convModel_ == nil)
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400397 return;
Olivier Soldanod4311552017-11-20 15:09:53 -0500398
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400399 auto uid = convModel_->filteredConversation(row).uid;
Olivier Soldanod4311552017-11-20 15:09:53 -0500400 if (selectedUid_ != uid) {
401 selectedUid_ = uid;
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400402 convModel_->selectConversation(uid);
403 convModel_->clearUnreadInteractions(uid);
404 [self reloadSelectorNotifications];
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400405 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400406}
407
Olivier Soldanod4311552017-11-20 15:09:53 -0500408- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400409{
Olivier Soldanod4311552017-11-20 15:09:53 -0500410 return [tableView makeViewWithIdentifier:@"HoverRowView" owner:nil];
411}
Alexandre Lisiona4bb0512016-04-19 18:04:31 -0400412
Olivier Soldanod4311552017-11-20 15:09:53 -0500413- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
414{
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400415 if (convModel_ == nil)
Olivier Soldanod4311552017-11-20 15:09:53 -0500416 return nil;
417
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400418 auto conversation = convModel_->filteredConversation(row);
Alexandre Lisiona4bb0512016-04-19 18:04:31 -0400419 NSTableCellView* result;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400420
Olivier Soldanod4311552017-11-20 15:09:53 -0500421 result = [tableView makeViewWithIdentifier:@"MainCell" owner:tableView];
Alexandre Lision4e280d62015-09-09 15:56:30 -0400422
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400423 NSTextField* unreadCount = [result viewWithTag:NOTIFICATONS_TAG];
Olivier Soldanod4311552017-11-20 15:09:53 -0500424 [unreadCount setHidden:(conversation.unreadMessages == 0)];
425 [unreadCount setIntValue:conversation.unreadMessages];
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400426 NSTextField* displayName = [result viewWithTag:DISPLAYNAME_TAG];
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400427 NSTextField* displayRingID = [result viewWithTag:RING_ID_LABEL];
Kateryna Kostiuk0bf03752018-05-09 13:57:15 -0400428 NSTextField* lastInteractionDate = [result viewWithTag:DATE_TAG];
429 NSTextField* interactionSnippet = [result viewWithTag:SNIPPET_TAG];
430 [displayName setStringValue:@""];
431 [displayRingID setStringValue:@""];
432 [lastInteractionDate setStringValue:@""];
433 [interactionSnippet setStringValue:@""];
434 NSImageView* photoView = [result viewWithTag:IMAGE_TAG];
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400435 NSString* displayNameString = bestNameForConversation(conversation, *convModel_);
436 NSString* displayIDString = bestIDForConversation(conversation, *convModel_);
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400437 if(displayNameString.length == 0 || [displayNameString isEqualToString:displayIDString]) {
438 [displayName setStringValue:displayIDString];
Anthony Léonard4adc3f02017-07-21 10:23:04 -0400439 [displayRingID setHidden:YES];
Kateryna Kostiuk9d6657a2017-04-25 13:42:25 -0400440 }
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400441 else {
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400442 [displayName setStringValue:displayNameString];
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400443 [displayRingID setStringValue:displayIDString];
Anthony Léonard4adc3f02017-07-21 10:23:04 -0400444 [displayRingID setHidden:NO];
Kateryna Kostiuk6a536362017-05-02 14:26:12 -0400445 }
Olivier Soldanod4311552017-11-20 15:09:53 -0500446 auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator());
Kateryna Kostiuk0bf03752018-05-09 13:57:15 -0400447 NSImage* image = QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(conversation, convModel_->owner)));
448 if(image) {
449 [NSLayoutConstraint deactivateConstraints:[photoView constraints]];
450 NSArray* constraints = [NSLayoutConstraint
451 constraintsWithVisualFormat:@"H:[photoView(54)]"
452 options:0
453 metrics:nil views:NSDictionaryOfVariableBindings(photoView)];
454 [NSLayoutConstraint activateConstraints:constraints];
455 } else {
456 [NSLayoutConstraint deactivateConstraints:[photoView constraints]];
457 NSArray* constraints = [NSLayoutConstraint
458 constraintsWithVisualFormat:@"H:[photoView(0)]"
459 options:0
460 metrics:nil views:NSDictionaryOfVariableBindings(photoView)];
461 [NSLayoutConstraint activateConstraints:constraints];
462 }
463 [photoView setImage: image];
Kateryna Kostiukd9039e92017-05-24 14:29:54 -0400464
465 NSView* presenceView = [result viewWithTag:PRESENCE_TAG];
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400466 auto contact = convModel_->owner.contactModel->getContact(conversation.participants[0]);
467 if (contact.isPresent) {
Kateryna Kostiukd9039e92017-05-24 14:29:54 -0400468 [presenceView setHidden:NO];
469 } else {
470 [presenceView setHidden:YES];
471 }
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400472 NSButton* addContactButton = [result viewWithTag:ADD_BUTTON_TAG];
473 NSButton* refuseContactButton = [result viewWithTag:REFUSE_BUTTON_TAG];
474 NSButton* blockContactButton = [result viewWithTag:BLOCK_BUTTON_TAG];
475 [addContactButton setHidden:YES];
476 [refuseContactButton setHidden:YES];
477 [blockContactButton setHidden:YES];
478
479 if (profileType(conversation, *convModel_) == lrc::api::profile::Type::PENDING) {
480 [lastInteractionDate setHidden:true];
481 [interactionSnippet setHidden:true];
482 [addContactButton setHidden:NO];
483 [refuseContactButton setHidden:NO];
484 [blockContactButton setHidden:NO];
485 [addContactButton setAction:@selector(acceptInvitation:)];
486 [addContactButton setTarget:self];
487 [refuseContactButton setAction:@selector(refuseInvitation:)];
488 [refuseContactButton setTarget:self];
489 [blockContactButton setAction:@selector(blockPendingContact:)];
490 [blockContactButton setTarget:self];
491 return result;
492 }
493
494 [lastInteractionDate setHidden:false];
495
496 [interactionSnippet setHidden:false];
497
498 auto lastUid = conversation.lastMessageUid;
499 if (conversation.interactions.find(lastUid) != conversation.interactions.end()) {
500 // last interaction snippet
501 std::string lastInteractionSnippet = conversation.interactions[lastUid].body;
502 std::stringstream ss(lastInteractionSnippet);
503 std::getline(ss, lastInteractionSnippet);
504 NSString* lastInteractionSnippetFixedString = [[NSString stringWithUTF8String:lastInteractionSnippet.c_str()]
505 stringByReplacingOccurrencesOfString:@"🕽" withString:@""];
506 lastInteractionSnippetFixedString = [lastInteractionSnippetFixedString stringByReplacingOccurrencesOfString:@"📞" withString:@""];
507 if (conversation.interactions[lastUid].type == lrc::api::interaction::Type::OUTGOING_DATA_TRANSFER
508 || conversation.interactions[lastUid].type == lrc::api::interaction::Type::INCOMING_DATA_TRANSFER) {
Kateryna Kostiuk67735232018-05-10 15:05:32 -0400509 lastInteractionSnippetFixedString = [lastInteractionSnippetFixedString lastPathComponent];
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400510 }
511 [interactionSnippet setStringValue:lastInteractionSnippetFixedString];
512
513 // last interaction date/time
514 std::time_t lastInteractionTimestamp = conversation.interactions[lastUid].timestamp;
515 std::time_t now = std::time(nullptr);
516 char interactionDay[64];
517 char nowDay[64];
518 std::strftime(interactionDay, sizeof(interactionDay), "%D", std::localtime(&lastInteractionTimestamp));
519 std::strftime(nowDay, sizeof(nowDay), "%D", std::localtime(&now));
520 if (std::string(interactionDay) == std::string(nowDay)) {
521 char interactionTime[64];
522 std::strftime(interactionTime, sizeof(interactionTime), "%R", std::localtime(&lastInteractionTimestamp));
523 [lastInteractionDate setStringValue:[NSString stringWithUTF8String:interactionTime]];
524 } else {
525 [lastInteractionDate setStringValue:[NSString stringWithUTF8String:interactionDay]];
526 }
527 }
528
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400529 return result;
530}
531
Olivier Soldanod4311552017-11-20 15:09:53 -0500532- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500533{
Olivier Soldanod4311552017-11-20 15:09:53 -0500534 return 60.0;
535}
536
537#pragma mark - NSTableDataSource methods
538
539- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView
540{
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400541 if (tableView == smartView && convModel_ != nullptr) {
542 return convModel_->allFilteredConversations().size();
Olivier Soldanod4311552017-11-20 15:09:53 -0500543 }
544
545 return 0;
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500546}
547
Kateryna Kostiuk882cbac2017-07-05 17:29:00 -0400548- (void)startCallForRow:(id)sender {
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400549 NSInteger row = [smartView rowForView:sender];
550 [smartView selectRowIndexes:[NSIndexSet indexSetWithIndex:row] byExtendingSelection:NO];
551 [self placeCall:nil];
552}
553
554- (IBAction)hangUpClickedAtRow:(id)sender {
555 NSInteger row = [smartView rowForView:sender];
Alexandre Lision57914fa2016-02-10 14:52:24 -0500556
Olivier Soldanod4311552017-11-20 15:09:53 -0500557 if (row == -1)
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500558 return;
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400559 if (convModel_ == nil)
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400560 return;
Alexandre Lision45c18672016-11-02 15:24:45 -0400561
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400562 auto conv = convModel_->filteredConversation(row);
Olivier Soldanod4311552017-11-20 15:09:53 -0500563 auto& callId = conv.callId;
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500564
Olivier Soldanod4311552017-11-20 15:09:53 -0500565 if (callId.empty())
566 return;
567
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400568 auto* callModel = convModel_->owner.callModel.get();
Olivier Soldanod4311552017-11-20 15:09:53 -0500569 callModel->hangUp(callId);
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400570}
571
Alexandre Lision45c18672016-11-02 15:24:45 -0400572- (void) displayErrorModalWithTitle:(NSString*) title WithMessage:(NSString*) message
573{
574 NSAlert* alert = [NSAlert alertWithMessageText:title
575 defaultButton:@"Ok"
576 alternateButton:nil
577 otherButton:nil
578 informativeTextWithFormat:message];
579
580 [alert beginSheetModalForWindow:self.view.window modalDelegate:nil didEndSelector:NULL contextInfo:NULL];
581}
582
Olivier Soldanod4311552017-11-20 15:09:53 -0500583- (void) processSearchFieldInput
Alexandre Lision45c18672016-11-02 15:24:45 -0400584{
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400585 if (convModel_ == nil) {
586 return;
587 }
588
589 convModel_->setFilter(std::string([[searchField stringValue] UTF8String]));
Alexandre Lision45c18672016-11-02 15:24:45 -0400590}
591
Olivier Soldanod4311552017-11-20 15:09:53 -0500592-(const lrc::api::account::Info&) chosenAccount
Kateryna Kostiukd2f7b5d2017-06-09 15:30:49 -0400593{
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400594 return convModel_->owner;
Kateryna Kostiukd2f7b5d2017-06-09 15:30:49 -0400595}
596
Alexandre Lisionf909fb12016-11-03 12:35:36 -0400597- (void) clearSearchField
598{
599 [searchField setStringValue:@""];
Olivier Soldanod4311552017-11-20 15:09:53 -0500600 [self processSearchFieldInput];
Kateryna Kostiuk9dd759c2017-07-11 12:06:55 -0400601}
602
Kateryna Kostiuk22ce9362018-04-17 13:12:50 -0400603-(void)updateConversationForNewContact:(NSString *)uId {
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400604 if (convModel_ == nil) {
Kateryna Kostiuk22ce9362018-04-17 13:12:50 -0400605 return;
606 }
607 [self clearSearchField];
608 auto uid = std::string([uId UTF8String]);
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400609 auto it = getConversationFromUid(uid, *convModel_);
610 if (it != convModel_->allFilteredConversations().end()) {
Kateryna Kostiuk22ce9362018-04-17 13:12:50 -0400611 @try {
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400612 auto contact = convModel_->owner.contactModel->getContact(it->participants[0]);
Kateryna Kostiuk22ce9362018-04-17 13:12:50 -0400613 if (!contact.profileInfo.uri.empty() && contact.profileInfo.uri.compare(selectedUid_) == 0) {
614 selectedUid_ = uid;
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400615 convModel_->selectConversation(uid);
Kateryna Kostiuk22ce9362018-04-17 13:12:50 -0400616 }
617 } @catch (NSException *exception) {
618 return;
619 }
620 }
621}
622
Alexandre Lision72a669e2016-09-14 17:52:16 -0400623/**
624 Copy a NSString in the general Pasteboard
625
626 @param sender the NSObject containing the represented object to copy
627 */
628- (void) copyStringToPasteboard:(id) sender
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400629{
630 NSPasteboard *pasteBoard = [NSPasteboard generalPasteboard];
631 [pasteBoard declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
632 [pasteBoard setString:[sender representedObject] forType:NSStringPboardType];
633}
634
Alexandre Lisionbf0385e2015-10-22 17:36:28 -0400635#pragma NSTextFieldDelegate
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400636
637- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
638{
Kateryna Kostiuk0febcfa2018-04-04 11:14:51 -0400639 if (commandSelector != @selector(insertNewline:) || [[searchField stringValue] isEqual:@""]) {
640 return NO;
641 }
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400642 if (convModel_ == nil) {
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400643 [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")];
644 return NO;
645 }
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400646 if (convModel_->allFilteredConversations().size() <= 0) {
Kateryna Kostiuk0febcfa2018-04-04 11:14:51 -0400647 return YES;
648 }
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400649 auto model = convModel_->filteredConversation(0);
Kateryna Kostiuk0febcfa2018-04-04 11:14:51 -0400650 auto uid = model.uid;
651 if (selectedUid_ == uid) {
652 return YES;
653 }
654 @try {
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400655 auto contact = convModel_->owner.contactModel->getContact(model.participants[0]);
Kateryna Kostiuk0febcfa2018-04-04 11:14:51 -0400656 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 -0400657 return YES;
658 }
Kateryna Kostiuk0febcfa2018-04-04 11:14:51 -0400659 selectedUid_ = uid;
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400660 convModel_->selectConversation(uid);
Kateryna Kostiuk0febcfa2018-04-04 11:14:51 -0400661 [self.view.window makeFirstResponder: smartView];
Kateryna Kostiuk0febcfa2018-04-04 11:14:51 -0400662 return YES;
663 } @catch (NSException *exception) {
664 return YES;
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400665 }
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400666}
667
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500668- (void)controlTextDidChange:(NSNotification *) notification
669{
Olivier Soldanod4311552017-11-20 15:09:53 -0500670 [self processSearchFieldInput];
Alexandre Lisionac4f5b62016-02-17 13:53:12 -0500671}
672
Alexandre Lision61db3552015-10-22 19:12:52 -0400673#pragma mark - NSPopOverDelegate
674
675- (void)popoverDidClose:(NSNotification *)notification
676{
677 if (addToContactPopover != nullptr) {
678 [addToContactPopover performClose:self];
679 addToContactPopover = NULL;
680 }
681}
682
Alexandre Lisionbf0385e2015-10-22 17:36:28 -0400683
Alexandre Lision61db3552015-10-22 19:12:52 -0400684#pragma mark - ContactLinkedDelegate
685
686- (void)contactLinked
687{
688 if (addToContactPopover != nullptr) {
689 [addToContactPopover performClose:self];
690 addToContactPopover = NULL;
691 }
692}
693
694#pragma mark - KeyboardShortcutDelegate
695
696- (void) onAddShortcut
697{
Olivier Soldanod4311552017-11-20 15:09:53 -0500698 if ([smartView selectedRow] == -1)
Alexandre Lision61db3552015-10-22 19:12:52 -0400699 return;
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400700 if (convModel_ == nil)
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400701 return ;
Alexandre Lision61db3552015-10-22 19:12:52 -0400702
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400703 auto uid = convModel_->filteredConversation([smartView selectedRow]).uid;
704 convModel_->makePermanent(uid);
Alexandre Lision61db3552015-10-22 19:12:52 -0400705}
706
707#pragma mark - ContextMenuDelegate
708
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400709- (NSMenu*) contextualMenuForRow:(int) index
Alexandre Lision61db3552015-10-22 19:12:52 -0400710{
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400711 if (convModel_ == nil)
Alexandre Lision61db3552015-10-22 19:12:52 -0400712 return nil;
713
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400714 auto conversation = convModel_->filteredConversation(NSInteger(index));
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400715
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400716 @try {
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400717 auto contact = convModel_->owner.contactModel->getContact(conversation.participants[0]);
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400718 if (contact.profileInfo.type == lrc::api::profile::Type::INVALID) {
719 return nil;
720 }
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400721
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400722 BOOL isSIP = false;
723 BOOL isRingContact = false;
724 /* for SIP contact show only call menu options
725 * if contact does not have uri that is not RING contact
726 * for trusted Ring contact show option block contact
727 * for untrasted contact show option add contact
728 */
Alexandre Lision72a669e2016-09-14 17:52:16 -0400729
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400730 if (contact.profileInfo.type == lrc::api::profile::Type::SIP) {
731 isSIP = true;
732 } else if (contact.profileInfo.uri.empty()) {
733 return nil;
734 }
735
736 else if (contact.profileInfo.type == lrc::api::profile::Type::RING && contact.isTrusted == true) {
737 isRingContact = true;
738 }
739 auto conversationUD = conversation.uid;
740 NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@""];
741 NSString* conversationUID = @(conversationUD.c_str());
742 NSMenuItem* separator = [NSMenuItem separatorItem];
743 NSMenuItem* videoCallItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Place video call",
744 @"Contextual menu action")
745 action:@selector(videoCall:)
746 keyEquivalent:@""];
747 NSMenuItem* audioCallItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Place audio call",
748 @"Contextual menu action")
749 action:@selector(audioCall:)
750 keyEquivalent:@""];
751 [videoCallItem setRepresentedObject: conversationUID];
752 [audioCallItem setRepresentedObject: conversationUID];
753 [theMenu addItem:videoCallItem];
754 [theMenu addItem:audioCallItem];
755 if (isSIP == false) {
756 [theMenu addItem:separator];
757 NSMenuItem* clearConversationItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Clear conversation", @"Contextual menu action")
758 action:@selector(clearConversation:)
759 keyEquivalent:@""];
760 [clearConversationItem setRepresentedObject: conversationUID];
761 [theMenu addItem:clearConversationItem];
762 if(isRingContact) {
763 NSMenuItem* blockContactItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Block contact", @"Contextual menu action")
764 action:@selector(blockContact:)
765 keyEquivalent:@""];
766 [blockContactItem setRepresentedObject: conversationUID];
767 [theMenu addItem:blockContactItem];
768 } else {
769 NSMenuItem* addContactItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"Add to contacts", @"Contextual menu action")
770 action:@selector(addContact:)
771 keyEquivalent:@"A"];
772 [addContactItem setRepresentedObject: conversationUID];
773 [theMenu addItem:addContactItem];
774 }
775 }
776 return theMenu;
Alexandre Lision61db3552015-10-22 19:12:52 -0400777 }
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400778 @catch (NSException *exception) {
779 return nil;
Alexandre Lisiond7bf2882015-10-22 22:54:32 -0400780 }
Alexandre Lision61db3552015-10-22 19:12:52 -0400781}
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400782
783- (void) addContact: (NSMenuItem* ) item {
784 auto menuObject = item.representedObject;
785 if(menuObject == nil) {
786 return;
787 }
788 NSString * convUId = (NSString*)menuObject;
789 std::string conversationID = std::string([convUId UTF8String]);
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400790 convModel_->makePermanent(conversationID);
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400791}
792
793- (void) blockContact: (NSMenuItem* ) item {
794 auto menuObject = item.representedObject;
795 if(menuObject == nil) {
796 return;
797 }
798 NSString * convUId = (NSString*)menuObject;
799 std::string conversationID = std::string([convUId UTF8String]);
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400800 //convModel_->clearHistory(conversationID);
801 convModel_->removeConversation(conversationID, true);
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400802}
803
804- (void) audioCall: (NSMenuItem* ) item {
805 auto menuObject = item.representedObject;
806 if(menuObject == nil) {
807 return;
808 }
809 NSString * convUId = (NSString*)menuObject;
810 std::string conversationID = std::string([convUId UTF8String]);
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400811 convModel_->placeAudioOnlyCall(conversationID);
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400812
813}
814
815- (void) videoCall: (NSMenuItem* ) item {
816 auto menuObject = item.representedObject;
817 if(menuObject == nil) {
818 return;
819 }
820 NSString * convUId = (NSString*)menuObject;
821 std::string conversationID = std::string([convUId UTF8String]);
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400822 convModel_->placeCall(conversationID);
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400823}
824
825- (void) clearConversation:(NSMenuItem* ) item {
826 auto menuObject = item.representedObject;
827 if(menuObject == nil) {
828 return;
829 }
830 NSString * convUId = (NSString*)menuObject;
831 std::string conversationID = std::string([convUId UTF8String]);
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400832 convModel_->clearHistory(conversationID);
833}
834
835- (void)acceptInvitation:(id)sender {
836 NSInteger row = [smartView rowForView:sender];
837
838 if (row == -1)
839 return;
840 if (convModel_ == nil)
841 return;
842
843 auto conv = convModel_->filteredConversation(row);
844 auto& convID = conv.Info::uid;
845
846 if (convID.empty())
847 return;
848 convModel_->makePermanent(convID);
849}
850
851- (void)refuseInvitation:(id)sender {
852 NSInteger row = [smartView rowForView:sender];
853
854 if (row == -1)
855 return;
856 if (convModel_ == nil)
857 return;
858
859 auto conv = convModel_->filteredConversation(row);
860 auto& convID = conv.Info::uid;
861
862 if (convID.empty())
863 return;
864 convModel_->removeConversation(convID);
865}
866
867- (void)blockPendingContact:(id)sender {
868 NSInteger row = [smartView rowForView:sender];
869
870 if (row == -1)
871 return;
872 if (convModel_ == nil)
873 return;
874
875 auto conv = convModel_->filteredConversation(row);
876 auto& convID = conv.Info::uid;
877
878 if (convID.empty())
879 return;
880 convModel_->removeConversation(convID, true);
881 [self deselect];
882 [delegate listTypeChanged];
Kateryna Kostiukd51a3252018-04-04 09:20:37 -0400883}
Alexandre Lision61db3552015-10-22 19:12:52 -0400884
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400885@end