The Battle for Wesnoth  1.15.5+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  for(const config& child : cfg.child_range("translation")) {
121  for(const auto& attribute : child.attribute_range()) {
122  std::string val = attribute.second.str();
123  if(translation::ci_search(val, filter)) {
124  found = true;
125  break;
126  }
127  }
128  }
129  if(!found) {
130  return false;
131  }
132  }
133  return true;
134  }
135  const std::vector<std::string> filtertext_;
136  };
137 
138  std::string make_display_dependencies(
139  const std::string& addon_id,
140  const addons_list& addons_list,
141  const addons_tracking_list& addon_states)
142  {
143  const addon_info& addon = addons_list.at(addon_id);
145 
146  const std::set<std::string>& deps = addon.resolve_dependencies(addons_list);
147 
148  for(const auto& dep_id : deps) {
149  addon_info dep;
150  addon_tracking_info depstate;
151 
152  addons_list::const_iterator ali = addons_list.find(dep_id);
153  addons_tracking_list::const_iterator tli = addon_states.find(dep_id);
154 
155  if(ali == addons_list.end()) {
156  dep.id = dep_id; // Build dummy addon_info.
157  } else {
158  dep = ali->second;
159  }
160 
161  if(tli == addon_states.end()) {
162  depstate = get_addon_tracking_info(dep);
163  } else {
164  depstate = tli->second;
165  }
166 
167  if(!str.empty()) {
168  str += ", ";
169  }
170 
172  }
173 
174  return str;
175  }
176 
177  std::string langcode_to_string(const std::string& lcode)
178  {
179  for(const auto & ld : get_languages())
180  {
181  if(ld.localename == lcode || ld.localename.substr(0, 2) == lcode) {
182  return ld.language;
183  }
184  }
185 
186  return "";
187  }
188 }
189 
190 REGISTER_DIALOG(addon_manager)
191 
192 const std::vector<std::pair<ADDON_STATUS_FILTER, std::string>> addon_manager::status_filter_types_{
193  {FILTER_ALL, N_("addons_view^All Add-ons")},
194  {FILTER_INSTALLED, N_("addons_view^Installed")},
195  {FILTER_UPGRADABLE, N_("addons_view^Upgradable")},
196  {FILTER_PUBLISHABLE, N_("addons_view^Publishable")},
197  {FILTER_NOT_INSTALLED, N_("addons_view^Not Installed")},
198 };
199 
200 const std::vector<std::pair<ADDON_TYPE, std::string>> addon_manager::type_filter_types_{
201  {ADDON_SP_CAMPAIGN, N_("addons_of_type^Campaigns")},
202  {ADDON_SP_SCENARIO, N_("addons_of_type^Scenarios")},
203  {ADDON_SP_MP_CAMPAIGN, N_("addons_of_type^SP/MP campaigns")},
204  {ADDON_MP_CAMPAIGN, N_("addons_of_type^MP campaigns")},
205  {ADDON_MP_SCENARIO, N_("addons_of_type^MP scenarios")},
206  {ADDON_MP_MAPS, N_("addons_of_type^MP map-packs")},
207  {ADDON_MP_ERA, N_("addons_of_type^MP eras")},
208  {ADDON_MP_FACTION, N_("addons_of_type^MP factions")},
209  {ADDON_MOD, N_("addons_of_type^Modifications")},
210  {ADDON_CORE, N_("addons_of_type^Cores")},
211  {ADDON_MEDIA, N_("addons_of_type^Resources")},
212  // FIXME: (also in WML) should this and Unknown be a single option in the UI?
213  {ADDON_OTHER, N_("addons_of_type^Other")},
214  {ADDON_UNKNOWN, N_("addons_of_type^Unknown")},
215 };
216 
217 const std::vector<addon_manager::addon_order> addon_manager::all_orders_{
218  {N_("addons_order^Name ($order)"), 0,
219  [](const addon_info& a, const addon_info& b) { return a.title < b.title; },
220  [](const addon_info& a, const addon_info& b) { return a.title > b.title; }},
221  {N_("addons_order^Author ($order)"), 1,
222  [](const addon_info& a, const addon_info& b) { return a.author < b.author; },
223  [](const addon_info& a, const addon_info& b) { return a.author > b.author; }},
224  {N_("addons_order^Size ($order)"), 2,
225  [](const addon_info& a, const addon_info& b) { return a.size < b.size; },
226  [](const addon_info& a, const addon_info& b) { return a.size > b.size; }},
227  {N_("addons_order^Downloads ($order)"), 3,
228  [](const addon_info& a, const addon_info& b) { return a.downloads < b.downloads; },
229  [](const addon_info& a, const addon_info& b) { return a.downloads > b.downloads; }},
230  {N_("addons_order^Type ($order)"), 4,
231  [](const addon_info& a, const addon_info& b) { return a.display_type() < b.display_type(); },
232  [](const addon_info& a, const addon_info& b) { return a.display_type() > b.display_type(); }},
233  {N_("addons_order^Last updated ($datelike_order)"), -1,
234  [](const addon_info& a, const addon_info& b) { return a.updated < b.updated; },
235  [](const addon_info& a, const addon_info& b) { return a.updated > b.updated; }},
236  {N_("addons_order^First uploaded ($datelike_order)"), -1,
237  [](const addon_info& a, const addon_info& b) { return a.created < b.created; },
238  [](const addon_info& a, const addon_info& b) { return a.created > b.created; }}
239 };
240 
242  : orders_()
243  , cfg_()
244  , client_(client)
245  , addons_()
246  , tracking_info_()
247  , need_wml_cache_refresh_(false)
248 {
249 }
250 
252 {
253  apply_filters(*textbox->get_window());
254 }
255 
257 {
258  std::string s;
259 
260  utils::string_map i18n_symbols {{"local_version", state.installed_version.str()}};
261 
262  switch(state.state) {
263  case ADDON_NONE:
264  s = !state.can_publish
265  ? _("addon_state^Not installed")
266  : _("addon_state^Published, not installed");
267  break;
268  case ADDON_INSTALLED:
269  s = !state.can_publish
270  ? _("addon_state^Installed")
271  : _("addon_state^Published");
272  break;
273  case ADDON_NOT_TRACKED:
274  s = !state.can_publish
275  ? _("addon_state^Installed, not tracking local version")
276  // Published add-ons often don't have local status information,
277  // hence untracked. This should be considered normal.
278  : _("addon_state^Published, not tracking local version");
279  break;
281  const std::string vstr = !state.can_publish
282  ? _("addon_state^Installed ($local_version|), upgradable")
283  : _("addon_state^Published ($local_version| installed), upgradable");
284 
285  s = utils::interpolate_variables_into_string(vstr, &i18n_symbols);
286  } break;
288  const std::string vstr = !state.can_publish
289  ? _("addon_state^Installed ($local_version|), outdated on server")
290  : _("addon_state^Published ($local_version| installed), outdated on server");
291 
292  s = utils::interpolate_variables_into_string(vstr, &i18n_symbols);
293  } break;
295  s = !state.can_publish
296  ? _("addon_state^Installed, not ready to publish")
297  : _("addon_state^Ready to publish");
298  break;
300  s = !state.can_publish
301  ? _("addon_state^Installed, broken")
302  : _("addon_state^Published, broken");
303  break;
304  default:
305  s = _("addon_state^Unknown");
306  }
307 
309 }
310 
312 {
313  window.set_escape_disabled(true);
314 
315  addon_list& list = find_widget<addon_list>(&window, "addons", false);
316 
317  text_box& filter = find_widget<text_box>(&window, "filter", false);
319 
321  this, std::placeholders::_1, std::ref(window)));
323  this, std::placeholders::_1, std::ref(window)));
325  this, std::placeholders::_1, std::ref(window)));
326 
328  this, std::placeholders::_1, std::ref(window)));
330  this, std::placeholders::_1, std::ref(window)));
331 
332  list.set_modified_signal_handler([this, &window]() { on_addon_select(window); });
333 
334  fetch_addons_list(window);
335  load_addon_list(window);
336 
337  menu_button& status_filter = find_widget<menu_button>(&window, "install_status_filter", false);
338 
339  std::vector<config> status_filter_entries;
340  for(const auto& f : status_filter_types_) {
341  status_filter_entries.emplace_back("label", t_string(f.second, GETTEXT_DOMAIN));
342  }
343 
344  status_filter.set_values(status_filter_entries);
345 
346  connect_signal_notify_modified(status_filter,
347  std::bind(&addon_manager::apply_filters, this, std::ref(window)));
348 
349  multimenu_button& type_filter = find_widget<multimenu_button>(&window, "type_filter", false);
350 
351  std::vector<config> type_filter_entries;
352  for(const auto& f : type_filter_types_) {
353  type_filter_entries.emplace_back("label", t_string(f.second, GETTEXT_DOMAIN), "checkbox", false);
354  }
355 
356  type_filter.set_values(type_filter_entries);
357 
358  connect_signal_notify_modified(type_filter,
359  std::bind(&addon_manager::apply_filters, this, std::ref(window)));
360 
361  menu_button& order_dropdown = find_widget<menu_button>(&window, "order_dropdown", false);
362 
363  std::vector<config> order_dropdown_entries;
364  for(const auto& f : all_orders_) {
365  utils::string_map symbols;
366 
367  symbols["order"] = _("ascending");
368  // TRANSLATORS: Sorting order of dates, oldest first
369  symbols["datelike_order"] = _("oldest to newest");
370  config entry{"label", VGETTEXT(f.label.c_str(), symbols)};
371  order_dropdown_entries.push_back(entry);
372  symbols["order"] = _("descending");
373  // TRANSLATORS: Sorting order of dates, newest first
374  symbols["datelike_order"] = _("newest to oldest");
375  entry["label"] = VGETTEXT(f.label.c_str(), symbols);
376  order_dropdown_entries.push_back(entry);
377  }
378 
379  order_dropdown.set_values(order_dropdown_entries);
380 
381  connect_signal_notify_modified(order_dropdown,
382  std::bind(&addon_manager::order_addons, this, std::ref(window)));
383 
384  button& url_go_button = find_widget<button>(&window, "url_go", false);
385  button& url_copy_button = find_widget<button>(&window, "url_copy", false);
386  text_box& url_textbox = find_widget<text_box>(&window, "url", false);
387 
388  url_textbox.set_active(false);
389 
391  url_copy_button.set_active(false);
392  url_copy_button.set_tooltip(_("Clipboard support not found, contact your packager"));
393  }
394 
396  // No point in displaying the button on platforms that can't do
397  // open_object().
399  }
400 
402  find_widget<button>(&window, "install", false),
403  std::bind(&addon_manager::install_selected_addon, this, std::ref(window)));
404 
406  find_widget<button>(&window, "uninstall", false),
407  std::bind(&addon_manager::uninstall_selected_addon, this, std::ref(window)));
408 
410  find_widget<button>(&window, "update", false),
411  std::bind(&addon_manager::update_selected_addon, this, std::ref(window)));
412 
414  find_widget<button>(&window, "publish", false),
415  std::bind(&addon_manager::publish_selected_addon, this, std::ref(window)));
416 
418  find_widget<button>(&window, "delete", false),
419  std::bind(&addon_manager::delete_selected_addon, this, std::ref(window)));
420 
422  find_widget<button>(&window, "update_all", false),
423  std::bind(&addon_manager::update_all_addons, this, std::ref(window)));
424 
426  url_go_button,
427  std::bind(&addon_manager::browse_url_callback, this, std::ref(url_textbox)));
428 
430  url_copy_button,
431  std::bind(&addon_manager::copy_url_callback, this, std::ref(url_textbox)));
432 
434  find_widget<button>(&window, "show_help", false),
435  std::bind(&addon_manager::show_help, this));
436 
437  if(stacked_widget* stk = find_widget<stacked_widget>(&window, "main_stack", false, false)) {
438  button& btn = find_widget<button>(&window, "details_toggle", false);
439  connect_signal_mouse_left_click(btn, std::bind(&addon_manager::toggle_details, this, std::ref(btn), std::ref(*stk)));
440  stk->select_layer(0);
441  }
442 
443  on_addon_select(window);
444 
445  window.set_enter_disabled(true);
446 
447  window.keyboard_capture(&filter);
449 
450  list.set_callback_order_change(std::bind(&addon_manager::on_order_changed, this, std::ref(window),
451  std::placeholders::_1, std::placeholders::_2));
452 
453  // Use handle the special addon_list retval to allow installing addons on double click
454  window.set_exit_hook(std::bind(&addon_manager::exit_hook, this, std::ref(window)));
455 }
456 
458 {
459  if(stk.current_layer() == 0) {
460  btn.set_label(_("addons^Back to List"));
461  stk.select_layer(1);
462  } else {
463  btn.set_label(_("Addon Details"));
464  stk.select_layer(0);
465  }
466 }
467 
469 {
471  if(!cfg_) {
472  gui2::show_error_message(_("An error occurred while downloading the add-ons list from the server."));
473  window.close();
474  }
475 }
476 
478 {
481  }
482 
484 
485  std::vector<std::string> publishable_addons = available_addons();
486 
487  for(std::string id : publishable_addons) {
488  if(addons_.find(id) == addons_.end()) {
489  // Get a config from the addon's pbl file
490  // Note that the name= key is necessary or stuff breaks, since the filter code uses this key
491  // to match add-ons in the config list. It also fills in addon_info's id field. It's also
492  // neccessay to set local_only here so that flag can be properly set after addons_ is cleared
493  // and recreated by read_addons_list.
494  config pbl_cfg = get_addon_pbl_info(id);
495  pbl_cfg["name"] = id;
496  pbl_cfg["local_only"] = true;
497 
498  // Add the add-on to the list.
499  addon_info addon(pbl_cfg);
500  addons_[id] = addon;
501 
502  // Add the addon to the config entry
503  cfg_.add_child("campaign", std::move(pbl_cfg));
504  }
505  }
506 
507  if(addons_.empty()) {
508  show_transient_message(_("No Add-ons Available"), _("There are no add-ons available for download from this server."));
509  }
510 
511  addon_list& list = find_widget<addon_list>(&window, "addons", false);
512  list.set_addons(addons_);
513 
514  bool has_upgradable_addons = false;
515  for(const auto& a : addons_) {
516  tracking_info_[a.first] = get_addon_tracking_info(a.second);
517 
518  if(tracking_info_[a.first].state == ADDON_INSTALLED_UPGRADABLE) {
519  has_upgradable_addons = true;
520  }
521  }
522 
523  find_widget<button>(&window, "update_all", false).set_active(has_upgradable_addons);
524 
525  apply_filters(window);
526 }
527 
529 {
530  load_addon_list(window);
531 
532  // Reselect the add-on.
533  find_widget<addon_list>(&window, "addons", false).select_addon(id);
534  on_addon_select(window);
535 }
536 
537 boost::dynamic_bitset<> addon_manager::get_name_filter_visibility(const window& window) const
538 {
539  const text_box& name_filter = find_widget<const text_box>(&window, "filter", false);
540  const std::string& text = name_filter.get_value();
541 
542  filter_transform filter(utils::split(text, ' '));
543  boost::dynamic_bitset<> res;
544 
545  const config::const_child_itors& addon_cfgs = cfg_.child_range("campaign");
546 
547  for(const auto& a : addons_)
548  {
549  const config& addon_cfg = *std::find_if(addon_cfgs.begin(), addon_cfgs.end(),
550  [&a](const config& cfg)
551  {
552  return cfg["name"] == a.first;
553  });
554 
555  res.push_back(filter(addon_cfg));
556  }
557 
558  return res;
559 }
560 
561 boost::dynamic_bitset<> addon_manager::get_status_filter_visibility(const window& window) const
562 {
563  const menu_button& status_filter = find_widget<const menu_button>(&window, "install_status_filter", false);
564  const ADDON_STATUS_FILTER selection = status_filter_types_[status_filter.get_value()].first;
565 
566  boost::dynamic_bitset<> res;
567  for(const auto& a : addons_) {
568  const addon_tracking_info& info = tracking_info_.at(a.second.id);
569 
570  res.push_back(
571  (selection == FILTER_ALL) ||
572  (selection == FILTER_INSTALLED && is_installed_addon_status(info.state)) ||
573  (selection == FILTER_UPGRADABLE && info.state == ADDON_INSTALLED_UPGRADABLE) ||
574  (selection == FILTER_PUBLISHABLE && info.can_publish == true) ||
575  (selection == FILTER_NOT_INSTALLED && info.state == ADDON_NONE)
576  );
577  }
578 
579  return res;
580 }
581 
582 boost::dynamic_bitset<> addon_manager::get_type_filter_visibility(const window& window) const
583 {
584  const multimenu_button& type_filter = find_widget<const multimenu_button>(&window, "type_filter", false);
585 
586  boost::dynamic_bitset<> toggle_states = type_filter.get_toggle_states();
587  if(toggle_states.none()) {
588  // Nothing selected. It means that *all* add-ons are shown.
589  boost::dynamic_bitset<> res_flipped(addons_.size());
590  return ~res_flipped;
591  } else {
592  boost::dynamic_bitset<> res;
593 
594  for(const auto& a : addons_) {
595  int index = std::distance(type_filter_types_.begin(),
596  std::find_if(type_filter_types_.begin(), type_filter_types_.end(),
597  [&a](const std::pair<ADDON_TYPE, std::string>& entry) {
598  return entry.first == a.second.type;
599  })
600  );
601  res.push_back(toggle_states[index]);
602  }
603 
604  return res;
605  }
606 }
607 
609 {
610  boost::dynamic_bitset<> res =
613  & get_name_filter_visibility(window);
614  find_widget<addon_list>(&window, "addons", false).set_addon_shown(res);
615 }
616 
618 {
619  const menu_button& order_menu = find_widget<const menu_button>(&window, "order_dropdown", false);
620  const addon_order& order_struct = all_orders_.at(order_menu.get_value() / 2);
623  if(order == listbox::SORT_ASCENDING) {
624  func = order_struct.sort_func_asc;
625  } else {
626  func = order_struct.sort_func_desc;
627  }
628 
629  find_widget<addon_list>(&window, "addons", false).set_addon_order(func);
630 }
631 
632 void addon_manager::on_order_changed(window& window, unsigned int sort_column, listbox::SORT_ORDER order)
633 {
634  menu_button& order_menu = find_widget<menu_button>(&window, "order_dropdown", false);
635  auto order_it = std::find_if(all_orders_.begin(), all_orders_.end(),
636  [sort_column](const addon_order& order) {return order.column_index == static_cast<int>(sort_column);});
637  int index = 2 * (std::distance(all_orders_.begin(), order_it));
638  if(order == listbox::SORT_DESCENDING) {
639  ++index;
640  }
641  order_menu.set_value(index);
642 }
643 
644 template<void(addon_manager::*fptr)(const addon_info& addon, window& window)>
646 {
647  // Explicitly return to the main page if we're in low-res mode so the list is visible.
648  if(stacked_widget* stk = find_widget<stacked_widget>(&window, "main_stack", false, false)) {
649  stk->select_layer(0);
650  find_widget<button>(&window, "details_toggle", false).set_label(_("Addon Details"));
651  }
652 
653  addon_list& addons = find_widget<addon_list>(&window, "addons", false);
654  const addon_info* addon = addons.get_selected_addon();
655 
656  if(addon == nullptr) {
657  return;
658  }
659 
660  try {
661  (this->*fptr)(*addon, window);
662  } catch(const addons_client::user_exit&) {
663  // User canceled the op.
664  }
665 }
666 
668 {
670 
671  // Take note if any wml_changes occurred
673 
675  reload_list_and_reselect_item(addon.id, window);
676  }
677 }
678 
680 {
681  if(have_addon_pbl_info(addon.id) || have_addon_in_vcs_tree(addon.id)) {
683  _("The following add-on appears to have publishing or version control information stored locally, and will not be removed:") + " " +
684  addon.display_title_full());
685  return;
686  }
687 
688  bool success = remove_local_addon(addon.id);
689 
690  if(!success) {
691  gui2::show_error_message(_("The following add-on could not be deleted properly:") + " " + addon.display_title_full());
692  } else {
694 
695  reload_list_and_reselect_item(addon.id, window);
696  }
697 }
698 
700 {
701  /* Currently, the install and update codepaths are the same, so this function simply
702  * calls the other. Since this might change in the future, I'm leaving this function
703  * here for now.
704  *
705  * - vultraz, 2017-03-12
706  */
707  install_addon(addon, window);
708 }
709 
711 {
712  for(const auto& a : addons_) {
713  if(tracking_info_[a.first].state == ADDON_INSTALLED_UPGRADABLE) {
715 
716  if(result.wml_changed) {
717  // Updating an add-on may have resulted in its dependencies being updated
718  // as well, so we need to reread version info blocks afterwards to make sure
719  // we don't try to re-download newly updated add-ons.
721  }
722 
723  // Take note if any wml_changes occurred
725  }
726  }
727 
729  load_addon_list(window);
730  }
731 }
732 
733 /** Performs all backend and UI actions for publishing the specified add-on. */
735 {
736  std::string server_msg;
737 
738  const std::string addon_id = addon.id;
739  config cfg = get_addon_pbl_info(addon_id);
740 
741  const version_info& version_to_publish = cfg["version"].str();
742 
743  if(version_to_publish <= tracking_info_[addon_id].remote_version) {
744  const int res = gui2::show_message(_("Warning"),
745  _("The remote version of this add-on is greater or equal to the version being uploaded. Do you really wish to continue?"),
747 
748  if(res != gui2::retval::OK) {
749  return;
750  }
751  }
752 
753  if(!::image::exists(cfg["icon"].str())) {
754  gui2::show_error_message(_("Invalid icon path. Make sure the path points to a valid image."));
755  } else if(!client_.request_distribution_terms(server_msg)) {
757  _("The server responded with an error:") + "\n" + client_.get_last_server_error());
758  } else if(gui2::show_message(_("Terms"), server_msg, gui2::dialogs::message::ok_cancel_buttons, true) == gui2::retval::OK) {
759  if(!client_.upload_addon(addon_id, server_msg, cfg)) {
760  const std::string& msg = _("The server responded with an error:") +
761  "\n\n" + client_.get_last_server_error();
762  const std::string& extra_data = client_.get_last_server_error_data();
763  if (!extra_data.empty()) {
764  // TODO: Allow user to copy the extra data portion to clipboard
765  // or something, maybe display it in a dialog similar to
766  // the WML load errors report in a monospace font and
767  // stuff (having a scroll container is especially
768  // important since a long list can cause the dialog to
769  // overflow).
770  gui2::show_error_message(msg + "\n\n" + extra_data, true);
771  } else {
772  gui2::show_error_message(msg, true);
773  }
774  } else {
775  gui2::show_transient_message(_("Response"), server_msg);
776  fetch_addons_list(window);
777  reload_list_and_reselect_item(addon_id, window);
778  }
779  }
780 }
781 
782 /** Performs all backend and UI actions for taking down the specified add-on. */
784 {
785  const std::string addon_id = addon.id;
786  const std::string& text = VGETTEXT(
787  "Deleting '$addon|' will permanently erase its download and upload counts on the add-ons server. Do you really wish to continue?",
788  {{"addon", make_addon_title(addon_id)}} // FIXME: need the real title!
789  );
790 
791  const int res = gui2::show_message(_("Confirm"), text, gui2::dialogs::message::yes_no_buttons);
792 
793  if(res != gui2::retval::OK) {
794  return;
795  }
796 
797  std::string server_msg;
798  if(!client_.delete_remote_addon(addon_id, server_msg)) {
799  gui2::show_error_message(_("The server responded with an error:") + "\n" + client_.get_last_server_error());
800  } else {
801  // FIXME: translation needed!
802  gui2::show_transient_message(_("Response"), server_msg);
803  fetch_addons_list(window);
804  reload_list_and_reselect_item(addon_id, window);
805  }
806 }
807 
808 /** Called when the player double-clicks an add-on. */
810 {
811  switch(tracking_info_[addon.id].state) {
812  case ADDON_NONE:
813  install_addon(addon, window);
814  break;
815  case ADDON_INSTALLED:
816  if(!tracking_info_[addon.id].can_publish) {
817  utils::string_map symbols{ { "addon", addon.display_title_full() } };
818  int res = gui2::show_message(_("Uninstall add-on"),
819  VGETTEXT("Do you want to uninstall '$addon|'?", symbols),
821  if(res == gui2::retval::OK) {
822  uninstall_addon(addon, window);
823  }
824  }
825  break;
827  update_addon(addon, window);
828  break;
831  publish_addon(addon, window);
832  break;
833  default:
834  break;
835  }
836 }
837 
839 {
840  help::show_help("installing_addons");
841 }
842 
844 {
845  /* TODO: ask for confirmation */
846  desktop::open_object(url_box.get_value());
847 }
848 
850 {
852 }
853 
854 static std::string format_addon_time(std::time_t time)
855 {
856  if(time) {
857  std::ostringstream ss;
858 
860  ? "%Y-%m-%d %I:%M %p"
861  : "%Y-%m-%d %H:%M";
862 
863  ss << std::put_time(std::localtime(&time), format);
864 
865  return ss.str();
866  }
867 
868  return font::unicode_em_dash;
869 }
870 
872 {
873  const addon_info* info = find_widget<addon_list>(&window, "addons", false).get_selected_addon();
874 
875  if(info == nullptr) {
876  return;
877  }
878 
879  widget* parent = &window;
880  if(stacked_widget* stk = find_widget<stacked_widget>(&window, "main_stack", false, false)) {
881  parent = stk->get_layer_grid(1);
882  }
883 
884  find_widget<drawing>(parent, "image", false).set_label(info->display_icon());
885 
886  find_widget<styled_widget>(parent, "title", false).set_label(info->display_title_translated_or_original());
887  find_widget<styled_widget>(parent, "description", false).set_label(info->description_translated());
888  find_widget<styled_widget>(parent, "version", false).set_label(info->version.str());
889  find_widget<styled_widget>(parent, "author", false).set_label(info->author);
890  find_widget<styled_widget>(parent, "type", false).set_label(info->display_type());
891 
892  styled_widget& status = find_widget<styled_widget>(parent, "status", false);
894  status.set_use_markup(true);
895 
896  find_widget<styled_widget>(parent, "size", false).set_label(size_display_string(info->size));
897  find_widget<styled_widget>(parent, "downloads", false).set_label(std::to_string(info->downloads));
898  find_widget<styled_widget>(parent, "created", false).set_label(format_addon_time(info->created));
899  find_widget<styled_widget>(parent, "updated", false).set_label(format_addon_time(info->updated));
900 
901  find_widget<styled_widget>(parent, "dependencies", false).set_label(!info->depends.empty()
902  ? make_display_dependencies(info->id, addons_, tracking_info_)
903  : _("addon_dependencies^None"));
904 
905  std::string languages;
906 
907  for(const auto& lc : info->locales) {
908  const std::string& langlabel = langcode_to_string(lc);
909  if(!langlabel.empty()) {
910  if(!languages.empty()) {
911  languages += ", ";
912  }
913  languages += langlabel;
914  }
915  }
916 
917  find_widget<styled_widget>(parent, "translations", false).set_label(!languages.empty() ? languages : _("translations^None"));
918 
919  const std::string& feedback_url = info->feedback_url;
920 
921  if(!feedback_url.empty()) {
922  find_widget<stacked_widget>(parent, "feedback_stack", false).select_layer(1);
923  find_widget<text_box>(parent, "url", false).set_value(feedback_url);
924  } else {
925  find_widget<stacked_widget>(parent, "feedback_stack", false).select_layer(0);
926  }
927 
928  bool installed = is_installed_addon_status(tracking_info_[info->id].state);
929  bool updatable = tracking_info_[info->id].state == ADDON_INSTALLED_UPGRADABLE;
930 
931  stacked_widget& action_stack = find_widget<stacked_widget>(parent, "action_stack", false);
932 
933  if(!tracking_info_[info->id].can_publish) {
934  action_stack.select_layer(0);
935 
936  stacked_widget& install_update_stack = find_widget<stacked_widget>(parent, "install_update_stack", false);
937  install_update_stack.select_layer(updatable ? 1 : 0);
938 
939  if(!updatable) {
940  find_widget<button>(parent, "install", false).set_active(!installed);
941  } else {
942  find_widget<button>(parent, "update", false).set_active(true);
943  }
944 
945  find_widget<button>(parent, "uninstall", false).set_active(installed);
946  } else {
947  action_stack.select_layer(1);
948 
949  // Always enable the publish button, but disable the delete button if not yet published.
950  find_widget<button>(parent, "publish", false).set_active(true);
951  find_widget<button>(parent, "delete", false).set_active(!info->local_only);
952  }
953 }
954 
956 {
959  return false;
960  }
961 
962  return true;
963 }
964 
965 } // namespace dialogs
966 } // namespace gui2
int size
Definition: info.hpp:93
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:285
void close()
Requests to close the window.
Definition: window.hpp:182
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:114
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:955
void load_addon_list(window &window)
Definition: manager.cpp:477
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:854
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_translated_or_original() const
Definition: info.cpp:198
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:371
#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:362
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:49
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:299
void update_all_addons(window &window)
Definition: manager.cpp:710
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
std::string str
Definition: statement.cpp:110
Simple push button.
std::string feedback_url
Definition: info.hpp:107
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:66
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.
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:809
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:132
void execute_action_on_selected_addon(window &window)
Definition: manager.cpp:645
#define b
const std::vector< std::string > filtertext_
Definition: manager.cpp:135
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:1028
boost::dynamic_bitset get_type_filter_visibility(const window &window) const
Definition: manager.cpp:582
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:102
static UNUSEDNOWARN std::string _(const char *str)
Definition: gettext.hpp:100
void on_addon_select(window &window)
Definition: manager.cpp:871
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:248
void reload_list_and_reselect_item(const std::string id, window &window)
Definition: manager.cpp:528
void uninstall_addon(const addon_info &addon, window &window)
Definition: manager.cpp:679
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:475
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:233
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:137
std::string display_icon() const
Get an icon path fixed for display (e.g.
Definition: info.cpp:223
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:83
Version in the server is newer than local installation.
Definition: state.hpp:27
void toggle_details(button &btn, stacked_widget &stk)
Definition: manager.cpp:457
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:240
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:306
void set_addons(const addons_list &addons)
Sets the add-ons to show.
Definition: addon_list.cpp:157
bool remove_local_addon(const std::string &addon)
Definition: manager.cpp:118
Dependencies not satisfied.
Definition: state.hpp:34
void copy_url_callback(text_box &url_box)
Definition: manager.cpp:849
Add-ons (campaignd) client class.
Definition: client.hpp:29
Various uncategorised dialogs.
int get_retval()
Definition: window.hpp:370
config get_addon_pbl_info(const std::string &addon_name)
Gets the publish information for an add-on.
Definition: manager.cpp:80
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:783
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:210
static std::string describe_status_verbose(const addon_tracking_info &state)
Definition: manager.cpp:256
bool ci_search(const std::string &s1, const std::string &s2)
Definition: gettext.cpp:518
bool local_only
Definition: info.hpp:114
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:110
std::vector< std::string > locales
Definition: info.hpp:100
Multiplayer scenario.
Definition: validation.hpp:47
void on_order_changed(window &window, unsigned int sort_column, listbox::SORT_ORDER order)
Definition: manager.cpp:632
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:537
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:75
void install_addon(const addon_info &addon, window &window)
Definition: manager.cpp:667
void browse_url_callback(text_box &url_box)
Definition: manager.cpp:843
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:116
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:809
std::string size_display_string(double size)
Get a human-readable representation of the specified byte count.
Definition: info.cpp:302
addon_manager(addons_client &client)
Definition: manager.cpp:241
void set_publish_function(addon_op_func_t function)
Sets the function to upload an addon to the addons server.
Definition: addon_list.hpp:96
std::time_t updated
Definition: info.hpp:109
void fetch_addons_list(window &window)
Definition: manager.cpp:468
language_list get_languages()
Definition: language.cpp:116
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:90
void delete_selected_addon(window &window)
Definition: manager.hpp:123
std::string display_title_full() const
Definition: info.cpp:215
Multiplayer plain (no WML) map pack.
Definition: validation.hpp:48
void apply_filters(window &window)
Definition: manager.cpp:608
#define N_(String)
Definition: gettext.hpp:108
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:561
#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:476
std::string make_addon_title(const std::string &id)
Replaces underscores to dress up file or dirnames as add-on titles.
Definition: info.cpp:311
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:84
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:104
#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
std::vector< std::string > split(const config_attribute_value &val)
void order_addons(window &window)
Definition: manager.cpp:617
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:84
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:272
bool upload_addon(const std::string &id, std::string &response_message, config &cfg)
Requests the specified add-on to be uploaded.
Definition: client.cpp:108
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:251
version_info version
Definition: info.hpp:89
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:78
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:94
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
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:192
bool request_distribution_terms(std::string &terms)
Request the add-ons server distribution terms message.
Definition: client.cpp:92
void update_addon(const addon_info &addon, window &window)
Definition: manager.cpp:699
std::map< std::string, addon_info > addons_list
Definition: info.hpp:27
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
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:460
Single-player campaign.
Definition: validation.hpp:43
bool use_twelve_hour_clock_format()
Definition: general.cpp:915
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:311
bool request_addons_list(config &cfg)
Request the add-ons list from the server.
Definition: client.cpp:75
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:54
Multiplayer campaign.
Definition: validation.hpp:46
#define GETTEXT_DOMAIN
Definition: manager.cpp:15
std::string author
Definition: info.hpp:91
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:734
auto * ss
Definition: result_set.cpp:281
std::string description_translated() const
Definition: info.cpp:204