The Battle for Wesnoth  1.17.17+dev
game_cache_options.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2023
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"
26 #include "gui/dialogs/message.hpp"
27 #include "gui/widgets/button.hpp"
28 #include "gui/widgets/label.hpp"
29 #include "gui/widgets/settings.hpp"
30 #include "gui/widgets/text_box.hpp"
31 #include "gui/widgets/window.hpp"
32 
33 #include <functional>
34 
35 #include "gettext.hpp"
36 
37 namespace gui2::dialogs
38 {
39 
40 REGISTER_DIALOG(game_cache_options)
41 
43  : modal_dialog(window_id())
44  , cache_path_(filesystem::get_cache_dir())
45  , clean_button_(nullptr)
46  , purge_button_(nullptr)
47  , size_label_(nullptr)
48 {
49 }
50 
52 {
53  clean_button_ = find_widget<button>(&window, "clean", false, true);
54  purge_button_ = find_widget<button>(&window, "purge", false, true);
55  size_label_ = find_widget<label>(&window, "size", false, true);
56 
58 
59  text_box_base& path_box = find_widget<text_box_base>(&window, "path", false);
60  path_box.set_value(cache_path_);
61  path_box.set_active(false);
62 
63  button& copy = find_widget<button>(&window, "copy", false);
66  this));
68  copy.set_active(false);
69  copy.set_tooltip(_("Clipboard support not found, contact your packager"));
70  }
71 
72  button& browse = find_widget<button>(&window, "browse", false);
75  this));
76 
79  this));
80 
83  this));
84 }
85 
87 {
88  size_label_ = nullptr;
89 }
90 
92 {
93  if(!size_label_) {
94  return;
95  }
96 
97  const cursor::setter cs(cursor::WAIT);
99 
100  if(size < 0) {
101  size_label_->set_label(_("dir_size^Unknown"));
102  } else {
103  size_label_->set_label(utils::si_string(size, true, _("unit_byte^B")));
104  }
105 
106  if(size == 0) {
107  clean_button_->set_active(false);
108  purge_button_->set_active(false);
109  }
110 }
111 
113 {
115 }
116 
118 {
120 }
121 
123 {
124  if(clean_cache()) {
125  show_message(
126  _("Cache Cleaned"),
127  _("The game data cache has been cleaned."));
128  } else {
129  show_error_message(_("The game data cache could not be completely cleaned."));
130  }
131 
133 }
134 
136 {
137  const cursor::setter cs(cursor::WAIT);
139 }
140 
142 {
143  if(purge_cache()) {
144  show_message(
145  _("Cache Purged"),
146  _("The game data cache has been purged."));
147  } else {
148  show_error_message(_("The game data cache could not be purged."));
149  }
150 
152 }
153 
155 {
156  const cursor::setter cs(cursor::WAIT);
158 }
159 
160 } // 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:37
virtual void set_active(const bool active) override
See styled_widget::set_active.
Definition: button.cpp:65
A Preferences subdialog including a report on the location and size of the game's WML cache,...
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
std::string cache_path_
The display function.
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
Abstract base class for all modal dialogs.
void set_tooltip(const t_string &tooltip)
virtual void set_label(const t_string &label)
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.
base class of top level items, the only item which needs to store the final canvases to draw on.
Definition: window.hpp:67
Declarations for File-IO.
static std::string _(const char *str)
Definition: gettext.hpp:93
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:29
void copy_to_clipboard(const std::string &text, const bool)
Copies text to the clipboard.
Definition: clipboard.cpp:34
bool available()
Whether wesnoth was compiled with support for a clipboard.
Definition: clipboard.cpp:55
bool open_object([[maybe_unused]] const std::string &path_or_url)
Definition: open.cpp:47
std::string get_cache_dir()
Definition: filesystem.cpp:867
int dir_size(const std::string &pname)
Returns the sum of the sizes of the files contained in a directory.
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:179
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:204
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:151
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:87
std::string si_string(double input, bool base2, const std::string &unit)
Convert into a string with an SI-postfix.
Desktop environment interaction functions.
This file contains the settings handling of the widget library.