2022-03-21 00:31:08 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
2022-12-14 20:32:04 +05:30
|
|
|
* Prism Launcher - Minecraft Launcher
|
2022-03-21 00:31:08 +05:30
|
|
|
* Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org>
|
2022-06-12 17:20:58 +05:30
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2022-12-14 20:32:04 +05:30
|
|
|
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
|
2022-03-21 00:31:08 +05:30
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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-06-30 05:32:57 +05:30
|
|
|
#include "LogPage.h"
|
|
|
|
#include "ui_LogPage.h"
|
2014-07-12 21:28:23 +05:30
|
|
|
|
2021-11-20 20:52:22 +05:30
|
|
|
#include "Application.h"
|
2014-11-10 00:18:35 +05:30
|
|
|
|
2014-07-12 21:28:23 +05:30
|
|
|
#include <QIcon>
|
2014-06-30 05:32:57 +05:30
|
|
|
#include <QScrollBar>
|
2014-11-09 05:29:22 +05:30
|
|
|
#include <QShortcut>
|
2014-06-30 05:32:57 +05:30
|
|
|
|
2015-07-10 04:41:06 +05:30
|
|
|
#include "launch/LaunchTask.h"
|
2021-11-22 08:25:16 +05:30
|
|
|
#include "settings/Setting.h"
|
|
|
|
|
|
|
|
#include "ui/GuiUtil.h"
|
|
|
|
#include "ui/ColorCache.h"
|
|
|
|
|
2021-10-18 04:17:02 +05:30
|
|
|
#include <BuildConfig.h>
|
2014-07-12 21:28:23 +05:30
|
|
|
|
2016-08-19 01:01:37 +05:30
|
|
|
class LogFormatProxyModel : public QIdentityProxyModel
|
|
|
|
{
|
|
|
|
public:
|
2018-07-15 18:21:05 +05:30
|
|
|
LogFormatProxyModel(QObject* parent = nullptr) : QIdentityProxyModel(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
QVariant data(const QModelIndex &index, int role) const override
|
|
|
|
{
|
|
|
|
switch(role)
|
|
|
|
{
|
|
|
|
case Qt::FontRole:
|
|
|
|
return m_font;
|
2022-05-02 22:40:45 +05:30
|
|
|
case Qt::ForegroundRole:
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
|
|
|
MessageLevel::Enum level = (MessageLevel::Enum) QIdentityProxyModel::data(index, LogModel::LevelRole).toInt();
|
|
|
|
return m_colors->getFront(level);
|
|
|
|
}
|
|
|
|
case Qt::BackgroundRole:
|
|
|
|
{
|
|
|
|
MessageLevel::Enum level = (MessageLevel::Enum) QIdentityProxyModel::data(index, LogModel::LevelRole).toInt();
|
|
|
|
return m_colors->getBack(level);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return QIdentityProxyModel::data(index, role);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void setFont(QFont font)
|
|
|
|
{
|
|
|
|
m_font = font;
|
|
|
|
}
|
|
|
|
|
|
|
|
void setColors(LogColorCache* colors)
|
|
|
|
{
|
|
|
|
m_colors.reset(colors);
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex find(const QModelIndex &start, const QString &value, bool reverse) const
|
|
|
|
{
|
|
|
|
QModelIndex parentIndex = parent(start);
|
|
|
|
auto compare = [&](int r) -> QModelIndex
|
|
|
|
{
|
|
|
|
QModelIndex idx = index(r, start.column(), parentIndex);
|
|
|
|
if (!idx.isValid() || idx == start)
|
|
|
|
{
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
QVariant v = data(idx, Qt::DisplayRole);
|
|
|
|
QString t = v.toString();
|
|
|
|
if (t.contains(value, Qt::CaseInsensitive))
|
|
|
|
return idx;
|
|
|
|
return QModelIndex();
|
|
|
|
};
|
|
|
|
if(reverse)
|
|
|
|
{
|
|
|
|
int from = start.row();
|
|
|
|
int to = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
for (int r = from; (r >= to); --r)
|
|
|
|
{
|
|
|
|
auto idx = compare(r);
|
|
|
|
if(idx.isValid())
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
// prepare for the next iteration
|
|
|
|
from = rowCount() - 1;
|
|
|
|
to = start.row();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int from = start.row();
|
|
|
|
int to = rowCount(parentIndex);
|
|
|
|
|
|
|
|
for (int i = 0; i < 2; ++i)
|
|
|
|
{
|
|
|
|
for (int r = from; (r < to); ++r)
|
|
|
|
{
|
|
|
|
auto idx = compare(r);
|
|
|
|
if(idx.isValid())
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
// prepare for the next iteration
|
|
|
|
from = 0;
|
|
|
|
to = start.row();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
2016-08-19 01:01:37 +05:30
|
|
|
private:
|
2018-07-15 18:21:05 +05:30
|
|
|
QFont m_font;
|
|
|
|
std::unique_ptr<LogColorCache> m_colors;
|
2016-08-19 01:01:37 +05:30
|
|
|
};
|
|
|
|
|
2016-08-10 12:11:58 +05:30
|
|
|
LogPage::LogPage(InstancePtr instance, QWidget *parent)
|
2018-07-15 18:21:05 +05:30
|
|
|
: QWidget(parent), ui(new Ui::LogPage), m_instance(instance)
|
2014-06-30 05:32:57 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
ui->setupUi(this);
|
|
|
|
ui->tabWidget->tabBar()->hide();
|
|
|
|
|
|
|
|
m_proxy = new LogFormatProxyModel(this);
|
|
|
|
// set up text colors in the log proxy and adapt them to the current theme foreground and background
|
|
|
|
{
|
|
|
|
auto origForeground = ui->text->palette().color(ui->text->foregroundRole());
|
|
|
|
auto origBackground = ui->text->palette().color(ui->text->backgroundRole());
|
|
|
|
m_proxy->setColors(new LogColorCache(origForeground, origBackground));
|
|
|
|
}
|
|
|
|
|
|
|
|
// set up fonts in the log proxy
|
|
|
|
{
|
2021-11-20 20:52:22 +05:30
|
|
|
QString fontFamily = APPLICATION->settings()->get("ConsoleFont").toString();
|
2018-07-15 18:21:05 +05:30
|
|
|
bool conversionOk = false;
|
2021-11-20 20:52:22 +05:30
|
|
|
int fontSize = APPLICATION->settings()->get("ConsoleFontSize").toInt(&conversionOk);
|
2018-07-15 18:21:05 +05:30
|
|
|
if(!conversionOk)
|
|
|
|
{
|
|
|
|
fontSize = 11;
|
|
|
|
}
|
|
|
|
m_proxy->setFont(QFont(fontFamily, fontSize));
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->text->setModel(m_proxy);
|
|
|
|
|
|
|
|
// set up instance and launch process recognition
|
|
|
|
{
|
|
|
|
auto launchTask = m_instance->getLaunchTask();
|
|
|
|
if(launchTask)
|
|
|
|
{
|
|
|
|
setInstanceLaunchTaskChanged(launchTask, true);
|
|
|
|
}
|
|
|
|
connect(m_instance.get(), &BaseInstance::launchTaskChanged, this, &LogPage::onInstanceLaunchTaskChanged);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto findShortcut = new QShortcut(QKeySequence(QKeySequence::Find), this);
|
|
|
|
connect(findShortcut, SIGNAL(activated()), SLOT(findActivated()));
|
|
|
|
auto findNextShortcut = new QShortcut(QKeySequence(QKeySequence::FindNext), this);
|
|
|
|
connect(findNextShortcut, SIGNAL(activated()), SLOT(findNextActivated()));
|
|
|
|
connect(ui->searchBar, SIGNAL(returnPressed()), SLOT(on_findButton_clicked()));
|
|
|
|
auto findPreviousShortcut = new QShortcut(QKeySequence(QKeySequence::FindPrevious), this);
|
|
|
|
connect(findPreviousShortcut, SIGNAL(activated()), SLOT(findPreviousActivated()));
|
2014-06-30 05:32:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
LogPage::~LogPage()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
delete ui;
|
2014-06-30 05:32:57 +05:30
|
|
|
}
|
|
|
|
|
2017-12-18 05:49:43 +05:30
|
|
|
void LogPage::modelStateToUI()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(m_model->wrapLines())
|
|
|
|
{
|
|
|
|
ui->text->setWordWrap(true);
|
|
|
|
ui->wrapCheckbox->setCheckState(Qt::Checked);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->text->setWordWrap(false);
|
|
|
|
ui->wrapCheckbox->setCheckState(Qt::Unchecked);
|
|
|
|
}
|
|
|
|
if(m_model->suspended())
|
|
|
|
{
|
|
|
|
ui->trackLogCheckbox->setCheckState(Qt::Unchecked);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui->trackLogCheckbox->setCheckState(Qt::Checked);
|
|
|
|
}
|
2017-12-18 05:49:43 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void LogPage::UIToModelState()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(!m_model)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_model->setLineWrap(ui->wrapCheckbox->checkState() == Qt::Checked);
|
|
|
|
m_model->suspend(ui->trackLogCheckbox->checkState() != Qt::Checked);
|
2017-12-18 05:49:43 +05:30
|
|
|
}
|
|
|
|
|
2019-04-08 03:29:04 +05:30
|
|
|
void LogPage::setInstanceLaunchTaskChanged(shared_qobject_ptr<LaunchTask> proc, bool initial)
|
2016-08-06 19:09:29 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
m_process = proc;
|
|
|
|
if(m_process)
|
|
|
|
{
|
|
|
|
m_model = proc->getLogModel();
|
|
|
|
m_proxy->setSourceModel(m_model.get());
|
|
|
|
if(initial)
|
|
|
|
{
|
|
|
|
modelStateToUI();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
UIToModelState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_proxy->setSourceModel(nullptr);
|
|
|
|
m_model.reset();
|
|
|
|
}
|
2016-08-06 19:09:29 +05:30
|
|
|
}
|
|
|
|
|
2019-04-08 03:29:04 +05:30
|
|
|
void LogPage::onInstanceLaunchTaskChanged(shared_qobject_ptr<LaunchTask> proc)
|
2017-12-18 05:49:43 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
setInstanceLaunchTaskChanged(proc, false);
|
2017-12-18 05:49:43 +05:30
|
|
|
}
|
|
|
|
|
2014-06-30 05:32:57 +05:30
|
|
|
bool LogPage::apply()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return true;
|
2014-06-30 05:32:57 +05:30
|
|
|
}
|
|
|
|
|
2014-07-12 21:28:23 +05:30
|
|
|
bool LogPage::shouldDisplay() const
|
2014-06-30 05:32:57 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return m_instance->isRunning() || m_proxy->rowCount() > 0;
|
2014-06-30 05:32:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void LogPage::on_btnPaste_clicked()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(!m_model)
|
|
|
|
return;
|
|
|
|
|
|
|
|
//FIXME: turn this into a proper task and move the upload logic out of GuiUtil!
|
2021-10-18 04:17:02 +05:30
|
|
|
m_model->append(
|
|
|
|
MessageLevel::Launcher,
|
2022-12-14 20:32:04 +05:30
|
|
|
QString("Log upload triggered at: %1").arg(
|
|
|
|
QDateTime::currentDateTime().toString(Qt::RFC2822Date)
|
2021-10-18 04:17:02 +05:30
|
|
|
)
|
|
|
|
);
|
2022-12-14 20:32:04 +05:30
|
|
|
auto url = GuiUtil::uploadPaste(tr("Minecraft Log"), m_model->toPlainText(), this);
|
|
|
|
if(url == "canceled")
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
2022-12-14 20:32:04 +05:30
|
|
|
m_model->append(MessageLevel::Error, QString("Log upload canceled"));
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2022-12-14 20:32:04 +05:30
|
|
|
else if(!url.isEmpty())
|
2018-07-15 18:21:05 +05:30
|
|
|
{
|
2022-12-14 20:32:04 +05:30
|
|
|
m_model->append(MessageLevel::Launcher, QString("Log uploaded to: %1").arg(url));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
m_model->append(MessageLevel::Error, QString("Log upload failed!"));
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2014-06-30 05:32:57 +05:30
|
|
|
}
|
|
|
|
|
2014-07-11 05:20:36 +05:30
|
|
|
void LogPage::on_btnCopy_clicked()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(!m_model)
|
|
|
|
return;
|
2021-10-18 04:17:02 +05:30
|
|
|
m_model->append(MessageLevel::Launcher, QString("Clipboard copy at: %1").arg(QDateTime::currentDateTime().toString(Qt::RFC2822Date)));
|
2018-07-15 18:21:05 +05:30
|
|
|
GuiUtil::setClipboardText(m_model->toPlainText());
|
2014-07-11 05:20:36 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void LogPage::on_btnClear_clicked()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(!m_model)
|
|
|
|
return;
|
|
|
|
m_model->clear();
|
|
|
|
m_container->refreshContainer();
|
2014-07-11 05:20:36 +05:30
|
|
|
}
|
|
|
|
|
2015-04-29 04:58:58 +05:30
|
|
|
void LogPage::on_btnBottom_clicked()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
ui->text->scrollToBottom();
|
2015-04-29 04:58:58 +05:30
|
|
|
}
|
|
|
|
|
2014-11-09 05:29:22 +05:30
|
|
|
void LogPage::on_trackLogCheckbox_clicked(bool checked)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
if(!m_model)
|
|
|
|
return;
|
|
|
|
m_model->suspend(!checked);
|
2014-11-09 05:29:22 +05:30
|
|
|
}
|
|
|
|
|
2016-04-11 01:22:01 +05:30
|
|
|
void LogPage::on_wrapCheckbox_clicked(bool checked)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
ui->text->setWordWrap(checked);
|
|
|
|
if(!m_model)
|
|
|
|
return;
|
|
|
|
m_model->setLineWrap(checked);
|
2016-04-11 01:22:01 +05:30
|
|
|
}
|
|
|
|
|
2016-08-10 12:11:58 +05:30
|
|
|
void LogPage::on_findButton_clicked()
|
2014-11-09 05:29:22 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
auto modifiers = QApplication::keyboardModifiers();
|
|
|
|
bool reverse = modifiers & Qt::ShiftModifier;
|
|
|
|
ui->text->findNext(ui->searchBar->text(), reverse);
|
2014-11-09 05:29:22 +05:30
|
|
|
}
|
|
|
|
|
2016-08-19 01:01:37 +05:30
|
|
|
void LogPage::findNextActivated()
|
2014-11-09 05:29:22 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
ui->text->findNext(ui->searchBar->text(), false);
|
2014-11-09 05:29:22 +05:30
|
|
|
}
|
|
|
|
|
2016-08-19 01:01:37 +05:30
|
|
|
void LogPage::findPreviousActivated()
|
2014-11-09 05:29:22 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
ui->text->findNext(ui->searchBar->text(), true);
|
2014-11-09 05:29:22 +05:30
|
|
|
}
|
|
|
|
|
2016-08-19 01:01:37 +05:30
|
|
|
void LogPage::findActivated()
|
2014-11-09 05:29:22 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
// focus the search bar if it doesn't have focus
|
|
|
|
if (!ui->searchBar->hasFocus())
|
|
|
|
{
|
|
|
|
ui->searchBar->setFocus();
|
|
|
|
ui->searchBar->selectAll();
|
|
|
|
}
|
2014-11-09 05:29:22 +05:30
|
|
|
}
|
2022-02-22 23:53:53 +05:30
|
|
|
|
|
|
|
void LogPage::retranslate()
|
|
|
|
{
|
|
|
|
ui->retranslateUi(this);
|
|
|
|
}
|