Skip to content

Commit

Permalink
feat: add option to close all gnupg daemon at close
Browse files Browse the repository at this point in the history
  • Loading branch information
saturneric committed May 3, 2024
1 parent ddfaa5b commit 3d3c3d9
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 24 deletions.
12 changes: 11 additions & 1 deletion src/core/GpgCoreInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,17 @@

namespace GpgFrontend {

void DestroyGpgFrontendCore() { SingletonStorageCollection::Destroy(); }
void DestroyGpgFrontendCore() {
// kill all daemon if necessary
auto settings = GlobalSettingStation::GetInstance().GetSettings();
auto kill_all_gnupg_daemon_at_close =
settings.value("gnupg/kill_all_gnupg_daemon_at_close", false).toBool();
if (kill_all_gnupg_daemon_at_close) {
GpgAdvancedOperator::KillAllGpgComponents();
}

SingletonStorageCollection::Destroy();
}

auto VerifyGpgconfPath(const QFileInfo& gnupg_install_fs_path) -> bool {
return gnupg_install_fs_path.isAbsolute() && gnupg_install_fs_path.exists() &&
Expand Down
28 changes: 28 additions & 0 deletions src/core/function/gpg/GpgAdvancedOperator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,34 @@ void GpgFrontend::GpgAdvancedOperator::ReloadGpgComponents(
}});
}

void GpgFrontend::GpgAdvancedOperator::KillAllGpgComponents() {
const auto gpgconf_path = Module::RetrieveRTValueTypedOrDefault<>(
"core", "gpgme.ctx.gpgconf_path", QString{});
GF_CORE_LOG_DEBUG("got gpgconf path from rt: {}", gpgconf_path);

if (gpgconf_path.isEmpty()) {
GF_CORE_LOG_ERROR("cannot get valid gpgconf path from rt, abort.");
return;
}

GpgFrontend::GpgCommandExecutor::ExecuteSync(
{gpgconf_path, QStringList{"--verbose", "--kill", "all"},
[=](int exit_code, const QString &p_out, const QString &p_err) {
GF_CORE_LOG_DEBUG("gpgconf --kill all command got exit code: {}",
exit_code);
bool success = true;
if (exit_code != 0) {
success = false;
GF_CORE_LOG_ERROR(
"gpgconf execute error, process stderr: {}, process stdout: {}",
p_err, p_out);
return;
}

GF_CORE_LOG_DEBUG("gpgconf --kill --all execute result: {}", success);
}});
}

void GpgFrontend::GpgAdvancedOperator::RestartGpgComponents() {
const auto gpgconf_path = Module::RetrieveRTValueTypedOrDefault<>(
"core", "gpgme.ctx.gpgconf_path", QString{});
Expand Down
6 changes: 6 additions & 0 deletions src/core/function/gpg/GpgAdvancedOperator.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ class GPGFRONTEND_CORE_EXPORT GpgAdvancedOperator {
* @return false
*/
static void StartKeyBoxd(OperationCallback);

/**
* @brief
*
*/
static void KillAllGpgComponents();
};

} // namespace GpgFrontend
53 changes: 32 additions & 21 deletions src/ui/dialog/controller/GnuPGControllerDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,18 @@ GnuPGControllerDialog::GnuPGControllerDialog(QWidget* parent)
ui_->gpgmeDebugLogCheckBox->setText(tr("Enable GpgME Debug Log"));
ui_->useCustomGnuPGInstallPathCheckBox->setText(tr("Use Custom GnuPG"));
ui_->useCustomGnuPGInstallPathButton->setText(tr("Select GnuPG Path"));
ui_->keyDatabseUseCustomCheckBox->setText(
ui_->keyDatabaseUseCustomCheckBox->setText(
tr("Use Custom GnuPG Key Database Path"));
ui_->customKeyDatabasePathSelectButton->setText(
tr("Select Key Database Path"));
ui_->restartGpgAgentOnStartCheckBox->setText(
tr("Restart Gpg Agent on start"));
ui_->killAllGnuPGDaemonCheckBox->setText(
tr("Kill all gnupg daemon at close"));

// tips
ui_->customGnuPGPathTipsLabel->setText(
tr("Tips: please select a directroy where \"gpgconf\" is located in."));
tr("Tips: please select a directory where \"gpgconf\" is located in."));
ui_->restartTipsLabel->setText(
tr("Tips: notice that modify any of these settings will cause an "
"Application restart."));
Expand All @@ -71,7 +73,7 @@ GnuPGControllerDialog::GnuPGControllerDialog(QWidget* parent)
UISignalStation::GetInstance(),
&UISignalStation::SignalRestartApplication);

