The Battle for Wesnoth  1.17.0-dev
manager_ui.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2021
3  by Iris Morelle <shadowm2006@gmail.com>
4  Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
5  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
17 #include "addon/manager_ui.hpp"
18 
19 #include "addon/client.hpp"
20 #include "addon/info.hpp"
21 #include "addon/manager.hpp"
22 #include "filesystem.hpp"
23 #include "formula/string_utils.hpp"
24 #include "preferences/game.hpp"
25 #include "gettext.hpp"
29 #include "gui/dialogs/message.hpp"
31 #include "gui/widgets/retval.hpp"
32 #include "log.hpp"
33 #include "wml_exception.hpp"
34 
35 static lg::log_domain log_config("config");
36 static lg::log_domain log_network("network");
37 static lg::log_domain log_filesystem("filesystem");
38 static lg::log_domain log_addons_client("addons-client");
39 
40 #define ERR_CFG LOG_STREAM(err, log_config)
41 
42 #define ERR_NET LOG_STREAM(err, log_network)
43 
44 #define ERR_FS LOG_STREAM(err, log_filesystem)
45 
46 #define LOG_AC LOG_STREAM(info, log_addons_client)
47 
48 
49 namespace {
50 
51 bool get_addons_list(addons_client& client, addons_list& list)
52 {
53  list.clear();
54 
55  config cfg;
56  client.request_addons_list(cfg);
57 
58  if(!cfg) {
59  return false;
60  }
61 
62  read_addons_list(cfg, list);
63 
64  return true;
65 }
66 
67 bool addons_manager_ui(const std::string& remote_address)
68 {
69  bool need_wml_cache_refresh = false;
70 
71  preferences::set_campaign_server(remote_address);
72 
73  try {
74  addons_client client(remote_address);
75  client.connect();
76 
77  gui2::dialogs::addon_manager dlg(client);
78  dlg.show();
79 
80  need_wml_cache_refresh = dlg.get_need_wml_cache_refresh();
81  } catch(const config::error& e) {
82  ERR_CFG << "config::error thrown during transaction with add-on server; \""<< e.message << "\"" << std::endl;
83  gui2::show_error_message(_("Network communication error."));
84  } catch(const network_asio::error& e) {
85  ERR_NET << "network_asio::error thrown during transaction with add-on server; \""<< e.what() << "\"" << std::endl;
86  gui2::show_error_message(_("Remote host disconnected."));
87  } catch(const filesystem::io_exception& e) {
88  ERR_FS << "filesystem::io_exception thrown while installing an addon; \"" << e.what() << "\"" << std::endl;
89  gui2::show_error_message(_("A problem occurred when trying to create the files necessary to install this add-on."));
90  } catch(const invalid_pbl_exception& e) {
91  ERR_CFG << "could not read .pbl file " << e.path << ": " << e.message << std::endl;
92 
93  utils::string_map symbols;
94  symbols["path"] = e.path;
95  symbols["msg"] = e.message;
96 
98  VGETTEXT("A local file with add-on publishing information could not be read.\n\nFile: $path\nError message: $msg", symbols));
99  } catch(const wml_exception& e) {
100  e.show();
101  } catch(const addons_client::user_exit&) {
102  LOG_AC << "initial connection canceled by user\n";
103  } catch(const addons_client::user_disconnect&) {
104  LOG_AC << "attempt to reconnect canceled by user\n";
105  } catch(const addons_client::invalid_server_address&) {
106  gui2::show_error_message(_("The add-ons server address specified is not valid."));
107  }
108 
109  return need_wml_cache_refresh;
110 }
111 
112 bool uninstall_local_addons()
113 {
114  const std::string list_lead = "\n\n";
115 
116  const std::vector<std::string>& addons = installed_addons();
117 
118  if(addons.empty()) {
119  gui2::show_error_message(_("You have no add-ons installed."));
120  return false;
121  }
122 
123  std::map<std::string, std::string> addon_titles_map;
124 
125  for(const std::string& id : addons) {
126  std::string title;
127 
128  if(have_addon_install_info(id)) {
129  // _info.cfg may have the add-on's title starting with 1.11.7,
130  // if the add-on was downloading using the revised _info.cfg writer.
131  config info_cfg;
132  get_addon_install_info(id, info_cfg);
133 
134  if(!info_cfg.empty()) {
135  title = info_cfg["title"].str();
136  }
137  }
138 
139  if(title.empty()) {
140  // Transform the id into a title as a last resort.
141  title = make_addon_title(id);
142  }
143 
144  addon_titles_map[id] = title;
145  }
146 
147  int res;
148 
149  std::vector<std::string> remove_ids;
150  std::set<std::string> remove_names;
151 
152  do {
153  gui2::dialogs::addon_uninstall_list dlg(addon_titles_map);
154  dlg.show();
155 
156  remove_ids = dlg.selected_addons();
157  if(remove_ids.empty()) {
158  return false;
159  }
160 
161  remove_names.clear();
162 
163  for(const std::string& id : remove_ids) {
164  remove_names.insert(addon_titles_map[id]);
165  }
166 
167  const std::string confirm_message = _n(
168  "Are you sure you want to remove the following installed add-on?",
169  "Are you sure you want to remove the following installed add-ons?",
170  remove_ids.size()) + list_lead + utils::bullet_list(remove_names);
171 
172  res = gui2::show_message(
173  _("Confirm")
174  , confirm_message
176  } while (res != gui2::retval::OK);
177 
178  std::set<std::string> failed_names, skipped_names, succeeded_names;
179 
180  for(const std::string& id : remove_ids) {
181  const std::string& name = addon_titles_map[id];
182 
184  skipped_names.insert(name);
185  } else if(remove_local_addon(id)) {
186  succeeded_names.insert(name);
187  } else {
188  failed_names.insert(name);
189  }
190  }
191 
192  if(!skipped_names.empty()) {
193  const std::string dlg_msg = _n(
194  "The following add-on appears to have publishing or version control information stored locally, and will not be removed:",
195  "The following add-ons appear to have publishing or version control information stored locally, and will not be removed:",
196  skipped_names.size());
197 
199  dlg_msg + list_lead + utils::bullet_list(skipped_names));
200  }
201 
202  if(!failed_names.empty()) {
204  "The following add-on could not be deleted properly:",
205  "The following add-ons could not be deleted properly:",
206  failed_names.size()) + list_lead + utils::bullet_list(failed_names));
207  }
208 
209  if(!succeeded_names.empty()) {
210  const std::string dlg_title =
211  _n("Add-on Deleted", "Add-ons Deleted", succeeded_names.size());
212  const std::string dlg_msg = _n(
213  "The following add-on was successfully deleted:",
214  "The following add-ons were successfully deleted:",
215  succeeded_names.size());
216 
218  dlg_title,
219  dlg_msg + list_lead + utils::bullet_list(succeeded_names), "", false, false, true);
220 
221  return true;
222  }
223 
224  return false;
225 }
226 
227 } // end anonymous namespace
228 
230 {
231  static const int addon_download = 0;
232  // NOTE: the following two values are also known by WML, so don't change them.
233  static const int addon_uninstall = 2;
234 
235  std::string host_name = preferences::campaign_server();
236  const bool have_addons = !installed_addons().empty();
237 
238  gui2::dialogs::addon_connect addon_dlg(host_name, have_addons);
239  addon_dlg.show();
240  int res = addon_dlg.get_retval();
241 
242  if(res == gui2::retval::OK) {
243  res = addon_download;
244  }
245 
246  switch(res) {
247  case addon_download:
248  return addons_manager_ui(host_name);
249  case addon_uninstall:
250  return uninstall_local_addons();
251  default:
252  return false;
253  }
254 }
255 
256 bool ad_hoc_addon_fetch_session(const std::vector<std::string>& addon_ids)
257 {
258  std::string remote_address = preferences::campaign_server();
259 
260  // These exception handlers copied from addon_manager_ui fcn above.
261  try {
262 
263  addons_client client(remote_address);
264  client.connect();
265 
266  addons_list addons;
267 
268  if(!get_addons_list(client, addons)) {
269  gui2::show_error_message(_("An error occurred while downloading the add-ons list from the server."));
270  return false;
271  }
272 
273  bool return_value = true;
274  std::ostringstream os;
275  for(const std::string& addon_id : addon_ids) {
276  addons_list::const_iterator it = addons.find(addon_id);
277  if(it != addons.end()) {
278  const addon_info& addon = it->second;
279  // don't redownload in case it was already downloaded for being another add-on's dependency
281  addons_client::install_result res = client.install_addon_with_checks(addons, addon);
282  return_value = return_value && (res.outcome == addons_client::install_outcome::success);
283  }
284  } else {
285  if(!return_value) {
286  os << ", ";
287  }
288  os << addon_id;
289  return_value = false;
290  }
291  }
292 
293  if(!return_value) {
294  utils::string_map symbols;
295  symbols["addon_ids"] = os.str();
296  gui2::show_error_message(VGETTEXT("Could not find add-ons matching the ids $addon_ids on the add-on server.", symbols));
297  }
298 
299  return return_value;
300 
301  } catch(const config::error& e) {
302  ERR_CFG << "config::error thrown during transaction with add-on server; \""<< e.message << "\"" << std::endl;
303  gui2::show_error_message(_("Network communication error."));
304  } catch(const network_asio::error& e) {
305  ERR_NET << "network_asio::error thrown during transaction with add-on server; \""<< e.what() << "\"" << std::endl;
306  gui2::show_error_message(_("Remote host disconnected."));
307  } catch(const filesystem::io_exception& e) {
308  ERR_FS << "io_exception thrown while installing an addon; \"" << e.what() << "\"" << std::endl;
309  gui2::show_error_message(_("A problem occurred when trying to create the files necessary to install this add-on."));
310  } catch(const invalid_pbl_exception& e) {
311  ERR_CFG << "could not read .pbl file " << e.path << ": " << e.message << std::endl;
312 
313  utils::string_map symbols;
314  symbols["path"] = e.path;
315  symbols["msg"] = e.message;
316 
318  VGETTEXT("A local file with add-on publishing information could not be read.\n\nFile: $path\nError message: $msg", symbols));
319  } catch(const wml_exception& e) {
320  e.show();
321  } catch(const addons_client::user_exit&) {
322  LOG_AC << "initial connection canceled by user\n";
323  } catch(const addons_client::invalid_server_address&) {
324  gui2::show_error_message(_("The add-ons server address specified is not valid."));
325  }
326 
327  return false;
328 }
Shows the list of addons on the server available for installation.
Definition: manager.hpp:54
void read_addons_list(const config &cfg, addons_list &dest)
Parse the specified add-ons list WML into an actual addons_list object.
Definition: info.cpp:294
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:153
void set_campaign_server(const std::string &host)
Definition: game.cpp:425
static std::string _n(const char *str1, const char *str2, int n)
Definition: gettext.hpp:97
std::map< std::string, t_string > string_map
const std::string path
Path to the faulty .pbl file.
Definition: manager.hpp:57
#define ERR_FS
Definition: manager_ui.cpp:44
#define ERR_CFG
Definition: manager_ui.cpp:40
std::string campaign_server()
Definition: game.cpp:416
This shows the dialog for managing addons and connecting to the addon server.
Definition: connect.hpp:33
Add a special kind of assert to validate whether the input from WML doesn&#39;t contain any problems that...
Exception thrown when the WML parser fails to read a .pbl file.
Definition: manager.hpp:44
#define LOG_AC
Definition: manager_ui.cpp:46
static bool file_exists(const bfs::path &fpath)
Definition: filesystem.cpp:263
void show_transient_message(const std::string &title, const std::string &message, const std::string &image, const bool message_use_markup, const bool title_use_markup, const bool restore_background)
Shows a transient message to the user.
void get_addon_install_info(const std::string &addon_name, config &cfg)
Gets the installation info (_info.cfg) for an add-on.
Definition: manager.cpp:93
std::vector< std::string > selected_addons() const
bool have_addon_in_vcs_tree(const std::string &addon_name)
Returns whether the specified add-on appears to be managed by a VCS or not.
Definition: manager.cpp:54
static std::string _(const char *str)
Definition: gettext.hpp:93
bool show(const unsigned auto_close_time=0)
Shows the window.
#define ERR_NET
Definition: manager_ui.cpp:42
static lg::log_domain log_network("network")
bool have_addon_install_info(const std::string &addon_name)
Returns true if there is a local installation info (_info.cfg) file for the add-on.
Definition: manager.cpp:88
void show() const
Shows the error in a dialog.
std::string bullet_list(const T &v, std::size_t indent=4, const std::string &bullet=font::unicode_bullet)
Generates a new string containing a bullet list.
bool get_need_wml_cache_refresh() const
Definition: manager.hpp:59
bool ad_hoc_addon_fetch_session(const std::vector< std::string > &addon_ids)
Conducts an ad-hoc add-ons server connection to download an add-on with a particular id and all it&#39;s ...
Definition: manager_ui.cpp:256
const std::string message
Error message to display.
Definition: manager.hpp:60
std::vector< std::string > installed_addons()
Retrieves the names of all installed add-ons.
Definition: manager.cpp:178
Shows a yes and no button.
Definition: message.hpp:80
bool remove_local_addon(const std::string &addon)
Removes the specified add-on, deleting its full directory structure.
Definition: manager.cpp:130
void connect()
Tries to establish a connection to the add-ons server.
Definition: client.cpp:68
const char * what() const noexcept
Definition: exceptions.hpp:36
bool manage_addons()
Shows the add-ons server connection dialog, for access to the various management front-ends.
Definition: manager_ui.cpp:229
Add-ons (campaignd) client class.
Definition: client.hpp:40
The add-on was correctly installed.
Helper class, don&#39;t construct this directly.
static lg::log_domain log_addons_client("addons-client")
std::string id
Text to match against addon_info.tags()
Definition: manager.cpp:215
bool have_addon_pbl_info(const std::string &addon_name)
Returns whether a .pbl file is present for the specified add-on or not.
Definition: manager.cpp:63
An exception object used when an IO error occurs.
Definition: filesystem.hpp:48
Declarations for File-IO.
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
Dialog with a checkbox list for choosing installed add-ons to remove.
install_outcome outcome
Overall outcome of the operation.
Definition: client.hpp:129
std::string make_addon_title(const std::string &id)
Replaces underscores to dress up file or dirnames as add-on titles.
Definition: info.cpp:320
static lg::log_domain log_filesystem("filesystem")
int get_retval() const
Returns the cached window exit code.
std::string get_addons_dir()
Standard logging facilities (interface).
std::string message
Definition: exceptions.hpp:30
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:206
#define e
Dialog was closed with the OK button.
Definition: retval.hpp:35
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
Contains the outcome of an add-on install operation.
Definition: client.hpp:124
std::map< std::string, addon_info > addons_list
Definition: info.hpp:28
install_result install_addon_with_checks(const addons_list &addons, const addon_info &addon)
Performs an add-on download and install cycle.
Definition: client.cpp:555
bool request_addons_list(config &cfg)
Request the add-ons list from the server.
Definition: client.cpp:114
bool empty() const
Definition: config.cpp:941
Networked add-ons (campaignd) client interface.
static lg::log_domain log_config("config")