The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
client.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
3  2008 - 2015 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
4  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "addon/info.hpp"
20 #include "network_asio.hpp"
21 
22 /**
23  * Add-ons (campaignd) client class.
24  *
25  * This class encapsulates much of the logic behind the campaignd
26  * add-ons server interaction for the client-side. Most networking
27  * operations with it are implemented here.
28  */
30 {
31 public:
33 
35  {
38  };
39 
42  struct user_exit {};
43 
44  addons_client(const addons_client&) = delete;
45  addons_client& operator=(const addons_client&) = delete;
46 
47  /**
48  * Constructor.
49  *
50  * @param address Add-ons server host address (i.e. localhost:15999).
51  */
52  explicit addons_client(const std::string& address);
53 
54  /**
55  * Try to establish a connection to the add-ons server.
56  */
57  void connect();
58 
59  /** Returns the last error message sent by the server, or an empty string. */
60  const std::string& get_last_server_error() const { return last_error_; }
61 
62  /** Returns the last error message extra data sent by the server, or an empty string. */
64 
65  /**
66  * Request the add-ons list from the server.
67  *
68  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
69  *
70  * @param cfg A config object whose contents are replaced with
71  * the server's list if available, cleared otherwise.
72  */
73  bool request_addons_list(config& cfg);
74 
75  /**
76  * Request the add-ons server distribution terms message.
77  */
79 
80  /**
81  * Do a 'smart' fetch of an add-on, checking to avoid overwrites for devs and resolving dependencies, using gui interaction to handle issues that arise
82  * Returns: outcome: abort in case the user chose to abort because of an issue
83  * failure in case we resolved checks and dependencies, but fetching this particular add-on failed
84  * success otherwise
85  * wml_changed: indicates if new wml content was installed at any point
86  */
87  install_result install_addon_with_checks(const addons_list& addons, const addon_info& addon);
88 
89  /**
90  * Requests the specified add-on to be uploaded.
91  *
92  * This method reads the add-on upload passphrase and other data
93  * from the associated .pbl file. If the .pbl file doesn't have a
94  * passphrase, a new, random one will be automatically generated
95  * and written to the file for the user.
96  *
97  * @todo Notify the user about the automatic passphrase.
98  *
99  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
100  *
101  * @param id Id. of the add-on to upload.
102  * @param response_message The server response message on success, such as "add-on accepted".
103  * @param cfg The pbl config of the add-on with the specified id.
104  */
105  bool upload_addon(const std::string& id, std::string& response_message, config& cfg);
106 
107  /**
108  * Requests the specified add-on to be removed from the server.
109  *
110  * This method reads the add-on upload passphrase from the associated
111  * .pbl file.
112  *
113  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
114  *
115  * @param id Id. of the add-on to take down.
116  * @param response_message The server response message on success, such as "add-on accepted".
117  */
118  bool delete_remote_addon(const std::string& id, std::string& response_message);
119 
120 private:
124  std::unique_ptr<network_asio::connection> conn_;
125  std::unique_ptr<gui2::dialogs::network_transmission> stat_;
128 
129  /**
130  * Downloads the specified add-on from the server.
131  *
132  * @return @a true on success, @a false on failure. Retrieve the error message with @a get_last_server_error.
133  *
134  * @param id Add-on id.
135  * @param title Add-on title, used for status display.
136  * @param archive_cfg Config object to hold the downloaded add-on archive data.
137  * @param increase_downloads Whether to request the server to increase the add-on's
138  * download count or not (e.g. when upgrading).
139  */
140  bool download_addon(config& archive_cfg, const std::string& id, const std::string& title, bool increase_downloads = true);
141 
142  /**
143  * Installs the specified add-on using an archive received from the server.
144  *
145  * An _info.cfg file will be added to the local directory for the add-on
146  * to keep track of version and dependency information.
147  */
148  bool install_addon(config& archive_cfg, const addon_info& info);
149 
150  // Asks the client to download and install an addon, reporting errors in a gui dialog. Returns true if new content was installed, false otherwise.
151  bool try_fetch_addon(const addon_info& addon);
152 
153  /**
154  * Warns the user about unresolved dependencies and installs them if they choose to do so.
155  * Returns: outcome: abort in case the user chose to abort because of an issue
156  * success otherwise
157  * wml_change: indicates if new wml content was installed
158  */
160 
161  /** Checks whether the given add-on has local .pbl or VCS information and asks before overwriting it. */
163 
164  /** Makes sure the add-ons server connection is working. */
165  void check_connected() const;
166 
167  /**
168  * Sends a request to the add-ons server.
169  *
170  * @note This is an asynchronous operation. @a display_status_window
171  * should be called afterwards to wait for it to finish.
172  *
173  * @param request The client request WML.
174  * @param response A config object whose contents are replaced
175  * with the server response WML.
176  */
177  void send_request(const config& request, config& response);
178 
179  /**
180  * Sends a simple request message to the add-ons server.
181  *
182  * The real request sent consists of a WML object with an empty
183  * child node whose name corresponds to @a request_string
184  *
185  * @note This is an asynchronous operation. @a display_status_window
186  * should be called afterwards to wait for it to finish.
187  *
188  * @param request_string The client request string.
189  * @param response A config object whose contents are replaced
190  * with the server response WML.
191  */
192  void send_simple_request(const std::string& request_string, config& response);
193 
194  /**
195  * Waits for a network transfer, displaying a status window.
196  *
197  * The window is displayed with the specified contents. This
198  * method doesn't return until the network transfer is complete. It
199  * will throw a @a user_exit exception if the user cancels the
200  * transfer by canceling the status window.
201  */
202  void wait_for_transfer_done(const std::string& status_message, bool track_upload = false);
203 
204  bool update_last_error(config& response_cfg);
205 };
install_result do_resolve_addon_dependencies(const addons_list &addons, const addon_info &addon)
Warns the user about unresolved dependencies and installs them if they choose to do so...
Definition: client.cpp:326
std::string port_
Definition: client.hpp:123
std::vector< char_t > string
bool download_addon(config &archive_cfg, const std::string &id, const std::string &title, bool increase_downloads=true)
Downloads the specified add-on from the server.
Definition: client.cpp:225
std::string host_
Definition: client.hpp:122
logger & info()
Definition: log.cpp:91
bool try_fetch_addon(const addon_info &addon)
Definition: client.cpp:307
bool update_last_error(config &response_cfg)
Definition: client.cpp:480
addons_client(const addons_client &)=delete
std::string last_error_
Definition: client.hpp:126
bool install_addon(config &archive_cfg, const addon_info &info)
Installs the specified add-on using an archive received from the server.
Definition: client.cpp:246
void send_request(const config &request, config &response)
Sends a request to the add-ons server.
Definition: client.cpp:503
const std::string & get_last_server_error() const
Returns the last error message sent by the server, or an empty string.
Definition: client.hpp:60
void check_connected() const
Makes sure the add-ons server connection is working.
Definition: client.cpp:494
addons_client & operator=(const addons_client &)=delete
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:63
void connect()
Try to establish a connection to the add-ons server.
Definition: client.cpp:63
Add-ons (campaignd) client class.
Definition: client.hpp:29
std::string addr_
Definition: client.hpp:121
bool do_check_before_overwriting_addon(const addon_info &addon)
Checks whether the given add-on has local .pbl or VCS information and asks before overwriting it...
Definition: client.cpp:421
void wait_for_transfer_done(const std::string &status_message, bool track_upload=false)
Waits for a network transfer, displaying a status window.
Definition: client.cpp:537
install_outcome outcome
Definition: client.hpp:36
std::string last_error_data_
Definition: client.hpp:127
std::unique_ptr< network_asio::connection > conn_
Definition: client.hpp:124
std::unique_ptr< gui2::dialogs::network_transmission > stat_
Definition: client.hpp:125
bool upload_addon(const std::string &id, std::string &response_message, config &cfg)
Requests the specified add-on to be uploaded.
Definition: client.cpp:109
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
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:193
bool request_distribution_terms(std::string &terms)
Request the add-ons server distribution terms message.
Definition: client.cpp:93
std::map< std::string, addon_info > addons_list
Definition: info.hpp:26
install_result install_addon_with_checks(const addons_list &addons, const addon_info &addon)
Do a 'smart' fetch of an add-on, checking to avoid overwrites for devs and resolving dependencies...
Definition: client.cpp:454
bool request_addons_list(config &cfg)
Request the add-ons list from the server.
Definition: client.cpp:76
void send_simple_request(const std::string &request_string, config &response)
Sends a simple request message to the add-ons server.
Definition: client.cpp:511