File indexing completed on 2024-11-24 04:49:36

0001 /*
0002     kleo/docaction.cpp
0003 
0004     This file is part of libkleopatra, the KDE keymanagement library
0005     SPDX-FileCopyrightText: 2022 g10 Code GmbH
0006     SPDX-FileContributor: Andre Heinecke <aheinecke@g10code.com>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include <config-libkleo.h>
0012 
0013 #include "docaction.h"
0014 
0015 #include <libkleo_debug.h>
0016 
0017 #include <QCoreApplication>
0018 #include <QDesktopServices>
0019 #include <QDir>
0020 #include <QFileInfo>
0021 #include <QString>
0022 #include <QUrl>
0023 
0024 using namespace Kleo;
0025 
0026 class Kleo::DocAction::Private
0027 {
0028 public:
0029     explicit Private(const QString &filename, const QString &pathHint);
0030     ~Private() = default;
0031 
0032     QString path;
0033     bool isEnabled = false;
0034 };
0035 
0036 DocAction::Private::Private(const QString &filename, const QString &pathHint)
0037 {
0038     QString tmp = pathHint;
0039     if (!tmp.startsWith(QLatin1Char('/'))) {
0040         tmp.prepend(QLatin1Char('/'));
0041     }
0042     QDir datadir(QCoreApplication::applicationDirPath() + (pathHint.isNull() ? QStringLiteral("/../share/kleopatra") : tmp));
0043 
0044     path = datadir.filePath(filename);
0045     QFileInfo fi(path);
0046     isEnabled = fi.exists();
0047 }
0048 
0049 DocAction::DocAction(const QIcon &icon, const QString &text, const QString &filename, const QString &pathHint, QObject *parent)
0050     : QAction(icon, text, parent)
0051     , d(new Private(filename, pathHint))
0052 {
0053     setVisible(d->isEnabled);
0054     setEnabled(d->isEnabled);
0055     connect(this, &QAction::triggered, this, [this]() {
0056         if (d->isEnabled) {
0057             qCDebug(LIBKLEO_LOG) << "Opening file:" << d->path;
0058             QDesktopServices::openUrl(QUrl::fromLocalFile(d->path));
0059         }
0060     });
0061 }
0062 
0063 DocAction::~DocAction() = default;
0064 
0065 #include "moc_docaction.cpp"