Commit 9994a362 authored by Ingo Klöcker's avatar Ingo Klöcker
Browse files

Add/use common helper for persisting the last used export directory

GnuPG-bug-id: 6121
parent e8c91dbd
......@@ -413,6 +413,8 @@ set(_kleopatra_SRCS
utils/accessibility.h
utils/action_data.cpp
utils/action_data.h
utils/applicationstate.cpp
utils/applicationstate.h
utils/archivedefinition.cpp
utils/archivedefinition.h
utils/auditlog.cpp
......
......@@ -15,6 +15,7 @@
#include "command_p.h"
#include <utils/applicationstate.h>
#include <utils/filedialog.h>
#include <Libkleo/Classify>
......@@ -178,8 +179,7 @@ bool ExportCertificateCommand::Private::requestFileNames(GpgME::Protocol protoco
return true;
}
KConfigGroup config(KSharedConfig::openConfig(), "ExportDialog");
const auto lastDir = config.readEntry("LastDirectory", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
const auto lastDir = ApplicationState::lastUsedExportDirectory();
QString proposedFileName = lastDir + QLatin1Char('/');
if (keys().size() == 1) {
......@@ -237,7 +237,7 @@ bool ExportCertificateCommand::Private::requestFileNames(GpgME::Protocol protoco
}
fileNames[protocol] = fname;
config.writeEntry("LastDirectory", fi.absolutePath());
ApplicationState::setLastUsedExportDirectory(fi.absolutePath());
return !fname.isEmpty();
}
......
......@@ -13,6 +13,7 @@
#include "exportgroupscommand.h"
#include "command_p.h"
#include <utils/applicationstate.h>
#include "utils/filedialog.h"
#include <Libkleo/Algorithm>
......@@ -42,23 +43,11 @@ namespace
static const QString certificateGroupFileExtension{QLatin1String{".kgrp"}};
QString getLastUsedExportDirectory()
{
KConfigGroup config{KSharedConfig::openConfig(), "ExportDialog"};
return config.readEntry("LastDirectory", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
}
void updateLastUsedExportDirectory(const QString &path)
{
KConfigGroup config{KSharedConfig::openConfig(), "ExportDialog"};
config.writeEntry("LastDirectory", QFileInfo{path}.absolutePath());
}
QString proposeFilename(const std::vector<KeyGroup> &groups)
{
QString filename;
filename = getLastUsedExportDirectory() + QLatin1Char{'/'};
filename = ApplicationState::lastUsedExportDirectory() + QLatin1Char{'/'};
if (groups.size() == 1) {
filename += groups.front().name().replace(QLatin1Char{'/'}, QLatin1Char{'_'});
} else {
......@@ -83,7 +72,7 @@ QString requestFilename(QWidget *parent, const std::vector<KeyGroup> &groups)
if (fi.suffix().isEmpty()) {
filename += certificateGroupFileExtension;
}
updateLastUsedExportDirectory(filename);
ApplicationState::setLastUsedExportDirectory(filename);
}
return filename;
......
......@@ -14,6 +14,7 @@
#include "command_p.h"
#include "fileoperationspreferences.h"
#include <utils/applicationstate.h>
#include "utils/filedialog.h"
#include <Libkleo/Classify>
......@@ -42,18 +43,6 @@ using namespace GpgME;
namespace
{
QString getLastUsedExportDirectory()
{
KConfigGroup config{KSharedConfig::openConfig(), "ExportDialog"};
return config.readEntry("LastDirectory", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
}
void updateLastUsedExportDirectory(const QString &path)
{
KConfigGroup config{KSharedConfig::openConfig(), "ExportDialog"};
config.writeEntry("LastDirectory", QFileInfo{path}.absolutePath());
}
QString openPGPCertificateFileExtension()
{
return QLatin1String{outputFileExtension(Class::OpenPGP | Class::Ascii | Class::Certificate,
......@@ -92,7 +81,7 @@ QString proposeFilename(const Key &key)
filename = QStringView{u"%1_%2_SECRET"}.arg(name, shortKeyID);
filename.replace(u'/', u'_');
return getLastUsedExportDirectory() + u'/' + filename + u'.' + certificateFileExtension(key.protocol());
return ApplicationState::lastUsedExportDirectory() + u'/' + filename + u'.' + certificateFileExtension(key.protocol());
}
QString secretKeyFileFilters(GpgME::Protocol protocol)
......@@ -122,7 +111,7 @@ QString requestFilename(const Key &key, const QString &proposedFilename, QWidget
if (fi.suffix().isEmpty()) {
filename += u'.' + certificateFileExtension(key.protocol());
}
updateLastUsedExportDirectory(filename);
ApplicationState::setLastUsedExportDirectory(filename);
}
return filename;
......
......@@ -14,6 +14,7 @@
#include "command_p.h"
#include "fileoperationspreferences.h"
#include <utils/applicationstate.h>
#include "utils/filedialog.h"
#include <Libkleo/Classify>
......@@ -40,18 +41,6 @@ namespace
{
#ifdef QGPGME_SUPPORTS_SECRET_SUBKEY_EXPORT
QString getLastUsedExportDirectory()
{
KConfigGroup config{KSharedConfig::openConfig(), "ExportDialog"};
return config.readEntry("LastDirectory", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
}
void updateLastUsedExportDirectory(const QString &path)
{
KConfigGroup config{KSharedConfig::openConfig(), "ExportDialog"};
config.writeEntry("LastDirectory", QFileInfo{path}.absolutePath());
}
QString openPGPCertificateFileExtension()
{
return QLatin1String{outputFileExtension(Class::OpenPGP | Class::Ascii | Class::Certificate,
......@@ -80,7 +69,7 @@ QString proposeFilename(const std::vector<Subkey> &subkeys)
}
filename.replace(u'/', u'_');
return getLastUsedExportDirectory() + u'/' + filename + u'.' + openPGPCertificateFileExtension();
return ApplicationState::lastUsedExportDirectory() + u'/' + filename + u'.' + openPGPCertificateFileExtension();
}
QString requestFilename(const std::vector<Subkey> &subkeys, const QString &proposedFilename, QWidget *parent)
......@@ -97,7 +86,7 @@ QString requestFilename(const std::vector<Subkey> &subkeys, const QString &propo
if (fi.suffix().isEmpty()) {
filename += u'.' + openPGPCertificateFileExtension();
}
updateLastUsedExportDirectory(filename);
ApplicationState::setLastUsedExportDirectory(filename);
}
return filename;
......
/* utils/applicationstate.cpp
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2022 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <config-kleopatra.h>
#include "applicationstate.h"
#include <KConfigGroup>
#include <KSharedConfig>
#include <QFileInfo>
#include <QStandardPaths>
QString ApplicationState::lastUsedExportDirectory()
{
const KConfigGroup config{KSharedConfig::openConfig(), "ExportDialog"};
return config.readEntry("LastDirectory", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation));
}
void ApplicationState::setLastUsedExportDirectory(const QString &path)
{
KConfigGroup config{KSharedConfig::openConfig(), "ExportDialog"};
config.writeEntry("LastDirectory", QFileInfo{path}.absolutePath());
}
/* utils/applicationstate.h
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2022 g10 Code GmbH
SPDX-FileContributor: Ingo Klöcker <dev@ingo-kloecker.de>
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
class QString;
namespace ApplicationState
{
QString lastUsedExportDirectory();
void setLastUsedExportDirectory(const QString &path);
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment