24 #include "connectionhandler.h" 26 #include "application.h" 27 #include "apprunner.h" 28 #include "configuration/doomseekerconfig.h" 30 #include "gui/configuration/doomseekerconfigurationdialog.h" 31 #include "gui/mainwindow.h" 32 #include "joincommandlinebuilder.h" 34 #include "plugins/engineplugin.h" 35 #include "plugins/pluginloader.h" 36 #include "refresher/canrefreshserver.h" 37 #include "refresher/refresher.h" 38 #include "serverapi/gameclientrunner.h" 39 #include "serverapi/message.h" 40 #include "serverapi/server.h" 41 #include "strings.hpp" 42 #include <QDesktopServices> 43 #include <QMessageBox> 47 DClass<ConnectionHandler>
57 QWidget *parentWidget;
63 : QObject(parentWidget)
65 d->forceRefresh =
false;
67 d->parentWidget = parentWidget;
70 ConnectionHandler::~ConnectionHandler()
74 void ConnectionHandler::checkResponse(
const ServerPtr &server,
int response)
76 server->disconnect(
this);
82 QMessageBox::critical(d->parentWidget, tr(
"Doomseeker - join server"),
83 tr(
"Connection to server timed out."));
86 QMessageBox::critical(d->parentWidget, tr(
"Doomseeker - join server"),
87 tr(
"An error occured while trying to connect to server."));
98 QTimer::singleShot(0,
this, SLOT(buildJoinCommandLine()));
103 gLog << QString(
"Attempting to connect to server: %1").arg(url.toString());
108 bool zdsScheme = url.scheme().compare(
"zds", Qt::CaseInsensitive) == 0;
109 for (
unsigned int i = 0; i < gPlugins->numPlugins(); ++i)
111 const EnginePlugin *plugin = gPlugins->plugin(i)->info();
112 if (plugin->data()->scheme.compare(url.scheme(), Qt::CaseInsensitive) == 0 ||
113 (zdsScheme && plugin->data()->scheme.left(2).compare(url.path().mid(1), Qt::CaseInsensitive) == 0))
119 if (handler ==
nullptr)
121 gLog <<
"Scheme not recognized starting normally.";
125 unsigned short port = url.port(handler->data()->defaultServerPort);
132 ServerPtr server = handler->
server(QHostAddress(address), port);
134 connectionHandler->d->forceRefresh =
true;
136 return connectionHandler;
139 void ConnectionHandler::finish(
int response)
141 emit finished(response);
144 void ConnectionHandler::buildJoinCommandLine()
147 gConfig.doomseeker.bRecordDemo ? GameDemo::Managed : GameDemo::NoDemo,
149 this->connect(builder, SIGNAL(commandLineBuildFinished()), SLOT(onCommandLineBuildFinished()));
153 void ConnectionHandler::onCommandLineBuildFinished()
158 runCommandLine(builtCli);
161 if (!builder->error().isEmpty())
162 QMessageBox::critical(d->parentWidget, tr(
"Doomseeker - join game"), builder->error());
163 if (builder->isConfigurationError())
164 DoomseekerConfigurationDialog::openConfiguration(gApp->mainWindow(), d->server->plugin());
166 builder->deleteLater();
172 Message message = AppRunner::runExecutable(cli);
175 gLog << tr(R
"(Error while launching executable for server "%1", game "%2": %3)") 176 .arg(d->server->name()).arg(d->server->engineName()).arg(message.contents()); 177 QMessageBox::critical(d->parentWidget, tr("Doomseeker - launch executable"), message.
contents());
181 void ConnectionHandler::refreshToJoin()
186 if (d->forceRefresh || (refreshCheck.shouldRefresh() && gConfig.doomseeker.bQueryBeforeLaunch))
188 this->connect(d->server.data(), SIGNAL(updated(ServerPtr,
int)),
189 SLOT(checkResponse(ServerPtr,
int)));
190 gRefresher->registerServer(d->server.data());
196 void ConnectionHandler::run()
205 void PluginUrlHandler::registerAll()
207 for (
unsigned int i = 0; i < gPlugins->numPlugins(); ++i)
208 registerScheme(gPlugins->plugin(i)->info()->data()->scheme);
211 registerScheme(
"zds");
214 void PluginUrlHandler::registerScheme(
const QString &scheme)
219 QDesktopServices::setUrlHandler(scheme, instance,
"handleUrl");
222 void PluginUrlHandler::handleUrl(
const QUrl &url)
224 if (QMessageBox::question(
nullptr, tr(
"Connect to server"),
225 tr(
"Do you want to connect to the server at %1?").arg(url.toString()),
226 QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes)
228 ConnectionHandler::connectByUrl(url);
static void translateServerAddress(const QString &addressString, QString &hostname, unsigned short &port, const QString &defaultAddress)
Translates string in format "hostname:port" to atomic values.
Structure holding parameters for application launch.
Message object used to pass messages throughout the Doomseeker's system.
bool isError() const
True if type() is equal to or greater than CUSTOM_ERROR.
Generates command line for joining specified server.
virtual ServerPtr server(const QHostAddress &address, unsigned short port) const
Creates an instance of server object from this plugin.
void obtainJoinCommandLine()
Runs asynchronously and emits commandLineBuildFinished() when done.
Response was parsed properly and Server information is available.
Server didn't respond at all.
QString contents() const
Customized displayable contents of this Message.
bool isValid() const
It's valid when at least executable is set.