The Logs screen only offered /var/log/pacman.log, which is unreadable without root and says nothing about what the app itself did. Let the user switch between that file and the new application journal, refresh it live while an operation runs, and record navigation and dialog events so the screen tells the whole story. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
111 lines
3.4 KiB
C++
111 lines
3.4 KiB
C++
#include "logs-page.h"
|
|
|
|
#include <QComboBox>
|
|
#include <QHBoxLayout>
|
|
#include <QLineEdit>
|
|
#include <QPushButton>
|
|
#include <QTextCursor>
|
|
#include <QTextEdit>
|
|
#include <QVBoxLayout>
|
|
|
|
#include "log-manager.h"
|
|
#include "ui-kit.h"
|
|
|
|
LogsPage::LogsPage(AppCore* core, QWidget* parent) : Page(core, parent) {
|
|
QVBoxLayout* layout = new QVBoxLayout(this);
|
|
layout->setContentsMargins(24, 20, 24, 20);
|
|
layout->setSpacing(12);
|
|
|
|
QHBoxLayout* filterRow = new QHBoxLayout();
|
|
filterRow->setSpacing(10);
|
|
|
|
m_source = new QComboBox();
|
|
m_source->addItem(QStringLiteral("Journal de l'application"));
|
|
m_source->addItem(QStringLiteral("Journal pacman (/var/log/pacman.log)"));
|
|
m_source->setMinimumHeight(36);
|
|
m_source->setToolTip("Choisir la source du journal affiché");
|
|
connect(m_source, &QComboBox::currentIndexChanged, this, [this](int) { load(); });
|
|
filterRow->addWidget(m_source);
|
|
|
|
m_filter = new QLineEdit();
|
|
m_filter->setPlaceholderText("Filtrer les logs (paquet, action, date)...");
|
|
m_filter->setMinimumHeight(36);
|
|
m_filter->setClearButtonEnabled(true);
|
|
m_filter->addAction(ui::icon("filter"), QLineEdit::LeadingPosition);
|
|
connect(m_filter, &QLineEdit::textChanged, this, [this](const QString&) {
|
|
applyFilter();
|
|
});
|
|
filterRow->addWidget(m_filter, 1);
|
|
|
|
layout->addLayout(filterRow);
|
|
|
|
m_text = new QTextEdit();
|
|
m_text->setReadOnly(true);
|
|
layout->addWidget(m_text, 1);
|
|
|
|
QHBoxLayout* buttonBar = new QHBoxLayout();
|
|
buttonBar->addStretch();
|
|
|
|
QPushButton* refreshButton = new QPushButton("Recharger");
|
|
refreshButton->setIcon(ui::icon("refresh"));
|
|
connect(refreshButton, &QPushButton::clicked, this, [this] {
|
|
load();
|
|
emit message(tr("Logs rechargés"));
|
|
});
|
|
buttonBar->addWidget(refreshButton);
|
|
|
|
layout->addLayout(buttonBar);
|
|
|
|
// The journal grows while the app runs: reload it whenever a new entry lands,
|
|
// but only while this page is actually showing it (so an install does not
|
|
// reload the text on a screen the user is not looking at).
|
|
connect(&LogManager::instance(), &LogManager::fileUpdated, this, [this] {
|
|
if (viewingAppLog() && isVisible()) load();
|
|
});
|
|
}
|
|
|
|
bool LogsPage::viewingAppLog() const {
|
|
return m_source->currentIndex() == 0;
|
|
}
|
|
|
|
void LogsPage::refresh() {
|
|
load();
|
|
}
|
|
|
|
void LogsPage::load() {
|
|
QString error;
|
|
m_raw = viewingAppLog() ? LogManager::instance().readFile(&error)
|
|
: core()->readPackageLog(&error);
|
|
|
|
if (!error.isEmpty()) {
|
|
m_text->setPlainText(error);
|
|
return;
|
|
}
|
|
if (m_raw.isEmpty()) {
|
|
m_text->setPlainText(viewingAppLog()
|
|
? QStringLiteral("Journal vide : rien n'a encore été enregistré.")
|
|
: QStringLiteral("Journal pacman vide ou illisible."));
|
|
return;
|
|
}
|
|
applyFilter();
|
|
}
|
|
|
|
// Renders the raw log, keeping only the lines matching the filter box.
|
|
void LogsPage::applyFilter() {
|
|
const QString needle = m_filter->text().trimmed();
|
|
if (needle.isEmpty()) {
|
|
m_text->setPlainText(m_raw);
|
|
} else {
|
|
QString filtered;
|
|
const QStringList lines = m_raw.split(QLatin1Char('\n'));
|
|
for (const QString& line : lines) {
|
|
if (line.contains(needle, Qt::CaseInsensitive)) {
|
|
filtered += line;
|
|
filtered += QLatin1Char('\n');
|
|
}
|
|
}
|
|
m_text->setPlainText(filtered);
|
|
}
|
|
m_text->moveCursor(QTextCursor::End);
|
|
}
|