2022-03-20 19:01:08 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
2022-12-14 15:02:04 +00:00
|
|
|
* Prism Launcher - Minecraft Launcher
|
2022-03-20 19:01:08 +00:00
|
|
|
* Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org>
|
2022-12-14 15:02:04 +00:00
|
|
|
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
|
2014-07-12 22:02:52 +01:00
|
|
|
*
|
2022-03-20 19:01:08 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3.
|
2014-07-12 22:02:52 +01:00
|
|
|
*
|
2022-03-20 19:01:08 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2014-07-12 22:02:52 +01:00
|
|
|
*
|
2022-03-20 19:01:08 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following copyright and
|
|
|
|
* permission notice:
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 MultiMC Contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2014-07-12 22:02:52 +01:00
|
|
|
*/
|
|
|
|
|
2014-07-12 16:58:23 +01:00
|
|
|
#include "OtherLogsPage.h"
|
|
|
|
#include "ui_OtherLogsPage.h"
|
|
|
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
2021-11-22 02:55:16 +00:00
|
|
|
#include "ui/GuiUtil.h"
|
|
|
|
|
2015-10-05 00:47:27 +01:00
|
|
|
#include <FileSystem.h>
|
2023-08-02 17:35:35 +01:00
|
|
|
#include <GZip.h>
|
2018-02-05 00:40:38 +00:00
|
|
|
#include <QShortcut>
|
2023-08-02 17:35:35 +01:00
|
|
|
#include "RecursiveFileSystemWatcher.h"
|
2014-07-12 16:58:23 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
OtherLogsPage::OtherLogsPage(QString path, IPathMatcher::Ptr fileFilter, QWidget* parent)
|
|
|
|
: QWidget(parent), ui(new Ui::OtherLogsPage), m_path(path), m_fileFilter(fileFilter), m_watcher(new RecursiveFileSystemWatcher(this))
|
2014-07-12 16:58:23 +01:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2014-07-20 23:10:13 +01:00
|
|
|
ui->tabWidget->tabBar()->hide();
|
2014-07-12 22:02:52 +01:00
|
|
|
|
2015-08-18 01:25:24 +01:00
|
|
|
m_watcher->setMatcher(fileFilter);
|
2015-01-27 21:31:07 +00:00
|
|
|
m_watcher->setRootDir(QDir::current().absoluteFilePath(m_path));
|
2014-07-12 22:02:52 +01:00
|
|
|
|
2015-08-18 01:25:24 +01:00
|
|
|
connect(m_watcher, &RecursiveFileSystemWatcher::filesChanged, this, &OtherLogsPage::populateSelectLogBox);
|
2014-07-12 22:02:52 +01:00
|
|
|
populateSelectLogBox();
|
2018-02-05 00:40:38 +00:00
|
|
|
|
|
|
|
auto findShortcut = new QShortcut(QKeySequence(QKeySequence::Find), this);
|
|
|
|
connect(findShortcut, &QShortcut::activated, this, &OtherLogsPage::findActivated);
|
|
|
|
|
|
|
|
auto findNextShortcut = new QShortcut(QKeySequence(QKeySequence::FindNext), this);
|
|
|
|
connect(findNextShortcut, &QShortcut::activated, this, &OtherLogsPage::findNextActivated);
|
|
|
|
|
|
|
|
auto findPreviousShortcut = new QShortcut(QKeySequence(QKeySequence::FindPrevious), this);
|
|
|
|
connect(findPreviousShortcut, &QShortcut::activated, this, &OtherLogsPage::findPreviousActivated);
|
|
|
|
|
|
|
|
connect(ui->searchBar, &QLineEdit::returnPressed, this, &OtherLogsPage::on_findButton_clicked);
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
OtherLogsPage::~OtherLogsPage()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2022-02-22 18:23:53 +00:00
|
|
|
void OtherLogsPage::retranslate()
|
|
|
|
{
|
|
|
|
ui->retranslateUi(this);
|
|
|
|
}
|
|
|
|
|
2018-03-19 01:36:12 +00:00
|
|
|
void OtherLogsPage::openedImpl()
|
2014-07-12 16:58:23 +01:00
|
|
|
{
|
|
|
|
m_watcher->enable();
|
|
|
|
}
|
2018-03-19 01:36:12 +00:00
|
|
|
void OtherLogsPage::closedImpl()
|
2014-07-12 16:58:23 +01:00
|
|
|
{
|
|
|
|
m_watcher->disable();
|
|
|
|
}
|
|
|
|
|
2014-07-12 22:02:52 +01:00
|
|
|
void OtherLogsPage::populateSelectLogBox()
|
|
|
|
{
|
|
|
|
ui->selectLogBox->clear();
|
|
|
|
ui->selectLogBox->addItems(m_watcher->files());
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_currentFile.isEmpty()) {
|
2015-08-18 01:25:24 +01:00
|
|
|
setControlsEnabled(false);
|
2014-07-12 22:02:52 +01:00
|
|
|
ui->selectLogBox->setCurrentIndex(-1);
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2014-07-12 22:02:52 +01:00
|
|
|
const int index = ui->selectLogBox->findText(m_currentFile);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (index != -1) {
|
2014-07-13 00:49:46 +01:00
|
|
|
ui->selectLogBox->setCurrentIndex(index);
|
2015-08-18 01:25:24 +01:00
|
|
|
setControlsEnabled(true);
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2015-08-18 01:25:24 +01:00
|
|
|
setControlsEnabled(false);
|
|
|
|
}
|
2014-07-12 22:02:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 16:58:23 +01:00
|
|
|
void OtherLogsPage::on_selectLogBox_currentIndexChanged(const int index)
|
|
|
|
{
|
|
|
|
QString file;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (index != -1) {
|
2014-07-13 00:49:46 +01:00
|
|
|
file = ui->selectLogBox->itemText(index);
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (file.isEmpty() || !QFile::exists(FS::PathCombine(m_path, file))) {
|
2014-07-12 16:58:23 +01:00
|
|
|
m_currentFile = QString();
|
2014-07-12 22:02:52 +01:00
|
|
|
ui->text->clear();
|
2014-07-12 16:58:23 +01:00
|
|
|
setControlsEnabled(false);
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2014-07-12 16:58:23 +01:00
|
|
|
m_currentFile = file;
|
|
|
|
on_btnReload_clicked();
|
|
|
|
setControlsEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OtherLogsPage::on_btnReload_clicked()
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_currentFile.isEmpty()) {
|
2015-08-18 01:25:24 +01:00
|
|
|
setControlsEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
2015-10-05 00:47:27 +01:00
|
|
|
QFile file(FS::PathCombine(m_path, m_currentFile));
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!file.open(QFile::ReadOnly)) {
|
2014-07-12 16:58:23 +01:00
|
|
|
setControlsEnabled(false);
|
2023-08-02 17:35:35 +01:00
|
|
|
ui->btnReload->setEnabled(true); // allow reload
|
2014-07-12 16:58:23 +01:00
|
|
|
m_currentFile = QString();
|
2023-08-02 17:35:35 +01:00
|
|
|
QMessageBox::critical(this, tr("Error"), tr("Unable to open %1 for reading: %2").arg(m_currentFile, file.errorString()));
|
|
|
|
} else {
|
|
|
|
auto setPlainText = [&](const QString& text) {
|
2021-11-20 15:22:22 +00:00
|
|
|
QString fontFamily = APPLICATION->settings()->get("ConsoleFont").toString();
|
2018-02-14 20:37:32 +00:00
|
|
|
bool conversionOk = false;
|
2021-11-20 15:22:22 +00:00
|
|
|
int fontSize = APPLICATION->settings()->get("ConsoleFontSize").toInt(&conversionOk);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!conversionOk) {
|
2018-02-14 20:37:32 +00:00
|
|
|
fontSize = 11;
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
QTextDocument* doc = ui->text->document();
|
2018-02-14 20:37:32 +00:00
|
|
|
doc->setDefaultFont(QFont(fontFamily, fontSize));
|
|
|
|
ui->text->setPlainText(text);
|
|
|
|
};
|
2023-08-02 17:35:35 +01:00
|
|
|
auto showTooBig = [&]() {
|
|
|
|
setPlainText(tr("The file (%1) is too big. You may want to open it in a viewer optimized "
|
|
|
|
"for large files.")
|
|
|
|
.arg(file.fileName()));
|
2015-08-18 01:25:24 +01:00
|
|
|
};
|
2023-08-02 17:35:35 +01:00
|
|
|
if (file.size() > (1024ll * 1024ll * 12ll)) {
|
2015-08-18 01:25:24 +01:00
|
|
|
showTooBig();
|
|
|
|
return;
|
2014-09-20 23:05:01 +01:00
|
|
|
}
|
2015-08-18 01:25:24 +01:00
|
|
|
QString content;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (file.fileName().endsWith(".gz")) {
|
2015-08-18 01:25:24 +01:00
|
|
|
QByteArray temp;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!GZip::unzip(file.readAll(), temp)) {
|
|
|
|
setPlainText(tr("The file (%1) is not readable.").arg(file.fileName()));
|
2015-08-18 01:25:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
content = QString::fromUtf8(temp);
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2015-08-18 01:25:24 +01:00
|
|
|
content = QString::fromUtf8(file.readAll());
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
if (content.size() >= 50000000ll) {
|
2015-08-18 01:25:24 +01:00
|
|
|
showTooBig();
|
|
|
|
return;
|
|
|
|
}
|
2018-02-14 20:37:32 +00:00
|
|
|
setPlainText(content);
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OtherLogsPage::on_btnPaste_clicked()
|
|
|
|
{
|
2022-12-14 15:02:04 +00:00
|
|
|
GuiUtil::uploadPaste(m_currentFile, ui->text->toPlainText(), this);
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|
2015-08-18 01:25:24 +01:00
|
|
|
|
2014-07-12 16:58:23 +01:00
|
|
|
void OtherLogsPage::on_btnCopy_clicked()
|
|
|
|
{
|
|
|
|
GuiUtil::setClipboardText(ui->text->toPlainText());
|
|
|
|
}
|
2015-08-18 01:25:24 +01:00
|
|
|
|
2014-07-12 16:58:23 +01:00
|
|
|
void OtherLogsPage::on_btnDelete_clicked()
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_currentFile.isEmpty()) {
|
2015-08-18 01:25:24 +01:00
|
|
|
setControlsEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
2022-12-17 09:26:06 +00:00
|
|
|
if (QMessageBox::question(this, tr("Confirm Deletion"),
|
2022-12-26 14:33:50 +00:00
|
|
|
tr("You are about to delete \"%1\".\n"
|
2022-12-14 15:02:04 +00:00
|
|
|
"This may be permanent and it will be gone from the logs folder.\n\n"
|
|
|
|
"Are you sure?")
|
|
|
|
.arg(m_currentFile),
|
|
|
|
QMessageBox::Yes, QMessageBox::No) == QMessageBox::No) {
|
2014-07-12 16:58:23 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-10-05 00:47:27 +01:00
|
|
|
QFile file(FS::PathCombine(m_path, m_currentFile));
|
2022-12-14 15:02:04 +00:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (FS::trash(file.fileName())) {
|
2022-12-14 15:02:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!file.remove()) {
|
|
|
|
QMessageBox::critical(this, tr("Error"), tr("Unable to delete %1: %2").arg(m_currentFile, file.errorString()));
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 01:25:24 +01:00
|
|
|
void OtherLogsPage::on_btnClean_clicked()
|
|
|
|
{
|
|
|
|
auto toDelete = m_watcher->files();
|
2023-08-02 17:35:35 +01:00
|
|
|
if (toDelete.isEmpty()) {
|
2015-08-18 01:25:24 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
QMessageBox* messageBox = new QMessageBox(this);
|
2022-12-17 09:26:06 +00:00
|
|
|
messageBox->setWindowTitle(tr("Confirm Cleanup"));
|
2023-08-02 17:35:35 +01:00
|
|
|
if (toDelete.size() > 5) {
|
2022-12-14 15:02:04 +00:00
|
|
|
messageBox->setText(tr("Are you sure you want to delete all log files?"));
|
2015-08-18 01:25:24 +01:00
|
|
|
messageBox->setDetailedText(toDelete.join('\n'));
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2022-12-14 15:02:04 +00:00
|
|
|
messageBox->setText(tr("Are you sure you want to delete all these files?\n%1").arg(toDelete.join('\n')));
|
2015-08-18 01:25:24 +01:00
|
|
|
}
|
|
|
|
messageBox->setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
|
|
|
|
messageBox->setDefaultButton(QMessageBox::Ok);
|
|
|
|
messageBox->setTextInteractionFlags(Qt::TextSelectableByMouse);
|
|
|
|
messageBox->setIcon(QMessageBox::Question);
|
|
|
|
messageBox->setTextInteractionFlags(Qt::TextBrowserInteraction);
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (messageBox->exec() != QMessageBox::Ok) {
|
2015-08-18 01:25:24 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
QStringList failed;
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto item : toDelete) {
|
2015-10-05 00:47:27 +01:00
|
|
|
QFile file(FS::PathCombine(m_path, item));
|
2023-08-02 17:35:35 +01:00
|
|
|
if (FS::trash(file.fileName())) {
|
2022-12-14 15:02:04 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!file.remove()) {
|
2015-08-18 01:25:24 +01:00
|
|
|
failed.push_back(item);
|
|
|
|
}
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!failed.empty()) {
|
|
|
|
QMessageBox* messageBox = new QMessageBox(this);
|
2015-08-18 01:25:24 +01:00
|
|
|
messageBox->setWindowTitle(tr("Error"));
|
2023-08-02 17:35:35 +01:00
|
|
|
if (failed.size() > 5) {
|
2015-08-18 01:25:24 +01:00
|
|
|
messageBox->setText(tr("Couldn't delete some files!"));
|
|
|
|
messageBox->setDetailedText(failed.join('\n'));
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2015-08-18 01:25:24 +01:00
|
|
|
messageBox->setText(tr("Couldn't delete some files:\n%1").arg(failed.join('\n')));
|
|
|
|
}
|
|
|
|
messageBox->setStandardButtons(QMessageBox::Ok);
|
|
|
|
messageBox->setDefaultButton(QMessageBox::Ok);
|
|
|
|
messageBox->setTextInteractionFlags(Qt::TextSelectableByMouse);
|
|
|
|
messageBox->setIcon(QMessageBox::Critical);
|
|
|
|
messageBox->setTextInteractionFlags(Qt::TextBrowserInteraction);
|
|
|
|
messageBox->exec();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 16:58:23 +01:00
|
|
|
void OtherLogsPage::setControlsEnabled(const bool enabled)
|
|
|
|
{
|
|
|
|
ui->btnReload->setEnabled(enabled);
|
|
|
|
ui->btnDelete->setEnabled(enabled);
|
|
|
|
ui->btnCopy->setEnabled(enabled);
|
|
|
|
ui->btnPaste->setEnabled(enabled);
|
|
|
|
ui->text->setEnabled(enabled);
|
2015-08-18 01:25:24 +01:00
|
|
|
ui->btnClean->setEnabled(enabled);
|
2014-07-12 16:58:23 +01:00
|
|
|
}
|
2018-02-05 00:40:38 +00:00
|
|
|
|
|
|
|
// FIXME: HACK, use LogView instead?
|
2023-08-02 17:35:35 +01:00
|
|
|
static void findNext(QPlainTextEdit* _this, const QString& what, bool reverse)
|
2018-02-05 00:40:38 +00:00
|
|
|
{
|
|
|
|
_this->find(what, reverse ? QTextDocument::FindFlag::FindBackward : QTextDocument::FindFlag(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void OtherLogsPage::on_findButton_clicked()
|
|
|
|
{
|
|
|
|
auto modifiers = QApplication::keyboardModifiers();
|
|
|
|
bool reverse = modifiers & Qt::ShiftModifier;
|
|
|
|
findNext(ui->text, ui->searchBar->text(), reverse);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OtherLogsPage::findNextActivated()
|
|
|
|
{
|
|
|
|
findNext(ui->text, ui->searchBar->text(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OtherLogsPage::findPreviousActivated()
|
|
|
|
{
|
|
|
|
findNext(ui->text, ui->searchBar->text(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OtherLogsPage::findActivated()
|
|
|
|
{
|
|
|
|
// focus the search bar if it doesn't have focus
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!ui->searchBar->hasFocus()) {
|
2018-02-05 00:40:38 +00:00
|
|
|
ui->searchBar->setFocus();
|
|
|
|
ui->searchBar->selectAll();
|
|
|
|
}
|
|
|
|
}
|