Skip to content
Snippets Groups Projects

QDS-11947 Clean cached user project

Merged Burak Hançerli requested to merge QDS-11947/clean-cached-projects into master
Files
12
+ 19
122
@@ -24,25 +24,14 @@
@@ -24,25 +24,14 @@
****************************************************************************/
****************************************************************************/
#include "backend.h"
#include "backend.h"
#include "qapplication.h"
#include <QCameraDevice>
#include <QDesktopServices>
#include <QDesktopServices>
#include <QElapsedTimer>
#include <QEventLoop>
#include <QFileInfo>
#include <QFileInfo>
 
#include <QGuiApplication>
#include <QJsonArray>
#include <QJsonArray>
#include <QJsonObject>
#include <QJsonObject>
#include <QMediaDevices>
#include <QMessageBox>
#include <QPermission>
#include <QSettings>
#include <QSettings>
#include <QSslSocket>
#include <QVideoSink>
#include <QtConcurrent>
#include "../3rdparty/zxing-cpp/example/ZXingQtReader.h"
using namespace ZXingQt;
Backend::Backend(QObject *parent)
Backend::Backend(QObject *parent)
: QObject(parent)
: QObject(parent)
@@ -147,113 +136,13 @@ void Backend::updatePopup(const QString &text, bool indeterminate)
@@ -147,113 +136,13 @@ void Backend::updatePopup(const QString &text, bool indeterminate)
void Backend::scanQrCode()
void Backend::scanQrCode()
{
{
// request permissions
m_qrScanner.reset(new QrScanner);
QCameraPermission permission;
connect(m_qrScanner.data(), &QrScanner::qrCodeScanned, this, [&](const QString &qrCode) {
QCoreApplication &app = *QCoreApplication::instance();
qDebug() << "QR code scanned:" << qrCode;
switch (app.checkPermission(permission)) {
parseDesignViewerUrl(QUrl(qrCode));
case Qt::PermissionStatus::Granted:
m_qrScanner.reset();
openCamera();
break;
case Qt::PermissionStatus::Undetermined:
case Qt::PermissionStatus::Denied:
app.requestPermission(permission, [this](const QPermission &permission) {
if (permission.status() == Qt::PermissionStatus::Denied) {
QMessageBox msgBox{QMessageBox::Critical,
"Critical:",
"Camera permission denied",
QMessageBox::Ok};
msgBox.exec();
return;
}
openCamera();
});
break;
}
}
void Backend::openCamera()
{
// start camera
m_captureSession.reset(new QMediaCaptureSession(this));
m_captureSession->setCamera(new QCamera(this));
m_captureSession->setVideoOutput(new CustomVideoWidget);
m_captureSession->camera()->setFocusMode(QCamera::FocusModeAuto);
CustomVideoWidget *videoWidget = qobject_cast<CustomVideoWidget *>(
m_captureSession->videoOutput());
connect(m_captureSession->videoSink(),
&QVideoSink::videoFrameChanged,
this,
[&](const QVideoFrame &frame) {
static int i = 0;
static QAtomicInt running = 0;
if (i++ < 20 || running > 0) {
return;
}
i = 0;
QtConcurrent::run([=] {
// lock the thread so that only one barcode can be read at a time
running++;
QElapsedTimer timer;
timer.start();
QList<Result> results = ReadBarcodes(frame.toImage());
qDebug() << "Barcode detection took" << timer.elapsed() << "ms";
// release the lock so that another barcode can be read
running--;
return results;
}).then([=](const QList<Result> &results) {
if (results.isEmpty() || !m_captureSession)
return;
qDebug() << "Stopping camera";
qobject_cast<CustomVideoWidget *>(m_captureSession->videoOutput())->close();
qDebug() << "Camera stopped";
Result result = results.first();
qDebug() << "Text: " << result.text();
qDebug() << "Format: " << result.format();
qDebug() << "Content:" << result.contentType();
// we have to use invokeMethod because we are in a different thread
// then where the serviceConnector is created
QMetaObject::invokeMethod(this,
"parseDesignViewerUrl",
Qt::QueuedConnection,
Q_ARG(QUrl, result.text()));
});
});
// stop camera when app is not active.
// i.e. when the user switches to another app or the screen is locked
QGuiApplication *app(qobject_cast<QGuiApplication *>(QCoreApplication::instance()));
connect(app, &QGuiApplication::applicationStateChanged, this, [&](Qt::ApplicationState state) {
if (state != Qt::ApplicationState::ApplicationActive && m_captureSession) {
qDebug() << "Application is not active. Stopping camera";
qobject_cast<CustomVideoWidget *>(m_captureSession->videoOutput())->close();
}
});
});
m_qrScanner->scanQrCode();
// stop camera when video widget is closed
connect(videoWidget, &CustomVideoWidget::closed, this, [&] {
qDebug() << "Video widget closed. Clearing capture session";
m_captureSession->camera()->stop();
m_captureSession->camera()->deleteLater();
m_captureSession->videoOutput()->deleteLater();
m_captureSession.clear();
});
videoWidget->setWindowFlags(Qt::Window | Qt::WindowStaysOnTopHint);
videoWidget->show();
m_captureSession->camera()->start();
}
}
void Backend::initializeProjectManager()
void Backend::initializeProjectManager()
@@ -382,9 +271,7 @@ void Backend::clearDemoCaches()
@@ -382,9 +271,7 @@ void Backend::clearDemoCaches()
{
{
emit popupOpen();
emit popupOpen();
updatePopup("Clearing demo caches...");
updatePopup("Clearing demo caches...");
m_projectManager.reset(new ProjectManager);
ProjectManager().clearDemoCaches();
m_projectManager->clearDemoCaches();
m_projectManager.reset();
emit popupClose();
emit popupClose();
}
}
@@ -500,6 +387,16 @@ void Backend::updateUserProjectList()
@@ -500,6 +387,16 @@ void Backend::updateUserProjectList()
const QString projectName{project.toObject().value("appName").toString()};
const QString projectName{project.toObject().value("appName").toString()};
qDebug() << "--" << projectName;
qDebug() << "--" << projectName;
}
}
 
 
// check if any project is removed on the cloud
 
for (const auto &project : m_projectList) {
 
const QString projectName{project.toObject().value("appName").toString()};
 
if (!projectList.value().contains(project)) {
 
qDebug() << "Project removed:" << projectName << ". Removing from cache...";
 
// remove the project from the cache
 
ProjectManager().clearCachedProject(project.toObject());
 
}
 
}
}
}
// we need to set m_projectList even if it is empty
// we need to set m_projectList even if it is empty
Loading