diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 24742edef..0b9f7a823 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -105,6 +105,7 @@ set(keepassx_SOURCES gui/EditWidgetIcons.cpp gui/EditWidgetProperties.cpp gui/FileDialog.cpp + gui/Font.cpp gui/IconModels.cpp gui/KeePass1OpenWidget.cpp gui/KMessageWidget.cpp diff --git a/src/gui/Font.cpp b/src/gui/Font.cpp new file mode 100644 index 000000000..3583622dd --- /dev/null +++ b/src/gui/Font.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2017 KeePassXC Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "Font.h" + +#include + +QFont Font::fixedFont() +{ + QFont fixedFont = QFontDatabase::systemFont(QFontDatabase::FixedFont); + +#ifdef Q_OS_WIN + // try to use Consolas on Windows, because the default Courier New has too many similar characters + QFont consolasFont = QFontDatabase().font("Consolas", fixedFont.styleName(), fixedFont.pointSize()); + const QFont defaultFont; + if (fixedFont != defaultFont) { + fixedFont = consolasFont; + } +#endif + + return fixedFont; +} diff --git a/src/gui/Font.h b/src/gui/Font.h new file mode 100644 index 000000000..bfc3d7d36 --- /dev/null +++ b/src/gui/Font.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2017 KeePassXC Team + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 or (at your option) + * version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef KEEPASSX_FONT_H +#define KEEPASSX_FONT_H + +#include + +class Font +{ +public: + static QFont fixedFont(); +private: + Font() {} +}; + +#endif // KEEPASSX_FONT_H diff --git a/src/gui/PasswordEdit.cpp b/src/gui/PasswordEdit.cpp index ad736bf20..b084f9cf0 100644 --- a/src/gui/PasswordEdit.cpp +++ b/src/gui/PasswordEdit.cpp @@ -19,8 +19,7 @@ #include "PasswordEdit.h" #include "core/Config.h" - -#include +#include "gui/Font.h" const QColor PasswordEdit::CorrectSoFarColor = QColor(255, 205, 15); const QColor PasswordEdit::ErrorColor = QColor(255, 125, 125); @@ -33,16 +32,7 @@ PasswordEdit::PasswordEdit(QWidget* parent) updateStylesheet(); // use a monospace font for the password field - QFont passwordFont = QFontDatabase::systemFont(QFontDatabase::FixedFont); -#ifdef Q_OS_WIN - // try to use Consolas on Windows, because the default Courier New has too many similar characters - QFont consolasFont = QFontDatabase().font("Consolas", passwordFont.styleName(), passwordFont.pointSize()); - const QFont defaultFont; - if (passwordFont != defaultFont) { - passwordFont = consolasFont; - } -#endif - + QFont passwordFont = Font::fixedFont(); passwordFont.setLetterSpacing(QFont::PercentageSpacing, 110); setFont(passwordFont); } diff --git a/src/gui/entry/EditEntryWidget.cpp b/src/gui/entry/EditEntryWidget.cpp index dda0ab1d0..db35b2f8f 100644 --- a/src/gui/entry/EditEntryWidget.cpp +++ b/src/gui/entry/EditEntryWidget.cpp @@ -47,6 +47,7 @@ #include "gui/FileDialog.h" #include "gui/MessageBox.h" #include "gui/Clipboard.h" +#include "gui/Font.h" #include "gui/entry/AutoTypeAssociationsModel.h" #include "gui/entry/EntryAttachmentsModel.h" #include "gui/entry/EntryAttributesModel.h" @@ -267,7 +268,15 @@ void EditEntryWidget::setupSSHAgent() { m_sshAgentUi->setupUi(m_sshAgentWidget); - connect(m_sshAgentUi->privateKeyComboBox, SIGNAL(currentTextChanged(QString)), SLOT(updateSSHAgentKeyInfo())); + QFont fixedFont = Font::fixedFont(); + m_sshAgentUi->fingerprintTextLabel->setFont(fixedFont); + m_sshAgentUi->commentTextLabel->setFont(fixedFont); + m_sshAgentUi->publicKeyEdit->setFont(fixedFont); + + connect(m_sshAgentUi->attachmentRadioButton, SIGNAL(clicked(bool)), SLOT(updateSSHAgentKeyInfo())); + connect(m_sshAgentUi->attachmentComboBox, SIGNAL(currentIndexChanged(int)), SLOT(updateSSHAgentKeyInfo())); + connect(m_sshAgentUi->externalFileRadioButton, SIGNAL(clicked(bool)), SLOT(updateSSHAgentKeyInfo())); + connect(m_sshAgentUi->externalFileEdit, SIGNAL(textChanged(QString)), SLOT(updateSSHAgentKeyInfo())); connect(m_sshAgentUi->browseButton, SIGNAL(clicked()), SLOT(browsePrivateKey())); connect(m_sshAgentUi->addToAgentButton, SIGNAL(clicked()), SLOT(addKeyToAgent())); connect(m_sshAgentUi->removeFromAgentButton, SIGNAL(clicked()), SLOT(removeKeyFromAgent())); @@ -279,8 +288,6 @@ void EditEntryWidget::setupSSHAgent() void EditEntryWidget::updateSSHAgent() { - // TODO: unsafe use of translations - QString prefix = tr("Attachment") + ": "; KeeAgentSettings settings; settings.fromXml(m_entryAttachments->value("KeeAgent.settings")); @@ -289,29 +296,33 @@ void EditEntryWidget::updateSSHAgent() m_sshAgentUi->requireUserConfirmationCheckBox->setChecked(settings.useConfirmConstraintWhenAdding()); m_sshAgentUi->lifetimeCheckBox->setChecked(settings.useLifetimeConstraintWhenAdding()); m_sshAgentUi->lifetimeSpinBox->setValue(settings.lifetimeConstraintDuration()); - m_sshAgentUi->privateKeyComboBox->clear(); + m_sshAgentUi->attachmentComboBox->clear(); m_sshAgentUi->addToAgentButton->setEnabled(false); m_sshAgentUi->removeFromAgentButton->setEnabled(false); m_sshAgentUi->copyToClipboardButton->setEnabled(false); + m_sshAgentUi->attachmentComboBox->addItem(""); + for (QString fileName : m_entryAttachments->keys()) { if (fileName == "KeeAgent.settings") { continue; } - m_sshAgentUi->privateKeyComboBox->addItem(prefix + fileName); + m_sshAgentUi->attachmentComboBox->addItem(fileName); } + m_sshAgentUi->attachmentComboBox->setCurrentText(settings.attachmentName()); + m_sshAgentUi->externalFileEdit->setText(settings.fileName()); + if (settings.selectedType() == "attachment") { - m_sshAgentUi->privateKeyComboBox->setCurrentText(prefix + settings.attachmentName()); - } else if (!settings.fileName().isEmpty()) { - m_sshAgentUi->privateKeyComboBox->addItem(settings.fileName()); - m_sshAgentUi->privateKeyComboBox->setCurrentText(settings.fileName()); + m_sshAgentUi->attachmentRadioButton->setChecked(true); } else { - m_sshAgentUi->privateKeyComboBox->setCurrentText(""); + m_sshAgentUi->externalFileRadioButton->setChecked(true); } m_sshAgentSettings = settings; + + updateSSHAgentKeyInfo(); } void EditEntryWidget::updateSSHAgentKeyInfo() @@ -319,28 +330,24 @@ void EditEntryWidget::updateSSHAgentKeyInfo() m_sshAgentUi->addToAgentButton->setEnabled(false); m_sshAgentUi->removeFromAgentButton->setEnabled(false); m_sshAgentUi->copyToClipboardButton->setEnabled(false); - m_sshAgentUi->fingerprintEdit->setText(""); - m_sshAgentUi->commentEdit->setText(""); + m_sshAgentUi->fingerprintTextLabel->setText(tr("n/a")); + m_sshAgentUi->commentTextLabel->setText(tr("n/a")); m_sshAgentUi->decryptButton->setEnabled(false); m_sshAgentUi->publicKeyEdit->document()->setPlainText(""); - if (m_sshAgentUi->privateKeyComboBox->currentText().isEmpty()) { - return; - } - OpenSSHKey key; if (!getOpenSSHKey(key)) { return; } - m_sshAgentUi->fingerprintEdit->setText(key.fingerprint()); + m_sshAgentUi->fingerprintTextLabel->setText(key.fingerprint()); if (key.encrypted()) { - m_sshAgentUi->commentEdit->setText(tr("(encrypted)")); + m_sshAgentUi->commentTextLabel->setText(tr("(encrypted)")); m_sshAgentUi->decryptButton->setEnabled(true); } else { - m_sshAgentUi->commentEdit->setText(key.comment()); + m_sshAgentUi->commentTextLabel->setText(key.comment()); } m_sshAgentUi->publicKeyEdit->document()->setPlainText(key.publicKey()); @@ -357,7 +364,7 @@ void EditEntryWidget::updateSSHAgentKeyInfo() void EditEntryWidget::saveSSHAgentConfig() { KeeAgentSettings settings; - QString privateKeyPath = m_sshAgentUi->privateKeyComboBox->currentText(); + QString privateKeyPath = m_sshAgentUi->attachmentComboBox->currentText(); settings.setAddAtDatabaseOpen(m_sshAgentUi->addKeyToAgentCheckBox->isChecked()); settings.setRemoveAtDatabaseClose(m_sshAgentUi->removeKeyFromAgentCheckBox->isChecked()); @@ -365,17 +372,13 @@ void EditEntryWidget::saveSSHAgentConfig() settings.setUseLifetimeConstraintWhenAdding(m_sshAgentUi->lifetimeCheckBox->isChecked()); settings.setLifetimeConstraintDuration(m_sshAgentUi->lifetimeSpinBox->value()); - // TODO: unsafe use of translations - QString prefix = tr("Attachment") + ": "; - if (privateKeyPath.startsWith(prefix)) { + if (m_sshAgentUi->attachmentRadioButton->isChecked()) { settings.setSelectedType("attachment"); - settings.setAttachmentName(privateKeyPath.remove(0, prefix.length())); - settings.setFileName(""); } else { settings.setSelectedType("file"); - settings.setFileName(privateKeyPath); - settings.setAttachmentName(""); } + settings.setAttachmentName(m_sshAgentUi->attachmentComboBox->currentText()); + settings.setFileName(m_sshAgentUi->externalFileEdit->text()); // we don't use this as we don't run an agent but for compatibility we set it if necessary settings.setAllowUseOfSshKey(settings.addAtDatabaseOpen() || settings.removeAtDatabaseClose()); @@ -396,23 +399,22 @@ void EditEntryWidget::browsePrivateKey() { QString fileName = QFileDialog::getOpenFileName(this, tr("Select private key"), ""); if (!fileName.isEmpty()) { - m_sshAgentUi->privateKeyComboBox->addItem(fileName); - m_sshAgentUi->privateKeyComboBox->setCurrentText(fileName); + m_sshAgentUi->externalFileEdit->setText(fileName); } } bool EditEntryWidget::getOpenSSHKey(OpenSSHKey& key) { - QString privateKeyPath = m_sshAgentUi->privateKeyComboBox->currentText(); QByteArray privateKeyData; - // TODO: unsafe use of translations - QString prefix = tr("Attachment") + ": "; - if (privateKeyPath.startsWith(prefix)) { - QString attachmentName = privateKeyPath.remove(0, prefix.length()); - privateKeyData = m_entryAttachments->value(attachmentName); + if (m_sshAgentUi->attachmentRadioButton->isChecked()) { + privateKeyData = m_entryAttachments->value(m_sshAgentUi->attachmentComboBox->currentText()); } else { - QFile localFile(privateKeyPath); + QFile localFile(m_sshAgentUi->externalFileEdit->text()); + + if (localFile.fileName().isEmpty()) { + return false; + } if (localFile.size() > 1024 * 1024) { showMessage(tr("File too large to be a private key"), MessageWidget::Error); @@ -427,6 +429,10 @@ bool EditEntryWidget::getOpenSSHKey(OpenSSHKey& key) privateKeyData = localFile.readAll(); } + if (privateKeyData.length() == 0) { + return false; + } + if (!key.parse(privateKeyData)) { showMessage(key.errorString(), MessageWidget::Error); return false; @@ -446,7 +452,7 @@ void EditEntryWidget::addKeyToAgent() if (!key.openPrivateKey(m_entry->password())) { showMessage(key.errorString(), MessageWidget::Error); } else { - m_sshAgentUi->commentEdit->setText(key.comment()); + m_sshAgentUi->commentTextLabel->setText(key.comment()); m_sshAgentUi->publicKeyEdit->document()->setPlainText(key.publicKey()); } @@ -484,7 +490,7 @@ void EditEntryWidget::decryptPrivateKey() if (!key.openPrivateKey(m_entry->password())) { showMessage(key.errorString(), MessageWidget::Error); } else { - m_sshAgentUi->commentEdit->setText(key.comment()); + m_sshAgentUi->commentTextLabel->setText(key.comment()); m_sshAgentUi->publicKeyEdit->document()->setPlainText(key.publicKey()); } } diff --git a/src/gui/entry/EditEntryWidgetSSHAgent.ui b/src/gui/entry/EditEntryWidgetSSHAgent.ui index 2d88327cc..34c8fae82 100644 --- a/src/gui/entry/EditEntryWidgetSSHAgent.ui +++ b/src/gui/entry/EditEntryWidgetSSHAgent.ui @@ -26,63 +26,6 @@ 0 - - - - Remove key from agent when database is closed/locked - - - - - - - Fingerprint - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - - - - - true - - - - - - - false - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter - - - true - - - - - - - Private key - - - Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - - - - - - Public key - - - Qt::AlignRight|Qt::AlignTop|Qt::AlignTrailing - - - @@ -118,15 +61,32 @@ - + - Comment + Fingerprint Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + Remove key from agent when database is closed/locked + + + + + + + Public key + + + Qt::AlignRight|Qt::AlignTop|Qt::AlignTrailing + + + @@ -134,26 +94,129 @@ - - - - - 0 - 0 - + + + + Comment - - true + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter - - + + - Browse... + Decrypt + + + + + + + Monospace + + + + n/a + + + Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + + Copy to clipboard + + + + + + + Private key + + + + + + External file + + + + + + + Browse... + + + + + + + Attachment + + + true + + + + + + + + + + + + Add to agent + + + + + + + Remove from agent + + + + + + + + + + 0 + 0 + + + + false + + + + + + @@ -162,43 +225,48 @@ - - - Copy to clipboard + + + + Monospace + - - - - - - - - Add to agent - - - - - - - Remove from agent - - - - - - - true - - - - Decrypt - - + + + + + + + Monospace + + + + n/a + + + Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + +