23 #include "cfgcustomservers.h"
24 #include "configuration/doomseekerconfig.h"
25 #include "plugins/enginedefaults.h"
26 #include "plugins/engineplugin.h"
27 #include "plugins/pluginloader.h"
28 #include "ui_cfgcustomservers.h"
29 #include <QHeaderView>
30 #include <QMessageBox>
31 #include <QStandardItemModel>
34 DClass<CFGCustomServers> :
public Ui::CFGCustomServers
37 QStandardItemModel *model;
47 connect(d->btnAdd, SIGNAL(clicked()),
this, SLOT(add()));
48 connect(d->btnDisableSelected, SIGNAL(clicked()),
this, SLOT(disableSelected()));
49 connect(d->btnEnableSelected, SIGNAL(clicked()),
this, SLOT(enableSelected()));
50 connect(d->btnRemove, SIGNAL(clicked()),
this, SLOT(
remove()));
51 connect(d->btnSetEngine, SIGNAL(clicked()),
this, SLOT(setEngine()));
53 prepareEnginesComboBox();
56 CFGCustomServers::~CFGCustomServers()
60 void CFGCustomServers::add()
62 int pluginIndex = d->cboEngines->itemData(d->cboEngines->currentIndex()).toInt();
63 const EnginePlugin *plugin = gPlugins->info(pluginIndex);
65 QString engineName = d->cboEngines->itemText(d->cboEngines->currentIndex());
67 add(engineName,
"", plugin->data()->defaultServerPort,
true);
70 void CFGCustomServers::add(
const QString &engineName,
const QString &host,
71 unsigned short port,
bool enabled)
73 QList<QStandardItem *> record;
75 auto engineItem =
new QStandardItem();
76 setEngineOnItem(engineItem, engineName);
78 QString portString = QString::number(port);
80 record.append(engineItem);
81 record.append(
new QStandardItem(host));
82 record.append(
new QStandardItem(portString));
84 auto enabledItem =
new QStandardItem();
85 enabledItem->setCheckState(enabled ? Qt::Checked : Qt::Unchecked);
86 enabledItem->setCheckable(
true);
87 enabledItem->setToolTip(tr(
"Toggle enabled state"));
88 record.append(enabledItem);
90 d->model->appendRow(record);
91 d->tvServers->resizeRowsToContents();
94 CFGCustomServers::CheckAndFixPorts CFGCustomServers::checkAndFixPorts(
int firstRow,
int lastRow)
96 CheckAndFixPorts returnValue = AllOk;
97 for (
int rowIndex = firstRow; rowIndex <= lastRow; ++rowIndex)
99 if (!isPortCorrect(rowIndex))
101 returnValue = AtLeastOneFixed;
103 setPortToDefault(rowIndex);
110 void CFGCustomServers::dataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight)
112 const QString MESSAGE_TITLE = tr(
"Doomseeker - pinned servers");
114 int leftmostColumn = topLeft.column();
115 int rightmostColumn = bottomRight.column();
117 if (isPortColumnWithinRange(leftmostColumn, rightmostColumn))
119 switch (checkAndFixPorts(topLeft.row(), bottomRight.row()))
125 case AtLeastOneFixed:
126 QMessageBox::warning(
this, MESSAGE_TITLE, tr(
"Port must be within range 1 - 65535"));
130 QMessageBox::warning(
this, MESSAGE_TITLE, tr(
"Unimplemented behavior!"));
136 void CFGCustomServers::disableSelected()
138 setEnabledOnSelected(
false);
141 void CFGCustomServers::enableSelected()
143 setEnabledOnSelected(
true);
146 void CFGCustomServers::setEnabledOnSelected(
bool enabled)
148 QItemSelectionModel *sel = d->tvServers->selectionModel();
149 QModelIndexList indexList = sel->selectedRows();
151 QModelIndexList::iterator it;
152 for (it = indexList.begin(); it != indexList.end(); ++it)
154 QStandardItem *item = d->model->itemFromIndex(it->sibling(it->row(), EnabledIndex));
155 item->setCheckState(enabled ? Qt::Checked : Qt::Unchecked);
159 const EnginePlugin *CFGCustomServers::getPluginInfoForRow(
int rowIndex)
161 QStandardItem *itemEngine = d->model->item(rowIndex, EngineColumnIndex);
162 QString engineName = itemEngine->data().toString();
163 int pluginIndex = gPlugins->pluginIndexFromName(engineName);
164 return gPlugins->info(pluginIndex);
167 bool CFGCustomServers::isPortColumnWithinRange(
int leftmostColumnIndex,
int rightmostColumnIndex)
169 return leftmostColumnIndex <= PortColumnIndex && rightmostColumnIndex >= PortColumnIndex;
172 bool CFGCustomServers::isPortCorrect(
int rowIndex)
174 const int MIN_PORT = 1;
175 const int MAX_PORT = 65535;
177 QStandardItem *item = d->model->item(rowIndex, PortColumnIndex);
179 int port = item->text().toInt(&ok);
181 return ok && port >= MIN_PORT && port <= MAX_PORT;
184 void CFGCustomServers::prepareEnginesComboBox()
186 d->cboEngines->clear();
188 for (
unsigned i = 0; i < gPlugins->numPlugins(); ++i)
191 d->cboEngines->addItem(plugin->icon(), plugin->data()->name, i);
194 if (d->cboEngines->count() > 0)
195 d->cboEngines->setCurrentIndex(0);
198 void CFGCustomServers::prepareTable()
200 d->model =
new QStandardItemModel(
this);
202 connect(d->model, SIGNAL(dataChanged(
const QModelIndex&,
const QModelIndex&)),
this, SLOT(dataChanged(
const QModelIndex&,
const QModelIndex&)));
205 labels <<
"" << tr(
"Host") << tr(
"Port") <<
"";
206 d->model->setHorizontalHeaderLabels(labels);
208 d->tvServers->setModel(d->model);
210 d->tvServers->setColumnWidth(EngineColumnIndex, 23);
211 d->tvServers->setColumnWidth(AddressColumnIndex, 180);
212 d->tvServers->setColumnWidth(PortColumnIndex, 60);
213 d->tvServers->setColumnWidth(EnabledIndex, 30);
215 QList<ColumnIndices> fixedSizeColumns;
216 fixedSizeColumns << EngineColumnIndex << EnabledIndex;
217 for (ColumnIndices columnIdx : fixedSizeColumns)
218 d->tvServers->horizontalHeader()->setSectionResizeMode(columnIdx, QHeaderView::Fixed);
219 d->tvServers->horizontalHeader()->setHighlightSections(
false);
221 d->tvServers->verticalHeader()->hide();
228 QList<CustomServerInfo> customServersList = gConfig.doomseeker.customServers.toList();
229 QList<CustomServerInfo>::iterator it;
230 for (it = customServersList.begin(); it != customServersList.end(); ++it)
231 add(it->engine, it->host, it->port, it->enabled);
234 void CFGCustomServers::remove()
236 QItemSelectionModel *selModel = d->tvServers->selectionModel();
237 QModelIndexList indexList = selModel->selectedRows();
240 QList<QStandardItem *> itemList;
241 for (
int i = 0; i < indexList.count(); ++i)
242 itemList << d->model->item(indexList[i].row(), 0);
244 for (
int i = 0; i < itemList.count(); ++i)
246 QModelIndex index = d->model->indexFromItem(itemList[i]);
247 d->model->removeRow(index.row());
253 gConfig.doomseeker.customServers = this->tableGetServers();
256 void CFGCustomServers::setEngine()
258 QItemSelectionModel *sel = d->tvServers->selectionModel();
259 QModelIndexList indexList = sel->selectedRows();
261 QModelIndexList::iterator it;
262 for (it = indexList.begin(); it != indexList.end(); ++it)
264 QStandardItem *item = d->model->itemFromIndex(*it);
265 QString engineName = d->cboEngines->itemText(d->cboEngines->currentIndex());
266 setEngineOnItem(item, engineName);
270 void CFGCustomServers::setEngineOnItem(QStandardItem *item,
const QString &engineName)
272 int engineId = gPlugins->pluginIndexFromName(engineName);
274 item->setData(engineName);
275 item->setToolTip(engineName);
279 item->setIcon(plugin->icon());
282 item->setIcon(gUnknownEngineIcon());
284 item->setEditable(
false);
288 void CFGCustomServers::setPortToDefault(
int rowIndex)
290 const EnginePlugin *pluginInfo = getPluginInfoForRow(rowIndex);
291 QString defaultPort = QString::number(pluginInfo->data()->defaultServerPort);
293 QStandardItem *itemPort = d->model->item(rowIndex, PortColumnIndex);
294 itemPort->setText(defaultPort);
297 QVector<CustomServerInfo> CFGCustomServers::tableGetServers()
299 QVector<CustomServerInfo> servers;
300 for (
int i = 0; i < d->model->rowCount(); ++i)
304 QStandardItem *item = d->model->item(i, EngineColumnIndex);
305 customServer.engine = item->data().toString();
307 customServer.engineIndex = gPlugins->pluginIndexFromName(customServer.engine);
309 item = d->model->item(i, AddressColumnIndex);
310 customServer.host = item->text();
312 item = d->model->item(i, PortColumnIndex);
313 customServer.port = item->text().toUShort();
315 item = d->model->item(i, EnabledIndex);
316 customServer.enabled = (item->checkState() == Qt::Checked);
318 servers << customServer;