2015-02-02 22:25:30 +00:00
|
|
|
/* Copyright 2013-2015 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 <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
2015-02-09 00:51:14 +00:00
|
|
|
#include "GuiUtil.h"
|
|
|
|
#include "RecursiveFileSystemWatcher.h"
|
2015-01-27 21:31:07 +00:00
|
|
|
#include <pathutils.h>
|
2014-07-12 15:58:23 +00:00
|
|
|
|
2015-01-27 21:31:07 +00:00
|
|
|
OtherLogsPage::OtherLogsPage(QString path, QWidget *parent)
|
|
|
|
: QWidget(parent), ui(new Ui::OtherLogsPage), m_path(path),
|
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
|
|
|
|
2014-07-12 23:49:46 +00:00
|
|
|
m_watcher->setFileExpression("(.*\\.log(\\.[0-9]*)?$)|(crash-.*\\.txt)");
|
2015-01-27 21:31:07 +00:00
|
|
|
m_watcher->setRootDir(QDir::current().absoluteFilePath(m_path));
|
2014-07-12 21:02:52 +00:00
|
|
|
|
|
|
|
connect(m_watcher, &RecursiveFileSystemWatcher::filesChanged, this,
|
|
|
|
&OtherLogsPage::populateSelectLogBox);
|
|
|
|
populateSelectLogBox();
|
2014-07-12 15:58:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OtherLogsPage::~OtherLogsPage()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
|
|
|
void OtherLogsPage::opened()
|
|
|
|
{
|
|
|
|
m_watcher->enable();
|
|
|
|
}
|
|
|
|
void OtherLogsPage::closed()
|
|
|
|
{
|
|
|
|
m_watcher->disable();
|
|
|
|
}
|
|
|
|
|
2014-07-12 21:02:52 +00:00
|
|
|
void OtherLogsPage::populateSelectLogBox()
|
|
|
|
{
|
|
|
|
ui->selectLogBox->clear();
|
|
|
|
ui->selectLogBox->addItems(m_watcher->files());
|
|
|
|
if (m_currentFile.isNull())
|
|
|
|
{
|
|
|
|
ui->selectLogBox->setCurrentIndex(-1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
const int index = ui->selectLogBox->findText(m_currentFile);
|
2014-09-20 22:05:01 +00:00
|
|
|
if (index != -1)
|
2014-07-12 23:49:46 +00:00
|
|
|
ui->selectLogBox->setCurrentIndex(index);
|
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-01-27 21:31:07 +00:00
|
|
|
if (file.isEmpty() || !QFile::exists(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-01-27 21:31:07 +00:00
|
|
|
QFile file(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
|
|
|
|
{
|
2014-09-20 22:05:01 +00:00
|
|
|
if (file.size() < 10000000ll)
|
|
|
|
{
|
|
|
|
ui->text->setPlainText(QString::fromUtf8(file.readAll()));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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()));
|
|
|
|
}
|
2014-07-12 15:58:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void OtherLogsPage::on_btnPaste_clicked()
|
|
|
|
{
|
|
|
|
GuiUtil::uploadPaste(ui->text->toPlainText(), this);
|
|
|
|
}
|
|
|
|
void OtherLogsPage::on_btnCopy_clicked()
|
|
|
|
{
|
|
|
|
GuiUtil::setClipboardText(ui->text->toPlainText());
|
|
|
|
}
|
|
|
|
void OtherLogsPage::on_btnDelete_clicked()
|
|
|
|
{
|
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-01-27 21:31:07 +00:00
|
|
|
QFile file(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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|