From 56c1e420d07c374b5f8d36d1e20ae6242c369e24 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Burak=20Han=C3=A7erli?= <burak.hancerli@qt.io>
Date: Tue, 5 Dec 2023 11:19:19 +0000
Subject: [PATCH] QDS-11424 Update user projects automatically

---
 src/backend.cpp     |  59 ++++++++++++++++++++++++++++++++-
 src/backend.h       |   9 +++++
 ui/SettingsPage.qml |  79 ++++++++++++++++++++++++++++++++++++++++++++
 ui/i18n/qml_en.qm   | Bin 0 -> 30 bytes
 ui/main.qml         |  22 +++++++++++-
 ui/resources.qrc    |   1 +
 ui/share.qrc        |  20 +++++++++++
 7 files changed, 188 insertions(+), 2 deletions(-)
 create mode 100644 ui/SettingsPage.qml
 create mode 100644 ui/i18n/qml_en.qm
 create mode 100644 ui/share.qrc

diff --git a/src/backend.cpp b/src/backend.cpp
index b198893..c17f463 100644
--- a/src/backend.cpp
+++ b/src/backend.cpp
@@ -72,7 +72,54 @@ void Backend::initialize()
         updateUserProjectList();
     }
 
-    qDebug("Initialization complete");
+    // Check if updateInBackground is enabled
+    updateInBackground(settings.value("system/updateInBackground").toBool());
+    qDebug() << "Initialization complete";
+}
+
+void Backend::updateInBackground(const bool &enabled)
+{
+    QSettings().setValue("system/updateInBackground", enabled);
+    if (enabled) {
+        qDebug() << "Update in background is enabled";
+        m_backgroundTimer.setInterval(1000 * 10);
+        connect(&m_backgroundTimer, &QTimer::timeout, this, [&] {
+            qDebug() << "Checking for updates in background";
+            if (m_userHash.isEmpty())
+                return;
+
+            m_serviceConnector.reset(new ServiceConnector);
+            QJsonArray projectList = m_serviceConnector->fetchUserProjectList(m_userHash);
+            if (projectList.isEmpty())
+                return;
+
+            qDebug() << "New projects available. Updating project list";
+            updateUserProjectList();
+        });
+        m_backgroundTimer.start();
+    } else {
+        qDebug() << "Update in background is disabled";
+        m_backgroundTimer.stop();
+    }
+}
+
+void Backend::cacheDemoProjects(const bool &enabled)
+{
+    QSettings().setValue("system/cacheDemoProjects", enabled);
+    if (enabled) {
+        qDebug() << "Caching demo projects is enabled";
+    } else {
+        qDebug() << "Caching demo projects is disabled";
+    }
+}
+void Backend::cacheUserProjects(const bool &enabled)
+{
+    QSettings().setValue("system/cacheUserProjects", enabled);
+    if (enabled) {
+        qDebug() << "Caching user projects is enabled";
+    } else {
+        qDebug() << "Caching user projects is disabled";
+    }
 }
 
 void Backend::updatePopup(const QString &text, bool indeterminate)
@@ -322,6 +369,16 @@ void Backend::updateUserProjectList()
     m_serviceConnector.reset(new ServiceConnector);
     QJsonArray projectList = m_serviceConnector->fetchUserProjectList(m_userHash);
 
+    if (projectList.isEmpty()) {
+        qCritical("Could not fetch available project list");
+        return;
+    }
+
+    if (projectList == m_projectListArray) {
+        qDebug("No new projects available");
+        return;
+    }
+    m_projectListArray = projectList;
     m_projectList.clear();
     qDebug("List of available projects fetched:");
     for (const auto &project : projectList) {
diff --git a/src/backend.h b/src/backend.h
index d3e99d3..124c949 100644
--- a/src/backend.h
+++ b/src/backend.h
@@ -54,6 +54,7 @@ private:
     // UI data
     QString m_logs;
     QStringList m_projectList;
+    QJsonArray m_projectListArray;
 
     // Other members
     QString m_userHash;
@@ -62,6 +63,8 @@ private:
     QScopedPointer<DesignStudioConnector> m_designStudioConnector;
     QThread m_dsConnectorThread;
 
+    QTimer m_backgroundTimer;
+
     // member functions
     void updateUserProjectList();
     void updatePopup(const QString &text, bool indeterminate = true);
@@ -91,6 +94,12 @@ public slots:
 
     void parseDesignViewerUrl(const QUrl &url);
     void registerUser(const QUrl &url);
+
+    // settings
+    void updateInBackground(const bool &enabled);
+    void cacheDemoProjects(const bool &enabled);
+    void cacheUserProjects(const bool &enabled);
+
 private slots:
     void initializeProjectManager();
 };
