-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathClientWidget.cpp
More file actions
315 lines (269 loc) · 9.32 KB
/
ClientWidget.cpp
File metadata and controls
315 lines (269 loc) · 9.32 KB
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
// SPDX-License-Identifier: GPL-2.0-or-later
// Copyright (C) 2019 The MMapper Authors
// Author: Nils Schimmelmann <nschimme@gmail.com> (Jahara)
#include "ClientWidget.h"
#include "../configuration/configuration.h"
#include "../global/AnsiOstream.h"
#include "../proxy/connectionlistener.h"
#include "ClientTelnet.h"
#include "PreviewWidget.h"
#include "displaywidget.h"
#include "stackedinputwidget.h"
#include "ui_ClientWidget.h"
#include <memory>
#include <QDateTime>
#include <QFileDialog>
#include <QScrollBar>
#include <QString>
#include <QTimer>
ClientWidget::ClientWidget(ConnectionListener &listener, QWidget *const parent)
: QWidget(parent)
, m_listener{listener}
{
setWindowTitle("MMapper Client");
initPipeline();
auto &ui = getUi();
// Port
ui.port->setText(QString("%1").arg(getConfig().connection.localPort));
ui.playButton->setFocus();
QObject::connect(ui.playButton, &QAbstractButton::clicked, this, [this]() {
getUi().parent->setCurrentIndex(1);
getTelnet().connectToHost(m_listener);
});
ui.input->installEventFilter(this);
ui.display->setFocusPolicy(Qt::TabFocus);
if constexpr (CURRENT_PLATFORM == PlatformEnum::Wasm) {
ui.playButton->click();
}
}
ClientWidget::~ClientWidget() = default;
void ClientWidget::playMume()
{
qDebug() << "[ClientWidget::playMume] Auto-starting client and connecting to MUME";
getUi().parent->setCurrentIndex(1);
getTelnet().connectToHost(m_listener);
}
ClientWidget::Pipeline::~Pipeline()
{
objs.clientTelnet.reset();
objs.ui.reset();
}
QSize ClientWidget::minimumSizeHint() const
{
return m_pipeline.objs.ui->display->sizeHint();
}
void ClientWidget::initPipeline()
{
m_pipeline.objs.ui = std::make_unique<Ui::ClientWidget>();
getUi().setupUi(this); // creates stacked input and display
initStackedInputWidget();
initDisplayWidget();
initClientTelnet();
}
void ClientWidget::initStackedInputWidget()
{
class NODISCARD LocalStackedInputWidgetOutputs final : public StackedInputWidgetOutputs
{
private:
ClientWidget &m_self;
public:
explicit LocalStackedInputWidgetOutputs(ClientWidget &self)
: m_self{self}
{}
private:
NODISCARD ClientWidget &getSelf() { return m_self; }
NODISCARD ClientTelnet &getTelnet() { return getSelf().getTelnet(); }
NODISCARD DisplayWidget &getDisplay() { return getSelf().getDisplay(); }
NODISCARD PreviewWidget &getPreview() { return getSelf().getPreview(); }
private:
void virt_sendUserInput(const QString &msg) final
{
if (!getTelnet().isConnected()) {
getTelnet().connectToHost(getSelf().m_listener);
} else {
getTelnet().sendToMud(msg);
}
}
void virt_displayMessage(const QString &msg) final
{
getDisplay().slot_displayText(msg);
getPreview().displayText(msg);
}
void virt_showMessage(const QString &msg, MAYBE_UNUSED int timeout) final
{
// REVISIT: Why is timeout ignored?
getSelf().slot_onShowMessage(msg);
}
void virt_requestPassword() final { getSelf().getInput().requestPassword(); }
void virt_scrollDisplay(bool pageUp) final
{
auto *scrollBar = getDisplay().verticalScrollBar();
if (scrollBar) {
int pageStep = scrollBar->pageStep();
int delta = pageUp ? -pageStep : pageStep;
scrollBar->setValue(scrollBar->value() + delta);
}
}
};
auto &out = m_pipeline.outputs.stackedInputWidgetOutputs;
out = std::make_unique<LocalStackedInputWidgetOutputs>(*this);
getInput().init(deref(out));
}
void ClientWidget::initDisplayWidget()
{
class NODISCARD LocalDisplayWidgetOutputs final : public DisplayWidgetOutputs
{
private:
ClientWidget &m_self;
public:
explicit LocalDisplayWidgetOutputs(ClientWidget &self)
: m_self{self}
{}
private:
NODISCARD ClientWidget &getSelf() { return m_self; }
NODISCARD ClientTelnet &getTelnet() { return getSelf().getTelnet(); }
private:
void virt_showMessage(const QString &msg, int /*timeout*/) final
{
getSelf().slot_onShowMessage(msg);
}
void virt_windowSizeChanged(const int width, const int height) final
{
getTelnet().onWindowSizeChanged(width, height);
}
void virt_returnFocusToInput() final { getSelf().getInput().setFocus(); }
void virt_showPreview(bool visible) final { getSelf().getPreview().setVisible(visible); }
};
auto &out = m_pipeline.outputs.displayWidgetOutputs;
out = std::make_unique<LocalDisplayWidgetOutputs>(*this);
getDisplay().init(deref(out));
}
void ClientWidget::initClientTelnet()
{
class NODISCARD LocalClientTelnetOutputs final : public ClientTelnetOutputs
{
private:
ClientWidget &m_self;
public:
explicit LocalClientTelnetOutputs(ClientWidget &self)
: m_self{self}
{}
private:
ClientWidget &getClient() { return m_self; }
DisplayWidget &getDisplay() { return getClient().getDisplay(); }
PreviewWidget &getPreview() { return getClient().getPreview(); }
StackedInputWidget &getInput() { return getClient().getInput(); }
private:
void virt_connected() final
{
getClient().relayMessage("Connected using the integrated client");
// Focus should be on the input
getInput().setFocus();
}
void virt_disconnected() final
{
getClient().displayReconnectHint();
getClient().relayMessage("Disconnected using the integrated client");
}
void virt_socketError(const QString &errorStr) final
{
getDisplay().slot_displayText(QString("\nInternal error! %1\n").arg(errorStr));
}
void virt_echoModeChanged(const bool echo) final
{
getInput().setEchoMode(echo ? EchoModeEnum::Visible : EchoModeEnum::Hidden);
}
void virt_sendToUser(const QString &str) final
{
getDisplay().slot_displayText(str);
getPreview().displayText(str);
// Re-open the password dialog if we get a message in hidden echo mode
if (getClient().getInput().getEchoMode() == EchoModeEnum::Hidden) {
getClient().getInput().requestPassword();
}
}
};
auto &out = m_pipeline.outputs.clientTelnetOutputs;
out = std::make_unique<LocalClientTelnetOutputs>(*this);
m_pipeline.objs.clientTelnet = std::make_unique<ClientTelnet>(deref(out));
}
DisplayWidget &ClientWidget::getDisplay()
{
return deref(getUi().display);
}
PreviewWidget &ClientWidget::getPreview()
{
return deref(getUi().preview);
}
StackedInputWidget &ClientWidget::getInput()
{
return deref(getUi().input);
}
ClientTelnet &ClientWidget::getTelnet() // NOLINT (no, this shouldn't be const)
{
return deref(m_pipeline.objs.clientTelnet);
}
void ClientWidget::slot_onVisibilityChanged(const bool /*visible*/)
{
if (!isUsingClient()) {
return;
}
// Delay connecting to verify that visibility is not just the dock popping back in
QTimer::singleShot(500, [this]() {
if (getTelnet().isConnected() && !isVisible()) {
// Disconnect if the widget is closed or minimized
getTelnet().disconnectFromHost();
} else if (!getTelnet().isConnected() && isVisible()) {
getInput().setFocus();
}
});
}
bool ClientWidget::isUsingClient() const
{
return getUi().parent->currentIndex() != 0;
}
void ClientWidget::displayReconnectHint()
{
constexpr const auto whiteOnCyan = getRawAnsi(AnsiColor16Enum::white, AnsiColor16Enum::cyan);
std::stringstream oss;
AnsiOstream aos{oss};
aos.writeWithColor(whiteOnCyan, "\n\n\nPress return to reconnect.\n");
getDisplay().slot_displayText(mmqt::toQStringUtf8(oss.str()));
}
void ClientWidget::slot_onShowMessage(const QString &message)
{
relayMessage(message);
}
void ClientWidget::slot_saveLog()
{
const auto getDocStringUtf8 = [](const QTextDocument *const pDoc) -> QByteArray {
auto &doc = deref(pDoc);
const QString string = doc.toPlainText();
return string.toUtf8();
};
const QByteArray logContent = getDocStringUtf8(getDisplay().document());
QString newFileName = "log-" + QDateTime::currentDateTime().toString("yyyyMMdd-hhmmss")
+ ".txt";
QFileDialog::saveFileContent(logContent, newFileName);
}
void ClientWidget::slot_saveLogAsHtml()
{
const auto getDocStringUtf8 = [](const QTextDocument *const pDoc) -> QByteArray {
auto &doc = deref(pDoc);
const QString string = doc.toHtml();
return string.toUtf8();
};
const QByteArray logContent = getDocStringUtf8(getDisplay().document());
QString newFileNameHtml = "log-" + QDateTime::currentDateTime().toString("yyyyMMdd-hhmmss")
+ ".html";
QFileDialog::saveFileContent(logContent, newFileNameHtml);
}
bool ClientWidget::focusNextPrevChild(MAYBE_UNUSED bool next)
{
if (getInput().hasFocus()) {
getDisplay().setFocus();
} else {
getInput().setFocus();
}
return true;
}