Files
qt-migration-legatus/qt6-after/qt6-migrated-legatus/Lts/Services/OcBaseDownload/pdf_downloader.cpp
T
torstenandCopilot b774f4035c Qt6 Migration: DDS-Stubs, Logger-Fixes, Export-Header Korrekturen
- NGL_ADD_LIBRARY2: 87 CMakeLists.txt konvertiert für Export-Header Generierung
- 44 Export-Header Include-Pfade korrigiert (_export.h)
- 81 DDS-Stub-Header generiert (*_dtc.h, *_fts.h, *_dts.h, *_ftc.h)
- INGLLogger: Duplikat gelöscht, m_logger Member entfernt, variadische Makros
- Q_FUNC_INFO Redefinition behoben (Qt6 definiert bereits)
- FATAL_LOG Makro hinzugefügt
- InfrastructureManagement DDS-Stubs (ism.hpp, ism_enums.hpp, ism_ftc.h)
- FilterManagement DDS-Stubs (flt_ftc.h, flt_dts.h)
- AreaEffectSimulation DDS-Stubs (aes.hpp, aes_enums.hpp)
- SymbolAppearance Klasse vollständig definiert mit content() Methode
- BuildingComponentFilter Klasse definiert mit Copy-Konstruktor
- Ftc Factory: nullptr Return (abstract class)
- AesFilter Klasse in aes.hpp hinzugefügt
- Dtc Klasse in ppm.hpp mit participantChanged Signal
- AesType Enum: alle 15 At* Werte definiert
- AesEffectState_def: EsReceived hinzugefügt
- DataModification Enum: eAdded/eModified/eRemoved
- function_reply_object.h → core_types.h Include-Pfad (72 Dateien)
- Enum-Class-Streaming Template-Operator in INGLLogger.h
- ism_mounting_point.h: QImage → QtGui/QImage, QGuiApplication/QScreen
- ism_data_plugin_interface.h: QSet Include, Include-Reihenfolge
- QRegExp → QRegularExpression in tpm_js_parser.cpp
- .underlying() Aufrufe zu direktem Enum-Vergleich korrigiert
- Include-Pfade: aes.hpp in symbol_appearance_settings_impl.h und aes_display_filter_impl.h

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-08-19 06:06:24 +02:00

191 lines
5.1 KiB
C++
Executable File

