2018-02-10 23:40:01 +00:00
|
|
|
/* Copyright 2013-2018 MultiMC Contributors
|
2014-07-12 21:02:52 +00:00
|
|
|
*
|
|
|
|
* 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 15:58:23 +00:00
|
|
|
#include "OtherLogsPage.h"
|
|
|
|
#include "ui_OtherLogsPage.h"
|
|
|
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
2015-02-09 00:51:14 +00:00
|
|
|
#include "GuiUtil.h"
|
|
|
|
#include "RecursiveFileSystemWatcher.h"
|
2015-08-18 00:25:24 +00:00
|
|
|
#include <GZip.h>
|
2015-10-04 23:47:27 +00:00
|
|
|
#include <FileSystem.h>
|
2018-02-05 00:40:38 +00:00
|
|
|
#include <QShortcut>
|
2014-07-12 15:58:23 +00:00
|
|
|
|
2015-08-18 00:25:24 +00:00
|
|
|
OtherLogsPage::OtherLogsPage(QString path, IPathMatcher::Ptr fileFilter, QWidget *parent)
|
|
|
|
: QWidget(parent), ui(new Ui::OtherLogsPage), m_path(path), m_fileFilter(fileFilter),
|
2014-07-12 21:02:52 +00:00
|
|
|
m_watcher(new RecursiveFileSystemWatcher(this))
|
2014-07-12 15:58:23 +00:00
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2014-07-20 22:10:13 +00:00
|
|
|
ui->tabWidget->tabBar()->hide();
|
2014-07-12 21:02:52 +00:00
|
|
|
|
2015-08-18 00:25:24 +00:00
|
|
|
m_watcher->setMatcher(fileFilter);
|
2015-01-27 21:31:07 +00:00
|
|
|
m_watcher->setRootDir(QDir::current().absoluteFilePath(m_path));
|
2014-07-12 21:02:52 +00:00
|
|
|
|
2015-08-18 00:25:24 +00:00
|
|
|
connect(m_watcher, &RecursiveFileSystemWatcher::filesChanged, this, &OtherLogsPage::populateSelectLogBox);
|
2014-07-12 21:02:52 +00: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 15:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OtherLogsPage::~OtherLogsPage()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2018-03-19 01:36:12 +00:00
|
|
|
void OtherLogsPage::openedImpl()
|
2014-07-12 15:58:23 +00:00
|
|
|
{
|
|
|
|
m_watcher->enable();
|
|
|
|
}
|
2018-03-19 01:36:12 +00:00
|
|
|
void OtherLogsPage::closedImpl()
|
2014-07-12 15:58:23 +00:00
|
|
|
{
|
|
|
|
m_watcher->disable();
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:02:52 +00:00
|
|
|
void OtherLogsPage::populateSelectLogBox()
|
|
|
|
{
|
|
|
|
ui->selectLogBox->clear();
|
|
|
|
ui->selectLogBox->addItems(m_watcher->files());
|
2015-08-18 00:25:24 +00:00
|
|
|
if (m_currentFile.isEmpty())
|
2014-07-12 21:02:52 +00:00
|
|
|
{
|
2015-08-18 00:25:24 +00:00
|
|
|
setControlsEnabled(false);
|
2014-07-12 21:02:52 +00:00
|
|
|
ui->selectLogBox->setCurrentIndex(-1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const int index = ui->selectLogBox->findText(m_currentFile);
|
2014-09-20 22:05:01 +00:00
|
|
|
if (index != -1)
|
2015-08-18 00:25:24 +00:00
|
|
|
{
|
2014-07-12 23:49:46 +00:00
|
|
|
ui->selectLogBox->setCurrentIndex(index);
|
2015-08-18 00:25:24 +00:00
|
|
|
setControlsEnabled(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setControlsEnabled(false);
|
|
|
|
}
|
2014-07-12 21:02:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 15:58:23 +00:00
|
|
|
void OtherLogsPage::on_selectLogBox_currentIndexChanged(const int index)
|
|
|
|
{
|
|
|
|
QString file;
|
|
|
|
if (index != -1)
|
|
|
|
{
|
2014-07-12 23:49:46 +00:00
|
|
|
file = ui->selectLogBox->itemText(index);
|
2014-07-12 15:58:23 +00:00
|
|
|
}
|
|
|
|
|
2015-10-04 23:47:27 +00:00
|
|
|
if (file.isEmpty() || !QFile::exists(FS::PathCombine(m_path, file)))
|
2014-07-12 15:58:23 +00:00
|
|
|
{
|
|
|
|
m_currentFile = QString();
|
2014-07-12 21:02:52 +00:00
|
|
|
ui->text->clear();
|
2014-07-12 15:58:23 +00:00
|
|
|
setControlsEnabled(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_currentFile = file;
|
|
|
|
on_btnReload_clicked();
|
|
|
|
setControlsEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OtherLogsPage::on_btnReload_clicked()
|
|
|
|
{
|
2015-08-18 00:25:24 +00:00
|
|
|
if(m_currentFile.isEmpty())
|
|
|
|
{
|
|
|
|
setControlsEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
2015-10-04 23:47:27 +00:00
|
|
|
QFile file(FS::PathCombine(m_path, m_currentFile));
|
2014-07-12 15:58:23 +00:00
|
|
|
if (!file.open(QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
setControlsEnabled(false);
|
|
|
|
ui->btnReload->setEnabled(true); // allow reload
|
|
|
|
m_currentFile = QString();
|
2014-07-12 21:02:52 +00:00
|
|
|
QMessageBox::critical(this, tr("Error"), tr("Unable to open %1 for reading: %2")
|
|
|
|
.arg(m_currentFile, file.errorString()));
|
2014-07-12 15:58:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-02-14 20:37:32 +00:00
|
|
|
auto setPlainText = [&](const QString & text)
|
|
|
|
{
|
|
|
|
QString fontFamily = MMC->settings()->get("ConsoleFont").toString();
|
|
|
|
bool conversionOk = false;
|
|
|
|
int fontSize = MMC->settings()->get("ConsoleFontSize").toInt(&conversionOk);
|
|
|
|
if(!conversionOk)
|
|
|
|
{
|
|
|
|
fontSize = 11;
|
|
|
|
}
|
|
|
|
QTextDocument *doc = ui->text->document();
|
|
|
|
doc->setDefaultFont(QFont(fontFamily, fontSize));
|
|
|
|
ui->text->setPlainText(text);
|
|
|
|
};
|
2015-08-18 00:25:24 +00:00
|
|
|
auto showTooBig = [&]()
|
2014-09-20 22:05:01 +00:00
|
|
|
{
|
2018-02-14 20:37:32 +00:00
|
|
|
setPlainText(
|
2014-09-20 22:05:01 +00:00
|
|
|
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 00:25:24 +00:00
|
|
|
};
|
2015-10-01 22:12:53 +00:00
|
|
|
if(file.size() > (1024ll * 1024ll * 12ll))
|
2015-08-18 00:25:24 +00:00
|
|
|
{
|
|
|
|
showTooBig();
|
|
|
|
return;
|
2014-09-20 22:05:01 +00:00
|
|
|
}
|
2015-08-18 00:25:24 +00:00
|
|
|
QString content;
|
|
|
|
if(file.fileName().endsWith(".gz"))
|
|
|
|
{
|
|
|
|
QByteArray temp;
|
2015-09-14 00:36:03 +00:00
|
|
|
if(!GZip::unzip(file.readAll(), temp))
|
2015-08-18 00:25:24 +00:00
|
|
|
{
|
2018-02-14 20:37:32 +00:00
|
|
|
setPlainText(
|
2015-08-18 00:25:24 +00:00
|
|
|
tr("The file (%1) is not readable.").arg(file.fileName()));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
content = QString::fromUtf8(temp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
content = QString::fromUtf8(file.readAll());
|
|
|
|
}
|
|
|
|
if (content.size() >= 50000000ll)
|
|
|
|
{
|
|
|
|
showTooBig();
|
|
|
|
return;
|
|
|
|
}
|
2018-02-14 20:37:32 +00:00
|
|
|
setPlainText(content);
|
2014-07-12 15:58:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OtherLogsPage::on_btnPaste_clicked()
|
|
|
|
{
|
|
|
|
GuiUtil::uploadPaste(ui->text->toPlainText(), this);
|
|
|
|
}
|
2015-08-18 00:25:24 +00:00
|
|
|
|
2014-07-12 15:58:23 +00:00
|
|
|
void OtherLogsPage::on_btnCopy_clicked()
|
|
|
|
{
|
|
|
|
GuiUtil::setClipboardText(ui->text->toPlainText());
|
|
|
|
}
|
2015-08-18 00:25:24 +00:00
|
|
|
|
2014-07-12 15:58:23 +00:00
|
|
|
void OtherLogsPage::on_btnDelete_clicked()
|
|
|
|
{
|
2015-08-18 00:25:24 +00:00
|
|
|
if(m_currentFile.isEmpty())
|
|
|
|
{
|
|
|
|
setControlsEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
2014-07-12 21:02:52 +00:00
|
|
|
if (QMessageBox::question(this, tr("Delete"),
|
|
|
|
tr("Do you really want to delete %1?").arg(m_currentFile),
|
|
|
|
QMessageBox::Yes, QMessageBox::No) == QMessageBox::No)
|
2014-07-12 15:58:23 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-10-04 23:47:27 +00:00
|
|
|
QFile file(FS::PathCombine(m_path, m_currentFile));
|
2014-07-12 15:58:23 +00:00
|
|
|
if (!file.remove())
|
|
|
|
{
|
2014-07-12 21:02:52 +00:00
|
|
|
QMessageBox::critical(this, tr("Error"), tr("Unable to delete %1: %2")
|
|
|
|
.arg(m_currentFile, file.errorString()));
|
2014-07-12 15:58:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 00:25:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
void OtherLogsPage::on_btnClean_clicked()
|
|
|
|
{
|
|
|
|
auto toDelete = m_watcher->files();
|
|
|
|
if(toDelete.isEmpty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QMessageBox *messageBox = new QMessageBox(this);
|
|
|
|
messageBox->setWindowTitle(tr("Clean up"));
|
|
|
|
if(toDelete.size() > 5)
|
|
|
|
{
|
|
|
|
messageBox->setText(tr("Do you really want to delete all log files?"));
|
|
|
|
messageBox->setDetailedText(toDelete.join('\n'));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-12-25 12:06:38 +00:00
|
|
|
messageBox->setText(tr("Do you really want to delete these files?\n%1").arg(toDelete.join('\n')));
|
2015-08-18 00:25:24 +00:00
|
|
|
}
|
|
|
|
messageBox->setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
|
|
|
|
messageBox->setDefaultButton(QMessageBox::Ok);
|
|
|
|
messageBox->setTextInteractionFlags(Qt::TextSelectableByMouse);
|
|
|
|
messageBox->setIcon(QMessageBox::Question);
|
|
|
|
messageBox->setTextInteractionFlags(Qt::TextBrowserInteraction);
|
|
|
|
|
|
|
|
if (messageBox->exec() != QMessageBox::Ok)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QStringList failed;
|
|
|
|
for(auto item: toDelete)
|
|
|
|
{
|
2015-10-04 23:47:27 +00:00
|
|
|
QFile file(FS::PathCombine(m_path, item));
|
2015-08-18 00:25:24 +00:00
|
|
|
if (!file.remove())
|
|
|
|
{
|
|
|
|
failed.push_back(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!failed.empty())
|
|
|
|
{
|
|
|
|
QMessageBox *messageBox = new QMessageBox(this);
|
|
|
|
messageBox->setWindowTitle(tr("Error"));
|
|
|
|
if(failed.size() > 5)
|
|
|
|
{
|
|
|
|
messageBox->setText(tr("Couldn't delete some files!"));
|
|
|
|
messageBox->setDetailedText(failed.join('\n'));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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 15:58:23 +00: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 00:25:24 +00:00
|
|
|
ui->btnClean->setEnabled(enabled);
|
2014-07-12 15:58:23 +00:00
|
|
|
}
|
2018-02-05 00:40:38 +00:00
|
|
|
|
|
|
|
// FIXME: HACK, use LogView instead?
|
|
|
|
static void findNext(QPlainTextEdit * _this, const QString& what, bool reverse)
|
|
|
|
{
|
|
|
|
_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
|
|
|
|
if (!ui->searchBar->hasFocus())
|
|
|
|
{
|
|
|
|
ui->searchBar->setFocus();
|
|
|
|
ui->searchBar->selectAll();
|
|
|
|
}
|
|
|
|
}
|