diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index 5dd2a877..3f747f50 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -694,6 +694,8 @@ SET(LAUNCHER_SOURCES ui/pages/instance/ServersPage.h ui/pages/instance/WorldListPage.cpp ui/pages/instance/WorldListPage.h + ui/pages/instance/StoragePage.cpp + ui/pages/instance/StoragePage.h # GUI - global settings pages ui/pages/global/AccountListPage.cpp @@ -910,6 +912,7 @@ qt_wrap_ui(LAUNCHER_UI ui/pages/instance/VersionPage.ui ui/pages/instance/WorldListPage.ui ui/pages/instance/ScreenshotsPage.ui + ui/pages/instance/StoragePage.ui ui/pages/modplatform/atlauncher/AtlOptionalModDialog.ui ui/pages/modplatform/atlauncher/AtlPage.ui ui/pages/modplatform/VanillaPage.ui diff --git a/launcher/InstancePageProvider.h b/launcher/InstancePageProvider.h index bf29377d..4b0318d7 100644 --- a/launcher/InstancePageProvider.h +++ b/launcher/InstancePageProvider.h @@ -16,6 +16,7 @@ #include "ui/pages/instance/WorldListPage.h" #include "ui/pages/instance/ServersPage.h" #include "ui/pages/instance/GameOptionsPage.h" +#include "ui/pages/instance/StoragePage.h" class InstancePageProvider : public QObject, public BasePageProvider { @@ -46,6 +47,7 @@ public: // values.append(new GameOptionsPage(onesix.get())); values.append(new ScreenshotsPage(FS::PathCombine(onesix->gameRoot(), "screenshots"))); values.append(new InstanceSettingsPage(onesix.get())); + values.append(new StoragePage(onesix.get())); auto logMatcher = inst->getLogFileMatcher(); if(logMatcher) { diff --git a/launcher/ui/pages/instance/StoragePage.cpp b/launcher/ui/pages/instance/StoragePage.cpp new file mode 100644 index 00000000..ae1c7395 --- /dev/null +++ b/launcher/ui/pages/instance/StoragePage.cpp @@ -0,0 +1,112 @@ +// SPDX-License-Identifier: GPL-3.0-only +/* + * PolyMC - Minecraft Launcher + * Copyright (c) 2022 Slendi + * + * 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 . + */ + +#include "StoragePage.h" +#include +#include "ui_StoragePage.h" + +#include +#include + +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); +} diff --git a/launcher/ui/pages/instance/StoragePage.h b/launcher/ui/pages/instance/StoragePage.h new file mode 100644 index 00000000..5731446e --- /dev/null +++ b/launcher/ui/pages/instance/StoragePage.h @@ -0,0 +1,75 @@ +// SPDX-License-Identifier: GPL-3.0-only +/* + * PolyMC - Minecraft Launcher + * Copyright (c) 2022 Slendi + * + * 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 . + * + * This file incorporates work covered by the following copyright and + * permission notice: + */ + +#pragma once + +#include + +#include "BaseInstance.h" +#include "ui/pages/BasePage.h" +#include + +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; +}; diff --git a/launcher/ui/pages/instance/StoragePage.ui b/launcher/ui/pages/instance/StoragePage.ui new file mode 100644 index 00000000..c7ea1ce7 --- /dev/null +++ b/launcher/ui/pages/instance/StoragePage.ui @@ -0,0 +1,221 @@ + + + StoragePage + + + + 0 + 0 + 731 + 538 + + + + + + + + + + 0 + 0 + + + + Resource packs + + + + + + + + 0 + 0 + + + + 0 B + + + + + + + + 0 + 0 + + + + Mods + + + + + + + 0 B + + + + + + + + 0 + 0 + + + + Saves + + + + + + + 0 B + + + + + + + + 0 + 0 + + + + Screenshots + + + + + + + 0 B + + + + + + + Clear Screenshots + + + + + + + + 0 + 0 + + + + Logs + + + + + + + 0 B + + + + + + + Clear Logs + + + + + + + + 0 + 0 + + + + Combined + + + + + + + 0 B + + + + + + + Clear all + + + + + + + false + + + + + + true + + + + + + + false + + + + + + true + + + + + + + false + + + + + + true + + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + + + + + +