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
|
#include "lootdialog.h"
#include "ui_lootdialog.h"
#include "loot.h"
#include "organizercore.h"
#include <utility.h>
#include <expanderwidget.h>
#include <QWebChannel>
using namespace MOBase;
MarkdownDocument::MarkdownDocument(QObject* parent)
: QObject(parent)
{
}
void MarkdownDocument::setText(const QString& text)
{
if (m_text == text)
return;
m_text = text;
emit textChanged(m_text);
}
MarkdownPage::MarkdownPage(QObject* parent)
: QWebEnginePage(parent)
{
}
bool MarkdownPage::acceptNavigationRequest(const QUrl &url, NavigationType, bool)
{
static const QStringList allowed = {"qrc", "data"};
if (!allowed.contains(url.scheme())) {
QDesktopServices::openUrl(url);
return false;
}
return true;
}
LootDialog::LootDialog(QWidget* parent, OrganizerCore& core, Loot& loot) :
QDialog(parent), ui(new Ui::LootDialog), m_core(core), m_loot(loot),
m_finished(false), m_cancelling(false)
{
createUI();
QObject::connect(
&m_loot, &Loot::output, this,
[&](auto&& s){ addOutput(s); }, Qt::QueuedConnection);
QObject::connect(
&m_loot, &Loot::progress,
this, [&](auto&& p){ setProgress(p); }, Qt::QueuedConnection);
QObject::connect(
&m_loot, &Loot::log, this,
[&](auto&& lv, auto&& s){ log(lv, s); }, Qt::QueuedConnection);
QObject::connect(
&m_loot, &Loot::finished, this,
[&]{ onFinished(); }, Qt::QueuedConnection);
}
LootDialog::~LootDialog() = default;
void LootDialog::setText(const QString& s)
{
ui->progressText->setText(s);
}
void LootDialog::setProgress(lootcli::Progress p)
{
setText(progressToString(p));
if (p == lootcli::Progress::Done) {
ui->progressBar->setRange(0, 1);
ui->progressBar->setValue(1);
}
}
QString LootDialog::progressToString(lootcli::Progress p)
{
using P = lootcli::Progress;
switch (p)
{
case P::CheckingMasterlistExistence: return tr("Checking masterlist existence");
case P::UpdatingMasterlist: return tr("Updating masterlist");
case P::LoadingLists: return tr("Loading lists");
case P::ReadingPlugins: return tr("Reading plugins");
case P::SortingPlugins: return tr("Sorting plugins");
case P::WritingLoadorder: return tr("Writing loadorder.txt");
case P::ParsingLootMessages: return tr("Parsing loot messages");
case P::Done: return tr("Done");
default: return QString("unknown progress %1").arg(static_cast<int>(p));
}
}
void LootDialog::addOutput(const QString& s)
{
if (m_core.settings().diagnostics().lootLogLevel() > lootcli::LogLevels::Debug) {
return;
}
const auto lines = s.split(QRegExp("[\\r\\n]"), QString::SkipEmptyParts);
for (auto&& line : lines) {
if (line.isEmpty()) {
continue;
}
addLineOutput(line);
}
}
bool LootDialog::result() const
{
return m_loot.result();
}
void LootDialog::cancel()
{
if (!m_finished && !m_cancelling) {
addLineOutput(tr("Stopping LOOT..."));
m_loot.cancel();
ui->buttons->setEnabled(false);
m_cancelling = true;
}
}
void LootDialog::openReport()
{
const auto path = m_loot.outPath();
shell::Open(path);
}
void LootDialog::createUI()
{
ui->setupUi(this);
ui->progressBar->setMaximum(0);
auto* page = new MarkdownPage(this);
ui->report->setPage(page);
auto* channel = new QWebChannel(this);
channel->registerObject("content", &m_report);
page->setWebChannel(channel);
const QString path = QApplication::applicationDirPath() + "/resources/markdown.html";
QFile f(path);
if (f.open(QFile::ReadOnly)) {
const QString html = f.readAll();
if (!html.isEmpty()) {
ui->report->setHtml(html);
} else {
log::error("failed to read '{}', {}", path, f.errorString());
}
} else {
log::error("can't open '{}', {}", path, f.errorString());
}
ui->openJsonReport->setEnabled(false);
connect(ui->openJsonReport, &QPushButton::clicked, [&]{ openReport(); });
new ExpanderWidget(ui->details, ui->detailsPanel);
connect(ui->buttons, &QDialogButtonBox::clicked, [&](auto* b){ onButton(b); });
resize(480, 275);
}
void LootDialog::closeEvent(QCloseEvent* e)
{
if (m_finished) {
QDialog::closeEvent(e);
} else {
cancel();
e->ignore();
}
}
void LootDialog::onButton(QAbstractButton* b)
{
if (ui->buttons->buttonRole(b) == QDialogButtonBox::RejectRole) {
if (m_finished) {
close();
} else {
cancel();
}
}
}
void LootDialog::addLineOutput(const QString& line)
{
ui->output->appendPlainText(line);
}
void LootDialog::onFinished()
{
m_finished = true;
if (m_cancelling) {
close();
} else {
handleReport();
ui->openJsonReport->setEnabled(true);
ui->buttons->setStandardButtons(QDialogButtonBox::Close);
}
}
void LootDialog::log(log::Levels lv, const QString& s)
{
if (lv >= log::Levels::Warning) {
log::log(lv, "{}", s);
}
if (m_core.settings().diagnostics().lootLogLevel() > lootcli::LogLevels::Debug) {
addLineOutput(QString("[%1] %2").arg(log::levelToString(lv)).arg(s));
}
}
void LootDialog::handleReport()
{
const auto& report = m_loot.report();
if (!report.messages.empty()) {
addLineOutput("");
}
QString md;
if (!report.messages.empty()) {
for (auto&& m : report.messages) {
log(m.type, m.text);
md += " - ";
switch (m.type)
{
case log::Error:
{
md += "**" + QObject::tr("Error") + "**: ";
break;
}
case log::Warning:
{
md += "**" + QObject::tr("Warning") + "**: ";
break;
}
default:
{
break;
}
}
md += m.text + "\n";
}
} else {
md += QObject::tr("**No messages.**");
}
m_report.setText(md);
for (auto&& p : report.plugins) {
m_core.pluginList()->addLootReport(p.name, p);
}
}
|