-
Notifications
You must be signed in to change notification settings - Fork 1
/
serverwindow.cpp
219 lines (188 loc) · 6.46 KB
/
serverwindow.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
#include "serverwindow.h"
#include "ui_serverwindow.h"
#include <network/server.h>
#include <QFile>
#include <QDir>
#include <QJsonArray>
#include <QJsonDocument>
#include <QMessageBox>
#include <QStandardPaths>
#include <utility>
#ifdef Q_OS_WIN
#include <QWinJumpList>
#include <QWinJumpListCategory>
#include <QWinJumpListItem>
#endif
const QString ServerWindow::serverFileNameFormat = "%1/servers.dat";
ServerWindow::ServerWindow(QWidget* parent) :
QMainWindow(parent),
ui(new Ui::ServerWindow)
{
ui->setupUi(this);
}
ServerWindow::~ServerWindow()
{
delete ui;
}
bool ServerWindow::connectToServer(QString serverName)
{
if (!serverList.contains(serverName)) {
QMessageBox::critical(this,
QApplication::applicationName(),
tr("The profile name does not exist in the server list."));
return false;
}
const Server s = serverList.value(serverName);
#ifdef Q_OS_WIN
QWinJumpList jumpList;
QWinJumpListCategory* recent = jumpList.recent();
QWinJumpListItem* jumpItem = new QWinJumpListItem(QWinJumpListItem::Link);
jumpItem->setTitle(QString("%1 @ %2:%3").arg(serverName)
.arg(s.getIp())
.arg(s.getPort()));
jumpItem->setFilePath(QDir::toNativeSeparators(QCoreApplication::applicationFilePath()));
jumpItem->setIcon(QIcon("://resources/codlogo.ico"));
jumpItem->setArguments(QStringList({"--connect", serverName}));
recent->addItem(jumpItem);
#endif
CommandWindow* commandWindow = new CommandWindow(s, this);
commandWindow->setAttribute(Qt::WA_DeleteOnClose, true);
commandWindow->raise();
commandWindow->show();
this->hide();
return true;
}
void ServerWindow::on_connectButton_clicked()
{
if (ui->profileBox->currentIndex() == -1
|| !serverList.contains(ui->profileBox->currentText())) {
return;
}
connectToServer(ui->profileBox->currentText());
}
void ServerWindow::on_saveButton_clicked()
{
QHostAddress serverIP;
if (ui->ipEdit->text().isEmpty() || serverIP.setAddress(ui->ipEdit->text())) {
QMessageBox::warning(this,
QApplication::applicationName(),
tr("The IP address is not valid."));
return;
} else if (ui->passwordEdit->text().isEmpty()) {
QMessageBox::warning(this,
QApplication::applicationName(),
tr("The password field cannot be empty."));
return;
}
Server s(ui->ipEdit->text(),
ui->portBox->text().toUShort(),
ui->passwordEdit->text().toUtf8());
serverList.insert(ui->profileBox->currentText(), std::move(s));
updateComboBoxItems(ui->profileBox, ui->profileBox->currentText());
if(ui->defaultCheck->isChecked()) {
defaultServer = ui->profileBox->currentText();
}
writeServers();
}
void ServerWindow::on_deleteButton_clicked()
{
int currentIndex = ui->profileBox->currentIndex();
if (currentIndex == -1) {
return;
}
serverList.remove(ui->profileBox->currentText());
ui->profileBox->removeItem(currentIndex);
writeServers();
}
void ServerWindow::on_defaultCheck_clicked()
{
if (ui->defaultCheck->isChecked()) {
defaultServer = ui->profileBox->currentText();
} else if (defaultServer == ui->profileBox->currentText()) {
defaultServer = "";
}
}
void ServerWindow::on_profileBox_currentIndexChanged(const QString& currentText)
{
if (currentText == defaultServer) {
ui->defaultCheck->setChecked(true);
} else {
ui->defaultCheck->setChecked(false);
}
const Server s = serverList.value(ui->profileBox->currentText());
ui->ipEdit->setText(s.getIp());
ui->portBox->setValue(s.getPort());
ui->passwordEdit->setText(s.getRconPassword());
}
void ServerWindow::updateComboBoxItems(QComboBox* combo, QString newItemText)
{
bool foundItem = false;
int insertIndex = 0;
for (int i = 0; i < combo->count(); i++) {
const QString currentText = combo->itemText(i);
if (currentText == newItemText) {
foundItem = true;
} else if (newItemText > currentText) {
insertIndex = i;
}
}
if (!foundItem) {
combo->insertItem(insertIndex, newItemText);
}
}
void ServerWindow::readServers(bool oldFormat)
{
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QFile file(QString(serverFileNameFormat).arg(path));
if (!file.open(QIODevice::ReadOnly)) {
qWarning("Couldn't open servers file.");
return;
}
QByteArray serverData = file.readAll();
QJsonObject rootObject = QJsonDocument::fromJson(serverData).object();
QJsonArray serversArray = rootObject["servers"].toArray();
foreach (const QJsonValue& value, serversArray) {
const QJsonObject& serverObj = value.toObject();
serverList.insert(serverObj["profile"].toString(), Server::fromJson(serverObj, oldFormat));
}
defaultServer = rootObject["default"].toString();
ui->profileBox->addItems(serverList.keys());
ui->profileBox->setCurrentText(defaultServer);
on_profileBox_currentIndexChanged(defaultServer);
}
void ServerWindow::writeServers()
{
QString path = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir dir(path);
if (!dir.exists()) {
dir.mkpath(path);
}
QFile file(QString(serverFileNameFormat).arg(path));
if (!file.open(QIODevice::WriteOnly)) {
qWarning("Couldn't write to servers file.");
return;
}
QMapIterator<QString, Server> i(serverList);
QJsonArray serversArray;
while (i.hasNext()) {
const Server s = i.next().value();
QJsonObject serverObject = s.toJSON();
serverObject["profile"] = i.key();
serversArray.append(serverObject);
}
QJsonObject rootObject;
rootObject["servers"] = serversArray;
rootObject["default"] = defaultServer;
QString data = QJsonDocument(rootObject).toJson(QJsonDocument::Compact);
file.write(data.toUtf8());
}
bool ServerWindow::moveToNewAppFolderLocation(const QString& oldApplicationName)
{
bool moved = false;
QDir dir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
dir.cdUp();
if (dir.exists(oldApplicationName)) {
moved = dir.rename(oldApplicationName, QApplication::applicationName());
}
return moved;
}