2022-03-20 19:01:08 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
2022-12-14 15:02:04 +00:00
|
|
|
* Prism Launcher - Minecraft Launcher
|
2022-03-20 19:01:08 +00:00
|
|
|
* Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org>
|
2022-06-12 12:50:58 +01:00
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2022-12-14 15:02:04 +00:00
|
|
|
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
|
2022-03-20 19:01:08 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2018-04-12 00:44:51 +01:00
|
|
|
#include "ServersPage.h"
|
2022-12-14 15:02:04 +00:00
|
|
|
#include "ui/dialogs/CustomMessageBox.h"
|
2018-04-12 00:44:51 +01:00
|
|
|
#include "ui_ServersPage.h"
|
|
|
|
|
|
|
|
#include <FileSystem.h>
|
|
|
|
#include <io/stream_reader.h>
|
|
|
|
#include <minecraft/MinecraftInstance.h>
|
2023-08-02 17:35:35 +01:00
|
|
|
#include <tag_compound.h>
|
|
|
|
#include <tag_list.h>
|
|
|
|
#include <tag_primitive.h>
|
|
|
|
#include <tag_string.h>
|
|
|
|
#include <sstream>
|
2018-04-12 00:44:51 +01:00
|
|
|
|
|
|
|
#include <QFileSystemWatcher>
|
2019-07-17 01:01:29 +01:00
|
|
|
#include <QMenu>
|
2022-11-01 21:45:15 +00:00
|
|
|
#include <QTimer>
|
2018-04-12 00:44:51 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
static const int COLUMN_COUNT = 2; // 3 , TBD: latency and other nice things.
|
2018-04-12 00:44:51 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
struct Server {
|
2018-07-15 13:51:05 +01:00
|
|
|
// Types
|
2023-08-02 17:35:35 +01:00
|
|
|
enum class AcceptsTextures : int { ASK = 0, ALWAYS = 1, NEVER = 2 };
|
2018-07-15 13:51:05 +01:00
|
|
|
|
|
|
|
// Methods
|
2023-08-02 17:35:35 +01:00
|
|
|
Server() { m_name = QObject::tr("Minecraft Server"); }
|
|
|
|
Server(const QString& name, const QString& address)
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
|
|
|
m_name = name;
|
|
|
|
m_address = address;
|
|
|
|
}
|
|
|
|
Server(nbt::tag_compound& server)
|
|
|
|
{
|
|
|
|
std::string addressStr(server["ip"]);
|
2019-03-29 15:25:37 +00:00
|
|
|
m_address = QString::fromUtf8(addressStr.c_str());
|
2018-07-15 13:51:05 +01:00
|
|
|
|
|
|
|
std::string nameStr(server["name"]);
|
2019-03-29 15:25:37 +00:00
|
|
|
m_name = QString::fromUtf8(nameStr.c_str());
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (server["icon"]) {
|
2018-07-15 13:51:05 +01:00
|
|
|
std::string base64str(server["icon"]);
|
|
|
|
m_icon = QByteArray::fromBase64(base64str.c_str());
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (server.has_key("acceptTextures", nbt::tag_type::Byte)) {
|
2018-07-15 13:51:05 +01:00
|
|
|
bool value = server["acceptTextures"].as<nbt::tag_byte>().get();
|
2023-08-02 17:35:35 +01:00
|
|
|
if (value) {
|
2018-07-15 13:51:05 +01:00
|
|
|
m_acceptsTextures = AcceptsTextures::ALWAYS;
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2018-07-15 13:51:05 +01:00
|
|
|
m_acceptsTextures = AcceptsTextures::NEVER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void serialize(nbt::tag_compound& server)
|
|
|
|
{
|
2019-03-29 15:25:37 +00:00
|
|
|
server.insert("name", m_name.trimmed().toUtf8().toStdString());
|
|
|
|
server.insert("ip", m_address.trimmed().toUtf8().toStdString());
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_icon.size()) {
|
2018-07-15 13:51:05 +01:00
|
|
|
server.insert("icon", m_icon.toBase64().toStdString());
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_acceptsTextures != AcceptsTextures::ASK) {
|
2018-07-15 13:51:05 +01:00
|
|
|
server.insert("acceptTextures", nbt::tag_byte(m_acceptsTextures == AcceptsTextures::ALWAYS));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data - persistent and user changeable
|
|
|
|
QString m_name;
|
|
|
|
QString m_address;
|
|
|
|
AcceptsTextures m_acceptsTextures = AcceptsTextures::ASK;
|
|
|
|
|
|
|
|
// Data - persistent and automatically updated
|
|
|
|
QByteArray m_icon;
|
|
|
|
|
|
|
|
// Data - temporary
|
|
|
|
bool m_checked = false;
|
|
|
|
bool m_up = false;
|
2023-08-02 17:35:35 +01:00
|
|
|
QString m_motd; // https://mctools.org/motd-creator
|
2018-07-15 13:51:05 +01:00
|
|
|
int m_ping = 0;
|
|
|
|
int m_currentPlayers = 0;
|
|
|
|
int m_maxPlayers = 0;
|
2018-04-12 00:44:51 +01:00
|
|
|
};
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
static std::unique_ptr<nbt::tag_compound> parseServersDat(const QString& filename)
|
2018-04-12 00:44:51 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
try {
|
2018-07-15 13:51:05 +01:00
|
|
|
QByteArray input = FS::read(filename);
|
|
|
|
std::istringstream foo(std::string(input.constData(), input.size()));
|
|
|
|
auto pair = nbt::io::read_compound(foo);
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (pair.first != "")
|
2018-07-15 13:51:05 +01:00
|
|
|
return nullptr;
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (pair.second == nullptr)
|
2018-07-15 13:51:05 +01:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
return std::move(pair.second);
|
2023-08-02 17:35:35 +01:00
|
|
|
} catch (...) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
static bool serializeServerDat(const QString& filename, nbt::tag_compound* levelInfo)
|
2018-04-12 00:44:51 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
try {
|
|
|
|
if (!FS::ensureFilePathExists(filename)) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
std::ostringstream s;
|
|
|
|
nbt::io::write_tag("", *levelInfo, s);
|
2023-08-02 17:35:35 +01:00
|
|
|
QByteArray val(s.str().data(), (int)s.str().size());
|
2018-07-15 13:51:05 +01:00
|
|
|
FS::write(filename, val);
|
|
|
|
return true;
|
2023-08-02 17:35:35 +01:00
|
|
|
} catch (...) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
class ServersModel : public QAbstractListModel {
|
2018-07-15 13:51:05 +01:00
|
|
|
Q_OBJECT
|
2023-08-02 17:35:35 +01:00
|
|
|
public:
|
|
|
|
enum Roles {
|
2018-07-15 13:51:05 +01:00
|
|
|
ServerPtrRole = Qt::UserRole,
|
|
|
|
};
|
2023-08-02 17:35:35 +01:00
|
|
|
explicit ServersModel(const QString& path, QObject* parent = 0) : QAbstractListModel(parent)
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
|
|
|
m_path = path;
|
|
|
|
m_watcher = new QFileSystemWatcher(this);
|
|
|
|
connect(m_watcher, &QFileSystemWatcher::fileChanged, this, &ServersModel::fileChanged);
|
|
|
|
connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &ServersModel::dirChanged);
|
|
|
|
m_saveTimer.setSingleShot(true);
|
|
|
|
m_saveTimer.setInterval(5000);
|
|
|
|
connect(&m_saveTimer, &QTimer::timeout, this, &ServersModel::save_internal);
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
virtual ~ServersModel(){};
|
2018-07-15 13:51:05 +01:00
|
|
|
|
|
|
|
void observe()
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_observed) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_observed = true;
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!m_loaded) {
|
2018-07-15 13:51:05 +01:00
|
|
|
load();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFSObserver();
|
|
|
|
}
|
|
|
|
|
|
|
|
void unobserve()
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!m_observed) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_observed = false;
|
|
|
|
|
|
|
|
updateFSObserver();
|
|
|
|
}
|
|
|
|
|
|
|
|
void lock()
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_locked) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
saveNow();
|
|
|
|
|
|
|
|
m_locked = true;
|
|
|
|
updateFSObserver();
|
|
|
|
}
|
|
|
|
|
|
|
|
void unlock()
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!m_locked) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_locked = false;
|
|
|
|
|
|
|
|
updateFSObserver();
|
|
|
|
}
|
|
|
|
|
|
|
|
int addEmptyRow(int position)
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_locked) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
if (position < 0 || position >= rowCount()) {
|
2018-07-15 13:51:05 +01:00
|
|
|
position = rowCount();
|
|
|
|
}
|
|
|
|
beginInsertRows(QModelIndex(), position, position);
|
|
|
|
m_servers.insert(position, Server());
|
|
|
|
endInsertRows();
|
|
|
|
scheduleSave();
|
|
|
|
return position;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool removeRow(int row)
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_locked) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
if (row < 0 || row >= rowCount()) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
beginRemoveRows(QModelIndex(), row, row);
|
|
|
|
m_servers.removeAt(row);
|
2023-08-02 17:35:35 +01:00
|
|
|
endRemoveRows(); // does absolutely nothing, the selected server stays as the next line...
|
2018-07-15 13:51:05 +01:00
|
|
|
scheduleSave();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool moveUp(int row)
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_locked) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
if (row <= 0) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
beginMoveRows(QModelIndex(), row, row, QModelIndex(), row - 1);
|
2022-05-02 18:10:45 +01:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
|
2023-08-02 17:35:35 +01:00
|
|
|
m_servers.swapItemsAt(row - 1, row);
|
2022-05-02 18:10:45 +01:00
|
|
|
#else
|
2023-08-02 17:35:35 +01:00
|
|
|
m_servers.swap(row - 1, row);
|
2022-05-02 18:10:45 +01:00
|
|
|
#endif
|
2018-07-15 13:51:05 +01:00
|
|
|
endMoveRows();
|
|
|
|
scheduleSave();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool moveDown(int row)
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_locked) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int count = rowCount();
|
2023-08-02 17:35:35 +01:00
|
|
|
if (row + 1 >= count) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
beginMoveRows(QModelIndex(), row, row, QModelIndex(), row + 2);
|
2022-05-02 18:10:45 +01:00
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 13, 0)
|
2023-08-02 17:35:35 +01:00
|
|
|
m_servers.swapItemsAt(row + 1, row);
|
2022-05-02 18:10:45 +01:00
|
|
|
#else
|
2023-08-02 17:35:35 +01:00
|
|
|
m_servers.swap(row + 1, row);
|
2022-05-02 18:10:45 +01:00
|
|
|
#endif
|
2018-07-15 13:51:05 +01:00
|
|
|
endMoveRows();
|
|
|
|
scheduleSave();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
|
|
|
|
{
|
|
|
|
if (section < 0 || section >= COLUMN_COUNT)
|
|
|
|
return QVariant();
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (role == Qt::DisplayRole) {
|
|
|
|
switch (section) {
|
2018-07-15 13:51:05 +01:00
|
|
|
case 0:
|
|
|
|
return tr("Name");
|
|
|
|
case 1:
|
|
|
|
return tr("Address");
|
|
|
|
case 2:
|
|
|
|
return tr("Latency");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return QAbstractListModel::headerData(section, orientation, role);
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
|
|
|
if (!index.isValid())
|
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
int row = index.row();
|
|
|
|
int column = index.column();
|
2023-08-02 17:35:35 +01:00
|
|
|
if (column < 0 || column >= COLUMN_COUNT)
|
2018-07-15 13:51:05 +01:00
|
|
|
return QVariant();
|
|
|
|
|
|
|
|
if (row < 0 || row >= m_servers.size())
|
|
|
|
return QVariant();
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
switch (column) {
|
2018-07-15 13:51:05 +01:00
|
|
|
case 0:
|
2023-08-02 17:35:35 +01:00
|
|
|
switch (role) {
|
|
|
|
case Qt::DecorationRole: {
|
|
|
|
auto& bytes = m_servers[row].m_icon;
|
|
|
|
if (bytes.size()) {
|
|
|
|
QPixmap px;
|
|
|
|
if (px.loadFromData(bytes))
|
|
|
|
return QIcon(px);
|
|
|
|
}
|
|
|
|
return APPLICATION->getThemedIcon("unknown_server");
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
case Qt::DisplayRole:
|
|
|
|
return m_servers[row].m_name;
|
|
|
|
case ServerPtrRole:
|
|
|
|
return QVariant::fromValue<void*>((void*)&m_servers[row]);
|
|
|
|
default:
|
|
|
|
return QVariant();
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
|
|
|
case 1:
|
2023-08-02 17:35:35 +01:00
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
return m_servers[row].m_address;
|
|
|
|
default:
|
|
|
|
return QVariant();
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
|
|
|
case 2:
|
2023-08-02 17:35:35 +01:00
|
|
|
switch (role) {
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
return m_servers[row].m_ping;
|
|
|
|
default:
|
|
|
|
return QVariant();
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
2022-11-13 14:35:55 +00:00
|
|
|
return parent.isValid() ? 0 : m_servers.size();
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
int columnCount(const QModelIndex& parent) const override
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
2022-11-13 14:35:55 +00:00
|
|
|
return parent.isValid() ? 0 : COLUMN_COUNT;
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
Server* at(int index)
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (index < 0 || index >= rowCount()) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return &m_servers[index];
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void setName(int row, const QString& name)
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_locked) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto server = at(row);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!server || server->m_name == name) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
server->m_name = name;
|
|
|
|
emit dataChanged(index(row, 0), index(row, COLUMN_COUNT - 1));
|
|
|
|
scheduleSave();
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void setAddress(int row, const QString& address)
|
2018-07-15 13:51:05 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_locked) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto server = at(row);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!server || server->m_address == address) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
server->m_address = address;
|
|
|
|
emit dataChanged(index(row, 0), index(row, COLUMN_COUNT - 1));
|
|
|
|
scheduleSave();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setAcceptsTextures(int row, Server::AcceptsTextures textures)
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_locked) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto server = at(row);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!server || server->m_acceptsTextures == textures) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
server->m_acceptsTextures = textures;
|
|
|
|
emit dataChanged(index(row, 0), index(row, COLUMN_COUNT - 1));
|
|
|
|
scheduleSave();
|
|
|
|
}
|
|
|
|
|
|
|
|
void load()
|
|
|
|
{
|
|
|
|
cancelSave();
|
|
|
|
beginResetModel();
|
|
|
|
QList<Server> servers;
|
|
|
|
auto serversDat = parseServersDat(serversPath());
|
2023-08-02 17:35:35 +01:00
|
|
|
if (serversDat) {
|
|
|
|
auto& serversList = serversDat->at("servers").as<nbt::tag_list>();
|
|
|
|
for (auto iter = serversList.begin(); iter != serversList.end(); iter++) {
|
|
|
|
auto& serverTag = (*iter).as<nbt::tag_compound>();
|
2018-07-15 13:51:05 +01:00
|
|
|
Server s(serverTag);
|
|
|
|
servers.append(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_servers.swap(servers);
|
|
|
|
m_loaded = true;
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void saveNow()
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (saveIsScheduled()) {
|
2018-07-15 13:51:05 +01:00
|
|
|
save_internal();
|
|
|
|
}
|
|
|
|
}
|
2018-04-12 00:44:51 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
public slots:
|
2018-07-15 13:51:05 +01:00
|
|
|
void dirChanged(const QString& path)
|
|
|
|
{
|
|
|
|
qDebug() << "Changed:" << path;
|
|
|
|
load();
|
|
|
|
}
|
|
|
|
void fileChanged(const QString& path)
|
|
|
|
{
|
|
|
|
qDebug() << "Changed:" << path;
|
|
|
|
}
|
2018-04-12 00:44:51 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
private slots:
|
2018-07-15 13:51:05 +01:00
|
|
|
void save_internal()
|
|
|
|
{
|
|
|
|
cancelSave();
|
|
|
|
QString path = serversPath();
|
|
|
|
qDebug() << "Server list about to be saved to" << path;
|
|
|
|
|
|
|
|
nbt::tag_compound out;
|
|
|
|
nbt::tag_list list;
|
2023-08-02 17:35:35 +01:00
|
|
|
for (auto& server : m_servers) {
|
2018-07-15 13:51:05 +01:00
|
|
|
nbt::tag_compound serverNbt;
|
|
|
|
server.serialize(serverNbt);
|
|
|
|
list.push_back(std::move(serverNbt));
|
|
|
|
}
|
|
|
|
out.insert("servers", nbt::value(std::move(list)));
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!serializeServerDat(path, &out)) {
|
2018-07-15 13:51:05 +01:00
|
|
|
qDebug() << "Failed to save server list:" << path << "Will try again.";
|
|
|
|
scheduleSave();
|
|
|
|
}
|
|
|
|
}
|
2018-04-12 00:44:51 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
private:
|
2018-07-15 13:51:05 +01:00
|
|
|
void scheduleSave()
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!m_loaded) {
|
2018-07-15 13:51:05 +01:00
|
|
|
qDebug() << "Server list should never save if it didn't successfully load, path:" << m_path;
|
|
|
|
return;
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!m_dirty) {
|
2018-07-15 13:51:05 +01:00
|
|
|
m_dirty = true;
|
|
|
|
qDebug() << "Server list save is scheduled for" << m_path;
|
|
|
|
}
|
|
|
|
m_saveTimer.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cancelSave()
|
|
|
|
{
|
|
|
|
m_dirty = false;
|
|
|
|
m_saveTimer.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool saveIsScheduled() const
|
|
|
|
{
|
|
|
|
return m_dirty;
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateFSObserver()
|
|
|
|
{
|
|
|
|
bool observingFS = m_watcher->directories().contains(m_path);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_observed && m_locked) {
|
|
|
|
if (!observingFS) {
|
2018-07-15 13:51:05 +01:00
|
|
|
qWarning() << "Will watch" << m_path;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!m_watcher->addPath(m_path)) {
|
2018-07-15 13:51:05 +01:00
|
|
|
qWarning() << "Failed to start watching" << m_path;
|
|
|
|
}
|
|
|
|
}
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
|
|
|
if (observingFS) {
|
2018-07-15 13:51:05 +01:00
|
|
|
qWarning() << "Will stop watching" << m_path;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!m_watcher->removePath(m_path)) {
|
2018-07-15 13:51:05 +01:00
|
|
|
qWarning() << "Failed to stop watching" << m_path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString serversPath()
|
|
|
|
{
|
|
|
|
QFileInfo foo(FS::PathCombine(m_path, "servers.dat"));
|
|
|
|
return foo.filePath();
|
|
|
|
}
|
2018-04-12 00:44:51 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
private:
|
2018-07-15 13:51:05 +01:00
|
|
|
bool m_loaded = false;
|
|
|
|
bool m_locked = false;
|
|
|
|
bool m_observed = false;
|
|
|
|
bool m_dirty = false;
|
|
|
|
QString m_path;
|
|
|
|
QList<Server> m_servers;
|
2023-08-02 17:35:35 +01:00
|
|
|
QFileSystemWatcher* m_watcher = nullptr;
|
2018-07-15 13:51:05 +01:00
|
|
|
QTimer m_saveTimer;
|
2018-04-12 00:44:51 +01:00
|
|
|
};
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
ServersPage::ServersPage(InstancePtr inst, QWidget* parent) : QMainWindow(parent), ui(new Ui::ServersPage)
|
2018-04-12 00:44:51 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
ui->setupUi(this);
|
|
|
|
m_inst = inst;
|
2018-07-27 22:57:09 +01:00
|
|
|
m_model = new ServersModel(inst->gameRoot(), this);
|
2023-08-02 17:35:35 +01:00
|
|
|
ui->serversView->setIconSize(QSize(64, 64));
|
2018-07-15 13:51:05 +01:00
|
|
|
ui->serversView->setModel(m_model);
|
2019-07-25 00:02:30 +01:00
|
|
|
ui->serversView->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
connect(ui->serversView, &QTreeView::customContextMenuRequested, this, &ServersPage::ShowContextMenu);
|
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
auto head = ui->serversView->header();
|
2023-08-02 17:35:35 +01:00
|
|
|
if (head->count()) {
|
2018-07-15 13:51:05 +01:00
|
|
|
head->setSectionResizeMode(0, QHeaderView::Stretch);
|
2023-08-02 17:35:35 +01:00
|
|
|
for (int i = 1; i < head->count(); i++) {
|
2018-07-15 13:51:05 +01:00
|
|
|
head->setSectionResizeMode(i, QHeaderView::ResizeToContents);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto selectionModel = ui->serversView->selectionModel();
|
|
|
|
connect(selectionModel, &QItemSelectionModel::currentChanged, this, &ServersPage::currentChanged);
|
2022-05-16 19:34:07 +01:00
|
|
|
connect(m_inst.get(), &MinecraftInstance::runningStatusChanged, this, &ServersPage::runningStateChanged);
|
2018-07-15 13:51:05 +01:00
|
|
|
connect(ui->nameLine, &QLineEdit::textEdited, this, &ServersPage::nameEdited);
|
|
|
|
connect(ui->addressLine, &QLineEdit::textEdited, this, &ServersPage::addressEdited);
|
|
|
|
connect(ui->resourceComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(resourceIndexChanged(int)));
|
|
|
|
connect(m_model, &QAbstractItemModel::rowsRemoved, this, &ServersPage::rowsRemoved);
|
|
|
|
|
|
|
|
m_locked = m_inst->isRunning();
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_locked) {
|
2018-07-15 13:51:05 +01:00
|
|
|
m_model->lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateState();
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ServersPage::~ServersPage()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_model->saveNow();
|
2019-08-10 18:58:58 +01:00
|
|
|
delete ui;
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
2022-02-22 18:23:53 +00:00
|
|
|
void ServersPage::retranslate()
|
|
|
|
{
|
|
|
|
ui->retranslateUi(this);
|
|
|
|
}
|
|
|
|
|
2019-07-25 00:02:30 +01:00
|
|
|
void ServersPage::ShowContextMenu(const QPoint& pos)
|
|
|
|
{
|
|
|
|
auto menu = ui->toolBar->createContextMenu(this, tr("Context menu"));
|
|
|
|
menu->exec(ui->serversView->mapToGlobal(pos));
|
|
|
|
delete menu;
|
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
QMenu* ServersPage::createPopupMenu()
|
2019-07-17 01:01:29 +01:00
|
|
|
{
|
|
|
|
QMenu* filteredMenu = QMainWindow::createPopupMenu();
|
2023-08-02 17:35:35 +01:00
|
|
|
filteredMenu->removeAction(ui->toolBar->toggleViewAction());
|
2019-07-17 01:01:29 +01:00
|
|
|
return filteredMenu;
|
|
|
|
}
|
|
|
|
|
2022-05-16 19:34:07 +01:00
|
|
|
void ServersPage::runningStateChanged(bool running)
|
2018-04-12 00:44:51 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_locked == running) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_locked = running;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_locked) {
|
2018-07-15 13:51:05 +01:00
|
|
|
m_model->lock();
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2018-07-15 13:51:05 +01:00
|
|
|
m_model->unlock();
|
|
|
|
}
|
|
|
|
updateState();
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
void ServersPage::currentChanged(const QModelIndex& current, const QModelIndex& previous)
|
2018-04-12 00:44:51 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
int nextServer = -1;
|
2023-08-02 17:35:35 +01:00
|
|
|
if (!current.isValid()) {
|
2018-07-15 13:51:05 +01:00
|
|
|
nextServer = -1;
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2018-07-15 13:51:05 +01:00
|
|
|
nextServer = current.row();
|
|
|
|
}
|
|
|
|
currentServer = nextServer;
|
|
|
|
updateState();
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// WARNING: this is here because currentChanged is not accurate when removing rows. the current item needs to be fixed up after removal.
|
|
|
|
void ServersPage::rowsRemoved(const QModelIndex& parent, int first, int last)
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (currentServer < first) {
|
2018-07-15 13:51:05 +01:00
|
|
|
// current was before the removal
|
|
|
|
return;
|
2023-08-02 17:35:35 +01:00
|
|
|
} else if (currentServer >= first && currentServer <= last) {
|
2018-07-15 13:51:05 +01:00
|
|
|
// current got removed...
|
|
|
|
return;
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2018-07-15 13:51:05 +01:00
|
|
|
// current was past the removal
|
|
|
|
int count = last - first + 1;
|
|
|
|
currentServer -= count;
|
|
|
|
}
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServersPage::nameEdited(const QString& name)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_model->setName(currentServer, name);
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServersPage::addressEdited(const QString& address)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_model->setAddress(currentServer, address);
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServersPage::resourceIndexChanged(int index)
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
auto acceptsTextures = Server::AcceptsTextures(index);
|
|
|
|
m_model->setAcceptsTextures(currentServer, acceptsTextures);
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServersPage::updateState()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
auto server = m_model->at(currentServer);
|
|
|
|
|
|
|
|
bool serverEditEnabled = server && !m_locked;
|
|
|
|
ui->addressLine->setEnabled(serverEditEnabled);
|
|
|
|
ui->nameLine->setEnabled(serverEditEnabled);
|
|
|
|
ui->resourceComboBox->setEnabled(serverEditEnabled);
|
2019-07-17 01:01:29 +01:00
|
|
|
ui->actionMove_Down->setEnabled(serverEditEnabled);
|
|
|
|
ui->actionMove_Up->setEnabled(serverEditEnabled);
|
|
|
|
ui->actionRemove->setEnabled(serverEditEnabled);
|
2021-05-22 17:07:08 +01:00
|
|
|
ui->actionJoin->setEnabled(serverEditEnabled);
|
2018-07-15 13:51:05 +01:00
|
|
|
|
2023-08-02 17:35:35 +01:00
|
|
|
if (server) {
|
2018-07-15 13:51:05 +01:00
|
|
|
ui->addressLine->setText(server->m_address);
|
|
|
|
ui->nameLine->setText(server->m_name);
|
|
|
|
ui->resourceComboBox->setCurrentIndex(int(server->m_acceptsTextures));
|
2023-08-02 17:35:35 +01:00
|
|
|
} else {
|
2018-07-15 13:51:05 +01:00
|
|
|
ui->addressLine->setText(QString());
|
|
|
|
ui->nameLine->setText(QString());
|
|
|
|
ui->resourceComboBox->setCurrentIndex(0);
|
|
|
|
}
|
|
|
|
|
2019-07-17 01:01:29 +01:00
|
|
|
ui->actionAdd->setDisabled(m_locked);
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServersPage::openedImpl()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_model->observe();
|
2022-11-19 20:12:31 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
ui->toolBar->setVisibilityState(m_wide_bar_setting->get().toByteArray());
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ServersPage::closedImpl()
|
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
m_model->unobserve();
|
2022-11-19 20:12:31 +00:00
|
|
|
|
|
|
|
m_wide_bar_setting->set(ui->toolBar->getVisibilityState());
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 01:01:29 +01:00
|
|
|
void ServersPage::on_actionAdd_triggered()
|
2018-04-12 00:44:51 +01:00
|
|
|
{
|
2018-07-15 13:51:05 +01:00
|
|
|
int position = m_model->addEmptyRow(currentServer + 1);
|
2023-08-02 17:35:35 +01:00
|
|
|
if (position < 0) {
|
2018-07-15 13:51:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// select the new row
|
|
|
|
ui->serversView->selectionModel()->setCurrentIndex(
|
2023-08-02 17:35:35 +01:00
|
|
|
m_model->index(position), QItemSelectionModel::SelectCurrent | QItemSelectionModel::Clear | QItemSelectionModel::Rows);
|
2018-07-15 13:51:05 +01:00
|
|
|
currentServer = position;
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 01:01:29 +01:00
|
|
|
void ServersPage::on_actionRemove_triggered()
|
2018-04-12 00:44:51 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
auto response =
|
|
|
|
CustomMessageBox::selectable(this, tr("Confirm Removal"),
|
|
|
|
tr("You are about to remove \"%1\".\n"
|
|
|
|
"This is permanent and the server will be gone from your list forever (A LONG TIME).\n\n"
|
|
|
|
"Are you sure?")
|
|
|
|
.arg(m_model->at(currentServer)->m_name),
|
|
|
|
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
|
|
|
|
->exec();
|
2022-12-14 15:02:04 +00:00
|
|
|
|
|
|
|
if (response != QMessageBox::Yes)
|
|
|
|
return;
|
|
|
|
|
2018-07-15 13:51:05 +01:00
|
|
|
m_model->removeRow(currentServer);
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 01:01:29 +01:00
|
|
|
void ServersPage::on_actionMove_Up_triggered()
|
2018-04-12 00:44:51 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_model->moveUp(currentServer)) {
|
|
|
|
currentServer--;
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
2019-07-17 01:01:29 +01:00
|
|
|
void ServersPage::on_actionMove_Down_triggered()
|
2018-04-12 00:44:51 +01:00
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
if (m_model->moveDown(currentServer)) {
|
|
|
|
currentServer++;
|
2018-07-15 13:51:05 +01:00
|
|
|
}
|
2018-04-12 00:44:51 +01:00
|
|
|
}
|
|
|
|
|
2021-05-22 17:07:08 +01:00
|
|
|
void ServersPage::on_actionJoin_triggered()
|
|
|
|
{
|
2023-08-02 17:35:35 +01:00
|
|
|
const auto& address = m_model->at(currentServer)->m_address;
|
2022-07-11 19:46:11 +01:00
|
|
|
APPLICATION->launch(m_inst, true, false, nullptr, std::make_shared<MinecraftServerTarget>(MinecraftServerTarget::parse(address)));
|
2021-05-22 17:07:08 +01:00
|
|
|
}
|
|
|
|
|
2018-04-12 00:44:51 +01:00
|
|
|
#include "ServersPage.moc"
|