-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
wml_error.cpp
231 lines (190 loc) · 6.07 KB
/
wml_error.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
/*
Copyright (C) 2014 by Ignacio R. 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.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/dialogs/wml_error.hpp"
#include "addon/info.hpp"
#include "addon/manager.hpp"
#include "desktop/clipboard.hpp"
#include "filesystem.hpp"
#include "gettext.hpp"
#include "gui/auxiliary/find_widget.tpp"
#include "gui/widgets/button.hpp"
#include "gui/widgets/control.hpp"
#include "gui/widgets/settings.hpp"
#include "gui/widgets/window.hpp"
#include "serialization/string_utils.hpp"
#include <boost/bind.hpp>
namespace
{
void strip_trailing_dir_separators(std::string& str)
{
while(filesystem::is_path_sep(str[str.size() - 1])) {
str.erase(str.size() - 1);
}
}
std::string format_file_list(const std::vector<std::string>& files_original)
{
if(files_original.empty()) {
return "";
}
const std::string& addons_path = filesystem::get_addons_dir();
std::vector<std::string> files(files_original);
BOOST_FOREACH(std::string & file, files)
{
std::string base;
std::string filename = filesystem::base_name(file);
std::string parent_path;
const bool is_main_cfg = filename == "_main.cfg";
if(is_main_cfg) {
parent_path = filesystem::directory_name(file) + "/..";
} else {
parent_path = filesystem::directory_name(file);
}
// Only proceed to pretty-format the filename if it's from the add-ons
// directory.
if(filesystem::normalize_path(parent_path) != filesystem::normalize_path(addons_path)) {
continue;
}
if(is_main_cfg) {
base = filesystem::directory_name(file);
// HACK: fool filesystem::base_name() into giving us the parent directory name
// alone by making base seem not like a directory path,
// otherwise it returns an empty string.
strip_trailing_dir_separators(base);
base = filesystem::base_name(base);
} else {
base = filename;
}
if(base.empty()) {
// We did something wrong. In the interest of not messing up the
// report, leave the original filename intact.
continue;
}
//
// Display the name as an add-on name instead of a filename.
//
if(!is_main_cfg) {
// Remove the file extension first.
static const std::string wml_suffix = ".cfg";
if(base.size() > wml_suffix.size()) {
const size_t suffix_pos = base.size() - wml_suffix.size();
if(base.substr(suffix_pos) == wml_suffix) {
base.erase(suffix_pos);
}
}
}
if(have_addon_install_info(base)) {
// _info.cfg may have the add-on's title starting with 1.11.7,
// if the add-on was downloaded using the revised _info.cfg writer.
config cfg;
get_addon_install_info(base, cfg);
const config& info_cfg = cfg.child("info");
if(info_cfg && !info_cfg["title"].empty()) {
file = info_cfg["title"].str();
continue;
}
}
// Fallback to using a synthetic title with underscores replaced with
// whitespace.
file = make_addon_title(base);
}
if(files.size() == 1) {
return utils::indent(files.front());
}
return utils::bullet_list(files);
}
}
namespace gui2
{
/*WIKI
* @page = GUIWindowDefinitionWML
* @order = 2_wml_error
*
* == WML error ==
*
* Dialog used to report WML parser or preprocessor errors.
*
* @begin{table}{dialog_widgets}
*
* summary & & control & m &
* Label used for displaying a brief summary of the error(s). $
*
* files & & control & m &
* Label used to display the list of affected add-ons or files, if
* applicable. It is hidden otherwise. It is recommended to place it
* after the summary label. $
*
* post_summary & & control & m &
* Label used for displaying instructions for reporting the error.
* It is recommended to place it after the file list label. It may be
* hidden if empty. $
*
* details & & control & m &
* Full report of the parser or preprocessor error(s) found. $
*
* copy & & button & m &
* Button that the user can click on to copy the error report to the
* system clipboard. $
*
* @end{table}
*/
REGISTER_DIALOG(wml_error)
twml_error::twml_error(const std::string& summary,
const std::string& post_summary,
const std::vector<std::string>& files,
const std::string& details)
: have_files_(!files.empty())
, have_post_summary_(!post_summary.empty())
, report_()
{
const std::string& file_list_text = format_file_list(files);
report_ = summary;
if(!file_list_text.empty()) {
report_ += "\n" + file_list_text;
}
if(!post_summary.empty()) {
report_ += "\n\n" + post_summary;
}
report_ += "\n\n";
report_ += _("Details:");
report_ += "\n\n" + utils::indent(details);
// Since this is likely to be pasted into a text file, add a final line
// break for convenience, since otherwise the report ends mid-line.
report_ += "\n";
register_label("summary", true, summary);
register_label("post_summary", true, post_summary);
register_label("files", true, file_list_text);
register_label("details", true, details);
}
void twml_error::pre_show(CVideo& /*video*/, twindow& window)
{
if(!have_files_) {
tcontrol& filelist = find_widget<tcontrol>(&window, "files", false);
filelist.set_visible(tcontrol::tvisible::invisible);
}
if(!have_post_summary_) {
tcontrol& post_summary
= find_widget<tcontrol>(&window, "post_summary", false);
post_summary.set_visible(tcontrol::tvisible::invisible);
}
tbutton& copy_button = find_widget<tbutton>(&window, "copy", false);
connect_signal_mouse_left_click(
copy_button, boost::bind(&twml_error::copy_report_callback, this));
if (!desktop::clipboard::available()) {
copy_button.set_active(false);
}
}
void twml_error::copy_report_callback()
{
desktop::clipboard::copy_to_clipboard(report_, false);
}
} // end namespace gui2