Libkleo

docaction.cpp
1/*
2 kleo/docaction.cpp
3
4 This file is part of libkleopatra, the KDE keymanagement library
5 SPDX-FileCopyrightText: 2022 g10 Code GmbH
6 SPDX-FileContributor: Andre Heinecke <aheinecke@g10code.com>
7
8 SPDX-License-Identifier: GPL-2.0-or-later
9*/
10
11#include <config-libkleo.h>
12
13#include "docaction.h"
14
15#include <libkleo_debug.h>
16
17#include <QCoreApplication>
18#include <QDesktopServices>
19#include <QDir>
20#include <QFileInfo>
21#include <QString>
22
23using namespace Kleo;
24
25class Kleo::DocAction::Private
26{
27public:
28 explicit Private(const QString &filename, const QUrl &url, const QString &pathHint);
29 ~Private() = default;
30
31 QString path;
32 bool isEnabled = false;
33 QUrl url;
34};
35
36DocAction::Private::Private(const QString &filename, const QUrl &url, const QString &pathHint)
37{
38 QString tmp = pathHint;
39 if (!tmp.startsWith(QLatin1Char('/'))) {
40 tmp.prepend(QLatin1Char('/'));
41 }
42 QDir datadir(QCoreApplication::applicationDirPath() + (pathHint.isNull() ? QStringLiteral("/../share/kleopatra") : tmp));
43
44 path = datadir.filePath(filename);
45 QFileInfo fi(path);
46 isEnabled = fi.exists();
47 if (!isEnabled) {
48 this->url = url;
49 isEnabled = url.isValid();
50 }
51}
52
53DocAction::DocAction(const QIcon &icon, const QString &text, const QString &filename, const QString &pathHint, const QUrl &url, QObject *parent)
55 , d(new Private(filename, url, pathHint))
56{
57 setVisible(d->isEnabled);
58 setEnabled(d->isEnabled);
59 connect(this, &QAction::triggered, this, [this]() {
60 if (d->isEnabled) {
61 qCDebug(LIBKLEO_LOG) << "Opening:" << (d->url.isValid() ? d->url.toString() : d->path);
62 QDesktopServices::openUrl(d->url.isValid() ? d->url : QUrl::fromLocalFile(d->path));
63 }
64 });
65}
66
67DocAction::~DocAction() = default;
68
69#include "moc_docaction.cpp"
void triggered(bool checked)
QString applicationDirPath()
bool openUrl(const QUrl &url)
QObject * parent() const const
bool isNull() const const
QString & prepend(QChar ch)
bool startsWith(QChar c, Qt::CaseSensitivity cs) const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QUrl fromLocalFile(const QString &localFile)
bool isValid() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:09:14 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.