Skip to content

Commit

Permalink
Move Hide Usernames/Passwords into view menu
Browse files Browse the repository at this point in the history
* Fixes keepassxreboot#4982
* Removed a lot of redundant and passthrough code
  • Loading branch information
droidmonkey committed Oct 12, 2020
1 parent c2f36d8 commit 1278216
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 196 deletions.
1 change: 1 addition & 0 deletions src/core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ static const QHash<Config::ConfigKey, ConfigDirective> configStrings = {
{Config::GUI_MinimizeOnStartup, {QS("GUI/MinimizeOnStartup"), Roaming, false}},
{Config::GUI_MinimizeOnClose, {QS("GUI/MinimizeOnClose"), Roaming, false}},
{Config::GUI_HideUsernames, {QS("GUI/HideUsernames"), Roaming, false}},
{Config::GUI_HidePasswords, {QS("GUI/HidePasswords"), Roaming, true}},
{Config::GUI_AdvancedSettings, {QS("GUI/AdvancedSettings"), Roaming, false}},
{Config::GUI_MonospaceNotes, {QS("GUI/MonospaceNotes"), Roaming, false}},
{Config::GUI_ApplicationTheme, {QS("GUI/ApplicationTheme"), Roaming, QS("auto")}},
Expand Down
1 change: 1 addition & 0 deletions src/core/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ class Config : public QObject
GUI_MinimizeOnStartup,
GUI_MinimizeOnClose,
GUI_HideUsernames,
GUI_HidePasswords,
GUI_AdvancedSettings,
GUI_MonospaceNotes,
GUI_ApplicationTheme,
Expand Down
32 changes: 0 additions & 32 deletions src/gui/DatabaseWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,38 +329,6 @@ void DatabaseWidget::setPreviewSplitterSizes(const QList<int>& sizes)
m_previewSplitter->setSizes(sizes);
}

/**
* Get current state of entry view 'Hide Usernames' setting
*/
bool DatabaseWidget::isUsernamesHidden() const
{
return m_entryView->isUsernamesHidden();
}

/**
* Set state of entry view 'Hide Usernames' setting
*/
void DatabaseWidget::setUsernamesHidden(bool hide)
{
m_entryView->setUsernamesHidden(hide);
}

/**
* Get current state of entry view 'Hide Passwords' setting
*/
bool DatabaseWidget::isPasswordsHidden() const
{
return m_entryView->isPasswordsHidden();
}

/**
* Set state of entry view 'Hide Passwords' setting
*/
void DatabaseWidget::setPasswordsHidden(bool hide)
{
m_entryView->setPasswordsHidden(hide);
}

/**
* Get current view state of entry view
*/
Expand Down
4 changes: 0 additions & 4 deletions src/gui/DatabaseWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ class DatabaseWidget : public QStackedWidget