/**
* @RDE_HEADER@
* $Id$
* $HeadURL$
* $Author$
* Created by Hannes Fritsche, NLE, 2019-04-30
*/
#include "pdf_downloader.h"
#include "pdf_download_socket.h"
#include "main_handler.h"
#include <nglsettings/nglsettings_settings.h>
#include <ngldb/dbconnection.h>
#include <ngldb/dbtransaction.h>
#include <ngldb/db_service_handler.h>
#include <ngltools/ngltools_filesystem.h>
#include <QJsonDocument>
namespace OcBaseDownload
{
PdfDownloader::PdfDownloader(const QVector<WebSocketInterface::Messages::PdfListMessage::PdfInformation> &pdfInfoVec, MainHandler *mainHandler, const QString &hostName, int exerciseId, QObject *parent)
: QObject(parent)
, m_pdfInfoVec(pdfInfoVec)
, m_socket(new PdfDownloadSocket(hostName, this))
, m_mainHandler(mainHandler)
, m_exerciseId(exerciseId)
, m_exTblGen(new DBAdministration::ExerciseTableGenerator(getLogger("ExerciseTableGenerator"), NGL::directory(NGL::FileType::ConfigFile, QLatin1String("xml")), this))
, m_downloadCount(0)
, m_totalDownloads(pdfInfoVec.size())
{
connect(m_socket, &PdfDownloadSocket::downloadFinished,
this, &PdfDownloader::onSingleDownloadFinished);
}
PdfDownloader::~PdfDownloader()
{
}
bool PdfDownloader::startDownload()
{
DEBUG_LOG() << "Downloading startet";
if (!m_socket->isConnected())
{
ERROR_LOG() << "Download could not be startet. Socket not connected.";
return false;
}
if (!openDbConnection())
{
ERROR_LOG() << "Could not open database connection";
return false;
}
return startNextDownload();
}
void PdfDownloader::onSingleDownloadFinished(const QByteArray &data)
{
DEBUG_LOG() << "Downloading file " << m_currentDownload.name << " finished.";
++m_downloadCount;
if (!data.size())
WARNING_LOG() << "Got finished download without data";
QString msg = QString::fromUtf8(data);
if (msg.startsWith(QStringLiteral("ERROR")))
{
ERROR_LOG() << msg;
startNextDownload();
return;
}
if (!m_dbConnection.isValid())
{
ERROR_LOG() << "Database not open";
return;
}
auto flmDbHandler = std::unique_ptr<NGLDB::DbServiceHandler<FileManagement::DatabaseInterface>>(new NGLDB::DbServiceHandler<FileManagement::DatabaseInterface>());
if (!flmDbHandler->init(0, m_dbConnection))
{
ERROR_LOG() << "Couldn't initialise flmDbHandler";
return;
}
DBTransaction trans(m_dbConnection);
FileManagement::Database::FlmFileContent dbData;
dbData.name = m_currentDownload.name;
dbData.isTemplate = m_currentDownload.isTemplate;
dbData.folderId = m_currentDownload.folderId;
dbData.fileCategoryId = m_currentDownload.fileCategory;
dbData.fileTypeId = FileManagement::FileType::Pdf;
dbData.fileStatusId = m_currentDownload.fileStatus;
if (m_currentDownload.exerciseId)
dbData.exerciseId = m_currentDownload.exerciseId;
if (m_currentDownload.phaseId)
dbData.phaseId = m_currentDownload.phaseId;
dbData.operatorId = m_currentDownload.operatorId;
dbData.content = data;
dbData.properties = QString::fromLatin1(QJsonDocument(m_currentDownload.properties.toObject()).toJson(QJsonDocument::Compact));
flmDbHandler->db()->insert(dbData);
if (!trans.commit())
ERROR_LOG() << "Error while inserting FLM data into database.";
startNextDownload();
}
bool PdfDownloader::startNextDownload()
{
if (m_pdfInfoVec.isEmpty())
{
DEBUG_LOG() << "No more pending downloads.";
closeDb(m_dbConnection);
emit allDownloadsFinished();
return false;
}
m_currentDownload = m_pdfInfoVec.takeFirst();
emit newState(100 / m_totalDownloads * m_downloadCount, tr("Downloading file \"%1\"").arg(m_currentDownload.name));
DEBUG_LOG() << "Downloading file " << m_currentDownload.name;
m_socket->downloadFromHost(m_currentDownload.id, m_currentDownload.fileSize);
return true;
}
bool PdfDownloader::openDbConnection()
{
if (m_dbConnection.isValid())
{
m_exTblGen->close();
closeDb(m_dbConnection);
}
bool bOk;
m_dbConnection = openDb(&bOk);
if (!bOk)
return false;
return m_exTblGen->init(m_dbConnection, QStringLiteral("exm"));
}
QSqlDatabase PdfDownloader::openDb(bool *bOk)
{
if (bOk)
*bOk = true;
// open postgres db, read table structure, read static data
auto s = Settings::settings();
const QString dbUser(QStringLiteral("ets_flm"));
const QString dbPasswd(QStringLiteral("ets11de"));
QSqlDatabase dbConnection = DBConnection::open(m_logger,
dbUser,
dbPasswd,
s->databaseDriver(),
s->databaseHostname(),
s->databaseName());
DEBUG_LOG() << "Trying to open database " << s->databaseHostname() << ":" << s->databaseName();
if (!dbConnection.isOpen())
{
ERROR_LOG() << "Error opening database " << s->databaseHostname() << ":" << s->databaseName();
if (bOk)
*bOk = false;
}
return dbConnection;
}
void PdfDownloader::closeDb(QSqlDatabase &dbConn)
{
const QString connName = dbConn.connectionName();
DEBUG_LOG() << "Closing database: " << connName;
dbConn.close();
dbConn = QSqlDatabase();
}
}