diff options
Diffstat (limited to 'pacman-c++/client/client.cpp')
| -rw-r--r-- | pacman-c++/client/client.cpp | 58 |
1 files changed, 41 insertions, 17 deletions
diff --git a/pacman-c++/client/client.cpp b/pacman-c++/client/client.cpp index d064d9e..96ee59a 100644 --- a/pacman-c++/client/client.cpp +++ b/pacman-c++/client/client.cpp | |||
| @@ -185,14 +185,14 @@ void Client::showConnectDialog() | |||
| 185 | QGridLayout *layout = new QGridLayout(m_dialog); | 185 | QGridLayout *layout = new QGridLayout(m_dialog); |
| 186 | layout->setSizeConstraint(QLayout::SetFixedSize); | 186 | layout->setSizeConstraint(QLayout::SetFixedSize); |
| 187 | 187 | ||
| 188 | QLabel *srvLabel = new QLabel("Address:", m_dialog); | 188 | QLabel *srvLabel = new QLabel("Server address:", m_dialog); |
| 189 | QLineEdit *srv = new QLineEdit(m_settings->value("address", "127.0.0.1").toString(), m_dialog); | 189 | |
| 190 | QLabel *portLabel = new QLabel("Port:", m_dialog); | 190 | QComboBox *srv = new QComboBox(m_dialog); |
| 191 | QDoubleSpinBox *port = new QDoubleSpinBox(m_dialog); | 191 | srv->setEditable(true); |
| 192 | port->setDecimals(0); | 192 | srv->setInsertPolicy(QComboBox::InsertAtTop); |
| 193 | port->setMinimum(1); | 193 | QString defentry = QString("127.0.0.1:%1").arg(Constants::Networking::port); |
| 194 | port->setMaximum(65535); | 194 | QStringList srvlist = m_settings->value("srvlist", QStringList(defentry)).toStringList(); |
| 195 | port->setValue(m_settings->value("port", Constants::Networking::port).toUInt()); | 195 | srv->addItems(srvlist); |
| 196 | 196 | ||
| 197 | QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, | 197 | QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, |
| 198 | Qt::Horizontal, m_dialog); | 198 | Qt::Horizontal, m_dialog); |
| @@ -201,15 +201,12 @@ void Client::showConnectDialog() | |||
| 201 | buttonBox->addButton(okButton, QDialogButtonBox::ButtonRole(QDialogButtonBox::AcceptRole)); | 201 | buttonBox->addButton(okButton, QDialogButtonBox::ButtonRole(QDialogButtonBox::AcceptRole)); |
| 202 | buttonBox->addButton(cancelButton, QDialogButtonBox::ButtonRole(QDialogButtonBox::RejectRole)); | 202 | buttonBox->addButton(cancelButton, QDialogButtonBox::ButtonRole(QDialogButtonBox::RejectRole)); |
| 203 | connect(buttonBox, SIGNAL(rejected()), m_dialog, SLOT(reject())); | 203 | connect(buttonBox, SIGNAL(rejected()), m_dialog, SLOT(reject())); |
| 204 | connect(buttonBox, SIGNAL(accepted()), m_dialog, SLOT(accept())); | 204 | connect(buttonBox, SIGNAL(accepted()), this, SLOT(onAcceptConnectDialog())); |
| 205 | 205 | ||
| 206 | layout->addWidget(srvLabel, 0, 0); | 206 | layout->addWidget(srvLabel, 0, 0); |
| 207 | layout->addWidget(srv, 0, 1); | 207 | layout->addWidget(srv, 0, 1); |
| 208 | layout->addWidget(portLabel, 1, 0); | ||
| 209 | layout->addWidget(port, 1, 1); | ||
| 210 | layout->addWidget(buttonBox, 4, 0, 1, 5); | 208 | layout->addWidget(buttonBox, 4, 0, 1, 5); |
| 211 | 209 | ||
| 212 | connect(m_dialog, SIGNAL(accepted()), this, SLOT(onAcceptConnectDialog())); | ||
| 213 | m_dialog->show(); | 210 | m_dialog->show(); |
| 214 | } | 211 | } |
| 215 | 212 | ||
| @@ -218,11 +215,38 @@ void Client::onAcceptConnectDialog() | |||
| 218 | if (m_dialog == NULL) | 215 | if (m_dialog == NULL) |
| 219 | return; | 216 | return; |
| 220 | QGridLayout *layout = static_cast<QGridLayout *>(m_dialog->layout()); | 217 | QGridLayout *layout = static_cast<QGridLayout *>(m_dialog->layout()); |
| 221 | QLineEdit *srv = static_cast<QLineEdit *>(layout->itemAtPosition(0, 1)->widget()); | 218 | QComboBox *srv = static_cast<QComboBox *>(layout->itemAtPosition(0, 1)->widget()); |
| 222 | QDoubleSpinBox *port = static_cast<QDoubleSpinBox *>(layout->itemAtPosition(1, 1)->widget()); | 219 | QStringList selected = srv->currentText().split(':'); |
| 223 | m_settings->setValue("address", srv->text()); | 220 | QString errormsg; |
| 224 | m_settings->setValue("port", int(port->value())); | 221 | if (errormsg.isNull() && selected.count() < 2) |
| 225 | m_mainWidget->doConnect(srv->text(), int(port->value())); | 222 | errormsg = "Invalid Syntax.\nUse: <address>:<port>"; |
| 223 | |||
| 224 | /* port */ | ||
| 225 | QString portstr = selected.takeLast(); | ||
| 226 | bool ok; | ||
| 227 | unsigned int port = portstr.toUInt(&ok); | ||
| 228 | if (!ok || port < 1 || port > 65535) | ||
| 229 | errormsg = "Invalid port number. Must be between 1 and 65535"; | ||
| 230 | |||
| 231 | /* address */ | ||
| 232 | QString address = selected.join(":"); | ||
| 233 | |||
| 234 | if (!errormsg.isNull()) | ||
| 235 | { | ||
| 236 | QMessageBox::critical(this, "Error", errormsg); | ||
| 237 | return; | ||
| 238 | } | ||
| 239 | |||
| 240 | emit m_dialog->accept(); | ||
| 241 | |||
| 242 | QStringList srvlist = m_settings->value("srvlist").toStringList(); | ||
| 243 | srvlist.insert(0, srv->currentText()); | ||
| 244 | srvlist.removeDuplicates(); | ||
| 245 | while (srvlist.count() > 20) | ||
| 246 | srvlist.removeAt(20 - 1); | ||
| 247 | m_settings->setValue("srvlist", srvlist); | ||
| 248 | |||
| 249 | m_mainWidget->doConnect(address, port); | ||
| 226 | } | 250 | } |
| 227 | 251 | ||
| 228 | bool Constants::server = false; | 252 | bool Constants::server = false; |
