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

0001 /*
0002     SPDX-FileCopyrightText: 2004 Aaron J. Seigo <aseigo@kde.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 "button.h"
0009 
0010 #include "dialog.h"
0011 #include <KAuthorized>
0012 #include <KLocalizedString>
0013 #include <KMessageBox>
0014 
0015 #include <QPointer>
0016 
0017 namespace KNSWidgets
0018 {
0019 class ButtonPrivate
0020 {
0021 public:
0022     explicit ButtonPrivate(Button *qq)
0023         : q(qq)
0024     {
0025     }
0026 
0027     void showDialog()
0028     {
0029         if (!KAuthorized::authorize(KAuthorized::GHNS)) {
0030             KMessageBox::information(q, QStringLiteral("Get Hot New Stuff is disabled by the administrator"), QStringLiteral("Get Hot New Stuff disabled"));
0031             return;
0032         }
0033         Q_ASSERT_X(!configFile.isEmpty(), Q_FUNC_INFO, "The configFile for the KNSWidgets::Button must be explicitly set");
0034 
0035         if (!dialog) {
0036             dialog.reset(new KNSWidgets::Dialog(configFile, q));
0037             dialog->setWindowTitle(q->text().remove(QLatin1Char('&')));
0038             QObject::connect(dialog.get(), &KNSWidgets::Dialog::finished, q, [this]() {
0039                 Q_EMIT q->dialogFinished(dialog->changedEntries());
0040             });
0041         }
0042         dialog->open();
0043     }
0044 
0045     Button *q;
0046     QString configFile;
0047     std::unique_ptr<KNSWidgets::Dialog> dialog;
0048 };
0049 
0050 Button::Button(const QString &text, const QString &configFile, QWidget *parent)
0051     : QPushButton(parent)
0052     , d(new ButtonPrivate(this))
0053 {
0054     setText(text);
0055     d->configFile = configFile;
0056 
0057     const bool authorized = KAuthorized::authorize(KAuthorized::GHNS);
0058     if (!authorized) {
0059         setEnabled(false);
0060         setVisible(false);
0061     }
0062 
0063     setIcon(QIcon::fromTheme(QStringLiteral("get-hot-new-stuff")));
0064     connect(this, &QAbstractButton::clicked, this, [this]() {
0065         d->showDialog();
0066     });
0067 }
0068 
0069 Button::Button(QWidget *parent)
0070     : Button(i18n("Download New Stuff..."), QString(), parent)
0071 {
0072 }
0073 
0074 Button::~Button() = default;
0075 
0076 void Button::setConfigFile(const QString &configFile)
0077 {
0078     Q_ASSERT_X(!d->dialog, Q_FUNC_INFO, "the configFile property must be set before the dialog is first shown");
0079     d->configFile = configFile;
0080 }
0081 }
0082 
0083 #include "moc_button.cpp"