blob: ef9db62a7298f69222f7ebe49af1f79c3a7ef95c [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 Kostiukdc720842020-09-10 16:44:02 -0400146 @autoreleasepool {
147 NSData *imageData = [[NSData alloc] initWithBase64EncodedString:account.profileInfo.avatar.toNSString() options:NSDataBase64DecodingIgnoreUnknownCharacters];
148 NSImage *image = [[NSImage alloc] initWithData:imageData];
149 if(image) {
150 [photoView setBordered:NO];
151 [photoView setImage: [image roundCorners: image.size.height * 0.5]];
152 [addProfilePhotoImage setHidden:YES];
153 } else {
154 [photoView setImage:nil];
155 [photoView setBordered:YES];
156 [addProfilePhotoImage setHidden:NO];
157 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400158 }
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400159 NSString* displayName = account.profileInfo.alias.toNSString();
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400160 [displayNameField setStringValue:displayName];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400161 [ringIDField setStringValue: account.profileInfo.uri.toNSString()];
Kateryna Kostiuk9a2791a2019-10-28 10:38:11 -0400162
163 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400164 bool hideLocalAccountConfig = !accountProperties.managerUri.isEmpty();
Kateryna Kostiuk9a2791a2019-10-28 10:38:11 -0400165 [passwordButton setHidden:hideLocalAccountConfig];
166 [linkDeviceButton setHidden:hideLocalAccountConfig];
167 [passwordField setHidden:hideLocalAccountConfig];
168 [exportAccountButton setHidden: hideLocalAccountConfig];
169
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400170 if(account.registeredName.isEmpty() && !hideLocalAccountConfig) {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400171 [registerNameButton setHidden:NO];
172 buttonRegisterWidthConstraint.constant = 260.0;
173 } else {
174 buttonRegisterWidthConstraint.constant = 0.0;
175 [registerNameButton setHidden:YES];
176 }
177
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400178 [registeredNameField setStringValue:account.registeredName.toNSString()];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400179
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400180 NSString *title = accountProperties.archiveHasPassword ?
181 NSLocalizedString(@"Change password", @"Password button title") :
182 NSLocalizedString(@"Create password", @"Password button title");
183
184 [passwordButton setTitle:title];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400185 self.accountEnabled = account.enabled;
186
187 NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[removeAccountButton attributedTitle]];
188 NSRange titleRange = NSMakeRange(0, [colorTitle length]);
189 [colorTitle addAttribute:NSForegroundColorAttributeName value:[NSColor errorColor] range:titleRange];
190 [removeAccountButton setAttributedTitle:colorTitle];
191 [devicesTableView reloadData];
192 [blockedContactsTableView reloadData];
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500193 self.accountStatus.bgColor = colorForAccountStatus(accountModel->getAccountInfo(self.selectedAccountID).status);
194 [self.accountStatus setNeedsDisplay:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400195}
196
197-(void) connectSignals {
198 QObject::disconnect(deviceAddedSignal);
199 QObject::disconnect(deviceRevokedSignal);
200 QObject::disconnect(deviceUpdatedSignal);
201 QObject::disconnect(bannedContactsChangedSignal);
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500202 QObject::disconnect(accountStateChangedSignal);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400203 deviceAddedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
204 &lrc::api::NewDeviceModel::deviceAdded,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400205 [self] (const QString& id) {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400206 [devicesTableView reloadData];
207 });
208 deviceRevokedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
209 &lrc::api::NewDeviceModel::deviceRevoked,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400210 [self] (const QString& id, const lrc::api::NewDeviceModel::Status status) {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400211 switch (status) {
212 case lrc::api::NewDeviceModel::Status::SUCCESS:
213 [devicesTableView reloadData];
214 break;
215 case lrc::api::NewDeviceModel::Status::WRONG_PASSWORD:
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400216 [self showAlertWithTitle: @"" andText: NSLocalizedString(@"Device revocation failed. Please check your password and try again.", @"Device revocation error")];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400217 break;
218 case lrc::api::NewDeviceModel::Status::UNKNOWN_DEVICE:
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400219 [self showAlertWithTitle: @"" andText: NSLocalizedString(@"Device revocation failed", @"Device revocation error")];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400220 break;
221 }
222 });
223 deviceUpdatedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
224 &lrc::api::NewDeviceModel::deviceUpdated,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400225 [self] (const QString& id) {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400226 [devicesTableView reloadData];
227 });
228 bannedContactsChangedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).contactModel,
229 &lrc::api::ContactModel::bannedStatusChanged,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400230 [self] (const QString& contactUri, bool banned) {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400231 [blockedContactsTableView reloadData];
232 });
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500233 accountStateChangedSignal = QObject::connect(self.accountModel,
234 &lrc::api::NewAccountModel::accountStatusChanged,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400235 [self] (const QString& accountID) {
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500236 if(accountID != self.selectedAccountID) {
237 return;
238 }
239 self.accountStatus.bgColor = colorForAccountStatus(accountModel->getAccountInfo(accountID).status);
240 [self.accountStatus setNeedsDisplay:YES];
241 });
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400242}
243
244-(void) showAlertWithTitle: (NSString *) title andText: (NSString *)text {
245 NSAlert *alert = [[NSAlert alloc] init];
246 [alert addButtonWithTitle:@"OK"];
247 [alert setMessageText:title];
248 [alert setInformativeText:text];
249 [alert runModal];
250}
251
252- (void)pictureTakerDidEnd:(IKPictureTaker *) picker
253 returnCode:(NSInteger) code
254 contextInfo:(void*) contextInfo
255{
Kateryna Kostiuk14b8aa72020-11-11 10:40:15 -0500256 //do nothing when editing canceled
Kateryna Kostiuk256814e2018-09-04 14:47:33 -0400257 if (code == 0) {
258 return;
259 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400260 if (auto outputImage = [picker outputImage]) {
261 auto image = [picker inputImage];
Kateryna Kostiuk2f2ef952019-05-07 11:04:41 -0400262 CGFloat newSize = MIN(MIN(image.size.height, image.size.width), MAX_IMAGE_SIZE);
263 outputImage = [outputImage imageResizeInsideMax: newSize];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400264 [photoView setImage: [outputImage roundCorners: outputImage.size.height * 0.5]];
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400265 [photoView setBordered:NO];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400266 [addProfilePhotoImage setHidden:YES];
Kateryna Kostiuk14b8aa72020-11-11 10:40:15 -0500267 NSData* imageData = [outputImage TIFFRepresentation];
268 NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData: imageData];
269 NSDictionary *properties = [[NSDictionary alloc] init];
270 imageData = [imageRep representationUsingType:NSPNGFileType properties: properties];
271 NSString * dataString = [imageData base64EncodedStringWithOptions:0];
272 self.accountModel->setAvatar(self.selectedAccountID, QString::fromNSString(dataString));
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400273 } else if(!photoView.image) {
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400274 [photoView setBordered:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400275 [addProfilePhotoImage setHidden:NO];
276 }
277}
278
279#pragma mark - RegisterNameDelegate methods
280
281- (void) didRegisterName:(NSString *) name withSuccess:(BOOL) success
282{
283 [self.accountModal close];
284 if(!success) {
285 return;
286 }
287
288 if(name.length == 0) {
289 return;
290 }
291 buttonRegisterWidthConstraint.constant = 0.0;
292 [registerNameButton setHidden:YES];
293 [registeredNameField setStringValue:name];
Kateryna Kostiukdf680762018-10-29 13:53:52 -0400294 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
295 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400296}
297
298#pragma mark - NSTextFieldDelegate delegate methods
299
Kateryna Kostiuk034eab32020-11-17 14:59:41 -0500300-(void)controlTextDidEndEditing:(NSNotification *)notification
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400301{
Kateryna Kostiuk034eab32020-11-17 14:59:41 -0500302 NSTextField* textField = [notification object];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400303 if (textField.tag != DISPLAYNAME) {
304 return;
305 }
Kateryna Kostiuk034eab32020-11-17 14:59:41 -0500306
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400307 NSString* displayName = textField.stringValue;
308
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400309 self.accountModel->setAlias(self.selectedAccountID, QString::fromNSString(displayName));
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400310 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
311 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
312}
313
314#pragma mark - NSTableViewDataSource methods
315
316- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
317 if(tableView == devicesTableView) {
318 return self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices().size();
319 } else if (tableView == blockedContactsTableView){
320 return self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts().size();
321 }
322 return 0;
323}
324
325#pragma mark - NSTableViewDelegate methods
326- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
327{
328 if(tableView == devicesTableView) {
329 NSTableCellView* deviceView = [tableView makeViewWithIdentifier:@"TableCellDeviceItem" owner:self];
330 NSTextField* nameLabel = [deviceView viewWithTag: DEVICE_NAME_TAG];
331 NSTextField* idLabel = [deviceView viewWithTag: DEVICE_ID_TAG];
332 NSButton* revokeButton = [deviceView viewWithTag: DEVICE_REVOKE_TAG];
333 NSButton* editButton = [deviceView viewWithTag: DEVICE_EDIT_TAG];
334 [editButton setAction:@selector(editDevice:)];
335 [editButton setTarget:self];
336 [revokeButton setAction:@selector(startDeviceRevocation:)];
337 [revokeButton setTarget:self];
338 auto devices = self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices();
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400339 auto device = devices[row];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400340
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400341 auto name = device.name;
342 auto deviceID = device.id;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400343
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400344 [nameLabel setStringValue: name.toNSString()];
345 [idLabel setStringValue: deviceID.toNSString()];
346 [revokeButton setHidden: device.isCurrent];
347 [editButton setHidden: !device.isCurrent];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400348 return deviceView;
349 } else if (tableView == blockedContactsTableView) {
350 NSTableCellView* contactView = [tableView makeViewWithIdentifier:@"TableCellBannedContactItem" owner:self];
351 NSTextField* nameLabel = [contactView viewWithTag: BANNED_CONTACT_NAME_TAG];
352 NSTextField* idLabel = [contactView viewWithTag: BANNED_CONTACT_ID_TAG];
353 NSButton* revokeButton = [contactView viewWithTag: UNBLOCK_CONTACT_TAG];
354 auto contacts = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts();
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400355 auto contactID = contacts[row];
356 [idLabel setStringValue: contactID.toNSString()];
357 try {
358 auto contact = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getContact(contactID);
359 [nameLabel setStringValue: bestNameForContact(contact)];
360 } catch (std::out_of_range& e) {
361 NSLog(@"contact out of range");
362 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400363 [revokeButton setAction:@selector(unblockContact:)];
364 [revokeButton setTarget:self];
365 return contactView;
366 }
367}
368
369- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
370{
371 if(tableView == devicesTableView) {
372 return tableView.rowHeight;
373 } else if (tableView == blockedContactsTableView) {
374 CGFloat height = self.bannedContactHeightConstraint.constant;
375 if(height == 150) {
376 return 52;
377 } else {
378 return 1;
379 }
380 }
381}
382
383- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
384{
385 return [tableView makeViewWithIdentifier:@"HoverRowView" owner:nil];
386}
387
388#pragma mark - Actions
389
390- (IBAction)editPhoto:(id)sender
391{
392 auto pictureTaker = [IKPictureTaker pictureTaker];
Kateryna Kostiukdc563242019-08-19 12:05:54 -0400393#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400394 if (@available(macOS 10.14, *)) {
395 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
396 if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied)
397 {
398 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
399 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400400
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400401 if(authStatus == AVAuthorizationStatusNotDetermined)
402 {
403 [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
404 if(!granted){
405 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
406 }
407 }];
408 }
409 }
Kateryna Kostiukdc563242019-08-19 12:05:54 -0400410#endif
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400411 [pictureTaker beginPictureTakerSheetForWindow:[self.view window]
412 withDelegate:self
413 didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:)
414 contextInfo:nil];
415
416}
417
418- (IBAction)startExportOnRing:(id)sender
419{
420 ExportPasswordWC *passwordWC = [[ExportPasswordWC alloc] initWithNibName:@"ExportPasswordWindow" bundle: nil accountmodel: self.accountModel];
421 passwordWC.selectedAccountID = self.selectedAccountID;
422 accountModal = passwordWC;
423 [self.view.window beginSheet: passwordWC.window completionHandler:nil];
424}
425- (IBAction)triggerAdwancedSettings: (NSButton *)sender {
Kateryna Kostiuk034eab32020-11-17 14:59:41 -0500426 [self.view.window makeFirstResponder:nil];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400427 [self.delegate triggerAdvancedOptions];
428}
429
430- (IBAction)enableAccount: (NSButton *)sender {
431 const auto& account = accountModel->getAccountInfo(self.selectedAccountID);
Kateryna Kostiuk53799642019-07-30 18:21:16 -0400432 self.accountModel->setAccountEnabled(self.selectedAccountID, !account.enabled);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400433 self.accountEnabled = account.enabled;
Kateryna Kostiuk53799642019-07-30 18:21:16 -0400434
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400435}
436
437- (IBAction)removeAccount:(id)sender
438{
439 NSAlert *alert = [[NSAlert alloc] init];
440 [alert addButtonWithTitle:@"OK"];
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400441 [alert addButtonWithTitle:NSLocalizedString(@"Cancel",@"Button Action")];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400442 [alert setMessageText: NSLocalizedString(@"Remove account",
443 @"Remove account alert title")];
444 [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.",
445 @"Remove account alert message")];
446
447 if ([alert runModal] == NSAlertFirstButtonReturn) {
448 self.accountModel->removeAccount(self.selectedAccountID);
449 }
450}
451
452- (IBAction)exportAccount:(id)sender
453{
Kateryna Kostiuk740a5962019-03-28 12:56:08 -0400454 NSSavePanel* filePicker = [NSSavePanel savePanel];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400455 NSString* name = [self.selectedAccountID.toNSString() stringByAppendingString: @".gz"];
Kateryna Kostiuk740a5962019-03-28 12:56:08 -0400456 [filePicker setNameFieldStringValue: name];
Kateryna Kostiukf4b1a792019-09-19 11:26:19 -0400457 if ([filePicker runModal] != NSFileHandlingPanelOKButton) {
458 return;
459 }
460 NSString *password = @"";
461 const char* fullPath = [[filePicker URL] fileSystemRepresentation];
462 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
463 if(accountProperties.archiveHasPassword) {
464 NSAlert *alert = [[NSAlert alloc] init];
465 [alert addButtonWithTitle:@"OK"];
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400466 [alert addButtonWithTitle:NSLocalizedString(@"Cancel",@"Button Action")];
Kateryna Kostiukf4b1a792019-09-19 11:26:19 -0400467 [alert setMessageText: NSLocalizedString(@"Enter account password",
468 @"Backup enter password")];
469 NSTextField *input = [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 20)];
470 [alert setAccessoryView:input];
471 if ([alert runModal] != NSAlertFirstButtonReturn) {
472 return;
Kateryna Kostiuk740a5962019-03-28 12:56:08 -0400473 }
Kateryna Kostiukf4b1a792019-09-19 11:26:19 -0400474 password = [input stringValue];
475 }
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400476 if (self.accountModel->exportToFile(self.selectedAccountID, fullPath, QString::fromNSString(password))) {
Kateryna Kostiukf4b1a792019-09-19 11:26:19 -0400477 [self didCompleteExportWithPath:[filePicker URL]];
478 } else {
479 [self showAlertWithTitle: @"" andText: NSLocalizedString(@"An error occured during the backup", @"Backup error")];
Kateryna Kostiuk740a5962019-03-28 12:56:08 -0400480 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400481}
482
483- (IBAction)startNameRegistration:(id)sender
484{
485 RegisterNameWC* registerWC = [[RegisterNameWC alloc] initWithNibName:@"RegisterNameWindow" bundle: nil accountmodel: self.accountModel];
486 registerWC.delegate = self;
487 registerWC.selectedAccountID = self.selectedAccountID;
488 self.accountModal = registerWC;
489 [self.view.window beginSheet:registerWC.window completionHandler:nil];
490}
491- (IBAction)changePassword:(id)sender
492{
493 PasswordChangeWC* passwordWC = [[PasswordChangeWC alloc] initWithNibName:@"PasswordChange" bundle: nil accountmodel: self.accountModel];
494 passwordWC.selectedAccountID = self.selectedAccountID;
495 passwordWC.delegate = self;
496 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
497 self.passwordModal = passwordWC;
498}
499
500- (IBAction)showBanned: (NSButton *)sender {
501 CGFloat height = self.bannedContactHeightConstraint.constant;
502 NSRect frame = self.view.frame;
503 if(height == 150) {
504 frame.size.height = frame.size.height - 150 - 10;
505 } else {
506 frame.size.height = frame.size.height + 150 + 10;
507 }
508 self.view.frame = frame;
509 [self.delegate updateFrame];
510 CGFloat advancedHeight = self.advancedButtonMarginConstraint.constant;
511 self.advancedButtonMarginConstraint.constant = (height== 2) ? 40 : 30;
512 self.bannedContactHeightConstraint.constant = (height== 2) ? 150 : 2;
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -0400513 [[[[self.blockedContactsTableView superview] superview] superview] setHidden:![[[[self.blockedContactsTableView superview] superview] superview] isHidden]];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400514 [blockedContactsTableView reloadData];
515}
516
517-(void) hideBannedContacts {
518 CGFloat height = self.bannedContactHeightConstraint.constant;
519 NSRect frame = self.view.frame;
520 if(height == 150) {
521 [self showBanned:nil];
522 }
523}
524
525- (IBAction)startDeviceRevocation:(NSView*)sender
526{
527 NSInteger row = [devicesTableView rowForView:sender];
528 if(row < 0) {
529 return;
530 }
531 auto devices = self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices();
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400532 if (devices.size() < row) {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400533 return;
534 }
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400535 auto device = devices[row];
536 [self proceedDeviceRevokationAlert:device.id];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400537}
538
539- (IBAction)unblockContact:(NSView*)sender
540{
541 NSInteger row = [blockedContactsTableView rowForView:sender];
542 if(row < 0) {
543 return;
544 }
545 auto contacts = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts();
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400546 auto contactID = contacts[row];
547 try {
548 auto contact = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getContact(contactID);
549 if(!contact.isBanned) {
550 return;
551 }
552 self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->addContact(contact);
553 } catch (std::out_of_range& e) {
554 NSLog(@"contact out of range");
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400555 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400556}
557
558- (IBAction)editDevice:(NSView*)sender
559{
560 NSInteger row = [devicesTableView rowForView:sender];
561 if(row < 0) {
562 return;
563 }
564
565 NSTableCellView* deviceView = [devicesTableView viewAtColumn:0 row:row makeIfNecessary:NO];
566 if(!deviceView || ![deviceView isKindOfClass:[NSTableCellView class]]) {
567 return;
568 }
569
570 NSTextField* nameLabel = [deviceView viewWithTag: DEVICE_NAME_TAG];
571 NSButton* editButton = [deviceView viewWithTag: DEVICE_EDIT_TAG];
572 if ([nameLabel isEditable]) {
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400573 self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->setCurrentDeviceName(QString::fromNSString(nameLabel.stringValue));
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400574 [nameLabel setEditable:NO];
575 [self.view.window makeFirstResponder:nil];
576 editButton.image = [NSImage imageNamed:NSImageNameTouchBarComposeTemplate];
577 return;
578 }
579 [nameLabel setEditable:YES];
580 [nameLabel becomeFirstResponder];
581 editButton.image = [NSImage imageNamed:NSImageNameTouchBarDownloadTemplate];
582}
583
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400584-(void) revokeDeviceWithID: (const QString&) deviceID password:(NSString *) password {
585 self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->revokeDevice(deviceID, QString::fromNSString(password));
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400586}
587
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400588-(void) proceedDeviceRevokationAlert: (const QString&) deviceID {
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400589 NSAlert *alert = [[NSAlert alloc] init];
590 [alert addButtonWithTitle:@"OK"];
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400591 [alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"Button Action")];
592 [alert setMessageText:NSLocalizedString(@"Revoke Device", @"Device revocation title")];
593 [alert setInformativeText:NSLocalizedString(@"Attention! This action could not be undone!",
594 @"Device revocation message")];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400595 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
596 if(accountProperties.archiveHasPassword) {
597 NSSecureTextField *passwordText = [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400598 [passwordText setPlaceholderString:NSLocalizedString(@"Enter account password", @"Backup enter password")];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400599 [alert setAccessoryView:passwordText];
600 if ([alert runModal] == NSAlertFirstButtonReturn) {
601 [self revokeDeviceWithID:deviceID password:[passwordText stringValue]];
602 }
603 } else {
604 if ([alert runModal] == NSAlertFirstButtonReturn) {
605 [self revokeDeviceWithID:deviceID password:@""];
606 }
607 }
608}
609
610#pragma mark - BackupAccountDelegate methods
611
612-(void) didCompleteExportWithPath:(NSURL*) fileUrl
613{
614 [[NSWorkspace sharedWorkspace] selectFile:fileUrl.path inFileViewerRootedAtPath:@""];
615}
616
617#pragma mark - PasswordChangeDelegate
618
619-(void) paswordCreatedWithSuccess:(BOOL) success
620{
Kateryna Kostiuk92a30322020-05-08 11:11:52 -0400621 NSString *title = success ? NSLocalizedString(@"Change password", @"Password button title") :
622 NSLocalizedString(@"Create password", @"Password button title");
623 [passwordButton setTitle: title];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400624}
625
626@end