File indexing completed on 2024-05-19 03:59:01

0001 /*
0002     SPDX-FileCopyrightText: 2021 Oleg Solovyov <mcpain@altlinux.org>
0003     SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #include "action.h"
0009 
0010 #include "dialog.h"
0011 #include <KAuthorized>
0012 #include <KLocalizedString>
0013 
0014 namespace KNSWidgets
0015 {
0016 class ActionPrivate
0017 {
0018 public:
0019     QString configFile;
0020     std::unique_ptr<Dialog> dialog;
0021 };
0022 
0023 Action::Action(const QString &text, const QString &configFile, QObject *parent)
0024     : QAction(parent)
0025     , d(new ActionPrivate)
0026 {
0027     if (text.isEmpty()) {
0028         setText(i18n("Download New Stuff..."));
0029     } else {
0030         setText(text);
0031     }
0032     d->configFile = configFile;
0033 
0034     const bool authorized = KAuthorized::authorize(KAuthorized::GHNS);
0035     if (!authorized) {
0036         setEnabled(false);
0037         setVisible(false);
0038     }
0039 
0040     setIcon(QIcon::fromTheme(QStringLiteral("get-hot-new-stuff")));
0041     connect(this, &QAction::triggered, this, [this]() {
0042         if (!KAuthorized::authorize(KAuthorized::GHNS)) {
0043             return;
0044         }
0045 
0046         if (!d->dialog) {
0047             d->dialog.reset(new KNSWidgets::Dialog(d->configFile));
0048             d->dialog->setWindowTitle(this->text().remove(QLatin1Char('&')));
0049             connect(d->dialog.get(), &KNSWidgets::Dialog::finished, this, [this]() {
0050                 Q_EMIT dialogFinished(d->dialog->changedEntries());
0051             });
0052         }
0053         d->dialog->open();
0054     });
0055 }
0056 
0057 Action::~Action() = default;
0058 }
0059 
0060 #include "moc_action.cpp"