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
17 #include "gui/dialogs/game_cache_options.hpp"
18
19 #include "desktop/clipboard.hpp"
20 #include "config_cache.hpp"
21 #include "cursor.hpp"
22 #include "desktop/open.hpp"
23 #include "filesystem.hpp"
24 #include "gui/auxiliary/find_widget.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
REGISTER_DIALOG(game_cache_options)77 REGISTER_DIALOG(game_cache_options)
78
79 game_cache_options::game_cache_options()
80 : cache_path_(filesystem::get_cache_dir())
81 , clean_button_(nullptr)
82 , purge_button_(nullptr)
83 , size_label_(nullptr)
84 {
85 }
86
pre_show(window & window)87 void game_cache_options::pre_show(window& window)
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
93 update_cache_size_display();
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);
100 connect_signal_mouse_left_click(copy,
101 std::bind(&game_cache_options::copy_to_clipboard_callback,
102 this));
103 if (!desktop::clipboard::available()) {
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);
109 connect_signal_mouse_left_click(browse,
110 std::bind(&game_cache_options::browse_cache_callback,
111 this));
112
113 connect_signal_mouse_left_click(*clean_button_,
114 std::bind(&game_cache_options::clean_cache_callback,
115 this));
116
117 connect_signal_mouse_left_click(*purge_button_,
118 std::bind(&game_cache_options::purge_cache_callback,
119 this));
120 }
121
post_show(window &)122 void game_cache_options::post_show(window& /*window*/)
123 {
124 size_label_ = nullptr;
125 }
126
update_cache_size_display()127 void game_cache_options::update_cache_size_display()
128 {
129 if(!size_label_) {
130 return;
131 }
132
133 const cursor::setter cs(cursor::WAIT);
134 const int size = filesystem::dir_size(cache_path_);
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
copy_to_clipboard_callback()148 void game_cache_options::copy_to_clipboard_callback()
149 {
150 desktop::clipboard::copy_to_clipboard(cache_path_, false);
151 }
152
browse_cache_callback()153 void game_cache_options::browse_cache_callback()
154 {
155 desktop::open_object(cache_path_);
156 }
157
clean_cache_callback()158 void game_cache_options::clean_cache_callback()
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
168 update_cache_size_display();
169 }
170
clean_cache()171 bool game_cache_options::clean_cache()
172 {
173 const cursor::setter cs(cursor::WAIT);
174 return game_config::config_cache::instance().clean_cache();
175 }
176
purge_cache_callback()177 void game_cache_options::purge_cache_callback()
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
187 update_cache_size_display();
188 }
189
purge_cache()190 bool game_cache_options::purge_cache()
191 {
192 const cursor::setter cs(cursor::WAIT);
193 return game_config::config_cache::instance().purge_cache();
194 }
195
196 } // namespace dialogs
197 } // namespace gui2
198