| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2004 Aaron J. Seigo <aseigo@kde.org> |
| 3 | SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.1-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "button.h" |
| 9 | |
| 10 | #include "dialog.h" |
| 11 | #include <KAuthorized> |
| 12 | #include <KLocalizedString> |
| 13 | #include <KMessageBox> |
| 14 | |
| 15 | #include <QPointer> |
| 16 | |
| 17 | namespace KNSWidgets |
| 18 | { |
| 19 | class ButtonPrivate |
| 20 | { |
| 21 | public: |
| 22 | explicit ButtonPrivate(Button *qq) |
| 23 | : q(qq) |
| 24 | { |
| 25 | } |
| 26 | |
| 27 | void showDialog() |
| 28 | { |
| 29 | if (!KAuthorized::authorize(action: KAuthorized::GHNS)) { |
| 30 | KMessageBox::information(parent: q, QStringLiteral("Get Hot New Stuff is disabled by the administrator" ), QStringLiteral("Get Hot New Stuff disabled" )); |
| 31 | return; |
| 32 | } |
| 33 | Q_ASSERT_X(!configFile.isEmpty(), Q_FUNC_INFO, "The configFile for the KNSWidgets::Button must be explicitly set" ); |
| 34 | |
| 35 | if (!dialog) { |
| 36 | dialog.reset(p: new KNSWidgets::Dialog(configFile, q)); |
| 37 | dialog->setWindowTitle(q->text().remove(c: QLatin1Char('&'))); |
| 38 | QObject::connect(sender: dialog.get(), signal: &KNSWidgets::Dialog::finished, context: q, slot: [this]() { |
| 39 | Q_EMIT q->dialogFinished(changedEntries: dialog->changedEntries()); |
| 40 | }); |
| 41 | } |
| 42 | dialog->open(); |
| 43 | } |
| 44 | |
| 45 | Button *q; |
| 46 | QString configFile; |
| 47 | std::unique_ptr<KNSWidgets::Dialog> dialog; |
| 48 | }; |
| 49 | |
| 50 | Button::Button(const QString &text, const QString &configFile, QWidget *parent) |
| 51 | : QPushButton(parent) |
| 52 | , d(new ButtonPrivate(this)) |
| 53 | { |
| 54 | setText(text); |
| 55 | d->configFile = configFile; |
| 56 | |
| 57 | const bool authorized = KAuthorized::authorize(action: KAuthorized::GHNS); |
| 58 | if (!authorized) { |
| 59 | setEnabled(false); |
| 60 | setVisible(false); |
| 61 | } |
| 62 | |
| 63 | setIcon(QIcon::fromTheme(QStringLiteral("get-hot-new-stuff" ))); |
| 64 | connect(sender: this, signal: &QAbstractButton::clicked, context: this, slot: [this]() { |
| 65 | d->showDialog(); |
| 66 | }); |
| 67 | } |
| 68 | |
| 69 | Button::Button(QWidget *parent) |
| 70 | : Button(i18nc("@action" , "Download New Stuff…" ), QString(), parent) |
| 71 | { |
| 72 | } |
| 73 | |
| 74 | Button::~Button() = default; |
| 75 | |
| 76 | void Button::setConfigFile(const QString &configFile) |
| 77 | { |
| 78 | Q_ASSERT_X(!d->dialog, Q_FUNC_INFO, "the configFile property must be set before the dialog is first shown" ); |
| 79 | d->configFile = configFile; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | #include "moc_button.cpp" |
| 84 | |