KNewStuff

qtquickdialogwrapper.cpp
1 /*
2  SPDX-FileCopyrightText: 2020 Alexander Lohnau <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.1-or-later
5 */
6 
7 #include "qtquickdialogwrapper.h"
8 
9 #include <QEventLoop>
10 #include <QQmlComponent>
11 #include <QQmlContext>
12 #include <QQmlEngine>
13 #include <QTimer>
14 
15 #include "core/engine.h"
16 
17 using namespace KNS3;
18 
19 class KNS3::QtQuickDialogWrapperPrivate
20 {
21 public:
22  QQmlEngine *engine = nullptr;
23  QObject *item = nullptr;
24  KNSCore::Engine *coreEngine = nullptr;
25  QList<KNSCore::EntryInternal> changedEntries;
26 };
27 
28 QtQuickDialogWrapper::QtQuickDialogWrapper(const QString &configFile, QObject *parent)
29  : QObject(parent)
30  , d(new QtQuickDialogWrapperPrivate())
31 {
32  d->engine = new QQmlEngine(this);
33  QQmlComponent component(d->engine);
34  d->engine->rootContext()->setContextProperty(QStringLiteral("knsrcfile"), configFile);
35  component.setData(QByteArrayLiteral("import QtQuick 2.7\n"
36  "import org.kde.newstuff 1.62 as NewStuff\n"
37  "\n"
38  "NewStuff.Dialog {\n"
39  " id: component\n"
40  " signal closed()\n"
41  " configFile: knsrcfile\n"
42  " onVisibleChanged: if (!visible) {closed()}\n"
43  "}"),
44  QUrl());
45  d->item = component.create();
46  // If there is an error on the QML side of things we get a nullptr
47  if (d->item) {
48  QObject *qtquickEngine = d->item->property("engine").value<QObject *>();
49  Q_ASSERT(qtquickEngine);
50  d->coreEngine = qtquickEngine->property("engine").value<KNSCore::Engine *>();
51  Q_ASSERT(d->coreEngine);
52 
53  connect(d->coreEngine, &KNSCore::Engine::signalEntryEvent, this, [this](const KNSCore::EntryInternal &entry, KNSCore::EntryInternal::EntryEvent event) {
54  if (event == KNSCore::EntryInternal::StatusChangedEvent) {
55  if (entry.status() == KNS3::Entry::Installing || entry.status() == KNS3::Entry::Updating) {
56  return; // We do not care about intermediate states
57  }
58  // To make sure we have no duplicates and always the latest entry
59  d->changedEntries.removeOne(entry);
60  d->changedEntries.append(entry);
61  }
62  });
63 
64  // Forward relevant signals
65  connect(d->item, SIGNAL(closed()), this, SIGNAL(closed()));
66  }
67 }
68 
69 QtQuickDialogWrapper::~QtQuickDialogWrapper()
70 {
71  // Empty destructor needed for std::unique_ptr to incomplete class.
72 }
73 
74 void QtQuickDialogWrapper::open()
75 {
76  if (d->item) {
77  d->changedEntries.clear();
78  QMetaObject::invokeMethod(d->item, "open");
79  }
80 }
81 
82 KNSCore::Engine *QtQuickDialogWrapper::engine()
83 {
84  return d->coreEngine;
85 }
86 
87 QList<KNSCore::EntryInternal> QtQuickDialogWrapper::exec()
88 {
89  open();
90  QEventLoop loop;
91  connect(this, &QtQuickDialogWrapper::closed, &loop, &QEventLoop::quit);
92  loop.exec();
93  return d->changedEntries;
94 }
void quit()
T value() const const
virtual bool event(QEvent *e)
QVariant property(const char *name) const const
int exec(QEventLoop::ProcessEventsFlags flags)
bool invokeMethod(QObject *obj, const char *member, Qt::ConnectionType type, QGenericReturnArgument ret, QGenericArgument val0, QGenericArgument val1, QGenericArgument val2, QGenericArgument val3, QGenericArgument val4, QGenericArgument val5, QGenericArgument val6, QGenericArgument val7, QGenericArgument val8, QGenericArgument val9)
KNewStuff data entry container.
Definition: entryinternal.h:49
KNewStuff engine.
Definition: engine.h:59
The namespace for the KNewStuff classes.
Definition: button.cpp:19
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void signalEntryEvent(const EntryInternal &entry, EntryInternal::EntryEvent event)
This is fired for any event related directly to a single EntryInternal instance.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Tue May 11 2021 22:41:42 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.