23 #include "cfgcustomservers.h"
24 #include "configuration/doomseekerconfig.h"
25 #include "plugins/engineplugin.h"
26 #include "plugins/pluginloader.h"
27 #include "ui_cfgcustomservers.h"
28 #include <QHeaderView>
29 #include <QMessageBox>
30 #include <QStandardItemModel>
34 #include "unknownengine.xpm"
36 DClass<CFGCustomServers> :
public Ui::CFGCustomServers
39 QStandardItemModel *model;
49 connect(d->btnAdd, SIGNAL(clicked()),
this, SLOT(add()));
50 connect(d->btnDisableSelected, SIGNAL(clicked()),
this, SLOT(disableSelected()));
51 connect(d->btnEnableSelected, SIGNAL(clicked()),
this, SLOT(enableSelected()));
52 connect(d->btnRemove, SIGNAL(clicked()),
this, SLOT(
remove()));
53 connect(d->btnSetEngine, SIGNAL(clicked()),
this, SLOT(setEngine()));
55 prepareEnginesComboBox();
58 CFGCustomServers::~CFGCustomServers()
62 void CFGCustomServers::add()
64 int pluginIndex = d->cboEngines->itemData(d->cboEngines->currentIndex()).toInt();
65 const EnginePlugin *plugin = gPlugins->info(pluginIndex);
67 QString engineName = d->cboEngines->itemText(d->cboEngines->currentIndex());
69 add(engineName,
"", plugin->data()->defaultServerPort,
true);
72 void CFGCustomServers::add(
const QString &engineName,
const QString &host,
73 unsigned short port,
bool enabled)
75 QList<QStandardItem *> record;
77 auto engineItem =
new QStandardItem();
78 setEngineOnItem(engineItem, engineName);
80 QString portString = QString::number(port);
82 record.append(engineItem);
83 record.append(
new QStandardItem(host));
84 record.append(
new QStandardItem(portString));
86 auto enabledItem =
new QStandardItem();
87 enabledItem->setCheckState(enabled ? Qt::Checked : Qt::Unchecked);
88 enabledItem->setCheckable(
true);
89 enabledItem->setToolTip(tr(
"Toggle enabled state"));
90 record.append(enabledItem);
92 d->model->appendRow(record);
93 d->tvServers->resizeRowsToContents();
96 CFGCustomServers::CheckAndFixPorts CFGCustomServers::checkAndFixPorts(
int firstRow,
int lastRow)
98 CheckAndFixPorts returnValue = AllOk;
99 for (
int rowIndex = firstRow; rowIndex <= lastRow; ++rowIndex)
101 if (!isPortCorrect(rowIndex))
103 returnValue = AtLeastOneFixed;
105 setPortToDefault(rowIndex);
112 void CFGCustomServers::dataChanged(
const QModelIndex &topLeft,
const QModelIndex &bottomRight)
114 const QString MESSAGE_TITLE = tr(
"Doomseeker - pinned servers");
116 int leftmostColumn = topLeft.column();
117 int rightmostColumn = bottomRight.column();
119 if (isPortColumnWithinRange(leftmostColumn, rightmostColumn))
121 switch (checkAndFixPorts(topLeft.row(), bottomRight.row()))
127 case AtLeastOneFixed:
128 QMessageBox::warning(
this, MESSAGE_TITLE, tr(
"Port must be within range 1 - 65535"));
132 QMessageBox::warning(
this, MESSAGE_TITLE, tr(
"Unimplemented behavior!"));
138 void CFGCustomServers::disableSelected()
140 setEnabledOnSelected(
false);
143 void CFGCustomServers::enableSelected()
145 setEnabledOnSelected(
true);
148 void CFGCustomServers::setEnabledOnSelected(
bool enabled)
150 QItemSelectionModel *sel = d->tvServers->selectionModel();
151 QModelIndexList indexList = sel->selectedRows();
153 QModelIndexList::iterator it;
154 for (it = indexList.begin(); it != indexList.end(); ++it)
156 QStandardItem *item = d->model->itemFromIndex(it->sibling(it->row(), EnabledIndex));
157 item->setCheckState(enabled ? Qt::Checked : Qt::Unchecked);
161 const EnginePlugin *CFGCustomServers::getPluginInfoForRow(
int rowIndex)
163 QStandardItem *itemEngine = d->model->item(rowIndex, EngineColumnIndex);
164 QString engineName = itemEngine->data().toString();
165 int pluginIndex = gPlugins->pluginIndexFromName(engineName);
166 return gPlugins->info(pluginIndex);
169 bool CFGCustomServers::isPortColumnWithinRange(
int leftmostColumnIndex,
int rightmostColumnIndex)
171 return leftmostColumnIndex <= PortColumnIndex && rightmostColumnIndex >= PortColumnIndex;
174 bool CFGCustomServers::isPortCorrect(
int rowIndex)
176 const int MIN_PORT = 1;
177 const int MAX_PORT = 65535;
179 QStandardItem *item = d->model->item(rowIndex, PortColumnIndex);
181 int port = item->text().toInt(&ok);
183 return ok && port >= MIN_PORT && port <= MAX_PORT;
186 void CFGCustomServers::prepareEnginesComboBox()
188 d->cboEngines->clear();
190 for (
unsigned i = 0; i < gPlugins->numPlugins(); ++i)
193 d->cboEngines->addItem(plugin->icon(), plugin->data()->name, i);
196 if (d->cboEngines->count() > 0)
197 d->cboEngines->setCurrentIndex(0);
200 void CFGCustomServers::prepareTable()
202 d->model =
new QStandardItemModel(
this);
204 connect(d->model, SIGNAL(dataChanged(
const QModelIndex&,
const QModelIndex&)),
this, SLOT(dataChanged(
const QModelIndex&,
const QModelIndex&)));
207 labels <<
"" << tr(
"Host") << tr(
"Port") <<
"";
208 d->model->setHorizontalHeaderLabels(labels);
210 d->tvServers->setModel(d->model);
212 d->tvServers->setColumnWidth(EngineColumnIndex, 23);
213 d->tvServers->setColumnWidth(AddressColumnIndex, 180);
214 d->tvServers->setColumnWidth(PortColumnIndex, 60);
215 d->tvServers->setColumnWidth(EnabledIndex, 30);
217 QList<ColumnIndices> fixedSizeColumns;
218 fixedSizeColumns << EngineColumnIndex << EnabledIndex;
219 for (ColumnIndices columnIdx : fixedSizeColumns)
220 d->tvServers->horizontalHeader()->setSectionResizeMode(columnIdx, QHeaderView::Fixed);
221 d->tvServers->horizontalHeader()->setHighlightSections(
false);
223 d->tvServers->verticalHeader()->hide();
230 QList<CustomServerInfo> customServersList = gConfig.doomseeker.customServers.toList();
231 QList<CustomServerInfo>::iterator it;
232 for (it = customServersList.begin(); it != customServersList.end(); ++it)
233 add(it->engine, it->host, it->port, it->enabled);
236 void CFGCustomServers::remove()
238 QItemSelectionModel *selModel = d->tvServers->selectionModel();
239 QModelIndexList indexList = selModel->selectedRows();
242 QList<QStandardItem *> itemList;
243 for (
int i = 0; i < indexList.count(); ++i)
244 itemList << d->model->item(indexList[i].row(), 0);
246 for (
int i = 0; i < itemList.count(); ++i)
248 QModelIndex index = d->model->indexFromItem(itemList[i]);
249 d->model->removeRow(index.row());
255 gConfig.doomseeker.customServers = this->tableGetServers();
258 void CFGCustomServers::setEngine()
260 QItemSelectionModel *sel = d->tvServers->selectionModel();
261 QModelIndexList indexList = sel->selectedRows();
263 QModelIndexList::iterator it;
264 for (it = indexList.begin(); it != indexList.end(); ++it)
266 QStandardItem *item = d->model->itemFromIndex(*it);
267 QString engineName = d->cboEngines->itemText(d->cboEngines->currentIndex());
268 setEngineOnItem(item, engineName);
272 void CFGCustomServers::setEngineOnItem(QStandardItem *item,
const QString &engineName)
274 int engineId = gPlugins->pluginIndexFromName(engineName);
276 item->setData(engineName);
277 item->setToolTip(engineName);
281 item->setIcon(plugin->icon());
284 item->setIcon(QPixmap(unknownengine_xpm));
286 item->setEditable(
false);
290 void CFGCustomServers::setPortToDefault(
int rowIndex)
292 const EnginePlugin *pluginInfo = getPluginInfoForRow(rowIndex);
293 QString defaultPort = QString::number(pluginInfo->data()->defaultServerPort);
295 QStandardItem *itemPort = d->model->item(rowIndex, PortColumnIndex);
296 itemPort->setText(defaultPort);
299 QVector<CustomServerInfo> CFGCustomServers::tableGetServers()
301 QVector<CustomServerInfo> servers;
302 for (
int i = 0; i < d->model->rowCount(); ++i)
306 QStandardItem *item = d->model->item(i, EngineColumnIndex);
307 customServer.engine = item->data().toString();
309 customServer.engineIndex = gPlugins->pluginIndexFromName(customServer.engine);
311 item = d->model->item(i, AddressColumnIndex);
312 customServer.host = item->text();
314 item = d->model->item(i, PortColumnIndex);
315 customServer.port = item->text().toUShort();
317 item = d->model->item(i, EnabledIndex);
318 customServer.enabled = (item->checkState() == Qt::Checked);
320 servers << customServer;