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