blob: ffeb7a92a7472eb541f527f29453a03438e375b7 [file] [log] [blame]
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -04001/*
2 * Copyright (C) 2015-2018 Savoir-faire Linux Inc.
3 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
4 * Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20#import "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>
37#import <globalinstances.h>
38
39#import "RegisterNameWC.h"
40#import "RestoreAccountWC.h"
41#import "BackupAccountWC.h"
42#import "views/NSColor+RingTheme.h"
43#import "views/NSImage+Extensions.h"
44#import "views/HoverTableRowView.h"
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -050045#import "views/RoundedTextField.h"
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040046#import "ExportPasswordWC.h"
47#import "utils.h"
48
49@interface AccRingGeneralVC ()
50
51@property (unsafe_unretained) IBOutlet NSTextField *displayNameField;
52@property (unsafe_unretained) IBOutlet NSTextField *ringIDField;
53@property (unsafe_unretained) IBOutlet NSTextField *registeredNameField;
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -050054@property (unsafe_unretained) IBOutlet RoundedTextField *accountStatus;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040055@property (unsafe_unretained) IBOutlet NSButton *registerNameButton;
56@property (unsafe_unretained) IBOutlet NSButton* photoView;
57@property (unsafe_unretained) IBOutlet NSButton* passwordButton;
58@property (unsafe_unretained) IBOutlet NSButton* removeAccountButton;
59@property (unsafe_unretained) IBOutlet NSImageView* addProfilePhotoImage;
60@property (unsafe_unretained) IBOutlet NSTableView* devicesTableView;
61@property (unsafe_unretained) IBOutlet NSTableView* blockedContactsTableView;
62@property (assign) IBOutlet NSLayoutConstraint* buttonRegisterWidthConstraint;
63@property (assign) IBOutlet NSLayoutConstraint* bannedContactHeightConstraint;
64@property (assign) IBOutlet NSLayoutConstraint* advancedButtonMarginConstraint;
65
66
67@property AbstractLoadingWC* accountModal;
68@property PasswordChangeWC* passwordModal;
69@property std::string selectedAccountID;
70
71@end
72
73@implementation AccRingGeneralVC
74
75QMetaObject::Connection deviceAddedSignal;
76QMetaObject::Connection deviceRevokedSignal;
77QMetaObject::Connection deviceUpdatedSignal;
78QMetaObject::Connection contactBlockedSignal;
79QMetaObject::Connection bannedContactsChangedSignal;
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -050080QMetaObject::Connection accountStateChangedSignal;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040081
82
83@synthesize displayNameField;
84@synthesize ringIDField;
85@synthesize registeredNameField;
86@synthesize photoView;
87@synthesize addProfilePhotoImage;
88@synthesize accountModel;
89@synthesize registerNameButton, passwordButton, removeAccountButton;
90@synthesize buttonRegisterWidthConstraint;
91@synthesize accountModal;
92@synthesize delegate;
93@synthesize devicesTableView;
94@synthesize blockedContactsTableView;
95
96
97typedef NS_ENUM(NSInteger, TagViews) {
98 DISPLAYNAME = 100,
99 DEVICE_NAME_TAG = 200,
100 DEVICE_ID_TAG = 300,
101 DEVICE_EDIT_TAG = 400,
102 DEVICE_REVOKE_TAG = 500,
103 BANNED_CONTACT_NAME_TAG = 600,
104 BANNED_CONTACT_ID_TAG = 700,
105 UNBLOCK_CONTACT_TAG = 800
106};
107
108-(id) initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountmodel:(lrc::api::NewAccountModel*) accountModel
109{
110 if (self = [self initWithNibName: nibNameOrNil bundle:nibBundleOrNil])
111 {
112 self.accountModel= accountModel;
113 }
114 return self;
115}
116
117- (void)awakeFromNib
118{
119 [super awakeFromNib];
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400120 [photoView setBordered:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400121 [addProfilePhotoImage setWantsLayer: YES];
122 devicesTableView.delegate = self;
123 devicesTableView.dataSource = self;
124 blockedContactsTableView.delegate = self;
125 blockedContactsTableView.dataSource= self;
Kateryna Kostiuka8525942018-10-17 14:33:39 -0400126 [[self view] setAutoresizingMask: NSViewMinXMargin | NSViewMaxXMargin];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400127}
128
129- (void)viewDidLoad {
130 [super viewDidLoad];
131 [self updateView];
132}
133
134- (void) setSelectedAccount:(std::string) account {
135 self.selectedAccountID = account;
136 [self connectSignals];
137 [self updateView];
138 [self hideBannedContacts];
139}
140
141-(void) updateView {
142 const auto& account = accountModel->getAccountInfo(self.selectedAccountID);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400143
Kateryna Kostiuk43a79232018-10-18 15:45:18 -0400144 NSData *imageData = [[NSData alloc] initWithBase64EncodedString:@(account.profileInfo.avatar.c_str()) options:NSDataBase64DecodingIgnoreUnknownCharacters];
145 NSImage *image = [[NSImage alloc] initWithData:imageData];
146 if(image) {
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400147 [photoView setBordered:NO];
Kateryna Kostiuk43a79232018-10-18 15:45:18 -0400148 [photoView setImage: [image roundCorners: 350]];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400149 [addProfilePhotoImage setHidden:YES];
150 } else {
151 [photoView setImage:nil];
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400152 [photoView setBordered:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400153 [addProfilePhotoImage setHidden:NO];
154 }
155 NSString* displayName = @(account.profileInfo.alias.c_str());
156 [displayNameField setStringValue:displayName];
157 [ringIDField setStringValue:@(account.profileInfo.uri.c_str())];
158 if(account.registeredName.empty()) {
159 [registerNameButton setHidden:NO];
160 buttonRegisterWidthConstraint.constant = 260.0;
161 } else {
162 buttonRegisterWidthConstraint.constant = 0.0;
163 [registerNameButton setHidden:YES];
164 }
165
166 [registeredNameField setStringValue:@(account.registeredName.c_str())];
167
168 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
169 [passwordButton setTitle:accountProperties.archiveHasPassword ? @"Change password" : @"Create password"];
170 self.accountEnabled = account.enabled;
171
172 NSMutableAttributedString *colorTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[removeAccountButton attributedTitle]];
173 NSRange titleRange = NSMakeRange(0, [colorTitle length]);
174 [colorTitle addAttribute:NSForegroundColorAttributeName value:[NSColor errorColor] range:titleRange];
175 [removeAccountButton setAttributedTitle:colorTitle];
176 [devicesTableView reloadData];
177 [blockedContactsTableView reloadData];
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500178 self.accountStatus.bgColor = colorForAccountStatus(accountModel->getAccountInfo(self.selectedAccountID).status);
179 [self.accountStatus setNeedsDisplay:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400180}
181
182-(void) connectSignals {
183 QObject::disconnect(deviceAddedSignal);
184 QObject::disconnect(deviceRevokedSignal);
185 QObject::disconnect(deviceUpdatedSignal);
186 QObject::disconnect(bannedContactsChangedSignal);
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500187 QObject::disconnect(accountStateChangedSignal);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400188 deviceAddedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
189 &lrc::api::NewDeviceModel::deviceAdded,
190 [self] (const std::string &id) {
191 [devicesTableView reloadData];
192 });
193 deviceRevokedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
194 &lrc::api::NewDeviceModel::deviceRevoked,
195 [self] (const std::string &id, const lrc::api::NewDeviceModel::Status status) {
196 switch (status) {
197 case lrc::api::NewDeviceModel::Status::SUCCESS:
198 [devicesTableView reloadData];
199 break;
200 case lrc::api::NewDeviceModel::Status::WRONG_PASSWORD:
201 [self showAlertWithTitle: @"" andText: @"Device revocation failed with error: Wrong password"];
202 break;
203 case lrc::api::NewDeviceModel::Status::UNKNOWN_DEVICE:
204 [self showAlertWithTitle: @"" andText: @"Device revocation failed with error: Unknown device"];
205 break;
206 }
207 });
208 deviceUpdatedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
209 &lrc::api::NewDeviceModel::deviceUpdated,
210 [self] (const std::string &id) {
211 [devicesTableView reloadData];
212 });
213 bannedContactsChangedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).contactModel,
214 &lrc::api::ContactModel::bannedStatusChanged,
215 [self] (const std::string &contactUri, bool banned) {
216 [blockedContactsTableView reloadData];
217 });
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500218 accountStateChangedSignal = QObject::connect(self.accountModel,
219 &lrc::api::NewAccountModel::accountStatusChanged,
220 [self] (const std::string& accountID) {
221 if(accountID != self.selectedAccountID) {
222 return;
223 }
224 self.accountStatus.bgColor = colorForAccountStatus(accountModel->getAccountInfo(accountID).status);
225 [self.accountStatus setNeedsDisplay:YES];
226 });
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400227}
228
229-(void) showAlertWithTitle: (NSString *) title andText: (NSString *)text {
230 NSAlert *alert = [[NSAlert alloc] init];
231 [alert addButtonWithTitle:@"OK"];
232 [alert setMessageText:title];
233 [alert setInformativeText:text];
234 [alert runModal];
235}
236
237- (void)pictureTakerDidEnd:(IKPictureTaker *) picker
238 returnCode:(NSInteger) code
239 contextInfo:(void*) contextInfo
240{
Kateryna Kostiuk256814e2018-09-04 14:47:33 -0400241 //do nothing when editing canceled
242 if (code == 0) {
243 return;
244 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400245 if (auto outputImage = [picker outputImage]) {
246 auto image = [picker inputImage];
247 CGFloat newSize = MIN(image.size.height, image.size.width);
248 outputImage = [outputImage cropImageToSize:CGSizeMake(newSize, newSize)];
249 [photoView setImage: [outputImage roundCorners: outputImage.size.height * 0.5]];
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400250 [photoView setBordered:NO];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400251 [addProfilePhotoImage setHidden:YES];
252 auto imageToBytes = QByteArray::fromNSData([outputImage TIFFRepresentation]).toBase64();
253 std::string imageToString = std::string(imageToBytes.constData(), imageToBytes.length());
254 self.accountModel->setAvatar(self.selectedAccountID, imageToString);
255 } else if(!photoView.image) {
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400256 [photoView setBordered:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400257 [addProfilePhotoImage setHidden:NO];
258 }
259}
260
261#pragma mark - RegisterNameDelegate methods
262
263- (void) didRegisterName:(NSString *) name withSuccess:(BOOL) success
264{
265 [self.accountModal close];
266 if(!success) {
267 return;
268 }
269
270 if(name.length == 0) {
271 return;
272 }
273 buttonRegisterWidthConstraint.constant = 0.0;
274 [registerNameButton setHidden:YES];
275 [registeredNameField setStringValue:name];
Kateryna Kostiukdf680762018-10-29 13:53:52 -0400276 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
277 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400278}
279
280#pragma mark - NSTextFieldDelegate delegate methods
281
282- (void)controlTextDidChange:(NSNotification *)notif
283{
284 NSTextField* textField = [notif object];
285 if (textField.tag != DISPLAYNAME) {
286 return;
287 }
288 NSString* displayName = textField.stringValue;
289
290 [NSObject cancelPreviousPerformRequestsWithTarget:self];
291 self.accountModel->setAlias(self.selectedAccountID, [displayName UTF8String]);
292 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
293 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
294}
295
296#pragma mark - NSTableViewDataSource methods
297
298- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
299 if(tableView == devicesTableView) {
300 return self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices().size();
301 } else if (tableView == blockedContactsTableView){
302 return self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts().size();
303 }
304 return 0;
305}
306
307#pragma mark - NSTableViewDelegate methods
308- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
309{
310 if(tableView == devicesTableView) {
311 NSTableCellView* deviceView = [tableView makeViewWithIdentifier:@"TableCellDeviceItem" owner:self];
312 NSTextField* nameLabel = [deviceView viewWithTag: DEVICE_NAME_TAG];
313 NSTextField* idLabel = [deviceView viewWithTag: DEVICE_ID_TAG];
314 NSButton* revokeButton = [deviceView viewWithTag: DEVICE_REVOKE_TAG];
315 NSButton* editButton = [deviceView viewWithTag: DEVICE_EDIT_TAG];
316 [editButton setAction:@selector(editDevice:)];
317 [editButton setTarget:self];
318 [revokeButton setAction:@selector(startDeviceRevocation:)];
319 [revokeButton setTarget:self];
320 auto devices = self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices();
321 auto device = devices.begin();
322
323 std::advance(device, row);
324
325 auto name = device->name;
326 auto deviceID = device->id;
327
328 [nameLabel setStringValue: @(name.c_str())];
329 [idLabel setStringValue: @(deviceID.c_str())];
330 [revokeButton setHidden: device->isCurrent];
331 [editButton setHidden: !device->isCurrent];
332 return deviceView;
333 } else if (tableView == blockedContactsTableView) {
334 NSTableCellView* contactView = [tableView makeViewWithIdentifier:@"TableCellBannedContactItem" owner:self];
335 NSTextField* nameLabel = [contactView viewWithTag: BANNED_CONTACT_NAME_TAG];
336 NSTextField* idLabel = [contactView viewWithTag: BANNED_CONTACT_ID_TAG];
337 NSButton* revokeButton = [contactView viewWithTag: UNBLOCK_CONTACT_TAG];
338 auto contacts = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts();
339 auto contactID = contacts.begin();
340 std::advance(contactID, row);
341 [idLabel setStringValue: @(contactID->c_str())];
342 auto contact = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getContact([@(contactID->c_str()) UTF8String]);
343 [nameLabel setStringValue: bestNameForContact(contact)];
344 [revokeButton setAction:@selector(unblockContact:)];
345 [revokeButton setTarget:self];
346 return contactView;
347 }
348}
349
350- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
351{
352 if(tableView == devicesTableView) {
353 return tableView.rowHeight;
354 } else if (tableView == blockedContactsTableView) {
355 CGFloat height = self.bannedContactHeightConstraint.constant;
356 if(height == 150) {
357 return 52;
358 } else {
359 return 1;
360 }
361 }
362}
363
364- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
365{
366 return [tableView makeViewWithIdentifier:@"HoverRowView" owner:nil];
367}
368
369#pragma mark - Actions
370
371- (IBAction)editPhoto:(id)sender
372{
373 auto pictureTaker = [IKPictureTaker pictureTaker];
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400374 if (@available(macOS 10.14, *)) {
375 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
376 if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied)
377 {
378 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
379 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400380
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400381 if(authStatus == AVAuthorizationStatusNotDetermined)
382 {
383 [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
384 if(!granted){
385 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
386 }
387 }];
388 }
389 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400390 [pictureTaker beginPictureTakerSheetForWindow:[self.view window]
391 withDelegate:self
392 didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:)
393 contextInfo:nil];
394
395}
396
397- (IBAction)startExportOnRing:(id)sender
398{
399 ExportPasswordWC *passwordWC = [[ExportPasswordWC alloc] initWithNibName:@"ExportPasswordWindow" bundle: nil accountmodel: self.accountModel];
400 passwordWC.selectedAccountID = self.selectedAccountID;
401 accountModal = passwordWC;
402 [self.view.window beginSheet: passwordWC.window completionHandler:nil];
403}
404- (IBAction)triggerAdwancedSettings: (NSButton *)sender {
405 [self.delegate triggerAdvancedOptions];
406}
407
408- (IBAction)enableAccount: (NSButton *)sender {
409 const auto& account = accountModel->getAccountInfo(self.selectedAccountID);
410 self.accountModel->enableAccount(self.selectedAccountID, !account.enabled);
411 self.accountEnabled = account.enabled;
412 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
413 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
414}
415
416- (IBAction)removeAccount:(id)sender
417{
418 NSAlert *alert = [[NSAlert alloc] init];
419 [alert addButtonWithTitle:@"OK"];
420 [alert addButtonWithTitle:@"Cancel"];
421 [alert setMessageText: NSLocalizedString(@"Remove account",
422 @"Remove account alert title")];
423 [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.",
424 @"Remove account alert message")];
425
426 if ([alert runModal] == NSAlertFirstButtonReturn) {
427 self.accountModel->removeAccount(self.selectedAccountID);
428 }
429}
430
431- (IBAction)exportAccount:(id)sender
432{
433 BackupAccountWC* passwordWC = [[BackupAccountWC alloc] initWithNibName:@"BackupAccountWindow" bundle: nil accountmodel: self.accountModel];
434 passwordWC.delegate = self;
435 [passwordWC setAllowFileSelection:NO];
Kateryna Kostiuk14366812018-08-24 16:30:20 -0400436 passwordWC.selectedAccountID = self.selectedAccountID;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400437 accountModal = passwordWC;
438 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
439}
440
441- (IBAction)startNameRegistration:(id)sender
442{
443 RegisterNameWC* registerWC = [[RegisterNameWC alloc] initWithNibName:@"RegisterNameWindow" bundle: nil accountmodel: self.accountModel];
444 registerWC.delegate = self;
445 registerWC.selectedAccountID = self.selectedAccountID;
446 self.accountModal = registerWC;
447 [self.view.window beginSheet:registerWC.window completionHandler:nil];
448}
449- (IBAction)changePassword:(id)sender
450{
451 PasswordChangeWC* passwordWC = [[PasswordChangeWC alloc] initWithNibName:@"PasswordChange" bundle: nil accountmodel: self.accountModel];
452 passwordWC.selectedAccountID = self.selectedAccountID;
453 passwordWC.delegate = self;
454 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
455 self.passwordModal = passwordWC;
456}
457
458- (IBAction)showBanned: (NSButton *)sender {
459 CGFloat height = self.bannedContactHeightConstraint.constant;
460 NSRect frame = self.view.frame;
461 if(height == 150) {
462 frame.size.height = frame.size.height - 150 - 10;
463 } else {
464 frame.size.height = frame.size.height + 150 + 10;
465 }
466 self.view.frame = frame;
467 [self.delegate updateFrame];
468 CGFloat advancedHeight = self.advancedButtonMarginConstraint.constant;
469 self.advancedButtonMarginConstraint.constant = (height== 2) ? 40 : 30;
470 self.bannedContactHeightConstraint.constant = (height== 2) ? 150 : 2;
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -0400471 [[[[self.blockedContactsTableView superview] superview] superview] setHidden:![[[[self.blockedContactsTableView superview] superview] superview] isHidden]];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400472 [blockedContactsTableView reloadData];
473}
474
475-(void) hideBannedContacts {
476 CGFloat height = self.bannedContactHeightConstraint.constant;
477 NSRect frame = self.view.frame;
478 if(height == 150) {
479 [self showBanned:nil];
480 }
481}
482
483- (IBAction)startDeviceRevocation:(NSView*)sender
484{
485 NSInteger row = [devicesTableView rowForView:sender];
486 if(row < 0) {
487 return;
488 }
489 auto devices = self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices();
490 auto device = devices.begin();
491 std::advance(device, row);
492 if(device == devices.end()) {
493 return;
494 }
495 [self proceedDeviceRevokationAlert:device->id];
496}
497
498- (IBAction)unblockContact:(NSView*)sender
499{
500 NSInteger row = [blockedContactsTableView rowForView:sender];
501 if(row < 0) {
502 return;
503 }
504 auto contacts = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts();
505 auto contactID = contacts.begin();
506 std::advance(contactID, row);
507 if(contactID == contacts.end()) {
508 return;
509 }
510 auto contact = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getContact([@(contactID->c_str()) UTF8String]);
511 if(!contact.isBanned) {
512 return;
513 }
514 self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->addContact(contact);
515}
516
517- (IBAction)editDevice:(NSView*)sender
518{
519 NSInteger row = [devicesTableView rowForView:sender];
520 if(row < 0) {
521 return;
522 }
523
524 NSTableCellView* deviceView = [devicesTableView viewAtColumn:0 row:row makeIfNecessary:NO];
525 if(!deviceView || ![deviceView isKindOfClass:[NSTableCellView class]]) {
526 return;
527 }
528
529 NSTextField* nameLabel = [deviceView viewWithTag: DEVICE_NAME_TAG];
530 NSButton* editButton = [deviceView viewWithTag: DEVICE_EDIT_TAG];
531 if ([nameLabel isEditable]) {
532 self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->setCurrentDeviceName([nameLabel.stringValue UTF8String]);
533 [nameLabel setEditable:NO];
534 [self.view.window makeFirstResponder:nil];
535 editButton.image = [NSImage imageNamed:NSImageNameTouchBarComposeTemplate];
536 return;
537 }
538 [nameLabel setEditable:YES];
539 [nameLabel becomeFirstResponder];
540 editButton.image = [NSImage imageNamed:NSImageNameTouchBarDownloadTemplate];
541}
542
543-(void) revokeDeviceWithID: (std::string) deviceID password:(NSString *) password {
544 self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->revokeDevice(deviceID, [password UTF8String]);
545}
546
547-(void) proceedDeviceRevokationAlert: (std::string) deviceID {
548 NSAlert *alert = [[NSAlert alloc] init];
549 [alert addButtonWithTitle:@"OK"];
550 [alert addButtonWithTitle:@"Cancel"];
551 [alert setMessageText:@"Revoke Device"];
552 [alert setInformativeText:@"Attention! This action could not be undone!"];
553 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
554 if(accountProperties.archiveHasPassword) {
555 NSSecureTextField *passwordText = [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
556 [passwordText setPlaceholderString:@"Enter password"];
557 [alert setAccessoryView:passwordText];
558 if ([alert runModal] == NSAlertFirstButtonReturn) {
559 [self revokeDeviceWithID:deviceID password:[passwordText stringValue]];
560 }
561 } else {
562 if ([alert runModal] == NSAlertFirstButtonReturn) {
563 [self revokeDeviceWithID:deviceID password:@""];
564 }
565 }
566}
567
568#pragma mark - BackupAccountDelegate methods
569
570-(void) didCompleteExportWithPath:(NSURL*) fileUrl
571{
572 [[NSWorkspace sharedWorkspace] selectFile:fileUrl.path inFileViewerRootedAtPath:@""];
573}
574
575#pragma mark - PasswordChangeDelegate
576
577-(void) paswordCreatedWithSuccess:(BOOL) success
578{
579 [passwordButton setTitle: success ? @"Change password" : @"Create password"];
580}
581
582@end