[Browser] Move downloading query info to another thread
parent
e1766da83c
commit
c8eb0f0eae
@ -0,0 +1,72 @@
|
||||
//
|
||||
// Created by koncord on 27.05.17.
|
||||
//
|
||||
|
||||
#include "netutils/QueryClient.hpp"
|
||||
#include "netutils/Utils.hpp"
|
||||
#include "QueryHelper.hpp"
|
||||
#include "PingHelper.hpp"
|
||||
|
||||
QueryUpdate *queryUpdate;
|
||||
|
||||
QueryHelper::QueryHelper(QAbstractItemModel *model)
|
||||
{
|
||||
queryThread = new QThread;
|
||||
queryUpdate = new QueryUpdate;
|
||||
queryUpdate->_model = model;
|
||||
connect(queryThread, SIGNAL(started()), queryUpdate, SLOT(process()));
|
||||
connect(queryUpdate, SIGNAL(finished()), queryThread, SLOT(quit()));
|
||||
queryUpdate->moveToThread(queryThread);
|
||||
}
|
||||
|
||||
void QueryHelper::refresh()
|
||||
{
|
||||
queryThread->start();
|
||||
}
|
||||
|
||||
void QueryHelper::terminate()
|
||||
{
|
||||
queryThread->terminate();
|
||||
}
|
||||
|
||||
void QueryUpdate::process()
|
||||
{
|
||||
auto data = QueryClient::Get().Query();
|
||||
if(QueryClient::Get().Status() != ID_MASTER_QUERY)
|
||||
return;
|
||||
|
||||
ServerModel *model = ((ServerModel*)_model);
|
||||
model->removeRows(0, model->rowCount());
|
||||
for(auto server : data)
|
||||
{
|
||||
model->insertRow(model->rowCount());
|
||||
int row = model->rowCount() - 1;
|
||||
|
||||
QModelIndex mi = model->index(row, ServerData::ADDR);
|
||||
model->setData(mi, server.first.ToString(true, ':'));
|
||||
|
||||
mi = model->index(row, ServerData::PLAYERS);
|
||||
model->setData(mi, (int)server.second.players.size());
|
||||
|
||||
mi = model->index(row, ServerData::MAX_PLAYERS);
|
||||
model->setData(mi, server.second.GetMaxPlayers());
|
||||
|
||||
mi = model->index(row, ServerData::HOSTNAME);
|
||||
model->setData(mi, server.second.GetName());
|
||||
|
||||
mi = model->index(row, ServerData::MODNAME);
|
||||
model->setData(mi, server.second.GetGameMode());
|
||||
|
||||
mi = model->index(row, ServerData::VERSION);
|
||||
model->setData(mi, server.second.GetVersion());
|
||||
|
||||
mi = model->index(row, ServerData::PASSW);
|
||||
model->setData(mi, server.second.GetPassword() == 1);
|
||||
|
||||
mi = model->index(row, ServerData::PING);
|
||||
model->setData(mi, PING_UNREACHABLE);
|
||||
|
||||
PingHelper::Get().Add(row, {server.first.ToString(false), server.first.GetPort()});
|
||||
}
|
||||
emit finished();
|
||||
}
|
@ -0,0 +1,37 @@
|
||||
//
|
||||
// Created by koncord on 27.05.17.
|
||||
//
|
||||
|
||||
|
||||
#ifndef OPENMW_QUERYHELPER_HPP
|
||||
#define OPENMW_QUERYHELPER_HPP
|
||||
|
||||
|
||||
#include <QObject>
|
||||
#include <QAbstractItemModel>
|
||||
|
||||
class QueryHelper : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit QueryHelper(QAbstractItemModel *model);
|
||||
public slots:
|
||||
void refresh();
|
||||
void terminate();
|
||||
private:
|
||||
QThread *queryThread;
|
||||
};
|
||||
|
||||
class QueryUpdate : public QObject
|
||||
{
|
||||
friend class QueryHelper;
|
||||
Q_OBJECT
|
||||
signals:
|
||||
void finished();
|
||||
public slots:
|
||||
void process();
|
||||
private:
|
||||
QAbstractItemModel *_model;
|
||||
};
|
||||
|
||||
#endif //OPENMW_QUERYHELPER_HPP
|
Loading…
Reference in New Issue