The Battle for Wesnoth  1.15.11+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"
32 #include "gui/dialogs/message.hpp"
34 #include "gui/widgets/button.hpp"
35 #include "gui/widgets/label.hpp"
39 #include "gui/widgets/drawing.hpp"
40 #include "gui/widgets/image.hpp"
41 #include "gui/widgets/listbox.hpp"
42 #include "gui/widgets/pane.hpp"
43 #include "gui/widgets/settings.hpp"
45 #include "gui/widgets/text_box.hpp"
46 #include "gui/widgets/window.hpp"
48 #include "formula/string_utils.hpp"
49 #include "picture.hpp"
50 #include "language.hpp"
51 #include "preferences/general.hpp"
52 #include "utils/general.hpp"
53 
54 #include "config.hpp"
55 
56 #include <functional>
57 
58 #include <iomanip>
59 #include <sstream>
60 #include <stdexcept>
61 
62 namespace gui2::dialogs
63 {
64 
65 namespace {
66  struct filter_transform
67  {
68  explicit filter_transform(const std::vector<std::string>& filtertext) : filtertext_(filtertext) {}
69  bool operator()(const config& cfg) const
70  {
71  for(const auto& filter : filtertext_)
72  {
73  bool found = false;
74  for(const auto& attribute : cfg.attribute_range())
75  {
76  std::string val = attribute.second.str();
77  if(std::search(val.begin(),
78  val.end(),
79  filter.begin(),
80  filter.end(),
82  != val.end())
83  {
84  found = true;
85  break;
86  }
87  }
88  for(const config& child : cfg.child_range("translation")) {
89  for(const auto& attribute : child.attribute_range()) {
90  std::string val = attribute.second.str();
91  if(translation::ci_search(val, filter)) {
92  found = true;
93  break;
94  }
95  }
96  }
97  if(!found) {
98  return false;
99  }
100  }
101  return true;
102  }
103  const std::vector<std::string> filtertext_;
104  };
105 
106  std::string make_display_dependencies(
107  const std::string& addon_id,
108  const addons_list& addons_list,
109  const addons_tracking_list& addon_states)
110  {
111  const addon_info& addon = addons_list.at(addon_id);
112  std::string str;
113 
114  const std::set<std::string>& deps = addon.resolve_dependencies(addons_list);
115 
116  for(const auto& dep_id : deps) {
117  addon_info dep;
118  addon_tracking_info depstate;
119 
120  addons_list::const_iterator ali = addons_list.find(dep_id);
121  addons_tracking_list::const_iterator tli = addon_states.find(dep_id);
122 
123  if(ali == addons_list.end()) {
124  dep.id = dep_id; // Build dummy addon_info.
125  } else {
126  dep = ali->second;
127  }
128 
129  if(tli == addon_states.end()) {
130  depstate = get_addon_tracking_info(dep);
131  } else {
132  depstate = tli->second;
133  }
134 
135  if(!str.empty()) {
136  str += ", ";
137  }
138 
140  }
141 
142  return str;
143  }
144 
145  std::string langcode_to_string(const std::string& lcode)
146  {
147  for(const auto & ld : get_languages())
148  {
149  if(ld.localename == lcode || ld.localename.substr(0, 2) == lcode) {
150  return ld.language;
151  }
152  }
153 
154  return "";
155  }
156 }
157 
158 REGISTER_DIALOG(addon_manager)
159 
160 const std::vector<std::pair<ADDON_STATUS_FILTER, std::string>> addon_manager::status_filter_types_{
161  {FILTER_ALL, N_("addons_view^All Add-ons")},
162  {FILTER_INSTALLED, N_("addons_view^Installed")},
163  {FILTER_UPGRADABLE, N_("addons_view^Upgradable")},
164  {FILTER_PUBLISHABLE, N_("addons_view^Publishable")},
165  {FILTER_NOT_INSTALLED, N_("addons_view^Not Installed")},
166 };
167 
168 const std::vector<std::pair<ADDON_TYPE, std::string>> addon_manager::type_filter_types_{
169  {ADDON_SP_CAMPAIGN, N_("addons_of_type^Campaigns")},
170  {ADDON_SP_SCENARIO, N_("addons_of_type^Scenarios")},
171  {ADDON_SP_MP_CAMPAIGN, N_("addons_of_type^SP/MP campaigns")},
172  {ADDON_MP_CAMPAIGN, N_("addons_of_type^MP campaigns")},
173  {ADDON_MP_SCENARIO, N_("addons_of_type^MP scenarios")},
174  {ADDON_MP_MAPS, N_("addons_of_type^MP map-packs")},
175  {ADDON_MP_ERA, N_("addons_of_type^MP eras")},
176  {ADDON_MP_FACTION, N_("addons_of_type^MP factions")},
177  {ADDON_MOD, N_("addons_of_type^Modifications")},
178  {ADDON_CORE, N_("addons_of_type^Cores")},
179  {ADDON_MEDIA, N_("addons_of_type^Resources")},
180  // FIXME: (also in WML) should this and Unknown be a single option in the UI?
181  {ADDON_OTHER, N_("addons_of_type^Other")},
182  {ADDON_UNKNOWN, N_("addons_of_type^Unknown")},
183 };
184 
185 const std::vector<addon_manager::addon_order> addon_manager::all_orders_{
186  {N_("addons_order^Name ($order)"), "name", 0,
187  [](const addon_info& a, const addon_info& b) { return a.title < b.title; },
188  [](const addon_info& a, const addon_info& b) { return a.title > b.title; }},
189  {N_("addons_order^Author ($order)"), "author", 1,
190  [](const addon_info& a, const addon_info& b) { return a.author < b.author; },
191  [](const addon_info& a, const addon_info& b) { return a.author > b.author; }},
192  {N_("addons_order^Size ($order)"), "size", 2,
193  [](const addon_info& a, const addon_info& b) { return a.size < b.size; },
194  [](const addon_info& a, const addon_info& b) { return a.size > b.size; }},
195  {N_("addons_order^Downloads ($order)"), "downloads", 3,
196  [](const addon_info& a, const addon_info& b) { return a.downloads < b.downloads; },
197  [](const addon_info& a, const addon_info& b) { return a.downloads > b.downloads; }},
198  {N_("addons_order^Type ($order)"), "type", 4,
199  [](const addon_info& a, const addon_info& b) { return a.display_type() < b.display_type(); },
200  [](const addon_info& a, const addon_info& b) { return a.display_type() > b.display_type(); }},
201  {N_("addons_order^Last updated ($datelike_order)"), "last_updated", -1,
202  [](const addon_info& a, const addon_info& b) { return a.updated < b.updated; },
203  [](const addon_info& a, const addon_info& b) { return a.updated > b.updated; }},
204  {N_("addons_order^First uploaded ($datelike_order)"), "first_uploaded", -1,
205  [](const addon_info& a, const addon_info& b) { return a.created < b.created; },
206  [](const addon_info& a, const addon_info& b) { return a.created > b.created; }}
207 };
208 
210  : orders_()
211  , cfg_()
212  , client_(client)
213  , addons_()
214  , tracking_info_()
215  , need_wml_cache_refresh_(false)
216 {
217 }
218 
219 static std::string describe_status_verbose(const addon_tracking_info& state)
220 {
221  std::string s;
222 
223  utils::string_map i18n_symbols {{"local_version", state.installed_version.str()}};
224 
225  switch(state.state) {
226  case ADDON_NONE:
227  s = !state.can_publish
228  ? _("addon_state^Not installed")
229  : _("addon_state^Published, not installed");
230  break;
231  case ADDON_INSTALLED:
232  s = !state.can_publish
233  ? _("addon_state^Installed")
234  : _("addon_state^Published");
235  break;
236  case ADDON_NOT_TRACKED:
237  s = !state.can_publish
238  ? _("addon_state^Installed, not tracking local version")
239  // Published add-ons often don't have local status information,
240  // hence untracked. This should be considered normal.
241  : _("addon_state^Published, not tracking local version");
242  break;
244  const std::string vstr = !state.can_publish
245  ? _("addon_state^Installed ($local_version|), upgradable")
246  : _("addon_state^Published ($local_version| installed), upgradable");
247 
248  s = utils::interpolate_variables_into_string(vstr, &i18n_symbols);
249  } break;
251  const std::string vstr = !state.can_publish
252  ? _("addon_state^Installed ($local_version|), outdated on server")
253  : _("addon_state^Published ($local_version| installed), outdated on server");
254 
255  s = utils::interpolate_variables_into_string(vstr, &i18n_symbols);
256  } break;
258  s = !state.can_publish
259  ? _("addon_state^Installed, not ready to publish")
260  : _("addon_state^Ready to publish");
261  break;
263  s = !state.can_publish
264  ? _("addon_state^Installed, broken")
265  : _("addon_state^Published, broken");
266  break;
267  default:
268  s = _("addon_state^Unknown");
269  }
270 
272 }
273 
275 {
276  window.set_escape_disabled(true);
277 
278  addon_list& list = find_widget<addon_list>(&window, "addons", false);
279 
280  text_box& filter = find_widget<text_box>(&window, "filter", false);
282 
284  this, std::placeholders::_1));
286  this, std::placeholders::_1));
288  this, std::placeholders::_1));
289 
291  this, std::placeholders::_1));
293  this, std::placeholders::_1));
294 
295  list.set_modified_signal_handler([this]() { on_addon_select(); });
296 
298  load_addon_list();
299 
300  menu_button& status_filter = find_widget<menu_button>(&window, "install_status_filter", false);
301 
302  std::vector<config> status_filter_entries;
303  for(const auto& f : status_filter_types_) {
304  status_filter_entries.emplace_back("label", t_string(f.second, GETTEXT_DOMAIN));
305  }
306 
307  status_filter.set_values(status_filter_entries);
308 
309  connect_signal_notify_modified(status_filter,
310  std::bind(&addon_manager::apply_filters, this));
311 
312  multimenu_button& type_filter = find_widget<multimenu_button>(&window, "type_filter", false);
313 
314  std::vector<config> type_filter_entries;
315  for(const auto& f : type_filter_types_) {
316  type_filter_entries.emplace_back("label", t_string(f.second, GETTEXT_DOMAIN), "checkbox", false);
317  }
318 
319  type_filter.set_values(type_filter_entries);
320 
321  connect_signal_notify_modified(type_filter,
322  std::bind(&addon_manager::apply_filters, this));
323 
324  menu_button& order_dropdown = find_widget<menu_button>(&window, "order_dropdown", false);
325 
326  std::vector<config> order_dropdown_entries;
327  for(const auto& f : all_orders_) {
328  utils::string_map symbols;
329 
330  symbols["order"] = _("ascending");
331  // TRANSLATORS: Sorting order of dates, oldest first
332  symbols["datelike_order"] = _("oldest to newest");
333  config entry{"label", VGETTEXT(f.label.c_str(), symbols)};
334  order_dropdown_entries.push_back(entry);
335  symbols["order"] = _("descending");
336  // TRANSLATORS: Sorting order of dates, newest first
337  symbols["datelike_order"] = _("newest to oldest");
338  entry["label"] = VGETTEXT(f.label.c_str(), symbols);
339  order_dropdown_entries.push_back(entry);
340  }
341 
342  order_dropdown.set_values(order_dropdown_entries);
343  {
344  const std::string saved_order_name = preferences::addon_manager_saved_order_name();
345  const preferences::SORT_ORDER saved_order_direction =
347 
348  if(!saved_order_name.empty()) {
349  auto order_it = std::find_if(all_orders_.begin(), all_orders_.end(),
350  [&saved_order_name](const addon_order& order) {return order.as_preference == saved_order_name;});
351  if(order_it != all_orders_.end()) {
352  int index = 2 * (std::distance(all_orders_.begin(), order_it));
354  if(saved_order_direction == preferences::SORT_ORDER::ASCENDING) {
355  func = order_it->sort_func_asc;
356  } else {
357  func = order_it->sort_func_desc;
358  ++index;
359  }
360  find_widget<menu_button>(&window, "order_dropdown", false).set_value(index, false);
361  auto& addons = find_widget<addon_list>(&window, "addons", false);
362  addons.set_addon_order(func);
363  addons.select_first_addon();
364  }
365  }
366  }
367 
368  connect_signal_notify_modified(order_dropdown,
369  std::bind(&addon_manager::order_addons, this));
370 
371  label& url_label = find_widget<label>(&window, "url", false);
372 
373  url_label.set_use_markup(true);
374  url_label.set_link_aware(true);
375 
377  find_widget<button>(&window, "install", false),
378  std::bind(&addon_manager::install_selected_addon, this));
379 
381  find_widget<button>(&window, "uninstall", false),
382  std::bind(&addon_manager::uninstall_selected_addon, this));
383 
385  find_widget<button>(&window, "update", false),
386  std::bind(&addon_manager::update_selected_addon, this));
387 
389  find_widget<button>(&window, "publish", false),
390  std::bind(&addon_manager::publish_selected_addon, this));
391 
393  find_widget<button>(&window, "delete", false),
394  std::bind(&addon_manager::delete_selected_addon, this));
395 
397  find_widget<button>(&window, "update_all", false),
398  std::bind(&addon_manager::update_all_addons, this));
399 
401  find_widget<button>(&window, "show_help", false),
402  std::bind(&addon_manager::show_help, this));
403 
404  if(stacked_widget* stk = find_widget<stacked_widget>(&window, "main_stack", false, false)) {
405  button& btn = find_widget<button>(&window, "details_toggle", false);
406  connect_signal_mouse_left_click(btn, std::bind(&addon_manager::toggle_details, this, std::ref(btn), std::ref(*stk)));
407  stk->select_layer(0);
408  }
409 
410  widget* version_filter_parent = &window;
411  if(stacked_widget* stk = find_widget<stacked_widget>(&window, "main_stack", false, false)) {
412  version_filter_parent = stk->get_layer_grid(1);
413  }
414 
415  menu_button& version_filter = find_widget<menu_button>(version_filter_parent, "version_filter", false);
416  connect_signal_notify_modified(version_filter,
418 
419  on_addon_select();
420 
421  window.set_enter_disabled(true);
422 
423  window.keyboard_capture(&filter);
425 
427  std::placeholders::_1, std::placeholders::_2));
428 
429  // Use handle the special addon_list retval to allow installing addons on double click
430  window.set_exit_hook(std::bind(&addon_manager::exit_hook, this, std::placeholders::_1));
431 }
432 
434 {
435  if(stk.current_layer() == 0) {
436  btn.set_label(_("addons^Back to List"));
437  stk.select_layer(1);
438  } else {
439  btn.set_label(_("Addon Details"));
440  stk.select_layer(0);
441  }
442 }
443 
445 {
447  if(!cfg_) {
448  gui2::show_error_message(_("An error occurred while downloading the add-ons list from the server."));
449  get_window()->close();
450  }
451 }
452 
454 {
457  }
458 
460 
461  std::vector<std::string> publishable_addons = available_addons();
462 
463  for(std::string id : publishable_addons) {
464  if(addons_.find(id) == addons_.end()) {
465  // Get a config from the addon's pbl file
466  // Note that the name= key is necessary or stuff breaks, since the filter code uses this key
467  // to match add-ons in the config list. It also fills in addon_info's id field. It's also
468  // neccessay to set local_only here so that flag can be properly set after addons_ is cleared
469  // and recreated by read_addons_list.
470  config pbl_cfg = get_addon_pbl_info(id);
471  pbl_cfg["name"] = id;
472  pbl_cfg["local_only"] = true;
473 
474  // Add the add-on to the list.
475  addon_info addon(pbl_cfg);
476  addons_[id] = addon;
477 
478  // Add the addon to the config entry
479  cfg_.add_child("campaign", std::move(pbl_cfg));
480  }
481  }
482 
483  if(addons_.empty()) {
484  show_transient_message(_("No Add-ons Available"), _("There are no add-ons available for download from this server."));
485  }
486 
487  addon_list& list = find_widget<addon_list>(get_window(), "addons", false);
488  list.set_addons(addons_);
489 
490  bool has_upgradable_addons = false;
491  for(const auto& a : addons_) {
492  tracking_info_[a.first] = get_addon_tracking_info(a.second);
493 
494  if(tracking_info_[a.first].state == ADDON_INSTALLED_UPGRADABLE) {
495  has_upgradable_addons = true;
496  }
497  }
498 
499  find_widget<button>(get_window(), "update_all", false).set_active(has_upgradable_addons);
500 
501  apply_filters();
502 }
503 
505 {
506  load_addon_list();
507 
508  // Reselect the add-on.
509  find_widget<addon_list>(get_window(), "addons", false).select_addon(id);
510  on_addon_select();
511 }
512 
513 boost::dynamic_bitset<> addon_manager::get_name_filter_visibility() const
514 {
515  const text_box& name_filter = find_widget<const text_box>(get_window(), "filter", false);
516  const std::string& text = name_filter.get_value();
517 
518  filter_transform filter(utils::split(text, ' '));
519  boost::dynamic_bitset<> res;
520 
521  const config::const_child_itors& addon_cfgs = cfg_.child_range("campaign");
522 
523  for(const auto& a : addons_)
524  {
525  const config& addon_cfg = *std::find_if(addon_cfgs.begin(), addon_cfgs.end(),
526  [&a](const config& cfg)
527  {
528  return cfg["name"] == a.first;
529  });
530 
531  res.push_back(filter(addon_cfg));
532  }
533 
534  return res;
535 }
536 
537 boost::dynamic_bitset<> addon_manager::get_status_filter_visibility() const
538 {
539  const menu_button& status_filter = find_widget<const menu_button>(get_window(), "install_status_filter", false);
540  const ADDON_STATUS_FILTER selection = status_filter_types_[status_filter.get_value()].first;
541 
542  boost::dynamic_bitset<> res;
543  for(const auto& a : addons_) {
544  const addon_tracking_info& info = tracking_info_.at(a.second.id);
545 
546  res.push_back(
547  (selection == FILTER_ALL) ||
548  (selection == FILTER_INSTALLED && is_installed_addon_status(info.state)) ||
549  (selection == FILTER_UPGRADABLE && info.state == ADDON_INSTALLED_UPGRADABLE) ||
550  (selection == FILTER_PUBLISHABLE && info.can_publish == true) ||
551  (selection == FILTER_NOT_INSTALLED && info.state == ADDON_NONE)
552  );
553  }
554 
555  return res;
556 }
557 
558 boost::dynamic_bitset<> addon_manager::get_type_filter_visibility() const
559 {
560  const multimenu_button& type_filter = find_widget<const multimenu_button>(get_window(), "type_filter", false);
561 
562  boost::dynamic_bitset<> toggle_states = type_filter.get_toggle_states();
563  if(toggle_states.none()) {
564  // Nothing selected. It means that *all* add-ons are shown.
565  boost::dynamic_bitset<> res_flipped(addons_.size());
566  return ~res_flipped;
567  } else {
568  boost::dynamic_bitset<> res;
569 
570  for(const auto& a : addons_) {
571  int index = std::distance(type_filter_types_.begin(),
572  std::find_if(type_filter_types_.begin(), type_filter_types_.end(),
573  [&a](const std::pair<ADDON_TYPE, std::string>& entry) {
574  return entry.first == a.second.type;
575  })
576  );
577  res.push_back(toggle_states[index]);
578  }
579 
580  return res;
581  }
582 }
583 
585 {
586  boost::dynamic_bitset<> res =
590  find_widget<addon_list>(get_window(), "addons", false).set_addon_shown(res);
591 }
592 
594 {
595  const menu_button& order_menu = find_widget<const menu_button>(get_window(), "order_dropdown", false);
596  const addon_order& order_struct = all_orders_.at(order_menu.get_value() / 2);
597  preferences::SORT_ORDER order = order_menu.get_value() % 2 == 0 ? preferences::SORT_ORDER::ASCENDING : preferences::SORT_ORDER::DESCENDING;
599  if(order == preferences::SORT_ORDER::ASCENDING) {
600  func = order_struct.sort_func_asc;
601  } else {
602  func = order_struct.sort_func_desc;
603  }
604 
605  find_widget<addon_list>(get_window(), "addons", false).set_addon_order(func);
606  preferences::set_addon_manager_saved_order_name(order_struct.as_preference);
608 }
609 
610 void addon_manager::on_order_changed(unsigned int sort_column, preferences::SORT_ORDER order)
611 {
612  menu_button& order_menu = find_widget<menu_button>(get_window(), "order_dropdown", false);
613  auto order_it = std::find_if(all_orders_.begin(), all_orders_.end(),
614  [sort_column](const addon_order& order) {return order.column_index == static_cast<int>(sort_column);});
615  int index = 2 * (std::distance(all_orders_.begin(), order_it));
616  if(order == preferences::SORT_ORDER::DESCENDING) {
617  ++index;
618  }
619  order_menu.set_value(index);
620  preferences::set_addon_manager_saved_order_name(order_it->as_preference);
622 }
623 
624 template<void(addon_manager::*fptr)(const addon_info& addon)>
626 {
627  // Explicitly return to the main page if we're in low-res mode so the list is visible.
628  if(stacked_widget* stk = find_widget<stacked_widget>(get_window(), "main_stack", false, false)) {
629  stk->select_layer(0);
630  find_widget<button>(get_window(), "details_toggle", false).set_label(_("Addon Details"));
631  }
632 
633  addon_list& addons = find_widget<addon_list>(get_window(), "addons", false);
634  const addon_info* addon = addons.get_selected_addon();
635 
636  if(addon == nullptr) {
637  return;
638  }
639 
640  try {
641  (this->*fptr)(*addon);
642  } catch(const addons_client::user_exit&) {
643  // User canceled the op.
644  }
645 }
646 
648 {
649  addon_info versioned_addon = addon;
650  widget* parent = get_window();
651  if(stacked_widget* stk = find_widget<stacked_widget>(get_window(), "main_stack", false, false)) {
652  parent = stk->get_layer_grid(1);
653  }
654  if(addon.id == find_widget<addon_list>(get_window(), "addons", false).get_selected_addon()->id) {
655  versioned_addon.current_version = find_widget<menu_button>(parent, "version_filter", false).get_value_string();
656  }
657 
659 
660  // Take note if any wml_changes occurred
662 
665  }
666 }
667 
669 {
670  if(have_addon_pbl_info(addon.id) || have_addon_in_vcs_tree(addon.id)) {
672  _("The following add-on appears to have publishing or version control information stored locally, and will not be removed:") + " " +
673  addon.display_title_full());
674  return;
675  }
676 
677  bool success = remove_local_addon(addon.id);
678 
679  if(!success) {
680  gui2::show_error_message(_("The following add-on could not be deleted properly:") + " " + addon.display_title_full());
681  } else {
683 
685  }
686 }
687 
689 {
690  /* Currently, the install and update codepaths are the same, so this function simply
691  * calls the other. Since this might change in the future, I'm leaving this function
692  * here for now.
693  *
694  * - vultraz, 2017-03-12
695  */
696  install_addon(addon);
697 }
698 
700 {
701  for(const auto& a : addons_) {
702  if(tracking_info_[a.first].state == ADDON_INSTALLED_UPGRADABLE) {
704 
705  if(result.wml_changed) {
706  // Updating an add-on may have resulted in its dependencies being updated
707  // as well, so we need to reread version info blocks afterwards to make sure
708  // we don't try to re-download newly updated add-ons.
710  }
711 
712  // Take note if any wml_changes occurred
714  }
715  }
716 
718  load_addon_list();
719  }
720 }
721 
722 /** Performs all backend and UI actions for publishing the specified add-on. */
724 {
725  std::string server_msg;
726 
727  const std::string addon_id = addon.id;
728  config cfg = get_addon_pbl_info(addon_id);
729 
730  const version_info& version_to_publish = cfg["version"].str();
731 
732  if(version_to_publish <= tracking_info_[addon_id].remote_version) {
733  const int res = gui2::show_message(_("Warning"),
734  _("The remote version of this add-on is greater or equal to the version being uploaded. Do you really wish to continue?"),
736 
737  if(res != gui2::retval::OK) {
738  return;
739  }
740  }
741 
742  // the passphrase isn't provided, prompt for it
743  if(cfg["passphrase"].empty()) {
744  if(!gui2::dialogs::addon_auth::execute(cfg)) {
745  return;
746  }
747  }
748 
749  if(!::image::exists(cfg["icon"].str())) {
750  gui2::show_error_message(_("Invalid icon path. Make sure the path points to a valid image."));
751  } else if(!client_.request_distribution_terms(server_msg)) {
753  _("The server responded with an error:") + "\n" + client_.get_last_server_error());
754  } else if(gui2::dialogs::addon_license_prompt::execute(server_msg)) {
755  if(!client_.upload_addon(addon_id, server_msg, cfg, tracking_info_[addon_id].state == ADDON_INSTALLED_LOCAL_ONLY)) {
756  const std::string& msg = _("The add-on was rejected by the server:") +
757  "\n\n" + client_.get_last_server_error();
758  const std::string& extra_data = client_.get_last_server_error_data();
759  if (!extra_data.empty()) {
760  // TODO: Allow user to copy the extra data portion to clipboard
761  // or something, maybe display it in a dialog similar to
762  // the WML load errors report in a monospace font and
763  // stuff (having a scroll container is especially
764  // important since a long list can cause the dialog to
765  // overflow).
766  gui2::show_error_message(msg + "\n\n" + extra_data, true);
767  } else {
768  gui2::show_error_message(msg, true);
769  }
770  } else {
771  gui2::show_transient_message(_("Response"), server_msg);
774  }
775  }
776 }
777 
778 /** Performs all backend and UI actions for taking down the specified add-on. */
780 {
781  const std::string addon_id = addon.id;
782  const std::string& text = VGETTEXT(
783  "Deleting '$addon|' will permanently erase its download and upload counts on the add-ons server. Do you really wish to continue?",
784  {{"addon", make_addon_title(addon_id)}} // FIXME: need the real title!
785  );
786 
787  const int res = gui2::show_message(_("Confirm"), text, gui2::dialogs::message::yes_no_buttons);
788 
789  if(res != gui2::retval::OK) {
790  return;
791  }
792 
793  std::string server_msg;
794  if(!client_.delete_remote_addon(addon_id, server_msg)) {
795  gui2::show_error_message(_("The server responded with an error:") + "\n" + client_.get_last_server_error());
796  } else {
797  // FIXME: translation needed!
798  gui2::show_transient_message(_("Response"), server_msg);
801  }
802 }
803 
804 /** Called when the player double-clicks an add-on. */
806 {
807  switch(tracking_info_[addon.id].state) {
808  case ADDON_NONE:
809  install_addon(addon);
810  break;
811  case ADDON_INSTALLED:
812  if(!tracking_info_[addon.id].can_publish) {
813  utils::string_map symbols{ { "addon", addon.display_title_full() } };
814  int res = gui2::show_message(_("Uninstall add-on"),
815  VGETTEXT("Do you want to uninstall '$addon|'?", symbols),
817  if(res == gui2::retval::OK) {
818  uninstall_addon(addon);
819  }
820  }
821  break;
823  update_addon(addon);
824  break;
827  publish_addon(addon);
828  break;
829  default:
830  break;
831  }
832 }
833 
835 {
836  help::show_help("installing_addons");
837 }
838 
839 static std::string format_addon_time(std::time_t time)
840 {
841  if(time) {
842  std::ostringstream ss;
843 
845  ? "%Y-%m-%d %I:%M %p"
846  : "%Y-%m-%d %H:%M";
847 
848  ss << std::put_time(std::localtime(&time), format);
849 
850  return ss.str();
851  }
852 
853  return font::unicode_em_dash;
854 }
855 
857 {
858  const addon_info* info = find_widget<addon_list>(get_window(), "addons", false).get_selected_addon();
859 
860  if(info == nullptr) {
861  return;
862  }
863 
864  widget* parent = get_window();
865  if(stacked_widget* stk = find_widget<stacked_widget>(get_window(), "main_stack", false, false)) {
866  parent = stk->get_layer_grid(1);
867  }
868 
869  find_widget<drawing>(parent, "image", false).set_label(info->display_icon());
870 
871  find_widget<styled_widget>(parent, "title", false).set_label(info->display_title_translated_or_original());
872  find_widget<styled_widget>(parent, "description", false).set_label(info->description_translated());
873  menu_button& version_filter = find_widget<menu_button>(parent, "version_filter", false);
874  find_widget<styled_widget>(parent, "author", false).set_label(info->author);
875  find_widget<styled_widget>(parent, "type", false).set_label(info->display_type());
876 
877  styled_widget& status = find_widget<styled_widget>(parent, "status", false);
879  status.set_use_markup(true);
880 
881  find_widget<styled_widget>(parent, "size", false).set_label(size_display_string(info->size));
882  find_widget<styled_widget>(parent, "downloads", false).set_label(std::to_string(info->downloads));
883  find_widget<styled_widget>(parent, "created", false).set_label(format_addon_time(info->created));
884  find_widget<styled_widget>(parent, "updated", false).set_label(format_addon_time(info->updated));
885 
886  find_widget<styled_widget>(parent, "dependencies", false).set_label(!info->depends.empty()
887  ? make_display_dependencies(info->id, addons_, tracking_info_)
888  : _("addon_dependencies^None"));
889 
890  std::string languages;
891 
892  for(const auto& lc : info->locales) {
893  const std::string& langlabel = langcode_to_string(lc);
894  if(!langlabel.empty()) {
895  if(!languages.empty()) {
896  languages += ", ";
897  }
898  languages += langlabel;
899  }
900  }
901 
902  find_widget<styled_widget>(parent, "translations", false).set_label(!languages.empty() ? languages : _("translations^None"));
903 
904  const std::string& feedback_url = info->feedback_url;
905  find_widget<label>(parent, "url", false).set_label(!feedback_url.empty() ? feedback_url : _("url^None"));
906 
907  bool installed = is_installed_addon_status(tracking_info_[info->id].state);
908  bool updatable = tracking_info_[info->id].state == ADDON_INSTALLED_UPGRADABLE;
909 
910  stacked_widget& action_stack = find_widget<stacked_widget>(parent, "action_stack", false);
911  // #TODO: Add tooltips with upload time and pack size
912  std::vector<config> version_filter_entries;
913 
914  if(!tracking_info_[info->id].can_publish) {
915  action_stack.select_layer(0);
916 
917  stacked_widget& install_update_stack = find_widget<stacked_widget>(parent, "install_update_stack", false);
918  install_update_stack.select_layer(updatable ? 1 : 0);
919 
920  if(!updatable) {
921  find_widget<button>(parent, "install", false).set_active(!installed);
922  } else {
923  find_widget<button>(parent, "update", false).set_active(true);
924  }
925 
926  find_widget<button>(parent, "uninstall", false).set_active(installed);
927 
928  for(const auto& f : info->versions) {
929  version_filter_entries.emplace_back("label", f.str());
930  }
931  version_filter.set_active(true);
932  } else {
933  action_stack.select_layer(1);
934 
935  // Always enable the publish button, but disable the delete button if not yet published.
936  find_widget<button>(parent, "publish", false).set_active(true);
937  find_widget<button>(parent, "delete", false).set_active(!info->local_only);
938 
939  // Show only the version to be published
940  version_filter_entries.emplace_back("label", info->current_version.str());
941  version_filter.set_active(false);
942  }
943  version_filter.set_values(version_filter_entries);
944 }
945 
947 {
948  widget* parent = get_window();
949  if(stacked_widget* stk = find_widget<stacked_widget>(get_window(), "main_stack", false, false)) {
950  parent = stk->get_layer_grid(1);
951  }
952 
953  const addon_info* info = find_widget<addon_list>(get_window(), "addons", false).get_selected_addon();
954 
955  if(info == nullptr) {
956  return;
957  }
958 
959  if(!tracking_info_[info->id].can_publish && is_installed_addon_status(tracking_info_[info->id].state)) {
960  bool updatable = tracking_info_[info->id].installed_version
961  != find_widget<menu_button>(parent, "version_filter", false).get_value_string();
962  stacked_widget& action_stack = find_widget<stacked_widget>(parent, "action_stack", false);
963  action_stack.select_layer(0);
964 
965  stacked_widget& install_update_stack = find_widget<stacked_widget>(parent, "install_update_stack", false);
966  install_update_stack.select_layer(1);
967  find_widget<button>(parent, "update", false).set_active(updatable);
968  }
969 }
970 
972 {
975  return false;
976  }
977 
978  return true;
979 }
980 
981 } // namespace dialogs
int size
Definition: info.hpp:86
const std::string & get_last_server_error() const
Returns the last error message sent by the server, or an empty string.
Definition: client.hpp:73
void read_addons_list(const config &cfg, addons_list &dest)
Parse the specified add-ons list WML into an actual addons_list object.
Definition: info.cpp:293
void close()
Requests to close the window.
Definition: window.hpp:181
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
std::string addon_manager_saved_order_name()
Definition: general.cpp:953
Modification of the game.
Definition: validation.hpp:109
Single-player scenario.
Definition: validation.hpp:101
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:96
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
boost::dynamic_bitset get_status_filter_visibility() const
Definition: manager.cpp:537
bool exit_hook(window &window)
Definition: manager.cpp:971
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:112
void set_addon_manager_saved_order_direction(SORT_ORDER value)
Definition: general.cpp:968
static std::string format_addon_time(std::time_t time)
Definition: manager.cpp:839
A menu_button is a styled_widget to choose an element from a list of elements.
Definition: menu_button.hpp:60
SORT_ORDER addon_manager_saved_order_direction()
Definition: general.cpp:963
Total Conversion Core.
Definition: validation.hpp:99
addons_tracking_list tracking_info_
Definition: manager.hpp:102
preferences::SORT_ORDER SORT_ORDER
Definition: listbox.cpp:41
std::string display_title_translated_or_original() const
Definition: info.cpp:206
logger & info()
Definition: log.cpp:88
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:356
void reload_list_and_reselect_item(const std::string id)
Definition: manager.cpp:504
void show_transient_message(const std::string &title, const std::string &message, const std::string &image, const bool message_use_markup, const bool title_use_markup, const bool restore_background)
Shows a transient message to the user.
void execute_default_action(const addon_info &addon)
Called when the player double-clicks an add-on.
Definition: manager.cpp:805
Base class for all widgets.
Definition: widget.hpp:49
int current_layer() const
Gets the current visible layer number.
Multiplayer faction.
Definition: validation.hpp:107
virtual void set_value(unsigned value, bool fire_event=false) override
Inherited from selectable_item.
Definition: menu_button.hpp:86
bool chars_equal_insensitive(char a, char b)
Definition: general.hpp:22
void set_escape_disabled(const bool escape_disabled)
Disable the escape key.
Definition: window.hpp:298
window * get_window() const
Returns a pointer to the dialog&#39;s window.
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:104
A label displays a text, the text can be wrapped but no scrollbars are provided.
Definition: label.hpp:56
A multimenu_button is a styled_widget to choose an element from a list of elements.
std::string feedback_url
Definition: info.hpp:100
bool have_addon_in_vcs_tree(const std::string &addon_name)
Returns whether the specified add-on appears to be managed by a VCS or not.
Definition: manager.cpp:53
static std::string _(const char *str)
Definition: gettext.hpp:92
Definitions for the interface to Wesnoth Markup Language (WML).
Define the common filters for the gui2::pane class.
void set_callback_order_change(std::function< void(unsigned, preferences::SORT_ORDER)> callback)
Sets up a callback that will be called when the player changes the sorting order. ...
Definition: addon_list.hpp:137
version_info installed_version
Definition: state.hpp:59
std::deque< std::unique_ptr< editor_action > > action_stack
Action stack typedef.
No tracking information available.
Definition: state.hpp:36
const_attr_itors attribute_range() const
Definition: config.cpp:833
Add-on is not installed.
Definition: state.hpp:23
void update_addon(const addon_info &addon)
Definition: manager.cpp:688
Class for a single line text area.
Definition: text_box.hpp:140
std::vector< std::string > available_addons()
Returns a list of local add-ons that can be published.
Definition: manager.cpp:172
#define b
const std::vector< std::string > filtertext_
Definition: manager.cpp:103
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:1010
Desktop environment interaction functions.
virtual unsigned get_value() const override
Inherited from selectable_item.
Definition: menu_button.hpp:83
boost::dynamic_bitset get_name_filter_visibility() const
Definition: manager.cpp:513
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
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:186
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 connect_signal_mouse_left_click(dispatcher &dispatcher, const signal_function &signal)
Connects a signal handler for a left mouse button click.
Definition: dispatcher.cpp:171
std::string display_icon() const
Get an icon path fixed for display (e.g.
Definition: info.cpp:231
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:76
std::string id
Definition: info.hpp:75
Version in the server is newer than local installation.
Definition: state.hpp:27
void toggle_details(button &btn, stacked_widget &stk)
Definition: manager.cpp:433
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:248
Shows a yes and no button.
Definition: message.hpp:79
virtual void set_use_markup(bool use_markup)
void refresh_addon_version_info_cache()
Refreshes the per-session cache of add-on&#39;s version information structs.
Definition: manager.cpp:355
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)
Removes the specified add-on, deleting its full directory structure.
Definition: manager.cpp:129
Dependencies not satisfied.
Definition: state.hpp:34
Add-ons (campaignd) client class.
Definition: client.hpp:39
int get_retval()
Definition: window.hpp:364
config get_addon_pbl_info(const std::string &addon_name)
Gets the publish information for an add-on.
Definition: manager.cpp:67
User aborted the operation because of an issue with dependencies or chose not to overwrite the add-on...
Miscellaneous content/media (unit packs, terrain packs, music packs, etc.).
Definition: validation.hpp:111
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:204
static std::string describe_status_verbose(const addon_tracking_info &state)
Definition: manager.cpp:219
bool ci_search(const std::string &s1, const std::string &s2)
Definition: gettext.cpp:517
void on_order_changed(unsigned int sort_column, preferences::SORT_ORDER order)
Definition: manager.cpp:610
bool local_only
Definition: info.hpp:107
std::time_t created
Definition: info.hpp:103
std::vector< std::string > locales
Definition: info.hpp:93
Multiplayer scenario.
Definition: validation.hpp:104
boost::dynamic_bitset get_toggle_states() const
Get the current state of the menu options.
Multiplayer era.
Definition: validation.hpp:106
void delete_addon(const addon_info &addon)
Performs all backend and UI actions for taking down the specified add-on.
Definition: manager.cpp:779
bool have_addon_pbl_info(const std::string &addon_name)
Returns whether a .pbl file is present for the specified add-on or not.
Definition: manager.cpp:62
addons_client & client_
Definition: manager.hpp:98
static map_location::DIRECTION s
std::string size_display_string(double size)
Get a human-readable representation of the specified byte count.
Definition: info.cpp:310
addon_manager(addons_client &client)
Definition: manager.cpp:209
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:102
void execute_default_action_on_selected_addon()
Definition: manager.hpp:144
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
std::string display_title_full() const
Definition: info.cpp:223
Multiplayer plain (no WML) map pack.
Definition: validation.hpp:105
#define N_(String)
Definition: gettext.hpp:100
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.
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
install_outcome outcome
Overall outcome of the operation.
Definition: client.hpp:125
std::set< version_info, std::greater< version_info > > versions
Definition: info.hpp:82
void select_layer(const int layer)
Selects and displays a particular layer.
boost::dynamic_bitset get_type_filter_visibility() const
Definition: manager.cpp:558
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.
void publish_addon(const addon_info &addon)
Performs all backend and UI actions for publishing the specified add-on.
Definition: manager.cpp:723
config & add_child(config_key_type key)
Definition: config.cpp:500
std::string make_addon_title(const std::string &id)
Replaces underscores to dress up file or dirnames as add-on titles.
Definition: info.cpp:319
void install_addon(const addon_info &addon)
Definition: manager.cpp:647
void uninstall_addon(const addon_info &addon)
Definition: manager.cpp:668
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
static const std::vector< std::pair< ADDON_TYPE, std::string > > type_filter_types_
Definition: manager.hpp:105
std::vector< std::string > depends
Definition: info.hpp:97
#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:102
std::vector< std::string > split(const config_attribute_value &val)
bool wml_changed
Specifies if WML on disk was altered and needs to be reloaded.
Definition: client.hpp:134
std::map< std::string, addon_tracking_info > addons_tracking_list
Definition: state.hpp:63
const std::string unicode_em_dash
Definition: constants.cpp:43
std::string title
Definition: info.hpp:76
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:280
Simple push button.
Definition: button.hpp:35
std::string str() const
Serializes the version number into string form.
bool upload_addon(const std::string &id, std::string &response_message, config &cfg, bool local_only)
Uploads an add-on to the server.
Definition: client.cpp:153
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
A stacked widget holds several widgets on top of each other.
int downloads
Definition: info.hpp:87
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:59
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:263
bool request_distribution_terms(std::string &terms)
Request the add-ons server distribution terms message.
Definition: client.cpp:130
Contains the outcome of an add-on install operation.
Definition: client.hpp:120
version_info current_version
Definition: info.hpp:81
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:64
install_result install_addon_with_checks(const addons_list &addons, const addon_info &addon)
Performs an add-on download and install cycle.
Definition: client.cpp:549
Single-player campaign.
Definition: validation.hpp:100
void set_addon_manager_saved_order_name(const std::string &value)
Definition: general.cpp:958
bool use_twelve_hour_clock_format()
Definition: general.cpp:928
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
Actions to be taken before showing the window.
Definition: manager.cpp:274
bool request_addons_list(config &cfg)
Request the add-ons list from the server.
Definition: client.cpp:113
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:103
#define GETTEXT_DOMAIN
Definition: manager.cpp:15
std::string author
Definition: info.hpp:84
void set_link_aware(bool l)
Definition: label.cpp:90
static const std::vector< addon_order > all_orders_
Definition: manager.hpp:106
Version in the server matches local installation.
Definition: state.hpp:25
std::string description_translated() const
Definition: info.cpp:212