Add storage page to instances.

This patch adds a new page to instance settings which allows users to
manage storage. This commit represents the start of it. Right now, only
some base features are implemented.

Signed-off-by: xSlendiX <slendi@socopon.com>
This commit is contained in:
xSlendiX 2022-11-17 00:17:07 +02:00
parent 46adfda5a6
commit cf317ca3d4
5 changed files with 413 additions and 0 deletions

View File

@ -694,6 +694,8 @@ SET(LAUNCHER_SOURCES
ui/pages/instance/ServersPage.h ui/pages/instance/ServersPage.h
ui/pages/instance/WorldListPage.cpp ui/pages/instance/WorldListPage.cpp
ui/pages/instance/WorldListPage.h ui/pages/instance/WorldListPage.h
ui/pages/instance/StoragePage.cpp
ui/pages/instance/StoragePage.h
# GUI - global settings pages # GUI - global settings pages
ui/pages/global/AccountListPage.cpp ui/pages/global/AccountListPage.cpp
@ -910,6 +912,7 @@ qt_wrap_ui(LAUNCHER_UI
ui/pages/instance/VersionPage.ui ui/pages/instance/VersionPage.ui
ui/pages/instance/WorldListPage.ui ui/pages/instance/WorldListPage.ui
ui/pages/instance/ScreenshotsPage.ui ui/pages/instance/ScreenshotsPage.ui
ui/pages/instance/StoragePage.ui
ui/pages/modplatform/atlauncher/AtlOptionalModDialog.ui ui/pages/modplatform/atlauncher/AtlOptionalModDialog.ui
ui/pages/modplatform/atlauncher/AtlPage.ui ui/pages/modplatform/atlauncher/AtlPage.ui
ui/pages/modplatform/VanillaPage.ui ui/pages/modplatform/VanillaPage.ui

View File

@ -16,6 +16,7 @@
#include "ui/pages/instance/WorldListPage.h" #include "ui/pages/instance/WorldListPage.h"
#include "ui/pages/instance/ServersPage.h" #include "ui/pages/instance/ServersPage.h"
#include "ui/pages/instance/GameOptionsPage.h" #include "ui/pages/instance/GameOptionsPage.h"
#include "ui/pages/instance/StoragePage.h"
class InstancePageProvider : public QObject, public BasePageProvider class InstancePageProvider : public QObject, public BasePageProvider
{ {
@ -46,6 +47,7 @@ public:
// values.append(new GameOptionsPage(onesix.get())); // values.append(new GameOptionsPage(onesix.get()));
values.append(new ScreenshotsPage(FS::PathCombine(onesix->gameRoot(), "screenshots"))); values.append(new ScreenshotsPage(FS::PathCombine(onesix->gameRoot(), "screenshots")));
values.append(new InstanceSettingsPage(onesix.get())); values.append(new InstanceSettingsPage(onesix.get()));
values.append(new StoragePage(onesix.get()));
auto logMatcher = inst->getLogFileMatcher(); auto logMatcher = inst->getLogFileMatcher();
if(logMatcher) if(logMatcher)
{ {

View File

@ -0,0 +1,112 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 Slendi <slendi@socopon.com>
*
* 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/>.
*/
#include "StoragePage.h"
#include <QTabBar>
#include "ui_StoragePage.h"
#include <filesystem>
#include <string>
unsigned get_directory_size(std::string path)
{
if (!std::filesystem::exists(path))
return 0;
unsigned file_size_total = 0;
for (auto const& entry : std::filesystem::directory_iterator(path)) {
if (entry.is_directory())
file_size_total += get_directory_size(entry.path().string());
else
file_size_total += entry.file_size();
}
return file_size_total;
}
void clear_directory_inner(std::string path)
{
if (!std::filesystem::exists(path))
return;
for (auto const& entry : std::filesystem::directory_iterator(path))
std::filesystem::remove_all(entry);
}
StoragePage::StoragePage(BaseInstance* inst, QWidget* parent) : QWidget(parent), ui(new Ui::StoragePage), m_inst(inst)
{
ui->setupUi(this);
update_calculations();
}
StoragePage::~StoragePage()
{
delete ui;
}
bool StoragePage::apply()
{
return true;
}
void StoragePage::retranslate()
{
ui->retranslateUi(this);
}
void StoragePage::HandleClearScreenshotsButton()
{
auto path = (m_inst->gameRoot() + "/screenshots").toStdString();
clear_directory_inner(path);
update_calculations();
}
void StoragePage::HandleClearLogsButton()
{
auto path = (m_inst->gameRoot() + "/logs").toStdString();
clear_directory_inner(path);
update_calculations();
}
void StoragePage::HandleClearAllButton()
{
HandleClearScreenshotsButton();
HandleClearLogsButton();
}
void StoragePage::update_calculations()
{
m_size_resource_packs = get_directory_size((m_inst->gameRoot() + "/texturepacks").toStdString()) +
get_directory_size((m_inst->gameRoot() + "/resourcepacks").toStdString());
m_size_mods = get_directory_size(m_inst->modsRoot().toStdString());
m_size_saves = get_directory_size((m_inst->gameRoot() + "/saves").toStdString());
m_size_screenshots = get_directory_size((m_inst->gameRoot() + "/screenshots").toStdString());
m_size_logs = get_directory_size((m_inst->gameRoot() + "/logs").toStdString());
QLocale locale = this->locale();
ui->label_resource_packs->setText(locale.formattedDataSize(m_size_resource_packs));
ui->label_mods->setText(locale.formattedDataSize(m_size_mods));
ui->label_saves->setText(locale.formattedDataSize(m_size_saves));
ui->label_screenshots->setText(locale.formattedDataSize(m_size_screenshots));
ui->label_logs->setText(locale.formattedDataSize(m_size_logs));
ui->label_combined->setText(
locale.formattedDataSize(m_size_resource_packs + m_size_mods + m_size_saves + m_size_screenshots + m_size_logs));
connect(ui->button_clear_screenshots, &QPushButton::clicked, this, &StoragePage::HandleClearScreenshotsButton);
connect(ui->button_clear_logs, &QPushButton::clicked, this, &StoragePage::HandleClearLogsButton);
connect(ui->button_clear_all, &QPushButton::clicked, this, &StoragePage::HandleClearAllButton);
}

View File

@ -0,0 +1,75 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 Slendi <slendi@socopon.com>
*
* 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:
*/
#pragma once
#include <QWidget>
#include "BaseInstance.h"
#include "ui/pages/BasePage.h"
#include <Application.h>
namespace Ui
{
class StoragePage;
}
class StoragePage : public QWidget, public BasePage
{
Q_OBJECT
public:
explicit StoragePage(BaseInstance *inst, QWidget *parent = 0);
virtual ~StoragePage();
virtual QString displayName() const override
{
return tr("Storage");
}
virtual QIcon icon() const override
{
auto icon = APPLICATION->getThemedIcon("notes");
if(icon.isNull())
icon = APPLICATION->getThemedIcon("news");
return icon;
}
virtual QString id() const override
{
return "storage";
}
virtual bool apply() override;
virtual QString helpPage() const override
{
return "Storage";
}
void retranslate() override;
void HandleClearScreenshotsButton();
void HandleClearLogsButton();
void HandleClearAllButton();
void update_calculations();
private:
Ui::StoragePage *ui;
BaseInstance *m_inst;
unsigned m_size_resource_packs, m_size_mods, m_size_saves, m_size_screenshots, m_size_logs;
};

View File

@ -0,0 +1,221 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>StoragePage</class>
<widget class="QWidget" name="StoragePage">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>731</width>
<height>538</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Resource packs</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLabel" name="label_resource_packs">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Mods</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLabel" name="label_mods">
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Saves</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLabel" name="label_saves">
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Screenshots</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QLabel" name="label_screenshots">
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="3" column="2">
<widget class="QPushButton" name="button_clear_screenshots">
<property name="text">
<string>Clear Screenshots</string>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_5">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Logs</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLabel" name="label_logs">
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="4" column="2">
<widget class="QPushButton" name="button_clear_logs">
<property name="text">
<string>Clear Logs</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_6">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Combined</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QLabel" name="label_combined">
<property name="text">
<string>0 B</string>
</property>
</widget>
</item>
<item row="5" column="2">
<widget class="QPushButton" name="button_clear_all">
<property name="text">
<string>Clear all</string>
</property>
</widget>
</item>
<item row="2" column="2">
<widget class="QPushButton" name="pushButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string/>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="pushButton_3">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string/>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="pushButton_2">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string/>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>