blob: df544c62851171107195f95cff35fd443dd82796 [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 "AccRingGeneralVC.h"
21
22//cocoa
23#import <Quartz/Quartz.h>
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -040024#import <AVFoundation/AVFoundation.h>
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040025
26
27//Qt
28#import <QSize>
29#import <QtMacExtras/qmacfunctions.h>
30#import <QPixmap>
31
32//LRC
33#import <api/lrc.h>
34#import <api/newaccountmodel.h>
35#import <api/newdevicemodel.h>
36#import <interfaces/pixmapmanipulatori.h>
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040037
38#import "RegisterNameWC.h"
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040039#import "views/NSColor+RingTheme.h"
40#import "views/NSImage+Extensions.h"
41#import "views/HoverTableRowView.h"
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -050042#import "views/RoundedTextField.h"
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040043#import "ExportPasswordWC.h"
44#import "utils.h"
Kateryna Kostiuk2f2ef952019-05-07 11:04:41 -040045#import "Constants.h"
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040046
47@interface AccRingGeneralVC ()
48
49@property (unsafe_unretained) IBOutlet NSTextField *displayNameField;
50@property (unsafe_unretained) IBOutlet NSTextField *ringIDField;
51@property (unsafe_unretained) IBOutlet NSTextField *registeredNameField;
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -040052@property (unsafe_unretained) IBOutlet NSTextField *passwordField;
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -050053@property (unsafe_unretained) IBOutlet RoundedTextField *accountStatus;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040054@property (unsafe_unretained) IBOutlet NSButton *registerNameButton;
55@property (unsafe_unretained) IBOutlet NSButton* photoView;
56@property (unsafe_unretained) IBOutlet NSButton* passwordButton;
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -040057@property (unsafe_unretained) IBOutlet NSButton* linkDeviceButton;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040058@property (unsafe_unretained) IBOutlet NSButton* removeAccountButton;
Kateryna Kostiuk2d621cb2019-10-25 18:57:32 -040059@property (unsafe_unretained) IBOutlet NSButton* exportAccountButton;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040060@property (unsafe_unretained) IBOutlet NSImageView* addProfilePhotoImage;
61@property (unsafe_unretained) IBOutlet NSTableView* devicesTableView;
62@property (unsafe_unretained) IBOutlet NSTableView* blockedContactsTableView;
63@property (assign) IBOutlet NSLayoutConstraint* buttonRegisterWidthConstraint;
64@property (assign) IBOutlet NSLayoutConstraint* bannedContactHeightConstraint;
65@property (assign) IBOutlet NSLayoutConstraint* advancedButtonMarginConstraint;
66
67
68@property AbstractLoadingWC* accountModal;
69@property PasswordChangeWC* passwordModal;
Kateryna Kostiukc867eb92020-03-08 13:15:17 -040070@property QString selectedAccountID;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040071
72@end
73
74@implementation AccRingGeneralVC
75
76QMetaObject::Connection deviceAddedSignal;
77QMetaObject::Connection deviceRevokedSignal;
78QMetaObject::Connection deviceUpdatedSignal;
79QMetaObject::Connection contactBlockedSignal;
80QMetaObject::Connection bannedContactsChangedSignal;
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -050081QMetaObject::Connection accountStateChangedSignal;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040082
83
84@synthesize displayNameField;
85@synthesize ringIDField;
86@synthesize registeredNameField;
87@synthesize photoView;
88@synthesize addProfilePhotoImage;
89@synthesize accountModel;
90@synthesize registerNameButton, passwordButton, removeAccountButton;
91@synthesize buttonRegisterWidthConstraint;
92@synthesize accountModal;
93@synthesize delegate;
94@synthesize devicesTableView;
95@synthesize blockedContactsTableView;
Kateryna Kostiukc7e68f32019-10-09 16:15:45 -040096@synthesize linkDeviceButton;
97@synthesize passwordField;
Kateryna Kostiuk2d621cb2019-10-25 18:57:32 -040098@synthesize exportAccountButton;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040099
100typedef NS_ENUM(NSInteger, TagViews) {
101 DISPLAYNAME = 100,
102 DEVICE_NAME_TAG = 200,
103 DEVICE_ID_TAG = 300,
104 DEVICE_EDIT_TAG = 400,
105 DEVICE_REVOKE_TAG = 500,
106 BANNED_CONTACT_NAME_TAG = 600,
107 BANNED_CONTACT_ID_TAG = 700,
108 UNBLOCK_CONTACT_TAG = 800
109};
110
111-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
112{
113 if (self = [self initWithNibName: nibNameOrNil bundle:nibBundleOrNil])
114 {
115 self.accountModel= accountModel;
116 }
117 return self;
118}
119
120- (void)awakeFromNib
121{
122 [super awakeFromNib];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400123 [addProfilePhotoImage setWantsLayer: YES];
124 devicesTableView.delegate = self;
125 devicesTableView.dataSource = self;
126 blockedContactsTableView.delegate = self;
127 blockedContactsTableView.dataSource= self;
Kateryna Kostiuk782ac772019-11-29 16:12:30 -0500128 [[self view] setAutoresizingMask: NSViewMinXMargin | NSViewMaxXMargin | NSViewWidthSizable];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400129}
130
131- (void)viewDidLoad {
132 [super viewDidLoad];
133 [self updateView];
134}
135
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400136- (void) setSelectedAccount:(const QString&) account {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400137 self.selectedAccountID = account;
138 [self connectSignals];
139 [self updateView];
140 [self hideBannedContacts];
141}
142
143-(void) updateView {
144 const auto& account = accountModel->getAccountInfo(self.selectedAccountID);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400145
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400146 NSData *imageData = [[NSData alloc] initWithBase64EncodedString:account.profileInfo.avatar.toNSString() options:NSDataBase64DecodingIgnoreUnknownCharacters];
Kateryna Kostiuk43a79232018-10-18 15:45:18 -0400147 NSImage *image = [[NSImage alloc] initWithData:imageData];
148 if(image) {
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400149 [photoView setBordered:NO];
Kateryna Kostiuk2f2ef952019-05-07 11:04:41 -0400150 [photoView setImage: [image roundCorners: image.size.height * 0.5]];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400151 [addProfilePhotoImage setHidden:YES];
152 } else {
153 [photoView setImage:nil];
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400154 [photoView setBordered:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400155 [addProfilePhotoImage setHidden:NO];
156 }
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400157 NSString* displayName = account.profileInfo.alias.toNSString();
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400158 [displayNameField setStringValue:displayName];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400159 [ringIDField setStringValue: account.profileInfo.uri.toNSString()];
Kateryna Kostiuk9a2791a2019-10-28 10:38:11 -0400160
161 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400162 bool hideLocalAccountConfig = !accountProperties.managerUri.isEmpty();
Kateryna Kostiuk9a2791a2019-10-28 10:38:11 -0400163 [passwordButton setHidden:hideLocalAccountConfig];
164 [linkDeviceButton setHidden:hideLocalAccountConfig];
165 [passwordField setHidden:hideLocalAccountConfig];
166 [exportAccountButton setHidden: hideLocalAccountConfig];
167
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400168 if(account.registeredName.isEmpty() && !hideLocalAccountConfig) {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400169 [registerNameButton setHidden:NO];
170 buttonRegisterWidthConstraint.constant = 260.0;
171 } else {
172 buttonRegisterWidthConstraint.constant = 0.0;
173 [registerNameButton setHidden:YES];
174 }
175
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400176 [registeredNameField setStringValue:account.registeredName.toNSString()];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400177
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400178 NSString *title = accountProperties.archiveHasPassword ?
179 NSLocalizedString(@"Change password", @"Password button title") :
180 NSLocalizedString(@"Create password", @"Password button title");
181
182 [passwordButton setTitle:title];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400183 self.accountEnabled = account.enabled;
184
185 NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[removeAccountButton attributedTitle]];
186 NSRange titleRange = NSMakeRange(0, [colorTitle length]);
187 [colorTitle addAttribute:NSForegroundColorAttributeName value:[NSColor errorColor] range:titleRange];
188 [removeAccountButton setAttributedTitle:colorTitle];
189 [devicesTableView reloadData];
190 [blockedContactsTableView reloadData];
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500191 self.accountStatus.bgColor = colorForAccountStatus(accountModel->getAccountInfo(self.selectedAccountID).status);
192 [self.accountStatus setNeedsDisplay:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400193}
194
195-(void) connectSignals {
196 QObject::disconnect(deviceAddedSignal);
197 QObject::disconnect(deviceRevokedSignal);
198 QObject::disconnect(deviceUpdatedSignal);
199 QObject::disconnect(bannedContactsChangedSignal);
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500200 QObject::disconnect(accountStateChangedSignal);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400201 deviceAddedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
202 &lrc::api::NewDeviceModel::deviceAdded,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400203 [self] (const QString& id) {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400204 [devicesTableView reloadData];
205 });
206 deviceRevokedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
207 &lrc::api::NewDeviceModel::deviceRevoked,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400208 [self] (const QString& id, const lrc::api::NewDeviceModel::Status status) {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400209 switch (status) {
210 case lrc::api::NewDeviceModel::Status::SUCCESS:
211 [devicesTableView reloadData];
212 break;
213 case lrc::api::NewDeviceModel::Status::WRONG_PASSWORD:
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400214 [self showAlertWithTitle: @"" andText: NSLocalizedString(@"Device revocation failed. Please check your password and try again.", @"Device revocation error")];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400215 break;
216 case lrc::api::NewDeviceModel::Status::UNKNOWN_DEVICE:
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400217 [self showAlertWithTitle: @"" andText: NSLocalizedString(@"Device revocation failed", @"Device revocation error")];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400218 break;
219 }
220 });
221 deviceUpdatedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
222 &lrc::api::NewDeviceModel::deviceUpdated,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400223 [self] (const QString& id) {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400224 [devicesTableView reloadData];
225 });
226 bannedContactsChangedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).contactModel,
227 &lrc::api::ContactModel::bannedStatusChanged,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400228 [self] (const QString& contactUri, bool banned) {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400229 [blockedContactsTableView reloadData];
230 });
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500231 accountStateChangedSignal = QObject::connect(self.accountModel,
232 &lrc::api::NewAccountModel::accountStatusChanged,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400233 [self] (const QString& accountID) {
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500234 if(accountID != self.selectedAccountID) {
235 return;
236 }
237 self.accountStatus.bgColor = colorForAccountStatus(accountModel->getAccountInfo(accountID).status);
238 [self.accountStatus setNeedsDisplay:YES];
239 });
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400240}
241
242-(void) showAlertWithTitle: (NSString *) title andText: (NSString *)text {
243 NSAlert *alert = [[NSAlert alloc] init];
244 [alert addButtonWithTitle:@"OK"];
245 [alert setMessageText:title];
246 [alert setInformativeText:text];
247 [alert runModal];
248}
249
250- (void)pictureTakerDidEnd:(IKPictureTaker *) picker
251 returnCode:(NSInteger) code
252 contextInfo:(void*) contextInfo
253{
Kateryna Kostiuk256814e2018-09-04 14:47:33 -0400254 //do nothing when editing canceled
255 if (code == 0) {
256 return;
257 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400258 if (auto outputImage = [picker outputImage]) {
259 auto image = [picker inputImage];
Kateryna Kostiuk2f2ef952019-05-07 11:04:41 -0400260 CGFloat newSize = MIN(MIN(image.size.height, image.size.width), MAX_IMAGE_SIZE);
261 outputImage = [outputImage imageResizeInsideMax: newSize];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400262 [photoView setImage: [outputImage roundCorners: outputImage.size.height * 0.5]];
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400263 [photoView setBordered:NO];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400264 [addProfilePhotoImage setHidden:YES];
265 auto imageToBytes = QByteArray::fromNSData([outputImage TIFFRepresentation]).toBase64();
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400266 self.accountModel->setAvatar(self.selectedAccountID, QString(imageToBytes));
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400267 } else if(!photoView.image) {
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400268 [photoView setBordered:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400269 [addProfilePhotoImage setHidden:NO];
270 }
271}
272
273#pragma mark - RegisterNameDelegate methods
274
275- (void) didRegisterName:(NSString *) name withSuccess:(BOOL) success
276{
277 [self.accountModal close];
278 if(!success) {
279 return;
280 }
281
282 if(name.length == 0) {
283 return;
284 }
285 buttonRegisterWidthConstraint.constant = 0.0;
286 [registerNameButton setHidden:YES];
287 [registeredNameField setStringValue:name];
Kateryna Kostiukdf680762018-10-29 13:53:52 -0400288 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
289 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400290}
291
292#pragma mark - NSTextFieldDelegate delegate methods
293
294- (void)controlTextDidChange:(NSNotification *)notif
295{
296 NSTextField* textField = [notif object];
297 if (textField.tag != DISPLAYNAME) {
298 return;
299 }
300 NSString* displayName = textField.stringValue;
301
302 [NSObject cancelPreviousPerformRequestsWithTarget:self];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400303 self.accountModel->setAlias(self.selectedAccountID, QString::fromNSString(displayName));
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400304 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
305 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
306}
307
308#pragma mark - NSTableViewDataSource methods
309
310- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
311 if(tableView == devicesTableView) {
312 return self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices().size();
313 } else if (tableView == blockedContactsTableView){
314 return self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts().size();
315 }
316 return 0;
317}
318
319#pragma mark - NSTableViewDelegate methods
320- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
321{
322 if(tableView == devicesTableView) {
323 NSTableCellView* deviceView = [tableView makeViewWithIdentifier:@"TableCellDeviceItem" owner:self];
324 NSTextField* nameLabel = [deviceView viewWithTag: DEVICE_NAME_TAG];
325 NSTextField* idLabel = [deviceView viewWithTag: DEVICE_ID_TAG];
326 NSButton* revokeButton = [deviceView viewWithTag: DEVICE_REVOKE_TAG];
327 NSButton* editButton = [deviceView viewWithTag: DEVICE_EDIT_TAG];
328 [editButton setAction:@selector(editDevice:)];
329 [editButton setTarget:self];
330 [revokeButton setAction:@selector(startDeviceRevocation:)];
331 [revokeButton setTarget:self];
332 auto devices = self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices();
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400333 auto device = devices[row];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400334
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400335 auto name = device.name;
336 auto deviceID = device.id;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400337
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400338 [nameLabel setStringValue: name.toNSString()];
339 [idLabel setStringValue: deviceID.toNSString()];
340 [revokeButton setHidden: device.isCurrent];
341 [editButton setHidden: !device.isCurrent];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400342 return deviceView;
343 } else if (tableView == blockedContactsTableView) {
344 NSTableCellView* contactView = [tableView makeViewWithIdentifier:@"TableCellBannedContactItem" owner:self];
345 NSTextField* nameLabel = [contactView viewWithTag: BANNED_CONTACT_NAME_TAG];
346 NSTextField* idLabel = [contactView viewWithTag: BANNED_CONTACT_ID_TAG];
347 NSButton* revokeButton = [contactView viewWithTag: UNBLOCK_CONTACT_TAG];
348 auto contacts = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts();
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400349 auto contactID = contacts[row];
350 [idLabel setStringValue: contactID.toNSString()];
351 try {
352 auto contact = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getContact(contactID);
353 [nameLabel setStringValue: bestNameForContact(contact)];
354 } catch (std::out_of_range& e) {
355 NSLog(@"contact out of range");
356 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400357 [revokeButton setAction:@selector(unblockContact:)];
358 [revokeButton setTarget:self];
359 return contactView;
360 }
361}
362
363- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
364{
365 if(tableView == devicesTableView) {
366 return tableView.rowHeight;
367 } else if (tableView == blockedContactsTableView) {
368 CGFloat height = self.bannedContactHeightConstraint.constant;
369 if(height == 150) {
370 return 52;
371 } else {
372 return 1;
373 }
374 }
375}
376
377- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
378{
379 return [tableView makeViewWithIdentifier:@"HoverRowView" owner:nil];
380}
381
382#pragma mark - Actions
383
384- (IBAction)editPhoto:(id)sender
385{
386 auto pictureTaker = [IKPictureTaker pictureTaker];
Kateryna Kostiukdc563242019-08-19 12:05:54 -0400387#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400388 if (@available(macOS 10.14, *)) {
389 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
390 if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied)
391 {
392 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
393 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400394
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400395 if(authStatus == AVAuthorizationStatusNotDetermined)
396 {
397 [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
398 if(!granted){
399 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
400 }
401 }];
402 }
403 }
Kateryna Kostiukdc563242019-08-19 12:05:54 -0400404#endif
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400405 [pictureTaker beginPictureTakerSheetForWindow:[self.view window]
406 withDelegate:self
407 didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:)
408 contextInfo:nil];
409
410}
411
412- (IBAction)startExportOnRing:(id)sender
413{
414 ExportPasswordWC *passwordWC = [[ExportPasswordWC alloc] initWithNibName:@"ExportPasswordWindow" bundle: nil accountmodel: self.accountModel];
415 passwordWC.selectedAccountID = self.selectedAccountID;
416 accountModal = passwordWC;
417 [self.view.window beginSheet: passwordWC.window completionHandler:nil];
418}
419- (IBAction)triggerAdwancedSettings: (NSButton *)sender {
420 [self.delegate triggerAdvancedOptions];
421}
422
423- (IBAction)enableAccount: (NSButton *)sender {
424 const auto& account = accountModel->getAccountInfo(self.selectedAccountID);
Kateryna Kostiuk53799642019-07-30 18:21:16 -0400425 self.accountModel->setAccountEnabled(self.selectedAccountID, !account.enabled);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400426 self.accountEnabled = account.enabled;
Kateryna Kostiuk53799642019-07-30 18:21:16 -0400427
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400428}
429
430- (IBAction)removeAccount:(id)sender
431{
432 NSAlert *alert = [[NSAlert alloc] init];
433 [alert addButtonWithTitle:@"OK"];
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400434 [alert addButtonWithTitle:NSLocalizedString(@"Cancel",@"Button Action")];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400435 [alert setMessageText: NSLocalizedString(@"Remove account",
436 @"Remove account alert title")];
437 [alert setInformativeText:NSLocalizedString(@"By clicking \"OK\" you will remove this account on this device! This action can not be undone. Also, your registered name can be lost.",
438 @"Remove account alert message")];
439
440 if ([alert runModal] == NSAlertFirstButtonReturn) {
441 self.accountModel->removeAccount(self.selectedAccountID);
442 }
443}
444
445- (IBAction)exportAccount:(id)sender
446{
Kateryna Kostiuk740a5962019-03-28 12:56:08 -0400447 NSSavePanel* filePicker = [NSSavePanel savePanel];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400448 NSString* name = [self.selectedAccountID.toNSString() stringByAppendingString: @".gz"];
Kateryna Kostiuk740a5962019-03-28 12:56:08 -0400449 [filePicker setNameFieldStringValue: name];
Kateryna Kostiukf4b1a792019-09-19 11:26:19 -0400450 if ([filePicker runModal] != NSFileHandlingPanelOKButton) {
451 return;
452 }
453 NSString *password = @"";
454 const char* fullPath = [[filePicker URL] fileSystemRepresentation];
455 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
456 if(accountProperties.archiveHasPassword) {
457 NSAlert *alert = [[NSAlert alloc] init];
458 [alert addButtonWithTitle:@"OK"];
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400459 [alert addButtonWithTitle:NSLocalizedString(@"Cancel",@"Button Action")];
Kateryna Kostiukf4b1a792019-09-19 11:26:19 -0400460 [alert setMessageText: NSLocalizedString(@"Enter account password",
461 @"Backup enter password")];
462 NSTextField *input = [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 20)];
463 [alert setAccessoryView:input];
464 if ([alert runModal] != NSAlertFirstButtonReturn) {
465 return;
Kateryna Kostiuk740a5962019-03-28 12:56:08 -0400466 }
Kateryna Kostiukf4b1a792019-09-19 11:26:19 -0400467 password = [input stringValue];
468 }
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400469 if (self.accountModel->exportToFile(self.selectedAccountID, fullPath, QString::fromNSString(password))) {
Kateryna Kostiukf4b1a792019-09-19 11:26:19 -0400470 [self didCompleteExportWithPath:[filePicker URL]];
471 } else {
472 [self showAlertWithTitle: @"" andText: NSLocalizedString(@"An error occured during the backup", @"Backup error")];
Kateryna Kostiuk740a5962019-03-28 12:56:08 -0400473 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400474}
475
476- (IBAction)startNameRegistration:(id)sender
477{
478 RegisterNameWC* registerWC = [[RegisterNameWC alloc] initWithNibName:@"RegisterNameWindow" bundle: nil accountmodel: self.accountModel];
479 registerWC.delegate = self;
480 registerWC.selectedAccountID = self.selectedAccountID;
481 self.accountModal = registerWC;
482 [self.view.window beginSheet:registerWC.window completionHandler:nil];
483}
484- (IBAction)changePassword:(id)sender
485{
486 PasswordChangeWC* passwordWC = [[PasswordChangeWC alloc] initWithNibName:@"PasswordChange" bundle: nil accountmodel: self.accountModel];
487 passwordWC.selectedAccountID = self.selectedAccountID;
488 passwordWC.delegate = self;
489 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
490 self.passwordModal = passwordWC;
491}
492
493- (IBAction)showBanned: (NSButton *)sender {
494 CGFloat height = self.bannedContactHeightConstraint.constant;
495 NSRect frame = self.view.frame;
496 if(height == 150) {
497 frame.size.height = frame.size.height - 150 - 10;
498 } else {
499 frame.size.height = frame.size.height + 150 + 10;
500 }
501 self.view.frame = frame;
502 [self.delegate updateFrame];
503 CGFloat advancedHeight = self.advancedButtonMarginConstraint.constant;
504 self.advancedButtonMarginConstraint.constant = (height== 2) ? 40 : 30;
505 self.bannedContactHeightConstraint.constant = (height== 2) ? 150 : 2;
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -0400506 [[[[self.blockedContactsTableView superview] superview] superview] setHidden:![[[[self.blockedContactsTableView superview] superview] superview] isHidden]];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400507 [blockedContactsTableView reloadData];
508}
509
510-(void) hideBannedContacts {
511 CGFloat height = self.bannedContactHeightConstraint.constant;
512 NSRect frame = self.view.frame;
513 if(height == 150) {
514 [self showBanned:nil];
515 }
516}
517
518- (IBAction)startDeviceRevocation:(NSView*)sender
519{
520 NSInteger row = [devicesTableView rowForView:sender];
521 if(row < 0) {
522 return;
523 }
524 auto devices = self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices();
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400525 if (devices.size() < row) {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400526 return;
527 }
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400528 auto device = devices[row];
529 [self proceedDeviceRevokationAlert:device.id];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400530}
531
532- (IBAction)unblockContact:(NSView*)sender
533{
534 NSInteger row = [blockedContactsTableView rowForView:sender];
535 if(row < 0) {
536 return;
537 }
538 auto contacts = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts();
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400539 auto contactID = contacts[row];
540 try {
541 auto contact = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getContact(contactID);
542 if(!contact.isBanned) {
543 return;
544 }
545 self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->addContact(contact);
546 } catch (std::out_of_range& e) {
547 NSLog(@"contact out of range");
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400548 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400549}
550
551- (IBAction)editDevice:(NSView*)sender
552{
553 NSInteger row = [devicesTableView rowForView:sender];
554 if(row < 0) {
555 return;
556 }
557
558 NSTableCellView* deviceView = [devicesTableView viewAtColumn:0 row:row makeIfNecessary:NO];
559 if(!deviceView || ![deviceView isKindOfClass:[NSTableCellView class]]) {
560 return;
561 }
562
563 NSTextField* nameLabel = [deviceView viewWithTag: DEVICE_NAME_TAG];
564 NSButton* editButton = [deviceView viewWithTag: DEVICE_EDIT_TAG];
565 if ([nameLabel isEditable]) {
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400566 self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->setCurrentDeviceName(QString::fromNSString(nameLabel.stringValue));
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400567 [nameLabel setEditable:NO];
568 [self.view.window makeFirstResponder:nil];
569 editButton.image = [NSImage imageNamed:NSImageNameTouchBarComposeTemplate];
570 return;
571 }
572 [nameLabel setEditable:YES];
573 [nameLabel becomeFirstResponder];
574 editButton.image = [NSImage imageNamed:NSImageNameTouchBarDownloadTemplate];
575}
576
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400577-(void) revokeDeviceWithID: (const QString&) deviceID password:(NSString *) password {
578 self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->revokeDevice(deviceID, QString::fromNSString(password));
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400579}
580
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400581-(void) proceedDeviceRevokationAlert: (const QString&) deviceID {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400582 NSAlert *alert = [[NSAlert alloc] init];
583 [alert addButtonWithTitle:@"OK"];
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400584 [alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"Button Action")];
585 [alert setMessageText:NSLocalizedString(@"Revoke Device", @"Device revocation title")];
586 [alert setInformativeText:NSLocalizedString(@"Attention! This action could not be undone!",
587 @"Device revocation message")];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400588 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
589 if(accountProperties.archiveHasPassword) {
590 NSSecureTextField *passwordText = [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400591 [passwordText setPlaceholderString:NSLocalizedString(@"Enter account password", @"Backup enter password")];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400592 [alert setAccessoryView:passwordText];
593 if ([alert runModal] == NSAlertFirstButtonReturn) {
594 [self revokeDeviceWithID:deviceID password:[passwordText stringValue]];
595 }
596 } else {
597 if ([alert runModal] == NSAlertFirstButtonReturn) {
598 [self revokeDeviceWithID:deviceID password:@""];
599 }
600 }
601}
602
603#pragma mark - BackupAccountDelegate methods
604
605-(void) didCompleteExportWithPath:(NSURL*) fileUrl
606{
607 [[NSWorkspace sharedWorkspace] selectFile:fileUrl.path inFileViewerRootedAtPath:@""];
608}
609
610#pragma mark - PasswordChangeDelegate
611
612-(void) paswordCreatedWithSuccess:(BOOL) success
613{
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400614 NSString *title = success ? NSLocalizedString(@"Change password", @"Password button title") :
615 NSLocalizedString(@"Create password", @"Password button title");
616 [passwordButton setTitle: title];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400617}
618
619@end