Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 1 | /* |
Sébastien Blin | 029ffa8 | 2019-01-02 17:43:48 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2019 Savoir-faire Linux Inc. |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 3 | * 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" |
kkostiuk | 94fdd64 | 2021-01-22 10:52:54 -0500 | [diff] [blame] | 21 | #import "utils.h" |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 22 | |
| 23 | //LRC |
| 24 | #import <api/lrc.h> |
| 25 | #import <api/newaccountmodel.h> |
| 26 | #import <api/newdevicemodel.h> |
kkostiuk | 94fdd64 | 2021-01-22 10:52:54 -0500 | [diff] [blame] | 27 | #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 Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 37 | |
| 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; |
kkostiuk | 94fdd64 | 2021-01-22 10:52:54 -0500 | [diff] [blame] | 43 | __unsafe_unretained IBOutlet NSTextField *noDefaultModeratorsLabel; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 44 | __unsafe_unretained IBOutlet NSButton *enableProxyButton; |
kkostiuk | 870acd4 | 2021-01-20 19:34:36 -0500 | [diff] [blame] | 45 | __unsafe_unretained IBOutlet NSButton *enableLocalModeratorButton; |
Kateryna Kostiuk | f362e2b | 2020-09-10 19:00:52 -0400 | [diff] [blame] | 46 | __unsafe_unretained IBOutlet NSButton *togleRendezVous; |
kkostiuk | 94fdd64 | 2021-01-22 10:52:54 -0500 | [diff] [blame] | 47 | IBOutlet NSTableView* defaultModeratorsView; |
| 48 | IBOutlet NSPopover* contactPickerPopoverVC; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 49 | } |
| 50 | @end |
| 51 | |
| 52 | @implementation AccAdvancedRingVC |
| 53 | |
| 54 | //Tags for views |
| 55 | const NSInteger NAME_SERVER_TAG = 100; |
| 56 | const NSInteger PROXY_SERVER_TAG = 200; |
| 57 | const NSInteger BOOTSTRAP_SERVER_TAG = 300; |
| 58 | |
| 59 | -(void) updateView { |
| 60 | lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID); |
Kateryna Kostiuk | fc1f2b9 | 2020-05-27 13:57:19 -0400 | [diff] [blame] | 61 | [allowIncoming setState: accountProperties.DHT.PublicInCalls]; |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 62 | [nameServerField setStringValue: accountProperties.RingNS.uri.toNSString()]; |
| 63 | [proxyServerField setStringValue: accountProperties.proxyServer.toNSString()]; |
| 64 | [bootstrapServerField setStringValue: accountProperties.hostname.toNSString()]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 65 | [enableProxyButton setState: accountProperties.proxyEnabled]; |
| 66 | [proxyServerField setEditable:accountProperties.proxyEnabled]; |
Kateryna Kostiuk | f362e2b | 2020-09-10 19:00:52 -0400 | [diff] [blame] | 67 | [togleRendezVous setState: accountProperties.isRendezVous]; |
kkostiuk | 870acd4 | 2021-01-20 19:34:36 -0500 | [diff] [blame] | 68 | [enableLocalModeratorButton setState: self.accountModel->isLocalModeratorsEnabled(self.selectedAccountID)]; |
kkostiuk | 94fdd64 | 2021-01-22 10:52:54 -0500 | [diff] [blame] | 69 | noDefaultModeratorsLabel.hidden = self.accountModel->getDefaultModerators(self.selectedAccountID).size() > 0; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | -(void) viewDidLoad { |
| 73 | [super viewDidLoad]; |
kkostiuk | 94fdd64 | 2021-01-22 10:52:54 -0500 | [diff] [blame] | 74 | defaultModeratorsView.delegate = self; |
| 75 | defaultModeratorsView.dataSource = self; |
Kateryna Kostiuk | 782ac77 | 2019-11-29 16:12:30 -0500 | [diff] [blame] | 76 | [[self view] setAutoresizingMask: NSViewMinXMargin | NSViewMaxXMargin | NSViewWidthSizable]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 77 | [self updateView]; |
| 78 | } |
| 79 | |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 80 | - (void) setSelectedAccount:(const QString&) account { |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 81 | [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 Kostiuk | fc1f2b9 | 2020-05-27 13:57:19 -0400 | [diff] [blame] | 89 | if(accountProperties.DHT.PublicInCalls != [sender state]) { |
| 90 | accountProperties.DHT.PublicInCalls = [sender state]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 91 | self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties); |
| 92 | } |
| 93 | } |
| 94 | |
Kateryna Kostiuk | f362e2b | 2020-09-10 19:00:52 -0400 | [diff] [blame] | 95 | - (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 | |
kkostiuk | 870acd4 | 2021-01-20 19:34:36 -0500 | [diff] [blame] | 103 | - (IBAction)enableLocalModerators:(id)sender { |
| 104 | self.accountModel->enableLocalModerators(self.selectedAccountID, [sender state]); |
| 105 | } |
| 106 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 107 | - (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 Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 122 | if(accountProperties.RingNS.uri != QString::fromNSString([sender stringValue])) { |
| 123 | accountProperties.RingNS.uri = QString::fromNSString([sender stringValue]); |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 124 | self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties); |
| 125 | } |
| 126 | return; |
| 127 | case PROXY_SERVER_TAG: |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 128 | if(accountProperties.proxyServer != QString::fromNSString([sender stringValue])) { |
| 129 | accountProperties.proxyServer = QString::fromNSString([sender stringValue]); |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 130 | self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties); |
| 131 | } |
| 132 | return; |
| 133 | case BOOTSTRAP_SERVER_TAG: |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 134 | if(accountProperties.hostname != QString::fromNSString([sender stringValue])) { |
| 135 | accountProperties.hostname = QString::fromNSString([sender stringValue]); |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 136 | self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties); |
| 137 | } |
| 138 | return; |
| 139 | default: |
| 140 | break; |
| 141 | } |
| 142 | |
| 143 | [super valueDidChange:sender]; |
| 144 | } |
| 145 | |
kkostiuk | 94fdd64 | 2021-01-22 10:52:54 -0500 | [diff] [blame] | 146 | - (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 Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 184 | #pragma mark - NSTextFieldDelegate methods |
| 185 | |
Kateryna Kostiuk | 034eab3 | 2020-11-17 14:59:41 -0500 | [diff] [blame] | 186 | -(void)controlTextDidEndEditing:(NSNotification *)notif |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 187 | { |
| 188 | NSTextField *textField = [notif object]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 189 | [self valueDidChange:textField]; |
| 190 | } |
| 191 | |
kkostiuk | 94fdd64 | 2021-01-22 10:52:54 -0500 | [diff] [blame] | 192 | #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 Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 263 | @end |