1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-15 18:19:55 +00:00
openmw-tes3mp/apps/browser/QueryHelper.cpp

87 lines
2.4 KiB
C++
Raw Normal View History

//
// Created by koncord on 27.05.17.
//
#include "netutils/QueryClient.hpp"
#include "netutils/Utils.hpp"
#include "QueryHelper.hpp"
#include "PingHelper.hpp"
QueryUpdate *queryUpdate;
2017-05-27 05:08:52 +00:00
QueryHelper::QueryHelper(QAbstractItemModel *model)
{
2017-05-27 05:08:52 +00:00
qRegisterMetaType<QueryData>("QueryData");
queryThread = new QThread;
queryUpdate = new QueryUpdate;
2017-05-27 05:08:52 +00:00
_model = model;
connect(queryThread, SIGNAL(started()), queryUpdate, SLOT(process()));
connect(queryUpdate, SIGNAL(finished()), queryThread, SLOT(quit()));
connect(queryUpdate, &QueryUpdate::finished, [this](){emit finished();});
2017-05-27 05:08:52 +00:00
connect(queryUpdate, SIGNAL(updateModel(QString, unsigned short, QueryData)),
this, SLOT(update(QString, unsigned short, QueryData)));
queryUpdate->moveToThread(queryThread);
}
void QueryHelper::refresh()
{
if (!queryThread->isRunning())
{
_model->removeRows(0, _model->rowCount());
queryThread->start();
emit started();
}
}
void QueryHelper::terminate()
{
queryThread->terminate();
}
2017-05-27 05:08:52 +00:00
void QueryHelper::update(QString addr, unsigned short port, QueryData data)
{
ServerModel *model = ((ServerModel*)_model);
2017-05-27 05:08:52 +00:00
model->insertRow(model->rowCount());
int row = model->rowCount() - 1;
2017-05-27 05:08:52 +00:00
QModelIndex mi = model->index(row, ServerData::ADDR);
model->setData(mi, addr + ":" + QString::number(port));
2017-05-27 05:08:52 +00:00
mi = model->index(row, ServerData::PLAYERS);
model->setData(mi, (int)data.players.size());
2017-05-27 05:08:52 +00:00
mi = model->index(row, ServerData::MAX_PLAYERS);
model->setData(mi, data.GetMaxPlayers());
2017-05-27 05:08:52 +00:00
mi = model->index(row, ServerData::HOSTNAME);
model->setData(mi, data.GetName());
2017-05-27 05:08:52 +00:00
mi = model->index(row, ServerData::MODNAME);
model->setData(mi, data.GetGameMode());
2017-05-27 05:08:52 +00:00
mi = model->index(row, ServerData::VERSION);
model->setData(mi, data.GetVersion());
2017-05-27 05:08:52 +00:00
mi = model->index(row, ServerData::PASSW);
model->setData(mi, data.GetPassword() == 1);
2017-05-27 05:08:52 +00:00
mi = model->index(row, ServerData::PING);
model->setData(mi, PING_UNREACHABLE);
PingHelper::Get().Add(row, {addr, port});
}
2017-05-27 05:08:52 +00:00
void QueryUpdate::process()
{
auto data = QueryClient::Get().Query();
if (QueryClient::Get().Status() != ID_MASTER_QUERY)
2017-05-27 05:08:52 +00:00
{
emit finished();
return;
}
2017-05-27 05:08:52 +00:00
for (auto server : data)
2017-05-27 05:08:52 +00:00
emit updateModel(server.first.ToString(false), server.first.GetPort(), server.second);
emit finished();
}