The Battle for Wesnoth  1.15.12+dev
game_version_dialog.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2013 - 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 "build_info.hpp"
20 #include "desktop/clipboard.hpp"
21 #include "desktop/open.hpp"
22 #include "desktop/version.hpp"
23 #include "filesystem.hpp"
24 #include "formula/string_utils.hpp"
25 #include "game_config.hpp"
27 #include "gui/widgets/button.hpp"
29 #include "gui/widgets/listbox.hpp"
31 #include "gui/widgets/settings.hpp"
34 #include "gui/widgets/window.hpp"
35 #ifdef _WIN32
36 #include "log_windows.hpp"
37 #endif
38 
39 #include <functional>
40 
41 namespace
42 {
43 
44 const std::string text_feature_on = "<span color='#0f0'>&#9679;</span>";
45 const std::string text_feature_off = "<span color='#f00'>&#9679;</span>";
46 
47 } // end anonymous namespace
48 
49 namespace gui2::dialogs
50 {
51 
52 REGISTER_DIALOG(game_version)
53 
55  : path_wid_stem_("path_")
56  , copy_wid_stem_("copy_")
57  , browse_wid_stem_("browse_")
58  , path_map_()
59 #ifdef _WIN32
60  , log_path_(lg::log_file_path())
61 #endif
62  , deps_()
64  , report_()
65 {
66  // NOTE: these path_map_ entries are referenced by the GUI2 WML
67  // definition of this dialog using preprocessor macros.
68  path_map_["datadir"] = game_config::path;
69  path_map_["config"] = filesystem::get_user_config_dir();
70  path_map_["userdata"] = filesystem::get_user_data_dir();
71  path_map_["saves"] = filesystem::get_saves_dir();
72  path_map_["addons"] = filesystem::get_addons_dir();
73  path_map_["cache"] = filesystem::get_cache_dir();
74 #ifdef _WIN32
75  path_map_["logs"] = filesystem::get_logs_dir();
76 #endif
77 
78  for(unsigned k = 0; k < game_config::LIB_COUNT; ++k) {
80 
82  e[0] = game_config::library_name(lib);
83  if(e[0].empty()) {
84  continue;
85  }
88  deps_.push_back(e);
89  }
90 
91  generate_plain_text_report();
92 }
93 
95 {
96  string_map i18n_syms;
97 
98  //
99  // General information.
100  //
101 
102  styled_widget& version_label = find_widget<styled_widget>(&window, "version", false);
103  i18n_syms["version"] = game_config::revision + " " + game_config::build_arch();
104  version_label.set_label(VGETTEXT("Version $version", i18n_syms));
105 
106  styled_widget& os_label = find_widget<styled_widget>(&window, "os", false);
107  i18n_syms["os"] = desktop::os_version();
108  os_label.set_label(VGETTEXT("Running on $os", i18n_syms));
109 
110  button& copy_all = find_widget<button>(&window, "copy_all", false);
112  copy_all,
113  std::bind(&game_version::report_copy_callback, this));
114 
115  //
116  // Game paths tab.
117  //
118 
119  for(const auto & path_ent : path_map_)
120  {
121  const std::string& path_id = path_ent.first;
122  const std::string& path_path = filesystem::normalize_path(path_ent.second, true);
123 
124  text_box_base& path_w
125  = find_widget<text_box_base>(&window, path_wid_stem_ + path_id, false);
126  button& copy_w = find_widget<button>(
127  &window, copy_wid_stem_ + path_id, false);
128  button& browse_w = find_widget<button>(
129  &window, browse_wid_stem_ + path_id, false);
130 
131  path_w.set_value(path_path);
132  path_w.set_active(false);
133 
135  copy_w,
137  this,
138  path_path));
140  browse_w,
142  this,
143  path_path));
144 
146  // No point in displaying these on platforms that can't do
147  // open_object().
148  browse_w.set_visible(widget::visibility::invisible);
149  }
150 
152  copy_w.set_active(false);
153  copy_w.set_tooltip(_("Clipboard support not found, contact your packager"));
154  }
155  }
156 
157 #ifndef _WIN32
158  for(const auto& wid : {"win32_paths", "label_logs", "path_logs", "copy_logs", "browse_logs"}) {
159  find_widget<widget>(&window, wid, false).set_visible(widget::visibility::invisible);
160  }
161 #else
162  button& stderr_button
163  = find_widget<button>(&window, "open_stderr", false);
165  stderr_button,
167  this,
168  log_path_));
169  stderr_button.set_active(!log_path_.empty());
170 #endif
171 
172  //
173  // Build info tab.
174  //
175 
176  std::map<std::string, string_map> list_data;
177 
178  listbox& deps_listbox
179  = find_widget<listbox>(&window, "deps_listbox", false);
180 
181  for(const auto & dep : deps_)
182  {
183  list_data["dep_name"]["label"] = dep[0];
184 
185  list_data["dep_build_version"]["label"] = dep[1];
186 
187  // The build version is always known, but runtime version isn't, esp.
188  // for header-only libraries like Boost for which the concept does not
189  // apply.
190  if(!dep[2].empty()) {
191  list_data["dep_rt_version"]["label"] = dep[2];
192  } else {
193  list_data["dep_rt_version"]["label"] = font::unicode_em_dash;
194  }
195 
196  deps_listbox.add_row(list_data);
197  }
198 
199  deps_listbox.select_row(0);
200  list_data.clear();
201 
202  //
203  // Features tab.
204  //
205 
206  listbox& opts_listbox
207  = find_widget<listbox>(&window, "opts_listbox", false);
208 
209  for(const auto & opt : opts_)
210  {
211  list_data["opt_name"]["label"] = opt.name;
212 
213  if(opt.enabled) {
214  list_data["opt_status"]["label"] = text_feature_on;
215  } else {
216  list_data["opt_status"]["label"] = text_feature_off;
217  }
218  list_data["opt_status"]["use_markup"] = "true";
219 
220  opts_listbox.add_row(list_data);
221  }
222 
223  opts_listbox.select_row(0);
224  list_data.clear();
225 
226  //
227  // Set-up page stack and auxiliary controls last.
228  //
229 
230  stacked_widget& pager
231  = find_widget<stacked_widget>(&window, "tabs_container", false);
232  pager.select_layer(0);
233 
234  listbox& tab_bar
235  = find_widget<listbox>(&window, "tab_bar", false);
236 
237  window.keyboard_capture(&tab_bar);
238 
239  const unsigned tab_count = tab_bar.get_item_count();
240  VALIDATE(tab_count == pager.get_layer_count(), "Tab bar and container size mismatch");
241 
243  std::bind(&game_version::tab_switch_callback, this));
244 }
245 
247 {
248  stacked_widget& pager
249  = find_widget<stacked_widget>(get_window(), "tabs_container", false);
250  listbox& tab_bar
251  = find_widget<listbox>(get_window(), "tab_bar", false);
252 
253  pager.select_layer(std::max<int>(0, tab_bar.get_selected_row()));
254 }
255 
257 {
258  desktop::open_object(path);
259 }
260 
262 {
264 }
265 
267 {
269 }
270 
272 {
274 }
275 
276 } // namespace dialogs
void report_copy_callback()
Callback function for the dialog-wide copy-to-clipboard button.
Abstract base class for text items.
const std::string path_wid_stem_
The display function.
std::string log_file_path()
Returns the path to the current log file.
bool available()
Whether wesnoth was compiled with support for a clipboard.
Definition: clipboard.cpp:54
unsigned int get_layer_count() const
Gets the total number of layers.
std::map< std::string, std::string > path_map_
void tab_switch_callback()
Callback function called when switching tabs.
This file contains the window object, this object is a top level container which has the event manage...
std::string get_logs_dir()
Returns the path to the permanent log storage directory.
Definition: log_windows.cpp:48
std::array< std::string, 4 > deplist_entry
Dialog displaying the various paths used by the game to locate resource and configuration files...
window * get_window() const
Returns a pointer to the dialog&#39;s window.
const std::string & library_build_version(LIBRARY_ID lib)
Retrieve the build-time version number of the given library.
Definition: build_info.cpp:354
std::vector< optional_feature > optional_features_table(bool localize)
Retrieve the features table.
Definition: build_info.cpp:335
std::string normalize_path(const std::string &fpath, bool normalize_separators, bool resolve_dot_entries)
Returns the absolute path of a file.
static std::string _(const char *str)
Definition: gettext.hpp:92
virtual void pre_show(window &window) override
Actions to be taken before showing the window.
std::string get_saves_dir()
bool select_row(const unsigned row, const bool select=true)
Selects a row.
Definition: listbox.cpp:251
constexpr bool open_object_is_supported()
Returns whether open_object() is supported/implemented for the current platform.
Definition: open.hpp:53
virtual void set_label(const t_string &label)
The listbox class.
Definition: listbox.hpp:42
Desktop environment interaction functions.
void copy_to_clipboard_callback(const std::string &path)
Callback function for copy-to-clipboard action buttons.
const std::string & library_name(LIBRARY_ID lib)
Retrieve the user-visible name for the given library.
Definition: build_info.cpp:372
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:186
std::vector< deplist_entry > deps_
#define VALIDATE(cond, message)
The macro to use for the validation of WML.
This file contains the settings handling of the widget library.
std::string get_user_data_dir()
Definition: filesystem.cpp:789
void browse_directory_callback(const std::string &path)
Callback function for browse-directory action buttons.
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
std::string path
Definition: game_config.cpp:38
std::string build_arch()
Obtain the processor architecture for this build.
Definition: build_info.cpp:302
unsigned get_item_count() const
Returns the number of items in the listbox.
Definition: listbox.cpp:132
Platform identification and version information functions.
bool open_object([[maybe_unused]] const std::string &path_or_url)
Definition: open.cpp:46
std::string get_cache_dir()
Definition: filesystem.cpp:794
Log file control routines for Windows.
const std::string revision
The user set the widget invisible, that means:
std::string os_version()
Returns a string with the running OS name and version information.
Definition: version.cpp:212
const std::string & library_runtime_version(LIBRARY_ID lib)
Retrieve the runtime version number of the given library.
Definition: build_info.cpp:363
std::map< std::string, t_string > string_map
Definition: widget.hpp:26
grid & add_row(const string_map &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:67
Declarations for File-IO.
Base class for all visible items.
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
void select_layer(const int layer)
Selects and displays a particular layer.
std::vector< game_config::optional_feature > opts_
std::string get_user_config_dir()
Definition: filesystem.cpp:760
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
std::string get_addons_dir()
const std::string unicode_em_dash
Definition: constants.cpp:43
Simple push button.
Definition: button.hpp:35
#define e
A stacked widget holds several widgets on top of each other.
base class of top level items, the only item which needs to store the final canvases to draw on...
Definition: window.hpp:64
std::string full_build_report()
Produce a bug report-style info dump.
Definition: build_info.cpp:646
virtual void set_active(const bool active) override
See styled_widget::set_active.