Skip to content
Snippets Groups Projects
Commit 9110c418 authored by Burak Hançerli's avatar Burak Hançerli :headphones:
Browse files

QDS-11236 Integrate with demo project endpoint

parent 56c1e420
No related branches found
No related tags found
1 merge request!26QDS-11236 Integrate with demo project endpoint
Pipeline #65201 passed
......@@ -240,17 +240,29 @@ void Backend::runDemoProject(const QString &projectName)
qDebug() << "Checking if demo project is cached for " << projectName;
emit popupOpen();
const bool cached = m_projectManager->isDemoProjectCached(projectName);
// sample project info
// [{"lastUpdate":1701947766739.9812,"name":"ClusterTutorial.qmlrc"}]
const QJsonArray projectList = m_serviceConnector->fetchUserDemoList();
QJsonObject projectInfo;
for (auto project : projectList) {
if (projectName == project.toObject().value("name").toString().remove(".qmlrc")) {
projectInfo = project.toObject();
break;
}
}
const bool cached = m_projectManager->isDemoProjectCached(projectInfo);
if (!cached) {
updatePopup("Downloading demo project...", false);
const QString url = "https://designviewer.qt.io/qmlprojects/" + projectName + ".qmlrc";
const QString url = "https://designviewer.qt.io/qmlprojects/demos/" + projectName
+ ".qmlrc";
QByteArray project = m_serviceConnector->fetchProject(url);
qDebug() << "Demo project is not cached. Trying to download from " << url << " ...";
updatePopup("Caching demo project...");
if (!m_projectManager->cacheDemoProject(project, projectName)) {
if (!m_projectManager->cacheDemoProject(project, projectInfo)) {
qCritical()
<< "Could not cache demo project. Please check the logs for more information.";
emit popupClose();
......
......@@ -403,8 +403,11 @@ bool ProjectManager::runCachedProject(const QJsonObject &projectInfo)
return runProject(projectPath);
}
bool ProjectManager::cacheDemoProject(const QByteArray &projectData, const QString &projectName)
bool ProjectManager::cacheDemoProject(const QByteArray &projectData, const QJsonObject &projectInfo)
{
// sample project info
//[{"lastUpdate":1701947766739.9812,"name":"ClusterTutorial.qmlrc"}]
const QString projectName = projectInfo.value("name").toString().remove(".qmlrc");
const QString demoProjectPath = m_demoProjectCachePath + "/" + projectName;
qDebug() << "Caching demo project " << projectName << " to " << demoProjectPath;
......@@ -440,6 +443,15 @@ bool ProjectManager::cacheDemoProject(const QByteArray &projectData, const QStri
return false;
}
}
// write project info to cache
QFile demoProjectInfoFile(demoProjectPath + "/projectInfo.json");
if (!demoProjectInfoFile.open(QIODevice::WriteOnly)) {
qCritical() << "Could not open demo project info file for writing";
return false;
}
demoProjectInfoFile.write(QJsonDocument(projectInfo).toJson());
return true;
}
......@@ -449,11 +461,42 @@ void ProjectManager::clearDemoCaches()
QDir(m_demoProjectCachePath).removeRecursively();
}
bool ProjectManager::isDemoProjectCached(const QString &projectName)
bool ProjectManager::isDemoProjectCached(const QJsonObject &projectInfo)
{
// sample project info
//[{"lastUpdate":1701947766739.9812,"name":"ClusterTutorial.qmlrc"}]
const QString projectName = projectInfo.value("name").toString().remove(".qmlrc");
const QString demoProjectPath = m_demoProjectCachePath + "/" + projectName;
qDebug() << "Checking if demo project " << projectName << " is cached";
return QDir(demoProjectPath).exists();
if (!QDir(demoProjectPath).exists()) {
qDebug() << "Demo project " << projectName << " is not cached";
return false;
}
qDebug() << "Demo project " << projectName << " is cached. Checking the last update time...";
const QString demoProjectInfoPath = demoProjectPath + "/projectInfo.json";
QFile demoProjectInfoFile(demoProjectInfoPath);
if (!demoProjectInfoFile.open(QIODevice::ReadOnly)) {
qCritical() << "Could not open demo project info file for reading";
return false;
}
const QJsonObject cachedProjectInfo
= QJsonDocument::fromJson(demoProjectInfoFile.readAll()).object();
const QJsonValue cachedLastModified = cachedProjectInfo.value("lastUpdate");
const QJsonValue cloudLastModified = projectInfo.value("lastUpdate");
qDebug() << "Demo project" << projectName << "cloud timestamp: " << cloudLastModified
<< "local timestamp:" << cachedLastModified;
if (cloudLastModified != cachedLastModified) {
qDebug() << "Demo project " << projectName << " is not up to date";
return false;
}
qDebug() << "Demo project " << projectName << " is up to date";
return true;
}
bool ProjectManager::runDemoProject(const QString &projectName)
......
......@@ -47,8 +47,8 @@ public:
bool isProjectCached(const QJsonObject &projectInfo);
bool runCachedProject(const QJsonObject &projectInfo);
bool cacheDemoProject(const QByteArray &projectData, const QString &projectName);
bool isDemoProjectCached(const QString &projectName);
bool cacheDemoProject(const QByteArray &projectData, const QJsonObject &projectInfo);
bool isDemoProjectCached(const QJsonObject &projectName);
bool runDemoProject(const QString &projectName);
void clearDemoCaches();
......
......@@ -103,3 +103,14 @@ QJsonArray ServiceConnector::fetchUserProjectList(const QString &userHash)
return projectList;
}
QJsonArray ServiceConnector::fetchUserDemoList()
{
auto reply = fetchResource("https://designviewer.qt.io/api/v1/demos");
if (reply.size() == 0) {
qCritical("Could not fetch available demo list");
return QJsonArray();
}
return QJsonDocument::fromJson(reply).array();
}
......@@ -36,6 +36,7 @@ public:
QByteArray fetchProject(const QString &url);
QByteArray fetchUserProject(const QString &userHash, const QString &projectName);
QJsonArray fetchUserProjectList(const QString &userHash);
QJsonArray fetchUserDemoList();
private:
QNetworkAccessManager m_manager;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment