Page MenuHome GnuPG

No OneTemporary

diff --git a/src/kleopatra_options.h b/src/kleopatra_options.h
index aa40c3984..e0762f8c1 100644
--- a/src/kleopatra_options.h
+++ b/src/kleopatra_options.h
@@ -1,53 +1,56 @@
/*
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2015 Bundesamt für Sicherheit in der Informationstechnik
SPDX-FileContributor: Intevation GmbH
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <config-kleopatra.h>
#include <KLocalizedString>
#include <QCommandLineParser>
static void kleopatra_options(QCommandLineParser *parser)
{
+ using namespace Qt::StringLiterals;
+
parser->addOptions({
QCommandLineOption({QStringLiteral("openpgp"), QStringLiteral("p")}, i18nc("@info:shell", "Use OpenPGP for the following operation")),
QCommandLineOption({QStringLiteral("cms"), QStringLiteral("c")}, i18nc("@info:shell", "Use CMS (X.509, S/MIME) for the following operation")),
QCommandLineOption(QStringLiteral("uiserver-socket"),
i18nc("@info:shell", "Location of the socket the ui server is listening on"),
QStringLiteral("argument")),
QCommandLineOption(QStringLiteral("daemon"), i18nc("@info:shell", "Run UI server only, hide main window")),
QCommandLineOption({QStringLiteral("import-certificate"), QStringLiteral("i")}, i18nc("@info:shell", "Import certificate file(s)")),
QCommandLineOption({QStringLiteral("encrypt"), QStringLiteral("e")}, i18nc("@info:shell", "Encrypt file(s)")),
QCommandLineOption({QStringLiteral("sign"), QStringLiteral("s")}, i18nc("@info:shell", "Sign file(s)")),
QCommandLineOption({QStringLiteral("sign-encrypt"), QStringLiteral("E")}, i18nc("@info:shell", "Sign and/or encrypt file(s)")),
QCommandLineOption(QStringLiteral("encrypt-sign"), i18nc("@info:shell", "Same as --sign-encrypt, do not use")),
QCommandLineOption({QStringLiteral("decrypt"), QStringLiteral("d")}, i18nc("@info:shell", "Decrypt file(s)")),
QCommandLineOption({QStringLiteral("verify"), QStringLiteral("V")}, i18nc("@info:shell", "Verify file/signature")),
QCommandLineOption({QStringLiteral("decrypt-verify"), QStringLiteral("D")}, i18nc("@info:shell", "Decrypt and/or verify file(s)")),
QCommandLineOption(QStringLiteral("search"), i18nc("@info:shell", "Search for a certificate on a keyserver")),
QCommandLineOption(QStringLiteral("checksum"), i18nc("@info:shell", "Create or check a checksum file")),
QCommandLineOption({QStringLiteral("query"), QStringLiteral("q")},
i18nc("If a certificate is already known it shows the certificate details dialog. "
"Otherwise it brings up the certificate search dialog.",
"Show details of a local certificate or search for it on a keyserver by fingerprint")),
QCommandLineOption(QStringLiteral("gen-key"), i18nc("@info:shell", "Create a new key pair or certificate signing request")),
QCommandLineOption(QStringLiteral("parent-windowid"), i18nc("@info:shell", "Parent Window Id for dialogs"), QStringLiteral("windowId")),
QCommandLineOption(QStringLiteral("config"), i18nc("@info:shell", "Open the config dialog")),
+ QCommandLineOption(u"standalone"_s, i18nc("@info:shell", "Run as standalone app without system tray icon")),
});
/* Security note: To avoid code execution by shared library injection
* through e.g. -platformpluginpath any external input should be seperated
* by a double dash -- this is why query / search uses positional arguments.
*
* For example on Windows there is an URLhandler for openpgp4fpr:
* be opened with Kleopatra's query function. And while a browser should
* urlescape such a query there might be tricks to inject a quote character
* and as such inject command line options for Kleopatra in an URL. */
parser->addPositionalArgument(QStringLiteral("files"), i18nc("@info:shell", "File(s) to process"), QStringLiteral("-- [files..]"));
parser->addPositionalArgument(QStringLiteral("query"), i18nc("@info:shell", "String or Fingerprint for query and search"), QStringLiteral("-- [query..]"));
}
diff --git a/src/kleopatraapplication.cpp b/src/kleopatraapplication.cpp
index 824883d60..3ca79cf11 100644
--- a/src/kleopatraapplication.cpp
+++ b/src/kleopatraapplication.cpp
@@ -1,945 +1,957 @@
/*
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB
SPDX-FileCopyrightText: 2016 Bundesamt für Sicherheit in der Informationstechnik
SPDX-FileContributor: Intevation GmbH
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <config-kleopatra.h>
#include "kleopatraapplication.h"
#include "kleopatra_options.h"
#include "mainwindow.h"
#include "settings.h"
#include "systrayicon.h"
#include <conf/configuredialog.h>
#include <conf/groupsconfigdialog.h>
#include <dialogs/smartcardwindow.h>
#include <smartcard/readerstatus.h>
#include <Libkleo/GnuPG>
#include <utils/kdpipeiodevice.h>
#include <utils/log.h>
#include <utils/userinfo.h>
#include <gpgme++/key.h>
#include <Libkleo/Classify>
#include <Libkleo/DnAttributes>
#include <Libkleo/FileSystemWatcher>
#include <Libkleo/KeyCache>
#include <Libkleo/KeyFilterManager>
#include <Libkleo/KeyGroupConfig>
#include <Libkleo/SystemInfo>
#include <uiserver/uiserver.h>
#include "commands/checksumcreatefilescommand.h"
#include "commands/checksumverifyfilescommand.h"
#include "commands/decryptverifyfilescommand.h"
#include "commands/detailscommand.h"
#include "commands/importcertificatefromfilecommand.h"
#include "commands/lookupcertificatescommand.h"
#include "commands/newcertificatesigningrequestcommand.h"
#include "commands/newopenpgpcertificatecommand.h"
#include "commands/signencryptfilescommand.h"
#include "dialogs/updatenotification.h"
#ifdef Q_OS_WIN
#include <utils/winapi-helpers.h>
#endif
#include "kleopatra_debug.h"
#include <KAboutApplicationDialog>
#include <KAboutData>
#include <KLocalizedString>
#include <KMessageBox>
#include <KWindowSystem>
#if __has_include(<KWaylandExtras>)
#include <KWaylandExtras>
#define HAVE_WAYLAND
#endif
#include <QDesktopServices>
#include <QDir>
#include <QFile>
#include <QFocusFrame>
#include <QProxyStyle>
#if QT_CONFIG(graphicseffect)
#include <QGraphicsEffect>
#endif
#include <QPointer>
#include <QSettings>
#include <QStyleOption>
#include <QStylePainter>
#include <KSharedConfig>
#ifdef Q_OS_WIN
#include <windows.h>
#endif
using namespace Kleo;
using namespace Kleo::Commands;
using namespace Qt::Literals::StringLiterals;
static void add_resources()
{
const QStringList iconDirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, u"libkleopatra/pics"_s, QStandardPaths::LocateDirectory);
// qCDebug(KLEOPATRA_LOG) << "Adding icon search paths:" << iconDirs;
QIcon::setFallbackSearchPaths(QIcon::fallbackSearchPaths() << iconDirs);
}
static QList<QByteArray> default_logging_options()
{
QList<QByteArray> result;
result.push_back("io");
return result;
}
namespace
{
class FocusFrame : public QFocusFrame
{
Q_OBJECT
public:
using QFocusFrame::QFocusFrame;
protected:
void paintEvent(QPaintEvent *event) override;
};
static QRect effectiveWidgetRect(const QWidget *w)
{
// based on QWidgetPrivate::effectiveRectFor
#if QT_CONFIG(graphicseffect)
if (auto graphicsEffect = w->graphicsEffect(); graphicsEffect && graphicsEffect->isEnabled())
return graphicsEffect->boundingRectFor(w->rect()).toAlignedRect();
#endif // QT_CONFIG(graphicseffect)
return w->rect();
}
static QRect clipRect(const QWidget *w)
{
// based on QWidgetPrivate::clipRect
if (!w->isVisible()) {
return QRect();
}
QRect r = effectiveWidgetRect(w);
int ox = 0;
int oy = 0;
while (w && w->isVisible() && !w->isWindow() && w->parentWidget()) {
ox -= w->x();
oy -= w->y();
w = w->parentWidget();
r &= QRect(ox, oy, w->width(), w->height());
}
return r;
}
void FocusFrame::paintEvent(QPaintEvent *)
{
if (!widget()) {
return;
}
QStylePainter p(this);
QStyleOptionFocusRect option;
initStyleOption(&option);
const int vmargin = style()->pixelMetric(QStyle::PM_FocusFrameVMargin, &option);
const int hmargin = style()->pixelMetric(QStyle::PM_FocusFrameHMargin, &option);
const QRect rect = clipRect(widget()).adjusted(0, 0, hmargin * 2, vmargin * 2);
p.setClipRect(rect);
p.drawPrimitive(QStyle::PE_FrameFocusRect, option);
}
}
class KleopatraApplication::Private
{
friend class ::KleopatraApplication;
KleopatraApplication *const q;
public:
explicit Private(KleopatraApplication *qq)
: q(qq)
, ignoreNewInstance(true)
, firstNewInstance(true)
#ifndef QT_NO_SYSTEMTRAYICON
, sysTray(nullptr)
#endif
{
}
~Private()
{
#ifndef QT_NO_SYSTEMTRAYICON
delete sysTray;
#endif
}
void setUpSysTrayIcon()
{
#ifndef QT_NO_SYSTEMTRAYICON
Q_ASSERT(readerStatus);
sysTray = new SysTrayIcon();
sysTray->setFirstCardWithNullPin(readerStatus->firstCardWithNullPin());
connect(readerStatus.get(), &SmartCard::ReaderStatus::firstCardWithNullPinChanged, sysTray, &SysTrayIcon::setFirstCardWithNullPin);
#endif
}
private:
void connectConfigureDialog()
{
if (configureDialog) {
if (q->mainWindow()) {
connect(configureDialog, SIGNAL(configCommitted()), q->mainWindow(), SLOT(slotConfigCommitted()));
}
connect(configureDialog, &ConfigureDialog::configCommitted, q, &KleopatraApplication::configurationChanged);
}
}
void disconnectConfigureDialog()
{
if (configureDialog) {
if (q->mainWindow()) {
disconnect(configureDialog, SIGNAL(configCommitted()), q->mainWindow(), SLOT(slotConfigCommitted()));
}
disconnect(configureDialog, &ConfigureDialog::configCommitted, q, &KleopatraApplication::configurationChanged);
}
}
public:
+ bool isStandalone = false;
bool ignoreNewInstance;
bool firstNewInstance;
QPointer<FocusFrame> focusFrame;
QPointer<KAboutApplicationDialog> aboutDialog;
QPointer<ConfigureDialog> configureDialog;
QPointer<GroupsConfigDialog> groupsConfigDialog;
QPointer<MainWindow> mainWindow;
QPointer<SmartCardWindow> smartCardWindow;
std::unique_ptr<SmartCard::ReaderStatus> readerStatus;
#ifndef QT_NO_SYSTEMTRAYICON
SysTrayIcon *sysTray;
#endif
std::shared_ptr<KeyGroupConfig> groupConfig;
std::shared_ptr<KeyCache> keyCache;
std::shared_ptr<Log> log;
std::shared_ptr<FileSystemWatcher> watcher;
std::shared_ptr<QSettings> distroSettings;
public:
void setupKeyCache()
{
keyCache = KeyCache::mutableInstance();
keyCache->setRefreshInterval(Settings{}.refreshInterval());
watcher.reset(new FileSystemWatcher);
watcher->whitelistFiles(gnupgFileWhitelist());
watcher->addPaths(gnupgFolderWhitelist());
watcher->setDelay(1000);
keyCache->addFileSystemWatcher(watcher);
keyCache->setGroupConfig(groupConfig);
keyCache->setGroupsEnabled(Settings().groupsEnabled());
// always enable remarks (aka tags); in particular, this triggers a
// relisting of the keys with signatures and signature notations
// after the initial (fast) key listing
keyCache->enableRemarks(true);
}
void setUpFilterManager()
{
if (!Settings{}.cmsEnabled()) {
KeyFilterManager::instance()->alwaysFilterByProtocol(GpgME::OpenPGP);
}
}
void setupLogging()
{
log = Log::mutableInstance();
const QByteArray envOptions = qgetenv("KLEOPATRA_LOGOPTIONS");
const bool logAll = envOptions.trimmed() == "all";
const QList<QByteArray> options = envOptions.isEmpty() ? default_logging_options() : envOptions.split(',');
const QByteArray dirNative = qgetenv("KLEOPATRA_LOGDIR");
if (dirNative.isEmpty()) {
return;
}
const QString dir = QFile::decodeName(dirNative);
const QString logFileName = QDir(dir).absoluteFilePath(QStringLiteral("kleopatra.log.%1").arg(QCoreApplication::applicationPid()));
std::unique_ptr<QFile> logFile(new QFile(logFileName));
if (!logFile->open(QIODevice::WriteOnly | QIODevice::Append)) {
qCDebug(KLEOPATRA_LOG) << "Could not open file for logging: " << logFileName << "\nLogging disabled";
return;
}
log->setOutputDirectory(dir);
if (logAll || options.contains("io")) {
log->setIOLoggingEnabled(true);
}
qInstallMessageHandler(Log::messageHandler);
if (logAll || options.contains("pipeio")) {
KDPipeIODevice::setDebugLevel(KDPipeIODevice::Debug);
}
UiServer::setLogStream(log->logFile());
}
void updateFocusFrame(QWidget *focusWidget)
{
if (focusWidget && focusWidget->inherits("QLabel") && focusWidget->window()->testAttribute(Qt::WA_KeyboardFocusChange)) {
if (!focusFrame) {
focusFrame = new FocusFrame{focusWidget};
}
focusFrame->setWidget(focusWidget);
} else if (focusFrame) {
focusFrame->setWidget(nullptr);
}
}
MainWindow *getOrCreateMainWindow()
{
auto mw = q->mainWindow();
if (!mw) {
mw = new MainWindow;
mw->setAttribute(Qt::WA_DeleteOnClose);
q->setMainWindow(mw);
}
return mw;
}
void exportFocusWindow()
{
#ifdef HAVE_WAYLAND
KWaylandExtras::self()->exportWindow(QGuiApplication::focusWindow());
#endif
}
};
class KleopatraProxyStyle : public QProxyStyle
{
public:
int styleHint(StyleHint hint, const QStyleOption *option = nullptr, const QWidget *widget = nullptr, QStyleHintReturn *returnData = nullptr) const override
{
// disable parent<->child navigation in tree views with left/right arrow keys
// because this interferes with column by column navigation that is required
// for accessibility
if (hint == QStyle::SH_ItemView_ArrowKeysNavigateIntoChildren)
return 0;
return QProxyStyle::styleHint(hint, option, widget, returnData);
}
};
KleopatraApplication::KleopatraApplication(int &argc, char *argv[])
: QApplication(argc, argv)
, d(new Private(this))
{
setStyle(new KleopatraProxyStyle);
connect(this, &QApplication::focusChanged, this, [this](QWidget *, QWidget *now) {
d->updateFocusFrame(now);
});
}
void KleopatraApplication::init()
{
const QString groupConfigPath = Kleo::gnupgHomeDirectory() + QStringLiteral("/kleopatra/kleopatragroupsrc");
d->groupConfig = std::make_shared<KeyGroupConfig>(groupConfigPath);
const auto blockedUrlSchemes = Settings{}.blockedUrlSchemes();
for (const auto &scheme : blockedUrlSchemes) {
QDesktopServices::setUrlHandler(scheme, this, "blockUrl");
}
add_resources();
DNAttributes::setOrder(Settings{}.attributeOrder());
/* Start the gpg-agent early, this is done explicitly
* because on an empty keyring our keylistings wont start
* the agent. In that case any assuan-connect calls to
* the agent will fail. The requested start via the
* connection is additionally done in case the gpg-agent
* is killed while Kleopatra is running. */
startGpgAgent();
d->readerStatus.reset(new SmartCard::ReaderStatus);
connect(d->readerStatus.get(), &SmartCard::ReaderStatus::startOfGpgAgentRequested, this, &KleopatraApplication::startGpgAgent);
d->setupKeyCache();
- d->setUpSysTrayIcon();
+ if (!d->isStandalone) {
+ d->setUpSysTrayIcon();
+ }
d->setUpFilterManager();
d->setupLogging();
#ifndef QT_NO_SYSTEMTRAYICON
if (d->sysTray) {
d->sysTray->show();
}
#endif
#ifdef HAVE_WAYLAND
connect(KWaylandExtras::self(), &KWaylandExtras::windowExported, this, [](const auto, const auto &token) {
qputenv("PINENTRY_GEOM_HINT", QUrl::toPercentEncoding(token));
});
connect(qApp, &QGuiApplication::focusWindowChanged, this, [this](auto w) {
if (!w) {
return;
}
d->exportFocusWindow();
});
QMetaObject::invokeMethod(
this,
[this]() {
d->exportFocusWindow();
},
Qt::QueuedConnection);
#endif
- if (!Kleo::userIsElevated()) {
- // For users running Kleo with elevated permissions on Windows we
- // always quit the application when the last window is closed.
- setQuitOnLastWindowClosed(false);
- }
+ // If Kleopatra is running in standalone mode or if it is running
+ // with elevated permissions on Windows then we quit the application
+ // when the last window is closed.
+ setQuitOnLastWindowClosed(isStandalone() || Kleo::userIsElevated());
// Sync config when we are about to quit
connect(this, &QApplication::aboutToQuit, this, []() {
KSharedConfig::openConfig()->sync();
});
}
KleopatraApplication::~KleopatraApplication()
{
delete d->groupsConfigDialog;
delete d->smartCardWindow;
delete d->mainWindow;
}
+void KleopatraApplication::setIsStandalone(bool standalone)
+{
+ d->isStandalone = standalone;
+}
+
+bool KleopatraApplication::isStandalone() const
+{
+ return d->isStandalone;
+}
+
namespace
{
using Func = void (KleopatraApplication::*)(const QStringList &, GpgME::Protocol);
}
void KleopatraApplication::slotActivateRequested(const QStringList &arguments, const QString &workingDirectory)
{
QCommandLineParser parser;
KAboutData::applicationData().setupCommandLine(&parser);
kleopatra_options(&parser);
QString err;
if (!arguments.isEmpty() && !parser.parse(arguments)) {
err = parser.errorText();
} else if (arguments.isEmpty()) {
// KDBusServices omits the application name if no other
// arguments are provided. In that case the parser prints
// a warning.
parser.parse(QStringList() << QCoreApplication::applicationFilePath());
}
if (err.isEmpty()) {
err = newInstance(parser, workingDirectory);
}
if (!err.isEmpty()) {
KMessageBox::error(nullptr, err.toHtmlEscaped(), i18nc("@title:window", "Failed to execute command"));
Q_EMIT setExitValue(1);
return;
}
Q_EMIT setExitValue(0);
}
QString KleopatraApplication::newInstance(const QCommandLineParser &parser, const QString &workingDirectory)
{
if (d->ignoreNewInstance) {
qCDebug(KLEOPATRA_LOG) << "New instance ignored because of ignoreNewInstance";
return QString();
}
// handle standard Qt options and KAboutData options
if (parser.isSet(u"help"_s)) {
KMessageBox::information(nullptr, parser.helpText());
return QString();
}
if (parser.isSet(u"help-all"_s)) {
// Qt doesn't provide the text for --help-all
KMessageBox::error(nullptr,
xi18nc("@info",
"For technical reasons you can only use this option if <application>%1</application> is not already running.",
qApp->applicationDisplayName()));
return QString();
}
if (parser.isSet(u"version"_s) || parser.isSet(u"author"_s) || parser.isSet(u"license"_s)) {
showAboutDialog();
return QString();
}
QStringList files;
const QDir cwd = QDir(workingDirectory);
bool queryMode = parser.isSet(QStringLiteral("query")) || parser.isSet(QStringLiteral("search"));
// Query and Search treat positional arguments differently, see below.
if (!queryMode) {
const auto positionalArguments = parser.positionalArguments();
for (const QString &file : positionalArguments) {
// We do not check that file exists here. Better handle
// these errors in the UI.
if (QFileInfo(file).isAbsolute()) {
files << QDir::fromNativeSeparators(file);
} else {
files << cwd.absoluteFilePath(file);
}
}
}
GpgME::Protocol protocol = GpgME::UnknownProtocol;
if (parser.isSet(QStringLiteral("openpgp"))) {
qCDebug(KLEOPATRA_LOG) << "found OpenPGP";
protocol = GpgME::OpenPGP;
}
if (parser.isSet(QStringLiteral("cms"))) {
qCDebug(KLEOPATRA_LOG) << "found CMS";
if (protocol == GpgME::OpenPGP) {
return i18n("Ambiguous protocol: --openpgp and --cms");
}
protocol = GpgME::CMS;
}
// Check for Parent Window id
WId parentId = 0;
if (parser.isSet(QStringLiteral("parent-windowid"))) {
#ifdef Q_OS_WIN
// WId is not a portable type as it is a pointer type on Windows.
// casting it from an integer is ok though as the values are guaranteed to
// be compatible in the documentation.
parentId = static_cast<WId>((parser.value(QStringLiteral("parent-windowid")).toUInt()));
#else
parentId = parser.value(QStringLiteral("parent-windowid")).toUInt();
#endif
}
// Handle openpgp4fpr URI scheme
QString needle;
if (queryMode) {
needle = parser.positionalArguments().join(QLatin1Char(' '));
}
if (needle.startsWith(QLatin1StringView("openpgp4fpr:"))) {
needle.remove(0, 12);
}
// Check for --search command.
if (parser.isSet(QStringLiteral("search"))) {
// This is an extra command instead of a combination with the
// similar query to avoid changing the older query commands behavior
// and query's "show details if a certificate exist or search on a
// keyserver" logic is hard to explain and use consistently.
if (needle.isEmpty()) {
return i18n("No search string specified for --search");
}
auto const cmd = new LookupCertificatesCommand(needle, nullptr);
cmd->setParentWId(parentId);
cmd->start();
return QString();
}
// Check for --query command
if (parser.isSet(QStringLiteral("query"))) {
if (needle.isEmpty()) {
return i18n("No fingerprint argument specified for --query");
}
auto cmd = Command::commandForQuery(needle);
cmd->setParentWId(parentId);
cmd->start();
return QString();
}
// Check for --gen-key command
if (parser.isSet(QStringLiteral("gen-key"))) {
if (protocol == GpgME::CMS) {
const Kleo::Settings settings{};
if (settings.cmsEnabled() && settings.cmsCertificateCreationAllowed()) {
auto cmd = new NewCertificateSigningRequestCommand;
cmd->setParentWId(parentId);
cmd->start();
} else {
return i18n("You are not allowed to create S/MIME certificate signing requests.");
}
} else {
auto cmd = new NewOpenPGPCertificateCommand;
cmd->setParentWId(parentId);
cmd->start();
}
return QString();
}
// Check for --config command
if (parser.isSet(QStringLiteral("config"))) {
openConfigDialogWithForeignParent(parentId);
return QString();
}
struct FuncInfo {
QString optionName;
Func func;
};
// While most of these options can be handled by the content autodetection
// below it might be useful to override the autodetection if the input is in
// doubt and you e.g. only want to import .asc files or fail and not decrypt them
// if they are actually encrypted data.
static const std::vector<FuncInfo> funcMap{
{QStringLiteral("import-certificate"), &KleopatraApplication::importCertificatesFromFile},
{QStringLiteral("encrypt"), &KleopatraApplication::encryptFiles},
{QStringLiteral("sign"), &KleopatraApplication::signFiles},
{QStringLiteral("encrypt-sign"), &KleopatraApplication::signEncryptFiles},
{QStringLiteral("sign-encrypt"), &KleopatraApplication::signEncryptFiles},
{QStringLiteral("decrypt"), &KleopatraApplication::decryptFiles},
{QStringLiteral("verify"), &KleopatraApplication::verifyFiles},
{QStringLiteral("decrypt-verify"), &KleopatraApplication::decryptVerifyFiles},
{QStringLiteral("checksum"), &KleopatraApplication::checksumFiles},
};
QString found;
Func foundFunc = nullptr;
for (const auto &[opt, fn] : funcMap) {
if (parser.isSet(opt) && found.isEmpty()) {
found = opt;
foundFunc = fn;
} else if (parser.isSet(opt)) {
return i18n(R"(Ambiguous commands "%1" and "%2")", found, opt);
}
}
QStringList errors;
if (!found.isEmpty()) {
if (files.empty()) {
return i18n("No files specified for \"%1\" command", found);
}
qCDebug(KLEOPATRA_LOG) << "found" << found;
(this->*foundFunc)(files, protocol);
} else {
if (files.empty()) {
if (!(d->firstNewInstance && isSessionRestored())) {
qCDebug(KLEOPATRA_LOG) << "openOrRaiseMainWindow";
openOrRaiseMainWindow();
}
} else {
for (const QString &fileName : std::as_const(files)) {
QFileInfo fi(fileName);
if (!fi.isReadable()) {
errors << i18n("Cannot read \"%1\"", fileName);
}
}
handleFiles(files, parentId);
}
}
d->firstNewInstance = false;
#ifdef Q_OS_WIN
// On Windows we might be started from the
// explorer in any working directory. E.g.
// a double click on a file. To avoid preventing
// the folder from deletion we set the
// working directory to the users homedir.
QDir::setCurrent(QDir::homePath());
#endif
return errors.join(QLatin1Char('\n'));
}
void KleopatraApplication::handleFiles(const QStringList &files, WId parentId)
{
auto mw = d->getOrCreateMainWindow();
const QList<Command *> allCmds = Command::commandsForFiles(files, mw->keyListController());
for (Command *cmd : allCmds) {
if (parentId) {
cmd->setParentWId(parentId);
} else {
cmd->setParentWidget(mw);
}
if (dynamic_cast<ImportCertificateFromFileCommand *>(cmd)) {
openOrRaiseMainWindow();
}
cmd->start();
}
}
const MainWindow *KleopatraApplication::mainWindow() const
{
return d->mainWindow;
}
MainWindow *KleopatraApplication::mainWindow()
{
return d->mainWindow;
}
void KleopatraApplication::setMainWindow(MainWindow *mainWindow)
{
if (mainWindow == d->mainWindow) {
return;
}
d->disconnectConfigureDialog();
d->mainWindow = mainWindow;
#ifndef QT_NO_SYSTEMTRAYICON
if (d->sysTray) {
d->sysTray->setMainWindow(mainWindow);
}
#endif
d->connectConfigureDialog();
}
static void open_or_raise(QWidget *w)
{
#ifdef Q_OS_WIN
if (w->isMinimized()) {
qCDebug(KLEOPATRA_LOG) << __func__ << "unminimizing and raising window";
w->raise();
} else if (w->isVisible()) {
qCDebug(KLEOPATRA_LOG) << __func__ << "raising window";
w->raise();
#else
if (w->isVisible()) {
qCDebug(KLEOPATRA_LOG) << __func__ << "activating window";
KWindowSystem::updateStartupId(w->windowHandle());
KWindowSystem::activateWindow(w->windowHandle());
#endif
} else {
qCDebug(KLEOPATRA_LOG) << __func__ << "showing window";
w->show();
}
}
void KleopatraApplication::toggleMainWindowVisibility()
{
if (mainWindow()) {
mainWindow()->setVisible(!mainWindow()->isVisible());
} else {
openOrRaiseMainWindow();
}
}
void KleopatraApplication::restoreMainWindow()
{
qCDebug(KLEOPATRA_LOG) << "restoring main window";
// Sanity checks
if (!isSessionRestored()) {
qCDebug(KLEOPATRA_LOG) << "Not in session restore";
return;
}
if (mainWindow()) {
qCDebug(KLEOPATRA_LOG) << "Already have main window";
return;
}
auto mw = d->getOrCreateMainWindow();
if (KMainWindow::canBeRestored(1)) {
// restore to hidden state, Mainwindow::readProperties() will
// restore saved visibility.
mw->restore(1, false);
}
}
void KleopatraApplication::openOrRaiseMainWindow()
{
auto mw = d->getOrCreateMainWindow();
open_or_raise(mw);
UpdateNotification::checkUpdate(mw);
}
void KleopatraApplication::openOrRaiseSmartCardWindow()
{
if (!d->smartCardWindow) {
d->smartCardWindow = new SmartCardWindow;
d->smartCardWindow->setAttribute(Qt::WA_DeleteOnClose);
}
open_or_raise(d->smartCardWindow);
}
void KleopatraApplication::openConfigDialogWithForeignParent(WId parentWId)
{
if (!d->configureDialog) {
d->configureDialog = new ConfigureDialog;
d->configureDialog->setAttribute(Qt::WA_DeleteOnClose);
d->connectConfigureDialog();
}
// This is similar to what the commands do.
if (parentWId) {
if (QWidget *pw = QWidget::find(parentWId)) {
d->configureDialog->setParent(pw, d->configureDialog->windowFlags());
} else {
d->configureDialog->setAttribute(Qt::WA_NativeWindow, true);
KWindowSystem::setMainWindow(d->configureDialog->windowHandle(), parentWId);
}
}
open_or_raise(d->configureDialog);
// If we have a parent we want to raise over it.
if (parentWId) {
d->configureDialog->raise();
}
}
void KleopatraApplication::openOrRaiseConfigDialog()
{
openConfigDialogWithForeignParent(0);
}
void KleopatraApplication::openOrRaiseGroupsConfigDialog(QWidget *parent)
{
if (!d->groupsConfigDialog) {
d->groupsConfigDialog = new GroupsConfigDialog{parent};
d->groupsConfigDialog->setAttribute(Qt::WA_DeleteOnClose);
} else {
// reparent the dialog to ensure it's shown on top of the (modal) parent
d->groupsConfigDialog->setParent(parent, Qt::Dialog);
}
open_or_raise(d->groupsConfigDialog);
}
#ifndef QT_NO_SYSTEMTRAYICON
void KleopatraApplication::startMonitoringSmartCard()
{
Q_ASSERT(d->readerStatus);
d->readerStatus->startMonitoring();
}
#endif // QT_NO_SYSTEMTRAYICON
void KleopatraApplication::importCertificatesFromFile(const QStringList &files, GpgME::Protocol /*proto*/)
{
openOrRaiseMainWindow();
if (!files.empty()) {
mainWindow()->importCertificatesFromFile(files);
}
}
void KleopatraApplication::encryptFiles(const QStringList &files, GpgME::Protocol proto)
{
auto const cmd = new SignEncryptFilesCommand(files, nullptr);
cmd->setEncryptionPolicy(Force);
cmd->setSigningPolicy(Allow);
if (proto != GpgME::UnknownProtocol) {
cmd->setProtocol(proto);
}
cmd->start();
}
void KleopatraApplication::signFiles(const QStringList &files, GpgME::Protocol proto)
{
auto const cmd = new SignEncryptFilesCommand(files, nullptr);
cmd->setSigningPolicy(Force);
cmd->setEncryptionPolicy(Deny);
if (proto != GpgME::UnknownProtocol) {
cmd->setProtocol(proto);
}
cmd->start();
}
void KleopatraApplication::signEncryptFiles(const QStringList &files, GpgME::Protocol proto)
{
auto const cmd = new SignEncryptFilesCommand(files, nullptr);
if (proto != GpgME::UnknownProtocol) {
cmd->setProtocol(proto);
}
cmd->start();
}
void KleopatraApplication::decryptFiles(const QStringList &files, GpgME::Protocol /*proto*/)
{
auto const cmd = new DecryptVerifyFilesCommand(files, nullptr);
cmd->setOperation(Decrypt);
cmd->start();
}
void KleopatraApplication::verifyFiles(const QStringList &files, GpgME::Protocol /*proto*/)
{
auto const cmd = new DecryptVerifyFilesCommand(files, nullptr);
cmd->setOperation(Verify);
cmd->start();
}
void KleopatraApplication::decryptVerifyFiles(const QStringList &files, GpgME::Protocol /*proto*/)
{
auto const cmd = new DecryptVerifyFilesCommand(files, nullptr);
cmd->start();
}
void KleopatraApplication::checksumFiles(const QStringList &files, GpgME::Protocol /*proto*/)
{
QStringList verifyFiles, createFiles;
for (const QString &file : files) {
if (isChecksumFile(file)) {
verifyFiles << file;
} else {
createFiles << file;
}
}
if (!verifyFiles.isEmpty()) {
auto const cmd = new ChecksumVerifyFilesCommand(verifyFiles, nullptr);
cmd->start();
}
if (!createFiles.isEmpty()) {
auto const cmd = new ChecksumCreateFilesCommand(createFiles, nullptr);
cmd->start();
}
}
void KleopatraApplication::setIgnoreNewInstance(bool ignore)
{
d->ignoreNewInstance = ignore;
}
bool KleopatraApplication::ignoreNewInstance() const
{
return d->ignoreNewInstance;
}
void KleopatraApplication::blockUrl(const QUrl &url)
{
qCDebug(KLEOPATRA_LOG) << "Blocking URL" << url;
KMessageBox::error(mainWindow(), i18n("Opening an external link is administratively prohibited."), i18nc("@title:window", "Prohibited"));
}
void KleopatraApplication::startGpgAgent()
{
Kleo::launchGpgAgent();
}
void KleopatraApplication::setDistributionSettings(const std::shared_ptr<QSettings> &settings)
{
d->distroSettings = settings;
Q_EMIT distributionSettingsChanged();
}
std::shared_ptr<QSettings> KleopatraApplication::distributionSettings() const
{
return d->distroSettings;
}
void KleopatraApplication::showAboutDialog()
{
// we show the About dialog ourselves so that we can pass up-to-date about data to it;
// KXmlGuiWindow takes a copy of the about data on creation and this copy might not
// contain the backend version information that's set by a background thread
if (!d->aboutDialog) {
qCDebug(KLEOPATRA_LOG) << __func__ << "Creating About dialog";
d->aboutDialog = new KAboutApplicationDialog(KAboutData::applicationData(), mainWindow());
d->aboutDialog->setAttribute(Qt::WA_DeleteOnClose);
}
if (d->aboutDialog->isMinimized()) {
qCDebug(KLEOPATRA_LOG) << __func__ << "Unminimizing About dialog";
d->aboutDialog->setWindowState((d->aboutDialog->windowState() & ~Qt::WindowMinimized) | Qt::WindowActive);
}
qCDebug(KLEOPATRA_LOG) << __func__ << "Showing About dialog";
d->aboutDialog->show();
}
#include "kleopatraapplication.moc"
#include "moc_kleopatraapplication.cpp"
diff --git a/src/kleopatraapplication.h b/src/kleopatraapplication.h
index adee05e05..5d9e2972d 100644
--- a/src/kleopatraapplication.h
+++ b/src/kleopatraapplication.h
@@ -1,118 +1,125 @@
/*
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2008 Klarälvdalens Datakonsult AB
SPDX-FileCopyrightText: 2016 Bundesamt für Sicherheit in der Informationstechnik
SPDX-FileContributor: Intevation GmbH
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <QApplication>
#include <QCommandLineParser>
#include <QElapsedTimer>
#include <gpgme++/global.h>
#include <memory>
extern QElapsedTimer startupTimer;
#define STARTUP_TIMING qCDebug(KLEOPATRA_LOG) << "Startup timing:" << startupTimer.elapsed() << "ms:"
#define STARTUP_TRACE qCDebug(KLEOPATRA_LOG) << "Startup timing:" << startupTimer.elapsed() << "ms:" << SRCNAME << __func__ << __LINE__;
class MainWindow;
class SysTrayIcon;
class QSettings;
class KleopatraApplication : public QApplication
{
Q_OBJECT
public:
/** Create a new Application object. You have to
* make sure to call init afterwards to get a valid object.
* This is to delay initialisation after the UniqueService
* call is done and our init / call might be forwarded to
* another instance. */
KleopatraApplication(int &argc, char *argv[]);
~KleopatraApplication() override;
+ /** By default, Kleopatra is started as unique application. This can be changed
+ * by calling setIsStandalone with \c true. A standalone Kleopatra instance doesn't
+ * have a system tray icon.
+ */
+ void setIsStandalone(bool standalone);
+ bool isStandalone() const;
+
/** Initialize the application. Without calling init any
* other call to KleopatraApplication will result in undefined behavior
* and likely crash. */
void init();
static KleopatraApplication *instance()
{
return qobject_cast<KleopatraApplication *>(qApp);
}
/** Starts a new instance or a command from the command line.
*
* Handles the parser options and starts the according commands.
* If ignoreNewInstance is set this function does nothing.
* The parser should have been initialized with kleopatra_options and
* already processed.
* If kleopatra is not session restored
*
* @param parser: The command line parser to use.
* @param workingDirectory: Optional working directory for file arguments.
*
* @returns an empty QString on success. A localized error message otherwise.
* */
QString newInstance(const QCommandLineParser &parser, const QString &workingDirectory = QString());
void setMainWindow(MainWindow *mw);
const MainWindow *mainWindow() const;
MainWindow *mainWindow();
void setIgnoreNewInstance(bool on);
bool ignoreNewInstance() const;
void toggleMainWindowVisibility();
void restoreMainWindow();
void openConfigDialogWithForeignParent(WId parentWId);
void showAboutDialog();
/* Add optional signed settings for specialized distributions */
void setDistributionSettings(const std::shared_ptr<QSettings> &settings);
std::shared_ptr<QSettings> distributionSettings() const;
public Q_SLOTS:
void openOrRaiseMainWindow();
void openOrRaiseSmartCardWindow();
void openOrRaiseConfigDialog();
void openOrRaiseGroupsConfigDialog(QWidget *parent);
#ifndef QT_NO_SYSTEMTRAYICON
void startMonitoringSmartCard();
#endif
void importCertificatesFromFile(const QStringList &files, GpgME::Protocol proto);
void encryptFiles(const QStringList &files, GpgME::Protocol proto);
void signFiles(const QStringList &files, GpgME::Protocol proto);
void signEncryptFiles(const QStringList &files, GpgME::Protocol proto);
void decryptFiles(const QStringList &files, GpgME::Protocol proto);
void verifyFiles(const QStringList &files, GpgME::Protocol proto);
void decryptVerifyFiles(const QStringList &files, GpgME::Protocol proto);
void checksumFiles(const QStringList &files, GpgME::Protocol /* unused */);
void slotActivateRequested(const QStringList &arguments, const QString &workingDirectory);
void handleFiles(const QStringList &files, WId parentId = 0);
Q_SIGNALS:
/* Emitted from slotActivateRequested to enable setting the
* correct exitValue */
void setExitValue(int value);
void configurationChanged();
void distributionSettingsChanged();
private Q_SLOTS:
// used as URL handler for URLs with schemes that shall be blocked
void blockUrl(const QUrl &url);
void startGpgAgent();
private:
class Private;
const std::unique_ptr<Private> d;
};
diff --git a/src/main.cpp b/src/main.cpp
index 6fb036260..7a7e22aaa 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -1,311 +1,319 @@
/*
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2001, 2002, 2004, 2008 Klarälvdalens Datakonsult AB
SPDX-FileCopyrightText: 2016 Bundesamt für Sicherheit in der Informationstechnik
SPDX-FileContributor: Intevation GmbH
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <config-kleopatra.h>
#include "aboutdata.h"
#include "kleopatraapplication.h"
#include "mainwindow.h"
#include "utils/migration.h"
#include "accessibility/accessiblewidgetfactory.h"
#include <commands/reloadkeyscommand.h>
#include <commands/selftestcommand.h>
#ifdef Q_OS_WIN
#include "conf/kmessageboxdontaskagainstorage.h"
#endif
#include "utils/kuniqueservice.h"
#include "utils/userinfo.h"
#include <Libkleo/GnuPG>
#include <utils/archivedefinition.h>
#include <uiserver/assuancommand.h>
#include <uiserver/createchecksumscommand.h>
#include <uiserver/decryptcommand.h>
#include <uiserver/decryptfilescommand.h>
#include <uiserver/decryptverifyfilescommand.h>
#include <uiserver/echocommand.h>
#include <uiserver/encryptcommand.h>
#include <uiserver/importfilescommand.h>
#include <uiserver/prepencryptcommand.h>
#include <uiserver/prepsigncommand.h>
#include <uiserver/selectcertificatecommand.h>
#include <uiserver/signcommand.h>
#include <uiserver/signencryptfilescommand.h>
#include <uiserver/uiserver.h>
#include <uiserver/verifychecksumscommand.h>
#include <uiserver/verifycommand.h>
#include <uiserver/verifyfilescommand.h>
#include <Libkleo/ChecksumDefinition>
#include "kleopatra_debug.h"
#include "kleopatra_options.h"
#include <KCrash>
#include <KIconTheme>
#include <KLocalizedString>
#include <KMessageBox>
#include <QAccessible>
#include <QElapsedTimer>
#include <QEventLoop>
#include <QMessageBox>
#include <QThreadPool>
#include <QTime>
#include <QTimer>
#include <gpgme++/error.h>
#include <gpgme++/global.h>
#include <QCommandLineParser>
#include <iostream>
#include <memory>
+using namespace Qt::StringLiterals;
+
QElapsedTimer startupTimer;
static bool selfCheck()
{
Kleo::Commands::SelfTestCommand cmd(nullptr);
cmd.setAutoDelete(false);
cmd.setAutomaticMode(true);
QEventLoop loop;
QObject::connect(&cmd, &Kleo::Commands::SelfTestCommand::finished, &loop, &QEventLoop::quit);
QTimer::singleShot(0, &cmd, &Kleo::Command::start); // start() may Q_EMIT finished()...
loop.exec();
if (cmd.isCanceled()) {
return false;
} else {
return true;
}
}
static void fillKeyCache(Kleo::UiServer *server)
{
auto cmd = new Kleo::ReloadKeysCommand(nullptr);
QObject::connect(cmd, SIGNAL(finished()), server, SLOT(enableCryptoCommands()));
cmd->start();
}
int main(int argc, char **argv)
{
startupTimer.start();
// Initialize GpgME
const GpgME::Error gpgmeInitError = GpgME::initializeLibrary(0);
STARTUP_TIMING << "GPGME Initialized";
#ifdef Q_OS_WIN
if (qEnvironmentVariableIsEmpty("GNUPGHOME")) {
if (qputenv("GNUPGHOME", Kleo::gnupgHomeDirectory().toUtf8())) {
qCDebug(KLEOPATRA_LOG) << "Set GNUPGHOME to" << Kleo::gnupgHomeDirectory();
} else {
qCWarning(KLEOPATRA_LOG) << "Failed to set GNUPGHOME to" << Kleo::gnupgHomeDirectory();
}
}
// The config files need to be migrated before the application is created. Otherwise, at least
// the staterc might already have been created at the new location.
Migration::migrateApplicationConfigFiles(QStringLiteral(KLEOPATRA_APPLICATION_NAME));
STARTUP_TIMING << "Config files migrated";
#endif
// Enforce the Breeze icon theme for all icons (including recoloring);
// needs to be done before creating the QApplication
KIconTheme::initTheme();
STARTUP_TIMING << "Icon theme initialized";
KleopatraApplication app(argc, argv);
// Set OrganizationDomain early as this is used to generate the service
// name that will be registered on the bus.
app.setApplicationName(QStringLiteral(KLEOPATRA_APPLICATION_NAME));
app.setOrganizationDomain(QStringLiteral("kde.org"));
STARTUP_TIMING << "Application created";
+
+ // Early parsing of command line to handle --standalone option
+ QCommandLineParser parser;
+ kleopatra_options(&parser);
+ (void)parser.parse(QApplication::arguments()); // ignore errors; they are handled later
+ app.setIsStandalone(parser.isSet(u"standalone"_s));
+
/* Create the unique service ASAP to prevent double starts if
* the application is started twice very quickly. */
- KUniqueService service;
- QObject::connect(&service, &KUniqueService::activateRequested, &app, &KleopatraApplication::slotActivateRequested);
- QObject::connect(&app, &KleopatraApplication::setExitValue, &service, [&service](int i) {
- service.setExitValue(i);
- });
- STARTUP_TIMING << "Service created";
+ if (!app.isStandalone()) {
+ KUniqueService service;
+ QObject::connect(&service, &KUniqueService::activateRequested, &app, &KleopatraApplication::slotActivateRequested);
+ QObject::connect(&app, &KleopatraApplication::setExitValue, &service, [&service](int i) {
+ service.setExitValue(i);
+ });
+ STARTUP_TIMING << "Unique service created";
+ }
QAccessible::installFactory(Kleo::accessibleWidgetFactory);
qCDebug(KLEOPATRA_LOG) << "Application created";
app.setWindowIcon(QIcon::fromTheme(QStringLiteral("kleopatra"), app.windowIcon()));
KLocalizedString::setApplicationDomain(QByteArrayLiteral("kleopatra"));
if (gpgmeInitError) {
// Show a failed initialization of GpgME after creating QApplication and KUniqueService,
// and after setting the application domain for the localization.
KMessageBox::error(nullptr,
xi18nc("@info",
"<para>The version of the <application>GpgME</application> library you are running against "
"is older than the one that the <application>GpgME++</application> library was built against.</para>"
"<para><application>Kleopatra</application> will not function in this setting.</para>"
"<para>Please ask your administrator for help in resolving this issue.</para>"),
i18nc("@title", "GpgME Too Old"));
return EXIT_FAILURE;
}
AboutData aboutData;
KAboutData::setApplicationData(aboutData);
KCrash::initialize();
if (Kleo::userIsElevated()) {
/* This is a safeguard against bugreports that something fails because
* of permission problems on windows. Some users still have the Windows
* Vista behavior of running things as Administrator. This can break
* GnuPG in horrible ways for example if a stale lockfile is left that
* can't be removed without another elevation.
*
* Note: This is not the same as running as root on Linux. Elevated means
* that you are temporarily running with the "normal" user environment but
* with elevated permissions.
* */
if (KMessageBox::warningContinueCancel(nullptr,
xi18nc("@info",
"<para><application>Kleopatra</application> cannot be run as adminstrator without "
"breaking file permissions in the GnuPG data folder.</para>"
"<para>To manage keys for other users please manage them as a normal user and "
"copy the <filename>AppData\\Roaming\\gnupg</filename> directory with proper permissions.</para>")
+ xi18nc("@info", "<para>Are you sure that you want to continue?</para>"),
i18nc("@title", "Running as Administrator"))
!= KMessageBox::Continue) {
return EXIT_FAILURE;
}
qCWarning(KLEOPATRA_LOG) << "User is running with administrative permissions.";
}
// Delay init after KUniqueservice call as this might already
// have terminated us and so we can avoid overhead (e.g. keycache
// setup / systray icon).
Migration::migrate();
app.init();
STARTUP_TIMING << "Application initialized";
- QCommandLineParser parser;
aboutData.setupCommandLine(&parser);
- kleopatra_options(&parser);
-
parser.process(QApplication::arguments());
aboutData.processCommandLine(&parser);
{
const unsigned int threads = QThreadPool::globalInstance()->maxThreadCount();
QThreadPool::globalInstance()->setMaxThreadCount(qMax(2U, threads));
}
Kleo::ChecksumDefinition::setInstallPath(Kleo::gpg4winInstallPath());
Kleo::ArchiveDefinition::setInstallPath(Kleo::gnupgInstallPath());
#ifndef DISABLE_UISERVER
int rc;
Kleo::UiServer *server = nullptr;
try {
server = new Kleo::UiServer(parser.value(QStringLiteral("uiserver-socket")));
STARTUP_TIMING << "UiServer created";
QObject::connect(server, &Kleo::UiServer::startKeyManagerRequested, &app, &KleopatraApplication::openOrRaiseMainWindow);
QObject::connect(server, &Kleo::UiServer::startConfigDialogRequested, &app, &KleopatraApplication::openOrRaiseConfigDialog);
#define REGISTER(Command) server->registerCommandFactory(std::shared_ptr<Kleo::AssuanCommandFactory>(new Kleo::GenericAssuanCommandFactory<Kleo::Command>))
REGISTER(CreateChecksumsCommand);
REGISTER(DecryptCommand);
REGISTER(DecryptFilesCommand);
REGISTER(DecryptVerifyFilesCommand);
REGISTER(EchoCommand);
REGISTER(EncryptCommand);
REGISTER(EncryptFilesCommand);
REGISTER(EncryptSignFilesCommand);
REGISTER(ImportFilesCommand);
REGISTER(PrepEncryptCommand);
REGISTER(PrepSignCommand);
REGISTER(SelectCertificateCommand);
REGISTER(SignCommand);
REGISTER(SignEncryptFilesCommand);
REGISTER(SignFilesCommand);
REGISTER(VerifyChecksumsCommand);
REGISTER(VerifyCommand);
REGISTER(VerifyFilesCommand);
#undef REGISTER
server->start();
STARTUP_TIMING << "UiServer started";
} catch (const std::exception &e) {
qCDebug(KLEOPATRA_LOG) << "Failed to start UI Server: " << e.what();
#ifdef Q_OS_WIN
// We should probably change the UIServer to be only run on Windows at all because
// only the Windows Explorer Plugin uses it. But the plan of GnuPG devs as of 2022 is to
// change the Windows Explorer Plugin to use the command line and then remove the
// UiServer for everyone.
QMessageBox::information(nullptr,
i18n("GPG UI Server Error"),
i18nc("This error message is only shown on Windows when the socket to communicate with "
"Windows Explorer could not be created. This often times means that the whole installation is "
"buggy. e.g. GnuPG is not installed at all.",
"<qt>The Kleopatra Windows Explorer Module could not be initialized.<br/>"
"The error given was: <b>%1</b><br/>"
"This likely means that there is a problem with your installation. Try reinstalling or "
"contact your Administrator for support.<br/>"
"You can try to continue to use Kleopatra but there might be other problems.</qt>",
QString::fromUtf8(e.what()).toHtmlEscaped()));
#endif
}
#endif // DISABLE_UISERVER
const bool daemon = parser.isSet(QStringLiteral("daemon"));
if (!daemon && app.isSessionRestored()) {
app.restoreMainWindow();
}
if (!selfCheck()) {
return EXIT_FAILURE;
}
STARTUP_TIMING << "SelfCheck completed";
if (server) {
fillKeyCache(server);
}
#ifndef QT_NO_SYSTEMTRAYICON
app.startMonitoringSmartCard();
#endif
app.setIgnoreNewInstance(false);
if (!daemon) {
const QString err = app.newInstance(parser);
if (!err.isEmpty()) {
std::cerr << i18n("Invalid arguments: %1", err).toLocal8Bit().constData() << "\n";
return EXIT_FAILURE;
}
STARTUP_TIMING << "new instance created";
}
#ifdef Q_OS_WIN
auto messageBoxConfigStorage = std::make_unique<KMessageBoxDontAskAgainConfigStorage>();
KMessageBox::setDontShowAgainInterface(messageBoxConfigStorage.get());
#endif
rc = app.exec();
app.setIgnoreNewInstance(true);
QObject::disconnect(server, &Kleo::UiServer::startKeyManagerRequested, &app, &KleopatraApplication::openOrRaiseMainWindow);
QObject::disconnect(server, &Kleo::UiServer::startConfigDialogRequested, &app, &KleopatraApplication::openOrRaiseConfigDialog);
if (server) {
server->stop();
server->waitForStopped();
delete server;
}
return rc;
}
diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp
index 76a902388..769502ca4 100644
--- a/src/mainwindow.cpp
+++ b/src/mainwindow.cpp
@@ -1,912 +1,910 @@
/*
This file is part of Kleopatra, the KDE keymanager
SPDX-FileCopyrightText: 2007 Klarälvdalens Datakonsult AB
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "mainwindow.h"
#include "kleopatraapplication.h"
#include "settings.h"
#include <config-kleopatra.h>
#include <interfaces/focusfirstchild.h>
#include "view/keycacheoverlay.h"
#include "view/keylistcontroller.h"
#include "view/searchbar.h"
#include "view/tabwidget.h"
#include "view/welcomewidget.h"
#include "commands/decryptverifyfilescommand.h"
#include "commands/importcertificatefromfilecommand.h"
#include "commands/importcrlcommand.h"
#include "commands/selftestcommand.h"
#include "commands/signencryptfilescommand.h"
#include "utils/action_data.h"
#include "utils/clipboardmenu.h"
#include "utils/detail_p.h"
#include "utils/filedialog.h"
#include "utils/gui-helper.h"
#include "utils/keyexportdraghandler.h"
#include "utils/userinfo.h"
#include <Libkleo/ApplicationPaletteWatcher>
#include <Libkleo/GnuPG>
#include "dialogs/debugdialog.h"
#include "dialogs/padwindow.h"
#include "dialogs/updatenotification.h"
// needed for GPGME_VERSION_NUMBER
#include <gpgme.h>
#include "kleopatra_debug.h"
#include <KAboutData>
#include <KActionCollection>
#include <KActionMenu>
#include <KColorScheme>
#include <KColorSchemeManager>
#include <KColorSchemeMenu>
#include <KConfigDialog>
#include <KConfigGroup>
#include <KEditToolBar>
#include <KHelpMenu>
#include <KLocalizedString>
#include <KMessageBox>
#include <KShortcutsDialog>
#include <KStandardAction>
#include <KStandardGuiItem>
#include <KToolBar>
#include <KXMLGUIFactory>
#include <QAction>
#include <QApplication>
#include <QLineEdit>
#include <QSize>
#include <QAbstractItemView>
#include <QCloseEvent>
#include <QDesktopServices>
#include <QDir>
#include <QLabel>
#include <QMenu>
#include <QMimeData>
#include <QPixmap>
#include <QProcess>
#include <QSettings>
#include <QStackedWidget>
#include <QStatusBar>
#include <QTimer>
#include <QVBoxLayout>
#include <Libkleo/Classify>
#include <Libkleo/Compliance>
#include <Libkleo/DocAction>
#include <Libkleo/Formatting>
#include <Libkleo/GnuPG>
#include <Libkleo/KeyCache>
#include <Libkleo/KeyListModel>
#include <Libkleo/KeyListSortFilterProxyModel>
#include <Libkleo/Stl_Util>
#include <Libkleo/SystemInfo>
#include <KSharedConfig>
#include <chrono>
#include <vector>
using namespace std::chrono_literals;
using namespace Kleo;
using namespace Kleo::Commands;
using namespace GpgME;
using namespace Qt::Literals::StringLiterals;
static KGuiItem KStandardGuiItem_quit()
{
static const QString app = KAboutData::applicationData().displayName();
KGuiItem item = KStandardGuiItem::quit();
item.setText(xi18nc("@action:button", "&Quit <application>%1</application>", app));
return item;
}
static KGuiItem KStandardGuiItem_close()
{
KGuiItem item = KStandardGuiItem::close();
item.setText(i18nc("@action:button", "Only &Close Window"));
return item;
}
static bool isQuitting = false;
namespace
{
class CertificateView : public QWidget, public FocusFirstChild
{
Q_OBJECT
public:
CertificateView(QWidget *parent = nullptr)
: QWidget{parent}
, ui{this}
{
}
SearchBar *searchBar() const
{
return ui.searchBar;
}
TabWidget *tabWidget() const
{
return ui.tabWidget;
}
void focusFirstChild(Qt::FocusReason reason) override
{
ui.searchBar->lineEdit()->setFocus(reason);
}
private:
struct UI {
TabWidget *tabWidget = nullptr;
SearchBar *searchBar = nullptr;
explicit UI(CertificateView *q)
{
auto vbox = new QVBoxLayout{q};
vbox->setSpacing(0);
searchBar = new SearchBar{q};
vbox->addWidget(searchBar);
tabWidget = new TabWidget{KeyTreeView::Option::NoDefaultContextMenu, q};
vbox->addWidget(tabWidget);
tabWidget->connectSearchBar(searchBar);
}
} ui;
};
}
class MainWindow::Private
{
friend class ::MainWindow;
MainWindow *const q;
public:
explicit Private(MainWindow *qq);
~Private();
template<typename T>
void createAndStart()
{
(new T(this->currentView(), &this->controller))->start();
}
template<typename T>
void createAndStart(QAbstractItemView *view)
{
(new T(view, &this->controller))->start();
}
template<typename T>
void createAndStart(const QStringList &a)
{
(new T(a, this->currentView(), &this->controller))->start();
}
template<typename T>
void createAndStart(const QStringList &a, QAbstractItemView *view)
{
(new T(a, view, &this->controller))->start();
}
void closeAndQuit()
{
- if (Kleo::userIsElevated()) {
- // For users running Kleo with elevated permissions on Windows we
- // always quit the application to avoid some problems.
+ if (qApp->quitOnLastWindowClosed()) {
qApp->quit();
}
const QString app = KAboutData::applicationData().displayName();
const int rc = KMessageBox::questionTwoActionsCancel(q,
xi18nc("@info",
"<application>%1</application> may be used by other applications as a service.<nl/>"
"You may instead want to close this window without exiting <application>%1</application>.",
app),
i18nc("@title:window", "Really Quit?"),
KStandardGuiItem_close(),
KStandardGuiItem_quit(),
KStandardGuiItem::cancel(),
QLatin1StringView("really-quit-") + app.toLower());
if (rc == KMessageBox::Cancel) {
return;
}
isQuitting = rc == KMessageBox::ButtonCode::SecondaryAction;
if (!q->close()) {
return;
}
// WARNING: 'this' might be deleted at this point!
if (rc == KMessageBox::ButtonCode::SecondaryAction) {
qApp->quit();
}
}
void configureToolbars()
{
KEditToolBar dlg(q->factory());
dlg.exec();
}
void editKeybindings()
{
KShortcutsDialog::showDialog(q->actionCollection(), KShortcutsEditor::LetterShortcutsAllowed, q);
updateSearchBarClickMessage();
}
void updateSearchBarClickMessage()
{
const QString shortcutStr = focusToClickSearchAction->shortcut().toString(QKeySequence::NativeText);
ui.searchTab->searchBar()->updateClickMessage(shortcutStr);
}
void updateStatusBar()
{
auto statusBar = std::make_unique<QStatusBar>();
auto settings = KleopatraApplication::instance()->distributionSettings();
bool showStatusbar = false;
if (settings) {
const QString statusline = settings->value(QStringLiteral("statusline"), {}).toString();
if (!statusline.isEmpty()) {
auto customStatusLbl = new QLabel(statusline);
statusBar->insertWidget(0, customStatusLbl);
showStatusbar = true;
}
}
if (DeVSCompliance::isActive()) {
auto statusLbl = std::make_unique<QLabel>(DeVSCompliance::name());
{
auto statusPalette = qApp->palette();
KColorScheme::adjustForeground(statusPalette,
DeVSCompliance::isCompliant() ? KColorScheme::NormalText : KColorScheme::NegativeText,
statusLbl->foregroundRole(),
KColorScheme::View);
statusLbl->setAutoFillBackground(true);
KColorScheme::adjustBackground(statusPalette,
DeVSCompliance::isCompliant() ? KColorScheme::PositiveBackground : KColorScheme::NegativeBackground,
QPalette::Window,
KColorScheme::View);
statusLbl->setPalette(statusPalette);
}
statusBar->insertPermanentWidget(0, statusLbl.release());
showStatusbar = true;
}
if (showStatusbar) {
q->setStatusBar(statusBar.release()); // QMainWindow takes ownership
} else {
q->setStatusBar(nullptr);
}
}
void selfTest()
{
createAndStart<SelfTestCommand>();
}
void configureGroups()
{
// open groups config dialog as independent top-level window
KleopatraApplication::instance()->openOrRaiseGroupsConfigDialog(nullptr);
}
void showHandbook();
void gnupgLogViewer()
{
// Warning: Don't assume that the program needs to be in PATH. On Windows, it will also be found next to the calling process.
if (!QProcess::startDetached(QStringLiteral("kwatchgnupg"), QStringList()))
KMessageBox::error(q,
i18n("Could not start the GnuPG Log Viewer (kwatchgnupg). "
"Please check your installation."),
i18n("Error Starting KWatchGnuPG"));
}
void forceUpdateCheck()
{
UpdateNotification::forceUpdateCheck(q);
}
void slotConfigCommitted();
void slotContextMenuRequested(QAbstractItemView *, const QPoint &p)
{
if (auto const menu = qobject_cast<QMenu *>(q->factory()->container(QStringLiteral("listview_popup"), q))) {
menu->exec(p);
} else {
qCDebug(KLEOPATRA_LOG) << "no \"listview_popup\" <Menu> in kleopatra's ui.rc file";
}
}
void slotFocusQuickSearch()
{
ui.searchTab->searchBar()->lineEdit()->setFocus();
}
void showView(QWidget *widget)
{
ui.stackWidget->setCurrentWidget(widget);
if (auto ffci = dynamic_cast<Kleo::FocusFirstChild *>(widget)) {
ffci->focusFirstChild(Qt::TabFocusReason);
}
}
void showPadView()
{
auto padWindow = new PadWindow();
padWindow->setAttribute(Qt::WA_DeleteOnClose);
padWindow->show();
}
void restartDaemons()
{
Kleo::restartGpgAgent();
}
private:
void setupActions();
QAbstractItemView *currentView() const
{
return ui.searchTab->tabWidget()->currentView();
}
void keyListingDone()
{
if (KeyCache::instance()->keys().empty()) {
showView(ui.welcomeWidget);
} else {
showView(ui.searchTab);
}
}
private:
ApplicationPaletteWatcher appPaletteWatcher;
Kleo::KeyListController controller;
bool firstShow : 1;
struct UI {
CertificateView *searchTab = nullptr;
WelcomeWidget *welcomeWidget = nullptr;
QStackedWidget *stackWidget = nullptr;
explicit UI(MainWindow *q);
} ui;
QAction *focusToClickSearchAction = nullptr;
ClipboardMenu *clipboadMenu = nullptr;
};
MainWindow::Private::UI::UI(MainWindow *q)
{
auto mainWidget = new QWidget{q};
auto mainLayout = new QVBoxLayout(mainWidget);
mainLayout->setContentsMargins({});
stackWidget = new QStackedWidget{q};
searchTab = new CertificateView{q};
stackWidget->addWidget(searchTab);
new KeyCacheOverlay(mainWidget, q);
welcomeWidget = new WelcomeWidget{q};
stackWidget->addWidget(welcomeWidget);
mainLayout->addWidget(stackWidget);
q->setCentralWidget(mainWidget);
}
MainWindow::Private::Private(MainWindow *qq)
: q(qq)
, controller(q)
, firstShow(true)
, ui(q)
{
Q_SET_OBJECT_NAME(controller);
AbstractKeyListModel *flatModel = AbstractKeyListModel::createFlatKeyListModel(q);
AbstractKeyListModel *hierarchicalModel = AbstractKeyListModel::createHierarchicalKeyListModel(q);
Q_SET_OBJECT_NAME(flatModel);
Q_SET_OBJECT_NAME(hierarchicalModel);
// check for GpgME >= 1.24.0
#if GPGME_VERSION_NUMBER >= 0x011800 && !defined(Q_OS_WIN)
auto keyExportDragHandler = std::make_shared<KeyExportDragHandler>();
flatModel->setDragHandler(keyExportDragHandler);
hierarchicalModel->setDragHandler(keyExportDragHandler);
#endif
controller.setFlatModel(flatModel);
controller.setHierarchicalModel(hierarchicalModel);
controller.setTabWidget(ui.searchTab->tabWidget());
ui.searchTab->tabWidget()->setFlatModel(flatModel);
ui.searchTab->tabWidget()->setHierarchicalModel(hierarchicalModel);
setupActions();
ui.stackWidget->setCurrentWidget(ui.searchTab);
connect(&controller, SIGNAL(contextMenuRequested(QAbstractItemView *, QPoint)), q, SLOT(slotContextMenuRequested(QAbstractItemView *, QPoint)));
connect(KeyCache::instance().get(), &KeyCache::keyListingDone, q, [this]() {
keyListingDone();
});
q->createGUI(QStringLiteral("kleopatra.rc"));
if (auto helpMenu = q->findChild<KHelpMenu *>()) {
qCDebug(KLEOPATRA_LOG) << "Hook into the help menu to show the About dialog ourselves";
connect(helpMenu, &KHelpMenu::showAboutApplication, KleopatraApplication::instance(), &KleopatraApplication::showAboutDialog);
}
// make toolbar buttons accessible by keyboard
auto toolbar = q->findChild<KToolBar *>();
if (toolbar) {
const auto toolbarButtons = toolbar->findChildren<QToolButton *>();
for (auto b : toolbarButtons) {
b->setFocusPolicy(Qt::TabFocus);
}
// move toolbar and its child widgets before the central widget in the tab order;
// this is necessary to make Shift+Tab work as expected
forceSetTabOrder(q, toolbar);
auto toolbarChildren = toolbar->findChildren<QWidget *>();
std::for_each(std::rbegin(toolbarChildren), std::rend(toolbarChildren), [toolbar](auto w) {
forceSetTabOrder(toolbar, w);
});
}
if (auto action = q->actionCollection()->action(QStringLiteral("help_whats_this"))) {
delete action;
}
q->setAcceptDrops(true);
// set default window size
q->resize(QSize(1024, 500));
q->setAutoSaveSettings();
updateSearchBarClickMessage();
connect(&appPaletteWatcher, &ApplicationPaletteWatcher::paletteChanged, q, [this]() {
updateStatusBar();
});
connect(KleopatraApplication::instance(), &KleopatraApplication::distributionSettingsChanged, q, [this]() {
updateStatusBar();
});
updateStatusBar();
if (KeyCache::instance()->initialized()) {
keyListingDone();
}
// delay setting the models to use the key cache so that the UI (including
// the "Loading certificate cache..." overlay) is shown before the
// blocking key cache initialization happens
QMetaObject::invokeMethod(
q,
[flatModel, hierarchicalModel]() {
flatModel->useKeyCache(true, KeyList::AllKeys);
hierarchicalModel->useKeyCache(true, KeyList::AllKeys);
},
Qt::QueuedConnection);
}
MainWindow::Private::~Private()
{
}
MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags)
: KXmlGuiWindow(parent, flags)
, d(new Private(this))
{
}
MainWindow::~MainWindow()
{
}
void MainWindow::Private::setupActions()
{
KActionCollection *const coll = q->actionCollection();
const std::vector<action_data> action_data = {
// see keylistcontroller.cpp for more actions
// Tools menu
#ifndef Q_OS_WIN
{
"tools_start_kwatchgnupg",
i18n("GnuPG Log Viewer"),
QString(),
"org.kde.kwatchgnupg",
q,
[this](bool) {
gnupgLogViewer();
},
QString(),
},
#endif
{
"tools_debug_view",
i18n("Show GnuPG Configuration"),
QString(),
"",
q,
[this](bool) {
auto dialog = new DebugDialog(q);
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->open();
},
QString(),
},
{
"tools_restart_backend",
i18nc("@action:inmenu", "Restart Background Processes"),
i18nc("@info:tooltip", "Restart the background processes, e.g. after making changes to the configuration."),
"view-refresh",
q,
[this](bool) {
restartDaemons();
},
{},
},
// Help menu
#ifdef Q_OS_WIN
{
"help_check_updates",
i18n("Check for updates"),
QString(),
"gpg4win-compact",
q,
[this](bool) {
forceUpdateCheck();
},
QString(),
},
#endif
// View menu
{
"pad_view",
i18nc("@action show input / output area for encrypting/signing resp. decrypting/verifying text", "Notepad"),
i18n("Show pad for encrypting/decrypting and signing/verifying text"),
"note",
q,
[this](bool) {
showPadView();
},
QString(),
},
{
"manage_smartcard",
i18nc("@action show smartcard management view", "Smartcards"),
i18n("Show smartcard management"),
"auth-sim-locked",
q,
[](bool) {
KleopatraApplication::instance()->openOrRaiseSmartCardWindow();
},
QString(),
},
// Settings menu
{
"settings_self_test",
i18n("Perform Self-Test"),
QString(),
nullptr,
q,
[this](bool) {
selfTest();
},
QString(),
},
{
"configure_groups",
i18n("Configure Groups..."),
QString(),
"group",
q,
[this](bool) {
configureGroups();
},
QString(),
},
// Toolbar
{
"configure_groups_toolbar",
i18nc("@action:intoolbar", "Groups"),
QString(),
"group",
q,
[this](bool) {
configureGroups();
},
QString(),
}};
make_actions_from_data(action_data, coll);
if (!Settings().groupsEnabled()) {
if (auto action = coll->action(QStringLiteral("configure_groups"))) {
delete action;
}
if (auto action = coll->action(QStringLiteral("configure_groups_toolbar"))) {
delete action;
}
}
KStandardAction::close(q, SLOT(close()), coll);
KStandardAction::quit(q, SLOT(closeAndQuit()), coll);
KStandardAction::configureToolbars(q, SLOT(configureToolbars()), coll);
KStandardAction::keyBindings(q, SLOT(editKeybindings()), coll);
KStandardAction::preferences(qApp, SLOT(openOrRaiseConfigDialog()), coll);
auto manager = KColorSchemeManager::instance();
KActionMenu *schemeMenu = KColorSchemeMenu::createMenu(manager, q);
coll->addAction(QStringLiteral("colorscheme_menu"), schemeMenu->menu()->menuAction());
focusToClickSearchAction = new QAction(i18nc("@action", "Set Focus to Quick Search"), q);
coll->addAction(QStringLiteral("focus_to_quickseach"), focusToClickSearchAction);
coll->setDefaultShortcut(focusToClickSearchAction, QKeySequence(Qt::ALT | Qt::Key_Q));
connect(focusToClickSearchAction, SIGNAL(triggered(bool)), q, SLOT(slotFocusQuickSearch()));
clipboadMenu = new ClipboardMenu(q);
clipboadMenu->setMainWindow(q);
clipboadMenu->clipboardMenu()->setIcon(QIcon::fromTheme(QStringLiteral("edit-paste")));
clipboadMenu->clipboardMenu()->setPopupMode(QToolButton::InstantPopup);
coll->addAction(QStringLiteral("clipboard_menu"), clipboadMenu->clipboardMenu());
/* Add additional help actions for documentation */
const auto compendium = new DocAction(QIcon{u":/gpg4win/gpg4win-compact"_s},
i18n("Gpg4win Compendium"),
i18nc("The Gpg4win compendium is only available"
"at this point (24.7.2017) in german and english."
"Please check with Gpg4win before translating this filename.",
"gpg4win-compendium-en.pdf"),
QStringLiteral("../share/gpg4win"),
QUrl(),
coll);
coll->addAction(QStringLiteral("help_doc_compendium"), compendium);
/* Documentation centered around the german approved VS-NfD mode for official
* RESTRICTED communication. This is only available in some distributions with
* the focus on official communications. */
const auto quickguide =
new DocAction(QIcon::fromTheme(QStringLiteral("help-contextual")),
i18n("&Quick Guide Encrypt and Sign"),
i18nc("Only available in German and English. Leave to English for other languages.", "encrypt_and_sign_gnupgvsd_en.pdf"),
QStringLiteral("../share/doc/gnupg-vsd"),
QUrl(),
coll);
coll->addAction(QStringLiteral("help_doc_quickguide"), quickguide);
coll->addAction(QStringLiteral("help_doc_symenc"), createSymmetricGuideAction(coll).release());
const auto groups = new DocAction(QIcon::fromTheme(QStringLiteral("help-contextual")),
i18n("Certificate &Groups"),
i18nc("Only available in German and English. Leave to English for other languages.", "groupfeature_gnupgvsd_en.pdf"),
QStringLiteral("../share/doc/gnupg-vsd"),
QUrl(),
coll);
coll->addAction(QStringLiteral("help_doc_groups"), groups);
#ifdef Q_OS_WIN
const auto gpgol =
new DocAction(QIcon::fromTheme(QStringLiteral("help-contextual")),
i18n("&Mail Encryption in Outlook"),
i18nc("Only available in German and English. Leave to English for other languages. Only shown on Windows.", "gpgol_outlook_addin_en.pdf"),
QStringLiteral("../share/doc/gnupg-vsd"),
QUrl(),
coll);
coll->addAction(QStringLiteral("help_doc_gpgol"), gpgol);
#endif
/* The submenu with advanced topics */
const auto certmngmnt =
new DocAction(QIcon::fromTheme(QStringLiteral("help-contextual")),
i18n("&Certification Management"),
i18nc("Only available in German and English. Leave to English for other languages.", "certification_management_gnupgvsd_en.pdf"),
QStringLiteral("../share/doc/gnupg-vsd"),
QUrl(),
coll);
coll->addAction(QStringLiteral("help_doc_cert_management"), certmngmnt);
const auto smartcard =
new DocAction(QIcon::fromTheme(QStringLiteral("help-contextual")),
i18n("&Smartcard Setup"),
i18nc("Only available in German and English. Leave to English for other languages.", "smartcard_setup_gnupgvsd_en.pdf"),
QStringLiteral("../share/doc/gnupg-vsd"),
QUrl(),
coll);
coll->addAction(QStringLiteral("help_doc_smartcard"), smartcard);
const auto man_gnupg = new DocAction(QIcon::fromTheme(QStringLiteral("help-contextual")),
i18n("GnuPG Command&line"),
QStringLiteral("gnupg_manual_en.pdf"),
QStringLiteral("../share/doc/gnupg-vsd"),
QUrl(QStringLiteral("https://gnupg.org/documentation/manuals/gnupg/")),
coll);
coll->addAction(QStringLiteral("help_doc_gnupg"), man_gnupg);
/* The secops */
const auto approvalmanual =
new DocAction(QIcon::fromTheme(QStringLiteral("dvipdf")),
i18n("Manual for VS-NfD Approval"),
i18nc("Only available in German and English. Keep the English file name for other languages.", "handbuch_zulassung_gnupgvsd_en.pdf"),
QStringLiteral("../share/doc/gnupg-vsd"),
QUrl(),
coll);
coll->addAction(QStringLiteral("help_doc_approval_manual"), approvalmanual);
const auto vsa =
new DocAction(QIcon::fromTheme(QStringLiteral("dvipdf")),
i18n("SecOps for VS-NfD Approval"),
i18nc("Only available in German and English. Keep the English file name for other languages.", "BSI-VSA-10867_ENG_secops.pdf"),
QStringLiteral("../share/doc/gnupg-vsd"),
QUrl(),
coll);
coll->addAction(QStringLiteral("help_doc_vsa"), vsa);
q->setStandardToolBarMenuEnabled(true);
controller.createActions(coll);
ui.searchTab->tabWidget()->createActions(coll);
}
void MainWindow::Private::slotConfigCommitted()
{
controller.updateConfig();
updateStatusBar();
}
bool MainWindow::queryClose()
{
qCDebug(KLEOPATRA_LOG) << __func__;
if (d->controller.hasRunningCommands()) {
if (d->controller.shutdownWarningRequired()) {
const int ret = KMessageBox::warningContinueCancel(this,
i18n("There are still some background operations ongoing. "
"These will be terminated when closing the window. "
"Proceed?"),
i18n("Ongoing Background Tasks"));
if (ret != KMessageBox::Continue) {
return false;
}
}
d->controller.cancelCommands();
if (d->controller.hasRunningCommands()) {
// wait for them to be finished:
setEnabled(false);
QEventLoop ev;
QTimer::singleShot(100ms, &ev, &QEventLoop::quit);
connect(&d->controller, &KeyListController::commandsExecuting, &ev, &QEventLoop::quit);
ev.exec();
if (d->controller.hasRunningCommands()) {
qCWarning(KLEOPATRA_LOG) << "controller still has commands running, this may crash now...";
}
setEnabled(true);
}
}
- if (isQuitting || qApp->isSavingSession() || Kleo::userIsElevated()) {
+ if (isQuitting || qApp->isSavingSession() || qApp->quitOnLastWindowClosed()) {
d->ui.searchTab->tabWidget()->saveViews();
return true;
} else {
hide();
return false;
}
}
void MainWindow::showEvent(QShowEvent *e)
{
KXmlGuiWindow::showEvent(e);
if (d->firstShow) {
d->ui.searchTab->tabWidget()->loadViews(KSharedConfig::openStateConfig(), QStringLiteral("KeyList"));
d->firstShow = false;
}
if (!savedGeometry.isEmpty()) {
restoreGeometry(savedGeometry);
}
}
void MainWindow::hideEvent(QHideEvent *e)
{
savedGeometry = saveGeometry();
KXmlGuiWindow::hideEvent(e);
}
void MainWindow::importCertificatesFromFile(const QStringList &files)
{
if (!files.empty()) {
d->createAndStart<ImportCertificateFromFileCommand>(files);
}
}
static QStringList extract_local_files(const QMimeData *data)
{
const QList<QUrl> urls = data->urls();
// begin workaround KDE/Qt misinterpretation of text/uri-list
QList<QUrl>::const_iterator end = urls.end();
if (urls.size() > 1 && !urls.back().isValid()) {
--end;
}
// end workaround
QStringList result;
std::transform(urls.begin(), end, std::back_inserter(result), std::mem_fn(&QUrl::toLocalFile));
result.erase(std::remove_if(result.begin(), result.end(), std::mem_fn(&QString::isEmpty)), result.end());
return result;
}
static bool can_decode_local_files(const QMimeData *data)
{
if (!data) {
return false;
}
return !extract_local_files(data).empty();
}
void MainWindow::dragEnterEvent(QDragEnterEvent *e)
{
qCDebug(KLEOPATRA_LOG);
if (can_decode_local_files(e->mimeData())) {
e->acceptProposedAction();
}
}
void MainWindow::dropEvent(QDropEvent *e)
{
qCDebug(KLEOPATRA_LOG);
if (e->source()) {
// The event comes from kleopatra itself; we don't want to import in this case.
return;
}
if (!can_decode_local_files(e->mimeData())) {
return;
}
e->setDropAction(Qt::CopyAction);
const QStringList files = extract_local_files(e->mimeData());
KleopatraApplication::instance()->handleFiles(files);
e->accept();
}
void MainWindow::readProperties(const KConfigGroup &cg)
{
qCDebug(KLEOPATRA_LOG);
KXmlGuiWindow::readProperties(cg);
setHidden(cg.readEntry("hidden", false));
}
void MainWindow::saveProperties(KConfigGroup &cg)
{
qCDebug(KLEOPATRA_LOG);
KXmlGuiWindow::saveProperties(cg);
cg.writeEntry("hidden", isHidden());
}
KeyListController *MainWindow::keyListController()
{
return &d->controller;
}
std::unique_ptr<DocAction> MainWindow::createSymmetricGuideAction(QObject *parent)
{
return std::make_unique<DocAction>(
QIcon::fromTheme(QStringLiteral("help-contextual")),
i18n("&Password-based Encryption"),
i18nc("Only available in German and English. Leave to English for other languages.", "symmetric_encryption_gnupgvsd_en.pdf"),
QStringLiteral("../share/doc/gnupg-vsd"),
QUrl(),
parent);
}
#include "mainwindow.moc"
#include "moc_mainwindow.cpp"

File Metadata

Mime Type
text/x-diff
Expires
Thu, Jul 17, 12:53 AM (1 d, 6 h)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
0d/a5/d40b35e01610381b210d40142a0e

Event Timeline