The Battle for Wesnoth  1.15.12+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 <functional>
33 
34 #include "gettext.hpp"
35 
36 namespace gui2::dialogs
37 {
38 
39 REGISTER_DIALOG(game_cache_options)
40 
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>(&window, "clean", false, true);
52  purge_button_ = find_widget<button>(&window, "purge", false, true);
53  size_label_ = find_widget<label>(&window, "size", false, true);
54 
56 
57  text_box_base& path_box = find_widget<text_box_base>(&window, "path", false);
58  path_box.set_value(cache_path_);
59  path_box.set_active(false);
60 
61  button& copy = find_widget<button>(&window, "copy", false);
64  this));
66  copy.set_active(false);
67  copy.set_tooltip(_("Clipboard support not found, contact your packager"));
68  }
69 
70  button& browse = find_widget<button>(&window, "browse", false);
73  this));
74 
77  this));
78 
79  connect_signal_mouse_left_click(*purge_button_,
81  this));
82 }
83 
85 {
86  size_label_ = nullptr;
87 }
88 
90 {
91  if(!size_label_) {
92  return;
93  }
94 
95  const cursor::setter cs(cursor::WAIT);
97 
98  if(size < 0) {
99  size_label_->set_label(_("dir_size^Unknown"));
100  } else {
101  size_label_->set_label(utils::si_string(size, true, _("unit_byte^B")));
102  }
103 
104  if(size == 0) {
105  clean_button_->set_active(false);
106  purge_button_->set_active(false);
107  }
108 }
109 
111 {
113 }
114 
116 {
118 }
119 
121 {
122  if(clean_cache()) {
123  show_message(
124  _("Cache Cleaned"),
125  _("The game data cache has been cleaned."));
126  } else {
127  show_error_message(_("The game data cache could not be completely cleaned."));
128  }
129 
131 }
132 
134 {
135  const cursor::setter cs(cursor::WAIT);
137 }
138 
140 {
141  if(purge_cache()) {
142  show_message(
143  _("Cache Purged"),
144  _("The game data cache has been purged."));
145  } else {
146  show_error_message(_("The game data cache could not be purged."));
147  }
148 
150 }
151 
153 {
154  const cursor::setter cs(cursor::WAIT);
156 }
157 
158 } // namespace dialogs
virtual void post_show(window &window) override
Actions to be taken after the window has been shown.
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:54
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...
static std::string _(const char *str)
Definition: gettext.hpp:92
virtual void set_label(const t_string &label)
Desktop environment interaction functions.
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:171
A Preferences subdialog including a report on the location and size of the game&#39;s WML cache...
bool open_object([[maybe_unused]] const std::string &path_or_url)
Definition: open.cpp:46
std::string get_cache_dir()
Definition: filesystem.cpp:794
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:33
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:64
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
virtual void set_active(const bool active) override
See styled_widget::set_active.
bool purge_cache()
Deletes all cache files.