Skip to content

Commit

Permalink
Added TOTP Column to entry table view
Browse files Browse the repository at this point in the history
  • Loading branch information
mstarke committed Nov 23, 2022
1 parent 69ec359 commit 05ba068
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions MacPass/MPEntryViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
NSString *const MPEntryTableCreatedColumnIdentifier = @"MPEntryTableCreatedColumnIdentifier";
NSString *const MPEntryTableModfiedColumnIdentifier = @"MPEntryTableModfiedColumnIdentifier";
NSString *const MPEntryTableHistoryColumnIdentifier = @"MPEntryTableHistoryColumnIdentifier";
NSString *const MPEntryTableTOTPColumnIdentifier = @"MPEntryTableTOTPColumnIdentifier";

NSString *const _MPTableImageCellView = @"ImageCell";
NSString *const _MPTableStringCellView = @"StringCell";
Expand All @@ -82,6 +83,8 @@ @interface MPEntryViewController () {

@property (weak) IBOutlet NSTableView *entryTable;
@property (assign) MPDisplayMode displayMode;
@property (nonatomic, assign) BOOL totpColumnHidden;
@property (strong) NSTimer *totpUpdateTimer;


/* Constraints */
Expand All @@ -103,13 +106,14 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self) {
_isDisplayingContextBar = NO;
_totpColumnHidden = NO;
_displayMode = MPDisplayModeEntries;
_entryArrayController = [[NSArrayController alloc] init];
_dataSource = [[MPEntryTableDataSource alloc] init];
_dataSource.viewController = self;
_contextBarViewController = [[MPContextBarViewController alloc] init];
_displayClearTextPasswords = [NSUserDefaults.standardUserDefaults boolForKey:kMPSettingsKeyDisplayClearTextPasswordsInEntryList];

[self bind:NSStringFromSelector(@selector(displayClearTextPasswords))
toObject:NSUserDefaultsController.sharedUserDefaultsController
withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyDisplayClearTextPasswordsInEntryList]
Expand Down Expand Up @@ -161,6 +165,8 @@ - (void)viewDidLoad {
NSTableColumn *modifiedColumn = [[NSTableColumn alloc] initWithIdentifier:MPEntryTableModfiedColumnIdentifier];
NSTableColumn *historyColumn = [[NSTableColumn alloc] initWithIdentifier:MPEntryTableHistoryColumnIdentifier];
NSTableColumn *indexColumn = [[NSTableColumn alloc] initWithIdentifier:MPEntryTableIndexColumnIdentifier];
NSTableColumn *totpTableColumn = [[NSTableColumn alloc] initWithIdentifier:MPEntryTableTOTPColumnIdentifier];

notesColumn.minWidth = 40.0;
attachmentsColumn.minWidth = 40.0;
createdColumn.minWidth = 40.0;
Expand All @@ -173,6 +179,7 @@ - (void)viewDidLoad {
[self.entryTable addTableColumn:modifiedColumn];
[self.entryTable addTableColumn:createdColumn];
[self.entryTable addTableColumn:historyColumn];
[self.entryTable addTableColumn:totpTableColumn];
[self.entryTable addTableColumn:indexColumn];

parentColumn.identifier = MPEntryTableParentColumnIdentifier;
Expand All @@ -195,6 +202,7 @@ - (void)viewDidLoad {
parentColumn.sortDescriptorPrototype = [NSSortDescriptor sortDescriptorWithKey:parentTitleKeyPath ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
modifiedColumn.sortDescriptorPrototype = [NSSortDescriptor sortDescriptorWithKey:timeInfoModificationTimeKeyPath ascending:YES selector:@selector(compare:)];
createdColumn.sortDescriptorPrototype = [NSSortDescriptor sortDescriptorWithKey:timeInfoCreationTimeKeyPath ascending:YES selector:@selector(compare:)];
totpTableColumn.sortDescriptorPrototype = [NSSortDescriptor sortDescriptorWithKey:NSStringFromSelector(@selector(timeOTP)) ascending:YES selector:@selector(compare:)];

indexColumn.headerCell.stringValue = @"";
indexColumn.headerToolTip = NSLocalizedString(@"ENTRY_INDEX_COLUMN_TOOLTIP", "Tooltip displayed on the index header cell");
Expand All @@ -208,6 +216,7 @@ - (void)viewDidLoad {
createdColumn.headerCell.stringValue = NSLocalizedString(@"CREATED", "Creating date column title");
modifiedColumn.headerCell.stringValue = NSLocalizedString(@"MODIFIED", "Modification date column title");
historyColumn.headerCell.stringValue = NSLocalizedString(@"HISTORY", "History count column title");
totpTableColumn.headerCell.stringValue = NSLocalizedString(@"TOPT", "TOTP column title");

[self.entryTable bind:NSContentBinding toObject:self.entryArrayController withKeyPath:NSStringFromSelector(@selector(arrangedObjects)) options:nil];
[self.entryTable bind:NSSortDescriptorsBinding toObject:self.entryArrayController withKeyPath:NSStringFromSelector(@selector(sortDescriptors)) options:nil];
Expand All @@ -232,6 +241,7 @@ - (void)viewDidLoad {
if(parentIndex != 1) {
[self.entryTable moveColumn:parentIndex toColumn:1];
}
[self _updateTOTPTimer];
}

- (void)setDisplayClearTextPasswords:(BOOL)displayClearTextPasswords {
Expand Down Expand Up @@ -261,6 +271,11 @@ - (void)registerNotificationsForDocument:(MPDocument *)document {
[self.contextBarViewController registerNotificationsForDocument:document];
}

- (void)setTotpColumnHidden:(BOOL)toptColumnVisible {
_totpColumnHidden = toptColumnVisible;
[self _updateTOTPTimer];
}

#pragma mark NSTableViewDelgate

- (void)_tableDidScroll:(NSNotification *)notification {
Expand Down Expand Up @@ -292,6 +307,7 @@ - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn
BOOL isCreatedColumn = [tableColumn.identifier isEqualToString:MPEntryTableCreatedColumnIdentifier];
BOOL isModifedColumn = [tableColumn.identifier isEqualToString:MPEntryTableModfiedColumnIdentifier];
BOOL isHistoryColumn = [tableColumn.identifier isEqualToString:MPEntryTableHistoryColumnIdentifier];
BOOL isTOPTColumn = [tableColumn.identifier isEqualToString:MPEntryTableTOTPColumnIdentifier];

NSTableCellView *view = nil;
BOOL displayClearTextPasswords = [NSUserDefaults.standardUserDefaults boolForKey:kMPSettingsKeyDisplayClearTextPasswordsInEntryList];
Expand Down Expand Up @@ -415,6 +431,10 @@ - (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn
NSStringFromSelector(@selector(password))];
[view.textField bind:NSValueBinding toObject:view withKeyPath:passwordKeyPath options:nil];
}
else if(isTOPTColumn) {
NSString *TOTPKeyPath = [NSString stringWithFormat:@"%@.%@", NSStringFromSelector(@selector(objectValue)), NSStringFromSelector(@selector(timeOTP))];
[view.textField bind:NSValueBinding toObject:view withKeyPath:TOTPKeyPath options:nil];
}
}
return view;
}
Expand Down Expand Up @@ -678,6 +698,7 @@ - (void)_setupHeaderMenu {
[headerMenu addItemWithTitle:NSLocalizedString(@"MODIFIED", "Menu item to toggle display of modified date column in entry table") action:NULL keyEquivalent:@""];
[headerMenu addItemWithTitle:NSLocalizedString(@"CREATED", "Menu item to toggle display of created date column in entry table") action:NULL keyEquivalent:@""];
[headerMenu addItemWithTitle:NSLocalizedString(@"HISTORY", "Menu item to toggle display of history count column in entry table") action:NULL keyEquivalent:@""];
[headerMenu addItemWithTitle:NSLocalizedString(@"TOTP", "Menu item to toggle display of TOTP count column in entry table") action:NULL keyEquivalent:@""];

NSArray *identifier = @[ MPEntryTableTitleColumnIdentifier,
MPEntryTableUserNameColumnIdentifier,
Expand All @@ -687,7 +708,8 @@ - (void)_setupHeaderMenu {
MPEntryTableAttachmentColumnIdentifier,
MPEntryTableModfiedColumnIdentifier,
MPEntryTableCreatedColumnIdentifier,
MPEntryTableHistoryColumnIdentifier ];
MPEntryTableHistoryColumnIdentifier,
MPEntryTableTOTPColumnIdentifier];

NSDictionary *options = @{ NSValueTransformerNameBindingOption : NSNegateBooleanTransformerName };
for(NSMenuItem *item in headerMenu.itemArray) {
Expand All @@ -696,6 +718,8 @@ - (void)_setupHeaderMenu {
[item bind:NSValueBinding toObject:column withKeyPath:NSHiddenBinding options:options];
}

NSTableColumn *totpColumn = [self.entryTable tableColumnWithIdentifier:MPEntryTableTOTPColumnIdentifier];
[self bind:NSStringFromSelector(@selector(totpColumnHidden)) toObject:totpColumn withKeyPath:NSHiddenBinding options:nil]; // double binding works?

[headerMenu addItem:[NSMenuItem separatorItem]];
NSMenuItem *showPasswordsItem = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(@"SHOW_CLEAR_TEXT_PASSWORDS", "Menu item to toggle display type of passwords") action:NULL keyEquivalent:@""];
Expand All @@ -707,6 +731,21 @@ - (void)_setupHeaderMenu {
self.entryTable.headerView.menu = headerMenu;
}

- (void)_updateTOTPTimer {
if(self.totpColumnHidden) {
[self.totpUpdateTimer invalidate];
self.totpUpdateTimer = nil;
return;
}
__weak MPEntryViewController *welf = self;
self.totpUpdateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
NSLog(@"Update TOTP Column Content");
NSIndexSet *columnIndex = [NSIndexSet indexSetWithIndex:[welf.entryTable columnWithIdentifier:MPEntryTableTOTPColumnIdentifier]];
NSIndexSet *rowIndexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,welf.entryTable.numberOfRows)];
[welf.entryTable reloadDataForRowIndexes:rowIndexes columnIndexes:columnIndex];
}];
}

#pragma mark Actions
- (void)copyPassword:(id)sender {
NSArray *nodes = self.currentTargetNodes;
Expand Down

0 comments on commit 05ba068

Please sign in to comment.