The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
game_cache_options.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Ignacio Riquelme Morelle <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #include "video.hpp"
36 
37 namespace gui2
38 {
39 
40 /*WIKI
41  * @page = GUIWindowDefinitionWML
42  * @order = 2_game_cache_options
43  *
44  * == Game cache options ==
45  *
46  * A Preferences subdialog including a report on the location and size of the
47  * game's WML cache, buttons to copy its path to clipboard or browse to it,
48  * and the possibility of clearing stale files from the cache or purging it
49  * entirely.
50  *
51  * @begin{table}{dialog_widgets}
52  *
53  * path & & text_box & m &
54  * Cache dir path. $
55  *
56  * copy & & button & m &
57  * Copies the cache path to clipboard. $
58  *
59  * browse & & button & m &
60  * Browses to the cache path using the platform's file management
61  * application. $
62  *
63  * size & & label & m &
64  * Current total size of the cache dir's contents. $
65  *
66  * clean & & button & m &
67  * Cleans the cache, erasing stale files not used by the Wesnoth
68  * version presently running the dialog. $
69  *
70  * purge & & button & m &
71  * Purges the cache in its entirety. $
72  *
73  * @end{table}
74  */
75 
76 REGISTER_DIALOG(game_cache_options)
77 
79  : cache_path_(filesystem::get_cache_dir())
80  , clean_button_(nullptr)
81  , purge_button_(nullptr)
82  , size_label_(nullptr)
83 {
84 }
85 
87 {
88  clean_button_ = &find_widget<tbutton>(&window, "clean", false);
89  purge_button_ = &find_widget<tbutton>(&window, "purge", false);
90  size_label_ = &find_widget<tlabel>(&window, "size", false);
91 
93 
94  ttext_& path_box = find_widget<ttext_>(&window, "path", false);
95  path_box.set_value(cache_path_);
96  path_box.set_active(false);
97 
98  tbutton& copy = find_widget<tbutton>(&window, "copy", false);
101  this));
103  copy.set_active(false);
104  copy.set_tooltip(_("Clipboard support not found, contact your packager"));
105  }
106 
107  tbutton& browse = find_widget<tbutton>(&window, "browse", false);
110  this));
111 
114  this,
115  std::ref(window.video())));
116 
117  connect_signal_mouse_left_click(*purge_button_,
119  this,
120  std::ref(window.video())));
121 }
122 
124 {
125  size_label_ = nullptr;
126 }
127 
129 {
130  if(!size_label_) {
131  return;
132  }
133 
134  const cursor::setter cs(cursor::WAIT);
136 
137  if(size < 0) {
138  size_label_->set_label(_("dir_size^Unknown"));
139  } else {
140  size_label_->set_label(utils::si_string(size, true, _("unit_byte^B")));
141  }
142 
143  if(size == 0) {
144  clean_button_->set_active(false);
145  purge_button_->set_active(false);
146  }
147 }
148 
150 {
152 }
153 
155 {
157 }
158 
160 {
161  if(clean_cache()) {
162  show_message(video,
163  _("Cache Cleaned"),
164  _("The game data cache has been cleaned."));
165  } else {
166  show_error_message(video,
167  _("The game data cache could not be completely cleaned."));
168  }
169 
171 }
172 
174 {
175  const cursor::setter cs(cursor::WAIT);
177 }
178 
180 {
181  if(purge_cache()) {
182  show_message(video,
183  _("Cache Purged"),
184  _("The game data cache has been purged."));
185  } else {
186  show_error_message(video,
187  _("The game data cache could not be purged."));
188  }
189 
191 }
192 
194 {
195  const cursor::setter cs(cursor::WAIT);
197 }
198 
199 } // end namespace gui2
std::string si_string(double input, bool base2, std::string unit)
Convert into a string with an SI-postfix.
virtual void set_active(const bool active) override
See tcontrol::set_active.
Definition: button.cpp:58
void show_error_message(CVideo &video, const std::string &message, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:198
static config_cache & instance()
Get reference to the singleton object.
bool available()
Whether wesnoth was compiled with support for a clipboard.
Definition: clipboard.cpp:61
virtual void set_active(const bool active) override
See tcontrol::set_active.
Definition: text.cpp:56
Definition: video.hpp:58
This file contains the window object, this object is a top level container which has the event manage...
REGISTER_DIALOG(label_settings)
virtual void set_label(const t_string &label)
Definition: control.cpp:330
void connect_signal_mouse_left_click(tdispatcher &dispatcher, const tsignal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.hpp:710
-file util.hpp
virtual void set_value(const std::string &text)
The set_value is virtual for the tpassword_box class.
Definition: text.cpp:95
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
Simple push button.
Definition: button.hpp:32
Desktop environment interaction functions.
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:82
This file contains the settings handling of the widget library.
bool clean_cache()
Deletes stale cache files not in use by the game.
void show_message(CVideo &video, const std::string &title, const std::string &message, const std::string &button_caption, const bool auto_close, const bool message_use_markup)
Shows a message to the user.
Definition: message.cpp:143
void set_tooltip(const t_string &tooltip)
Definition: control.hpp:265
bool open_object(const std::string &path_or_url)
Opens the specified object with the default application configured for its type.
Definition: open.cpp:53
std::string get_cache_dir()
int dir_size(const std::string &path)
Returns the sum of the sizes of the files contained in a directory.
void post_show(twindow &window)
Inherited from tdialog.
void pre_show(twindow &window)
Inherited from tdialog.
Declarations for File-IO.
void purge_cache_callback(CVideo &video)
void clean_cache_callback(CVideo &video)
GLsizeiptr size
Definition: glew.h:1649
void copy_to_clipboard(const std::string &text, const bool)
Copies text to the clipboard.
Definition: clipboard.cpp:40
GLenum GLint ref
Definition: glew.h:1813
Abstract base class for text items.
Definition: text.hpp:43
bool purge_cache()
Deletes all cache files.