/
guishellapp.cpp
445 lines (378 loc) · 12.1 KB
/
guishellapp.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/** @file shellapp.cpp Shell GUI application.
*
* @authors Copyright © 2013-2019 Jaakko Keränen <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details. You should have received a copy of the GNU
* General Public License along with this program; if not, see:
* http://www.gnu.org/licenses</small>
*/
#include "guishellapp.h"
#include "aboutdialog.h"
#include "linkwindow.h"
#include "localserverdialog.h"
#include "opendialog.h"
#include "preferences.h"
#include <de/Config>
#include <de/EscapeParser>
#include <de/FileSystem>
#include <de/Id>
#include <de/PackageLoader>
#include <de/ServerFinder>
#include <de/Timer>
#include <de/WindowSystem>
#include <doomsday/network/LocalServer>
#include <SDL_messagebox.h>
using namespace de;
using namespace network;
DE_PIMPL(GuiShellApp)
, DE_OBSERVES(ServerFinder, Update)
{
ServerFinder finder;
ImageBank imageBank;
// QMenuBar *menuBar;
// QMenu *localMenu;
PopupMenuWidget *localMenu;
#ifdef MACOSX
// QAction *stopAction;
// QAction *disconnectAction;
#endif
// QList<LinkWindow *> windows;
Hash<int, LocalServer *> localServers; // port as key
Timer localCheckTimer;
ui::ListDataT<ui::ActionItem> localServerMenuItems;
Preferences *prefs;
Impl(Public *i) : Base(i), prefs(nullptr)
{
localCheckTimer.setInterval(1.0_s);
localCheckTimer.setSingleShot(false);
finder.audienceForUpdate() += this;
}
~Impl() override
{
self().glDeinit();
}
void foundServersUpdated() override
{
DE_ASSERT(inMainThread());
const auto found = finder.foundServers();
// Add new servers.
for (const auto &sv : found)
{
}
// Remove servers that are not present.
}
void loadAllShaders()
{
// Load all the shader program definitions.
FS::FoundFiles found;
self().findInPackages("shaders.dei", found);
DE_FOR_EACH(FS::FoundFiles, i, found)
{
LOG_MSG("Loading shader definitions from %s") << (*i)->description();
self().shaders().addFromInfo(**i);
}
}
DE_PIMPL_AUDIENCE(LocalServerStop)
};
DE_AUDIENCE_METHOD(GuiShellApp, LocalServerStop)
GuiShellApp::GuiShellApp(const StringList &args)
: BaseGuiApp(args)
, d(new Impl(this))
{
// Application metadata.
{
auto &md = metadata();
md.set(ORG_DOMAIN, "dengine.net");
md.set(ORG_NAME, "Deng Team");
md.set(APP_NAME, "Shell");
md.set(APP_VERSION, SHELL_VERSION);
}
// d->localMenu = new QMenu(tr("Running Servers"));
// connect(d->localMenu, SIGNAL(aboutToShow()), this, SLOT(updateLocalServerMenu()));
#ifdef MACOSX
// setQuitOnLastWindowClosed(false);
// // On macOS, the menu is not window-specific.
// d->menuBar = new QMenuBar(0);
// QMenu *menu = d->menuBar->addMenu(tr("Connection"));
// menu->addAction(tr("Connect..."), this, SLOT(connectToServer()),
// QKeySequence(tr("Ctrl+O", "Connection|Connect")));
// d->disconnectAction = menu->addAction(tr("Disconnect"), this, SLOT(disconnectFromServer()),
// QKeySequence(tr("Ctrl+D", "Connection|Disconnect")));
// d->disconnectAction->setDisabled(true);
// menu->addSeparator();
// menu->addAction(tr("Close Window"), this, SLOT(closeActiveWindow()),
// QKeySequence(tr("Ctrl+W", "Connection|Close Window")));
// QMenu *svMenu = d->menuBar->addMenu(tr("Server"));
// svMenu->addAction(tr("New Local Server..."), this, SLOT(startLocalServer()),
// QKeySequence(tr("Ctrl+N", "Server|New Local Server")));
// d->stopAction = svMenu->addAction(tr("Stop"), this, SLOT(stopServer()));
// svMenu->addSeparator();
// svMenu->addMenu(d->localMenu);
// connect(menu, SIGNAL(aboutToShow()), this, SLOT(updateMenu()));
// connect(svMenu, SIGNAL(aboutToShow()), this, SLOT(updateMenu()));
// // These will appear in the application menu:
// menu->addAction(tr("Preferences..."), this, SLOT(showPreferences()), QKeySequence(tr("Ctrl+,")));
// menu->addAction(tr("About"), this, SLOT(aboutShell()));
// d->menuBar->addMenu(makeHelpMenu());
#endif
d->localCheckTimer.audienceForTrigger() += [this]() { checkLocalServers(); };
d->localCheckTimer.start();
// newOrReusedConnectionWindow();
}
void GuiShellApp::initialize()
{
addInitPackage("net.dengine.shell");
initSubsystems();
windowSystem().style().load(packageLoader().package("net.dengine.stdlib.gui"));
d->imageBank.addFromInfo(FS::locate<const File>("/packs/net.dengine.shell/images.dei"));
d->loadAllShaders();
}
void GuiShellApp::quitRequested()
{
if (countOpenConnections() == 1)
{
// The window will ask for confirmation when receiving a close event.
return;
}
// Too many or no open connections, so just quit without asking.
GuiApp::quitRequested();
}
LinkWindow *GuiShellApp::newOrReusedConnectionWindow()
{
LinkWindow *found = nullptr;
// Look for a window with a closed connection.
windowSystem().forAll([&found](GLWindow &w) {
auto &win = w.as<LinkWindow>();
if (!win.isConnected())
{
found = &win;
found->raise();
return LoopAbort;
}
return LoopContinue;
});
if (!found)
{
found = windowSystem().newWindow<LinkWindow>(Stringf("link%04u", windowSystem().count()));
// connect(found, SIGNAL(linkOpened(LinkWindow*)),this, SLOT(updateMenu()));
// connect(found, SIGNAL(linkClosed(LinkWindow*)), this, SLOT(updateMenu()));
// connect(found, SIGNAL(closed(LinkWindow *)), this, SLOT(windowClosed(LinkWindow *)));
found->show();
// Initial position and size.
// if (other)
// {
// found->move(other->pos() + QPoint(30, 30));
// }
}
windowSystem().setFocusedWindow(found->id());
// d->windows.prepend(found);
// found->show();
return found;
}
int GuiShellApp::countOpenConnections() const
{
int count = 0;
windowSystem().forAll([&count](GLWindow &w) {
if (auto *win = maybeAs<LinkWindow>(&w))
{
if (win->isConnected()) ++count;
}
return LoopContinue;
});
return count;
}
GuiShellApp &GuiShellApp::app()
{
return *static_cast<GuiShellApp *>(DE_BASE_GUI_APP);
}
PopupMenuWidget &GuiShellApp::localServersMenu()
{
return *d->localMenu;
}
//QMenu *GuiShellApp::makeHelpMenu()
//{
// QMenu *helpMenu = new QMenu(tr("&Help"));
// helpMenu->addAction(tr("Shell Help"), this, SLOT(showHelp()));
// return helpMenu;
//}
ServerFinder &GuiShellApp::serverFinder()
{
return d->finder;
}
void GuiShellApp::connectToServer()
{
LinkWindow *win = newOrReusedConnectionWindow();
OpenDialog *dlg = new OpenDialog;
dlg->setDeleteAfterDismissed(true);
if (dlg->exec(win->root()))
{
win->openConnection(dlg->address());
}
}
void GuiShellApp::connectToLocalServer()
{
// QAction *act = dynamic_cast<QAction *>(sender());
// Address host = act->data().value<Address>();
// LinkWindow *win = newOrReusedConnectionWindow();
// win->openConnection(convert(host.asText()));
}
void GuiShellApp::disconnectFromServer()
{
// LinkWindow *win = dynamic_cast<LinkWindow *>(activeWindow());
// if (win)
// {
// win->closeConnection();
// }
}
void GuiShellApp::closeActiveWindow()
{
// QWidget *win = activeWindow();
// if (win) win->close();
}
void GuiShellApp::startLocalServer()
{
try
{
#ifdef MACOSX
// App folder randomization means we can't find Doomsday.app on our own.
if (!Config::get().has("Preferences.appFolder"))
{
showPreferences();
return;
}
#endif
auto *win = &windowSystem().focusedWindow()->as<LinkWindow>();
auto *dlg = new LocalServerDialog;
dlg->setDeleteAfterDismissed(true);
if (dlg->exec(win->root()))
{
StringList opts = dlg->additionalOptions();
if (!Preferences::iwadFolder().isEmpty())
{
// TODO: Make the subdir recursion a setting.
opts << "-iwadr" << Preferences::iwadFolder();
}
auto *sv = new LocalServer;
sv->setApplicationPath(Config::get().gets("Preferences.appFolder"));
if (!dlg->name().isEmpty())
{
sv->setName(dlg->name());
}
sv->start(dlg->port(),
dlg->gameMode(),
opts,
dlg->runtimeFolder());
d->localServers[dlg->port()] = sv;
newOrReusedConnectionWindow()->waitForLocalConnection(
dlg->port(), sv->errorLogPath(), dlg->name());
}
}
catch (const Error &er)
{
EscapeParser esc;
esc.parse(er.asText());
SDL_MessageBoxData mbox{};
mbox.title = "Failed to Start Server";
mbox.message = esc.plainText();
SDL_ShowMessageBox(&mbox, nullptr);
showPreferences();
}
}
void GuiShellApp::updateLocalServerMenu()
{
// d->localMenu->setDisabled(d->finder.foundServers().isEmpty());
// d->localMenu->clear();
// foreach (const Address &host, d->finder.foundServers())
// {
// QString label = QString("%1 - %2 (%3/%4)")
// .arg(host.asText().c_str())
// .arg(d->finder.name(host).c_str())
// .arg(d->finder.playerCount(host))
// .arg(d->finder.maxPlayers(host));
// QAction *act = d->localMenu->addAction(label, this, SLOT(connectToLocalServer()));
// act->setData(QVariant::fromValue(host));
// }
}
void GuiShellApp::aboutShell()
{
auto &win = windowSystem().focusedWindow()->as<LinkWindow>();
auto *about = new AboutDialog;
about->setDeleteAfterDismissed(true);
about->exec(win.root());
}
void GuiShellApp::showHelp()
{
openBrowserUrl("https://manual.dengine.net/multiplayer/shell_help");
}
void GuiShellApp::openWebAddress(const String &url)
{
openBrowserUrl(url);
}
void GuiShellApp::showPreferences()
{
LinkWindow &win = windowSystem().focusedWindow()->as<LinkWindow>();
auto *prefs = new Preferences;
prefs->setDeleteAfterDismissed(true);
prefs->exec(win.root());
// if (!d->prefs)
// {
// d->prefs = new Preferences;
// connect(d->prefs, SIGNAL(finished(int)), this, SLOT(preferencesDone()));
// foreach (LinkWindow *win, d->windows)
// {
// connect(d->prefs, SIGNAL(consoleFontChanged()), win, SLOT(updateConsoleFontFromPreferences()));
// }
// d->prefs->show();
// }
// else
// {
// d->prefs->activateWindow();
// }
}
void GuiShellApp::updateMenu()
{
#ifdef MACOSX
// LinkWindow *win = dynamic_cast<LinkWindow *>(activeWindow());
// d->stopAction->setEnabled(win && win->isConnected());
// d->disconnectAction->setEnabled(win && win->isConnected());
#endif
updateLocalServerMenu();
}
void GuiShellApp::checkLocalServers()
{
List<int> stoppedPorts;
for (auto iter = d->localServers.begin(); iter != d->localServers.end(); )
{
LocalServer *sv = iter->second;
if (!sv->isRunning())
{
stoppedPorts << iter->first;
delete sv;
iter = d->localServers.erase(iter);
}
else
{
++iter;
}
}
for (int port : stoppedPorts)
{
DE_NOTIFY(LocalServerStop, i)
{
i->localServerStopped(port);
}
}
}
ImageBank &GuiShellApp::imageBank()
{
return app().d->imageBank;
}