swift
serverform.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2014 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QIntValidator>
7 #include <QLabel>
8 #include <QLineEdit>
9 #include <QToolButton>
10 
11 #include "ui_serverform.h"
12 
13 #include "misc/network/user.h"
14 
15 using namespace swift::misc;
16 using namespace swift::misc::network;
17 
18 namespace swift::gui::editors
19 {
20  CServerForm::CServerForm(QWidget *parent) : CForm(parent), ui(new Ui::CNetworkServerForm)
21  {
22  ui->setupUi(this);
23  ui->le_Port->setValidator(new QIntValidator(1, 65535, this));
24  this->initServerTypes();
25 
26  connect(ui->cbp_Ecosystem, &CEcosystemComboBox::currentTextChanged, this, &CServerForm::onChangedEcoSystem);
27  connect(ui->cb_ServerType, &QComboBox::currentTextChanged, this, &CServerForm::onChangedServerType);
28  connect(ui->tb_Unhide, &QToolButton::clicked, this, &CServerForm::tempUnhidePassword);
29  }
30 
31  CServerForm::~CServerForm() = default;
32 
33  void CServerForm::setServer(const CServer &server)
34  {
35  const CUser user = server.getUser();
36  ui->le_NetworkId->setText(user.getId());
37  ui->le_RealName->setText(user.getRealName());
38  ui->le_Name->setText(server.getName());
39  ui->cb_ServerType->setCurrentText(server.getServerTypeAsString());
40  ui->cbp_Ecosystem->setCurrentEcosystem(server.getEcosystem());
41  ui->le_Password->setText(user.getPassword());
42  ui->le_Description->setText(server.getDescription());
43  ui->le_Address->setText(server.getAddress());
44  ui->le_Port->setText(QString::number(server.getPort()));
45  ui->form_ServerFsd->setValue(server.getFsdSetup());
46  }
47 
49  {
50  const CUser user(ui->le_NetworkId->text().trimmed(), ui->le_RealName->text().trimmed().simplified(), QString(),
51  ui->le_Password->text().trimmed());
52  const CFsdSetup fsdSetup(ui->form_ServerFsd->getValue());
53  const CServer server(ui->le_Name->text().trimmed().simplified(),
54  ui->le_Description->text().trimmed().simplified(), ui->le_Address->text().trimmed(),
55  ui->le_Port->text().trimmed().toInt(), user, fsdSetup,
56  ui->cbp_Ecosystem->getSelectedEcosystem(), this->getServerType(), true);
57  return server;
58  }
59 
61  {
62  return ui->cb_ServerType->currentData().value<CServer::ServerType>();
63  }
64 
65  void CServerForm::resetToFirstTab() { ui->tw_ServerForm->setCurrentIndex(0); }
66 
67  void CServerForm::setReadOnly(bool readOnly)
68  {
69  ui->form_ServerFsd->setReadOnly(readOnly);
70 
71  ui->le_NetworkId->setReadOnly(readOnly);
72  ui->le_RealName->setReadOnly(readOnly);
73  ui->le_Name->setReadOnly(readOnly);
74  ui->le_Description->setReadOnly(readOnly);
75  ui->le_Address->setReadOnly(readOnly);
76  ui->le_Port->setReadOnly(readOnly);
77  ui->le_Password->setReadOnly(readOnly);
78  ui->cb_ServerType->setEnabled(!readOnly);
79  ui->cbp_Ecosystem->setEnabled(!readOnly);
80  ui->tb_Unhide->setVisible(!readOnly);
81  this->forceStyleSheetUpdate();
82  }
83 
85  {
86  if (ui->wi_Password->isVisible() == show) { return; }
87  if (m_passwordNameLabel.isEmpty()) { m_passwordNameLabel = ui->lbl_IdPassword->text(); }
88  ui->lbl_IdPassword->setText(show ? m_passwordNameLabel : "Id");
89  ui->wi_Password->setVisible(show);
90  }
91 
92  void CServerForm::initServerTypes()
93  {
94  // init all server type values
95  int c = 0;
96  ui->cb_ServerType->clear();
97  for (const int type : CServer::allServerTypes())
98  {
99  const auto st = static_cast<CServer::ServerType>(type);
100  ui->cb_ServerType->insertItem(c++, CServer::serverTypeToString(st), QVariant::fromValue(type));
101  }
102  }
103 
104  void CServerForm::onChangedServerType(const QString &text)
105  {
106  Q_UNUSED(text);
107  const CServer::ServerType t = this->getServerType();
108  const CServer dummy(t);
109  const CEcosystem es = dummy.getEcosystem();
110  if (es.isUnspecified()) { return; }
111  if (es.isSystem(CEcosystem::NoSystem)) { return; }
112  ui->cbp_Ecosystem->setCurrentEcosystem(es);
113  }
114 
115  void CServerForm::onChangedEcoSystem(const QString &text)
116  {
117  Q_UNUSED(text);
118  const CEcosystem es = ui->cbp_Ecosystem->getSelectedEcosystem();
119  const CServer dummy(es);
120  if (dummy.hasUnspecifiedServerType()) { return; }
121  ui->cb_ServerType->setCurrentText(dummy.getServerTypeAsString());
122  }
123 
124  void CServerForm::tempUnhidePassword() { CGuiUtility::tempUnhidePassword(ui->le_Password); }
125 
127  {
128  Q_UNUSED(nested);
129  const CServer server = this->getServer();
130  return server.validate();
131  }
132 } // namespace swift::gui::editors
static void tempUnhidePassword(QLineEdit *lineEdit, int unhideMs=5000)
Temp. unhide password.
Definition: guiutility.cpp:483
Form base class.
Definition: form.h:27
void forceStyleSheetUpdate()
Forces a stylesheet update.
Definition: form.cpp:47
void showPasswordField(bool show)
Show the password field.
Definition: serverform.cpp:84
swift::misc::network::CServer::ServerType getServerType() const
Get currently selected server type.
Definition: serverform.cpp:60
swift::misc::CStatusMessageList validate(bool nested=false) const
Validate, empty list means OK.
Definition: serverform.cpp:126
void setReadOnly(bool readonly)
Set editable.
Definition: serverform.cpp:67
void setServer(const swift::misc::network::CServer &server)
Set server.
Definition: serverform.cpp:33
void resetToFirstTab()
Reset to 1st tab.
Definition: serverform.cpp:65
swift::misc::network::CServer getServer() const
Get server.
Definition: serverform.cpp:48
Status messages, e.g. from Core -> GUI.
Ecosystem of server belonging together.
Definition: ecosystem.h:21
bool isUnspecified() const
Unknown system?
Definition: ecosystem.h:51
bool isSystem(System s) const
Is system?
Definition: ecosystem.h:54
Value object for a FSD setup.
Definition: fsdsetup.h:24
Value object encapsulating information of a server.
Definition: server.h:28
const QString & getDescription() const
Get description.
Definition: server.h:110
const QString & getServerTypeAsString() const
Get server type as string.
Definition: server.cpp:120
int getPort() const
Get port.
Definition: server.h:116
CStatusMessageList validate() const
Validate, provide details about issues.
Definition: server.cpp:126
const QString & getName() const
Get name.
Definition: server.h:92
const CFsdSetup & getFsdSetup() const
Get FSD setup.
Definition: server.h:140
const QString & getAddress() const
Get address.
Definition: server.h:80
const CEcosystem & getEcosystem() const
Get the ecosystem.
Definition: server.h:122
const CUser & getUser() const
Get user.
Definition: server.h:86
Value object encapsulating information of a user.
Definition: user.h:28
const QString & getPassword() const
Get password.
Definition: user.h:65
const QString & getRealName() const
Get full name.
Definition: user.h:59
const QString & getId() const
Get id.
Definition: user.h:119
Free functions in swift::misc.
void clicked(bool checked)
void currentTextChanged(const QString &text)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
QString number(double n, char format, int precision)
QVariant fromValue(T &&value)
void show()