feat: add restrictions to key database

This commit is contained in:
saturneric 2024-11-17 14:30:43 +01:00
parent a4e6258497
commit 1ae49c85f0
4 changed files with 75 additions and 27 deletions

View File

@ -99,9 +99,12 @@ auto InitGpgME(const QString& gpgconf_path, const QString& gnupg_path) -> bool {
gpgme_set_locale(nullptr, LC_MESSAGES, setlocale(LC_MESSAGES, nullptr));
#endif
if (!gnupg_path.isEmpty()) {
if (!gpgconf_path.isEmpty()) {
CheckGpgError(gpgme_set_engine_info(GPGME_PROTOCOL_GPGCONF,
gpgconf_path.toUtf8(), nullptr));
}
if (!gnupg_path.isEmpty()) {
CheckGpgError(gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP,
gnupg_path.toUtf8(), nullptr));
}

View File

@ -28,15 +28,19 @@
#include "KeyDatabaseEditDialog.h"
#include "core/utils/GpgUtils.h"
#include "core/utils/MemoryUtils.h"
#include "ui_KeyDatabaseEditDialog.h"
namespace GpgFrontend::UI {
KeyDatabaseEditDialog::KeyDatabaseEditDialog(QWidget* parent)
: GeneralDialog("KeyDatabaseEditDialog", parent),
ui_(GpgFrontend::SecureCreateSharedObject<Ui_KeyDatabaseEditDialog>()) {
ui_(GpgFrontend::SecureCreateSharedObject<Ui_KeyDatabaseEditDialog>()),
key_database_infos_(GetGpgKeyDatabaseInfos()) {
ui_->setupUi(this);
ui_->keyDBPathShowLabel->setHidden(true);
ui_->keyDBNameLabel->setText(tr("Key Database Name"));
ui_->keyDBPathLabel->setText(tr("Key Database Path"));
ui_->selectKeyDBButton->setText(tr("Select A Key Database Path"));
@ -53,13 +57,15 @@ KeyDatabaseEditDialog::KeyDatabaseEditDialog(QWidget* parent)
"exists readable directory."));
}
if (!path.trimmed().isEmpty()) {
path_ = QFileInfo(path).absoluteFilePath();
ui_->keyDBPathShowLabel->setText(path_);
ui_->keyDBPathShowLabel->setHidden(false);
}
});
connect(ui_->buttonBox, &QDialogButtonBox::accepted, this,
&KeyDatabaseEditDialog::slot_button_box_accepted);
connect(ui_->buttonBox, &QDialogButtonBox::accepted, this, &QDialog::accept);
connect(ui_->buttonBox, &QDialogButtonBox::rejected, this, &QDialog::reject);
setAttribute(Qt::WA_DeleteOnClose);
setModal(true);
@ -67,7 +73,29 @@ KeyDatabaseEditDialog::KeyDatabaseEditDialog(QWidget* parent)
void KeyDatabaseEditDialog::slot_button_box_accepted() {
name_ = ui_->keyDBNameLineEdit->text();
if (name_.isEmpty()) {
slot_show_err_msg(tr("The key database name cannot be empty."));
return;
}
if (path_.isEmpty()) {
slot_show_err_msg(tr("The key database path cannot be empty."));
return;
}
for (const auto& info : key_database_infos_) {
if (info.name == name_) {
slot_show_err_msg(tr("A key database with the name '%1' already exists. "
"Please choose a different name.")
.arg(name_));
return;
}
}
slot_clear_err_msg();
emit SignalKeyDatabaseInfoAccepted(name_, path_);
this->accept();
}
auto KeyDatabaseEditDialog::check_custom_gnupg_key_database_path(
@ -82,8 +110,18 @@ void KeyDatabaseEditDialog::SetDefaultName(QString name) {
name_ = std::move(name);
}
void KeyDatabaseEditDialog::SetDefaultPath(QString path) {
void KeyDatabaseEditDialog::SetDefaultPath(const QString& path) {
path_ = QFileInfo(path).absoluteFilePath();
ui_->keyDBPathShowLabel->setText(path_);
}
void KeyDatabaseEditDialog::slot_show_err_msg(const QString& error_msg) {
ui_->errorLabel->setText(error_msg);
ui_->errorLabel->setStyleSheet("color: red;");
ui_->errorLabel->setHidden(false);
}
void KeyDatabaseEditDialog::slot_clear_err_msg() {
ui_->errorLabel->setText({});
ui_->errorLabel->setHidden(true);
}
}; // namespace GpgFrontend::UI

View File

@ -30,6 +30,7 @@
#include <utility>
#include "core/model/KeyDatabaseInfo.h"
#include "ui/dialog/GeneralDialog.h"
class Ui_KeyDatabaseEditDialog;
@ -42,7 +43,7 @@ class KeyDatabaseEditDialog : public GeneralDialog {
void SetDefaultName(QString name);
void SetDefaultPath(QString path);
void SetDefaultPath(const QString& path);
signals:
void SignalKeyDatabaseInfoAccepted(QString name, QString path);
@ -51,9 +52,14 @@ class KeyDatabaseEditDialog : public GeneralDialog {
std::shared_ptr<Ui_KeyDatabaseEditDialog> ui_; ///<
QString name_;
QString path_;
QList<KeyDatabaseInfo> key_database_infos_;
void slot_button_box_accepted();
void slot_show_err_msg(const QString& error_msg);
void slot_clear_err_msg();
auto check_custom_gnupg_key_database_path(const QString& path) -> bool;
};

View File

@ -24,7 +24,14 @@
</widget>
</item>
<item>
<widget class="QLineEdit" name="keyDBNameLineEdit"/>
<widget class="QLineEdit" name="keyDBNameLineEdit">
<property name="maxLength">
<number>18</number>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="keyDBPathLabel">
@ -53,7 +60,7 @@
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
<enum>Qt::Orientation::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
@ -63,15 +70,25 @@
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="errorLabel">
<property name="text">
<string/>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
<enum>Qt::Orientation::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
<set>QDialogButtonBox::StandardButton::Cancel|QDialogButtonBox::StandardButton::Ok</set>
</property>
</widget>
</item>
@ -79,22 +96,6 @@
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>KeyDatabaseEditDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>