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::audio;
17 using namespace swift::misc::network;
18 
19 namespace swift::gui::editors
20 {
21  CServerForm::CServerForm(QWidget *parent) : CForm(parent), ui(new Ui::CNetworkServerForm)
22  {
23  ui->setupUi(this);
24  ui->le_Port->setValidator(new QIntValidator(1, 65535, this));
25  this->initServerTypes();
26 
27  connect(ui->cbp_Ecosystem, &CEcosystemComboBox::currentTextChanged, this, &CServerForm::onChangedEcoSystem);
28  connect(ui->cb_ServerType, &QComboBox::currentTextChanged, this, &CServerForm::onChangedServerType);
29  connect(ui->tb_Unhide, &QToolButton::clicked, this, &CServerForm::tempUnhidePassword);
30  }
31 
33 
34  void CServerForm::setServer(const CServer &server)
35  {
36  const CUser user = server.getUser();
37  ui->le_NetworkId->setText(user.getId());
38  ui->le_RealName->setText(user.getRealName());
39  ui->le_Name->setText(server.getName());
40  ui->cb_ServerType->setCurrentText(server.getServerTypeAsString());
41  ui->cbp_Ecosystem->setCurrentEcosystem(server.getEcosystem());
42  ui->le_Password->setText(user.getPassword());
43  ui->le_Description->setText(server.getDescription());
44  ui->le_Address->setText(server.getAddress());
45  ui->le_Port->setText(QString::number(server.getPort()));
46  ui->form_ServerFsd->setValue(server.getFsdSetup());
47  }
48 
50  {
51  const CUser user(ui->le_NetworkId->text().trimmed(), ui->le_RealName->text().trimmed().simplified(), QString(),
52  ui->le_Password->text().trimmed());
53  const CFsdSetup fsdSetup(ui->form_ServerFsd->getValue());
54  const CServer server(ui->le_Name->text().trimmed().simplified(),
55  ui->le_Description->text().trimmed().simplified(), ui->le_Address->text().trimmed(),
56  ui->le_Port->text().trimmed().toInt(), user, fsdSetup,
57  ui->cbp_Ecosystem->getSelectedEcosystem(), this->getServerType(), true);
58  return server;
59  }
60 
62  {
63  return ui->cb_ServerType->currentData().value<CServer::ServerType>();
64  }
65 
66  void CServerForm::resetToFirstTab() { ui->tw_ServerForm->setCurrentIndex(0); }
67 
68  void CServerForm::setReadOnly(bool readOnly)
69  {
70  ui->form_ServerFsd->setReadOnly(readOnly);
71 
72  ui->le_NetworkId->setReadOnly(readOnly);
73  ui->le_RealName->setReadOnly(readOnly);
74  ui->le_Name->setReadOnly(readOnly);
75  ui->le_Description->setReadOnly(readOnly);
76  ui->le_Address->setReadOnly(readOnly);
77  ui->le_Port->setReadOnly(readOnly);
78  ui->le_Password->setReadOnly(readOnly);
79  ui->cb_ServerType->setEnabled(!readOnly);
80  ui->cbp_Ecosystem->setEnabled(!readOnly);
81  ui->tb_Unhide->setVisible(!readOnly);
82  this->forceStyleSheetUpdate();
83  }
84 
86  {
87  if (ui->wi_Password->isVisible() == show) { return; }
88  if (m_passwordNameLabel.isEmpty()) { m_passwordNameLabel = ui->lbl_IdPassword->text(); }
89  ui->lbl_IdPassword->setText(show ? m_passwordNameLabel : "Id");
90  ui->wi_Password->setVisible(show);
91  }
92 
93  void CServerForm::initServerTypes()
94  {
95  // init all server type values
96  int c = 0;
97  ui->cb_ServerType->clear();
98  for (const int type : CServer::allServerTypes())
99  {
100  const CServer::ServerType st = static_cast<CServer::ServerType>(type);
101  ui->cb_ServerType->insertItem(c++, CServer::serverTypeToString(st), QVariant::fromValue(type));
102  }
103  }
104 
105  void CServerForm::onChangedServerType(const QString &text)
106  {
107  Q_UNUSED(text);
108  const CServer::ServerType t = this->getServerType();
109  const CServer dummy(t);
110  const CEcosystem es = dummy.getEcosystem();
111  if (es.isUnspecified()) { return; }
112  if (es.isSystem(CEcosystem::NoSystem)) { return; }
113  ui->cbp_Ecosystem->setCurrentEcosystem(es);
114  }
115 
116  void CServerForm::onChangedEcoSystem(const QString &text)
117  {
118  Q_UNUSED(text);
119  const CEcosystem es = ui->cbp_Ecosystem->getSelectedEcosystem();
120  const CServer dummy(es);
121  if (dummy.hasUnspecifiedServerType()) { return; }
122  ui->cb_ServerType->setCurrentText(dummy.getServerTypeAsString());
123  }
124 
125  void CServerForm::tempUnhidePassword() { CGuiUtility::tempUnhidePassword(ui->le_Password); }
126 
128  {
129  Q_UNUSED(nested);
130  const CServer server = this->getServer();
131  return server.validate();
132  }
133 } // 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:85
swift::misc::network::CServer::ServerType getServerType() const
Get currently selected server type.
Definition: serverform.cpp:61
virtual ~CServerForm()
Destructor.
Definition: serverform.cpp:32
virtual swift::misc::CStatusMessageList validate(bool nested=false) const
Validate, empty list means OK.
Definition: serverform.cpp:127
virtual void setReadOnly(bool readonly)
Set editable.
Definition: serverform.cpp:68
void setServer(const swift::misc::network::CServer &server)
Set server.
Definition: serverform.cpp:34
void resetToFirstTab()
Reset to 1st tab.
Definition: serverform.cpp:66
swift::misc::network::CServer getServer() const
Get server.
Definition: serverform.cpp:49
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.