-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
manager_ui.cpp
316 lines (247 loc) · 9.64 KB
/
manager_ui.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
Copyright (C) 2003 - 2008 by David White <dave@whitevine.net>
2008 - 2015 by Ignacio Riquelme Morelle <shadowm2006@gmail.com>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "addon/manager_ui.hpp"
#include "addon/client.hpp"
#include "addon/info.hpp"
#include "addon/manager.hpp"
#include "filesystem.hpp"
#include "formula/string_utils.hpp"
#include "preferences/game.hpp"
#include "gettext.hpp"
#include "gui/dialogs/addon/manager.hpp"
#include "gui/dialogs/addon/uninstall_list.hpp"
#include "gui/dialogs/addon/connect.hpp"
#include "gui/dialogs/message.hpp"
#include "gui/dialogs/transient_message.hpp"
#include "gui/widgets/window.hpp"
#include "log.hpp"
#include "wml_exception.hpp"
static lg::log_domain log_config("config");
static lg::log_domain log_network("network");
static lg::log_domain log_filesystem("filesystem");
static lg::log_domain log_addons_client("addons-client");
#define ERR_CFG LOG_STREAM(err, log_config)
#define ERR_NET LOG_STREAM(err, log_network)
#define ERR_FS LOG_STREAM(err, log_filesystem)
#define LOG_AC LOG_STREAM(info, log_addons_client)
namespace {
bool get_addons_list(addons_client& client, addons_list& list)
{
list.clear();
config cfg;
client.request_addons_list(cfg);
if(!cfg) {
return false;
}
read_addons_list(cfg, list);
return true;
}
bool addons_manager_ui(CVideo& v, const std::string& remote_address)
{
bool need_wml_cache_refresh = false;
preferences::set_campaign_server(remote_address);
try {
addons_client client(v, remote_address);
client.connect();
gui2::dialogs::addon_manager dlg(client);
dlg.show();
need_wml_cache_refresh = dlg.get_need_wml_cache_refresh();
} catch(const config::error& e) {
ERR_CFG << "config::error thrown during transaction with add-on server; \""<< e.message << "\"" << std::endl;
gui2::show_error_message(_("Network communication error."));
} catch(const network_asio::error& e) {
ERR_NET << "network_asio::error thrown during transaction with add-on server; \""<< e.what() << "\"" << std::endl;
gui2::show_error_message(_("Remote host disconnected."));
} catch(const filesystem::io_exception& e) {
ERR_FS << "filesystem::io_exception thrown while installing an addon; \"" << e.what() << "\"" << std::endl;
gui2::show_error_message(_("A problem occurred when trying to create the files necessary to install this add-on."));
} catch(const invalid_pbl_exception& e) {
ERR_CFG << "could not read .pbl file " << e.path << ": " << e.message << std::endl;
utils::string_map symbols;
symbols["path"] = e.path;
symbols["msg"] = e.message;
gui2::show_error_message(
vgettext("A local file with add-on publishing information could not be read.\n\nFile: $path\nError message: $msg", symbols));
} catch(wml_exception& e) {
e.show();
} catch(const addons_client::user_exit&) {
LOG_AC << "initial connection canceled by user\n";
} catch(const addons_client::invalid_server_address&) {
gui2::show_error_message(_("The add-ons server address specified is not valid."));
}
return need_wml_cache_refresh;
}
bool uninstall_local_addons()
{
const std::string list_lead = "\n\n";
const std::vector<std::string>& addons = installed_addons();
if(addons.empty()) {
gui2::show_error_message(_("You have no add-ons installed."));
return false;
}
std::map<std::string, std::string> addon_titles_map;
for(const std::string& id : addons) {
std::string title;
if(have_addon_install_info(id)) {
// _info.cfg may have the add-on's title starting with 1.11.7,
// if the add-on was downloading using the revised _info.cfg writer.
config cfg;
get_addon_install_info(id, cfg);
const config& info_cfg = cfg.child("info");
if(info_cfg) {
title = info_cfg["title"].str();
}
}
if(title.empty()) {
// Transform the id into a title as a last resort.
title = make_addon_title(id);
}
addon_titles_map[id] = title;
}
int res;
std::vector<std::string> remove_ids;
std::set<std::string> remove_names;
do {
gui2::dialogs::addon_uninstall_list dlg(addon_titles_map);
dlg.show();
remove_ids = dlg.selected_addons();
if(remove_ids.empty()) {
return false;
}
remove_names.clear();
for(const std::string& id : remove_ids) {
remove_names.insert(addon_titles_map[id]);
}
const std::string confirm_message = _n(
"Are you sure you want to remove the following installed add-on?",
"Are you sure you want to remove the following installed add-ons?",
remove_ids.size()) + list_lead + utils::bullet_list(remove_names);
res = gui2::show_message(
_("Confirm")
, confirm_message
, gui2::dialogs::message::yes_no_buttons);
} while (res != gui2::window::OK);
std::set<std::string> failed_names, skipped_names, succeeded_names;
for(const std::string& id : remove_ids) {
const std::string& name = addon_titles_map[id];
if(have_addon_pbl_info(id) || have_addon_in_vcs_tree(id)) {
skipped_names.insert(name);
} else if(remove_local_addon(id)) {
succeeded_names.insert(name);
} else {
failed_names.insert(name);
}
}
if(!skipped_names.empty()) {
const std::string dlg_msg = _n(
"The following add-on appears to have publishing or version control information stored locally, and will not be removed:",
"The following add-ons appear to have publishing or version control information stored locally, and will not be removed:",
skipped_names.size());
gui2::show_error_message(
dlg_msg + list_lead + utils::bullet_list(skipped_names));
}
if(!failed_names.empty()) {
gui2::show_error_message(_n(
"The following add-on could not be deleted properly:",
"The following add-ons could not be deleted properly:",
failed_names.size()) + list_lead + utils::bullet_list(failed_names));
}
if(!succeeded_names.empty()) {
const std::string dlg_title =
_n("Add-on Deleted", "Add-ons Deleted", succeeded_names.size());
const std::string dlg_msg = _n(
"The following add-on was successfully deleted:",
"The following add-ons were successfully deleted:",
succeeded_names.size());
gui2::show_transient_message(
dlg_title,
dlg_msg + list_lead + utils::bullet_list(succeeded_names), "", false, false, true);
return true;
}
return false;
}
} // end anonymous namespace
bool manage_addons(CVideo& v)
{
static const int addon_download = 0;
// NOTE: the following two values are also known by WML, so don't change them.
static const int addon_uninstall = 2;
std::string host_name = preferences::campaign_server();
const bool have_addons = !installed_addons().empty();
gui2::dialogs::addon_connect addon_dlg(host_name, have_addons);
addon_dlg.show();
int res = addon_dlg.get_retval();
if(res == gui2::window::OK) {
res = addon_download;
}
switch(res) {
case addon_download:
return addons_manager_ui(v, host_name);
case addon_uninstall:
return uninstall_local_addons();
default:
return false;
}
}
bool ad_hoc_addon_fetch_session(CVideo& v, const std::vector<std::string>& addon_ids)
{
std::string remote_address = preferences::campaign_server();
// These exception handlers copied from addon_manager_ui fcn above.
try {
addons_client client(v, remote_address);
client.connect();
addons_list addons;
if(!get_addons_list(client, addons)) {
gui2::show_error_message(_("An error occurred while downloading the add-ons list from the server."));
return false;
}
bool return_value = true;
for(const std::string & addon_id : addon_ids) {
addons_list::const_iterator it = addons.find(addon_id);
if(it != addons.end()) {
const addon_info& addon = it->second;
addons_client::install_result res = client.install_addon_with_checks(addons, addon);
return_value = return_value && (res.outcome == addons_client::install_outcome::success);
} else {
utils::string_map symbols;
symbols["addon_id"] = addon_id;
gui2::show_error_message(vgettext("Could not find an add-on matching id $addon_id on the add-on server.", symbols));
return_value = false;
}
}
return return_value;
} catch(const config::error& e) {
ERR_CFG << "config::error thrown during transaction with add-on server; \""<< e.message << "\"" << std::endl;
gui2::show_error_message(_("Network communication error."));
} catch(const network_asio::error& e) {
ERR_NET << "network_asio::error thrown during transaction with add-on server; \""<< e.what() << "\"" << std::endl;
gui2::show_error_message(_("Remote host disconnected."));
} catch(const filesystem::io_exception& e) {
ERR_FS << "io_exception thrown while installing an addon; \"" << e.what() << "\"" << std::endl;
gui2::show_error_message(_("A problem occurred when trying to create the files necessary to install this add-on."));
} catch(const invalid_pbl_exception& e) {
ERR_CFG << "could not read .pbl file " << e.path << ": " << e.message << std::endl;
utils::string_map symbols;
symbols["path"] = e.path;
symbols["msg"] = e.message;
gui2::show_error_message(
vgettext("A local file with add-on publishing information could not be read.\n\nFile: $path\nError message: $msg", symbols));
} catch(wml_exception& e) {
e.show();
} catch(const addons_client::user_exit&) {
LOG_AC << "initial connection canceled by user\n";
} catch(const addons_client::invalid_server_address&) {
gui2::show_error_message(_("The add-ons server address specified is not valid."));
}
return false;
}