The Battle for Wesnoth  1.15.1+dev
addon_list.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2016 - 2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #pragma once
15 
16 #include "addon/info.hpp"
17 #include "addon/manager.hpp"
18 #include "addon/state.hpp"
20 #include "gui/widgets/listbox.hpp"
21 #include "gui/widgets/widget.hpp"
22 
23 #include <boost/dynamic_bitset.hpp>
24 #include <functional>
25 #include <string>
26 #include <vector>
27 
28 namespace gui2
29 {
30 
31 namespace implementation
32 {
33  struct builder_addon_list;
34 }
35 
36 class addon_list : public container_base
37 {
39 
40 public:
41  using addon_sort_func = std::function<bool(const addon_info&, const addon_info&)>;
42 
43  explicit addon_list(const implementation::builder_addon_list& builder);
44 
45  /** Special retval for the toggle panels in the addons list */
46  static const int DEFAULT_ACTION_RETVAL = 200;
47 
48  /** Sets the add-ons to show. */
49  void set_addons(const addons_list& addons);
50 
51  /** Sets up a callback that will be called when the player selects an add-on. */
52  void set_modified_signal_handler(const std::function<void()>& callback)
53  {
54  connect_signal_notify_modified(get_listbox(), std::bind(callback));
55  }
56 
57  /** Returns the selected add-on. */
58  const addon_info* get_selected_addon() const;
59 
60  /** Returns the selected add-on id, for use with remote publish/delete ops. */
61  std::string get_remote_addon_id();
62 
63  /** Selects the add-on with the given ID. */
64  void select_addon(const std::string& id);
65 
66  using addon_op_func_t = std::function<void(const addon_info&)>;
67 
68  /**
69  * Helper to wrap the execution of any of the addon operation functions.
70  * It catches addons_client::user_exit exceptions and halts GUI2 event execution
71  * after calling the given function.
72  */
73  void addon_action_wrapper(addon_op_func_t& func, const addon_info& addon, bool& handled, bool& halt);
74 
75  /** Sets the function to call when the player clicks the install button. */
77  {
78  install_function_ = function;
79  }
80 
81  /** Sets the function to call when the player clicks the uninstall button. */
83  {
84  uninstall_function_ = function;
85  }
86 
87  /** Sets the function to call when the player clicks the update button. */
89  {
90  update_function_ = function;
91  }
92 
93  /** Sets the function to upload an addon to the addons server. */
95  {
96  publish_function_ = function;
97  }
98 
99  /** Sets the function to install an addon from the addons server. */
101  {
102  delete_function_ = function;
103  }
104 
105  /** Filters which add-ons are visible. 1 = visible, 0 = hidden. */
106  void set_addon_shown(boost::dynamic_bitset<>& shown)
107  {
108  get_listbox().set_row_shown(shown);
109  }
110 
111  void set_addon_order(addon_sort_func func);
112 
113  /**
114  * Changes the color of an add-on state string (installed, outdated, etc.) according to the state itself.
115  * This function is here because the add-on list widget itself needs it.
116  */
117  static std::string colorize_addon_state_string(const std::string& str, ADDON_STATUS state, bool verbose = false);
118 
119  /** Determines if install status of each widget is shown. */
120  void set_install_status_visibility(visibility visibility)
121  {
122  install_status_visibility_ = visibility;
123  }
124 
125  /** Determines if install/uninstall buttons are shown for each widget. */
126  void set_install_buttons_visibility(visibility visibility)
127  {
128  install_buttons_visibility_ = visibility;
129  }
130 
131  /** Adds the internal listbox to the keyboard event chain. */
132  void add_list_to_keyboard_chain();
133 
134  /** Sets up a callback that will be called when the player changes the sorting order. */
135  void set_callback_order_change(std::function<void(unsigned, listbox::SORT_ORDER)> callback) {
136  get_listbox().set_callback_order_change(callback);
137  }
138 
139  /** See @ref styled_widget::set_active. */
140  virtual void set_active(const bool) override
141  {
142  // DO NOTHING
143  }
144 
145  /** See @ref styled_widget::get_active. */
146  virtual bool get_active() const override
147  {
148  return true;
149  }
150 
151  /** See @ref styled_widget::get_state. */
152  virtual unsigned get_state() const override
153  {
154  return 0;
155  }
156 
157 private:
158  std::vector<const addon_info*> addon_vector_;
159 
162 
166 
169 
170  static std::string describe_status(const addon_tracking_info& info);
171 
172  /** Returns the underlying list box. */
173  listbox& get_listbox();
174 
175  void finalize_setup();
176 
177  /** Needed because otherwise the add-on with the first ID would be initially selected. */
178  void select_first_addon();
179 
180 public:
181  /** Static type getter that does not rely on the widget being constructed. */
182  static const std::string& type();
183 
184 private:
185  /** Inherited from styled_widget, implemented by REGISTER_WIDGET. */
186  virtual const std::string& get_control_type() const override;
187 
188  /** See @ref container_::set_self_active. */
189  void set_self_active(const bool) override
190  {
191  // DO NOTHING
192  }
193 };
194 
196 {
197  explicit addon_list_definition(const config& cfg);
198 
200  {
201  explicit resolution(const config& cfg);
202 
204  };
205 };
206 
207 namespace implementation
208 {
209 
211 {
212 public:
213  explicit builder_addon_list(const config& cfg);
214 
216 
217  widget* build() const;
218 
219 private:
222 };
223 }
224 }
Base class of a resolution, contains the common keys for a resolution.
visibility install_status_visibility_
Definition: addon_list.hpp:160
virtual widget * build() const =0
std::function< bool(const addon_info &, const addon_info &)> addon_sort_func
Definition: addon_list.hpp:41
logger & info()
Definition: log.cpp:90
ADDON_STATUS
Defines various add-on installation statuses.
Definition: state.hpp:21
Base class for all widgets.
Definition: widget.hpp:47
void set_addon_shown(boost::dynamic_bitset<> &shown)
Filters which add-ons are visible.
Definition: addon_list.hpp:106
addon_op_func_t delete_function_
Definition: addon_list.hpp:168
void set_install_status_visibility(visibility visibility)
Determines if install status of each widget is shown.
Definition: addon_list.hpp:120
virtual unsigned get_state() const override
See styled_widget::get_state.
Definition: addon_list.hpp:152
virtual void set_active(const bool) override
See styled_widget::set_active.
Definition: addon_list.hpp:140
Generic file dialog.
Definition: field-fwd.hpp:22
The listbox class.
Definition: listbox.hpp:40
visibility install_buttons_visibility_
Definition: addon_list.hpp:161
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
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:248
void set_callback_order_change(std::function< void(unsigned, listbox::SORT_ORDER)> callback)
Sets up a callback that will be called when the player changes the sorting order. ...
Definition: addon_list.hpp:135
std::vector< const addon_info * > addon_vector_
Definition: addon_list.hpp:158
virtual bool get_active() const override
See styled_widget::get_active.
Definition: addon_list.hpp:146
widget::visibility install_buttons_visibility_
Definition: addon_list.hpp:221
void set_self_active(const bool) override
See container_::set_self_active.
Definition: addon_list.hpp:189
addon_op_func_t uninstall_function_
Definition: addon_list.hpp:164
window * build(const builder_window::window_resolution *definition)
Builds a window.
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
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
visibility
Visibility settings done by the user.
Definition: widget.hpp:57
A generic container base class.
addon_op_func_t publish_function_
Definition: addon_list.hpp:167
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
addon_op_func_t install_function_
Definition: addon_list.hpp:163
void set_install_buttons_visibility(visibility visibility)
Determines if install/uninstall buttons are shown for each widget.
Definition: addon_list.hpp:126
point resolution()
Definition: general.cpp:373
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
Stores additional status information about add-ons.
Definition: state.hpp:45
std::function< void(const addon_info &)> addon_op_func_t
Definition: addon_list.hpp:66
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
std::map< std::string, addon_info > addons_list
Definition: info.hpp:26
std::shared_ptr< builder_grid > builder_grid_ptr
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
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
addon_op_func_t update_function_
Definition: addon_list.hpp:165