The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
game_cache_options.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2014 - 2025
3 by Iris Morelle <shadowm2006@gmail.com>
4 Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY.
12
13 See the COPYING file for more details.
14*/
15
16#define GETTEXT_DOMAIN "wesnoth-lib"
17
19
20#include "desktop/clipboard.hpp"
21#include "config_cache.hpp"
22#include "cursor.hpp"
23#include "desktop/open.hpp"
24#include "filesystem.hpp"
27#include "gui/widgets/label.hpp"
30
31#include <functional>
32
33#include "gettext.hpp"
34
35namespace gui2::dialogs
36{
37
38REGISTER_DIALOG(game_cache_options)
39
41 : modal_dialog(window_id())
42 , cache_path_(filesystem::get_cache_dir())
43 , clean_button_(nullptr)
44 , purge_button_(nullptr)
45 , size_label_(nullptr)
46{
47}
48
50{
51 clean_button_ = find_widget<button>("clean", false, true);
52 purge_button_ = find_widget<button>("purge", false, true);
53 size_label_ = find_widget<label>("size", false, true);
54
56
57 text_box_base& path_box = find_widget<text_box_base>("path");
58 path_box.set_value(cache_path_);
59 path_box.set_active(false);
60
61 button& copy = find_widget<button>("copy");
62 connect_signal_mouse_left_click(copy,
64 this));
65
66 button& browse = find_widget<button>("browse");
67 connect_signal_mouse_left_click(browse,
69 this));
70
71 connect_signal_mouse_left_click(*clean_button_,
73 this));
74
75 connect_signal_mouse_left_click(*purge_button_,
77 this));
78}
79
81{
82 size_label_ = nullptr;
83}
84
86{
87 if(!size_label_) {
88 return;
89 }
90
92 const int size = filesystem::dir_size(cache_path_);
93
94 if(size < 0) {
95 size_label_->set_label(_("dir_size^Unknown"));
96 } else {
97 size_label_->set_label(utils::si_string(size, true, _("unit_byte^B")));
98 }
99
100 if(size == 0) {
103 }
104}
105
110
115
117{
118 if(clean_cache()) {
120 _("Cache Cleaned"),
121 _("The game data cache has been cleaned."));
122 } else {
123 show_error_message(_("The game data cache could not be completely cleaned."));
124 }
125
127}
128
134
136{
137 if(purge_cache()) {
139 _("Cache Purged"),
140 _("The game data cache has been purged."));
141 } else {
142 show_error_message(_("The game data cache could not be purged."));
143 }
144
146}
147
153
154} // namespace dialogs
bool purge_cache()
Deletes all cache files.
static config_cache & instance()
Get reference to the singleton object.
bool clean_cache()
Deletes stale cache files not in use by the game.
Simple push button.
Definition button.hpp:36
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition button.cpp:64
virtual void post_show() override
Actions to be taken after the window has been shown.
virtual void pre_show() override
Actions to be taken before showing the window.
std::string cache_path_
The display function.
Abstract base class for all modal dialogs.
virtual void set_label(const t_string &text)
Abstract base class for text items.
virtual void set_value(const std::string &text)
The set_value is virtual for the password_box class.
virtual void set_active(const bool active) override
See styled_widget::set_active.
Declarations for File-IO.
static std::string _(const char *str)
Definition gettext.hpp:100
This file contains the window object, this object is a top level container which has the event manage...
#define REGISTER_DIALOG(window_id)
Wrapper for REGISTER_DIALOG2.
@ WAIT
Definition cursor.hpp:28
void copy_to_clipboard(const std::string &text)
Copies text to the clipboard.
Definition clipboard.cpp:24
bool open_object(const std::string &path_or_url)
Opens the specified object with the default application configured for its type.
Definition open.cpp:50
int dir_size(const std::string &path)
Returns the sum of the sizes of the files contained in a directory.
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition message.cpp:201
void show_message(const std::string &title, const std::string &msg, const std::string &button_caption, const bool auto_close, const bool message_use_markup, const bool title_use_markup)
Shows a message to the user.
Definition message.cpp:148
std::string si_string(double input, bool base2, const std::string &unit)
Convert into a string with an SI-postfix.
Desktop environment interaction functions.