Skip to content

Commit

Permalink
Hide hardware key widgets if no key is detected
Browse files Browse the repository at this point in the history
  • Loading branch information
phoerious committed Dec 13, 2023
1 parent fd584d4 commit a5d50f4
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 142 deletions.
12 changes: 0 additions & 12 deletions share/translations/keepassxc_en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1525,14 +1525,6 @@ Backup database located at %2</source>
<source>Browse…</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Refresh hardware tokens</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Refresh</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Unlock Database</source>
<translation type="unfinished"></translation>
Expand Down Expand Up @@ -1627,10 +1619,6 @@ If you do not have a key file, please leave the field empty.</source>
<source>Detecting hardware keys…</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>No hardware keys detected</source>
<translation type="unfinished"></translation>
</message>
<message>
<source>Select hardware key…</source>
<translation type="unfinished"></translation>
Expand Down
50 changes: 24 additions & 26 deletions src/gui/DatabaseOpenWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ DatabaseOpenWidget::DatabaseOpenWidget(QWidget* parent)
m_ui->keyFileLabelHelp->setIcon(icons()->icon("system-help").pixmap(QSize(12, 12)));
connect(m_ui->keyFileLabelHelp, SIGNAL(clicked(bool)), SLOT(openKeyFileHelp()));

toggleHardwareKeyWidgets(false);

#ifdef WITH_XC_YUBIKEY
m_ui->hardwareKeyProgress->setVisible(false);
QSizePolicy sp = m_ui->hardwareKeyProgress->sizePolicy();
sp.setRetainSizeWhenHidden(true);
m_ui->hardwareKeyProgress->setSizePolicy(sp);

connect(m_ui->buttonRedetectYubikey, SIGNAL(clicked()), SLOT(pollHardwareKey()));
connect(YubiKey::instance(), SIGNAL(detectComplete(bool)), SLOT(hardwareKeyResponse(bool)), Qt::QueuedConnection);

connect(YubiKey::instance(), &YubiKey::userInteractionRequest, this, [this] {
Expand All @@ -119,12 +119,6 @@ DatabaseOpenWidget::DatabaseOpenWidget(QWidget* parent)
}
});
connect(YubiKey::instance(), &YubiKey::challengeCompleted, this, [this] { m_ui->messageWidget->hide(); });
#else
m_ui->hardwareKeyLabel->setVisible(false);
m_ui->hardwareKeyLabelHelp->setVisible(false);
m_ui->buttonRedetectYubikey->setVisible(false);
m_ui->challengeResponseCombo->setVisible(false);
m_ui->hardwareKeyProgress->setVisible(false);
#endif

// QuickUnlock actions
Expand All @@ -135,6 +129,14 @@ DatabaseOpenWidget::DatabaseOpenWidget(QWidget* parent)

DatabaseOpenWidget::~DatabaseOpenWidget() = default;

void DatabaseOpenWidget::toggleHardwareKeyWidgets(bool state)
{
m_ui->hardwareKeyCombo->setVisible(state);
m_ui->hardwareKeyProgress->setVisible(false);
m_ui->hardwareKeyLabel->setVisible(state);
m_ui->hardwareKeyLabelHelp->setVisible(state);
}

void DatabaseOpenWidget::showEvent(QShowEvent* event)
{
DialogyWidget::showEvent(event);
Expand Down Expand Up @@ -219,7 +221,7 @@ void DatabaseOpenWidget::clearForms()
m_ui->keyFileLineEdit->clear();
m_ui->keyFileLineEdit->setShowPassword(false);
m_ui->keyFileLineEdit->setClearButtonEnabled(true);
m_ui->challengeResponseCombo->clear();
m_ui->hardwareKeyCombo->clear();
m_ui->centralStack->setCurrentIndex(0);

QString error;
Expand Down Expand Up @@ -398,9 +400,9 @@ QSharedPointer<CompositeKey> DatabaseOpenWidget::buildDatabaseKey()
auto lastChallengeResponse = config()->get(Config::LastChallengeResponse).toHash();
lastChallengeResponse.remove(m_filename);

int selectionIndex = m_ui->challengeResponseCombo->currentIndex();
int selectionIndex = m_ui->hardwareKeyCombo->currentIndex();
if (selectionIndex > 0) {
auto slot = m_ui->challengeResponseCombo->itemData(selectionIndex).value<YubiKeySlot>();
auto slot = m_ui->hardwareKeyCombo->itemData(selectionIndex).value<YubiKeySlot>();
auto crKey = QSharedPointer<ChallengeResponseKey>(new ChallengeResponseKey(slot));
databaseKey->addChallengeResponseKey(crKey);

Expand Down Expand Up @@ -446,11 +448,10 @@ void DatabaseOpenWidget::pollHardwareKey()
return;
}

m_ui->challengeResponseCombo->clear();
m_ui->challengeResponseCombo->addItem(tr("Detecting hardware keys…"));
m_ui->hardwareKeyCombo->clear();
m_ui->hardwareKeyCombo->addItem(tr("Detecting hardware keys…"));
m_ui->hardwareKeyCombo->setEnabled(false);

m_ui->buttonRedetectYubikey->setEnabled(false);
m_ui->challengeResponseCombo->setEnabled(false);
m_ui->hardwareKeyProgress->setVisible(true);
m_pollingHardwareKey = true;

Expand All @@ -459,19 +460,15 @@ void DatabaseOpenWidget::pollHardwareKey()

void DatabaseOpenWidget::hardwareKeyResponse(bool found)
{
m_ui->challengeResponseCombo->clear();
m_ui->buttonRedetectYubikey->setEnabled(true);
m_ui->hardwareKeyProgress->setVisible(false);
m_ui->hardwareKeyCombo->clear();
m_pollingHardwareKey = false;

if (!found) {
m_ui->challengeResponseCombo->addItem(tr("No hardware keys detected"));
m_ui->challengeResponseCombo->setEnabled(false);
toggleHardwareKeyWidgets(false);
return;
} else {
m_ui->challengeResponseCombo->addItem(tr("Select hardware key…"));
}

m_ui->hardwareKeyCombo->addItem(tr("Select hardware key…"));
YubiKeySlot lastUsedSlot;
if (config()->get(Config::RememberLastKeyFiles).toBool()) {
auto lastChallengeResponse = config()->get(Config::LastChallengeResponse).toHash();
Expand All @@ -487,15 +484,16 @@ void DatabaseOpenWidget::hardwareKeyResponse(bool found)
int selectedIndex = 0;
for (auto& slot : YubiKey::instance()->foundKeys()) {
// add detected YubiKey to combo box
m_ui->challengeResponseCombo->addItem(YubiKey::instance()->getDisplayName(slot), QVariant::fromValue(slot));
m_ui->hardwareKeyCombo->addItem(YubiKey::instance()->getDisplayName(slot), QVariant::fromValue(slot));
// Select this YubiKey + Slot if we used it in the past
if (lastUsedSlot == slot) {
selectedIndex = m_ui->challengeResponseCombo->count() - 1;
selectedIndex = m_ui->hardwareKeyCombo->count() - 1;
}
}

m_ui->challengeResponseCombo->setCurrentIndex(selectedIndex);
m_ui->challengeResponseCombo->setEnabled(true);
toggleHardwareKeyWidgets(true);
m_ui->hardwareKeyCombo->setEnabled(true);
m_ui->hardwareKeyCombo->setCurrentIndex(selectedIndex);
}

void DatabaseOpenWidget::openHardwareKeyHelp()
Expand Down
1 change: 1 addition & 0 deletions src/gui/DatabaseOpenWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ private slots:
void openKeyFileHelp();

private:
void toggleHardwareKeyWidgets(bool state);
#ifdef WITH_XC_YUBIKEY
QPointer<DeviceListener> m_deviceListener;
#endif
Expand Down
164 changes: 60 additions & 104 deletions src/gui/DatabaseOpenWidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
<property name="font">
<font>
<pointsize>12</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
Expand Down Expand Up @@ -200,6 +199,54 @@
<property name="topMargin">
<number>3</number>
</property>
<item row="1" column="3">
<layout class="QGridLayout" name="gridLayout">
<property name="spacing">
<number>0</number>
</property>
<item row="1" column="2">
<widget class="QProgressBar" name="hardwareKeyProgress">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>2</height>
</size>
</property>
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>0</number>
</property>
<property name="value">
<number>-1</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QComboBox" name="hardwareKeyCombo">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="accessibleName">
<string>Hardware key slot selection</string>
</property>
<property name="editable">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_4">
<property name="spacing">
Expand Down Expand Up @@ -251,54 +298,6 @@
</item>
</layout>
</item>
<item row="1" column="3">
<layout class="QGridLayout" name="gridLayout">
<property name="spacing">
<number>0</number>
</property>
<item row="1" column="2">
<widget class="QProgressBar" name="hardwareKeyProgress">
<property name="maximumSize">
<size>
<width>16777215</width>
<height>2</height>
</size>
</property>
<property name="minimum">
<number>0</number>
</property>
<property name="maximum">
<number>0</number>
</property>
<property name="value">
<number>-1</number>
</property>
<property name="textVisible">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QComboBox" name="challengeResponseCombo">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="accessibleName">
<string>Hardware key slot selection</string>
</property>
<property name="editable">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<layout class="QVBoxLayout" name="verticalLayout_7">
<property name="spacing">
Expand All @@ -315,7 +314,7 @@
<string>Hardware Key:</string>
</property>
<property name="buddy">
<cstring>challengeResponseCombo</cstring>
<cstring>hardwareKeyCombo</cstring>
</property>
</widget>
</item>
Expand Down Expand Up @@ -357,7 +356,7 @@
</layout>
</item>
<item>
<spacer name="verticalSpacer_6">
<spacer name="hardwareKeyLabelSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
Expand All @@ -375,11 +374,8 @@
</layout>
</item>
<item row="0" column="3">
<layout class="QGridLayout" name="gridLayout_2">
<property name="verticalSpacing">
<number>0</number>
</property>
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout" stretch="1,0">
<item>
<widget class="PasswordWidget" name="keyFileLineEdit" native="true">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
Expand All @@ -395,58 +391,19 @@
</property>
</widget>
</item>
</layout>
</item>
<item row="0" column="4">
<widget class="QPushButton" name="buttonBrowseFile">
<property name="toolTip">
<string>Browse for key file</string>
</property>
<property name="accessibleName">
<string>Browse for key file</string>
</property>
<property name="text">
<string>Browse…</string>
</property>
</widget>
</item>
<item row="1" column="4">
<layout class="QVBoxLayout" name="verticalLayout_3">
<property name="spacing">
<number>0</number>
</property>
<item>
<widget class="QPushButton" name="buttonRedetectYubikey">
<property name="enabled">
<bool>true</bool>
</property>
<widget class="QPushButton" name="buttonBrowseFile">
<property name="toolTip">
<string>Refresh hardware tokens</string>
<string>Browse for key file</string>
</property>
<property name="accessibleName">
<string>Refresh hardware tokens</string>
<string>Browse for key file</string>
</property>
<property name="text">
<string>Refresh</string>
<string>Browse…</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_5">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>0</width>
<height>2</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
Expand Down Expand Up @@ -525,7 +482,6 @@
<property name="font">
<font>
<pointsize>10</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
Expand Down Expand Up @@ -635,13 +591,13 @@
</customwidget>
</customwidgets>
<tabstops>
<tabstop>quickUnlockButton</tabstop>
<tabstop>resetQuickUnlockButton</tabstop>
<tabstop>editPassword</tabstop>
<tabstop>keyFileLineEdit</tabstop>
<tabstop>buttonBrowseFile</tabstop>
<tabstop>challengeResponseCombo</tabstop>
<tabstop>buttonRedetectYubikey</tabstop>
<tabstop>quickUnlockButton</tabstop>
<tabstop>resetQuickUnlockButton</tabstop>
<tabstop>hardwareKeyCombo</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources/>
<connections/>
Expand Down

0 comments on commit a5d50f4

Please sign in to comment.