-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverconfig.cpp
executable file
·263 lines (213 loc) · 8.04 KB
/
serverconfig.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
/*
* This file is part of community judge client project developed by Shervin Kh.
* Copyright (C) 2014 Shervin Kh.
* License: GPLv3 Or Later
* Full license could be found in License file shipped with program or at http://www.gnu.org/licenses/
*/
#include "serverconfig.h"
#include "mainwidget.h"
#include "connection.h"
#include <QtGui>
#if QT_VERSION >= 0x050000
#include <QtWidgets>
#endif
void ServerConfig::initData()
{
description["can_register"] = "Enables users to make registeration requests";
type["can_register"] = "Boolean";
description["can_submit"] = "Enables users to make submissions";
type["can_submit"] = "Boolean";
description["can_viewresult"] = "Enables users to view submissions' results";
type["can_viewresult"] = "Boolean";
description["can_viewresult_all_nonpublic"] = "Enables users to view non-public submissions' results from other people";
type["can_viewresult_all_nonpublic"] = "Boolean";
description["can_viewresult_all_public"] = "Enables users to view public submissions' results from other people";
type["can_viewresult_all_public"] = "Boolean";
description["registered_enabled"] = "Automatically enable users while registering";
type["registered_enabled"] = "Boolean";
description["max_subs_in_queue"] = "Maximum number of In-Queue submissions that a user can have";
type["max_subs_in_queue"] = "Integer";
description["max_packet_size"] = "Incoming connections with packets larger that this many bytes will be dropped";
type["max_packet_size"] = "Integer";
description["server_timeout"] = "Incoming connections will be dropped after this many seconds. Do not set too low that you can't connect.";
type["server_timeout"] = "Integer";
description["default_result_count"] = "Default entry count for view result requests";
type["default_result_count"] = "Integer";
description["score_system"] = "Enables score system";
type["score_system"] = "Boolean";
description["judge_enabled"] = "Enables judging";
type["judge_enabled"] = "Boolean";
description["judge_threads"] = "Maximum number of judging threads (0 = Auto set)";
type["judge_threads"] = "Integer";
description["max_library_size"] = "Maximum library size in KB. This will be added to memory limit "
"to set the kill threshold for programs";
type["max_library_size"] = "Integer";
description["real_timer"] = "Use real time instead of cpu time for time measurements";
type["real_timer"] = "Boolean";
}
ServerConfig::ServerConfig(MainWidget *MW, QWidget *parent) :
QWidget(parent), mw(MW)
{
caution = new QLabel("<b><font color=\"red\">Proceed with caution!</font> Changing some configs "
"to an inappropriate value might make your server unusable.</b>");
caution->setWordWrap(true);
getConfig = new QPushButton("&Get Configs");
getConfig->setMinimumSize(getConfig->sizeHint() * 1.3);
setConfig = new QPushButton("&Set Configs");
setConfig->setMinimumSize(setConfig->sizeHint() * 1.3);
buttonLayout = new QHBoxLayout;
buttonLayout->addStretch();
buttonLayout->addWidget(getConfig);
buttonLayout->addStretch();
buttonLayout->addWidget(setConfig);
buttonLayout->addStretch();
QFont fnt;
fnt.setPointSize(12);
nameLabel = new QLabel("<b>Name: </b>");
nameLabel->setFont(fnt);
name = new QLabel;
name->setWordWrap(true);
descLabel = new QLabel("<b>Description: </b>");
descLabel->setFont(fnt);
desc = new QLabel;
desc->setWordWrap(true);
valueLabel = new QLabel;
valueLabel->setFont(fnt);
valueEdit = new QLineEdit;
valueCheckBox = new QCheckBox("Enabled");
connect(valueEdit, SIGNAL(textChanged(QString)), this, SLOT(lineEditChanged()));
connect(valueCheckBox, SIGNAL(stateChanged(int)), this, SLOT(checkBoxChanged()));
configView = new QGridLayout;
configView->addWidget(nameLabel, 0, 0);
configView->addWidget(name, 0, 1);
configView->addWidget(descLabel, 1, 0);
configView->addWidget(desc, 1, 1);
configView->addWidget(valueLabel, 2, 0);
configView->addWidget(valueEdit, 2, 1);
configView->addWidget(valueCheckBox, 2, 1);
dataWidget = new QWidget;
dataWidget->setLayout(configView);
rightLayout = new QVBoxLayout;
rightLayout->addStretch();
rightLayout->addWidget(dataWidget);
rightLayout->addStretch();
configList = new QListWidget;
configList->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Preferred);
downLayout = new QHBoxLayout;
downLayout->addWidget(configList);
downLayout->addLayout(rightLayout);
downLayout->addStretch();
totalLayout = new QVBoxLayout;
totalLayout->addWidget(caution);
totalLayout->addLayout(buttonLayout);
totalLayout->addLayout(downLayout);
connect(getConfig, SIGNAL(clicked()), this, SLOT(getData()));
connect(setConfig, SIGNAL(clicked()), this, SLOT(setData()));
connect(configList, SIGNAL(currentTextChanged(QString)), this, SLOT(handleChange()));
setLayout(totalLayout);
handleChange();
initData();
}
void ServerConfig::init()
{
original.clear();
now.clear();
updateData();
}
void ServerConfig::onSwitch()
{
if (!mw->connection()->isBusy())
getData();
}
void ServerConfig::updateData()
{
configList->clear();
foreach (QString key, now.keys())
configList->addItem(key);
}
void ServerConfig::getData()
{
DataPointer DS;
if (DS = mw->connection()->prepare(this, SLOT(getDone(StatCode,QByteArray))))
{
(*DS) << static_cast<int>(StatCode::GetConfig);
mw->connection()->send();
}
}
void ServerConfig::setData()
{
DataTable DT;
foreach (QString key, original.keys())
if (original[key] != now[key])
DT[key] = now[key];
DataPointer DS;
if (DS = mw->connection()->prepare(this, SLOT(setDone(StatCode,QByteArray))))
{
(*DS) << static_cast<int>(StatCode::SetConfig);
(*DS) << DT;
mw->connection()->send();
}
}
void ServerConfig::getDone(StatCode stat, const QByteArray &BA)
{
if (!mw->handleGeneralStatCodes(stat) && stat == StatCode::OperationSuccessful)
{
QDataStream DS(BA);
DS.setVersion(QDataStream::Qt_4_8);
DS >> original;
now = original;
updateData();
}
}
void ServerConfig::setDone(StatCode stat, const QByteArray &)
{
if (!mw->handleGeneralStatCodes(stat) && stat == StatCode::OperationSuccessful)
{
QMessageBox::information(this, "Done", "Operation was successful");
original = now;
}
}
void ServerConfig::handleChange()
{
if (configList->currentRow() == -1)
dataWidget->setVisible(false);
else
{
dataWidget->setVisible(true);
QString cur = configList->currentItem()->text();
name->setText(cur);
desc->setText(description[cur].toString());
valueLabel->setText(QString("<b>Value (%1):").arg(type[cur].toString()));
if (type[cur] == "Boolean")
{
valueEdit->setVisible(false);
valueCheckBox->setVisible(true);
valueCheckBox->setChecked(now[cur].toBool());
}
else
{
valueEdit->setVisible(true);
valueCheckBox->setVisible(false);
valueEdit->setText(now[cur].toString());
if (type[cur] == "Integer")
valueEdit->setValidator(new QIntValidator);
else if (type[cur] == "Real")
valueEdit->setValidator(new QDoubleValidator);
else if (type[cur] == "String")
valueEdit->setValidator(NULL);
}
}
}
void ServerConfig::checkBoxChanged()
{
if (configList->currentRow() != -1)
now[configList->currentItem()->text()] = valueCheckBox->isChecked();
}
void ServerConfig::lineEditChanged()
{
int x = 0;
QString val = valueEdit->text();
if (configList->currentRow() != -1 && (!valueEdit->validator() ||
valueEdit->validator()->validate(val, x) == QValidator::Acceptable))
now[configList->currentItem()->text()] = valueEdit->text();
}