forked from Drifter321/admintool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_slots.cpp
409 lines (345 loc) · 14 KB
/
main_slots.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
#include "ui_mainwindow.h"
#include "mainwindow.h"
#include "worker.h"
#include "query.h"
#include "serverinfo.h"
#include "settings.h"
#include <QMessageBox>
#include <QInputDialog>
#include <QPalette>
#include <QSignalMapper>
#include <QDesktopServices>
#define STEAMID_COLUMN 4
#define NAME_COLUMN 1
extern QPalette defaultPalette;
extern QMap<int, QString> appIDMap;
extern Settings *settings;
extern QList<ServerInfo *> serverList;
extern QColor errorColor;
extern QColor queryingColor;
extern QList<ContextMenuItem> contextMenuItems;
void MainWindow::ConnectSlots()
{
this->ui->commandText->connect(this->ui->commandText, &QLineEdit::returnPressed, this, &MainWindow::processCommand);
this->ui->commandText->connect(this->ui->sendChat, &QLineEdit::returnPressed, this, &MainWindow::sendChat);
this->ui->rconPassword->connect(this->ui->rconPassword, &QLineEdit::returnPressed, this, &MainWindow::rconLogin);
this->ui->rconPassword->connect(this->ui->rconPassword, &QLineEdit::textChanged, this, &MainWindow::passwordUpdated);
this->ui->rconSave->connect(this->ui->rconSave, &QCheckBox::toggled, this, &MainWindow::rconSaveToggled);
this->ui->rconLogin->connect(this->ui->rconLogin, &QPushButton::released, this, &MainWindow::rconLogin);
this->ui->logGetLog->connect(this->ui->logGetLog, &QPushButton::released, this, &MainWindow::getLog);
this->ui->actionAdd_Server->connect(this->ui->actionAdd_Server, &QAction::triggered, this, &MainWindow::addServerEntry);
this->ui->actionDark_Theme->connect(this->ui->actionDark_Theme, &QAction::triggered, this, &MainWindow::darkThemeTriggered);
this->ui->actionSet_Log_Port->connect(this->ui->actionSet_Log_Port, &QAction::triggered, this, &MainWindow::showPortEntry);
this->ui->actionAbout->connect(this->ui->actionAbout, &QAction::triggered, this, &MainWindow::showAbout);
this->ui->browserTable->connect(this->ui->browserTable, &QTableWidget::itemSelectionChanged, this, &MainWindow::browserTableItemSelected);
this->ui->rconShow->connect(this->ui->rconShow, &QCheckBox::clicked, this, &MainWindow::showRconClicked);
this->ui->playerTable->connect(this->ui->playerTable, &QTableWidget::customContextMenuRequested, this, &MainWindow::customPlayerContextMenu);
this->ui->browserTable->connect(this->ui->browserTable, &QTableWidget::customContextMenuRequested, this, &MainWindow::serverBrowserContextMenu);
}
QString CreateCommand(QString command, QString subValue, ContextTypes type, QString name, QString SteamID)
{
QString target = "";
if(type == ContextTypeSteamID)
{
target = SteamID;
}
else if(type == ContextTypeName)
{
target = name;
}
if(!subValue.isEmpty())
{
if(target.isEmpty())
return command.arg(subValue);
else
return command.arg(target, subValue);
}
else
{
if(target.isEmpty())
return command;
else
return command.arg(target);
}
}
void MainWindow::customPlayerContextMenu(const QPoint &pos)
{
int row = this->ui->playerTable->rowAt(pos.y());
if(row != -1)
{
QPoint globalpos = this->ui->playerTable->mapToGlobal(pos);
globalpos.setY(globalpos.y()+15);
globalpos.setX(globalpos.x()+5);
QString name = this->ui->playerTable->item(row, NAME_COLUMN)->text();
QString steamid = this->ui->playerTable->item(row, STEAMID_COLUMN)->text();
QMenu *pContextMenu = new QMenu(this);
QSignalMapper* signalMapper = new QSignalMapper(pContextMenu);
ContextMenuItem ctxItem;
foreach(ctxItem, contextMenuItems)
{
if((ctxItem.type == ContextTypeSteamID && steamid.isEmpty()) || (ctxItem.type == ContextTypeName && name.isEmpty()))
continue;
if(ctxItem.subItems.length() > 0)
{
QMenu *submenu = new QMenu(ctxItem.display, pContextMenu);
submenu->addSection(ctxItem.subTitle);
if(!ctxItem.defaultSub.isNull())
{
QAction *pAction = new QAction("Default", submenu);
pAction->connect(pAction, &QAction::triggered, signalMapper, static_cast<void(QSignalMapper::*)()>(&QSignalMapper::map));
signalMapper->setMapping(pAction, CreateCommand(ctxItem.defaultSub, "", ctxItem.type, name, steamid));
submenu->addAction(pAction);
}
CtxSubItem ctxSubItem;
foreach(ctxSubItem, ctxItem.subItems)
{
QAction *pAction = new QAction(ctxSubItem.display, submenu);
pAction->connect(pAction, &QAction::triggered, signalMapper, static_cast<void(QSignalMapper::*)()>(&QSignalMapper::map));
signalMapper->setMapping(pAction, CreateCommand(ctxItem.cmd, ctxSubItem.val, ctxItem.type, name, steamid));
submenu->addAction(pAction);
}
pContextMenu->addMenu(submenu);
}
else
{
QAction *pAction = new QAction(ctxItem.display, pContextMenu);
pAction->connect(pAction, &QAction::triggered, signalMapper, static_cast<void(QSignalMapper::*)()>(&QSignalMapper::map));
signalMapper->setMapping(pAction, CreateCommand(ctxItem.cmd, "", ctxItem.type, name, steamid));
pContextMenu->addAction(pAction);
}
}
signalMapper->connect(signalMapper, static_cast<void(QSignalMapper::*)(const QString &)>(&QSignalMapper::mapped), this, &MainWindow::playerContextMenuAction);
pContextMenu->connect(pContextMenu, &QMenu::aboutToHide, this, &MainWindow::hideContextMenu);
pContextMenu->exec(globalpos);
}
}
void MainWindow::hideContextMenu()
{
QObject* obj = sender();
obj->deleteLater();
}
void MainWindow::playerContextMenuAction(const QString &cmd)
{
if(this->ui->browserTable->selectedItems().size() == 0)
{
this->browserTableItemSelected();
return;
}
ServerTableIndexItem *item = this->GetServerTableIndexItem(this->ui->browserTable->currentRow());
ServerInfo *info = item->GetServerInfo();
if(info && (info->rcon == NULL || !info->rcon->isAuthed))
{
QList<QueuedCommand>cmds;
cmds.append(QueuedCommand(cmd, QueuedCommandType::ContextCommand));
this->rconLoginQueued(cmds);
return;
}
info->rcon->execCommand(cmd, false);
}
void MainWindow::serverBrowserContextMenu(const QPoint &pos)
{
int row = this->ui->browserTable->rowAt(pos.y());
QPoint globalpos = this->ui->browserTable->mapToGlobal(pos);
globalpos.setY(globalpos.y()+15);
globalpos.setX(globalpos.x()+5);
QMenu *pContextMenu = new QMenu(this);
if(row == -1)
{
QAction *add = new QAction("Add Server", pContextMenu);
add->connect(add, &QAction::triggered, this, [this]{addServerEntry();});
pContextMenu->addAction(add);
}
else
{
//Add delete action.
QAction *del = new QAction("Delete Server", pContextMenu);
del->connect(del, &QAction::triggered, this, [this]{deleteServerDialog();});
pContextMenu->addAction(del);
//Add connect action
QAction *con = new QAction("Connect", pContextMenu);
con->connect(con, &QAction::triggered, this, [this]{connectToServer();}, Qt::QueuedConnection);
pContextMenu->addAction(con);
}
pContextMenu->connect(pContextMenu, &QMenu::aboutToHide, this, &MainWindow::hideContextMenu);
pContextMenu->exec(globalpos);
}
void MainWindow::addServerEntry()
{
QMessageBox message(this);
while(true)
{
bool ok;
QString server = QInputDialog::getText(this, tr("Add Server"), tr("IP:Port"), QLineEdit::Normal,"", &ok, Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
if(ok && !server.isEmpty())
{
AddServerError error;
this->AddServerToList(server, &error);
if(error == AddServerError_None || error == AddServerError_Hostname)
{
settings->SaveSettings();
break;
}
else if(error == AddServerError_AlreadyExists)//Valid ip but exists.
{
message.setText("Server already exists");
message.exec();
break;
}
else
{
message.setText("Invalid IP or Port");
message.exec();
}
}
else
break;
}
}
bool MainWindow::deleteServerDialog()
{
ServerTableIndexItem *id = this->GetServerTableIndexItem(this->ui->browserTable->currentRow());
if(!id)
return false;
ServerInfo *info = id->GetServerInfo();
int index = serverList.indexOf(info);
QMessageBox message(this);
message.setInformativeText(QString("Delete %1?").arg(info->hostPort));
message.setText("Delete server from list?");
message.setStandardButtons(QMessageBox::Ok|QMessageBox::Cancel);
message.setDefaultButton(QMessageBox::Cancel);
int ret = message.exec();
if(ret == QMessageBox::Ok)
{
this->ui->browserTable->removeRow(this->ui->browserTable->currentRow());
for(int i = 0; i < this->ui->browserTable->rowCount(); i++)
{
QTableWidgetItem *item = this->ui->browserTable->item(i, kBrowserColIndex);
int other = item->data(Qt::DisplayRole).toInt();
if(other > index)
{
item->setData(Qt::DisplayRole, other-1);
}
}
serverList.removeAll(info);
pLogHandler->removeServer(info);
delete info;
info = nullptr;
settings->SaveSettings();
if(this->ui->browserTable->selectedItems().size() == 0)
{
//Clear everything no servers left.
this->ui->rulesTable->setRowCount(0);
this->ui->playerTable->setRowCount(0);
this->ui->infoTable->setRowCount(0);
this->ui->chatOutput->setHtml("");
this->ui->commandOutput->setPlainText("");
this->ui->rconPassword->setText("");
this->ui->rconSave->setChecked(false);
}
return true;
}
return false;
}
void MainWindow::connectToServer()
{
ServerTableIndexItem *id = this->GetServerTableIndexItem(this->ui->browserTable->currentRow());
if(!id)
return;
ServerInfo *info = id->GetServerInfo();
QString url = QString("steam://connect/%1").arg(info->hostPort);
QDesktopServices::openUrl(QUrl(url));
}
void MainWindow::browserTableItemSelected()
{
if(this->ui->browserTable->selectedItems().size() == 0)
{
this->SetRconEnabled(false);
return;
}
this->SetRconEnabled(true);
ServerTableIndexItem *item = this->GetServerTableIndexItem(this->ui->browserTable->currentRow());
ServerInfo *info = item->GetServerInfo();
this->SetRconSignals(true);
this->RestoreRcon(info);
this->SetRconSignals(false);
this->UpdateSelectedItemInfo(true, true);
}
void MainWindow::darkThemeTriggered()
{
if(this->ui->actionDark_Theme->isChecked())
{
QPalette palette;
palette.setColor(QPalette::Window, QColor(50,50,50));
palette.setColor(QPalette::WindowText, Qt::white);
palette.setColor(QPalette::Base, QColor(60,60,60));
palette.setColor(QPalette::AlternateBase, QColor(80,80,80));
palette.setColor(QPalette::ToolTipBase, Qt::white);
palette.setColor(QPalette::ToolTipText, Qt::white);
palette.setColor(QPalette::Text, Qt::white);
palette.setColor(QPalette::Button, QColor(50,50,50));
palette.setColor(QPalette::ButtonText, Qt::white);
palette.setColor(QPalette::BrightText, Qt::red);
palette.setColor(QPalette::Highlight, QColor(80,80,80).lighter());
palette.setColor(QPalette::HighlightedText, Qt::black);
qApp->setPalette(palette);
}
else
{
qApp->setPalette(defaultPalette);
}
QTableWidgetItem *hostname;
QColor color = this->GetTextColor();
for(int i = 0; i < this->ui->browserTable->rowCount(); i++)
{
hostname = this->ui->browserTable->item(i, kBrowserColHostname);
if(this->ui->browserTable->item(i, kBrowserColVACIcon))
{
this->ui->browserTable->item(i, kBrowserColVACIcon)->setData(Qt::DecorationRole, this->GetVACImage());
}
if(this->ui->browserTable->item(i, kBrowserColLockIcon))
{
this->ui->browserTable->item(i, kBrowserColLockIcon)->setData(Qt::DecorationRole, this->GetLockImage());
}
if(hostname && (hostname->textColor() == Qt::white || hostname->textColor() == Qt::black))
{
hostname->setTextColor(color);
}
}
}
void MainWindow::showPortEntry()
{
bool ok;
uint port = QInputDialog::getInt(this, tr("Select Port"), tr("Port Range %1 -> %2").arg(QString::number(PORT_MIN), QString::number(PORT_MAX)), this->u16logPort, PORT_MIN, PORT_MAX, 1, &ok, Qt::WindowSystemMenuHint | Qt::WindowTitleHint);
if(ok)
{
this->u16logPort = port;
settings->SaveSettings();
}
}
void MainWindow::showAbout()
{
QMessageBox message(this);
message.setTextFormat(Qt::RichText);
message.setText(
"Version: 1.1.0\n"
"Created by Dr!fter @ <a href=\"https://github.com/Drifter321\">https://github.com/Drifter321</a><br>"
"Using miniupnpc @ <a href=\"https://github.com/miniupnp/miniupnp\">https://github.com/miniupnp/miniupnp</a><br><br>"
"This product includes GeoLite2 data created by MaxMind, available from<br>"
"<a href=\"http://www.maxmind.com\">http://www.maxmind.com</a>."
);
message.exec();
}
void MainWindow::AddRconHistory(QString chat)
{
while(this->commandHistory.size() > 30)
this->commandHistory.removeLast();
this->commandHistory.prepend(chat);
this->commandIter->toFront();
}
void MainWindow::AddChatHistory(QString txt)
{
while(this->sayHistory.size() > 30)
this->sayHistory.removeLast();
this->sayHistory.prepend(txt);
this->sayIter->toFront();
}