Don't duplicate code for showing log entries

This commit is contained in:
Martchus 2017-09-08 16:56:10 +02:00
parent 5046cb3eed
commit bae7f7c4c8
3 changed files with 26 additions and 12 deletions

View File

@ -236,12 +236,11 @@ void TrayWidget::showLog()
void TrayWidget::showNotifications()
{
auto *dlg = new TextViewDialog(tr("New notifications"), this);
for (const SyncthingLogEntry &entry : m_notifications) {
dlg->browser()->append(entry.when % QChar(':') % QChar(' ') % QChar('\n') % entry.message % QChar('\n'));
}
m_notifications.clear();
auto *const dlg = TextViewDialog::forLogEntries(m_notifications, tr("New notifications"));
dlg->setAttribute(Qt::WA_DeleteOnClose, true);
centerWidget(dlg);
showDialog(dlg);
m_notifications.clear();
dismissNotifications();
}

View File

@ -21,9 +21,11 @@
#include <QVBoxLayout>
#include <algorithm>
#include <functional>
#include <limits>
using namespace std;
using namespace std::placeholders;
using namespace Dialogs;
using namespace Data;
@ -131,19 +133,20 @@ TextViewDialog *TextViewDialog::forLogEntries(SyncthingConnection &connection)
auto *const dlg = new TextViewDialog(tr("Log"));
const auto loadLog = [dlg, &connection] {
connect(dlg, &QWidget::destroyed, bind(static_cast<bool (*)(const QMetaObject::Connection &)>(&QObject::disconnect),
connection.requestLog([dlg](const std::vector<SyncthingLogEntry> &entries) {
dlg->browser()->clear();
for (const SyncthingLogEntry &entry : entries) {
dlg->browser()->append(
entry.when % QChar(':') % QChar(' ') % QChar('\n') % entry.message % QChar('\n'));
}
})));
connection.requestLog(bind(&TextViewDialog::showLogEntries, dlg, _1))));
};
connect(dlg, &TextViewDialog::reload, loadLog);
loadLog();
return dlg;
}
TextViewDialog *TextViewDialog::forLogEntries(const std::vector<SyncthingLogEntry> &logEntries, const QString &title)
{
auto *const dlg = new TextViewDialog(title.isEmpty() ? tr("Log") : title);
dlg->showLogEntries(logEntries);
return dlg;
}
void TextViewDialog::keyPressEvent(QKeyEvent *event)
{
switch (event->key()) {
@ -156,4 +159,12 @@ void TextViewDialog::keyPressEvent(QKeyEvent *event)
default:;
}
}
void TextViewDialog::showLogEntries(const std::vector<SyncthingLogEntry> &logEntries)
{
browser()->clear();
for (const SyncthingLogEntry &entry : logEntries) {
browser()->append(entry.when % QChar(':') % QChar(' ') % QChar('\n') % entry.message % QChar('\n'));
}
}
}

View File

@ -10,6 +10,7 @@ QT_FORWARD_DECLARE_CLASS(QTextBrowser)
namespace Data {
class SyncthingConnection;
struct SyncthingDir;
struct SyncthingLogEntry;
}
namespace QtGui {
@ -22,6 +23,7 @@ public:
QTextBrowser *browser();
static TextViewDialog *forDirectoryErrors(const Data::SyncthingDir &dir);
static TextViewDialog *forLogEntries(Data::SyncthingConnection &connection);
static TextViewDialog *forLogEntries(const std::vector<Data::SyncthingLogEntry> &logEntries, const QString &title = QString());
Q_SIGNALS:
void reload();
@ -30,6 +32,8 @@ protected:
void keyPressEvent(QKeyEvent *event);
private:
void showLogEntries(const std::vector<Data::SyncthingLogEntry> &logEntries);
QTextBrowser *m_browser;
};