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#include <QUrl>
23
24using namespace Kleo;
25
26class Kleo::DocAction::Private
27{
28public:
29 explicit Private(const QString &filename, const QString &pathHint);
30 ~Private() = default;
31
32 QString path;
33 bool isEnabled = false;
34};
35
36DocAction::Private::Private(const QString &filename, 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}
48
49DocAction::DocAction(const QIcon &icon, const QString &text, const QString &filename, const QString &pathHint, QObject *parent)
51 , d(new Private(filename, pathHint))
52{
53 setVisible(d->isEnabled);
54 setEnabled(d->isEnabled);
55 connect(this, &QAction::triggered, this, [this]() {
56 if (d->isEnabled) {
57 qCDebug(LIBKLEO_LOG) << "Opening file:" << d->path;
58 QDesktopServices::openUrl(QUrl::fromLocalFile(d->path));
59 }
60 });
61}
62
63DocAction::~DocAction() = default;
64
65#include "moc_docaction.cpp"
void triggered(bool checked)
QString applicationDirPath()
QObject * parent() const const
bool isNull() const const
QString & prepend(QChar ch)
bool startsWith(const QString &s, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:39:42 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.