KNewStuff

button.cpp
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
17namespace KNSWidgets
18{
19class ButtonPrivate
20{
21public:
22 explicit ButtonPrivate(Button *qq)
23 : q(qq)
24 {
25 }
26
27 void showDialog()
28 {
29 if (!KAuthorized::authorize(KAuthorized::GHNS)) {
30 KMessageBox::information(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(new KNSWidgets::Dialog(configFile, q));
37 dialog->setWindowTitle(q->text().remove(QLatin1Char('&')));
38 QObject::connect(dialog.get(), &KNSWidgets::Dialog::finished, q, [this]() {
39 Q_EMIT q->dialogFinished(dialog->changedEntries());
40 });
41 }
42 dialog->open();
43 }
44
45 Button *q;
46 QString configFile;
47 std::unique_ptr<KNSWidgets::Dialog> dialog;
48};
49
50Button::Button(const QString &text, const QString &configFile, QWidget *parent)
51 : QPushButton(parent)
52 , d(new ButtonPrivate(this))
53{
55 d->configFile = configFile;
56
57 const bool authorized = KAuthorized::authorize(KAuthorized::GHNS);
58 if (!authorized) {
59 setEnabled(false);
60 setVisible(false);
61 }
62
63 setIcon(QIcon::fromTheme(QStringLiteral("get-hot-new-stuff")));
64 connect(this, &QAbstractButton::clicked, this, [this]() {
65 d->showDialog();
66 });
67}
68
70 : Button(i18nc("@action", "Download New Stuff…"), QString(), parent)
71{
72}
73
74Button::~Button() = default;
75
76void 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"
A button which when clicked will open a dialog with a NewStuff.Page at the base.
Definition Button.qml:17
static Q_INVOKABLE bool authorize(const QString &action)
QPushButton subclass that encapsulates the logic for showing the KNewStuff dialog.
Definition button.h:30
Button(const QString &text, const QString &configFile, QWidget *parent)
Constructor used when the details of the KHotNewStuff download is known when the button is created.
Definition button.cpp:50
void setConfigFile(const QString &configFile)
Definition button.cpp:76
This class is a wrapper around the QtQuick QML dialog.
Definition dialog.h:34
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void information(QWidget *parent, const QString &text, const QString &title=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
void clicked(bool checked)
void setIcon(const QIcon &icon)
void finished(int result)
QIcon fromTheme(const QString &name)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
void setEnabled(bool)
virtual void setVisible(bool visible)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:54:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.