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