blob: 2a220f4f12b3fc027983abc8888bd9cab58d998c [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"
45
46@interface AccRingGeneralVC ()
47
48@property (unsafe_unretained) IBOutlet NSTextField *displayNameField;
49@property (unsafe_unretained) IBOutlet NSTextField *ringIDField;
50@property (unsafe_unretained) IBOutlet NSTextField *registeredNameField;
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -050051@property (unsafe_unretained) IBOutlet RoundedTextField *accountStatus;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040052@property (unsafe_unretained) IBOutlet NSButton *registerNameButton;
53@property (unsafe_unretained) IBOutlet NSButton* photoView;
54@property (unsafe_unretained) IBOutlet NSButton* passwordButton;
55@property (unsafe_unretained) IBOutlet NSButton* removeAccountButton;
56@property (unsafe_unretained) IBOutlet NSImageView* addProfilePhotoImage;
57@property (unsafe_unretained) IBOutlet NSTableView* devicesTableView;
58@property (unsafe_unretained) IBOutlet NSTableView* blockedContactsTableView;
59@property (assign) IBOutlet NSLayoutConstraint* buttonRegisterWidthConstraint;
60@property (assign) IBOutlet NSLayoutConstraint* bannedContactHeightConstraint;
61@property (assign) IBOutlet NSLayoutConstraint* advancedButtonMarginConstraint;
62
63
64@property AbstractLoadingWC* accountModal;
65@property PasswordChangeWC* passwordModal;
66@property std::string selectedAccountID;
67
68@end
69
70@implementation AccRingGeneralVC
71
72QMetaObject::Connection deviceAddedSignal;
73QMetaObject::Connection deviceRevokedSignal;
74QMetaObject::Connection deviceUpdatedSignal;
75QMetaObject::Connection contactBlockedSignal;
76QMetaObject::Connection bannedContactsChangedSignal;
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -050077QMetaObject::Connection accountStateChangedSignal;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040078
79
80@synthesize displayNameField;
81@synthesize ringIDField;
82@synthesize registeredNameField;
83@synthesize photoView;
84@synthesize addProfilePhotoImage;
85@synthesize accountModel;
86@synthesize registerNameButton, passwordButton, removeAccountButton;
87@synthesize buttonRegisterWidthConstraint;
88@synthesize accountModal;
89@synthesize delegate;
90@synthesize devicesTableView;
91@synthesize blockedContactsTableView;
92
93
94typedef NS_ENUM(NSInteger, TagViews) {
95 DISPLAYNAME = 100,
96 DEVICE_NAME_TAG = 200,
97 DEVICE_ID_TAG = 300,
98 DEVICE_EDIT_TAG = 400,
99 DEVICE_REVOKE_TAG = 500,
100 BANNED_CONTACT_NAME_TAG = 600,
101 BANNED_CONTACT_ID_TAG = 700,
102 UNBLOCK_CONTACT_TAG = 800
103};
104
105-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
106{
107 if (self = [self initWithNibName: nibNameOrNil bundle:nibBundleOrNil])
108 {
109 self.accountModel= accountModel;
110 }
111 return self;
112}
113
114- (void)awakeFromNib
115{
116 [super awakeFromNib];
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400117 [photoView setBordered:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400118 [addProfilePhotoImage setWantsLayer: YES];
119 devicesTableView.delegate = self;
120 devicesTableView.dataSource = self;
121 blockedContactsTableView.delegate = self;
122 blockedContactsTableView.dataSource= self;
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400123 [[self view] setAutoresizingMask: NSViewMinXMargin | NSViewMaxXMargin];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400124}
125
126- (void)viewDidLoad {
127 [super viewDidLoad];
128 [self updateView];
129}
130
131- (void) setSelectedAccount:(std::string) account {
132 self.selectedAccountID = account;
133 [self connectSignals];
134 [self updateView];
135 [self hideBannedContacts];
136}
137
138-(void) updateView {
139 const auto& account = accountModel->getAccountInfo(self.selectedAccountID);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400140
Kateryna Kostiuk43a79232018-10-18 15:45:18 -0400141 NSData *imageData = [[NSData alloc] initWithBase64EncodedString:@(account.profileInfo.avatar.c_str()) options:NSDataBase64DecodingIgnoreUnknownCharacters];
142 NSImage *image = [[NSImage alloc] initWithData:imageData];
143 if(image) {
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400144 [photoView setBordered:NO];
Kateryna Kostiuk43a79232018-10-18 15:45:18 -0400145 [photoView setImage: [image roundCorners: 350]];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400146 [addProfilePhotoImage setHidden:YES];
147 } else {
148 [photoView setImage:nil];
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400149 [photoView setBordered:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400150 [addProfilePhotoImage setHidden:NO];
151 }
152 NSString* displayName = @(account.profileInfo.alias.c_str());
153 [displayNameField setStringValue:displayName];
154 [ringIDField setStringValue:@(account.profileInfo.uri.c_str())];
155 if(account.registeredName.empty()) {
156 [registerNameButton setHidden:NO];
157 buttonRegisterWidthConstraint.constant = 260.0;
158 } else {
159 buttonRegisterWidthConstraint.constant = 0.0;
160 [registerNameButton setHidden:YES];
161 }
162
163 [registeredNameField setStringValue:@(account.registeredName.c_str())];
164
165 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
166 [passwordButton setTitle:accountProperties.archiveHasPassword ? @"Change password" : @"Create password"];
167 self.accountEnabled = account.enabled;
168
169 NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[removeAccountButton attributedTitle]];
170 NSRange titleRange = NSMakeRange(0, [colorTitle length]);
171 [colorTitle addAttribute:NSForegroundColorAttributeName value:[NSColor errorColor] range:titleRange];
172 [removeAccountButton setAttributedTitle:colorTitle];
173 [devicesTableView reloadData];
174 [blockedContactsTableView reloadData];
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500175 self.accountStatus.bgColor = colorForAccountStatus(accountModel->getAccountInfo(self.selectedAccountID).status);
176 [self.accountStatus setNeedsDisplay:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400177}
178
179-(void) connectSignals {
180 QObject::disconnect(deviceAddedSignal);
181 QObject::disconnect(deviceRevokedSignal);
182 QObject::disconnect(deviceUpdatedSignal);
183 QObject::disconnect(bannedContactsChangedSignal);
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500184 QObject::disconnect(accountStateChangedSignal);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400185 deviceAddedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
186 &lrc::api::NewDeviceModel::deviceAdded,
187 [self] (const std::string &id) {
188 [devicesTableView reloadData];
189 });
190 deviceRevokedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
191 &lrc::api::NewDeviceModel::deviceRevoked,
192 [self] (const std::string &id, const lrc::api::NewDeviceModel::Status status) {
193 switch (status) {
194 case lrc::api::NewDeviceModel::Status::SUCCESS:
195 [devicesTableView reloadData];
196 break;
197 case lrc::api::NewDeviceModel::Status::WRONG_PASSWORD:
198 [self showAlertWithTitle: @"" andText: @"Device revocation failed with error: Wrong password"];
199 break;
200 case lrc::api::NewDeviceModel::Status::UNKNOWN_DEVICE:
201 [self showAlertWithTitle: @"" andText: @"Device revocation failed with error: Unknown device"];
202 break;
203 }
204 });
205 deviceUpdatedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
206 &lrc::api::NewDeviceModel::deviceUpdated,
207 [self] (const std::string &id) {
208 [devicesTableView reloadData];
209 });
210 bannedContactsChangedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).contactModel,
211 &lrc::api::ContactModel::bannedStatusChanged,
212 [self] (const std::string &contactUri, bool banned) {
213 [blockedContactsTableView reloadData];
214 });
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500215 accountStateChangedSignal = QObject::connect(self.accountModel,
216 &lrc::api::NewAccountModel::accountStatusChanged,
217 [self] (const std::string& accountID) {
218 if(accountID != self.selectedAccountID) {
219 return;
220 }
221 self.accountStatus.bgColor = colorForAccountStatus(accountModel->getAccountInfo(accountID).status);
222 [self.accountStatus setNeedsDisplay:YES];
223 });
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400224}
225
226-(void) showAlertWithTitle: (NSString *) title andText: (NSString *)text {
227 NSAlert *alert = [[NSAlert alloc] init];
228 [alert addButtonWithTitle:@"OK"];
229 [alert setMessageText:title];
230 [alert setInformativeText:text];
231 [alert runModal];
232}
233
234- (void)pictureTakerDidEnd:(IKPictureTaker *) picker
235 returnCode:(NSInteger) code
236 contextInfo:(void*) contextInfo
237{
Kateryna Kostiuk256814e2018-09-04 14:47:33 -0400238 //do nothing when editing canceled
239 if (code == 0) {
240 return;
241 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400242 if (auto outputImage = [picker outputImage]) {
243 auto image = [picker inputImage];
244 CGFloat newSize = MIN(image.size.height, image.size.width);
245 outputImage = [outputImage cropImageToSize:CGSizeMake(newSize, newSize)];
246 [photoView setImage: [outputImage roundCorners: outputImage.size.height * 0.5]];
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400247 [photoView setBordered:NO];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400248 [addProfilePhotoImage setHidden:YES];
249 auto imageToBytes = QByteArray::fromNSData([outputImage TIFFRepresentation]).toBase64();
250 std::string imageToString = std::string(imageToBytes.constData(), imageToBytes.length());
251 self.accountModel->setAvatar(self.selectedAccountID, imageToString);
252 } else if(!photoView.image) {
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400253 [photoView setBordered:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400254 [addProfilePhotoImage setHidden:NO];
255 }
256}
257
258#pragma mark - RegisterNameDelegate methods
259
260- (void) didRegisterName:(NSString *) name withSuccess:(BOOL) success
261{
262 [self.accountModal close];
263 if(!success) {
264 return;
265 }
266
267 if(name.length == 0) {
268 return;
269 }
270 buttonRegisterWidthConstraint.constant = 0.0;
271 [registerNameButton setHidden:YES];
272 [registeredNameField setStringValue:name];
Kateryna Kostiukdf680762018-10-29 13:53:52 -0400273 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
274 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400275}
276
277#pragma mark - NSTextFieldDelegate delegate methods
278
279- (void)controlTextDidChange:(NSNotification *)notif
280{
281 NSTextField* textField = [notif object];
282 if (textField.tag != DISPLAYNAME) {
283 return;
284 }
285 NSString* displayName = textField.stringValue;
286
287 [NSObject cancelPreviousPerformRequestsWithTarget:self];
288 self.accountModel->setAlias(self.selectedAccountID, [displayName UTF8String]);
289 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
290 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
291}
292
293#pragma mark - NSTableViewDataSource methods
294
295- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
296 if(tableView == devicesTableView) {
297 return self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices().size();
298 } else if (tableView == blockedContactsTableView){
299 return self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts().size();
300 }
301 return 0;
302}
303
304#pragma mark - NSTableViewDelegate methods
305- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
306{
307 if(tableView == devicesTableView) {
308 NSTableCellView* deviceView = [tableView makeViewWithIdentifier:@"TableCellDeviceItem" owner:self];
309 NSTextField* nameLabel = [deviceView viewWithTag: DEVICE_NAME_TAG];
310 NSTextField* idLabel = [deviceView viewWithTag: DEVICE_ID_TAG];
311 NSButton* revokeButton = [deviceView viewWithTag: DEVICE_REVOKE_TAG];
312 NSButton* editButton = [deviceView viewWithTag: DEVICE_EDIT_TAG];
313 [editButton setAction:@selector(editDevice:)];
314 [editButton setTarget:self];
315 [revokeButton setAction:@selector(startDeviceRevocation:)];
316 [revokeButton setTarget:self];
317 auto devices = self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices();
318 auto device = devices.begin();
319
320 std::advance(device, row);
321
322 auto name = device->name;
323 auto deviceID = device->id;
324
325 [nameLabel setStringValue: @(name.c_str())];
326 [idLabel setStringValue: @(deviceID.c_str())];
327 [revokeButton setHidden: device->isCurrent];
328 [editButton setHidden: !device->isCurrent];
329 return deviceView;
330 } else if (tableView == blockedContactsTableView) {
331 NSTableCellView* contactView = [tableView makeViewWithIdentifier:@"TableCellBannedContactItem" owner:self];
332 NSTextField* nameLabel = [contactView viewWithTag: BANNED_CONTACT_NAME_TAG];
333 NSTextField* idLabel = [contactView viewWithTag: BANNED_CONTACT_ID_TAG];
334 NSButton* revokeButton = [contactView viewWithTag: UNBLOCK_CONTACT_TAG];
335 auto contacts = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts();
336 auto contactID = contacts.begin();
337 std::advance(contactID, row);
338 [idLabel setStringValue: @(contactID->c_str())];
339 auto contact = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getContact([@(contactID->c_str()) UTF8String]);
340 [nameLabel setStringValue: bestNameForContact(contact)];
341 [revokeButton setAction:@selector(unblockContact:)];
342 [revokeButton setTarget:self];
343 return contactView;
344 }
345}
346
347- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
348{
349 if(tableView == devicesTableView) {
350 return tableView.rowHeight;
351 } else if (tableView == blockedContactsTableView) {
352 CGFloat height = self.bannedContactHeightConstraint.constant;
353 if(height == 150) {
354 return 52;
355 } else {
356 return 1;
357 }
358 }
359}
360
361- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
362{
363 return [tableView makeViewWithIdentifier:@"HoverRowView" owner:nil];
364}
365
366#pragma mark - Actions
367
368- (IBAction)editPhoto:(id)sender
369{
370 auto pictureTaker = [IKPictureTaker pictureTaker];
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400371 if (@available(macOS 10.14, *)) {
372 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
373 if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied)
374 {
375 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
376 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400377
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400378 if(authStatus == AVAuthorizationStatusNotDetermined)
379 {
380 [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
381 if(!granted){
382 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
383 }
384 }];
385 }
386 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400387 [pictureTaker beginPictureTakerSheetForWindow:[self.view window]
388 withDelegate:self
389 didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:)
390 contextInfo:nil];
391
392}
393
394- (IBAction)startExportOnRing:(id)sender
395{
396 ExportPasswordWC *passwordWC = [[ExportPasswordWC alloc] initWithNibName:@"ExportPasswordWindow" bundle: nil accountmodel: self.accountModel];
397 passwordWC.selectedAccountID = self.selectedAccountID;
398 accountModal = passwordWC;
399 [self.view.window beginSheet: passwordWC.window completionHandler:nil];
400}
401- (IBAction)triggerAdwancedSettings: (NSButton *)sender {
402 [self.delegate triggerAdvancedOptions];
403}
404
405- (IBAction)enableAccount: (NSButton *)sender {
406 const auto& account = accountModel->getAccountInfo(self.selectedAccountID);
407 self.accountModel->enableAccount(self.selectedAccountID, !account.enabled);
408 self.accountEnabled = account.enabled;
409 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
410 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
411}
412
413- (IBAction)removeAccount:(id)sender
414{
415 NSAlert *alert = [[NSAlert alloc] init];
416 [alert addButtonWithTitle:@"OK"];
417 [alert addButtonWithTitle:@"Cancel"];
418 [alert setMessageText: NSLocalizedString(@"Remove account",
419 @"Remove account alert title")];
420 [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.",
421 @"Remove account alert message")];
422
423 if ([alert runModal] == NSAlertFirstButtonReturn) {
424 self.accountModel->removeAccount(self.selectedAccountID);
425 }
426}
427
428- (IBAction)exportAccount:(id)sender
429{
Kateryna Kostiuk740a5962019-03-28 12:56:08 -0400430 NSSavePanel* filePicker = [NSSavePanel savePanel];
431 NSString* name = [@(self.selectedAccountID.c_str()) stringByAppendingString: @".gz"];
432 [filePicker setNameFieldStringValue: name];
433
434 if ([filePicker runModal] == NSFileHandlingPanelOKButton) {
435 const char* fullPath = [[filePicker URL] fileSystemRepresentation];
436 if (self.accountModel->exportToFile(self.selectedAccountID, fullPath)) {
437 [self didCompleteExportWithPath:[filePicker URL]];
438 } else {
439 [self showAlertWithTitle: @"" andText: NSLocalizedString(@"An error occured during the backup", @"Backup error")];
440 }
441 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400442}
443
444- (IBAction)startNameRegistration:(id)sender
445{
446 RegisterNameWC* registerWC = [[RegisterNameWC alloc] initWithNibName:@"RegisterNameWindow" bundle: nil accountmodel: self.accountModel];
447 registerWC.delegate = self;
448 registerWC.selectedAccountID = self.selectedAccountID;
449 self.accountModal = registerWC;
450 [self.view.window beginSheet:registerWC.window completionHandler:nil];
451}
452- (IBAction)changePassword:(id)sender
453{
454 PasswordChangeWC* passwordWC = [[PasswordChangeWC alloc] initWithNibName:@"PasswordChange" bundle: nil accountmodel: self.accountModel];
455 passwordWC.selectedAccountID = self.selectedAccountID;
456 passwordWC.delegate = self;
457 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
458 self.passwordModal = passwordWC;
459}
460
461- (IBAction)showBanned: (NSButton *)sender {
462 CGFloat height = self.bannedContactHeightConstraint.constant;
463 NSRect frame = self.view.frame;
464 if(height == 150) {
465 frame.size.height = frame.size.height - 150 - 10;
466 } else {
467 frame.size.height = frame.size.height + 150 + 10;
468 }
469 self.view.frame = frame;
470 [self.delegate updateFrame];
471 CGFloat advancedHeight = self.advancedButtonMarginConstraint.constant;
472 self.advancedButtonMarginConstraint.constant = (height== 2) ? 40 : 30;
473 self.bannedContactHeightConstraint.constant = (height== 2) ? 150 : 2;
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -0400474 [[[[self.blockedContactsTableView superview] superview] superview] setHidden:![[[[self.blockedContactsTableView superview] superview] superview] isHidden]];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400475 [blockedContactsTableView reloadData];
476}
477
478-(void) hideBannedContacts {
479 CGFloat height = self.bannedContactHeightConstraint.constant;
480 NSRect frame = self.view.frame;
481 if(height == 150) {
482 [self showBanned:nil];
483 }
484}
485
486- (IBAction)startDeviceRevocation:(NSView*)sender
487{
488 NSInteger row = [devicesTableView rowForView:sender];
489 if(row < 0) {
490 return;
491 }
492 auto devices = self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices();
493 auto device = devices.begin();
494 std::advance(device, row);
495 if(device == devices.end()) {
496 return;
497 }
498 [self proceedDeviceRevokationAlert:device->id];
499}
500
501- (IBAction)unblockContact:(NSView*)sender
502{
503 NSInteger row = [blockedContactsTableView rowForView:sender];
504 if(row < 0) {
505 return;
506 }
507 auto contacts = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts();
508 auto contactID = contacts.begin();
509 std::advance(contactID, row);
510 if(contactID == contacts.end()) {
511 return;
512 }
513 auto contact = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getContact([@(contactID->c_str()) UTF8String]);
514 if(!contact.isBanned) {
515 return;
516 }
517 self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->addContact(contact);
518}
519
520- (IBAction)editDevice:(NSView*)sender
521{
522 NSInteger row = [devicesTableView rowForView:sender];
523 if(row < 0) {
524 return;
525 }
526
527 NSTableCellView* deviceView = [devicesTableView viewAtColumn:0 row:row makeIfNecessary:NO];
528 if(!deviceView || ![deviceView isKindOfClass:[NSTableCellView class]]) {
529 return;
530 }
531
532 NSTextField* nameLabel = [deviceView viewWithTag: DEVICE_NAME_TAG];
533 NSButton* editButton = [deviceView viewWithTag: DEVICE_EDIT_TAG];
534 if ([nameLabel isEditable]) {
535 self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->setCurrentDeviceName([nameLabel.stringValue UTF8String]);
536 [nameLabel setEditable:NO];
537 [self.view.window makeFirstResponder:nil];
538 editButton.image = [NSImage imageNamed:NSImageNameTouchBarComposeTemplate];
539 return;
540 }
541 [nameLabel setEditable:YES];
542 [nameLabel becomeFirstResponder];
543 editButton.image = [NSImage imageNamed:NSImageNameTouchBarDownloadTemplate];
544}
545
546-(void) revokeDeviceWithID: (std::string) deviceID password:(NSString *) password {
547 self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->revokeDevice(deviceID, [password UTF8String]);
548}
549
550-(void) proceedDeviceRevokationAlert: (std::string) deviceID {
551 NSAlert *alert = [[NSAlert alloc] init];
552 [alert addButtonWithTitle:@"OK"];
553 [alert addButtonWithTitle:@"Cancel"];
554 [alert setMessageText:@"Revoke Device"];
555 [alert setInformativeText:@"Attention! This action could not be undone!"];
556 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
557 if(accountProperties.archiveHasPassword) {
558 NSSecureTextField *passwordText = [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
559 [passwordText setPlaceholderString:@"Enter password"];
560 [alert setAccessoryView:passwordText];
561 if ([alert runModal] == NSAlertFirstButtonReturn) {
562 [self revokeDeviceWithID:deviceID password:[passwordText stringValue]];
563 }
564 } else {
565 if ([alert runModal] == NSAlertFirstButtonReturn) {
566 [self revokeDeviceWithID:deviceID password:@""];
567 }
568 }
569}
570
571#pragma mark - BackupAccountDelegate methods
572
573-(void) didCompleteExportWithPath:(NSURL*) fileUrl
574{
575 [[NSWorkspace sharedWorkspace] selectFile:fileUrl.path inFileViewerRootedAtPath:@""];
576}
577
578#pragma mark - PasswordChangeDelegate
579
580-(void) paswordCreatedWithSuccess:(BOOL) success
581{
582 [passwordButton setTitle: success ? @"Change password" : @"Create password"];
583}
584
585@end