191 lines
6.5 KiB
C++
Executable File
191 lines
6.5 KiB
C++
Executable File
/**
|
|
* @RDE_HEADER@
|
|
* Call Libre/MS Office
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
*/
|
|
|
|
#include "flm_office_integration.h"
|
|
#include <LtsSession/lts_session.h>
|
|
#include <LtsUtilsSettings/settings.h>
|
|
#include <nglsettings/nglsettings_settings.h>
|
|
#include <ngltools/ngltools_filesystem.h>
|
|
#include "settings.h"
|
|
|
|
#include <QFileInfo>
|
|
#include <QProcess>
|
|
#include <QTemporaryFile>
|
|
|
|
namespace FileManagement
|
|
{
|
|
|
|
OfficeIntegration::OfficeIntegration(const FileManagement::FileContent &fileContent, QObject *parent)
|
|
: QObject(parent)
|
|
, m_selectedFileContent(fileContent)
|
|
, m_logger(getLogger("FileManagementOfficeIntegration"))
|
|
{}
|
|
|
|
OfficeIntegration::~OfficeIntegration()
|
|
{
|
|
delete m_icaFile;
|
|
}
|
|
|
|
bool OfficeIntegration::openOfficeWindow()
|
|
{
|
|
m_errorString.clear();
|
|
const bool bUseMSOffice = LtsUtils::ProjectSettings::getInstance()->value(QStringLiteral("Office"), QStringLiteral("Type")).toString() == QStringLiteral("MSOffice");
|
|
const bool bRet = bUseMSOffice ? callWfica() : callLibreOffice();
|
|
if (!bRet)
|
|
ERROR_LOG(m_logger) << m_errorString;
|
|
return bRet;
|
|
}
|
|
|
|
bool OfficeIntegration::callWfica()
|
|
{
|
|
const QString program = QStringLiteral("/opt/Citrix/ICAClient/wfica");
|
|
QFileInfo fi(program);
|
|
if (!fi.exists() || !fi.isExecutable() || !fi.isFile())
|
|
{
|
|
m_errorString = tr("Could not find wfica executable at '%1'").arg(program);
|
|
return false;
|
|
}
|
|
const QString icaFileTemplate(QStringLiteral("office.ica"));
|
|
const QString icaFileTemplatePath = NGL::findFile(NGL::FileType::ConfigFile, icaFileTemplate);
|
|
if (icaFileTemplatePath.isEmpty())
|
|
{
|
|
m_errorString = tr("Could not find ICA config file '%1'").arg(icaFileTemplate);
|
|
return false;
|
|
}
|
|
delete m_icaFile;
|
|
m_icaFile = new QTemporaryFile(this);
|
|
QFile f(icaFileTemplatePath);
|
|
if (!f.open(QIODevice::ReadOnly))
|
|
{
|
|
m_errorString = tr("Could not open ICA config file '%1' for reading").arg(icaFileTemplatePath);
|
|
return false;
|
|
}
|
|
if (!m_icaFile->open())
|
|
{
|
|
m_errorString = tr("Could not open temporary file (%1)").arg(m_icaFile->errorString());
|
|
return false;
|
|
}
|
|
QByteArray ba = f.readAll();
|
|
ba = ba.replace("@USERNAME@", getWfIcaUsername().toUtf8());
|
|
m_icaFile->write(ba);
|
|
m_icaFile->flush();
|
|
f.close();
|
|
|
|
QStringList arguments;
|
|
arguments << m_icaFile->fileName() << QStringLiteral("-param") << getParas();
|
|
|
|
QProcess *myProcess = new QProcess(this);
|
|
connect (myProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
|
|
this, &OfficeIntegration::deleteLater);
|
|
myProcess->start(program, arguments);
|
|
|
|
INFO_LOG(m_logger) << "icaFile: =" << m_icaFile->fileName() <<
|
|
", args: '" << arguments.join(QLatin1Char(' '));
|
|
DEBUG_LOG(m_logger) << "FileManagement::OfficeIntegration::callWfica(): " << myProcess->errorString();
|
|
return true;
|
|
}
|
|
|
|
bool OfficeIntegration::callLibreOffice()
|
|
{
|
|
startLibreOfficeServer();
|
|
// QString connString = QStringLiteral("jdbc:postgresql://localhost:5432/lts_ets_pro?user=postgres&password=postgres");
|
|
QString connString = QStringLiteral("jdbc:postgresql://%DB_HOST%:5432/%DB_NAME%?user=postgres&password=postgres");
|
|
|
|
auto s = Settings::settings();
|
|
connString.replace(QStringLiteral("%DB_NAME%"), s->databaseName());
|
|
connString.replace(QStringLiteral("%DB_HOST%"), s->databaseHostname());
|
|
|
|
DEBUG_LOG(m_logger) << "DB connection string: " << connString;
|
|
|
|
QString program = QStringLiteral("java");
|
|
QStringList arguments;
|
|
arguments << QStringLiteral("-jar") << QStringLiteral("/opt/libreOfficeAddin/RheinmetallLibreOfficeLauncher.jar")
|
|
<< QStringLiteral("--jdbcConnectionString") << connString
|
|
<< QStringLiteral("--operatorId") << QString::number(m_selectedFileContent.operatorId())
|
|
<< QStringLiteral("--documentId") << QString::number(m_selectedFileContent.id())
|
|
<< QStringLiteral("--fileCategoryId") << QString::number(m_selectedFileContent.fileCategory().underlying())
|
|
<< QStringLiteral("--libreOfficeHost") << QStringLiteral("localhost")
|
|
<< QStringLiteral("--libreOfficePort") << QStringLiteral("8100");
|
|
|
|
QProcess *myProcess = new QProcess(this);
|
|
connect(myProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished), myProcess, &QProcess::deleteLater);
|
|
connect(myProcess, &QProcess::errorOccurred, this, [this, myProcess](QProcess::ProcessError error)
|
|
{
|
|
ERROR_LOG(m_logger) << "FileManagement::OfficeIntegration::callLibreOffice(): " << error << " = " << myProcess->errorString();
|
|
});
|
|
|
|
myProcess->start(program, arguments);
|
|
return true;
|
|
}
|
|
|
|
void OfficeIntegration::startLibreOfficeServer()
|
|
{
|
|
QString program = QStringLiteral("soffice");
|
|
|
|
QStringList arguments;
|
|
arguments << QStringLiteral("--norestore") << QStringLiteral("--nodefault") << QStringLiteral("--accept=""socket,host=localhost,port=8100,tcpNoDelay=1;urp;""");
|
|
|
|
QProcess *myProcess = new QProcess(this);
|
|
connect (myProcess, QOverload<int, QProcess::ExitStatus>::of(&QProcess::finished),
|
|
this, &OfficeIntegration::deleteLater);
|
|
myProcess->start(program, arguments);
|
|
|
|
DEBUG_LOG(m_logger) << "FileManagement::OfficeIntegration::startLibreOfficeServer(): " << myProcess->errorString();
|
|
}
|
|
|
|
QString OfficeIntegration::getParas() const
|
|
{
|
|
QString paras;
|
|
paras += QStringLiteral("\"{");
|
|
paras += QStringLiteral("jdbcConnectionString:"); // should be odbcConnectionString...
|
|
paras += buildJdbcString();
|
|
paras += QStringLiteral(",");
|
|
|
|
paras += QStringLiteral("operatorId:");
|
|
paras += QString::number(m_selectedFileContent.operatorId());
|
|
paras += QStringLiteral(",");
|
|
|
|
paras += QStringLiteral("documentId:");
|
|
paras += QString::number(m_selectedFileContent.id());
|
|
paras += QStringLiteral(",");
|
|
|
|
paras += QStringLiteral("fileCategoryId:");
|
|
paras += QString::number(m_selectedFileContent.fileCategory().underlying());
|
|
|
|
if (m_selectedFileContent.exerciseId().is_set())
|
|
{
|
|
paras += QStringLiteral(",");
|
|
paras += QStringLiteral("exerciseId:");
|
|
paras += QString::number(m_selectedFileContent.exerciseId().get());
|
|
if (m_selectedFileContent.phaseId().is_set())
|
|
{
|
|
paras += QStringLiteral(",");
|
|
paras += QStringLiteral("phaseId:");
|
|
paras += QString::number(m_selectedFileContent.phaseId().get());
|
|
}
|
|
}
|
|
|
|
paras += QStringLiteral("}\"");
|
|
|
|
return paras;
|
|
}
|
|
|
|
QString OfficeIntegration::buildJdbcString() const
|
|
{
|
|
auto s = Settings::settings();
|
|
return QStringLiteral("DSN=%1_%2_PSQL").arg(s->databaseHostname(), s->databaseName());
|
|
}
|
|
|
|
QString OfficeIntegration::getWfIcaUsername() const
|
|
{
|
|
// TODO: make configurable?
|
|
return QStringLiteral("guez\\%1").arg(LtsSession::LtsSession::workstationName());
|
|
}
|
|
|
|
} // namespace FileManagement
|