The Battle for Wesnoth  1.15.0-dev
manager.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
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 "addon/info.hpp"
20 #include "addon/manager.hpp"
21 #include "addon/state.hpp"
22 
23 #include "desktop/clipboard.hpp"
24 #include "desktop/open.hpp"
25 
26 #include "help/help.hpp"
27 #include "gettext.hpp"
28 #include "gui/auxiliary/filter.hpp"
30 #include "gui/dialogs/message.hpp"
32 #include "gui/widgets/button.hpp"
33 #include "gui/widgets/label.hpp"
37 #include "gui/widgets/drawing.hpp"
38 #include "gui/widgets/image.hpp"
39 #include "gui/widgets/listbox.hpp"
40 #include "gui/widgets/pane.hpp"
41 #include "gui/widgets/settings.hpp"
43 #include "gui/widgets/text_box.hpp"
44 #include "gui/widgets/window.hpp"
46 #include "formula/string_utils.hpp"
47 #include "picture.hpp"
48 #include "language.hpp"
49 #include "preferences/general.hpp"
50 #include "utils/general.hpp"
51 
52 #include "config.hpp"
53 
54 #include "utils/functional.hpp"
55 
56 #include <iomanip>
57 #include <sstream>
58 #include <stdexcept>
59 
60 namespace gui2
61 {
62 namespace dialogs
63 {
64 
65 /*WIKI
66  * @page = GUIWindowDefinitionWML
67  * @order = 2_addon_list
68  *
69  * == Addon list ==
70  *
71  * This shows the dialog with the addons to install. This dialog is under
72  * construction and only used with --new-widgets.
73  *
74  * @begin{table}{dialog_widgets}
75  *
76  * addons & & listbox & m &
77  * A listbox that will contain the info about all addons on the server. $
78  *
79  * -name & & styled_widget & o &
80  * The name of the addon. $
81  *
82  * -version & & styled_widget & o &
83  * The version number of the addon. $
84  *
85  * -author & & styled_widget & o &
86  * The author of the addon. $
87  *
88  * -downloads & & styled_widget & o &
89  * The number of times the addon has been downloaded. $
90  *
91  * -size & & styled_widget & o &
92  * The size of the addon. $
93  *
94  * @end{table}
95  */
96 
97 namespace {
98  struct filter_transform
99  {
100  explicit filter_transform(const std::vector<std::string>& filtertext) : filtertext_(filtertext) {}
101  bool operator()(const config& cfg) const
102  {
103  for(const auto& filter : filtertext_)
104  {
105  bool found = false;
106  for(const auto& attribute : cfg.attribute_range())
107  {
108  std::string val = attribute.second.str();
109  if(std::search(val.begin(),
110  val.end(),
111  filter.begin(),
112  filter.end(),
114  != val.end())
115  {
116  found = true;
117  break;
118  }
119  }
120  if(!found) {
121  return false;
122  }
123  }
124  return true;
125  }
126  const std::vector<std::string> filtertext_;
127  };
128 
129  std::string make_display_dependencies(
130  const std::string& addon_id,
131  const addons_list& addons_list,
132  const addons_tracking_list& addon_states)
133  {
134  const addon_info& addon = addons_list.at(addon_id);
135  std::string str;
136 
137  const std::set<std::string>& deps = addon.resolve_dependencies(addons_list);
138 
139  for(const auto& dep_id : deps) {
140  addon_info dep;
141  addon_tracking_info depstate;
142 
143  addons_list::const_iterator ali = addons_list.find(dep_id);
144  addons_tracking_list::const_iterator tli = addon_states.find(dep_id);
145 
146  if(ali == addons_list.end()) {
147  dep.id = dep_id; // Build dummy addon_info.
148  } else {
149  dep = ali->second;
150  }
151 
152  if(tli == addon_states.end()) {
153  depstate = get_addon_tracking_info(dep);
154  } else {
155  depstate = tli->second;
156  }
157 
158  if(!str.empty()) {
159  str += ", ";
160  }
161 
163  }
164 
165  return str;
166  }
167 
168  std::string langcode_to_string(const std::string& lcode)
169  {
170  for(const auto & ld : get_languages())
171  {
172  if(ld.localename == lcode || ld.localename.substr(0, 2) == lcode) {
173  return ld.language;
174  }
175  }
176 
177  return "";
178  }
179 }
180 
181 REGISTER_DIALOG(addon_manager)
182 
183 const std::vector<std::pair<ADDON_STATUS_FILTER, std::string>> addon_manager::status_filter_types_{
184  {FILTER_ALL, N_("addons_view^All Add-ons")},
185  {FILTER_INSTALLED, N_("addons_view^Installed")},
186  {FILTER_UPGRADABLE, N_("addons_view^Upgradable")},
187  {FILTER_PUBLISHABLE, N_("addons_view^Publishable")},
188  {FILTER_NOT_INSTALLED, N_("addons_view^Not Installed")},
189 };
190 
191 const std::vector<std::pair<ADDON_TYPE, std::string>> addon_manager::type_filter_types_{
192  {ADDON_SP_CAMPAIGN, N_("addons_of_type^Campaigns")},
193  {ADDON_SP_SCENARIO, N_("addons_of_type^Scenarios")},
194  {ADDON_SP_MP_CAMPAIGN, N_("addons_of_type^SP/MP campaigns")},
195  {ADDON_MP_CAMPAIGN, N_("addons_of_type^MP campaigns")},
196  {ADDON_MP_SCENARIO, N_("addons_of_type^MP scenarios")},
197  {ADDON_MP_MAPS, N_("addons_of_type^MP map-packs")},
198  {ADDON_MP_ERA, N_("addons_of_type^MP eras")},
199  {ADDON_MP_FACTION, N_("addons_of_type^MP factions")},
200  {ADDON_MOD, N_("addons_of_type^Modifications")},
201  {ADDON_CORE, N_("addons_of_type^Cores")},
202  {ADDON_MEDIA, N_("addons_of_type^Resources")},
203  // FIXME: (also in WML) should this and Unknown be a single option in the UI?
204  {ADDON_OTHER, N_("addons_of_type^Other")},
205  {ADDON_UNKNOWN, N_("addons_of_type^Unknown")},
206 };
207 
208 const std::vector<addon_manager::addon_order> addon_manager::all_orders_{
209  {N_("addons_order^Name ($order)"), 0,
210  [](const addon_info& a, const addon_info& b) { return a.title < b.title; },
211  [](const addon_info& a, const addon_info& b) { return a.title > b.title; }},
212  {N_("addons_order^Author ($order)"), 1,
213  [](const addon_info& a, const addon_info& b) { return a.author < b.author; },
214  [](const addon_info& a, const addon_info& b) { return a.author > b.author; }},
215  {N_("addons_order^Size ($order)"), 2,
216  [](const addon_info& a, const addon_info& b) { return a.size < b.size; },
217  [](const addon_info& a, const addon_info& b) { return a.size > b.size; }},
218  {N_("addons_order^Downloads ($order)"), 3,
219  [](const addon_info& a, const addon_info& b) { return a.downloads < b.downloads; },
220  [](const addon_info& a, const addon_info& b) { return a.downloads > b.downloads; }},
221  {N_("addons_order^Type ($order)"), 4,
222  [](const addon_info& a, const addon_info& b) { return a.display_type() < b.display_type(); },
223  [](const addon_info& a, const addon_info& b) { return a.display_type() > b.display_type(); }},
224  {N_("addons_order^Last updated ($datelike_order)"), -1,
225  [](const addon_info& a, const addon_info& b) { return a.updated < b.updated; },
226  [](const addon_info& a, const addon_info& b) { return a.updated > b.updated; }},
227  {N_("addons_order^First uploaded ($datelike_order)"), -1,
228  [](const addon_info& a, const addon_info& b) { return a.created < b.created; },
229  [](const addon_info& a, const addon_info& b) { return a.created > b.created; }}
230 };
231 
233  : orders_()
234  , cfg_()
235  , client_(client)
236  , addons_()
237  , tracking_info_()
238  , need_wml_cache_refresh_(false)
239 {
240 }
241 
243 {
244  apply_filters(*textbox->get_window());
245 }
246 
247 static std::string describe_status_verbose(const addon_tracking_info& state)
248 {
249  std::string s;
250 
251  utils::string_map i18n_symbols {{"local_version", state.installed_version.str()}};
252 
253  switch(state.state) {
254  case ADDON_NONE:
255  s = !state.can_publish
256  ? _("addon_state^Not installed")
257  : _("addon_state^Published, not installed");
258  break;
259  case ADDON_INSTALLED:
260  s = !state.can_publish
261  ? _("addon_state^Installed")
262  : _("addon_state^Published");
263  break;
264  case ADDON_NOT_TRACKED:
265  s = !state.can_publish
266  ? _("addon_state^Installed, not tracking local version")
267  // Published add-ons often don't have local status information,
268  // hence untracked. This should be considered normal.
269  : _("addon_state^Published, not tracking local version");
270  break;
272  const std::string vstr = !state.can_publish
273  ? _("addon_state^Installed ($local_version|), upgradable")
274  : _("addon_state^Published ($local_version| installed), upgradable");
275 
276  s = utils::interpolate_variables_into_string(vstr, &i18n_symbols);
277  } break;
279  const std::string vstr = !state.can_publish
280  ? _("addon_state^Installed ($local_version|), outdated on server")
281  : _("addon_state^Published ($local_version| installed), outdated on server");
282 
283  s = utils::interpolate_variables_into_string(vstr, &i18n_symbols);
284  } break;
286  s = !state.can_publish
287  ? _("addon_state^Installed, not ready to publish")
288  : _("addon_state^Ready to publish");
289  break;
291  s = !state.can_publish
292  ? _("addon_state^Installed, broken")
293  : _("addon_state^Published, broken");
294  break;
295  default:
296  s = _("addon_state^Unknown");
297  }
298 
300 }
301 
303 {
304  window.set_escape_disabled(true);
305 
306  addon_list& list = find_widget<addon_list>(&window, "addons", false);
307 
308  text_box& filter = find_widget<text_box>(&window, "filter", false);
310 
312  this, std::placeholders::_1, std::ref(window)));
314  this, std::placeholders::_1, std::ref(window)));
316  this, std::placeholders::_1, std::ref(window)));
317 
319  this, std::placeholders::_1, std::ref(window)));
321  this, std::placeholders::_1, std::ref(window)));
322 
323  list.set_modified_signal_handler([this, &window]() { on_addon_select(window); });
324 
325  fetch_addons_list(window);
326  load_addon_list(window);
327 
328  menu_button& status_filter = find_widget<menu_button>(&window, "install_status_filter", false);
329 
330  std::vector<config> status_filter_entries;
331  for(const auto& f : status_filter_types_) {
332  status_filter_entries.emplace_back("label", t_string(f.second, GETTEXT_DOMAIN));
333  }
334 
335  status_filter.set_values(status_filter_entries);
336 
337  connect_signal_notify_modified(status_filter,
338  std::bind(&addon_manager::apply_filters, this, std::ref(window)));
339 
340  multimenu_button& type_filter = find_widget<multimenu_button>(&window, "type_filter", false);
341 
342  std::vector<config> type_filter_entries;
343  for(const auto& f : type_filter_types_) {
344  type_filter_entries.emplace_back("label", t_string(f.second, GETTEXT_DOMAIN), "checkbox", false);
345  }
346 
347  type_filter.set_values(type_filter_entries);
348 
349  connect_signal_notify_modified(type_filter,
350  std::bind(&addon_manager::apply_filters, this, std::ref(window)));
351 
352  menu_button& order_dropdown = find_widget<menu_button>(&window, "order_dropdown", false);
353 
354  std::vector<config> order_dropdown_entries;
355  for(const auto& f : all_orders_) {
356  utils::string_map symbols;
357 
358  symbols["order"] = _("ascending");
359  // TRANSLATORS: Sorting order of dates, oldest first
360  symbols["datelike_order"] = _("oldest to newest");
361  config entry{"label", VGETTEXT(f.label.c_str(), symbols)};
362  order_dropdown_entries.push_back(entry);
363  symbols["order"] = _("descending");
364  // TRANSLATORS: Sorting order of dates, newest first
365  symbols["datelike_order"] = _("newest to oldest");
366  entry["label"] = VGETTEXT(f.label.c_str(), symbols);
367  order_dropdown_entries.push_back(entry);
368  }
369 
370  order_dropdown.set_values(order_dropdown_entries);
371 
372  connect_signal_notify_modified(order_dropdown,
373  std::bind(&addon_manager::order_addons, this, std::ref(window)));
374 
375  button& url_go_button = find_widget<button>(&window, "url_go", false);
376  button& url_copy_button = find_widget<button>(&window, "url_copy", false);
377  text_box& url_textbox = find_widget<text_box>(&window, "url", false);
378 
379  url_textbox.set_active(false);
380 
382  url_copy_button.set_active(false);
383  url_copy_button.set_tooltip(_("Clipboard support not found, contact your packager"));
384  }
385 
387  // No point in displaying the button on platforms that can't do
388  // open_object().
390  }
391 
393  find_widget<button>(&window, "install", false),
394  std::bind(&addon_manager::install_selected_addon, this, std::ref(window)));
395 
397  find_widget<button>(&window, "uninstall", false),
398  std::bind(&addon_manager::uninstall_selected_addon, this, std::ref(window)));
399 
401  find_widget<button>(&window, "update", false),
402  std::bind(&addon_manager::update_selected_addon, this, std::ref(window)));
403 
405  find_widget<button>(&window, "publish", false),
406  std::bind(&addon_manager::publish_selected_addon, this, std::ref(window)));
407 
409  find_widget<button>(&window, "delete", false),
410  std::bind(&addon_manager::delete_selected_addon, this, std::ref(window)));
411 
413  find_widget<button>(&window, "update_all", false),
414  std::bind(&addon_manager::update_all_addons, this, std::ref(window)));
415 
417  url_go_button,
418  std::bind(&addon_manager::browse_url_callback, this, std::ref(url_textbox)));
419 
421  url_copy_button,
422  std::bind(&addon_manager::copy_url_callback, this, std::ref(url_textbox)));
423 
425  find_widget<button>(&window, "show_help", false),
426  std::bind(&addon_manager::show_help, this));
427 
428  if(stacked_widget* stk = find_widget<stacked_widget>(&window, "main_stack", false, false)) {
429  button& btn = find_widget<button>(&window, "details_toggle", false);
430  connect_signal_mouse_left_click(btn, std::bind(&addon_manager::toggle_details, this, std::ref(btn), std::ref(*stk)));
431  stk->select_layer(0);
432  }
433 
434  on_addon_select(window);
435 
436  window.set_enter_disabled(true);
437 
438  window.keyboard_capture(&filter);
440 
441  list.set_callback_order_change(std::bind(&addon_manager::on_order_changed, this, std::ref(window),
442  std::placeholders::_1, std::placeholders::_2));
443 
444  // Use handle the special addon_list retval to allow installing addons on double click
445  window.set_exit_hook(std::bind(&addon_manager::exit_hook, this, std::ref(window)));
446 }
447 
449 {
450  if(stk.current_layer() == 0) {
451  btn.set_label(_("addons^Back to List"));
452  stk.select_layer(1);
453  } else {
454  btn.set_label(_("Addon Details"));
455  stk.select_layer(0);
456  }
457 }
458 
460 {
462  if(!cfg_) {
463  gui2::show_error_message(_("An error occurred while downloading the add-ons list from the server."));
464  window.close();
465  }
466 }
467 
469 {
472  }
473 
475 
476  std::vector<std::string> publishable_addons = available_addons();
477 
478  for(std::string id : publishable_addons) {
479  if(addons_.find(id) == addons_.end()) {
480  // Get a config from the addon's pbl file
481  // Note that the name= key is necessary or stuff breaks, since the filter code uses this key
482  // to match add-ons in the config list. It also fills in addon_info's id field. It's also
483  // neccessay to set local_only here so that flag can be properly set after addons_ is cleared
484  // and recreated by read_addons_list.
485  config pbl_cfg = get_addon_pbl_info(id);
486  pbl_cfg["name"] = id;
487  pbl_cfg["local_only"] = true;
488 
489  // Add the add-on to the list.
490  addon_info addon(pbl_cfg);
491  addons_[id] = addon;
492 
493  // Add the addon to the config entry
494  cfg_.add_child("campaign", std::move(pbl_cfg));
495  }
496  }
497 
498  if(addons_.empty()) {
499  show_transient_message(_("No Add-ons Available"), _("There are no add-ons available for download from this server."));
500  }
501 
502  addon_list& list = find_widget<addon_list>(&window, "addons", false);
503  list.set_addons(addons_);
504 
505  bool has_upgradable_addons = false;
506  for(const auto& a : addons_) {
507  tracking_info_[a.first] = get_addon_tracking_info(a.second);
508 
509  if(tracking_info_[a.first].state == ADDON_INSTALLED_UPGRADABLE) {
510  has_upgradable_addons = true;
511  }
512  }
513 
514  find_widget<button>(&window, "update_all", false).set_active(has_upgradable_addons);
515 
516  apply_filters(window);
517 }
518 
520 {
521  load_addon_list(window);
522 
523  // Reselect the add-on.
524  find_widget<addon_list>(&window, "addons", false).select_addon(id);
525  on_addon_select(window);
526 }
527 
528 boost::dynamic_bitset<> addon_manager::get_name_filter_visibility(const window& window) const
529 {
530  const text_box& name_filter = find_widget<const text_box>(&window, "filter", false);
531  const std::string& text = name_filter.get_value();
532 
533  filter_transform filter(utils::split(text, ' '));
534  boost::dynamic_bitset<> res;
535 
536  const config::const_child_itors& addon_cfgs = cfg_.child_range("campaign");
537 
538  for(const auto& a : addons_)
539  {
540  const config& addon_cfg = *std::find_if(addon_cfgs.begin(), addon_cfgs.end(),
541  [&a](const config& cfg)
542  {
543  return cfg["name"] == a.first;
544  });
545 
546  res.push_back(filter(addon_cfg));
547  }
548 
549  return res;
550 }
551 
552 boost::dynamic_bitset<> addon_manager::get_status_filter_visibility(const window& window) const
553 {
554  const menu_button& status_filter = find_widget<const menu_button>(&window, "install_status_filter", false);
555  const ADDON_STATUS_FILTER selection = status_filter_types_[status_filter.get_value()].first;
556 
557  boost::dynamic_bitset<> res;
558  for(const auto& a : addons_) {
559  const addon_tracking_info& info = tracking_info_.at(a.second.id);
560 
561  res.push_back(
562  (selection == FILTER_ALL) ||
563  (selection == FILTER_INSTALLED && is_installed_addon_status(info.state)) ||
564  (selection == FILTER_UPGRADABLE && info.state == ADDON_INSTALLED_UPGRADABLE) ||
565  (selection == FILTER_PUBLISHABLE && info.can_publish == true) ||
566  (selection == FILTER_NOT_INSTALLED && info.state == ADDON_NONE)
567  );
568  }
569 
570  return res;
571 }
572 
573 boost::dynamic_bitset<> addon_manager::get_type_filter_visibility(const window& window) const
574 {
575  const multimenu_button& type_filter = find_widget<const multimenu_button>(&window, "type_filter", false);
576 
577  boost::dynamic_bitset<> toggle_states = type_filter.get_toggle_states();
578  if(toggle_states.none()) {
579  // Nothing selected. It means that *all* add-ons are shown.
580  boost::dynamic_bitset<> res_flipped(addons_.size());
581  return ~res_flipped;
582  } else {
583  boost::dynamic_bitset<> res;
584 
585  for(const auto& a : addons_) {
586  int index = std::distance(type_filter_types_.begin(),
587  std::find_if(type_filter_types_.begin(), type_filter_types_.end(),
588  [&a](const std::pair<ADDON_TYPE, std::string>& entry) {
589  return entry.first == a.second.type;
590  })
591  );
592  res.push_back(toggle_states[index]);
593  }
594 
595  return res;
596  }
597 }
598 
600 {
601  boost::dynamic_bitset<> res =
604  & get_name_filter_visibility(window);
605  find_widget<addon_list>(&window, "addons", false).set_addon_shown(res);
606 }
607 
609 {
610  const menu_button& order_menu = find_widget<const menu_button>(&window, "order_dropdown", false);
611  const addon_order& order_struct = all_orders_.at(order_menu.get_value() / 2);
614  if(order == listbox::SORT_ASCENDING) {
615  func = order_struct.sort_func_asc;
616  } else {
617  func = order_struct.sort_func_desc;
618  }
619 
620  find_widget<addon_list>(&window, "addons", false).set_addon_order(func);
621 }
622 
623 void addon_manager::on_order_changed(window& window, unsigned int sort_column, listbox::SORT_ORDER order)
624 {
625  menu_button& order_menu = find_widget<menu_button>(&window, "order_dropdown", false);
626  auto order_it = std::find_if(all_orders_.begin(), all_orders_.end(),
627  [sort_column](const addon_order& order) {return order.column_index == static_cast<int>(sort_column);});
628  int index = 2 * (std::distance(all_orders_.begin(), order_it));
629  if(order == listbox::SORT_DESCENDING) {
630  ++index;
631  }
632  order_menu.set_value(index);
633 }
634 
635 template<void(addon_manager::*fptr)(const addon_info& addon, window& window)>
637 {
638  // Explicitly return to the main page if we're in low-res mode so the list is visible.
639  if(stacked_widget* stk = find_widget<stacked_widget>(&window, "main_stack", false, false)) {
640  stk->select_layer(0);
641  find_widget<button>(&window, "details_toggle", false).set_label(_("Addon Details"));
642  }
643 
644  addon_list& addons = find_widget<addon_list>(&window, "addons", false);
645  const addon_info* addon = addons.get_selected_addon();
646 
647  if(addon == nullptr) {
648  return;
649  }
650 
651  try {
652  (this->*fptr)(*addon, window);
653  } catch(const addons_client::user_exit&) {
654  // User canceled the op.
655  }
656 }
657 
659 {
661 
662  // Take note if any wml_changes occurred
664 
666  reload_list_and_reselect_item(addon.id, window);
667  }
668 }
669 
671 {
672  if(have_addon_pbl_info(addon.id) || have_addon_in_vcs_tree(addon.id)) {
674  _("The following add-on appears to have publishing or version control information stored locally, and will not be removed:") + " " +
675  addon.display_title());
676  return;
677  }
678 
679  bool success = remove_local_addon(addon.id);
680 
681  if(!success) {
682  gui2::show_error_message(_("The following add-on could not be deleted properly:") + " " + addon.display_title());
683  } else {
685 
686  reload_list_and_reselect_item(addon.id, window);
687  }
688 }
689 
691 {
692  /* Currently, the install and update codepaths are the same, so this function simply
693  * calls the other. Since this might change in the future, I'm leaving this function
694  * here for now.
695  *
696  * - vultraz, 2017-03-12
697  */
698  install_addon(addon, window);
699 }
700 
702 {
703  for(const auto& a : addons_) {
704  if(tracking_info_[a.first].state == ADDON_INSTALLED_UPGRADABLE) {
706 
707  // Take note if any wml_changes occurred
709  }
710  }
711 
713  load_addon_list(window);
714  }
715 }
716 
717 /** Performs all backend and UI actions for publishing the specified add-on. */
719 {
720  std::string server_msg;
721 
722  const std::string addon_id = addon.id;
723  config cfg = get_addon_pbl_info(addon_id);
724 
725  const version_info& version_to_publish = cfg["version"].str();
726 
727  if(version_to_publish <= tracking_info_[addon_id].remote_version) {
728  const int res = gui2::show_message(_("Warning"),
729  _("The remote version of this add-on is greater or equal to the version being uploaded. Do you really wish to continue?"),
731 
732  if(res != gui2::retval::OK) {
733  return;
734  }
735  }
736 
737  if(!::image::exists(cfg["icon"].str())) {
738  gui2::show_error_message(_("Invalid icon path. Make sure the path points to a valid image."));
739  } else if(!client_.request_distribution_terms(server_msg)) {
741  _("The server responded with an error:") + "\n" + client_.get_last_server_error());
742  } else if(gui2::show_message(_("Terms"), server_msg, gui2::dialogs::message::ok_cancel_buttons, true) == gui2::retval::OK) {
743  if(!client_.upload_addon(addon_id, server_msg, cfg)) {
744  const std::string& msg = _("The server responded with an error:") +
745  "\n\n" + client_.get_last_server_error();
746  const std::string& extra_data = client_.get_last_server_error_data();
747  if (!extra_data.empty()) {
748  // TODO: Allow user to copy the extra data portion to clipboard
749  // or something, maybe display it in a dialog similar to
750  // the WML load errors report in a monospace font and
751  // stuff (having a scroll container is especially
752  // important since a long list can cause the dialog to
753  // overflow).
754  gui2::show_error_message(msg + "\n\n" + extra_data, true);
755  } else {
756  gui2::show_error_message(msg, true);
757  }
758  } else {
759  gui2::show_transient_message(_("Response"), server_msg);
760  fetch_addons_list(window);
761  reload_list_and_reselect_item(addon_id, window);
762  }
763  }
764 }
765 
766 /** Performs all backend and UI actions for taking down the specified add-on. */
768 {
769  const std::string addon_id = addon.id;
770  const std::string& text = VGETTEXT(
771  "Deleting '$addon|' will permanently erase its download and upload counts on the add-ons server. Do you really wish to continue?",
772  {{"addon", make_addon_title(addon_id)}} // FIXME: need the real title!
773  );
774 
775  const int res = gui2::show_message(_("Confirm"), text, gui2::dialogs::message::yes_no_buttons);
776 
777  if(res != gui2::retval::OK) {
778  return;
779  }
780 
781  std::string server_msg;
782  if(!client_.delete_remote_addon(addon_id, server_msg)) {
783  gui2::show_error_message(_("The server responded with an error:") + "\n" + client_.get_last_server_error());
784  } else {
785  // FIXME: translation needed!
786  gui2::show_transient_message(_("Response"), server_msg);
787  fetch_addons_list(window);
788  reload_list_and_reselect_item(addon_id, window);
789  }
790 }
791 
792 /** Called when the player double-clicks an add-on. */
794 {
795  switch(tracking_info_[addon.id].state) {
796  case ADDON_NONE:
797  install_addon(addon, window);
798  break;
799  case ADDON_INSTALLED:
800  if(!tracking_info_[addon.id].can_publish) {
801  utils::string_map symbols{ { "addon", addon.display_title() } };
802  int res = gui2::show_message(_("Uninstall add-on"),
803  VGETTEXT("Do you want to uninstall '$addon|'?", symbols),
805  if(res == gui2::retval::OK) {
806  uninstall_addon(addon, window);
807  }
808  }
809  break;
811  update_addon(addon, window);
812  break;
815  publish_addon(addon, window);
816  break;
817  default:
818  break;
819  }
820 }
821 
823 {
824  help::show_help("installing_addons");
825 }
826 
828 {
829  /* TODO: ask for confirmation */
830  desktop::open_object(url_box.get_value());
831 }
832 
834 {
836 }
837 
838 static std::string format_addon_time(std::time_t time)
839 {
840  if(time) {
841  std::ostringstream ss;
842 
844  ? "%Y-%m-%d %I:%M %p"
845  : "%Y-%m-%d %H:%M";
846 
847  ss << std::put_time(std::localtime(&time), format);
848 
849  return ss.str();
850  }
851 
852  return font::unicode_em_dash;
853 }
854 
856 {
857  const addon_info* info = find_widget<addon_list>(&window, "addons", false).get_selected_addon();
858 
859  if(info == nullptr) {
860  return;
861  }
862 
863  widget* parent = &window;
864  if(stacked_widget* stk = find_widget<stacked_widget>(&window, "main_stack", false, false)) {
865  parent = stk->get_layer_grid(1);
866  }
867 
868  find_widget<drawing>(parent, "image", false).set_label(info->display_icon());
869 
870  find_widget<styled_widget>(parent, "title", false).set_label(info->display_title());
871  find_widget<styled_widget>(parent, "description", false).set_label(info->description);
872  find_widget<styled_widget>(parent, "version", false).set_label(info->version.str());
873  find_widget<styled_widget>(parent, "author", false).set_label(info->author);
874  find_widget<styled_widget>(parent, "type", false).set_label(info->display_type());
875 
876  styled_widget& status = find_widget<styled_widget>(parent, "status", false);
878  status.set_use_markup(true);
879 
880  find_widget<styled_widget>(parent, "size", false).set_label(size_display_string(info->size));
881  find_widget<styled_widget>(parent, "downloads", false).set_label(std::to_string(info->downloads));
882  find_widget<styled_widget>(parent, "created", false).set_label(format_addon_time(info->created));
883  find_widget<styled_widget>(parent, "updated", false).set_label(format_addon_time(info->updated));
884 
885  find_widget<styled_widget>(parent, "dependencies", false).set_label(!info->depends.empty()
886  ? make_display_dependencies(info->id, addons_, tracking_info_)
887  : _("addon_dependencies^None"));
888 
889  std::string languages;
890 
891  for(const auto& lc : info->locales) {
892  const std::string& langlabel = langcode_to_string(lc);
893  if(!langlabel.empty()) {
894  if(!languages.empty()) {
895  languages += ", ";
896  }
897  languages += langlabel;
898  }
899  }
900 
901  find_widget<styled_widget>(parent, "translations", false).set_label(!languages.empty() ? languages : _("translations^None"));
902 
903  const std::string& feedback_url = info->feedback_url;
904 
905  if(!feedback_url.empty()) {
906  find_widget<stacked_widget>(parent, "feedback_stack", false).select_layer(1);
907  find_widget<text_box>(parent, "url", false).set_value(feedback_url);
908  } else {
909  find_widget<stacked_widget>(parent, "feedback_stack", false).select_layer(0);
910  }
911 
912  bool installed = is_installed_addon_status(tracking_info_[info->id].state);
913  bool updatable = tracking_info_[info->id].state == ADDON_INSTALLED_UPGRADABLE;
914 
915  stacked_widget& action_stack = find_widget<stacked_widget>(parent, "action_stack", false);
916 
917  if(!tracking_info_[info->id].can_publish) {
918  action_stack.select_layer(0);
919 
920  stacked_widget& install_update_stack = find_widget<stacked_widget>(parent, "install_update_stack", false);
921  install_update_stack.select_layer(updatable ? 1 : 0);
922 
923  if(!updatable) {
924  find_widget<button>(parent, "install", false).set_active(!installed);
925  } else {
926  find_widget<button>(parent, "update", false).set_active(true);
927  }
928 
929  find_widget<button>(parent, "uninstall", false).set_active(installed);
930  } else {
931  action_stack.select_layer(1);
932 
933  // Always enable the publish button, but disable the delete button if not yet published.
934  find_widget<button>(parent, "publish", false).set_active(true);
935  find_widget<button>(parent, "delete", false).set_active(!info->local_only);
936  }
937 }
938 
940 {
943  return false;
944  }
945 
946  return true;
947 }
948 
949 } // namespace dialogs
950 } // namespace gui2
int size
Definition: info.hpp:41
const std::string & get_last_server_error() const
Returns the last error message sent by the server, or an empty string.
Definition: client.hpp:66
void read_addons_list(const config &cfg, addons_list &dest)
Definition: info.cpp:197
void close()
Requests to close the window.
Definition: window.hpp:183
void set_text_changed_callback(std::function< void(text_box_base *textbox, const std::string text)> cb)
Set the text_changed callback.
ADDON_STATUS state
Definition: state.hpp:56
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
Modification of the game.
Definition: validation.hpp:52
Single-player scenario.
Definition: validation.hpp:44
void show_help(const std::string &show_topic, int xloc, int yloc)
Open the help browser, show topic with id show_topic.
Definition: help.cpp:116
config cfg_
Config which contains the list with the campaigns.
Definition: manager.hpp:81
Abstract base class for text items.
std::string interpolate_variables_into_string(const std::string &str, const string_map *const symbols)
Function which will interpolate variables, starting with &#39;$&#39; in the string &#39;str&#39; with the equivalent ...
std::map< std::string, t_string > string_map
bool exit_hook(window &window)
Definition: manager.cpp:939
void load_addon_list(window &window)
Definition: manager.cpp:468
std::function< bool(const addon_info &, const addon_info &)> addon_sort_func
Definition: addon_list.hpp:41
an add-on that fits in no other category
Definition: validation.hpp:55
static std::string format_addon_time(std::time_t time)
Definition: manager.cpp:838
Simple push button.
Definition: menu_button.hpp:41
Total Conversion Core.
Definition: validation.hpp:42
bool available()
Whether wesnoth was compiled with support for a clipboard.
Definition: clipboard.cpp:56
addons_tracking_list tracking_info_
Definition: manager.hpp:87
std::string display_title() const
Get a title or automatic title for display.
Definition: info.cpp:126
void execute_default_action_on_selected_addon(window &window)
Definition: manager.hpp:129
logger & info()
Definition: log.cpp:90
void add_list_to_keyboard_chain()
Adds the internal listbox to the keyboard event chain.
Definition: addon_list.cpp:361
#define a
Shows an ok and cancel button.
Definition: message.hpp:75
std::string get_value() const
This file contains the window object, this object is a top level container which has the event manage...
child_itors child_range(config_key_type key)
Definition: config.cpp:366
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.
Base class for all widgets.
Definition: widget.hpp:47
std::string description
Definition: info.hpp:33
int current_layer() const
Gets the current visible layer number.
Multiplayer faction.
Definition: validation.hpp:50
virtual void set_value(unsigned value, bool fire_event=false) override
Inherited from selectable_item.
Definition: menu_button.hpp:67
void set_escape_disabled(const bool escape_disabled)
Disable the escape key.
Definition: window.hpp:300
void update_all_addons(window &window)
Definition: manager.cpp:701
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
static const std::vector< std::pair< ADDON_STATUS_FILTER, std::string > > status_filter_types_
Definition: manager.hpp:89
void install_selected_addon(window &window)
Definition: manager.hpp:99
bool chars_equal_insensitive(char a, char b)
Definition: general.hpp:21
Simple push button.
std::string feedback_url
Definition: info.hpp:55
bool have_addon_in_vcs_tree(const std::string &addon_name)
Returns true if the specified add-ons appear to be managed by a &#39;supported&#39; VCS.
Definition: manager.cpp:65
std::deque< editor_action_ptr > action_stack
Action stack typedef.
Definitions for the interface to Wesnoth Markup Language (WML).
Define the common filters for the gui2::pane class.
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
version_info installed_version
Definition: state.hpp:59
No tracking information available.
Definition: state.hpp:36
const_attr_itors attribute_range() const
Definition: config.cpp:811
Add-on is not installed.
Definition: state.hpp:23
Class for a single line text area.
Definition: text_box.hpp:121
Generic file dialog.
Definition: field-fwd.hpp:22
std::vector< std::string > available_addons()
Returns a list of local add-ons that can be published.
Definition: manager.cpp:131
void execute_action_on_selected_addon(window &window)
Definition: manager.cpp:636
#define b
const std::vector< std::string > filtertext_
Definition: manager.cpp:126
virtual void set_label(const t_string &label)
bool exists(const image::locator &i_locator)
returns true if the given image actually exists, without loading it.
Definition: picture.cpp:1217
boost::dynamic_bitset get_type_filter_visibility(const window &window) const
Definition: manager.cpp:573
Desktop environment interaction functions.
virtual unsigned get_value() const override
Inherited from selectable_item.
Definition: menu_button.hpp:64
void set_delete_function(addon_op_func_t function)
Sets the function to install an addon from the addons server.
Definition: addon_list.hpp:100
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:89
void on_addon_select(window &window)
Definition: manager.cpp:855
Version in the server is older than local installation.
Definition: state.hpp:29
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:232
void reload_list_and_reselect_item(const std::string id, window &window)
Definition: manager.cpp:519
void uninstall_addon(const addon_info &addon, window &window)
Definition: manager.cpp:670
bool is_installed_addon_status(ADDON_STATUS s)
Definition: state.hpp:39
This file contains the settings handling of the widget library.
ADDON_STATUS_FILTER
Add-on installation status filters for the user interface.
Definition: state.hpp:83
void set_visible(const visibility visible)
Definition: widget.cpp:473
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:217
void set_callback_order_change(std::function< void(unsigned, listbox::SORT_ORDER)> callback)
Sets up a callback that will be called when the player changes the sorting order. ...
Definition: addon_list.hpp:135
std::string display_icon() const
Get an icon path fixed for display (e.g.
Definition: info.cpp:135
const std::string & get_last_server_error_data() const
Returns the last error message extra data sent by the server, or an empty string. ...
Definition: client.hpp:69
std::string id
Definition: info.hpp:31
Version in the server is newer than local installation.
Definition: state.hpp:27
void toggle_details(button &btn, stacked_widget &stk)
Definition: manager.cpp:448
void set_values(const std::vector<::config > &values, unsigned selected=0)
std::string display_type() const
Get an add-on type identifier for display in the user&#39;s language.
Definition: info.cpp:152
Shows a yes and no button.
Definition: message.hpp:79
void refresh_addon_version_info_cache()
Refreshes the per-session cache of add-on&#39;s version information structs.
Definition: manager.cpp:283
void set_addons(const addons_list &addons)
Sets the add-ons to show.
Definition: addon_list.cpp:147
bool remove_local_addon(const std::string &addon)
Definition: manager.cpp:117
Dependencies not satisfied.
Definition: state.hpp:34
void copy_url_callback(text_box &url_box)
Definition: manager.cpp:833
Add-ons (campaignd) client class.
Definition: client.hpp:29
Various uncategorised dialogs.
int get_retval()
Definition: window.hpp:371
config get_addon_pbl_info(const std::string &addon_name)
Gets the publish information for an add-on.
Definition: manager.cpp:79
void delete_addon(const addon_info &addon, window &window)
Performs all backend and UI actions for taking down the specified add-on.
Definition: manager.cpp:767
Miscellaneous content/media (unit packs, terrain packs, music packs, etc.).
Definition: validation.hpp:54
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:238
static std::string describe_status_verbose(const addon_tracking_info &state)
Definition: manager.cpp:247
bool local_only
Definition: info.hpp:67
bool open_object(const std::string &path_or_url)
Opens the specified object with the default application configured for its type.
Definition: open.cpp:55
std::time_t created
Definition: info.hpp:58
std::vector< std::string > locales
Definition: info.hpp:48
Multiplayer scenario.
Definition: validation.hpp:47
void on_order_changed(window &window, unsigned int sort_column, listbox::SORT_ORDER order)
Definition: manager.cpp:623
boost::dynamic_bitset get_toggle_states() const
Get the current state of the menu options.
Multiplayer era.
Definition: validation.hpp:49
boost::dynamic_bitset get_name_filter_visibility(const window &window) const
Definition: manager.cpp:528
bool have_addon_pbl_info(const std::string &addon_name)
Returns true if there&#39;s a local .pbl file stored for the specified add-on.
Definition: manager.cpp:74
void install_addon(const addon_info &addon, window &window)
Definition: manager.cpp:658
void browse_url_callback(text_box &url_box)
Definition: manager.cpp:827
addons_client & client_
Definition: manager.hpp:83
The user set the widget invisible, that means:
window * get_window()
Get the parent window.
Definition: widget.cpp:114
static map_location::DIRECTION s
void uninstall_selected_addon(window &window)
Definition: manager.hpp:105
void execute_default_action(const addon_info &addon, window &window)
Called when the player double-clicks an add-on.
Definition: manager.cpp:793
std::string size_display_string(double size)
Get a human-readable representation of the specified byte count.
Definition: info.cpp:217
addon_manager(addons_client &client)
Definition: manager.cpp:232
void set_publish_function(addon_op_func_t function)
Sets the function to upload an addon to the addons server.
Definition: addon_list.hpp:94
std::time_t updated
Definition: info.hpp:57
void fetch_addons_list(window &window)
Definition: manager.cpp:459
language_list get_languages()
Definition: language.cpp:115
void set_update_function(addon_op_func_t function)
Sets the function to call when the player clicks the update button.
Definition: addon_list.hpp:88
void delete_selected_addon(window &window)
Definition: manager.hpp:123
Multiplayer plain (no WML) map pack.
Definition: validation.hpp:48
void apply_filters(window &window)
Definition: manager.cpp:599
#define N_(String)
Definition: gettext.hpp:97
std::size_t index(const std::string &str, const std::size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
No version in the server.
Definition: state.hpp:31
Base class for all visible items.
boost::dynamic_bitset get_status_filter_visibility(const window &window) const
Definition: manager.cpp:552
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
install_outcome outcome
Definition: client.hpp:36
void select_layer(const int layer)
Selects and displays a particular layer.
static std::string colorize_addon_state_string(const std::string &str, ADDON_STATUS state, bool verbose=false)
Changes the color of an add-on state string (installed, outdated, etc.) according to the state itself...
Definition: addon_list.cpp:64
Represents version numbers.
config & add_child(config_key_type key)
Definition: config.cpp:479
std::string make_addon_title(const std::string &id)
Replaces underscores to dress up file or dirnames as add-on titles.
Definition: info.cpp:226
void set_uninstall_function(addon_op_func_t function)
Sets the function to call when the player clicks the uninstall button.
Definition: addon_list.hpp:82
bool open_object_is_supported()
Returns whether open_object() is supported/implemented for the current platform.
Definition: open.cpp:46
void copy_to_clipboard(const std::string &text, const bool)
Copies text to the clipboard.
Definition: clipboard.cpp:35
static const std::vector< std::pair< ADDON_TYPE, std::string > > type_filter_types_
Definition: manager.hpp:90
std::vector< std::string > depends
Definition: info.hpp:52
#define f
addon_tracking_info get_addon_tracking_info(const addon_info &addon)
Get information about an add-on comparing its local state with the add-ons server entry...
Definition: state.cpp:24
Hybrid campaign.
Definition: validation.hpp:45
void order_addons(window &window)
Definition: manager.cpp:608
std::map< std::string, addon_tracking_info > addons_tracking_list
Definition: state.hpp:63
const std::string unicode_em_dash
Definition: constants.cpp:40
std::string title
Definition: info.hpp:32
std::set< std::string > resolve_dependencies(const addons_list &addons) const
Resolve an add-on&#39;s dependency tree in a recursive fashion.
Definition: info.cpp:184
bool upload_addon(const std::string &id, std::string &response_message, config &cfg)
Requests the specified add-on to be uploaded.
Definition: client.cpp:113
Simple push button.
Definition: button.hpp:35
std::string str() const
Serializes the version number into string form.
void publish_selected_addon(window &window)
Definition: manager.hpp:117
void on_filtertext_changed(text_box_base *textbox)
Definition: manager.cpp:242
version_info version
Definition: info.hpp:37
void show_error_message(const std::string &msg, bool message_use_markup)
Shows an error message to the user.
Definition: message.cpp:205
void set_install_function(addon_op_func_t function)
Sets the function to call when the player clicks the install button.
Definition: addon_list.hpp:76
void set_values(const std::vector<::config > &values)
Set the available menu options.
Stores additional status information about add-ons.
Definition: state.hpp:45
Dialog was closed with the OK button.
Definition: retval.hpp:34
int downloads
Definition: info.hpp:42
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
void update_selected_addon(window &window)
Definition: manager.hpp:111
bool delete_remote_addon(const std::string &id, std::string &response_message)
Requests the specified add-on to be removed from the server.
Definition: client.cpp:197
bool request_distribution_terms(std::string &terms)
Request the add-ons server distribution terms message.
Definition: client.cpp:97
void update_addon(const addon_info &addon, window &window)
Definition: manager.cpp:690
std::map< std::string, addon_info > addons_list
Definition: info.hpp:26
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
install_result install_addon_with_checks(const addons_list &addons, const addon_info &addon)
Do a &#39;smart&#39; fetch of an add-on, checking to avoid overwrites for devs and resolving dependencies...
Definition: client.cpp:467
Single-player campaign.
Definition: validation.hpp:43
bool use_twelve_hour_clock_format()
Definition: general.cpp:1044
static const int DEFAULT_ACTION_RETVAL
Special retval for the toggle panels in the addons list.
Definition: addon_list.hpp:46
virtual void pre_show(window &window) override
Inherited from modal_dialog.
Definition: manager.cpp:302
bool request_addons_list(config &cfg)
Request the add-ons list from the server.
Definition: client.cpp:80
void set_modified_signal_handler(const std::function< void()> &callback)
Sets up a callback that will be called when the player selects an add-on.
Definition: addon_list.hpp:52
Multiplayer campaign.
Definition: validation.hpp:46
#define GETTEXT_DOMAIN
Definition: manager.cpp:15
std::string author
Definition: info.hpp:39
static const std::vector< addon_order > all_orders_
Definition: manager.hpp:91
virtual void set_active(const bool active) override
See styled_widget::set_active.
Version in the server matches local installation.
Definition: state.hpp:25
void publish_addon(const addon_info &addon, window &window)
Performs all backend and UI actions for publishing the specified add-on.
Definition: manager.cpp:718