connect(ui_->keyDatabseUseCustomCheckBox, &QCheckBox::stateChanged, this,
connect(ui_->keyDatabaseUseCustomCheckBox, &QCheckBox::stateChanged, this,
[=](int state) {
ui_->customKeyDatabasePathSelectButton->setDisabled(
state != Qt::CheckState::Checked);
Expand All @@ -83,7 +85,7 @@ GnuPGControllerDialog::GnuPGControllerDialog(QWidget* parent)
state != Qt::CheckState::Checked);
});

connect(ui_->keyDatabseUseCustomCheckBox, &QCheckBox::stateChanged, this,
connect(ui_->keyDatabaseUseCustomCheckBox, &QCheckBox::stateChanged, this,
&GnuPGControllerDialog::slot_update_custom_key_database_path_label);

connect(ui_->useCustomGnuPGInstallPathCheckBox, &QCheckBox::stateChanged,
Expand All @@ -98,7 +100,7 @@ GnuPGControllerDialog::GnuPGControllerDialog(QWidget* parent)
this, tr("Open Directory"), {},
QFileDialog::ShowDirsOnly | QFileDialog::DontResolveSymlinks);

GF_UI_LOG_DEBUG("key databse path selected: {}",
GF_UI_LOG_DEBUG("key database path selected: {}",
selected_custom_key_database_path);

custom_key_database_path_ = selected_custom_key_database_path;
Expand All @@ -108,7 +110,7 @@ GnuPGControllerDialog::GnuPGControllerDialog(QWidget* parent)

// update ui
this->slot_update_custom_key_database_path_label(
this->ui_->keyDatabseUseCustomCheckBox->checkState());
this->ui_->keyDatabaseUseCustomCheckBox->checkState());
});

