diff --git a/src/backend.cpp b/src/backend.cpp index 7a5d26d7b23d95f050223d3c4c54a41831b6a60a..e88b71effaa82efea497687c08b59b92f0970a61 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -170,37 +170,43 @@ void Backend::initDesignStudioConnector() updatePopup("Receiving project..."); }); - connect(m_designStudioConnector.data(), - &DesignStudioConnector::projectReceived, - this, - [this](const QByteArray &projectData) { - qDebug() << "Project received from Design Studio"; - initializeProjectManager(); - emit popupOpen(); - updatePopup("Unpacking project..."); - qDebug() << "Project data size: " << projectData.size(); - const QString projectPath = m_projectManager->unpackProject(projectData); - - if (projectPath.isEmpty()) { - qCritical() << "Could not unpack project. Please check the logs for more " - "information."; - emit popupClose(); - return; - } - - qDebug() << "Project unpacked to " << projectPath; - updatePopup("Running project..."); - - if (!m_projectManager->runProject(projectPath)) { - qCritical() << "Could not run project. Please check the logs for more " - "information."; - } else { - m_projectManager->showAppWindow(); - } - + connect( + m_designStudioConnector.data(), + &DesignStudioConnector::projectReceived, + this, + [this](const QByteArray &projectData) { + qDebug() << "Project received from Design Studio"; + initializeProjectManager(); + emit popupOpen(); + updatePopup("Unpacking project..."); + qDebug() << "Project data size: " << projectData.size(); + const QString projectPath = m_projectManager->unpackProject(projectData); + + if (projectPath.isEmpty()) { + qCritical() << "Could not unpack project. Please check the logs for more " + "information."; emit popupClose(); - m_designStudioConnector->sendProjectReceived(); - }); + return; + } + + qDebug() << "Project unpacked to " << projectPath; + updatePopup("Running project..."); + + qDebug() << "Project received confirmation sent to Design Studio"; + if (!m_projectManager->runProject(projectPath)) { + qCritical() << "Could not run project. Please check the logs for more " + "information."; + } else { + m_projectManager->showAppWindow(); + } + + QMetaObject::invokeMethod(m_designStudioConnector.data(), + "sendProjectReceived", + Qt::QueuedConnection); + + emit popupClose(); + }, + Qt::QueuedConnection); qDebug() << "Design Studio Connector is initialized"; } @@ -429,3 +435,9 @@ void Backend::parseDesignViewerUrl(const QUrl &url) qWarning() << "Unknown QR code data: " << urlData; } } + +void Backend::popupInterrupted() +{ + qDebug() << "Popup closed prematurely. Interrupting active downloads (if any)"; + QMetaObject::invokeMethod(&m_serviceConnector, "interrupted", Qt::QueuedConnection); +} diff --git a/src/backend.h b/src/backend.h index 5ce42d7ee7eb62112e7c31eea76a82999bfd9d48..c7f209032a72f06c5e3567aa049301a79742759d 100644 --- a/src/backend.h +++ b/src/backend.h @@ -56,7 +56,6 @@ private: QJsonArray m_projectList; // Other members - ServiceConnector m_serviceConnector; QThread m_dsConnectorThread; QScopedPointer<ProjectManager> m_projectManager; @@ -114,6 +113,8 @@ public slots: bool autoScaleProject(); QString userHash(); + void popupInterrupted(); + private slots: void initializeProjectManager(); void enableBackgroundUpdate(const bool &enabled); diff --git a/src/dsconnector.cpp b/src/dsconnector.cpp index d82c31dfd87eefbea6d47faaab8e52bc99f8b79b..a608535595d8f23866ee6275cf3f8b742e69a8cf 100644 --- a/src/dsconnector.cpp +++ b/src/dsconnector.cpp @@ -121,5 +121,5 @@ void DesignStudioConnector::sendProjectReceived() } m_tcpSocket->write("::qmlrc-received::"); - m_tcpSocket->flush(); + m_tcpSocket->waitForBytesWritten(3000); } diff --git a/src/dsconnector.h b/src/dsconnector.h index 2a5c193b045310f8c51820b30ad0b44ad8984e9c..66e1ec5cf81be42c11752f6b8a211fa96b536e61 100644 --- a/src/dsconnector.h +++ b/src/dsconnector.h @@ -38,6 +38,7 @@ class DesignStudioConnector : public QObject public: explicit DesignStudioConnector(QObject *parent = nullptr); +public slots: void sendProjectReceived(); private: diff --git a/src/serviceconnector.cpp b/src/serviceconnector.cpp index 422709fe28bae79787126c96ffd89e67036c3040..f8314f21ac7005f1293b6f63f632f831536e6f7b 100644 --- a/src/serviceconnector.cpp +++ b/src/serviceconnector.cpp @@ -46,6 +46,11 @@ std::optional<QByteArray> ServiceConnector::fetchResource(const QString &url, co }); QEventLoop loop; + QObject::connect(this, &ServiceConnector::interrupted, reply.data(), [&]() { + qDebug() << "Aborting network request"; + reply->abort(); + loop.quit(); + }); QObject::connect(reply.data(), &QNetworkReply::finished, &loop, &QEventLoop::quit); QObject::connect(reply.data(), &QNetworkReply::downloadProgress, diff --git a/src/serviceconnector.h b/src/serviceconnector.h index 07b18f1cd6009d890a3f80fd23ae1dd14ed8e7e1..9d5d4f50e8c521c4f46eda06d49b2c04ff2deb9b 100644 --- a/src/serviceconnector.h +++ b/src/serviceconnector.h @@ -49,6 +49,7 @@ private: signals: void downloadProgress(float percentage); + void interrupted(); }; #endif // SERVICECONNECTOR_H diff --git a/ui/main.qml b/ui/main.qml index 4b69f686738758ee978b3cd897588fc27422a994..c10ec23cc4b196397d72cf8d55611665f1a03136 100644 --- a/ui/main.qml +++ b/ui/main.qml @@ -35,6 +35,7 @@ Rectangle { } Popup { + property var popupCloseReceived : false id: popup anchors.centerIn: parent width: 300 @@ -43,6 +44,12 @@ Rectangle { focus: true closePolicy: Popup.CloseOnEscape + onClosed: { + if (!popupCloseReceived) { + backend.popupInterrupted(); + } + } + ColumnLayout { anchors.fill: parent Text { @@ -62,8 +69,10 @@ Rectangle { target: backend function onPopupOpen() { popup.open() + popup.popupCloseReceived = false } function onPopupClose() { + popup.popupCloseReceived = true popup.close() } function onPopupTextChanged(text) {