blob: 6e8924dcb072e899d4e7adcb0b43803cbd323578 [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"
45#import "ExportPasswordWC.h"
46#import "utils.h"
47
48@interface AccRingGeneralVC ()
49
50@property (unsafe_unretained) IBOutlet NSTextField *displayNameField;
51@property (unsafe_unretained) IBOutlet NSTextField *ringIDField;
52@property (unsafe_unretained) IBOutlet NSTextField *registeredNameField;
53@property (unsafe_unretained) IBOutlet NSButton *registerNameButton;
54@property (unsafe_unretained) IBOutlet NSButton* photoView;
55@property (unsafe_unretained) IBOutlet NSButton* passwordButton;
56@property (unsafe_unretained) IBOutlet NSButton* removeAccountButton;
57@property (unsafe_unretained) IBOutlet NSImageView* addProfilePhotoImage;
58@property (unsafe_unretained) IBOutlet NSTableView* devicesTableView;
59@property (unsafe_unretained) IBOutlet NSTableView* blockedContactsTableView;
60@property (assign) IBOutlet NSLayoutConstraint* buttonRegisterWidthConstraint;
61@property (assign) IBOutlet NSLayoutConstraint* bannedContactHeightConstraint;
62@property (assign) IBOutlet NSLayoutConstraint* advancedButtonMarginConstraint;
63
64
65@property AbstractLoadingWC* accountModal;
66@property PasswordChangeWC* passwordModal;
67@property std::string selectedAccountID;
68
69@end
70
71@implementation AccRingGeneralVC
72
73QMetaObject::Connection deviceAddedSignal;
74QMetaObject::Connection deviceRevokedSignal;
75QMetaObject::Connection deviceUpdatedSignal;
76QMetaObject::Connection contactBlockedSignal;
77QMetaObject::Connection bannedContactsChangedSignal;
78
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];
175}
176
177-(void) connectSignals {
178 QObject::disconnect(deviceAddedSignal);
179 QObject::disconnect(deviceRevokedSignal);
180 QObject::disconnect(deviceUpdatedSignal);
181 QObject::disconnect(bannedContactsChangedSignal);
182 deviceAddedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
183 &lrc::api::NewDeviceModel::deviceAdded,
184 [self] (const std::string &id) {
185 [devicesTableView reloadData];
186 });
187 deviceRevokedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
188 &lrc::api::NewDeviceModel::deviceRevoked,
189 [self] (const std::string &id, const lrc::api::NewDeviceModel::Status status) {
190 switch (status) {
191 case lrc::api::NewDeviceModel::Status::SUCCESS:
192 [devicesTableView reloadData];
193 break;
194 case lrc::api::NewDeviceModel::Status::WRONG_PASSWORD:
195 [self showAlertWithTitle: @"" andText: @"Device revocation failed with error: Wrong password"];
196 break;
197 case lrc::api::NewDeviceModel::Status::UNKNOWN_DEVICE:
198 [self showAlertWithTitle: @"" andText: @"Device revocation failed with error: Unknown device"];
199 break;
200 }
201 });
202 deviceUpdatedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).deviceModel,
203 &lrc::api::NewDeviceModel::deviceUpdated,
204 [self] (const std::string &id) {
205 [devicesTableView reloadData];
206 });
207 bannedContactsChangedSignal = QObject::connect(&*(self.accountModel->getAccountInfo(self.selectedAccountID)).contactModel,
208 &lrc::api::ContactModel::bannedStatusChanged,
209 [self] (const std::string &contactUri, bool banned) {
210 [blockedContactsTableView reloadData];
211 });
212}
213
214-(void) showAlertWithTitle: (NSString *) title andText: (NSString *)text {
215 NSAlert *alert = [[NSAlert alloc] init];
216 [alert addButtonWithTitle:@"OK"];
217 [alert setMessageText:title];
218 [alert setInformativeText:text];
219 [alert runModal];
220}
221
222- (void)pictureTakerDidEnd:(IKPictureTaker *) picker
223 returnCode:(NSInteger) code
224 contextInfo:(void*) contextInfo
225{
Kateryna Kostiuk256814e2018-09-04 14:47:33 -0400226 //do nothing when editing canceled
227 if (code == 0) {
228 return;
229 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400230 if (auto outputImage = [picker outputImage]) {
231 auto image = [picker inputImage];
232 CGFloat newSize = MIN(image.size.height, image.size.width);
233 outputImage = [outputImage cropImageToSize:CGSizeMake(newSize, newSize)];
234 [photoView setImage: [outputImage roundCorners: outputImage.size.height * 0.5]];
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400235 [photoView setBordered:NO];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400236 [addProfilePhotoImage setHidden:YES];
237 auto imageToBytes = QByteArray::fromNSData([outputImage TIFFRepresentation]).toBase64();
238 std::string imageToString = std::string(imageToBytes.constData(), imageToBytes.length());
239 self.accountModel->setAvatar(self.selectedAccountID, imageToString);
240 } else if(!photoView.image) {
Kateryna Kostiuk39ce23d2018-10-02 14:33:37 -0400241 [photoView setBordered:YES];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400242 [addProfilePhotoImage setHidden:NO];
243 }
244}
245
246#pragma mark - RegisterNameDelegate methods
247
248- (void) didRegisterName:(NSString *) name withSuccess:(BOOL) success
249{
250 [self.accountModal close];
251 if(!success) {
252 return;
253 }
254
255 if(name.length == 0) {
256 return;
257 }
258 buttonRegisterWidthConstraint.constant = 0.0;
259 [registerNameButton setHidden:YES];
260 [registeredNameField setStringValue:name];
Kateryna Kostiukdf680762018-10-29 13:53:52 -0400261 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
262 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400263}
264
265#pragma mark - NSTextFieldDelegate delegate methods
266
267- (void)controlTextDidChange:(NSNotification *)notif
268{
269 NSTextField* textField = [notif object];
270 if (textField.tag != DISPLAYNAME) {
271 return;
272 }
273 NSString* displayName = textField.stringValue;
274
275 [NSObject cancelPreviousPerformRequestsWithTarget:self];
276 self.accountModel->setAlias(self.selectedAccountID, [displayName UTF8String]);
277 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
278 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
279}
280
281#pragma mark - NSTableViewDataSource methods
282
283- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
284 if(tableView == devicesTableView) {
285 return self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices().size();
286 } else if (tableView == blockedContactsTableView){
287 return self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts().size();
288 }
289 return 0;
290}
291
292#pragma mark - NSTableViewDelegate methods
293- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
294{
295 if(tableView == devicesTableView) {
296 NSTableCellView* deviceView = [tableView makeViewWithIdentifier:@"TableCellDeviceItem" owner:self];
297 NSTextField* nameLabel = [deviceView viewWithTag: DEVICE_NAME_TAG];
298 NSTextField* idLabel = [deviceView viewWithTag: DEVICE_ID_TAG];
299 NSButton* revokeButton = [deviceView viewWithTag: DEVICE_REVOKE_TAG];
300 NSButton* editButton = [deviceView viewWithTag: DEVICE_EDIT_TAG];
301 [editButton setAction:@selector(editDevice:)];
302 [editButton setTarget:self];
303 [revokeButton setAction:@selector(startDeviceRevocation:)];
304 [revokeButton setTarget:self];
305 auto devices = self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices();
306 auto device = devices.begin();
307
308 std::advance(device, row);
309
310 auto name = device->name;
311 auto deviceID = device->id;
312
313 [nameLabel setStringValue: @(name.c_str())];
314 [idLabel setStringValue: @(deviceID.c_str())];
315 [revokeButton setHidden: device->isCurrent];
316 [editButton setHidden: !device->isCurrent];
317 return deviceView;
318 } else if (tableView == blockedContactsTableView) {
319 NSTableCellView* contactView = [tableView makeViewWithIdentifier:@"TableCellBannedContactItem" owner:self];
320 NSTextField* nameLabel = [contactView viewWithTag: BANNED_CONTACT_NAME_TAG];
321 NSTextField* idLabel = [contactView viewWithTag: BANNED_CONTACT_ID_TAG];
322 NSButton* revokeButton = [contactView viewWithTag: UNBLOCK_CONTACT_TAG];
323 auto contacts = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts();
324 auto contactID = contacts.begin();
325 std::advance(contactID, row);
326 [idLabel setStringValue: @(contactID->c_str())];
327 auto contact = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getContact([@(contactID->c_str()) UTF8String]);
328 [nameLabel setStringValue: bestNameForContact(contact)];
329 [revokeButton setAction:@selector(unblockContact:)];
330 [revokeButton setTarget:self];
331 return contactView;
332 }
333}
334
335- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row
336{
337 if(tableView == devicesTableView) {
338 return tableView.rowHeight;
339 } else if (tableView == blockedContactsTableView) {
340 CGFloat height = self.bannedContactHeightConstraint.constant;
341 if(height == 150) {
342 return 52;
343 } else {
344 return 1;
345 }
346 }
347}
348
349- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
350{
351 return [tableView makeViewWithIdentifier:@"HoverRowView" owner:nil];
352}
353
354#pragma mark - Actions
355
356- (IBAction)editPhoto:(id)sender
357{
358 auto pictureTaker = [IKPictureTaker pictureTaker];
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400359 if (@available(macOS 10.14, *)) {
360 AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
361 if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied)
362 {
363 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
364 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400365
Kateryna Kostiuk91b44e32018-09-28 17:08:02 -0400366 if(authStatus == AVAuthorizationStatusNotDetermined)
367 {
368 [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
369 if(!granted){
370 [pictureTaker setValue:0 forKey:IKPictureTakerAllowsVideoCaptureKey];
371 }
372 }];
373 }
374 }
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400375 [pictureTaker beginPictureTakerSheetForWindow:[self.view window]
376 withDelegate:self
377 didEndSelector:@selector(pictureTakerDidEnd:returnCode:contextInfo:)
378 contextInfo:nil];
379
380}
381
382- (IBAction)startExportOnRing:(id)sender
383{
384 ExportPasswordWC *passwordWC = [[ExportPasswordWC alloc] initWithNibName:@"ExportPasswordWindow" bundle: nil accountmodel: self.accountModel];
385 passwordWC.selectedAccountID = self.selectedAccountID;
386 accountModal = passwordWC;
387 [self.view.window beginSheet: passwordWC.window completionHandler:nil];
388}
389- (IBAction)triggerAdwancedSettings: (NSButton *)sender {
390 [self.delegate triggerAdvancedOptions];
391}
392
393- (IBAction)enableAccount: (NSButton *)sender {
394 const auto& account = accountModel->getAccountInfo(self.selectedAccountID);
395 self.accountModel->enableAccount(self.selectedAccountID, !account.enabled);
396 self.accountEnabled = account.enabled;
397 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
398 self.accountModel->setAccountConfig(self.selectedAccountID, accountProperties);
399}
400
401- (IBAction)removeAccount:(id)sender
402{
403 NSAlert *alert = [[NSAlert alloc] init];
404 [alert addButtonWithTitle:@"OK"];
405 [alert addButtonWithTitle:@"Cancel"];
406 [alert setMessageText: NSLocalizedString(@"Remove account",
407 @"Remove account alert title")];
408 [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.",
409 @"Remove account alert message")];
410
411 if ([alert runModal] == NSAlertFirstButtonReturn) {
412 self.accountModel->removeAccount(self.selectedAccountID);
413 }
414}
415
416- (IBAction)exportAccount:(id)sender
417{
418 BackupAccountWC* passwordWC = [[BackupAccountWC alloc] initWithNibName:@"BackupAccountWindow" bundle: nil accountmodel: self.accountModel];
419 passwordWC.delegate = self;
420 [passwordWC setAllowFileSelection:NO];
Kateryna Kostiuk14366812018-08-24 16:30:20 -0400421 passwordWC.selectedAccountID = self.selectedAccountID;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400422 accountModal = passwordWC;
423 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
424}
425
426- (IBAction)startNameRegistration:(id)sender
427{
428 RegisterNameWC* registerWC = [[RegisterNameWC alloc] initWithNibName:@"RegisterNameWindow" bundle: nil accountmodel: self.accountModel];
429 registerWC.delegate = self;
430 registerWC.selectedAccountID = self.selectedAccountID;
431 self.accountModal = registerWC;
432 [self.view.window beginSheet:registerWC.window completionHandler:nil];
433}
434- (IBAction)changePassword:(id)sender
435{
436 PasswordChangeWC* passwordWC = [[PasswordChangeWC alloc] initWithNibName:@"PasswordChange" bundle: nil accountmodel: self.accountModel];
437 passwordWC.selectedAccountID = self.selectedAccountID;
438 passwordWC.delegate = self;
439 [self.view.window beginSheet:passwordWC.window completionHandler:nil];
440 self.passwordModal = passwordWC;
441}
442
443- (IBAction)showBanned: (NSButton *)sender {
444 CGFloat height = self.bannedContactHeightConstraint.constant;
445 NSRect frame = self.view.frame;
446 if(height == 150) {
447 frame.size.height = frame.size.height - 150 - 10;
448 } else {
449 frame.size.height = frame.size.height + 150 + 10;
450 }
451 self.view.frame = frame;
452 [self.delegate updateFrame];
453 CGFloat advancedHeight = self.advancedButtonMarginConstraint.constant;
454 self.advancedButtonMarginConstraint.constant = (height== 2) ? 40 : 30;
455 self.bannedContactHeightConstraint.constant = (height== 2) ? 150 : 2;
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -0400456 [[[[self.blockedContactsTableView superview] superview] superview] setHidden:![[[[self.blockedContactsTableView superview] superview] superview] isHidden]];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400457 [blockedContactsTableView reloadData];
458}
459
460-(void) hideBannedContacts {
461 CGFloat height = self.bannedContactHeightConstraint.constant;
462 NSRect frame = self.view.frame;
463 if(height == 150) {
464 [self showBanned:nil];
465 }
466}
467
468- (IBAction)startDeviceRevocation:(NSView*)sender
469{
470 NSInteger row = [devicesTableView rowForView:sender];
471 if(row < 0) {
472 return;
473 }
474 auto devices = self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->getAllDevices();
475 auto device = devices.begin();
476 std::advance(device, row);
477 if(device == devices.end()) {
478 return;
479 }
480 [self proceedDeviceRevokationAlert:device->id];
481}
482
483- (IBAction)unblockContact:(NSView*)sender
484{
485 NSInteger row = [blockedContactsTableView rowForView:sender];
486 if(row < 0) {
487 return;
488 }
489 auto contacts = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getBannedContacts();
490 auto contactID = contacts.begin();
491 std::advance(contactID, row);
492 if(contactID == contacts.end()) {
493 return;
494 }
495 auto contact = self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->getContact([@(contactID->c_str()) UTF8String]);
496 if(!contact.isBanned) {
497 return;
498 }
499 self.accountModel->getAccountInfo(self.selectedAccountID).contactModel->addContact(contact);
500}
501
502- (IBAction)editDevice:(NSView*)sender
503{
504 NSInteger row = [devicesTableView rowForView:sender];
505 if(row < 0) {
506 return;
507 }
508
509 NSTableCellView* deviceView = [devicesTableView viewAtColumn:0 row:row makeIfNecessary:NO];
510 if(!deviceView || ![deviceView isKindOfClass:[NSTableCellView class]]) {
511 return;
512 }
513
514 NSTextField* nameLabel = [deviceView viewWithTag: DEVICE_NAME_TAG];
515 NSButton* editButton = [deviceView viewWithTag: DEVICE_EDIT_TAG];
516 if ([nameLabel isEditable]) {
517 self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->setCurrentDeviceName([nameLabel.stringValue UTF8String]);
518 [nameLabel setEditable:NO];
519 [self.view.window makeFirstResponder:nil];
520 editButton.image = [NSImage imageNamed:NSImageNameTouchBarComposeTemplate];
521 return;
522 }
523 [nameLabel setEditable:YES];
524 [nameLabel becomeFirstResponder];
525 editButton.image = [NSImage imageNamed:NSImageNameTouchBarDownloadTemplate];
526}
527
528-(void) revokeDeviceWithID: (std::string) deviceID password:(NSString *) password {
529 self.accountModel->getAccountInfo(self.selectedAccountID).deviceModel->revokeDevice(deviceID, [password UTF8String]);
530}
531
532-(void) proceedDeviceRevokationAlert: (std::string) deviceID {
533 NSAlert *alert = [[NSAlert alloc] init];
534 [alert addButtonWithTitle:@"OK"];
535 [alert addButtonWithTitle:@"Cancel"];
536 [alert setMessageText:@"Revoke Device"];
537 [alert setInformativeText:@"Attention! This action could not be undone!"];
538 lrc::api::account::ConfProperties_t accountProperties = self.accountModel->getAccountConfig(self.selectedAccountID);
539 if(accountProperties.archiveHasPassword) {
540 NSSecureTextField *passwordText = [[NSSecureTextField alloc] initWithFrame:NSMakeRect(0, 0, 200, 24)];
541 [passwordText setPlaceholderString:@"Enter password"];
542 [alert setAccessoryView:passwordText];
543 if ([alert runModal] == NSAlertFirstButtonReturn) {
544 [self revokeDeviceWithID:deviceID password:[passwordText stringValue]];
545 }
546 } else {
547 if ([alert runModal] == NSAlertFirstButtonReturn) {
548 [self revokeDeviceWithID:deviceID password:@""];
549 }
550 }
551}
552
553#pragma mark - BackupAccountDelegate methods
554
555-(void) didCompleteExportWithPath:(NSURL*) fileUrl
556{
557 [[NSWorkspace sharedWorkspace] selectFile:fileUrl.path inFileViewerRootedAtPath:@""];
558}
559
560#pragma mark - PasswordChangeDelegate
561
562-(void) paswordCreatedWithSuccess:(BOOL) success
563{
564 [passwordButton setTitle: success ? @"Change password" : @"Create password"];
565}
566
567@end