From d011f122d7a55e88deb2bba47d7c02658fa6bac9 Mon Sep 17 00:00:00 2001
From: Burak Hancerli <burak.hancerli@qt.io>
Date: Mon, 9 Dec 2024 14:04:13 +0100
Subject: [PATCH] fix: increase the ping timer interval to be able to wait for
 larger projects

---
 3rdparty/qtquickdesigner-components |  2 +-
 src/backend/backend.cpp             |  9 ++-
 src/backend/dsconnector/ds.cpp      |  2 +-
 src/backend/projectmanager.cpp      | 98 ++++++++++++++++++++++-------
 src/backend/projectmanager.h        |  4 +-
 5 files changed, 89 insertions(+), 26 deletions(-)

diff --git a/3rdparty/qtquickdesigner-components b/3rdparty/qtquickdesigner-components
index 34b97d4..04a5f67 160000
--- a/3rdparty/qtquickdesigner-components
+++ b/3rdparty/qtquickdesigner-components
@@ -1 +1 @@
-Subproject commit 34b97d40d86d7c243cb6bf8245d717397ef25268
+Subproject commit 04a5f670d3e7ae28e54bf194a3dd70e13d05ffc4
diff --git a/src/backend/backend.cpp b/src/backend/backend.cpp
index 17c2976..5a7be45 100644
--- a/src/backend/backend.cpp
+++ b/src/backend/backend.cpp
@@ -26,9 +26,12 @@
 #include "backend.h"
 
 #include <QDesktopServices>
-#include <QJniObject>
 #include <QNetworkInterface>
 
+#ifdef Q_OS_ANDROID
+#include <QJniObject>
+#endif
+
 #include "logger.h"
 
 #ifdef QT_DEBUG
@@ -266,12 +269,16 @@ void Backend::setKeepScreenOn(bool keepScreenOn)
 
 void Backend::setAndroidScreenOn(bool on)
 {
+#ifdef Q_OS_ANDROID
     QNativeInterface::QAndroidApplication::runOnAndroidMainThread([=]() {
         QJniObject activity = QNativeInterface::QAndroidApplication::context();
         QJniObject window = activity.callObjectMethod("getWindow", "()Landroid/view/Window;");
         on ? window.callMethod<void>("addFlags", "(I)V", FLAG_KEEP_SCREEN_ON)
            : window.callMethod<void>("clearFlags", "(I)V", FLAG_KEEP_SCREEN_ON);
     });
+#else
+    Q_UNUSED(on);
+#endif
 }
 
 QString Backend::lastDesignStudioIp() const
