The Battle for Wesnoth  1.15.2+dev
game_cache_options.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Iris Morelle <shadowm2006@gmail.com>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 
19 #include "desktop/clipboard.hpp"
20 #include "config_cache.hpp"
21 #include "cursor.hpp"
22 #include "desktop/open.hpp"
23 #include "filesystem.hpp"
25 #include "gui/dialogs/message.hpp"
26 #include "gui/widgets/button.hpp"
27 #include "gui/widgets/label.hpp"
28 #include "gui/widgets/settings.hpp"
29 #include "gui/widgets/text_box.hpp"
30 #include "gui/widgets/window.hpp"
31 
32 #include "utils/functional.hpp"
33 
34 #include "gettext.hpp"
35 
36 namespace gui2
37 {
38 namespace dialogs
39 {
40 
41 /*WIKI
42  * @page = GUIWindowDefinitionWML
43  * @order = 2_game_cache_options
44  *
45  * == Game cache options ==
46  *
47  * A Preferences subdialog including a report on the location and size of the
48  * game's WML cache, buttons to copy its path to clipboard or browse to it,
49  * and the possibility of clearing stale files from the cache or purging it
50  * entirely.
51  *
52  * @begin{table}{dialog_widgets}
53  *
54  * path & & text_box & m &
55  * Cache dir path. $
56  *
57  * copy & & button & m &
58  * Copies the cache path to clipboard. $
59  *
60  * browse & & button & m &
61  * Browses to the cache path using the platform's file management
62  * application. $
63  *
64  * size & & label & m &
65  * Current total size of the cache dir's contents. $
66  *
67  * clean & & button & m &
68  * Cleans the cache, erasing stale files not used by the Wesnoth
69  * version presently running the dialog. $
70  *
71  * purge & & button & m &
72  * Purges the cache in its entirety. $
73  *
74  * @end{table}
75  */
76 
77 REGISTER_DIALOG(game_cache_options)
78 
80  : cache_path_(filesystem::get_cache_dir())
81  , clean_button_(nullptr)
82  , purge_button_(nullptr)
83  , size_label_(nullptr)
84 {
85 }
86 
88 {
89  clean_button_ = find_widget<button>(&window, "clean", false, true);
90  purge_button_ = find_widget<button>(&window, "purge", false, true);
91  size_label_ = find_widget<label>(&window, "size", false, true);
92 
94 
95  text_box_base& path_box = find_widget<text_box_base>(&window, "path", false);
96  path_box.set_value(cache_path_);
97  path_box.set_active(false);
98 
99  button& copy = find_widget<button>(&window, "copy", false);
102  this));
104  copy.set_active(false);
105  copy.set_tooltip(_("Clipboard support not found, contact your packager"));
106  }
107 
108  button& browse = find_widget<button>(&window, "browse", false);
111  this));
112 
115  this));
116 
117  connect_signal_mouse_left_click(*purge_button_,
119  this));
120 }
121 
123 {
124  size_label_ = nullptr;
125 }
126 
128 {
129  if(!size_label_) {
130  return;
131  }
132 
133  const cursor::setter cs(cursor::WAIT);
135 
136  if(size < 0) {
137  size_label_->set_label(_("dir_size^Unknown"));
138  } else {
139  size_label_->set_label(utils::si_string(size, true, _("unit_byte^B")));
140  }
141 
142  if(size == 0) {
143  clean_button_->set_active(false);
144  purge_button_->set_active(false);
145  }
146 }
147 
149 {
151 }
152 
154 {
156 }
157 
159 {
160  if(clean_cache()) {
161  show_message(
162  _("Cache Cleaned"),
163  _("The game data cache has been cleaned."));
164  } else {
165  show_error_message(_("The game data cache could not be completely cleaned."));
166  }
167 
169 }
170 
172 {
173  const cursor::setter cs(cursor::WAIT);
175 }
176 
178 {
179  if(purge_cache()) {
180  show_message(
181  _("Cache Purged"),
182  _("The game data cache has been purged."));
183  } else {
184  show_error_message(_("The game data cache could not be purged."));
185  }
186 
188 }
189 
191 {
192  const cursor::setter cs(cursor::WAIT);
194 }
195 
196 } // namespace dialogs
197 } // namespace gui2
virtual void post_show(window &window) override
Inherited from modal_dialog.
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:152
static config_cache & instance()
Get reference to the singleton object.
Abstract base class for text items.
bool available()
Whether wesnoth was compiled with support for a clipboard.
Definition: clipboard.cpp:56
std::string cache_path_
The display function.
This file contains the window object, this object is a top level container which has the event manage...
Generic file dialog.
Definition: field-fwd.hpp:22
virtual void set_label(const t_string &label)
Desktop environment interaction functions.
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:91
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
void set_tooltip(const t_string &tooltip)
This file contains the settings handling of the widget library.
std::string si_string(double input, bool base2, const std::string &unit)
Convert into a string with an SI-postfix.
bool clean_cache()
Deletes stale cache files not in use by the game.
void connect_signal_mouse_left_click(dispatcher &dispatcher, const signal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:233
Various uncategorised dialogs.
bool open_object(const std::string &path_or_url)
Opens the specified object with the default application configured for its type.
Definition: open.cpp:55
std::string get_cache_dir()
Definition: filesystem.cpp:785
int dir_size(const std::string &pname)
Returns the sum of the sizes of the files contained in a directory.
Declarations for File-IO.
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.
Definition: button.cpp:62
void copy_to_clipboard(const std::string &text, const bool)
Copies text to the clipboard.
Definition: clipboard.cpp:35
Simple push button.
Definition: button.hpp:35
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:205
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
virtual void pre_show(window &window) override
Inherited from modal_dialog.
virtual void set_active(const bool active) override
See styled_widget::set_active.
bool purge_cache()
Deletes all cache files.