diff --git a/ui/SettingsPage.qml b/ui/SettingsPage.qml
new file mode 100644
index 0000000..7e86b60
--- /dev/null
+++ b/ui/SettingsPage.qml
@@ -0,0 +1,79 @@
+import QtQuick
+import QtQuick.Controls 6.4
+import QtQuick.Layouts
+
+Item {
+    id: settingsPage
+
+    ColumnLayout {
+        anchors.fill: parent
+
+        Item {
+            id: item2
+            Layout.preferredWidth: 10
+            Layout.preferredHeight: 10
+            Layout.fillWidth: true
+            Layout.fillHeight: true
+        }
+
+        ColumnLayout {
+            id: column2
+            Layout.fillWidth: true
+
+            CheckBox {
+                id: checkBox
+                text: qsTr("Update user projects in the backgroud")
+                font.pointSize: 15
+                onCheckStateChanged: backend.updateInBackground(checkState)
+            }
+
+            Text{
+                leftPadding: 45
+                text: qsTr("Checks new projects in each 10 seconds")
+                font.pointSize: 12
+            }
+
+//            CheckBox {
+//                id: checkBox2
+//                text: qsTr("Cache user projects")
+//                font.pointSize: 15
+//                onCheckStateChanged: backend.cacheUserProjects(checkState)
+//            }
+
+//            Text{
+//                leftPadding: 45
+//                text: qsTr("Downloads the user projects each time if disabled")
+//                font.pointSize: 12
+//            }
+
+//            CheckBox {
+//                id: checkBox3
+//                text: qsTr("Cache demo projects")
+//                font.pointSize: 15
+//                onCheckStateChanged: backend.cacheDemoProjects(checkState)
+//            }
+
+//            Text{
+//                leftPadding: 45
+//                text: qsTr("Downloads the demo projects each time if disabled")
+//                font.pointSize: 12
+//            }
+        }
+
+        Item {
+            id: item3
+            Layout.fillHeight: true
+            Layout.fillWidth: true
+            Layout.preferredHeight: 10
+            Layout.preferredWidth: 10
+        }
+
+//        Button {
+//            id: downloadButton
+//            text: qsTr("Save")
+//            onClicked: backend.saveSettings(checkBox.checked)
+//            Layout.fillWidth: true
+//            Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
+//        }
+    }
+}
diff --git a/ui/i18n/qml_en.qm b/ui/i18n/qml_en.qm
new file mode 100644
index 0000000000000000000000000000000000000000..ce6afd21067436b699a9d6d6df69a625b068facd
GIT binary patch
literal 30
kcmcE7ks@*G{hX<16=n7(EZlo{IRgU&Q)*rZh+<>}0MslB#{d8T

literal 0
HcmV?d00001

diff --git a/ui/main.qml b/ui/main.qml
index 9d520dc..24f84af 100644
--- a/ui/main.qml
+++ b/ui/main.qml
@@ -111,6 +111,11 @@ Rectangle {
             Layout.fillWidth: true
         }
 
+        SettingsPage {
+            id: settingsPage
+            Layout.fillWidth: true
+        }
+
         AboutHeader {
             id: headerPage
             Layout.fillWidth: true
@@ -207,6 +212,21 @@ Rectangle {
                     }
                 }
 
+                TabButton {
+                    id: settings
+                    text: qsTr("Settings")
+                    Layout.fillWidth: true
+                    checkable: true
+                    autoExclusive: true
+
+                    Connections {
+                        target: settings
+                        function onClicked(){
+                            stackLayout.currentIndex = 4
+                        }
+                    }
+                }
+
 
                 TabButton {
                     id: about
@@ -218,7 +238,7 @@ Rectangle {
                     Connections {
                         target: about
                         function onClicked(){
-                            stackLayout.currentIndex = 4
+                            stackLayout.currentIndex = 5
                         }
                     }
                 }
diff --git a/ui/resources.qrc b/ui/resources.qrc
index cee050c..329112b 100644
--- a/ui/resources.qrc
+++ b/ui/resources.qrc
@@ -7,5 +7,6 @@
         <file>Network.qml</file>
         <file>ExamplesPage.qml</file>
         <file>AboutHeader.qml</file>
+        <file>SettingsPage.qml</file>
     </qresource>
 </RCC>
diff --git a/ui/share.qrc b/ui/share.qrc
new file mode 100644
index 0000000..64fdfa6
--- /dev/null
+++ b/ui/share.qrc
@@ -0,0 +1,20 @@
+
+<RCC>
+<qresource>
+<file>AboutHeader.qml</file>
+<file>content/images/appicon.png</file>
+<file>DesignViewer.qmlproject</file>
+<file>ExamplesPage.qml</file>
+<file>HomePage.qml</file>
+<file>imports/DesignViewer/Constants.qml</file>
+<file>imports/DesignViewer/DirectoryFontLoader.qml</file>
+<file>imports/DesignViewer/EventListModel.qml</file>
+<file>imports/DesignViewer/EventListSimulator.qml</file>
+<file>imports/DesignViewer/qmldir</file>
+<file>Logs.qml</file>
+<file>main.qml</file>
+<file>Network.qml</file>
+<file>qtquickcontrols2.conf</file>
+<file>SettingsPage.qml</file>
+</qresource>
+</RCC>
\ No newline at end of file
-- 
GitLab