diff --git a/src/backend/dsconnector/ds.cpp b/src/backend/dsconnector/ds.cpp
index b681751..6275546 100644
--- a/src/backend/dsconnector/ds.cpp
+++ b/src/backend/dsconnector/ds.cpp
@@ -21,7 +21,7 @@ DesignStudio::DesignStudio(QWebSocket *socket, const QString &m_deviceUuid, QObj
 void DesignStudio::initPingPong()
 {
     m_pingTimer.setInterval(1000);
-    m_pongTimer.setInterval(10000);
+    m_pongTimer.setInterval(30000);
     m_pongTimer.setSingleShot(true);
     m_pingTimer.setSingleShot(true);
 
diff --git a/src/backend/projectmanager.cpp b/src/backend/projectmanager.cpp
index 6f2bcb1..acb50e3 100644
--- a/src/backend/projectmanager.cpp
+++ b/src/backend/projectmanager.cpp
@@ -32,6 +32,7 @@
 #include <QRandomGenerator>
 #include <QRegularExpression>
 #include <QResource>
+#include <QStandardPaths>
 #include <QTemporaryDir>
 
 ProjectManager::ProjectManager(QObject *parent)
@@ -40,45 +41,98 @@ ProjectManager::ProjectManager(QObject *parent)
 
 ProjectManager::~ProjectManager()
 {
-    qDebug() << "ProjectManager destroyed. Cleaning up resources.";
-    cleanupResources();
+    unregisterResource(m_projectData, m_projectPath);
 }
 
-void ProjectManager::cleanupResources()
+bool ProjectManager::unregisterResource(const QByteArray &data, const QString &path)
 {
-    const uchar *data;
-    if (m_projectData.size()) {
-        qDebug() << "Unregistering the previous data from QRC system. Path: " << m_projectPath
-                 << " Size: " << m_projectData.size() << " bytes.";
-        data = reinterpret_cast<const uchar *>(m_projectData.data());
-        if (!QResource::unregisterResource(data, m_projectPath)) {
+    const uchar *charPtr;
+    if (data.size()) {
+        qDebug() << "Unregistering the previous data from QRC system. Path: " << path
+                 << " Size: " << data.size() << " bytes.";
+        charPtr = reinterpret_cast<const uchar *>(data.data());
+        if (!QResource::unregisterResource(charPtr, path)) {
             qCritical() << "Cannot unregister the previous resource data.";
+            return false;
         }
     }
+
+    return true;
 }
 
-QString ProjectManager::unpackProject(const QByteArray &project)
+bool ProjectManager::registerResource(const QByteArray &data, const QString &path)
 {
-    cleanupResources();
+    if (!QDir(path).removeRecursively()) {
+        qWarning() << "Could not remove resource path: " << path;
+        return false;
+    }
 
-    m_projectData = project;
-    const uchar *data;
-    data = reinterpret_cast<const uchar *>(m_projectData.data());
-    qDebug() << "Registering resource data. Size: " << m_projectData.size();
+    qDebug() << "Registering resource data. Size: " << data.size();
+    const uchar *resourceData = reinterpret_cast<const uchar *>(data.data());
+    if (!QResource::registerResource(resourceData, path)) {
+        qCritical() << "Cannot register the resource data.";
+        return false;
+    }
 
-    const QString resourcePath{"/" + QString::number(QRandomGenerator::global()->generate())};
-    m_projectPath = resourcePath;
+    return true;
+}
 
-    if (!QDir(resourcePath).removeRecursively()) {
-        qWarning() << "Could not remove resource path: " << resourcePath;
+bool ProjectManager::copyResourceToFs(const QString &sourcePath, const QString &destPath)
+{
+    QDir tmpDir(destPath);
+    if (tmpDir.exists()) {
+        if (!tmpDir.removeRecursively()) {
+            qCritical() << "Could not remove tmp dir: " << destPath;
+            return false;
+        }
     }
 
-    if (!QResource::registerResource(data, resourcePath)) {
-        qCritical() << "Can not load the resource data.";
+    if (!tmpDir.mkpath(destPath)) {
+        qCritical() << "Could not create tmp dir: " << destPath;
+        return false;
+    }
+
+    QDirIterator it(sourcePath,
+                    QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot,
+                    QDirIterator::Subdirectories);
+
+    while (it.hasNext()) {
+        it.next();
+        const QString filePath = it.filePath();
+        const QString relativePath = it.filePath().mid(sourcePath.length());
+        const QString targetPath = destPath + relativePath;
+
+        qDebug() << "Copying file: " << filePath << " to " << targetPath;
+        if (it.fileInfo().isDir()) {
+            QDir().mkpath(targetPath);
+        } else {
+            QFile file(filePath);
+            if (!file.copy(targetPath)) {
+                qCritical() << "Could not copy file: " << filePath << " to " << targetPath;
+                return false;
+            }
+        }
+    }
+
+    return true;
+}
+
+QString ProjectManager::unpackProject(const QByteArray &project)
+{
+    if (!unregisterResource(m_projectData, m_projectPath)) {
+        qCritical() << "Could not unregister previous resource: " << m_projectPath;
         return {};
     }
 
-    return QString(":") + resourcePath;
+    const QString resourcePath{":/" + QString::number(QRandomGenerator::global()->generate())};
+    if (!registerResource(project, resourcePath)) {
+        qCritical() << "Could not register resource: " << resourcePath;
+        return {};
+    }
+    m_projectData = project;
+    m_projectPath = resourcePath;
+
+    return resourcePath;
 }
 
 QString ProjectManager::findFile(const QString &dir, const QString &filter)
diff --git a/src/backend/projectmanager.h b/src/backend/projectmanager.h
index cde6e95..5240f85 100644
--- a/src/backend/projectmanager.h
+++ b/src/backend/projectmanager.h
@@ -52,7 +52,9 @@ private:
     QScopedPointer<QQuickWindow> m_quickWindow;
 
     // resource management
-    void cleanupResources();
+    bool registerResource(const QByteArray &data, const QString &path);
+    bool unregisterResource(const QByteArray &data, const QString &path);
+    bool copyResourceToFs(const QString &sourcePath, const QString &targetPath);
     QString unpackProject(const QByteArray &project);
 
     // project parsers
-- 
GitLab