Files

68 lines
1.9 KiB
C++
Raw Permalink Normal View History

2017-12-19 18:52:48 +01:00
#include "lunchmealwidget.h"
#include <QIcon>
2017-12-23 11:10:57 +01:00
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QUrl>
#include <QMessageBox>
#include <QStringBuilder>
2017-12-19 18:52:48 +01:00
#include "stripswidget.h"
#include "mainwindow.h"
#include "zeiterfassungapi.h"
2017-12-19 18:59:49 +01:00
#include "lunchmealdialog.h"
2018-03-11 15:27:39 +01:00
#include "lunchmealsettings.h"
2017-12-19 18:59:49 +01:00
2017-12-19 18:52:48 +01:00
LunchMealWidget::LunchMealWidget(StripsWidget &stripsWidget) :
QToolButton(&stripsWidget),
m_stripsWidget(stripsWidget)
{
setIcon(QIcon(QStringLiteral(":/zeiterfassung/plugins/lunchmealplugin/images/lunch-meal.png")));
setText(tr("Lunch meal"));
connect(this, &QAbstractButton::pressed, this, &LunchMealWidget::pressedSlot);
2017-12-23 11:10:57 +01:00
connect(&m_stripsWidget, &StripsWidget::dateChanged, this, &LunchMealWidget::dateChanged);
dateChanged(m_stripsWidget.date());
2017-12-19 18:52:48 +01:00
}
void LunchMealWidget::pressedSlot()
{
2017-12-28 17:12:08 +01:00
LunchMealDialog dialog(m_stripsWidget.date(), m_content, this);
2017-12-19 18:59:49 +01:00
dialog.exec();
2017-12-19 18:52:48 +01:00
}
2017-12-23 11:10:57 +01:00
void LunchMealWidget::dateChanged(const QDate &date)
{
setEnabled(false);
setVisible(false);
2018-03-11 15:27:39 +01:00
LunchMealSettings settings(m_stripsWidget.mainWindow().settings());
2017-12-23 11:10:57 +01:00
2018-03-16 12:26:56 +01:00
auto url = QUrl(settings.url().arg(date.toString(settings.dateFormat())));
m_reply = std::unique_ptr<QNetworkReply>(m_stripsWidget.mainWindow().erfassung().manager()->get(QNetworkRequest(url)));
2017-12-23 11:10:57 +01:00
connect(m_reply.get(), &QNetworkReply::finished, this, &LunchMealWidget::finished);
}
void LunchMealWidget::finished()
{
if(m_reply->error() != QNetworkReply::NoError &&
m_reply->error() != QNetworkReply::ContentNotFoundError)
{
QMessageBox::warning(&m_stripsWidget.mainWindow(), tr("Could not load lunch meal!"),
tr("Could not load lunch meal!") % "\n\n" % m_reply->errorString());
goto after;
}
if(m_reply->error() == QNetworkReply::NoError)
{
setEnabled(true);
setVisible(true);
m_content = m_reply->readAll();
}
after:
m_reply = Q_NULLPTR;
}