2018-11-03 18:07:45 +01:00
|
|
|
#include "./internalerrorsdialog.h"
|
2017-08-20 01:20:47 +02:00
|
|
|
|
2021-05-21 22:35:28 +02:00
|
|
|
#include <c++utilities/io/ansiescapecodes.h>
|
|
|
|
|
2017-08-20 01:20:47 +02:00
|
|
|
#include <QHBoxLayout>
|
|
|
|
#include <QIcon>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QStringBuilder>
|
|
|
|
#include <QTextBrowser>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
|
|
|
#include <algorithm>
|
2017-08-20 01:57:56 +02:00
|
|
|
#include <iostream>
|
2017-08-20 01:20:47 +02:00
|
|
|
#include <limits>
|
|
|
|
|
|
|
|
using namespace std;
|
2019-06-10 22:48:26 +02:00
|
|
|
using namespace CppUtilities;
|
2017-08-20 01:20:47 +02:00
|
|
|
using namespace Data;
|
|
|
|
|
|
|
|
namespace QtGui {
|
|
|
|
|
2018-11-03 18:07:45 +01:00
|
|
|
InternalErrorsDialog *InternalErrorsDialog::s_instance = nullptr;
|
|
|
|
std::vector<InternalError> InternalErrorsDialog::s_internalErrors;
|
2017-08-20 01:20:47 +02:00
|
|
|
|
2018-11-03 18:07:45 +01:00
|
|
|
InternalErrorsDialog::InternalErrorsDialog()
|
2017-08-20 01:20:47 +02:00
|
|
|
: TextViewDialog(tr("Internal errors"))
|
|
|
|
, m_request(tr("Request URL:"))
|
|
|
|
, m_response(tr("Response:"))
|
|
|
|
, m_statusLabel(new QLabel(this))
|
|
|
|
{
|
|
|
|
if (!s_instance) {
|
|
|
|
s_instance = this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add layout to show status and additional buttons
|
2017-10-01 21:20:25 +02:00
|
|
|
auto *const buttonLayout = new QHBoxLayout;
|
2019-06-25 13:49:53 +02:00
|
|
|
buttonLayout->setContentsMargins(0, 0, 0, 0);
|
2017-08-20 01:20:47 +02:00
|
|
|
|
|
|
|
// add label for overall status
|
|
|
|
QFont boldFont(m_statusLabel->font());
|
|
|
|
boldFont.setBold(true);
|
|
|
|
m_statusLabel->setFont(boldFont);
|
|
|
|
buttonLayout->addWidget(m_statusLabel);
|
|
|
|
updateStatusLabel();
|
|
|
|
|
|
|
|
// add errors to text view
|
|
|
|
for (const InternalError &error : s_internalErrors) {
|
|
|
|
internalAddError(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a button for clearing errors
|
|
|
|
if (!s_internalErrors.empty()) {
|
|
|
|
auto *const clearButton = new QPushButton(this);
|
|
|
|
clearButton->setText(tr("Clear errors"));
|
|
|
|
clearButton->setIcon(QIcon::fromTheme(QStringLiteral("edit-clear")));
|
2019-06-25 13:49:53 +02:00
|
|
|
buttonLayout->setContentsMargins(0, 0, 0, 0);
|
2017-08-20 01:20:47 +02:00
|
|
|
buttonLayout->addItem(new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum));
|
|
|
|
buttonLayout->addWidget(clearButton);
|
2018-11-03 18:07:45 +01:00
|
|
|
connect(clearButton, &QPushButton::clicked, &InternalErrorsDialog::clearErrors);
|
|
|
|
connect(clearButton, &QPushButton::clicked, this, &InternalErrorsDialog::errorsCleared);
|
2017-08-20 01:20:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
layout()->addItem(buttonLayout);
|
|
|
|
}
|
|
|
|
|
2018-11-03 18:07:45 +01:00
|
|
|
InternalErrorsDialog::~InternalErrorsDialog()
|
2017-08-20 01:20:47 +02:00
|
|
|
{
|
|
|
|
if (s_instance == this) {
|
|
|
|
s_instance = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 18:07:45 +01:00
|
|
|
void InternalErrorsDialog::addError(InternalError &&newError)
|
2017-08-20 01:20:47 +02:00
|
|
|
{
|
|
|
|
s_internalErrors.emplace_back(newError);
|
|
|
|
if (s_instance) {
|
|
|
|
s_instance->internalAddError(s_internalErrors.back());
|
|
|
|
s_instance->updateStatusLabel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 01:14:33 +02:00
|
|
|
void InternalErrorsDialog::addError(const QString &message, const QUrl &url, const QByteArray &response)
|
|
|
|
{
|
|
|
|
s_internalErrors.emplace_back(message, url, response);
|
|
|
|
if (s_instance) {
|
|
|
|
s_instance->internalAddError(s_internalErrors.back());
|
|
|
|
s_instance->updateStatusLabel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-03 18:07:45 +01:00
|
|
|
void InternalErrorsDialog::internalAddError(const InternalError &error)
|
2017-08-20 01:20:47 +02:00
|
|
|
{
|
2021-05-21 22:35:28 +02:00
|
|
|
const QString url = error.url.toString();
|
2021-10-07 18:18:08 +02:00
|
|
|
auto *const b = browser();
|
|
|
|
b->append(QChar('[') % QString::fromUtf8(error.when.toString(DateTimeOutputFormat::Iso, true).data()) % QChar(']') % QChar(' ') % error.message);
|
2021-05-21 22:35:28 +02:00
|
|
|
if (!url.isEmpty()) {
|
2021-10-07 18:18:08 +02:00
|
|
|
b->append(m_request % QChar(' ') % url);
|
2021-05-21 22:35:28 +02:00
|
|
|
}
|
|
|
|
if (!error.response.isEmpty()) {
|
2021-10-07 18:18:08 +02:00
|
|
|
b->append(m_response % QChar('\n') % QString::fromLocal8Bit(error.response));
|
2021-05-21 22:35:28 +02:00
|
|
|
}
|
2017-08-20 01:57:56 +02:00
|
|
|
|
|
|
|
// also log errors to console
|
2021-05-21 22:35:28 +02:00
|
|
|
using namespace EscapeCodes;
|
|
|
|
cerr << Phrases::Error << error.message.toLocal8Bit().data() << Phrases::End;
|
2017-08-20 01:57:56 +02:00
|
|
|
if (!error.url.isEmpty()) {
|
2021-05-21 22:35:28 +02:00
|
|
|
cerr << "request URL: " << url.toLocal8Bit().data() << '\n';
|
2017-08-20 01:57:56 +02:00
|
|
|
}
|
|
|
|
if (!error.response.isEmpty()) {
|
2021-05-21 22:35:28 +02:00
|
|
|
cerr << "response: " << error.response.data() << '\n';
|
2017-08-20 01:57:56 +02:00
|
|
|
}
|
2017-08-20 01:20:47 +02:00
|
|
|
}
|
|
|
|
|
2018-11-03 18:07:45 +01:00
|
|
|
void InternalErrorsDialog::updateStatusLabel()
|
2017-08-20 01:20:47 +02:00
|
|
|
{
|
2021-07-03 19:29:49 +02:00
|
|
|
m_statusLabel->setText(tr("%1 error(s) occurred", nullptr, static_cast<int>(min<size_t>(s_internalErrors.size(), numeric_limits<int>::max())))
|
2017-08-20 01:20:47 +02:00
|
|
|
.arg(s_internalErrors.size()));
|
|
|
|
}
|
|
|
|
|
2018-11-03 18:07:45 +01:00
|
|
|
void InternalErrorsDialog::clearErrors()
|
2017-08-20 01:20:47 +02:00
|
|
|
{
|
|
|
|
s_internalErrors.clear();
|
|
|
|
if (s_instance) {
|
|
|
|
s_instance->updateStatusLabel();
|
|
|
|
s_instance->browser()->clear();
|
|
|
|
}
|
|
|
|
}
|
2017-09-17 21:48:15 +02:00
|
|
|
} // namespace QtGui
|