2018-02-11 05:10:01 +05:30
|
|
|
/* Copyright 2013-2018 MultiMC Contributors
|
2014-07-13 02:32:52 +05:30
|
|
|
*
|
|
|
|
* 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 21:28:23 +05:30
|
|
|
#include "OtherLogsPage.h"
|
|
|
|
#include "ui_OtherLogsPage.h"
|
|
|
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
2015-02-09 06:21:14 +05:30
|
|
|
#include "GuiUtil.h"
|
|
|
|
#include "RecursiveFileSystemWatcher.h"
|
2015-08-18 05:55:24 +05:30
|
|
|
#include <GZip.h>
|
2015-10-05 05:17:27 +05:30
|
|
|
#include <FileSystem.h>
|
2018-02-05 06:10:38 +05:30
|
|
|
#include <QShortcut>
|
2014-07-12 21:28:23 +05:30
|
|
|
|
2015-08-18 05:55:24 +05:30
|
|
|
OtherLogsPage::OtherLogsPage(QString path, IPathMatcher::Ptr fileFilter, QWidget *parent)
|
|
|
|
: QWidget(parent), ui(new Ui::OtherLogsPage), m_path(path), m_fileFilter(fileFilter),
|
2014-07-13 02:32:52 +05:30
|
|
|
m_watcher(new RecursiveFileSystemWatcher(this))
|
2014-07-12 21:28:23 +05:30
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
2014-07-21 03:40:13 +05:30
|
|
|
ui->tabWidget->tabBar()->hide();
|
2014-07-13 02:32:52 +05:30
|
|
|
|
2015-08-18 05:55:24 +05:30
|
|
|
m_watcher->setMatcher(fileFilter);
|
2015-01-28 03:01:07 +05:30
|
|
|
m_watcher->setRootDir(QDir::current().absoluteFilePath(m_path));
|
2014-07-13 02:32:52 +05:30
|
|
|
|
2015-08-18 05:55:24 +05:30
|
|
|
connect(m_watcher, &RecursiveFileSystemWatcher::filesChanged, this, &OtherLogsPage::populateSelectLogBox);
|
2014-07-13 02:32:52 +05:30
|
|
|
populateSelectLogBox();
|
2018-02-05 06:10:38 +05:30
|
|
|
|
|
|
|
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 21:28:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
OtherLogsPage::~OtherLogsPage()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OtherLogsPage::opened()
|
|
|
|
{
|
|
|
|
m_watcher->enable();
|
|
|
|
}
|
|
|
|
void OtherLogsPage::closed()
|
|
|
|
{
|
|
|
|
m_watcher->disable();
|
|
|
|
}
|
|
|
|
|
2014-07-13 02:32:52 +05:30
|
|
|
void OtherLogsPage::populateSelectLogBox()
|
|
|
|
{
|
|
|
|
ui->selectLogBox->clear();
|
|
|
|
ui->selectLogBox->addItems(m_watcher->files());
|
2015-08-18 05:55:24 +05:30
|
|
|
if (m_currentFile.isEmpty())
|
2014-07-13 02:32:52 +05:30
|
|
|
{
|
2015-08-18 05:55:24 +05:30
|
|
|
setControlsEnabled(false);
|
2014-07-13 02:32:52 +05:30
|
|
|
ui->selectLogBox->setCurrentIndex(-1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const int index = ui->selectLogBox->findText(m_currentFile);
|
2014-09-21 03:35:01 +05:30
|
|
|
if (index != -1)
|
2015-08-18 05:55:24 +05:30
|
|
|
{
|
2014-07-13 05:19:46 +05:30
|
|
|
ui->selectLogBox->setCurrentIndex(index);
|
2015-08-18 05:55:24 +05:30
|
|
|
setControlsEnabled(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setControlsEnabled(false);
|
|
|
|
}
|
2014-07-13 02:32:52 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:28:23 +05:30
|
|
|
void OtherLogsPage::on_selectLogBox_currentIndexChanged(const int index)
|
|
|
|
{
|
|
|
|
QString file;
|
|
|
|
if (index != -1)
|
|
|
|
{
|
2014-07-13 05:19:46 +05:30
|
|
|
file = ui->selectLogBox->itemText(index);
|
2014-07-12 21:28:23 +05:30
|
|
|
}
|
|
|
|
|
2015-10-05 05:17:27 +05:30
|
|
|
if (file.isEmpty() || !QFile::exists(FS::PathCombine(m_path, file)))
|
2014-07-12 21:28:23 +05:30
|
|
|
{
|
|
|
|
m_currentFile = QString();
|
2014-07-13 02:32:52 +05:30
|
|
|
ui->text->clear();
|
2014-07-12 21:28:23 +05:30
|
|
|
setControlsEnabled(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_currentFile = file;
|
|
|
|
on_btnReload_clicked();
|
|
|
|
setControlsEnabled(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OtherLogsPage::on_btnReload_clicked()
|
|
|
|
{
|
2015-08-18 05:55:24 +05:30
|
|
|
if(m_currentFile.isEmpty())
|
|
|
|
{
|
|
|
|
setControlsEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
2015-10-05 05:17:27 +05:30
|
|
|
QFile file(FS::PathCombine(m_path, m_currentFile));
|
2014-07-12 21:28:23 +05:30
|
|
|
if (!file.open(QFile::ReadOnly))
|
|
|
|
{
|
|
|
|
setControlsEnabled(false);
|
|
|
|
ui->btnReload->setEnabled(true); // allow reload
|
|
|
|
m_currentFile = QString();
|
2014-07-13 02:32:52 +05:30
|
|
|
QMessageBox::critical(this, tr("Error"), tr("Unable to open %1 for reading: %2")
|
|
|
|
.arg(m_currentFile, file.errorString()));
|
2014-07-12 21:28:23 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-08-18 05:55:24 +05:30
|
|
|
auto showTooBig = [&]()
|
2014-09-21 03:35:01 +05:30
|
|
|
{
|
|
|
|
ui->text->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 05:55:24 +05:30
|
|
|
};
|
2015-10-02 03:42:53 +05:30
|
|
|
if(file.size() > (1024ll * 1024ll * 12ll))
|
2015-08-18 05:55:24 +05:30
|
|
|
{
|
|
|
|
showTooBig();
|
|
|
|
return;
|
2014-09-21 03:35:01 +05:30
|
|
|
}
|
2015-08-18 05:55:24 +05:30
|
|
|
QString content;
|
|
|
|
if(file.fileName().endsWith(".gz"))
|
|
|
|
{
|
|
|
|
QByteArray temp;
|
2015-09-14 06:06:03 +05:30
|
|
|
if(!GZip::unzip(file.readAll(), temp))
|
2015-08-18 05:55:24 +05:30
|
|
|
{
|
|
|
|
ui->text->setPlainText(
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
ui->text->setPlainText(content);
|
2014-07-12 21:28:23 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OtherLogsPage::on_btnPaste_clicked()
|
|
|
|
{
|
|
|
|
GuiUtil::uploadPaste(ui->text->toPlainText(), this);
|
|
|
|
}
|
2015-08-18 05:55:24 +05:30
|
|
|
|
2014-07-12 21:28:23 +05:30
|
|
|
void OtherLogsPage::on_btnCopy_clicked()
|
|
|
|
{
|
|
|
|
GuiUtil::setClipboardText(ui->text->toPlainText());
|
|
|
|
}
|
2015-08-18 05:55:24 +05:30
|
|
|
|
2014-07-12 21:28:23 +05:30
|
|
|
void OtherLogsPage::on_btnDelete_clicked()
|
|
|
|
{
|
2015-08-18 05:55:24 +05:30
|
|
|
if(m_currentFile.isEmpty())
|
|
|
|
{
|
|
|
|
setControlsEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
2014-07-13 02:32:52 +05:30
|
|
|
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 21:28:23 +05:30
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2015-10-05 05:17:27 +05:30
|
|
|
QFile file(FS::PathCombine(m_path, m_currentFile));
|
2014-07-12 21:28:23 +05:30
|
|
|
if (!file.remove())
|
|
|
|
{
|
2014-07-13 02:32:52 +05:30
|
|
|
QMessageBox::critical(this, tr("Error"), tr("Unable to delete %1: %2")
|
|
|
|
.arg(m_currentFile, file.errorString()));
|
2014-07-12 21:28:23 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-18 05:55:24 +05:30
|
|
|
|
|
|
|
|
|
|
|
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 17:36:38 +05:30
|
|
|
messageBox->setText(tr("Do you really want to delete these files?\n%1").arg(toDelete.join('\n')));
|
2015-08-18 05:55:24 +05:30
|
|
|
}
|
|
|
|
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-05 05:17:27 +05:30
|
|
|
QFile file(FS::PathCombine(m_path, item));
|
2015-08-18 05:55:24 +05:30
|
|
|
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 21:28:23 +05:30
|
|
|
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 05:55:24 +05:30
|
|
|
ui->btnClean->setEnabled(enabled);
|
2014-07-12 21:28:23 +05:30
|
|
|
}
|
2018-02-05 06:10:38 +05:30
|
|
|
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|