/* * \copyright Copyright (c) 2017-2018 Governikus GmbH & Co. KG, Germany */ #include "PdfExporter.h" #include "AppSettings.h" #include "LanguageLoader.h" #include "PdfCreator.h" #include using namespace governikus; namespace { QString getValueOrWhitespace(const QStringList& pValues, int i) { if (i < pValues.size() && !pValues.at(i).isEmpty()) { return pValues.at(i); } return QStringLiteral(" "); } } PdfExporter::PdfExporter(const QString& pFilename, bool pOpenFile, bool pFixFilename) : mFilename(pFilename) , mOpenFile(pOpenFile) , mColoredRow(false) , mColumnCount(0) , mContent() { if (pFixFilename && !mFilename.isEmpty() && !mFilename.endsWith(QLatin1String(".pdf"), Qt::CaseInsensitive)) { mFilename += QStringLiteral(".pdf"); } } QString PdfExporter::getContent() const { return mContent.join(QString()); } void PdfExporter::checkOpenFile(bool pSuccess) { if (mOpenFile && pSuccess) { QDesktopServices::openUrl(QUrl(QStringLiteral("file:///") + mFilename)); } } void PdfExporter::initTable(int pColumnCount, const QList& pWidth, const QStringList& pValues) { Q_ASSERT(mColumnCount == 0); mColumnCount = pColumnCount; mContent << QStringLiteral(""); for (int i = 0; i < mColumnCount; ++i) { const auto& width = i < pWidth.size() ? QStringLiteral(" width='%1'").arg(pWidth.at(i)) : QString(); mContent << QStringLiteral("").arg(width, getValueOrWhitespace(pValues, i)); } mContent << QStringLiteral(""); } void PdfExporter::addTableRow(const QStringList& pValues) { mContent << (mColoredRow ? QStringLiteral("") : QStringLiteral("")); for (int i = 0; i < mColumnCount; ++i) { mContent << QStringLiteral("").arg(getValueOrWhitespace(pValues, i)); } mContent << QStringLiteral(""); } void PdfExporter::closeTable() { mContent << QStringLiteral("
%2
%2
"); mColumnCount = 0; mColoredRow = false; } void PdfExporter::toggleRowColor() { mColoredRow = !mColoredRow; } bool PdfExporter::exportHistory() { if (mFilename.isEmpty()) { return false; } mContent.clear(); const auto& locale = LanguageLoader::getInstance().getUsedLocale(); initTable(3, {180, 80}, {tr("Date"), tr("Details")}); const auto& dateTimeFormat = tr("dd.MM.yyyy hh:mm AP"); const auto& infos = AppSettings::getInstance().getHistorySettings().getHistoryInfos(); for (const auto& entry : infos) { toggleRowColor(); const QString& dateTimeEntry = locale.toString(entry.getDateTime(), dateTimeFormat); addTableRow({dateTimeEntry, tr("Provider:"), entry.getSubjectName()}); addTableRow({QString(), tr("Purpose:"), entry.getPurpose()}); addTableRow({QString(), tr("Data:"), entry.getRequestedData()}); } closeTable(); const auto& now = QDateTime::currentDateTime(); QString date = locale.toString(now, tr("dd.MM.yyyy")); QString time = locale.toString(now, tr("hh:mm AP")); const auto& headline = tr("At %1 %2 the following data were saved:").arg(date, time); PdfCreator pdf(mFilename, tr("History"), headline, getContent()); const bool success = pdf.save(); checkOpenFile(success); return success; } bool PdfExporter::exportSelfInfo(const QDateTime& pDate, const QVector >& pInfoData) { if (mFilename.isEmpty()) { return false; } mContent.clear(); initTable(2, {180}, {tr("Entry"), tr("Content")}); for (const auto& entry : pInfoData) { if (!entry.first.isEmpty()) { toggleRowColor(); } addTableRow({entry.first, entry.second}); } closeTable(); const auto& locale = LanguageLoader::getInstance().getUsedLocale(); QString date = locale.toString(pDate, tr("dd.MM.yyyy")); QString time = locale.toString(pDate, tr("hh:mm AP")); const auto& headline = tr("At %1 %2 the following data has been read out of your ID card:").arg(date, time); PdfCreator pdf(mFilename, tr("Information"), headline, getContent()); const bool success = pdf.save(); checkOpenFile(success); return success; }