Write temporary file only when viewed in PDF viewer

This commit is contained in:
Daniel Brunner committed 2018-11-18 15:26:00 +01:00
1 parent 79f9fb451b
commit f0061c88a1
4 files changed
+57 -53

No files matched your search

+35 -31
View File
@@ -1,5 +1,6 @@
#include "reportswidget.h"
// Qt includes
#include <QStatusBar>
#include <QMenu>
#include <QToolBar>
@@ -11,7 +12,13 @@
#include <QDir>
#include <QDesktopServices>
// dbcorelib includes
#include "utils/timeutils.h"
// zeiterfassungguilib includes
#include "mainwindow.h"
// zeiterfassungnetworklib includes
#include "zeiterfassungapi.h"
ReportsWidget::ReportsWidget(MainWindow &mainWindow) :
@@ -47,7 +54,7 @@ void ReportsWidget::dateChanged(const QDate &date)
return;
}
auto monthBegin = QDate(date.year(), date.month(), 1);
const auto monthBegin = beginOfMonth(date);
if(monthBegin != m_date)
{
m_date = monthBegin;
@@ -65,6 +72,7 @@ void ReportsWidget::refresh()
setText(tr("Balance: %0, Holidays: %1").arg(tr("???")).arg(tr("???")));
m_content.clear();
m_actionRefreshReport->setEnabled(false);
m_actionOpenReport->setEnabled(false);
@@ -74,21 +82,15 @@ void ReportsWidget::refresh()
void ReportsWidget::finished()
{
if(!m_reply->success())
if(m_reply->success())
{
m_date = QDate();
QMessageBox::warning(this, tr("Could not load report!"), tr("Could not load report!") % "\n\n" % m_reply->message());
goto after;
}
{
auto content = m_reply->content();
m_content = m_reply->content();
QString balance;
{
static QRegularExpression regex(QStringLiteral("Gleitzeit +([0-9]+\\:[0-9]+\\-?) +([0-9]+\\:[0-9]+\\-?)"));
auto match = regex.match(content);
static const QRegularExpression regex(QStringLiteral("Gleitzeit +([0-9]+\\:[0-9]+\\-?) +([0-9]+\\:[0-9]+\\-?)"));
const auto match = regex.match(m_content);
if(match.hasMatch())
{
balance = match.captured(2);
@@ -108,8 +110,8 @@ void ReportsWidget::finished()
QString holidays;
{
static QRegularExpression regex(QStringLiteral("Urlaubsanspruch +(\\-?[0-9]+\\.[0-9]+) +(\\-?[0-9]+\\.[0-9]+)"));
auto match = regex.match(content);
static const QRegularExpression regex(QStringLiteral("Urlaubsanspruch +(\\-?[0-9]+\\.[0-9]+) +(\\-?[0-9]+\\.[0-9]+)"));
const auto match = regex.match(m_content);
if(match.hasMatch())
holidays = match.captured(2);
else
@@ -120,31 +122,33 @@ void ReportsWidget::finished()
}
setText(tr("Balance: %0, Holidays: %1").arg(balance).arg(holidays));
{
QTemporaryFile file(QDir::temp().absoluteFilePath(QStringLiteral("reportXXXXXX.pdf")));
file.setAutoRemove(false);
if(!file.open())
{
QMessageBox::warning(this, tr("Could not write report!"), tr("Could not write report!") % "\n\n" % file.errorString());
goto after;
}
file.write(content);
m_url = QUrl::fromLocalFile(file.fileName());
}
m_actionOpenReport->setEnabled(true);
}
else
QMessageBox::warning(this, tr("Could not load report!"), tr("Could not load report!") % "\n\n" % m_reply->message());
m_actionOpenReport->setEnabled(true);
after:
m_actionRefreshReport->setEnabled(true);
m_reply = Q_NULLPTR;
}
void ReportsWidget::openReport()
{
if(!QDesktopServices::openUrl(m_url))
QUrl url;
{
QTemporaryFile file(QDir::temp().absoluteFilePath(QStringLiteral("reportXXXXXX.pdf")));
file.setAutoRemove(false);
if(!file.open())
{
QMessageBox::warning(this, tr("Could not write report!"), tr("Could not write report!") % "\n\n" % file.errorString());
return;
}
file.write(m_content);
url = QUrl::fromLocalFile(file.fileName());
}
if(!QDesktopServices::openUrl(url))
QMessageBox::warning(this, tr("Could not launch your default PDF viewer!"), tr("Could not launch your default PDF viewer!"));
}