blob: 6f27381ca20fb079de495545e807d3863286c174 [file] [log] [blame]
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -04001/*
Sébastien Blin029ffa82019-01-02 17:43:48 -05002 * Copyright (C) 2015-2019 Savoir-faire Linux Inc.
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -04003 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
4 * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20#import "AccAdvancedRingVC.h"
kkostiuk94fdd642021-01-22 10:52:54 -050021#import "utils.h"
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040022
23//LRC
24#import <api/lrc.h>
25#import <api/newaccountmodel.h>
26#import <api/newdevicemodel.h>
kkostiuk94fdd642021-01-22 10:52:54 -050027#import <api/contactmodel.h>
28#import <api/contact.h>
29#import <globalinstances.h>
30#import <api/conversationmodel.h>
31
32#import "delegates/ImageManipulationDelegate.h"
33
34//Qt
35#import <QtMacExtras/qmacfunctions.h>
36#import <QPixmap>
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040037
38@interface AccAdvancedRingVC () {
39 __unsafe_unretained IBOutlet NSButton *allowIncoming;
40 __unsafe_unretained IBOutlet NSTextField *nameServerField;
41 __unsafe_unretained IBOutlet NSTextField *proxyServerField;
42 __unsafe_unretained IBOutlet NSTextField *bootstrapServerField;
kkostiuk94fdd642021-01-22 10:52:54 -050043 __unsafe_unretained IBOutlet NSTextField *noDefaultModeratorsLabel;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040044 __unsafe_unretained IBOutlet NSButton *enableProxyButton;
kkostiuk870acd42021-01-20 19:34:36 -050045 __unsafe_unretained IBOutlet NSButton *enableLocalModeratorButton;
Kateryna Kostiukf362e2b2020-09-10 19:00:52 -040046 __unsafe_unretained IBOutlet NSButton *togleRendezVous;
kkostiuk94fdd642021-01-22 10:52:54 -050047 IBOutlet NSTableView* defaultModeratorsView;
48 IBOutlet NSPopover* contactPickerPopoverVC;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040049}
50@end
51
52@implementation AccAdvancedRingVC
53
54//Tags for views
55const NSInteger NAME_SERVER_TAG = 100;
56const NSInteger PROXY_SERVER_TAG = 200;
57const NSInteger BOOTSTRAP_SERVER_TAG = 300;
58
59-(void) updateView {
60 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
Kateryna Kostiukfc1f2b92020-05-27 13:57:19 -040061 [allowIncoming setState: accountProperties.DHT.PublicInCalls];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040062 [nameServerField setStringValue: accountProperties.RingNS.uri.toNSString()];
63 [proxyServerField setStringValue: accountProperties.proxyServer.toNSString()];
64 [bootstrapServerField setStringValue: accountProperties.hostname.toNSString()];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040065 [enableProxyButton setState: accountProperties.proxyEnabled];
66 [proxyServerField setEditable:accountProperties.proxyEnabled];
Kateryna Kostiukf362e2b2020-09-10 19:00:52 -040067 [togleRendezVous setState: accountProperties.isRendezVous];
kkostiuk870acd42021-01-20 19:34:36 -050068 [enableLocalModeratorButton setState: self.accountModel->isLocalModeratorsEnabled(self.selectedAccountID)];
kkostiuk94fdd642021-01-22 10:52:54 -050069 noDefaultModeratorsLabel.hidden = self.accountModel->getDefaultModerators(self.selectedAccountID).size() > 0;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040070}
71
72-(void) viewDidLoad {
73 [super viewDidLoad];
kkostiuk94fdd642021-01-22 10:52:54 -050074 defaultModeratorsView.delegate = self;
75 defaultModeratorsView.dataSource = self;
Kateryna Kostiuk782ac772019-11-29 16:12:30 -050076 [[self view] setAutoresizingMask: NSViewMinXMargin | NSViewMaxXMargin | NSViewWidthSizable];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040077 [self updateView];
78}
79
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040080- (void) setSelectedAccount:(const QString&) account {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040081 [super setSelectedAccount: account];
82 [self updateView];
83}
84
85#pragma mark - Actions
86
87- (IBAction)allowCallFromUnknownPeer:(id)sender {
88 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
Kateryna Kostiukfc1f2b92020-05-27 13:57:19 -040089 if(accountProperties.DHT.PublicInCalls != [sender state]) {
90 accountProperties.DHT.PublicInCalls = [sender state];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040091 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
92 }
93}
94
Kateryna Kostiukf362e2b2020-09-10 19:00:52 -040095- (IBAction)enableRendezVous:(id)sender {
96 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
97 if(accountProperties.isRendezVous != [sender state]) {
98 accountProperties.isRendezVous = [sender state];
99 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
100 }
101}
102
kkostiuk870acd42021-01-20 19:34:36 -0500103- (IBAction)enableLocalModerators:(id)sender {
104 self.accountModel->enableLocalModerators(self.selectedAccountID, [sender state]);
105}
106
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400107- (IBAction)enableProxy:(id)sender {
108 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
109 if(accountProperties.proxyEnabled != [sender state]) {
110 accountProperties.proxyEnabled = [sender state];
111 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
112 }
113 [proxyServerField setEditable:[sender state]];
114}
115
116- (IBAction) valueDidChange: (id) sender
117{
118 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
119
120 switch ([sender tag]) {
121 case NAME_SERVER_TAG:
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400122 if(accountProperties.RingNS.uri != QString::fromNSString([sender stringValue])) {
123 accountProperties.RingNS.uri = QString::fromNSString([sender stringValue]);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400124 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
125 }
126 return;
127 case PROXY_SERVER_TAG:
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400128 if(accountProperties.proxyServer != QString::fromNSString([sender stringValue])) {
129 accountProperties.proxyServer = QString::fromNSString([sender stringValue]);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400130 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
131 }
132 return;
133 case BOOTSTRAP_SERVER_TAG:
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400134 if(accountProperties.hostname != QString::fromNSString([sender stringValue])) {
135 accountProperties.hostname = QString::fromNSString([sender stringValue]);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400136 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
137 }
138 return;
139 default:
140 break;
141 }
142
143 [super valueDidChange:sender];
144}
145
kkostiuk94fdd642021-01-22 10:52:54 -0500146- (IBAction)removeModerator:(id)sender
147{
148 NSInteger row = [defaultModeratorsView rowForView:sender];
149 if(row < 0) {
150 return;
151 }
152 auto moderators = self.accountModel->getDefaultModerators(self.selectedAccountID);
153 if ((moderators.size()-1) < row) {
154 return;
155 }
156 auto moderator = moderators[row];
157 self.accountModel->setDefaultModerator(self.selectedAccountID, moderator, false);
158 NSIndexSet *indexes = [[NSIndexSet alloc] initWithIndex:row];
159 [defaultModeratorsView removeRowsAtIndexes:indexes withAnimation: NSTableViewAnimationSlideUp];
160 [defaultModeratorsView noteNumberOfRowsChanged];
161 noDefaultModeratorsLabel.hidden = self.accountModel->getDefaultModerators(self.selectedAccountID).size() > 0;
162}
163
164- (IBAction)selectContact:(id)sender
165{
166 if (contactPickerPopoverVC != nullptr) {
167 [contactPickerPopoverVC performClose:self];
168 contactPickerPopoverVC = NULL;
169 } else {
170 auto* contactSelectorVC = [[ChooseContactVC alloc] initWithNibName:@"ChooseContactVC" bundle:nil];
171 auto* contModel = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel.get();
172 [contactSelectorVC setUpCpntactPickerwithModel:self.accountModel andAccountId:self.selectedAccountID];
173 contactSelectorVC.delegate = self;
174 contactPickerPopoverVC = [[NSPopover alloc] init];
175 [contactPickerPopoverVC setContentSize:contactSelectorVC.view.frame.size];
176 [contactPickerPopoverVC setContentViewController:contactSelectorVC];
177 [contactPickerPopoverVC setAnimates:YES];
178 [contactPickerPopoverVC setBehavior:NSPopoverBehaviorTransient];
179 [contactPickerPopoverVC setDelegate:self];
180 [contactPickerPopoverVC showRelativeToRect:[sender bounds] ofView:sender preferredEdge:NSMinYEdge];
181 }
182}
183
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400184#pragma mark - NSTextFieldDelegate methods
185
Kateryna Kostiuk034eab32020-11-17 14:59:41 -0500186-(void)controlTextDidEndEditing:(NSNotification *)notif
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400187{
188 NSTextField *textField = [notif object];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400189 [self valueDidChange:textField];
190}
191
kkostiuk94fdd642021-01-22 10:52:54 -0500192#pragma mark - NSTableViewDelegate methods
193- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
194{
195 if(tableView == defaultModeratorsView) {
196 NSTableCellView* moderatorCell = [tableView makeViewWithIdentifier:@"TableCellDefaultModerator" owner:self];
197 NSImageView* avatar = [moderatorCell viewWithTag: 100];
198 NSTextField* nameLabel = [moderatorCell viewWithTag: 200];
199 NSTextField* profileNameLabel = [moderatorCell viewWithTag: 300];
200 NSButton* removeModerator = [moderatorCell viewWithTag: 400];
201
202 auto moderators = self.accountModel->getDefaultModerators(self.selectedAccountID);
203 if ((moderators.size() - 1) < row) {
204 return nil;
205 }
206 auto moderator = moderators[row];
207 auto& moderatorInfo = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getContact(moderator);
208 auto convOpt = getConversationFromURI(moderatorInfo.profileInfo.uri, *self.accountModel->getAccountInfo(self.selectedAccountID).conversationModel);
209 if (convOpt.has_value()) {
210 lrc::api::conversation::Info& conversation = *convOpt;
211 auto& imageManip = reinterpret_cast<Interfaces::ImageManipulationDelegate&>(GlobalInstances::pixmapManipulator());
212 NSImage* image = QtMac::toNSImage(qvariant_cast<QPixmap>(imageManip.conversationPhoto(conversation, self.accountModel->getAccountInfo(self.selectedAccountID))));
213 if(image) {
214 avatar.wantsLayer = YES;
215 avatar.layer.cornerRadius = avatar.frame.size.width * 0.5;
216 [avatar setImage:image];
217 }
218 }
219 [nameLabel setStringValue: bestIDForContact(moderatorInfo)];
220 [profileNameLabel setStringValue: bestNameForContact(moderatorInfo)];
221 [removeModerator setAction:@selector(removeModerator:)];
222 [removeModerator setTarget:self];
223 return moderatorCell;
224 }
225 return [super tableView:tableView viewForTableColumn:tableColumn row:row];
226}
227
228- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
229{
230 if(![tableView isEnabled]) {
231 return nil;
232 }
233 return [tableView makeViewWithIdentifier:@"HoverRowView" owner:nil];
234}
235
236#pragma mark - NSTableViewDataSource methods
237
238- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
239 if(tableView == defaultModeratorsView) {
240 return self.accountModel->getDefaultModerators(self.selectedAccountID).size();
241 }
242 return [super numberOfRowsInTableView:tableView];
243}
244
245#pragma mark Popover delegate
246
247- (void)popoverWillClose:(NSNotification *)notification
248{
249 if (contactPickerPopoverVC != nullptr) {
250 [contactPickerPopoverVC performClose:self];
251 contactPickerPopoverVC = NULL;
252 }
253}
254
255# pragma mark -ChooseContactVCDelegate
256
257-(void)contactChosen:(const QString&)contactUri {
258 self.accountModel->setDefaultModerator(self.selectedAccountID, contactUri, true);
259 [defaultModeratorsView reloadData];
260 noDefaultModeratorsLabel.hidden = self.accountModel->getDefaultModerators(self.selectedAccountID).size() > 0;
261}
262
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400263@end