File indexing completed on 2024-04-21 15:02:27
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 "entry_p.h" 0011 #include "qtquickdialogwrapper.h" 0012 #include "ui/widgetquestionlistener.h" 0013 #include <KAuthorized> 0014 #include <KLocalizedString> 0015 0016 #include <QPointer> 0017 0018 using namespace KNS3; 0019 0020 namespace KNSWidgets 0021 { 0022 class ActionPrivate 0023 { 0024 public: 0025 QString configFile; 0026 QPointer<QtQuickDialogWrapper> dialog; 0027 }; 0028 0029 Action::Action(const QString &text, const QString &configFile, QObject *parent) 0030 : QAction(parent) 0031 , d(new ActionPrivate) 0032 { 0033 if (text.isEmpty()) { 0034 setText(i18n("Download New Stuff...")); 0035 } else { 0036 setText(text); 0037 } 0038 d->configFile = configFile; 0039 init(); 0040 } 0041 0042 Action::~Action() 0043 { 0044 } 0045 0046 void Action::init() 0047 { 0048 const bool authorized = KAuthorized::authorize(KAuthorized::GHNS); 0049 if (!authorized) { 0050 setEnabled(false); 0051 setVisible(false); 0052 } 0053 0054 setIcon(QIcon::fromTheme(QStringLiteral("get-hot-new-stuff"))); 0055 connect(this, &QAction::triggered, this, &Action::showDialog); 0056 } 0057 0058 void Action::setConfigFile(const QString &configFile) 0059 { 0060 d->configFile = configFile; 0061 } 0062 0063 void Action::showDialog() 0064 { 0065 if (!KAuthorized::authorize(KAuthorized::GHNS)) { 0066 return; 0067 } 0068 Q_EMIT aboutToShowDialog(); 0069 0070 if (!d->dialog) { 0071 d->dialog = new QtQuickDialogWrapper(d->configFile, this); 0072 connect(d->dialog.data(), &KNS3::QtQuickDialogWrapper::closed, this, [this]() { 0073 const QList<KNSCore::EntryInternal> changedInternalEntries = d->dialog->changedEntries(); 0074 #if KNEWSTUFFWIDGETS_BUILD_DEPRECATED_SINCE(5, 91) 0075 QList<KNS3::Entry> changedEntries; 0076 for (const KNSCore::EntryInternal &e : changedInternalEntries) { 0077 changedEntries << EntryPrivate::fromInternal(&e); 0078 } 0079 Q_EMIT dialogFinished(changedEntries); 0080 #endif 0081 Q_EMIT dialogFinished(changedInternalEntries); 0082 }); 0083 } 0084 d->dialog->open(); 0085 } 0086 0087 } 0088 0089 #include "moc_action.cpp"