blob: 90a8280514b1ba788d70fc9c83f58016f5236e89 [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;
70@property std::string selectedAccountID;
71
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
136- (void) setSelectedAccount:(std::string) account {
137 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 Kostiuk43a79232018-10-18 15:45:18 -0400146 NSData *imageData = [[NSData alloc] initWithBase64EncodedString:@(account.profileInfo.avatar.c_str()) options:NSDataBase64DecodingIgnoreUnknownCharacters];
147 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 }
157 NSString* displayName = @(account.profileInfo.alias.c_str());
158 [displayNameField setStringValue:displayName];
159 [ringIDField setStringValue:@(account.profileInfo.uri.c_str())];
Kateryna Kostiuk9a2791a2019-10-28 10:38:11 -0400160
161 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
162 bool hideLocalAccountConfig = !accountProperties.managerUri.empty();
163 [passwordButton setHidden:hideLocalAccountConfig];
164 [linkDeviceButton setHidden:hideLocalAccountConfig];
165 [passwordField setHidden:hideLocalAccountConfig];
166 [exportAccountButton setHidden: hideLocalAccountConfig];
167
168 if(account.registeredName.empty() && !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
176 [registeredNameField setStringValue:@(account.registeredName.c_str())];
177
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400178 [passwordButton setTitle:accountProperties.archiveHasPassword ? @"Change password" : @"Create password"];
179 self.accountEnabled = account.enabled;
180
181 NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[removeAccountButton attributedTitle]];
182 NSRange titleRange = NSMakeRange(0, [colorTitle length]);
183 [colorTitle addAttribute:NSForegroundColorAttributeName value:[NSColor errorColor] range:titleRange];
184 [removeAccountButton setAttributedTitle:colorTitle];
185 [devicesTableView reloadData];
186 [blockedContactsTableView reloadData];
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500187 self.accountStatus.bgColor = colorForAccountStatus(accountModel->getAccountInfo(self.selectedAccountID).status);
188 [self.accountStatus setNeedsDisplay:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400189}
190
191-(void) connectSignals {
192 QObject::disconnect(deviceAddedSignal);
193 QObject::disconnect(deviceRevokedSignal);
194 QObject::disconnect(deviceUpdatedSignal);
195 QObject::disconnect(bannedContactsChangedSignal);
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500196 QObject::disconnect(accountStateChangedSignal);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400197 deviceAddedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
198 &lrc::api::NewDeviceModel::deviceAdded,
199 [self] (const std::string &id) {
200 [devicesTableView reloadData];
201 });
202 deviceRevokedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
203 &lrc::api::NewDeviceModel::deviceRevoked,
204 [self] (const std::string &id, const lrc::api::NewDeviceModel::Status status) {
205 switch (status) {
206 case lrc::api::NewDeviceModel::Status::SUCCESS:
207 [devicesTableView reloadData];
208 break;
209 case lrc::api::NewDeviceModel::Status::WRONG_PASSWORD:
210 [self showAlertWithTitle: @"" andText: @"Device revocation failed with error: Wrong password"];
211 break;
212 case lrc::api::NewDeviceModel::Status::UNKNOWN_DEVICE:
213 [self showAlertWithTitle: @"" andText: @"Device revocation failed with error: Unknown device"];
214 break;
215 }
216 });
217 deviceUpdatedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
218 &lrc::api::NewDeviceModel::deviceUpdated,
219 [self] (const std::string &id) {
220 [devicesTableView reloadData];
221 });
222 bannedContactsChangedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).contactModel,
223 &lrc::api::ContactModel::bannedStatusChanged,
224 [self] (const std::string &contactUri, bool banned) {
225 [blockedContactsTableView reloadData];
226 });
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500227 accountStateChangedSignal = QObject::connect(self.accountModel,
228 &lrc::api::NewAccountModel::accountStatusChanged,
229 [self] (const std::string& accountID) {
230 if(accountID != self.selectedAccountID) {
231 return;
232 }
233 self.accountStatus.bgColor = colorForAccountStatus(accountModel->getAccountInfo(accountID).status);
234 [self.accountStatus setNeedsDisplay:YES];
235 });
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400236}
237
238-(void) showAlertWithTitle: (NSString *) title andText: (NSString *)text {
239 NSAlert *alert = [[NSAlert alloc] init];
240 [alert addButtonWithTitle:@"OK"];
241 [alert setMessageText:title];
242 [alert setInformativeText:text];
243 [alert runModal];
244}
245
246- (void)pictureTakerDidEnd:(IKPictureTaker *) picker
247 returnCode:(NSInteger) code
248 contextInfo:(void*) contextInfo
249{
Kateryna Kostiuk256814e2018-09-04 14:47:33 -0400250 //do nothing when editing canceled
251 if (code == 0) {
252 return;
253 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400254 if (auto outputImage = [picker outputImage]) {
255 auto image = [picker inputImage];
Kateryna Kostiuk2f2ef952019-05-07 11:04:41 -0400256 CGFloat newSize = MIN(MIN(image.size.height, image.size.width), MAX_IMAGE_SIZE);
257 outputImage = [outputImage imageResizeInsideMax: newSize];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400258 [photoView setImage: [outputImage roundCorners: outputImage.size.height * 0.5]];
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400259 [photoView setBordered:NO];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400260 [addProfilePhotoImage setHidden:YES];
261 auto imageToBytes = QByteArray::fromNSData([outputImage TIFFRepresentation]).toBase64();
262 std::string imageToString = std::string(imageToBytes.constData(), imageToBytes.length());
263 self.accountModel->setAvatar(self.selectedAccountID, imageToString);
264 } else if(!photoView.image) {
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400265 [photoView setBordered:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400266 [addProfilePhotoImage setHidden:NO];
267 }
268}
269
270#pragma mark - RegisterNameDelegate methods
271
272- (void) didRegisterName:(NSString *) name withSuccess:(BOOL) success
273{
274 [self.accountModal close];
275 if(!success) {
276 return;
277 }
278
279 if(name.length == 0) {
280 return;
281 }
282 buttonRegisterWidthConstraint.constant = 0.0;
283 [registerNameButton setHidden:YES];
284 [registeredNameField setStringValue:name];
Kateryna Kostiukdf680762018-10-29 13:53:52 -0400285 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
286 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400287}
288
289#pragma mark - NSTextFieldDelegate delegate methods
290
291- (void)controlTextDidChange:(NSNotification *)notif
292{
293 NSTextField* textField = [notif object];
294 if (textField.tag != DISPLAYNAME) {
295 return;
296 }
297 NSString* displayName = textField.stringValue;
298
299 [NSObject cancelPreviousPerformRequestsWithTarget:self];
300 self.accountModel->setAlias(self.selectedAccountID, [displayName UTF8String]);
301 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
302 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
303}
304
305#pragma mark - NSTableViewDataSource methods
306
307- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
308 if(tableView == devicesTableView) {
309 return self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices().size();
310 } else if (tableView == blockedContactsTableView){
311 return self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts().size();
312 }
313 return 0;
314}
315
316#pragma mark - NSTableViewDelegate methods
317- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
318{
319 if(tableView == devicesTableView) {
320 NSTableCellView* deviceView = [tableView makeViewWithIdentifier:@"TableCellDeviceItem" owner:self];
321 NSTextField* nameLabel = [deviceView viewWithTag: DEVICE_NAME_TAG];
322 NSTextField* idLabel = [deviceView viewWithTag: DEVICE_ID_TAG];
323 NSButton* revokeButton = [deviceView viewWithTag: DEVICE_REVOKE_TAG];
324 NSButton* editButton = [deviceView viewWithTag: DEVICE_EDIT_TAG];
325 [editButton setAction:@selector(editDevice:)];
326 [editButton setTarget:self];
327 [revokeButton setAction:@selector(startDeviceRevocation:)];
328 [revokeButton setTarget:self];
329 auto devices = self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices();
330 auto device = devices.begin();
331
332 std::advance(device, row);
333
334 auto name = device->name;
335 auto deviceID = device->id;
336
337 [nameLabel setStringValue: @(name.c_str())];
338 [idLabel setStringValue: @(deviceID.c_str())];
339 [revokeButton setHidden: device->isCurrent];
340 [editButton setHidden: !device->isCurrent];
341 return deviceView;
342 } else if (tableView == blockedContactsTableView) {
343 NSTableCellView* contactView = [tableView makeViewWithIdentifier:@"TableCellBannedContactItem" owner:self];
344 NSTextField* nameLabel = [contactView viewWithTag: BANNED_CONTACT_NAME_TAG];
345 NSTextField* idLabel = [contactView viewWithTag: BANNED_CONTACT_ID_TAG];
346 NSButton* revokeButton = [contactView viewWithTag: UNBLOCK_CONTACT_TAG];
347 auto contacts = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts();
348 auto contactID = contacts.begin();
349 std::advance(contactID, row);
350 [idLabel setStringValue: @(contactID->c_str())];
351 auto contact = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getContact([@(contactID->c_str()) UTF8String]);
352 [nameLabel setStringValue: bestNameForContact(contact)];
353 [revokeButton setAction:@selector(unblockContact:)];
354 [revokeButton setTarget:self];
355 return contactView;
356 }
357}
358
359- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
360{
361 if(tableView == devicesTableView) {
362 return tableView.rowHeight;
363 } else if (tableView == blockedContactsTableView) {
364 CGFloat height = self.bannedContactHeightConstraint.constant;
365 if(height == 150) {
366 return 52;
367 } else {
368 return 1;
369 }
370 }
371}
372
373- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
374{
375 return [tableView makeViewWithIdentifier:@"HoverRowView" owner:nil];
376}
377
378#pragma mark - Actions
379
380- (IBAction)editPhoto:(id)sender
381{
382 auto pictureTaker = [IKPictureTaker pictureTaker];
Kateryna Kostiukdc563242019-08-19 12:05:54 -0400383#if __MAC_OS_X_VERSION_MAX_ALLOWED >= 101400
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400384 if (@available(macOS 10.14, *)) {
385 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
386 if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied)
387 {
388 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
389 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400390
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400391 if(authStatus == AVAuthorizationStatusNotDetermined)
392 {
393 [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
394 if(!granted){
395 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
396 }
397 }];
398 }
399 }
Kateryna Kostiukdc563242019-08-19 12:05:54 -0400400#endif
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400401 [pictureTaker beginPictureTakerSheetForWindow:[self.view window]
402 withDelegate:self
403 didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:)
404 contextInfo:nil];
405
406}
407
408- (IBAction)startExportOnRing:(id)sender
409{
410 ExportPasswordWC *passwordWC = [[ExportPasswordWC alloc] initWithNibName:@"ExportPasswordWindow" bundle: nil accountmodel: self.accountModel];
411 passwordWC.selectedAccountID = self.selectedAccountID;
412 accountModal = passwordWC;
413 [self.view.window beginSheet: passwordWC.window completionHandler:nil];
414}
415- (IBAction)triggerAdwancedSettings: (NSButton *)sender {
416 [self.delegate triggerAdvancedOptions];
417}
418
419- (IBAction)enableAccount: (NSButton *)sender {
420 const auto& account = accountModel->getAccountInfo(self.selectedAccountID);
Kateryna Kostiuk53799642019-07-30 18:21:16 -0400421 self.accountModel->setAccountEnabled(self.selectedAccountID, !account.enabled);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400422 self.accountEnabled = account.enabled;
Kateryna Kostiuk53799642019-07-30 18:21:16 -0400423
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400424}
425
426- (IBAction)removeAccount:(id)sender
427{
428 NSAlert *alert = [[NSAlert alloc] init];
429 [alert addButtonWithTitle:@"OK"];
430 [alert addButtonWithTitle:@"Cancel"];
431 [alert setMessageText: NSLocalizedString(@"Remove account",
432 @"Remove account alert title")];
433 [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.",
434 @"Remove account alert message")];
435
436 if ([alert runModal] == NSAlertFirstButtonReturn) {
437 self.accountModel->removeAccount(self.selectedAccountID);
438 }
439}
440
441- (IBAction)exportAccount:(id)sender
442{
Kateryna Kostiuk740a5962019-03-28 12:56:08 -0400443 NSSavePanel* filePicker = [NSSavePanel savePanel];
444 NSString* name = [@(self.selectedAccountID.c_str()) stringByAppendingString: @".gz"];
445 [filePicker setNameFieldStringValue: name];
Kateryna Kostiukf4b1a792019-09-19 11:26:19 -0400446 if ([filePicker runModal] != NSFileHandlingPanelOKButton) {
447 return;
448 }
449 NSString *password = @"";
450 const char* fullPath = [[filePicker URL] fileSystemRepresentation];
451 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
452 if(accountProperties.archiveHasPassword) {
453 NSAlert *alert = [[NSAlert alloc] init];
454 [alert addButtonWithTitle:@"OK"];
455 [alert addButtonWithTitle:@"Cancel"];
456 [alert setMessageText: NSLocalizedString(@"Enter account password",
457 @"Backup enter password")];
458 NSTextField *input = [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 20)];
459 [alert setAccessoryView:input];
460 if ([alert runModal] != NSAlertFirstButtonReturn) {
461 return;
Kateryna Kostiuk740a5962019-03-28 12:56:08 -0400462 }
Kateryna Kostiukf4b1a792019-09-19 11:26:19 -0400463 password = [input stringValue];
464 }
465 if (self.accountModel->exportToFile(self.selectedAccountID, fullPath, [password UTF8String])) {
466 [self didCompleteExportWithPath:[filePicker URL]];
467 } else {
468 [self showAlertWithTitle: @"" andText: NSLocalizedString(@"An error occured during the backup", @"Backup error")];
Kateryna Kostiuk740a5962019-03-28 12:56:08 -0400469 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400470}
471
472- (IBAction)startNameRegistration:(id)sender
473{
474 RegisterNameWC* registerWC = [[RegisterNameWC alloc] initWithNibName:@"RegisterNameWindow" bundle: nil accountmodel: self.accountModel];
475 registerWC.delegate = self;
476 registerWC.selectedAccountID = self.selectedAccountID;
477 self.accountModal = registerWC;
478 [self.view.window beginSheet:registerWC.window completionHandler:nil];
479}
480- (IBAction)changePassword:(id)sender
481{
482 PasswordChangeWC* passwordWC = [[PasswordChangeWC alloc] initWithNibName:@"PasswordChange" bundle: nil accountmodel: self.accountModel];
483 passwordWC.selectedAccountID = self.selectedAccountID;
484 passwordWC.delegate = self;
485 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
486 self.passwordModal = passwordWC;
487}
488
489- (IBAction)showBanned: (NSButton *)sender {
490 CGFloat height = self.bannedContactHeightConstraint.constant;
491 NSRect frame = self.view.frame;
492 if(height == 150) {
493 frame.size.height = frame.size.height - 150 - 10;
494 } else {
495 frame.size.height = frame.size.height + 150 + 10;
496 }
497 self.view.frame = frame;
498 [self.delegate updateFrame];
499 CGFloat advancedHeight = self.advancedButtonMarginConstraint.constant;
500 self.advancedButtonMarginConstraint.constant = (height== 2) ? 40 : 30;
501 self.bannedContactHeightConstraint.constant = (height== 2) ? 150 : 2;
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -0400502 [[[[self.blockedContactsTableView superview] superview] superview] setHidden:![[[[self.blockedContactsTableView superview] superview] superview] isHidden]];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400503 [blockedContactsTableView reloadData];
504}
505
506-(void) hideBannedContacts {
507 CGFloat height = self.bannedContactHeightConstraint.constant;
508 NSRect frame = self.view.frame;
509 if(height == 150) {
510 [self showBanned:nil];
511 }
512}
513
514- (IBAction)startDeviceRevocation:(NSView*)sender
515{
516 NSInteger row = [devicesTableView rowForView:sender];
517 if(row < 0) {
518 return;
519 }
520 auto devices = self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices();
521 auto device = devices.begin();
522 std::advance(device, row);
523 if(device == devices.end()) {
524 return;
525 }
526 [self proceedDeviceRevokationAlert:device->id];
527}
528
529- (IBAction)unblockContact:(NSView*)sender
530{
531 NSInteger row = [blockedContactsTableView rowForView:sender];
532 if(row < 0) {
533 return;
534 }
535 auto contacts = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts();
536 auto contactID = contacts.begin();
537 std::advance(contactID, row);
538 if(contactID == contacts.end()) {
539 return;
540 }
541 auto contact = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getContact([@(contactID->c_str()) UTF8String]);
542 if(!contact.isBanned) {
543 return;
544 }
545 self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->addContact(contact);
546}
547
548- (IBAction)editDevice:(NSView*)sender
549{
550 NSInteger row = [devicesTableView rowForView:sender];
551 if(row < 0) {
552 return;
553 }
554
555 NSTableCellView* deviceView = [devicesTableView viewAtColumn:0 row:row makeIfNecessary:NO];
556 if(!deviceView || ![deviceView isKindOfClass:[NSTableCellView class]]) {
557 return;
558 }
559
560 NSTextField* nameLabel = [deviceView viewWithTag: DEVICE_NAME_TAG];
561 NSButton* editButton = [deviceView viewWithTag: DEVICE_EDIT_TAG];
562 if ([nameLabel isEditable]) {
563 self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->setCurrentDeviceName([nameLabel.stringValue UTF8String]);
564 [nameLabel setEditable:NO];
565 [self.view.window makeFirstResponder:nil];
566 editButton.image = [NSImage imageNamed:NSImageNameTouchBarComposeTemplate];
567 return;
568 }
569 [nameLabel setEditable:YES];
570 [nameLabel becomeFirstResponder];
571 editButton.image = [NSImage imageNamed:NSImageNameTouchBarDownloadTemplate];
572}
573
574-(void) revokeDeviceWithID: (std::string) deviceID password:(NSString *) password {
575 self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->revokeDevice(deviceID, [password UTF8String]);
576}
577
578-(void) proceedDeviceRevokationAlert: (std::string) deviceID {
579 NSAlert *alert = [[NSAlert alloc] init];
580 [alert addButtonWithTitle:@"OK"];
581 [alert addButtonWithTitle:@"Cancel"];
582 [alert setMessageText:@"Revoke Device"];
583 [alert setInformativeText:@"Attention! This action could not be undone!"];
584 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
585 if(accountProperties.archiveHasPassword) {
586 NSSecureTextField *passwordText = [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
587 [passwordText setPlaceholderString:@"Enter password"];
588 [alert setAccessoryView:passwordText];
589 if ([alert runModal] == NSAlertFirstButtonReturn) {
590 [self revokeDeviceWithID:deviceID password:[passwordText stringValue]];
591 }
592 } else {
593 if ([alert runModal] == NSAlertFirstButtonReturn) {
594 [self revokeDeviceWithID:deviceID password:@""];
595 }
596 }
597}
598
599#pragma mark - BackupAccountDelegate methods
600
601-(void) didCompleteExportWithPath:(NSURL*) fileUrl
602{
603 [[NSWorkspace sharedWorkspace] selectFile:fileUrl.path inFileViewerRootedAtPath:@""];
604}
605
606#pragma mark - PasswordChangeDelegate
607
608-(void) paswordCreatedWithSuccess:(BOOL) success
609{
610 [passwordButton setTitle: success ? @"Change password" : @"Create password"];
611}
612
613@end