23 #include "serverlistrowhandler.h"
24 #include "serverlistcolumn.h"
25 #include "serverlistmodel.h"
26 #include "configuration/doomseekerconfig.h"
27 #include "gui/helpers/playersdiagram.h"
28 #include "gui/widgets/serverlistview.h"
29 #include "gui/dockBuddiesList.h"
30 #include "gui/mainwindow.h"
31 #include "gui/serverlist.h"
32 #include "ip2c/ip2c.h"
33 #include "serverapi/playerslist.h"
34 #include "serverapi/server.h"
36 #include "application.h"
40 using namespace ServerListColumnId;
42 DClass<ServerListRowHandler>
51 int rowIndex, const ServerPtr &server)
52 : model(parentModel), row(rowIndex)
57 ServerListRowHandler::ServerListRowHandler(
ServerListModel* parentModel,
int rowIndex)
58 : model(parentModel), row(rowIndex)
60 d->server = serverFromList(parentModel, rowIndex);
63 ServerListRowHandler::~ServerListRowHandler()
69 for (
int i = 0; i < NUM_SERVERLIST_COLUMNS; ++i)
71 if(!ServerListColumns::isColumnVital(i))
78 void ServerListRowHandler::emptyItem(QStandardItem* item)
80 item->setData(
"", Qt::DisplayRole);
81 item->setData(QVariant(), Qt::DecorationRole);
82 item->setData(QVariant(), DTSort);
85 QStringList ServerListRowHandler::extractValidGameCVarNames(
const QList<GameCVar> &cvars)
88 foreach (
const GameCVar &cvar, cvars)
92 result << cvar.
name();
98 void ServerListRowHandler::fillAddressColumn()
100 QStandardItem* pItem = item(IDAddress);
101 fillItem(pItem, d->server->address(), d->server->hostName() );
104 void ServerListRowHandler::fillItem(QStandardItem* item,
const QString& str)
106 QString strLowcase = str.toLower();
107 item->setData(str, Qt::DisplayRole);
108 item->setData(strLowcase, DTSort);
111 void ServerListRowHandler::fillItem(QStandardItem* item,
int sort,
const QString& str)
116 item->setData(sort, DTSort);
119 void ServerListRowHandler::fillItem(QStandardItem* item,
int num)
123 item->setData(var, Qt::DisplayRole);
124 item->setData(var, DTSort);
127 void ServerListRowHandler::fillItem(QStandardItem* item,
const QHostAddress& addr,
const QString& actualDisplay)
129 QVariant var = addr.toIPv4Address();
131 if (actualDisplay.isEmpty())
133 item->setData(addr.toString(), Qt::DisplayRole);
137 item->setData(actualDisplay, Qt::DisplayRole);
139 item->setData(var, DTSort);
142 void ServerListRowHandler::fillItem(QStandardItem* item,
const QString& sort,
const QPixmap& icon)
144 item->setIcon(QIcon(icon));
145 item->setData(sort, DTSort);
148 void ServerListRowHandler::fillItem(QStandardItem* item,
int sort,
const QPixmap& image)
150 item->setData(image, Qt::DecorationRole);
151 item->setData(sort, DTSort);
154 void ServerListRowHandler::fillPlayerColumn()
156 QStandardItem* pItem = item(IDPlayers);
158 int style = gConfig.doomseeker.slotStyle;
159 bool botsAreNotPlayers = gConfig.doomseeker.bBotsAreNotPlayers;
164 if (botsAreNotPlayers)
166 sortValue = players.numClientsWithoutBots();
173 if(style != NUM_SLOTSTYLES)
179 fillItem(pItem, sortValue, QString(
"%1/%2").arg(players.
numClients())
180 .arg(d->server->numTotalSlots()));
184 pItem->setData(QVariant(QVariant::Invalid), style == NUM_SLOTSTYLES ? Qt::DecorationRole : Qt::DisplayRole);
185 pItem->setData(style == NUM_SLOTSTYLES ? 0 : USERROLE_RIGHTALIGNDECORATION, Qt::UserRole);
188 void ServerListRowHandler::fillPortIconColumn()
190 QStandardItem* pItem = item(IDPort);
191 QPixmap icon = d->server->icon();
192 if(d->server->isKnown())
194 if(d->server->isLockedAnywhere())
196 QPainter iconPainter(&icon);
197 iconPainter.drawPixmap(0, 0, QPixmap(
":/locked.png"));
200 else if(d->server->isSecure())
202 QPainter iconPainter(&icon);
203 iconPainter.drawPixmap(0, 0, QPixmap(
":/shield.png"));
207 fillItem(pItem, d->server->metaObject()->className(), icon);
210 void ServerListRowHandler::fillServerPointerColumn()
212 QStandardItem* pItem = item(IDHiddenServerPointer);
213 QVariant savePointer = qVariantFromValue(d->server);
214 pItem->setData(savePointer, DTPointerToServerStructure);
217 QStandardItem* ServerListRowHandler::item(
int columnIndex)
219 return model->item(row, columnIndex);
222 void ServerListRowHandler::redraw()
224 updateServer(d->server->lastResponse());
231 ServerPtr ServerListRowHandler::server()
236 void ServerListRowHandler::setBackgroundColor()
239 if (d->server->isCustom())
241 color = gConfig.doomseeker.customServersColor;
243 else if (gApp->mainWindow()->buddiesList()->hasBuddy(d->server))
245 if (gConfig.doomseeker.bMarkServersWithBuddies)
247 color = gConfig.doomseeker.buddyServersColor;
251 for (
int column = 0; column < NUM_SERVERLIST_COLUMNS; ++column)
253 QBrush brush = !color.isEmpty()
254 ? QBrush(QColor(color))
256 QStandardItem* pItem = item(column);
257 pItem->setBackground(brush);
261 void ServerListRowHandler::setBad()
263 QStandardItem* qstdItem;
267 qstdItem = item(IDServerName);
268 fillItem(qstdItem, tr(
"<ERROR>"));
270 qstdItem = item(IDHiddenGroup);
271 fillItem(qstdItem, SGBad);
274 void ServerListRowHandler::setBanned()
276 QStandardItem* qstdItem;
280 qstdItem = item(IDServerName);
281 fillItem(qstdItem, tr(
"You are banned from this server!"));
283 qstdItem = item(IDHiddenGroup);
284 fillItem(qstdItem, SGBanned);
287 void ServerListRowHandler::setCountryFlag()
289 QStandardItem* pItem = item(IDServerName);
291 if (!IP2C::instance()->isDataAccessLocked())
294 if (countryInfo.isValid())
296 QPixmap flag = *countryInfo.flag;
297 pItem->setIcon(flag);
302 void ServerListRowHandler::setFirstQuery()
304 QStandardItem* qstdItem = item(IDHiddenGroup);
305 fillItem(qstdItem, SGFirstQuery);
308 void ServerListRowHandler::setGood()
310 QStandardItem* qstdItem;
315 qstdItem = item(IDPing);
316 fillItem(qstdItem, d->server->ping());
318 qstdItem = item(IDServerName);
319 fillItem(qstdItem, d->server->name());
321 qstdItem = item(IDIwad);
322 fillItem(qstdItem, d->server->iwad());
324 qstdItem = item(IDMap);
325 fillItem(qstdItem, d->server->map());
328 foreach(
const PWad &wad, d->server->wads())
332 strTmp += QString(
"[%1] ").arg(wad.
name());
336 strTmp += wad.
name() +
" ";
340 qstdItem = item(IDWads);
341 fillItem(qstdItem, strTmp);
343 qstdItem = item(IDGametype);
344 QString fullGameModeName = d->server->gameMode().name();
345 QStringList modifierNames = extractValidGameCVarNames(d->server->modifiers());
346 if (!modifierNames.isEmpty())
348 fullGameModeName += QString(
" (%1)").arg(modifierNames.join(
", "));
350 fillItem(qstdItem, fullGameModeName);
352 qstdItem = item(IDHiddenGroup);
353 fillItem(qstdItem, SGNormal);
356 void ServerListRowHandler::setRefreshing()
358 QStandardItem* qstdItem = item(IDServerName);
359 qstdItem->setText(tr(
"<REFRESHING>"));
362 void ServerListRowHandler::setTimeout()
364 QStandardItem* qstdItem;
368 qstdItem = item(IDServerName);
369 fillItem(qstdItem, tr(
"<NO RESPONSE>"));
371 qstdItem = item(IDHiddenGroup);
372 fillItem(qstdItem, SGTimeout);
375 void ServerListRowHandler::setWait()
377 QStandardItem* qstdItem;
381 qstdItem = item(IDServerName);
382 fillItem(qstdItem, tr(
"<Refreshed too soon, wait a while and try again>") );
384 qstdItem = item(IDHiddenGroup);
385 fillItem(qstdItem, SGWait);
388 ServerPtr ServerListRowHandler::serverFromList(
ServerListModel* parentModel,
int rowIndex)
390 QStandardItem* pItem = parentModel->item(rowIndex, IDHiddenServerPointer);
391 QVariant pointer = qVariantFromValue(pItem->data(DTPointerToServerStructure));
392 if (!pointer.isValid())
396 return qVariantValue<ServerPtr>(pointer);
405 int ServerListRowHandler::updateServer(
int response)
407 fillServerPointerColumn();
408 fillPortIconColumn();
426 if (d->server->isKnown())
442 if (d->server->isRefreshing())
449 gLog << tr(
"Unkown server response (%1): %2:%3").arg(response)
450 .arg(d->server->address().toString()).arg(d->server->port());
454 setBackgroundColor();
const QString & name() const
Nice name to display to user in Create Game dialog and in other widgets.
IP2CCountryInfo obtainCountryInfo(unsigned int ipaddress)
void clearNonVitalFields()
bool isValid() const
'Null' objects are invalid.
Player is banned from that server.
"Dummy" response for servers that weren't refreshed yet.
Response from the server was erroreneous.
Response was parsed properly and Server information is available.
Flag and name of the country.
bool isOptional() const
Is this WAD required to join the server?
Server didn't respond at all.
int numClients() const
Overall number of people and bots on the server.
A general game setting or variable (like fraglimit).
Server responded with "wait", may happen when we refresh too quickly.
const QString & name() const
File name of the WAD.