connect(
Expand Down Expand Up @@ -263,33 +265,40 @@ void GnuPGControllerDialog::slot_update_custom_gnupg_install_path_label(
void GnuPGControllerDialog::set_settings() {
auto settings = GlobalSettingStation::GetInstance().GetSettings();

bool non_ascii_at_file_operation =
auto non_ascii_at_file_operation =
settings.value("gnupg/non_ascii_at_file_operation", true).toBool();
if (non_ascii_at_file_operation)
if (non_ascii_at_file_operation) {
ui_->asciiModeCheckBox->setCheckState(Qt::Checked);
}

bool const use_custom_key_database_path =
auto use_custom_key_database_path =
settings.value("gnupg/use_custom_key_database_path", false).toBool();
if (use_custom_key_database_path) {
ui_->keyDatabseUseCustomCheckBox->setCheckState(Qt::Checked);
ui_->keyDatabaseUseCustomCheckBox->setCheckState(Qt::Checked);
}

bool const enable_gpgme_debug_log =
auto enable_gpgme_debug_log =
settings.value("gnupg/enable_gpgme_debug_log", false).toBool();
if (enable_gpgme_debug_log) {
ui_->gpgmeDebugLogCheckBox->setCheckState(Qt::Checked);
}

auto kill_all_gnupg_daemon_at_close =
settings.value("gnupg/kill_all_gnupg_daemon_at_close", false).toBool();
if (kill_all_gnupg_daemon_at_close) {
ui_->killAllGnuPGDaemonCheckBox->setCheckState(Qt::Checked);
}

this->slot_update_custom_key_database_path_label(
ui_->keyDatabseUseCustomCheckBox->checkState());
ui_->keyDatabaseUseCustomCheckBox->checkState());

bool const use_custom_gnupg_install_path =
auto use_custom_gnupg_install_path =
settings.value("gnupg/use_custom_gnupg_install_path", false).toBool();
if (use_custom_gnupg_install_path) {
ui_->useCustomGnuPGInstallPathCheckBox->setCheckState(Qt::Checked);
}

bool const use_pinentry_as_password_input_dialog =
auto use_pinentry_as_password_input_dialog =
settings
.value("gnupg/use_pinentry_as_password_input_dialog",
QString::fromLocal8Bit(qgetenv("container")) != "flatpak")
Expand All @@ -298,7 +307,7 @@ void GnuPGControllerDialog::set_settings() {
ui_->usePinentryAsPasswordInputDialogCheckBox->setCheckState(Qt::Checked);
}

bool const restart_gpg_agent_on_start =
auto restart_gpg_agent_on_start =
settings.value("gnupg/restart_gpg_agent_on_start", false).toBool();
if (restart_gpg_agent_on_start) {
ui_->restartGpgAgentOnStartCheckBox->setCheckState(Qt::Checked);
Expand All @@ -320,7 +329,7 @@ void GnuPGControllerDialog::apply_settings() {
settings.setValue("gnupg/non_ascii_at_file_operation",
ui_->asciiModeCheckBox->isChecked());
settings.setValue("gnupg/use_custom_key_database_path",
ui_->keyDatabseUseCustomCheckBox->isChecked());
ui_->keyDatabaseUseCustomCheckBox->isChecked());
settings.setValue("gnupg/use_custom_gnupg_install_path",
ui_->useCustomGnuPGInstallPathCheckBox->isChecked());
settings.setValue("gnupg/use_pinentry_as_password_input_dialog",
Expand All @@ -333,6 +342,8 @@ void GnuPGControllerDialog::apply_settings() {
ui_->currentCustomGnuPGInstallPathLabel->text());
settings.setValue("gnupg/restart_gpg_agent_on_start",
ui_->restartGpgAgentOnStartCheckBox->isChecked());
settings.setValue("gnupg/kill_all_gnupg_daemon_at_close",
ui_->killAllGnuPGDaemonCheckBox->isChecked());
}

auto GnuPGControllerDialog::get_restart_needed() const -> int {
Expand All @@ -347,23 +358,23 @@ void GnuPGControllerDialog::slot_set_restart_needed(int mode) {
auto GnuPGControllerDialog::check_custom_gnupg_path(QString path) -> bool {
if (path.isEmpty()) return false;

QFileInfo dir_info(path);
QFileInfo const dir_info(path);
if (!dir_info.exists() || !dir_info.isReadable() || !dir_info.isDir()) {
QMessageBox::critical(
this, tr("Illegal GnuPG Path"),
tr("Target GnuPG Path is not an exists readable directory."));
return false;
}

QDir dir(path);
QDir const dir(path);
if (!dir.isAbsolute()) {
QMessageBox::critical(this, tr("Illegal GnuPG Path"),
tr("Target GnuPG Path is not an absolute path."));
}
#ifdef WINDOWS
QFileInfo gpgconf_info(path + "/gpgconf.exe");
QFileInfo const gpgconf_info(path + "/gpgconf.exe");
#else
QFileInfo gpgconf_info(path + "/gpgconf");
QFileInfo const gpgconf_info(path + "/gpgconf");
#endif

if (!gpgconf_info.exists() || !gpgconf_info.isFile() ||
Expand All @@ -381,7 +392,7 @@ auto GnuPGControllerDialog::check_custom_gnupg_key_database_path(QString path)
-> bool {
if (path.isEmpty()) return false;

QFileInfo dir_info(path);
QFileInfo const dir_info(path);
if (!dir_info.exists() || !dir_info.isReadable() || !dir_info.isDir()) {
QMessageBox::critical(this, tr("Illegal GnuPG Key Database Path"),
tr("Target GnuPG Key Database Path is not an "
Expand Down
2 changes: 1 addition & 1 deletion src/ui/dialog/controller/ModuleControllerDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void ModuleControllerDialog::slot_load_module_details(
info << " - " << tr("Hash") << ": " << module->GetModuleHash() << Qt::endl;
info << " - " << tr("Path") << ": " << module->GetModulePath() << Qt::endl;

bool if_activated = module_manager_->IsModuleActivated(module_id);
auto if_activated = module_manager_->IsModuleActivated(module_id);

info << " - " << tr("Auto Activate") << ": "
<< (module_so.auto_activate ? tr("True") : tr("False")) << Qt::endl;
Expand Down
9 changes: 8 additions & 1 deletion ui/GnuPGControllerDialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="killAllGnuPGDaemonCheckBox">
<property name="text">
<string>Kill all gnupg daemon at close</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
Expand All @@ -75,7 +82,7 @@
<item row="0" column="0">
<layout class="QVBoxLayout" name="verticalLayout_6">
<item>
<widget class="QCheckBox" name="keyDatabseUseCustomCheckBox">
<widget class="QCheckBox" name="keyDatabaseUseCustomCheckBox">
<property name="text">
<string>Use Custom GnuPG Key Database Path</string>
</property>
Expand Down

0 comments on commit 3d3c3d9

Please sign in to comment.