Merge remote-tracking branch 'upstream/develop' into rework-settings

Signed-off-by: TheKodeToad <TheKodeToad@proton.me>
This commit is contained in:
TheKodeToad 2025-05-22 23:04:57 +01:00
commit 618e6bd96b
No known key found for this signature in database
GPG key ID: 5E39D70B4C93C38E
295 changed files with 11681 additions and 2609 deletions

View file

@ -146,10 +146,7 @@ void ExternalResourcesPage::openedImpl()
m_model->startWatching();
auto const setting_name = QString("WideBarVisibility_%1").arg(id());
if (!APPLICATION->settings()->contains(setting_name))
m_wide_bar_setting = APPLICATION->settings()->registerSetting(setting_name);
else
m_wide_bar_setting = APPLICATION->settings()->getSetting(setting_name);
m_wide_bar_setting = APPLICATION->settings()->getOrRegisterSetting(setting_name);
ui->actionsToolbar->setVisibilityState(m_wide_bar_setting->get().toByteArray());
}

View file

@ -35,17 +35,18 @@
#pragma once
#include <QWidget>
#include "Application.h"
#include "BaseInstance.h"
#include "ui/pages/BasePage.h"
#include "ui/widgets/MinecraftSettingsWidget.h"
#include <QWidget>
class InstanceSettingsPage : public MinecraftSettingsWidget, public BasePage {
Q_OBJECT
public:
explicit InstanceSettingsPage(MinecraftInstancePtr instance, QWidget* parent = nullptr) : MinecraftSettingsWidget(std::move(instance), parent)
explicit InstanceSettingsPage(MinecraftInstancePtr instance, QWidget* parent = nullptr)
: MinecraftSettingsWidget(std::move(instance), parent)
{
connect(APPLICATION, &Application::globalSettingsAboutToOpen, this, &InstanceSettingsPage::saveSettings);
connect(APPLICATION, &Application::globalSettingsApplied, this, &InstanceSettingsPage::loadSettings);

View file

@ -52,90 +52,81 @@
#include <BuildConfig.h>
class LogFormatProxyModel : public QIdentityProxyModel {
public:
LogFormatProxyModel(QObject* parent = nullptr) : QIdentityProxyModel(parent) {}
QVariant data(const QModelIndex& index, int role) const override
{
const LogColors& colors = APPLICATION->themeManager()->getLogColors();
QVariant LogFormatProxyModel::data(const QModelIndex& index, int role) const
{
const LogColors& colors = APPLICATION->themeManager()->getLogColors();
switch (role) {
case Qt::FontRole:
return m_font;
case Qt::ForegroundRole: {
auto level = static_cast<MessageLevel::Enum>(QIdentityProxyModel::data(index, LogModel::LevelRole).toInt());
QColor result = colors.foreground.value(level);
switch (role) {
case Qt::FontRole:
return m_font;
case Qt::ForegroundRole: {
auto level = static_cast<MessageLevel::Enum>(QIdentityProxyModel::data(index, LogModel::LevelRole).toInt());
QColor result = colors.foreground.value(level);
if (result.isValid())
return result;
if (result.isValid())
return result;
break;
}
case Qt::BackgroundRole: {
auto level = static_cast<MessageLevel::Enum>(QIdentityProxyModel::data(index, LogModel::LevelRole).toInt());
QColor result = colors.background.value(level);
if (result.isValid())
return result;
break;
}
break;
}
case Qt::BackgroundRole: {
auto level = static_cast<MessageLevel::Enum>(QIdentityProxyModel::data(index, LogModel::LevelRole).toInt());
QColor result = colors.background.value(level);
return QIdentityProxyModel::data(index, role);
if (result.isValid())
return result;
break;
}
}
void setFont(QFont font) { m_font = font; }
return QIdentityProxyModel::data(index, role);
}
QModelIndex find(const QModelIndex& start, const QString& value, bool reverse) const
{
QModelIndex parentIndex = parent(start);
auto compare = [this, start, parentIndex, value](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;
QModelIndex LogFormatProxyModel::find(const QModelIndex& start, const QString& value, bool reverse) const
{
QModelIndex parentIndex = parent(start);
auto compare = [this, start, parentIndex, value](int r) -> QModelIndex {
QModelIndex idx = index(r, start.column(), parentIndex);
if (!idx.isValid() || idx == start) {
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();
}
}
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;
private:
QFont m_font;
};
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();
}
LogPage::LogPage(InstancePtr instance, QWidget* parent) : QWidget(parent), ui(new Ui::LogPage), m_instance(instance)
{
@ -189,6 +180,13 @@ void LogPage::modelStateToUI()
ui->text->setWordWrap(false);
ui->wrapCheckbox->setCheckState(Qt::Unchecked);
}
if (m_model->colorLines()) {
ui->text->setColorLines(true);
ui->colorCheckbox->setCheckState(Qt::Checked);
} else {
ui->text->setColorLines(false);
ui->colorCheckbox->setCheckState(Qt::Unchecked);
}
if (m_model->suspended()) {
ui->trackLogCheckbox->setCheckState(Qt::Unchecked);
} else {
@ -202,6 +200,7 @@ void LogPage::UIToModelState()
return;
}
m_model->setLineWrap(ui->wrapCheckbox->checkState() == Qt::Checked);
m_model->setColorLines(ui->colorCheckbox->checkState() == Qt::Checked);
m_model->suspend(ui->trackLogCheckbox->checkState() != Qt::Checked);
}
@ -291,6 +290,14 @@ void LogPage::on_wrapCheckbox_clicked(bool checked)
m_model->setLineWrap(checked);
}
void LogPage::on_colorCheckbox_clicked(bool checked)
{
ui->text->setColorLines(checked);
if (!m_model)
return;
m_model->setColorLines(checked);
}
void LogPage::on_findButton_clicked()
{
auto modifiers = QApplication::keyboardModifiers();

View file

@ -35,6 +35,7 @@
#pragma once
#include <QIdentityProxyModel>
#include <QWidget>
#include <Application.h>
@ -46,7 +47,18 @@ namespace Ui {
class LogPage;
}
class QTextCharFormat;
class LogFormatProxyModel;
class LogFormatProxyModel : public QIdentityProxyModel {
public:
LogFormatProxyModel(QObject* parent = nullptr) : QIdentityProxyModel(parent) {}
QVariant data(const QModelIndex& index, int role) const override;
QFont getFont() const { return m_font; }
void setFont(QFont font) { m_font = font; }
QModelIndex find(const QModelIndex& start, const QString& value, bool reverse) const;
private:
QFont m_font;
};
class LogPage : public QWidget, public BasePage {
Q_OBJECT
@ -70,6 +82,7 @@ class LogPage : public QWidget, public BasePage {
void on_trackLogCheckbox_clicked(bool checked);
void on_wrapCheckbox_clicked(bool checked);
void on_colorCheckbox_clicked(bool checked);
void on_findButton_clicked();
void findActivated();

View file

@ -74,6 +74,16 @@
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="colorCheckbox">
<property name="text">
<string>Color lines</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
@ -170,6 +180,7 @@
<tabstop>tabWidget</tabstop>
<tabstop>trackLogCheckbox</tabstop>
<tabstop>wrapCheckbox</tabstop>
<tabstop>colorCheckbox</tabstop>
<tabstop>btnCopy</tabstop>
<tabstop>btnPaste</tabstop>
<tabstop>btnClear</tabstop>

View file

@ -347,13 +347,18 @@ void ManagedPackPage::onUpdateTaskCompleted(bool did_succeed) const
if (m_instance_window != nullptr)
m_instance_window->close();
CustomMessageBox::selectable(nullptr, tr("Update Successful"), tr("The instance updated to pack version %1 successfully.").arg(m_inst->getManagedPackVersionName()), QMessageBox::Information)
->show();
CustomMessageBox::selectable(nullptr, tr("Update Successful"),
tr("The instance updated to pack version %1 successfully.").arg(m_inst->getManagedPackVersionName()),
QMessageBox::Information)
->show();
} else {
CustomMessageBox::selectable(nullptr, tr("Update Failed"), tr("The instance failed to update to pack version %1. Please check launcher logs for more information.").arg(m_inst->getManagedPackVersionName()), QMessageBox::Critical)
->show();
CustomMessageBox::selectable(
nullptr, tr("Update Failed"),
tr("The instance failed to update to pack version %1. Please check launcher logs for more information.")
.arg(m_inst->getManagedPackVersionName()),
QMessageBox::Critical)
->show();
}
}
void ModrinthManagedPackPage::update()

View file

@ -1,21 +1,22 @@
#include <QObject>
#include <QTcpSocket>
#include <qtconcurrentrun.h>
#include <QJsonDocument>
#include <QJsonObject>
#include <qtconcurrentrun.h>
#include <QObject>
#include <QTcpSocket>
#include <Exception.h>
#include "McClient.h"
#include "Json.h"
#include "McClient.h"
// 7 first bits
// 7 first bits
#define SEGMENT_BITS 0x7F
// last bit
#define CONTINUE_BIT 0x80
McClient::McClient(QObject *parent, QString domain, QString ip, short port): QObject(parent), m_domain(domain), m_ip(ip), m_port(port) {}
McClient::McClient(QObject* parent, QString domain, QString ip, short port) : QObject(parent), m_domain(domain), m_ip(ip), m_port(port) {}
void McClient::getStatusData() {
void McClient::getStatusData()
{
qDebug() << "Connecting to socket..";
connect(&m_socket, &QTcpSocket::connected, this, [this]() {
@ -25,28 +26,28 @@ void McClient::getStatusData() {
connect(&m_socket, &QTcpSocket::readyRead, this, &McClient::readRawResponse);
});
connect(&m_socket, &QTcpSocket::errorOccurred, this, [this]() {
emitFail("Socket disconnected: " + m_socket.errorString());
});
connect(&m_socket, &QTcpSocket::errorOccurred, this, [this]() { emitFail("Socket disconnected: " + m_socket.errorString()); });
m_socket.connectToHost(m_ip, m_port);
}
void McClient::sendRequest() {
void McClient::sendRequest()
{
QByteArray data;
writeVarInt(data, 0x00); // packet ID
writeVarInt(data, 763); // hardcoded protocol version (763 = 1.20.1)
writeVarInt(data, m_domain.size()); // server address length
writeString(data, m_domain.toStdString()); // server address
writeFixedInt(data, m_port, 2); // server port
writeVarInt(data, 0x01); // next state
writePacketToSocket(data); // send handshake packet
writeVarInt(data, 0x00); // packet ID
writeVarInt(data, 763); // hardcoded protocol version (763 = 1.20.1)
writeVarInt(data, m_domain.size()); // server address length
writeString(data, m_domain.toStdString()); // server address
writeFixedInt(data, m_port, 2); // server port
writeVarInt(data, 0x01); // next state
writePacketToSocket(data); // send handshake packet
writeVarInt(data, 0x00); // packet ID
writePacketToSocket(data); // send status packet
writeVarInt(data, 0x00); // packet ID
writePacketToSocket(data); // send status packet
}
void McClient::readRawResponse() {
void McClient::readRawResponse()
{
if (m_responseReadState == 2) {
return;
}
@ -56,28 +57,27 @@ void McClient::readRawResponse() {
m_wantedRespLength = readVarInt(m_resp);
m_responseReadState = 1;
}
if (m_responseReadState == 1 && m_resp.size() >= m_wantedRespLength) {
if (m_resp.size() > m_wantedRespLength) {
qDebug() << "Warning: Packet length doesn't match actual packet size (" << m_wantedRespLength << " expected vs " << m_resp.size() << " received)";
qDebug() << "Warning: Packet length doesn't match actual packet size (" << m_wantedRespLength << " expected vs "
<< m_resp.size() << " received)";
}
parseResponse();
m_responseReadState = 2;
}
}
void McClient::parseResponse() {
void McClient::parseResponse()
{
qDebug() << "Received response successfully";
int packetID = readVarInt(m_resp);
if (packetID != 0x00) {
throw Exception(
QString("Packet ID doesn't match expected value (0x00 vs 0x%1)")
.arg(packetID, 0, 16)
);
throw Exception(QString("Packet ID doesn't match expected value (0x00 vs 0x%1)").arg(packetID, 0, 16));
}
Q_UNUSED(readVarInt(m_resp)); // json length
Q_UNUSED(readVarInt(m_resp)); // json length
// 'resp' should now be the JSON string
QJsonDocument doc = QJsonDocument::fromJson(m_resp);
@ -85,8 +85,9 @@ void McClient::parseResponse() {
}
// From https://wiki.vg/Protocol#VarInt_and_VarLong
void McClient::writeVarInt(QByteArray &data, int value) {
while ((value & ~SEGMENT_BITS)) { // check if the value is too big to fit in 7 bits
void McClient::writeVarInt(QByteArray& data, int value)
{
while ((value & ~SEGMENT_BITS)) { // check if the value is too big to fit in 7 bits
// Write 7 bits
data.append((value & SEGMENT_BITS) | CONTINUE_BIT);
@ -98,7 +99,8 @@ void McClient::writeVarInt(QByteArray &data, int value) {
}
// From https://wiki.vg/Protocol#VarInt_and_VarLong
int McClient::readVarInt(QByteArray &data) {
int McClient::readVarInt(QByteArray& data)
{
int value = 0;
int position = 0;
char currentByte;
@ -107,17 +109,20 @@ int McClient::readVarInt(QByteArray &data) {
currentByte = readByte(data);
value |= (currentByte & SEGMENT_BITS) << position;
if ((currentByte & CONTINUE_BIT) == 0) break;
if ((currentByte & CONTINUE_BIT) == 0)
break;
position += 7;
}
if (position >= 32) throw Exception("VarInt is too big");
if (position >= 32)
throw Exception("VarInt is too big");
return value;
}
char McClient::readByte(QByteArray &data) {
char McClient::readByte(QByteArray& data)
{
if (data.isEmpty()) {
throw Exception("No more bytes to read");
}
@ -128,17 +133,20 @@ char McClient::readByte(QByteArray &data) {
}
// write number with specified size in big endian format
void McClient::writeFixedInt(QByteArray &data, int value, int size) {
void McClient::writeFixedInt(QByteArray& data, int value, int size)
{
for (int i = size - 1; i >= 0; i--) {
data.append((value >> (i * 8)) & 0xFF);
}
}
void McClient::writeString(QByteArray &data, const std::string &value) {
void McClient::writeString(QByteArray& data, const std::string& value)
{
data.append(value.c_str());
}
void McClient::writePacketToSocket(QByteArray &data) {
void McClient::writePacketToSocket(QByteArray& data)
{
// we prefix the packet with its length
QByteArray dataWithSize;
writeVarInt(dataWithSize, data.size());
@ -151,14 +159,15 @@ void McClient::writePacketToSocket(QByteArray &data) {
data.clear();
}
void McClient::emitFail(QString error) {
void McClient::emitFail(QString error)
{
qDebug() << "Minecraft server ping for status error:" << error;
emit failed(error);
emit finished();
}
void McClient::emitSucceed(QJsonObject data) {
void McClient::emitSucceed(QJsonObject data)
{
emit succeeded(data);
emit finished();
}

View file

@ -1,8 +1,8 @@
#include <QObject>
#include <QTcpSocket>
#include <QFuture>
#include <QJsonDocument>
#include <QJsonObject>
#include <QFuture>
#include <QObject>
#include <QTcpSocket>
#include <Exception.h>
@ -22,29 +22,30 @@ class McClient : public QObject {
unsigned m_wantedRespLength = 0;
QByteArray m_resp;
public:
explicit McClient(QObject *parent, QString domain, QString ip, short port);
public:
explicit McClient(QObject* parent, QString domain, QString ip, short port);
//! Read status data of the server, and calls the succeeded() signal with the parsed JSON data
void getStatusData();
private:
private:
void sendRequest();
//! Accumulate data until we have a full response, then call parseResponse() once
void readRawResponse();
void parseResponse();
void writeVarInt(QByteArray &data, int value);
int readVarInt(QByteArray &data);
char readByte(QByteArray &data);
void writeVarInt(QByteArray& data, int value);
int readVarInt(QByteArray& data);
char readByte(QByteArray& data);
//! write number with specified size in big endian format
void writeFixedInt(QByteArray &data, int value, int size);
void writeString(QByteArray &data, const std::string &value);
void writeFixedInt(QByteArray& data, int value, int size);
void writeString(QByteArray& data, const std::string& value);
void writePacketToSocket(QByteArray &data);
void writePacketToSocket(QByteArray& data);
void emitFail(QString error);
void emitSucceed(QJsonObject data);
signals:
signals:
void succeeded(QJsonObject data);
void failed(QString error);
void finished();

View file

@ -1,23 +1,25 @@
#include <QObject>
#include <QDnsLookup>
#include <QtNetwork/qtcpsocket.h>
#include <QDnsLookup>
#include <QHostInfo>
#include <QObject>
#include "McResolver.h"
McResolver::McResolver(QObject *parent, QString domain, int port): QObject(parent), m_constrDomain(domain), m_constrPort(port) {}
McResolver::McResolver(QObject* parent, QString domain, int port) : QObject(parent), m_constrDomain(domain), m_constrPort(port) {}
void McResolver::ping() {
void McResolver::ping()
{
pingWithDomainSRV(m_constrDomain, m_constrPort);
}
void McResolver::pingWithDomainSRV(QString domain, int port) {
QDnsLookup *lookup = new QDnsLookup(this);
void McResolver::pingWithDomainSRV(QString domain, int port)
{
QDnsLookup* lookup = new QDnsLookup(this);
lookup->setName(QString("_minecraft._tcp.%1").arg(domain));
lookup->setType(QDnsLookup::SRV);
connect(lookup, &QDnsLookup::finished, this, [this, domain, port]() {
QDnsLookup *lookup = qobject_cast<QDnsLookup *>(sender());
QDnsLookup* lookup = qobject_cast<QDnsLookup*>(sender());
lookup->deleteLater();
@ -43,8 +45,9 @@ void McResolver::pingWithDomainSRV(QString domain, int port) {
lookup->lookup();
}
void McResolver::pingWithDomainA(QString domain, int port) {
QHostInfo::lookupHost(domain, this, [this, port](const QHostInfo &hostInfo){
void McResolver::pingWithDomainA(QString domain, int port)
{
QHostInfo::lookupHost(domain, this, [this, port](const QHostInfo& hostInfo) {
if (hostInfo.error() != QHostInfo::NoError) {
emitFail("A record lookup failed");
return;
@ -55,19 +58,21 @@ void McResolver::pingWithDomainA(QString domain, int port) {
emitFail("No A entries found for domain");
return;
}
const auto& firstRecord = records.at(0);
emitSucceed(firstRecord.toString(), port);
});
});
}
void McResolver::emitFail(QString error) {
void McResolver::emitFail(QString error)
{
qDebug() << "DNS resolver error:" << error;
emit failed(error);
emit finished();
}
void McResolver::emitSucceed(QString ip, int port) {
void McResolver::emitSucceed(QString ip, int port)
{
emit succeeded(ip, port);
emit finished();
}

View file

@ -1,8 +1,8 @@
#include <QtNetwork/qtcpsocket.h>
#include <QDnsLookup>
#include <QHostInfo>
#include <QObject>
#include <QString>
#include <QDnsLookup>
#include <QtNetwork/qtcpsocket.h>
#include <QHostInfo>
// resolve the IP and port of a Minecraft server
class McResolver : public QObject {
@ -11,17 +11,17 @@ class McResolver : public QObject {
QString m_constrDomain;
int m_constrPort;
public:
explicit McResolver(QObject *parent, QString domain, int port);
public:
explicit McResolver(QObject* parent, QString domain, int port);
void ping();
private:
private:
void pingWithDomainSRV(QString domain, int port);
void pingWithDomainA(QString domain, int port);
void emitFail(QString error);
void emitSucceed(QString ip, int port);
signals:
signals:
void succeeded(QString ip, int port);
void failed(QString error);
void finished();

View file

@ -48,6 +48,7 @@
#include <QMessageBox>
#include <QSortFilterProxyModel>
#include <algorithm>
#include <memory>
#include "Application.h"
@ -146,8 +147,16 @@ void ModFolderPage::downloadMods()
return;
}
ResourceDownload::ModDownloadDialog mdownload(this, m_model, m_instance);
if (mdownload.exec()) {
m_downloadDialog = new ResourceDownload::ModDownloadDialog(this, m_model, m_instance);
connect(this, &QObject::destroyed, m_downloadDialog, &QDialog::close);
connect(m_downloadDialog, &QDialog::finished, this, &ModFolderPage::downloadDialogFinished);
m_downloadDialog->open();
}
void ModFolderPage::downloadDialogFinished(int result)
{
if (result) {
auto tasks = new ConcurrentTask(tr("Download Mods"), APPLICATION->settings()->get("NumberOfConcurrentDownloads").toInt());
connect(tasks, &Task::failed, [this, tasks](QString reason) {
CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show();
@ -165,8 +174,12 @@ void ModFolderPage::downloadMods()
tasks->deleteLater();
});
for (auto& task : mdownload.getTasks()) {
tasks->addTask(task);
if (m_downloadDialog) {
for (auto& task : m_downloadDialog->getTasks()) {
tasks->addTask(task);
}
} else {
qWarning() << "ResourceDownloadDialog vanished before we could collect tasks!";
}
ProgressDialog loadDialog(this);
@ -175,6 +188,8 @@ void ModFolderPage::downloadMods()
m_model->update();
}
if (m_downloadDialog)
m_downloadDialog->deleteLater();
}
void ModFolderPage::updateMods(bool includeDeps)
@ -300,36 +315,12 @@ void ModFolderPage::changeModVersion()
if (mods_list.length() != 1 || mods_list[0]->metadata() == nullptr)
return;
ResourceDownload::ModDownloadDialog mdownload(this, m_model, m_instance);
mdownload.setResourceMetadata((*mods_list.begin())->metadata());
if (mdownload.exec()) {
auto tasks = new ConcurrentTask("Download Mods", APPLICATION->settings()->get("NumberOfConcurrentDownloads").toInt());
connect(tasks, &Task::failed, [this, tasks](QString reason) {
CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show();
tasks->deleteLater();
});
connect(tasks, &Task::aborted, [this, tasks]() {
CustomMessageBox::selectable(this, tr("Aborted"), tr("Download stopped by user."), QMessageBox::Information)->show();
tasks->deleteLater();
});
connect(tasks, &Task::succeeded, [this, tasks]() {
QStringList warnings = tasks->warnings();
if (warnings.count())
CustomMessageBox::selectable(this, tr("Warnings"), warnings.join('\n'), QMessageBox::Warning)->show();
m_downloadDialog = new ResourceDownload::ModDownloadDialog(this, m_model, m_instance);
connect(this, &QObject::destroyed, m_downloadDialog, &QDialog::close);
connect(m_downloadDialog, &QDialog::finished, this, &ModFolderPage::downloadDialogFinished);
tasks->deleteLater();
});
for (auto& task : mdownload.getTasks()) {
tasks->addTask(task);
}
ProgressDialog loadDialog(this);
loadDialog.setSkipButton(true, tr("Abort"));
loadDialog.execWithTask(tasks);
m_model->update();
}
m_downloadDialog->setResourceMetadata((*mods_list.begin())->metadata());
m_downloadDialog->open();
}
void ModFolderPage::exportModMetadata()

View file

@ -38,7 +38,9 @@
#pragma once
#include <QPointer>
#include "ExternalResourcesPage.h"
#include "ui/dialogs/ResourceDownloadDialog.h"
class ModFolderPage : public ExternalResourcesPage {
Q_OBJECT
@ -63,6 +65,7 @@ class ModFolderPage : public ExternalResourcesPage {
void removeItems(const QItemSelection& selection) override;
void downloadMods();
void downloadDialogFinished(int result);
void updateMods(bool includeDeps = false);
void deleteModMetadata();
void exportModMetadata();
@ -70,6 +73,7 @@ class ModFolderPage : public ExternalResourcesPage {
protected:
std::shared_ptr<ModFolderModel> m_model;
QPointer<ResourceDownload::ModDownloadDialog> m_downloadDialog;
};
class CoreModFolderPage : public ModFolderPage {

View file

@ -43,20 +43,44 @@
#include <FileSystem.h>
#include <GZip.h>
#include <QDir>
#include <QDirIterator>
#include <QFileSystemWatcher>
#include <QShortcut>
#include "RecursiveFileSystemWatcher.h"
#include <QUrl>
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))
OtherLogsPage::OtherLogsPage(InstancePtr instance, QWidget* parent)
: QWidget(parent)
, ui(new Ui::OtherLogsPage)
, m_instance(instance)
, m_basePath(instance->gameRoot())
, m_logSearchPaths(instance->getLogFileSearchPaths())
, m_model(new LogModel(this))
{
ui->setupUi(this);
ui->tabWidget->tabBar()->hide();
m_watcher->setMatcher(fileFilter);
m_watcher->setRootDir(QDir::current().absoluteFilePath(m_path));
m_proxy = new LogFormatProxyModel(this);
connect(m_watcher, &RecursiveFileSystemWatcher::filesChanged, this, &OtherLogsPage::populateSelectLogBox);
populateSelectLogBox();
// set up fonts in the log proxy
{
QString fontFamily = APPLICATION->settings()->get("ConsoleFont").toString();
bool conversionOk = false;
int fontSize = APPLICATION->settings()->get("ConsoleFontSize").toInt(&conversionOk);
if (!conversionOk) {
fontSize = 11;
}
m_proxy->setFont(QFont(fontFamily, fontSize));
}
ui->text->setModel(m_proxy);
m_model->setMaxLines(m_instance->getConsoleMaxLines());
m_model->setStopOnOverflow(m_instance->shouldStopOnConsoleOverflow());
m_model->setOverflowMessage(tr("Cannot display this log since the log length surpassed %1 lines.").arg(m_model->getMaxLines()));
m_proxy->setSourceModel(m_model.get());
connect(&m_watcher, &QFileSystemWatcher::directoryChanged, this, &OtherLogsPage::populateSelectLogBox);
auto findShortcut = new QShortcut(QKeySequence(QKeySequence::Find), this);
connect(findShortcut, &QShortcut::activated, this, &OtherLogsPage::findActivated);
@ -82,29 +106,54 @@ void OtherLogsPage::retranslate()
void OtherLogsPage::openedImpl()
{
m_watcher->enable();
const QStringList failedPaths = m_watcher.addPaths(m_logSearchPaths);
for (const QString& path : m_logSearchPaths) {
if (failedPaths.contains(path))
qDebug() << "Failed to start watching" << path;
else
qDebug() << "Started watching" << path;
}
populateSelectLogBox();
}
void OtherLogsPage::closedImpl()
{
m_watcher->disable();
const QStringList failedPaths = m_watcher.removePaths(m_logSearchPaths);
for (const QString& path : m_logSearchPaths) {
if (failedPaths.contains(path))
qDebug() << "Failed to stop watching" << path;
else
qDebug() << "Stopped watching" << path;
}
}
void OtherLogsPage::populateSelectLogBox()
{
const QString prevCurrentFile = m_currentFile;
ui->selectLogBox->blockSignals(true);
ui->selectLogBox->clear();
ui->selectLogBox->addItems(m_watcher->files());
if (m_currentFile.isEmpty()) {
setControlsEnabled(false);
ui->selectLogBox->setCurrentIndex(-1);
} else {
const int index = ui->selectLogBox->findText(m_currentFile);
ui->selectLogBox->addItems(getPaths());
ui->selectLogBox->blockSignals(false);
if (!prevCurrentFile.isEmpty()) {
const int index = ui->selectLogBox->findText(prevCurrentFile);
if (index != -1) {
ui->selectLogBox->blockSignals(true);
ui->selectLogBox->setCurrentIndex(index);
ui->selectLogBox->blockSignals(false);
setControlsEnabled(true);
// don't refresh file
return;
} else {
setControlsEnabled(false);
}
}
on_selectLogBox_currentIndexChanged(ui->selectLogBox->currentIndex());
}
void OtherLogsPage::on_selectLogBox_currentIndexChanged(const int index)
@ -114,7 +163,7 @@ void OtherLogsPage::on_selectLogBox_currentIndexChanged(const int index)
file = ui->selectLogBox->itemText(index);
}
if (file.isEmpty() || !QFile::exists(FS::PathCombine(m_path, file))) {
if (file.isEmpty() || !QFile::exists(FS::PathCombine(m_basePath, file))) {
m_currentFile = QString();
ui->text->clear();
setControlsEnabled(false);
@ -131,7 +180,7 @@ void OtherLogsPage::on_btnReload_clicked()
setControlsEnabled(false);
return;
}
QFile file(FS::PathCombine(m_path, m_currentFile));
QFile file(FS::PathCombine(m_basePath, m_currentFile));
if (!file.open(QFile::ReadOnly)) {
setControlsEnabled(false);
ui->btnReload->setEnabled(true); // allow reload
@ -139,14 +188,8 @@ void OtherLogsPage::on_btnReload_clicked()
QMessageBox::critical(this, tr("Error"), tr("Unable to open %1 for reading: %2").arg(m_currentFile, file.errorString()));
} else {
auto setPlainText = [this](const QString& text) {
QString fontFamily = APPLICATION->settings()->get("ConsoleFont").toString();
bool conversionOk = false;
int fontSize = APPLICATION->settings()->get("ConsoleFontSize").toInt(&conversionOk);
if (!conversionOk) {
fontSize = 11;
}
QTextDocument* doc = ui->text->document();
doc->setDefaultFont(QFont(fontFamily, fontSize));
doc->setDefaultFont(m_proxy->getFont());
ui->text->setPlainText(text);
};
auto showTooBig = [setPlainText, &file]() {
@ -158,22 +201,65 @@ void OtherLogsPage::on_btnReload_clicked()
showTooBig();
return;
}
QString content;
if (file.fileName().endsWith(".gz")) {
QByteArray temp;
if (!GZip::unzip(file.readAll(), temp)) {
setPlainText(tr("The file (%1) is not readable.").arg(file.fileName()));
return;
MessageLevel::Enum last = MessageLevel::Unknown;
auto handleLine = [this, &last](QString line) {
if (line.isEmpty())
return false;
if (line.back() == '\n')
line = line.remove(line.size() - 1, 1);
MessageLevel::Enum level = MessageLevel::Unknown;
// if the launcher part set a log level, use it
auto innerLevel = MessageLevel::fromLine(line);
if (innerLevel != MessageLevel::Unknown) {
level = innerLevel;
}
// If the level is still undetermined, guess level
if (level == MessageLevel::StdErr || level == MessageLevel::StdOut || level == MessageLevel::Unknown) {
level = LogParser::guessLevel(line, last);
}
last = level;
m_model->append(level, line);
return m_model->isOverFlow();
};
// Try to determine a level for each line
ui->text->clear();
ui->text->setModel(nullptr);
m_model->clear();
if (file.fileName().endsWith(".gz")) {
QString line;
auto error = GZip::readGzFileByBlocks(&file, [&line, handleLine](const QByteArray& d) {
auto block = d;
int newlineIndex = block.indexOf('\n');
while (newlineIndex != -1) {
line += QString::fromUtf8(block).left(newlineIndex);
block.remove(0, newlineIndex + 1);
if (handleLine(line)) {
line.clear();
return false;
}
line.clear();
newlineIndex = block.indexOf('\n');
}
line += QString::fromUtf8(block);
return true;
});
if (!error.isEmpty()) {
setPlainText(tr("The file (%1) encountered an error when reading: %2.").arg(file.fileName(), error));
return;
} else if (!line.isEmpty()) {
handleLine(line);
}
content = QString::fromUtf8(temp);
} else {
content = QString::fromUtf8(file.readAll());
while (!file.atEnd() && !handleLine(QString::fromUtf8(file.readLine()))) {
}
}
if (content.size() >= 50000000ll) {
showTooBig();
return;
}
setPlainText(content);
ui->text->setModel(m_proxy);
ui->text->scrollToBottom();
}
}
@ -187,6 +273,11 @@ void OtherLogsPage::on_btnCopy_clicked()
GuiUtil::setClipboardText(ui->text->toPlainText());
}
void OtherLogsPage::on_btnBottom_clicked()
{
ui->text->scrollToBottom();
}
void OtherLogsPage::on_btnDelete_clicked()
{
if (m_currentFile.isEmpty()) {
@ -201,7 +292,7 @@ void OtherLogsPage::on_btnDelete_clicked()
QMessageBox::Yes, QMessageBox::No) == QMessageBox::No) {
return;
}
QFile file(FS::PathCombine(m_path, m_currentFile));
QFile file(FS::PathCombine(m_basePath, m_currentFile));
if (FS::trash(file.fileName())) {
return;
@ -214,7 +305,7 @@ void OtherLogsPage::on_btnDelete_clicked()
void OtherLogsPage::on_btnClean_clicked()
{
auto toDelete = m_watcher->files();
auto toDelete = getPaths();
if (toDelete.isEmpty()) {
return;
}
@ -237,7 +328,9 @@ void OtherLogsPage::on_btnClean_clicked()
}
QStringList failed;
for (auto item : toDelete) {
QFile file(FS::PathCombine(m_path, item));
QString absolutePath = FS::PathCombine(m_basePath, item);
QFile file(absolutePath);
qDebug() << "Deleting log" << absolutePath;
if (FS::trash(file.fileName())) {
continue;
}
@ -263,6 +356,24 @@ void OtherLogsPage::on_btnClean_clicked()
}
}
void OtherLogsPage::on_wrapCheckbox_clicked(bool checked)
{
ui->text->setWordWrap(checked);
if (!m_model)
return;
m_model->setLineWrap(checked);
ui->text->scrollToBottom();
}
void OtherLogsPage::on_colorCheckbox_clicked(bool checked)
{
ui->text->setColorLines(checked);
if (!m_model)
return;
m_model->setColorLines(checked);
ui->text->scrollToBottom();
}
void OtherLogsPage::setControlsEnabled(const bool enabled)
{
ui->btnReload->setEnabled(enabled);
@ -273,27 +384,44 @@ void OtherLogsPage::setControlsEnabled(const bool enabled)
ui->btnClean->setEnabled(enabled);
}
// FIXME: HACK, use LogView instead?
static void findNext(QPlainTextEdit* _this, const QString& what, bool reverse)
QStringList OtherLogsPage::getPaths()
{
_this->find(what, reverse ? QTextDocument::FindFlag::FindBackward : QTextDocument::FindFlag(0));
QDir baseDir(m_basePath);
QStringList result;
for (QString searchPath : m_logSearchPaths) {
QDir searchDir(searchPath);
QStringList filters{ "*.log", "*.log.gz" };
if (searchPath != m_basePath)
filters.append("*.txt");
QStringList entries = searchDir.entryList(filters, QDir::Files | QDir::Readable, QDir::SortFlag::Time);
for (const QString& name : entries)
result.append(baseDir.relativeFilePath(searchDir.filePath(name)));
}
return result;
}
void OtherLogsPage::on_findButton_clicked()
{
auto modifiers = QApplication::keyboardModifiers();
bool reverse = modifiers & Qt::ShiftModifier;
findNext(ui->text, ui->searchBar->text(), reverse);
ui->text->findNext(ui->searchBar->text(), reverse);
}
void OtherLogsPage::findNextActivated()
{
findNext(ui->text, ui->searchBar->text(), false);
ui->text->findNext(ui->searchBar->text(), false);
}
void OtherLogsPage::findPreviousActivated()
{
findNext(ui->text, ui->searchBar->text(), true);
ui->text->findNext(ui->searchBar->text(), true);
}
void OtherLogsPage::findActivated()

View file

@ -39,6 +39,8 @@
#include <Application.h>
#include <pathmatcher/IPathMatcher.h>
#include <QFileSystemWatcher>
#include "LogPage.h"
#include "ui/pages/BasePage.h"
namespace Ui {
@ -51,7 +53,7 @@ class OtherLogsPage : public QWidget, public BasePage {
Q_OBJECT
public:
explicit OtherLogsPage(QString path, IPathMatcher::Ptr fileFilter, QWidget* parent = 0);
explicit OtherLogsPage(InstancePtr instance, QWidget* parent = 0);
~OtherLogsPage();
QString id() const override { return "logs"; }
@ -71,6 +73,10 @@ class OtherLogsPage : public QWidget, public BasePage {
void on_btnCopy_clicked();
void on_btnDelete_clicked();
void on_btnClean_clicked();
void on_btnBottom_clicked();
void on_wrapCheckbox_clicked(bool checked);
void on_colorCheckbox_clicked(bool checked);
void on_findButton_clicked();
void findActivated();
@ -80,10 +86,17 @@ class OtherLogsPage : public QWidget, public BasePage {
private:
void setControlsEnabled(bool enabled);
QStringList getPaths();
private:
Ui::OtherLogsPage* ui;
QString m_path;
InstancePtr m_instance;
/** Path to display log paths relative to. */
QString m_basePath;
QStringList m_logSearchPaths;
QString m_currentFile;
IPathMatcher::Ptr m_fileFilter;
RecursiveFileSystemWatcher* m_watcher;
QFileSystemWatcher m_watcher;
LogFormatProxyModel* m_proxy;
shared_qobject_ptr<LogModel> m_model;
};

View file

@ -33,90 +33,6 @@
<string notr="true">Tab 1</string>
</attribute>
<layout class="QGridLayout" name="gridLayout_2">
<item row="2" column="1">
<widget class="QLineEdit" name="searchBar"/>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="findButton">
<property name="text">
<string>Find</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="4">
<widget class="QPlainTextEdit" name="text">
<property name="enabled">
<bool>false</bool>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
</widget>
</item>
<item row="0" column="0" colspan="4">
<layout class="QGridLayout" name="gridLayout">
<item row="3" column="1">
<widget class="QPushButton" name="btnCopy">
<property name="toolTip">
<string>Copy the whole log into the clipboard</string>
</property>
<property name="text">
<string>&amp;Copy</string>
</property>
</widget>
</item>
<item row="3" column="3">
<widget class="QPushButton" name="btnDelete">
<property name="toolTip">
<string>Clear the log</string>
</property>
<property name="text">
<string>Delete</string>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QPushButton" name="btnPaste">
<property name="toolTip">
<string>Upload the log to the paste service configured in preferences.</string>
</property>
<property name="text">
<string>Upload</string>
</property>
</widget>
</item>
<item row="3" column="4">
<widget class="QPushButton" name="btnClean">
<property name="toolTip">
<string>Clear the log</string>
</property>
<property name="text">
<string>Clean</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QPushButton" name="btnReload">
<property name="text">
<string>Reload</string>
</property>
</widget>
</item>
<item row="0" column="0" colspan="5">
<widget class="QComboBox" name="selectLogBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label">
<property name="text">
@ -124,12 +40,173 @@
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="searchBar"/>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="findButton">
<property name="text">
<string>&amp;Find</string>
</property>
</widget>
</item>
<item row="2" column="3">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item row="2" column="4">
<widget class="QPushButton" name="btnBottom">
<property name="toolTip">
<string>Scroll all the way to bottom</string>
</property>
<property name="text">
<string>&amp;Bottom</string>
</property>
</widget>
</item>
<item row="1" column="0" colspan="5">
<widget class="LogView" name="text">
<property name="enabled">
<bool>false</bool>
</property>
<property name="undoRedoEnabled">
<bool>false</bool>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
<property name="plainText">
<string notr="true"/>
</property>
<property name="textInteractionFlags">
<set>Qt::LinksAccessibleByKeyboard|Qt::LinksAccessibleByMouse|Qt::TextBrowserInteraction|Qt::TextSelectableByKeyboard|Qt::TextSelectableByMouse</set>
</property>
<property name="centerOnScroll">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="0" colspan="5">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="5">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QComboBox" name="selectLogBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnDelete">
<property name="toolTip">
<string>Delete the selected log</string>
</property>
<property name="text">
<string>&amp;Delete Selected</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnClean">
<property name="toolTip">
<string>Delete all the logs</string>
</property>
<property name="text">
<string>Delete &amp;All</string>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0" colspan="5">
<layout class="QHBoxLayout" name="horizontalLayout1">
<item>
<widget class="QCheckBox" name="wrapCheckbox">
<property name="text">
<string>Wrap lines</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="colorCheckbox">
<property name="text">
<string>Color lines</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="btnCopy">
<property name="toolTip">
<string>Copy the whole log into the clipboard</string>
</property>
<property name="text">
<string>&amp;Copy</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnPaste">
<property name="toolTip">
<string>Upload the log to the paste service configured in preferences</string>
</property>
<property name="text">
<string>&amp;Upload</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnReload">
<property name="toolTip">
<string>Reload the contents of the log from the disk</string>
</property>
<property name="text">
<string>&amp;Reload</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>LogView</class>
<extends>QPlainTextEdit</extends>
<header>ui/widgets/LogView.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>tabWidget</tabstop>
<tabstop>selectLogBox</tabstop>
@ -138,6 +215,8 @@
<tabstop>btnPaste</tabstop>
<tabstop>btnDelete</tabstop>
<tabstop>btnClean</tabstop>
<tabstop>wrapCheckbox</tabstop>
<tabstop>colorCheckbox</tabstop>
<tabstop>text</tabstop>
<tabstop>searchBar</tabstop>
<tabstop>findButton</tabstop>

View file

@ -84,8 +84,16 @@ void ResourcePackPage::downloadResourcePacks()
if (m_instance->typeName() != "Minecraft")
return; // this is a null instance or a legacy instance
ResourceDownload::ResourcePackDownloadDialog mdownload(this, m_model, m_instance);
if (mdownload.exec()) {
m_downloadDialog = new ResourceDownload::ResourcePackDownloadDialog(this, m_model, m_instance);
connect(this, &QObject::destroyed, m_downloadDialog, &QDialog::close);
connect(m_downloadDialog, &QDialog::finished, this, &ResourcePackPage::downloadDialogFinished);
m_downloadDialog->open();
}
void ResourcePackPage::downloadDialogFinished(int result)
{
if (result) {
auto tasks = new ConcurrentTask("Download Resource Pack", APPLICATION->settings()->get("NumberOfConcurrentDownloads").toInt());
connect(tasks, &Task::failed, [this, tasks](QString reason) {
CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show();
@ -103,8 +111,12 @@ void ResourcePackPage::downloadResourcePacks()
tasks->deleteLater();
});
for (auto& task : mdownload.getTasks()) {
tasks->addTask(task);
if (m_downloadDialog) {
for (auto& task : m_downloadDialog->getTasks()) {
tasks->addTask(task);
}
} else {
qWarning() << "ResourceDownloadDialog vanished before we could collect tasks!";
}
ProgressDialog loadDialog(this);
@ -113,6 +125,8 @@ void ResourcePackPage::downloadResourcePacks()
m_model->update();
}
if (m_downloadDialog)
m_downloadDialog->deleteLater();
}
void ResourcePackPage::updateResourcePacks()
@ -235,34 +249,10 @@ void ResourcePackPage::changeResourcePackVersion()
if (resource.metadata() == nullptr)
return;
ResourceDownload::ResourcePackDownloadDialog mdownload(this, m_model, m_instance);
mdownload.setResourceMetadata(resource.metadata());
if (mdownload.exec()) {
auto tasks = new ConcurrentTask("Download Resource Packs", APPLICATION->settings()->get("NumberOfConcurrentDownloads").toInt());
connect(tasks, &Task::failed, [this, tasks](QString reason) {
CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show();
tasks->deleteLater();
});
connect(tasks, &Task::aborted, [this, tasks]() {
CustomMessageBox::selectable(this, tr("Aborted"), tr("Download stopped by user."), QMessageBox::Information)->show();
tasks->deleteLater();
});
connect(tasks, &Task::succeeded, [this, tasks]() {
QStringList warnings = tasks->warnings();
if (warnings.count())
CustomMessageBox::selectable(this, tr("Warnings"), warnings.join('\n'), QMessageBox::Warning)->show();
m_downloadDialog = new ResourceDownload::ResourcePackDownloadDialog(this, m_model, m_instance);
connect(this, &QObject::destroyed, m_downloadDialog, &QDialog::close);
connect(m_downloadDialog, &QDialog::finished, this, &ResourcePackPage::downloadDialogFinished);
tasks->deleteLater();
});
for (auto& task : mdownload.getTasks()) {
tasks->addTask(task);
}
ProgressDialog loadDialog(this);
loadDialog.setSkipButton(true, tr("Abort"));
loadDialog.execWithTask(tasks);
m_model->update();
}
}
m_downloadDialog->setResourceMetadata(resource.metadata());
m_downloadDialog->open();
}

View file

@ -37,7 +37,10 @@
#pragma once
#include <QPointer>
#include "ExternalResourcesPage.h"
#include "ui/dialogs/ResourceDownloadDialog.h"
#include "ui_ExternalResourcesPage.h"
#include "minecraft/mod/ResourcePackFolderModel.h"
@ -62,10 +65,12 @@ class ResourcePackPage : public ExternalResourcesPage {
private slots:
void downloadResourcePacks();
void downloadDialogFinished(int result);
void updateResourcePacks();
void deleteResourcePackMetadata();
void changeResourcePackVersion();
protected:
std::shared_ptr<ResourcePackFolderModel> m_model;
QPointer<ResourceDownload::ResourceDownloadDialog> m_downloadDialog;
};

View file

@ -150,7 +150,8 @@ class FilterModel : public QIdentityProxyModel {
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole) {
QVariant result = sourceModel()->data(mapToSource(proxyIndex), role);
return result.toString().remove(QRegularExpression("\\.png$"));
static const QRegularExpression s_removeChars("\\.png$");
return result.toString().remove(s_removeChars);
}
if (role == Qt::DecorationRole) {
QVariant result = sourceModel()->data(mapToSource(proxyIndex), QFileSystemModel::FilePathRole);
@ -559,10 +560,7 @@ void ScreenshotsPage::openedImpl()
}
auto const setting_name = QString("WideBarVisibility_%1").arg(id());
if (!APPLICATION->settings()->contains(setting_name))
m_wide_bar_setting = APPLICATION->settings()->registerSetting(setting_name);
else
m_wide_bar_setting = APPLICATION->settings()->getSetting(setting_name);
m_wide_bar_setting = APPLICATION->settings()->getOrRegisterSetting(setting_name);
ui->toolBar->setVisibilityState(m_wide_bar_setting->get().toByteArray());
}

View file

@ -1,47 +1,41 @@
#include <QFutureWatcher>
#include "ServerPingTask.h"
#include "McResolver.h"
#include "McClient.h"
#include <Json.h>
#include "McClient.h"
#include "McResolver.h"
#include "ServerPingTask.h"
unsigned getOnlinePlayers(QJsonObject data) {
unsigned getOnlinePlayers(QJsonObject data)
{
return Json::requireInteger(Json::requireObject(data, "players"), "online");
}
void ServerPingTask::executeTask() {
void ServerPingTask::executeTask()
{
qDebug() << "Querying status of " << QString("%1:%2").arg(m_domain).arg(m_port);
// Resolve the actual IP and port for the server
McResolver *resolver = new McResolver(nullptr, m_domain, m_port);
McResolver* resolver = new McResolver(nullptr, m_domain, m_port);
QObject::connect(resolver, &McResolver::succeeded, this, [this, resolver](QString ip, int port) {
qDebug() << "Resolved Address for" << m_domain << ": " << ip << ":" << port;
// Now that we have the IP and port, query the server
McClient *client = new McClient(nullptr, m_domain, ip, port);
McClient* client = new McClient(nullptr, m_domain, ip, port);
QObject::connect(client, &McClient::succeeded, this, [this](QJsonObject data) {
m_outputOnlinePlayers = getOnlinePlayers(data);
qDebug() << "Online players: " << m_outputOnlinePlayers;
emitSucceeded();
});
QObject::connect(client, &McClient::failed, this, [this](QString error) {
emitFailed(error);
});
QObject::connect(client, &McClient::failed, this, [this](QString error) { emitFailed(error); });
// Delete McClient object when done
QObject::connect(client, &McClient::finished, this, [this, client]() {
client->deleteLater();
});
QObject::connect(client, &McClient::finished, this, [this, client]() { client->deleteLater(); });
client->getStatusData();
});
QObject::connect(resolver, &McResolver::failed, this, [this](QString error) {
emitFailed(error);
});
QObject::connect(resolver, &McResolver::failed, this, [this](QString error) { emitFailed(error); });
// Delete McResolver object when done
QObject::connect(resolver, &McResolver::finished, [resolver]() {
resolver->deleteLater();
});
QObject::connect(resolver, &McResolver::finished, [resolver]() { resolver->deleteLater(); });
resolver->ping();
}

View file

@ -5,18 +5,17 @@
#include <tasks/Task.h>
class ServerPingTask : public Task {
Q_OBJECT
public:
public:
explicit ServerPingTask(QString domain, int port) : Task(), m_domain(domain), m_port(port) {}
~ServerPingTask() override = default;
int m_outputOnlinePlayers = -1;
private:
private:
QString m_domain;
int m_port;
protected:
protected:
virtual void executeTask() override;
};

View file

@ -255,11 +255,7 @@ class ServersModel : public QAbstractListModel {
return false;
}
beginMoveRows(QModelIndex(), row, row, QModelIndex(), row - 1);
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
m_servers.swapItemsAt(row - 1, row);
#else
m_servers.swap(row - 1, row);
#endif
endMoveRows();
scheduleSave();
return true;
@ -275,11 +271,7 @@ class ServersModel : public QAbstractListModel {
return false;
}
beginMoveRows(QModelIndex(), row, row, QModelIndex(), row + 2);
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
m_servers.swapItemsAt(row + 1, row);
#else
m_servers.swap(row + 1, row);
#endif
endMoveRows();
scheduleSave();
return true;
@ -711,10 +703,7 @@ void ServersPage::openedImpl()
m_model->observe();
auto const setting_name = QString("WideBarVisibility_%1").arg(id());
if (!APPLICATION->settings()->contains(setting_name))
m_wide_bar_setting = APPLICATION->settings()->registerSetting(setting_name);
else
m_wide_bar_setting = APPLICATION->settings()->getSetting(setting_name);
m_wide_bar_setting = APPLICATION->settings()->getOrRegisterSetting(setting_name);
ui->toolBar->setVisibilityState(m_wide_bar_setting->get().toByteArray());

View file

@ -81,8 +81,16 @@ void ShaderPackPage::downloadShaderPack()
if (m_instance->typeName() != "Minecraft")
return; // this is a null instance or a legacy instance
ResourceDownload::ShaderPackDownloadDialog mdownload(this, m_model, m_instance);
if (mdownload.exec()) {
m_downloadDialog = new ResourceDownload::ShaderPackDownloadDialog(this, m_model, m_instance);
connect(this, &QObject::destroyed, m_downloadDialog, &QDialog::close);
connect(m_downloadDialog, &QDialog::finished, this, &ShaderPackPage::downloadDialogFinished);
m_downloadDialog->open();
}
void ShaderPackPage::downloadDialogFinished(int result)
{
if (result) {
auto tasks = new ConcurrentTask("Download Shader Packs", APPLICATION->settings()->get("NumberOfConcurrentDownloads").toInt());
connect(tasks, &Task::failed, [this, tasks](QString reason) {
CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show();
@ -100,8 +108,12 @@ void ShaderPackPage::downloadShaderPack()
tasks->deleteLater();
});
for (auto& task : mdownload.getTasks()) {
tasks->addTask(task);
if (m_downloadDialog) {
for (auto& task : m_downloadDialog->getTasks()) {
tasks->addTask(task);
}
} else {
qWarning() << "ResourceDownloadDialog vanished before we could collect tasks!";
}
ProgressDialog loadDialog(this);
@ -110,6 +122,8 @@ void ShaderPackPage::downloadShaderPack()
m_model->update();
}
if (m_downloadDialog)
m_downloadDialog->deleteLater();
}
void ShaderPackPage::updateShaderPacks()
@ -232,34 +246,10 @@ void ShaderPackPage::changeShaderPackVersion()
if (resource.metadata() == nullptr)
return;
ResourceDownload::ShaderPackDownloadDialog mdownload(this, m_model, m_instance);
mdownload.setResourceMetadata(resource.metadata());
if (mdownload.exec()) {
auto tasks = new ConcurrentTask("Download Shader Packs", APPLICATION->settings()->get("NumberOfConcurrentDownloads").toInt());
connect(tasks, &Task::failed, [this, tasks](QString reason) {
CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show();
tasks->deleteLater();
});
connect(tasks, &Task::aborted, [this, tasks]() {
CustomMessageBox::selectable(this, tr("Aborted"), tr("Download stopped by user."), QMessageBox::Information)->show();
tasks->deleteLater();
});
connect(tasks, &Task::succeeded, [this, tasks]() {
QStringList warnings = tasks->warnings();
if (warnings.count())
CustomMessageBox::selectable(this, tr("Warnings"), warnings.join('\n'), QMessageBox::Warning)->show();
m_downloadDialog = new ResourceDownload::ShaderPackDownloadDialog(this, m_model, m_instance);
connect(this, &QObject::destroyed, m_downloadDialog, &QDialog::close);
connect(m_downloadDialog, &QDialog::finished, this, &ShaderPackPage::downloadDialogFinished);
tasks->deleteLater();
});
for (auto& task : mdownload.getTasks()) {
tasks->addTask(task);
}
ProgressDialog loadDialog(this);
loadDialog.setSkipButton(true, tr("Abort"));
loadDialog.execWithTask(tasks);
m_model->update();
}
m_downloadDialog->setResourceMetadata(resource.metadata());
m_downloadDialog->open();
}

View file

@ -37,7 +37,9 @@
#pragma once
#include <QPointer>
#include "ExternalResourcesPage.h"
#include "ui/dialogs/ResourceDownloadDialog.h"
class ShaderPackPage : public ExternalResourcesPage {
Q_OBJECT
@ -54,10 +56,12 @@ class ShaderPackPage : public ExternalResourcesPage {
public slots:
void downloadShaderPack();
void downloadDialogFinished(int result);
void updateShaderPacks();
void deleteShaderPackMetadata();
void changeShaderPackVersion();
private:
std::shared_ptr<ShaderPackFolderModel> m_model;
QPointer<ResourceDownload::ShaderPackDownloadDialog> m_downloadDialog;
};

View file

@ -90,8 +90,15 @@ void TexturePackPage::downloadTexturePacks()
if (m_instance->typeName() != "Minecraft")
return; // this is a null instance or a legacy instance
ResourceDownload::TexturePackDownloadDialog mdownload(this, m_model, m_instance);
if (mdownload.exec()) {
m_downloadDialog = new ResourceDownload::TexturePackDownloadDialog(this, m_model, m_instance);
connect(this, &QObject::destroyed, m_downloadDialog, &QDialog::close);
connect(m_downloadDialog, &QDialog::finished, this, &TexturePackPage::downloadDialogFinished);
m_downloadDialog->open();
}
void TexturePackPage::downloadDialogFinished(int result)
{
if (result) {
auto tasks = new ConcurrentTask("Download Texture Packs", APPLICATION->settings()->get("NumberOfConcurrentDownloads").toInt());
connect(tasks, &Task::failed, [this, tasks](QString reason) {
CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show();
@ -109,8 +116,12 @@ void TexturePackPage::downloadTexturePacks()
tasks->deleteLater();
});
for (auto& task : mdownload.getTasks()) {
tasks->addTask(task);
if (m_downloadDialog) {
for (auto& task : m_downloadDialog->getTasks()) {
tasks->addTask(task);
}
} else {
qWarning() << "ResourceDownloadDialog vanished before we could collect tasks!";
}
ProgressDialog loadDialog(this);
@ -119,6 +130,8 @@ void TexturePackPage::downloadTexturePacks()
m_model->update();
}
if (m_downloadDialog)
m_downloadDialog->deleteLater();
}
void TexturePackPage::updateTexturePacks()
@ -241,34 +254,10 @@ void TexturePackPage::changeTexturePackVersion()
if (resource.metadata() == nullptr)
return;
ResourceDownload::TexturePackDownloadDialog mdownload(this, m_model, m_instance);
mdownload.setResourceMetadata(resource.metadata());
if (mdownload.exec()) {
auto tasks = new ConcurrentTask("Download Texture Packs", APPLICATION->settings()->get("NumberOfConcurrentDownloads").toInt());
connect(tasks, &Task::failed, [this, tasks](QString reason) {
CustomMessageBox::selectable(this, tr("Error"), reason, QMessageBox::Critical)->show();
tasks->deleteLater();
});
connect(tasks, &Task::aborted, [this, tasks]() {
CustomMessageBox::selectable(this, tr("Aborted"), tr("Download stopped by user."), QMessageBox::Information)->show();
tasks->deleteLater();
});
connect(tasks, &Task::succeeded, [this, tasks]() {
QStringList warnings = tasks->warnings();
if (warnings.count())
CustomMessageBox::selectable(this, tr("Warnings"), warnings.join('\n'), QMessageBox::Warning)->show();
m_downloadDialog = new ResourceDownload::TexturePackDownloadDialog(this, m_model, m_instance);
connect(this, &QObject::destroyed, m_downloadDialog, &QDialog::close);
connect(m_downloadDialog, &QDialog::finished, this, &TexturePackPage::downloadDialogFinished);
tasks->deleteLater();
});
for (auto& task : mdownload.getTasks()) {
tasks->addTask(task);
}
ProgressDialog loadDialog(this);
loadDialog.setSkipButton(true, tr("Abort"));
loadDialog.execWithTask(tasks);
m_model->update();
}
m_downloadDialog->setResourceMetadata(resource.metadata());
m_downloadDialog->open();
}

View file

@ -37,7 +37,10 @@
#pragma once
#include <QPointer>
#include "ExternalResourcesPage.h"
#include "ui/dialogs/ResourceDownloadDialog.h"
#include "ui_ExternalResourcesPage.h"
#include "minecraft/mod/TexturePackFolderModel.h"
@ -57,10 +60,12 @@ class TexturePackPage : public ExternalResourcesPage {
public slots:
void updateFrame(const QModelIndex& current, const QModelIndex& previous) override;
void downloadTexturePacks();
void downloadDialogFinished(int result);
void updateTexturePacks();
void deleteTexturePackMetadata();
void changeTexturePackVersion();
private:
std::shared_ptr<TexturePackFolderModel> m_model;
QPointer<ResourceDownload::TexturePackDownloadDialog> m_downloadDialog;
};

View file

@ -124,10 +124,7 @@ void VersionPage::retranslate()
void VersionPage::openedImpl()
{
auto const setting_name = QString("WideBarVisibility_%1").arg(id());
if (!APPLICATION->settings()->contains(setting_name))
m_wide_bar_setting = APPLICATION->settings()->registerSetting(setting_name);
else
m_wide_bar_setting = APPLICATION->settings()->getSetting(setting_name);
m_wide_bar_setting = APPLICATION->settings()->getOrRegisterSetting(setting_name);
ui->toolBar->setVisibilityState(m_wide_bar_setting->get().toByteArray());
}

View file

@ -119,10 +119,7 @@ void WorldListPage::openedImpl()
}
auto const setting_name = QString("WideBarVisibility_%1").arg(id());
if (!APPLICATION->settings()->contains(setting_name))
m_wide_bar_setting = APPLICATION->settings()->registerSetting(setting_name);
else
m_wide_bar_setting = APPLICATION->settings()->getSetting(setting_name);
m_wide_bar_setting = APPLICATION->settings()->getOrRegisterSetting(setting_name);
ui->toolBar->setVisibilityState(m_wide_bar_setting->get().toByteArray());
}