QStringList customEntryAttributes() const;
bool isEditWidgetModified() const;
bool isUsernamesHidden() const;
void setUsernamesHidden(bool hide);
bool isPasswordsHidden() const;
void setPasswordsHidden(bool hide);
void clearAllWidgets();
bool currentEntryHasTitle();
bool currentEntryHasUsername();
Expand Down
12 changes: 0 additions & 12 deletions src/gui/DatabaseWidgetStateSync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ DatabaseWidgetStateSync::DatabaseWidgetStateSync(QObject* parent)
{
m_mainSplitterSizes = variantToIntList(config()->get(Config::GUI_SplitterState));
m_previewSplitterSizes = variantToIntList(config()->get(Config::GUI_PreviewSplitterState));
m_hideUsernames = config()->get(Config::GUI_HideUsernames).toBool();
m_hidePasswords = true;
m_listViewState = config()->get(Config::GUI_ListViewState).toByteArray();
m_searchViewState = config()->get(Config::GUI_SearchViewState).toByteArray();

Expand All @@ -48,7 +46,6 @@ void DatabaseWidgetStateSync::sync()
{
config()->set(Config::GUI_SplitterState, intListToVariant(m_mainSplitterSizes));
config()->set(Config::GUI_PreviewSplitterState, intListToVariant(m_previewSplitterSizes));
config()->set(Config::GUI_HideUsernames, m_hideUsernames);
config()->set(Config::GUI_ListViewState, m_listViewState);
config()->set(Config::GUI_SearchViewState, m_searchViewState);
config()->sync();
Expand Down Expand Up @@ -104,9 +101,6 @@ void DatabaseWidgetStateSync::setActive(DatabaseWidget* dbWidget)
*/
void DatabaseWidgetStateSync::restoreListView()
{
m_activeDbWidget->setUsernamesHidden(m_hideUsernames);
m_activeDbWidget->setPasswordsHidden(m_hidePasswords);

if (!m_listViewState.isEmpty()) {
m_activeDbWidget->setEntryViewState(m_listViewState);
}
Expand All @@ -129,9 +123,6 @@ void DatabaseWidgetStateSync::restoreListView()
*/
void DatabaseWidgetStateSync::restoreSearchView()
{
m_activeDbWidget->setUsernamesHidden(m_hideUsernames);
m_activeDbWidget->setPasswordsHidden(m_hidePasswords);

if (!m_searchViewState.isEmpty()) {
m_activeDbWidget->setEntryViewState(m_searchViewState);
} else {
Expand Down Expand Up @@ -169,9 +160,6 @@ void DatabaseWidgetStateSync::updateViewState()
return;
}

m_hideUsernames = m_activeDbWidget->isUsernamesHidden();
m_hidePasswords = m_activeDbWidget->isPasswordsHidden();

if (m_activeDbWidget->isSearchActive()) {
m_searchViewState = m_activeDbWidget->entryViewState();
} else {
Expand Down
3 changes: 0 additions & 3 deletions src/gui/DatabaseWidgetStateSync.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@ private slots:
QList<int> m_mainSplitterSizes;
QList<int> m_previewSplitterSizes;

bool m_hideUsernames;
bool m_hidePasswords;

QByteArray m_listViewState;
QByteArray m_searchViewState;
};
Expand Down
29 changes: 9 additions & 20 deletions src/gui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,6 @@ MainWindow::MainWindow()
shortcut = new QShortcut(dbTabModifier + Qt::Key_9, this);
connect(shortcut, &QShortcut::activated, [this]() { selectDatabaseTab(m_ui->tabWidget->count() - 1); });

// Toggle password and username visibility in entry view
new QShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_C, this, SLOT(togglePasswordsHidden()));
new QShortcut(Qt::CTRL + Qt::SHIFT + Qt::Key_B, this, SLOT(toggleUsernamesHidden()));

m_ui->actionDatabaseNew->setIcon(resources()->icon("document-new"));
m_ui->actionDatabaseOpen->setIcon(resources()->icon("document-open"));
m_ui->menuRecentDatabases->setIcon(resources()->icon("document-open-recent"));
Expand Down Expand Up @@ -1126,22 +1122,6 @@ void MainWindow::databaseTabChanged(int tabIndex)
m_actionMultiplexer.setCurrentObject(m_ui->tabWidget->currentDatabaseWidget());
}

void MainWindow::togglePasswordsHidden()
{
auto dbWidget = m_ui->tabWidget->currentDatabaseWidget();
if (dbWidget) {
dbWidget->setPasswordsHidden(!dbWidget->isPasswordsHidden());
}
}

void MainWindow::toggleUsernamesHidden()
{
auto dbWidget = m_ui->tabWidget->currentDatabaseWidget();
if (dbWidget) {
dbWidget->setUsernamesHidden(!dbWidget->isUsernamesHidden());
}
}

void MainWindow::closeEvent(QCloseEvent* event)
{
if (m_appExiting) {
Expand Down Expand Up @@ -1767,4 +1747,13 @@ void MainWindow::initViewMenu()
show();
});

m_ui->actionHideUsernames->setChecked(config()->get(Config::GUI_HideUsernames).toBool());
connect(m_ui->actionHideUsernames, &QAction::toggled, this, [](bool checked) {
config()->set(Config::GUI_HideUsernames, checked);
});

m_ui->actionHidePasswords->setChecked(config()->get(Config::GUI_HidePasswords).toBool());
connect(m_ui->actionHidePasswords, &QAction::toggled, this, [](bool checked) {
config()->set(Config::GUI_HidePasswords, checked);
});
}
2 changes: 0 additions & 2 deletions src/gui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,6 @@ private slots:
void selectNextDatabaseTab();
void selectPreviousDatabaseTab();
void selectDatabaseTab(int tabIndex, bool wrap = false);
void togglePasswordsHidden();
void toggleUsernamesHidden();
void obtainContextFocusLock();
void releaseContextFocusLock();
void agentEnabled(bool enabled);
Expand Down
27 changes: 27 additions & 0 deletions src/gui/MainWindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,8 @@
<addaction name="actionAlwaysOnTop"/>
<addaction name="actionShowPreviewPanel"/>
<addaction name="actionShowToolbar"/>
<addaction name="actionHideUsernames"/>
<addaction name="actionHidePasswords"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuEntries"/>
Expand Down Expand Up @@ -984,6 +986,31 @@
<string notr="true">Ctrl+Shift+A</string>
</property>
</action>
<action name="actionHideUsernames">
<property name="checkable">
<bool>true</bool>
</property>
<property name="text">
<string>Hide Usernames</string>
</property>
<property name="shortcut">
<string notr="true">Ctrl+Shift+B</string>
</property>
</action>
<action name="actionHidePasswords">
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>true</bool>
</property>
<property name="text">
<string>Hide Passwords</string>
</property>
<property name="shortcut">
<string notr="true">Ctrl+Shift+C</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
Expand Down
60 changes: 20 additions & 40 deletions src/gui/entry/EntryModel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@
EntryModel::EntryModel(QObject* parent)
: QAbstractTableModel(parent)
, m_group(nullptr)
, m_hideUsernames(false)
, m_hidePasswords(true)
, HiddenContentDisplay(QString("\u25cf").repeated(6))
, DateFormat(Qt::DefaultLocaleShortDate)
{
connect(config(), &Config::changed, this, &EntryModel::onConfigChanged);
}

Entry* EntryModel::entryFromIndex(const QModelIndex& index) const
Expand Down Expand Up @@ -156,17 +155,20 @@ QVariant EntryModel::data(const QModelIndex& index, int role) const
}
return result;
case Username:
if (m_hideUsernames) {
if (config()->get(Config::GUI_HideUsernames).toBool()) {
result = EntryModel::HiddenContentDisplay;
} else {
result = entry->resolveMultiplePlaceholders(entry->username());
}
if (attr->isReference(EntryAttributes::UserNameKey)) {
result.prepend(tr("Ref: ", "Reference abbreviation"));
}
if (entry->username().isEmpty() && !config()->get(Config::Security_PasswordEmptyPlaceholder).toBool()) {
result = "";
}
return result;
case Password:
if (m_hidePasswords) {
if (config()->get(Config::GUI_HidePasswords).toBool()) {
result = EntryModel::HiddenContentDisplay;
} else {
result = entry->resolveMultiplePlaceholders(entry->password());
Expand Down Expand Up @@ -537,6 +539,20 @@ void EntryModel::entryDataChanged(Entry* entry)
emit dataChanged(index(row, 0), index(row, columnCount() - 1));
}

void EntryModel::onConfigChanged(Config::ConfigKey key)
{
switch (key) {
case Config::GUI_HideUsernames:
emit dataChanged(index(0, Username), index(rowCount() - 1, Username), {Qt::DisplayRole});
break;
case Config::GUI_HidePasswords:
emit dataChanged(index(0, Password), index(rowCount() - 1, Password), {Qt::DisplayRole});
break;
default:
break;
}
}

void EntryModel::severConnections()
{
if (m_group) {
Expand All @@ -560,39 +576,3 @@ void EntryModel::makeConnections(const Group* group)
connect(group, SIGNAL(entryMovedDown()), SLOT(entryMovedDown()));
connect(group, SIGNAL(entryDataChanged(Entry*)), SLOT(entryDataChanged(Entry*)));
}

/**
* Get current state of 'Hide Usernames' setting
*/
bool EntryModel::isUsernamesHidden() const
{
return m_hideUsernames;
}

/**
* Set state of 'Hide Usernames' setting and signal change
*/
void EntryModel::setUsernamesHidden(bool hide)
{
m_hideUsernames = hide;
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
emit usernamesHiddenChanged();
}

/**
* Get current state of 'Hide Passwords' setting
*/
bool EntryModel::isPasswordsHidden() const
{
return m_hidePasswords;
}

/**
* Set state of 'Hide Passwords' setting and signal change
*/
void EntryModel::setPasswordsHidden(bool hide)
{
m_hidePasswords = hide;
emit dataChanged(index(0, 0), index(rowCount() - 1, columnCount() - 1));
emit passwordsHiddenChanged();
}
16 changes: 4 additions & 12 deletions src/gui/entry/EntryModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <QAbstractTableModel>
#include <QPixmap>

#include "core/Config.h"

class Entry;
class Group;

Expand Down Expand Up @@ -64,15 +66,6 @@ class EntryModel : public QAbstractTableModel
void setGroup(Group* group);
void setEntries(const QList<Entry*>& entries);

bool isUsernamesHidden() const;
void setUsernamesHidden(bool hide);
bool isPasswordsHidden() const;
void setPasswordsHidden(bool hide);

signals:
void usernamesHiddenChanged();
void passwordsHiddenChanged();

private slots:
void entryAboutToAdd(Entry* entry);
void entryAdded(Entry* entry);
Expand All @@ -84,6 +77,8 @@ private slots:
void entryMovedDown();
void entryDataChanged(Entry* entry);

void onConfigChanged(Config::ConfigKey key);

private:
void severConnections();
void makeConnections(const Group* group);
Expand All @@ -93,9 +88,6 @@ private slots:
QList<Entry*> m_orgEntries;
QList<const Group*> m_allGroups;

bool m_hideUsernames;
bool m_hidePasswords;

const QString HiddenContentDisplay;
const Qt::DateFormat DateFormat;
};
Expand Down
Loading

0 comments on commit 1278216

Please sign in to comment.