2019-08-24 20:47:32 +02:00
|
|
|
|
#include "ConnectionConfigurationWidget.h"
|
|
|
|
|
|
#include "SslModeModel.h"
|
|
|
|
|
|
#include "ConnectionConfig.h"
|
|
|
|
|
|
#include "ConnectionController.h"
|
|
|
|
|
|
#include "ConnectionListModel.h"
|
2021-07-04 16:45:50 +02:00
|
|
|
|
#include "Pgsql_Connection.h"
|
|
|
|
|
|
#include "Pgsql_PgException.h"
|
2019-08-24 20:47:32 +02:00
|
|
|
|
#include "util.h"
|
|
|
|
|
|
#include <QApplication>
|
2021-07-04 16:45:50 +02:00
|
|
|
|
#include <QCheckBox>
|
|
|
|
|
|
#include <QComboBox>
|
2019-08-24 20:47:32 +02:00
|
|
|
|
#include <QDataWidgetMapper>
|
2021-07-04 16:45:50 +02:00
|
|
|
|
#include <QDebug>
|
|
|
|
|
|
#include <QDialog>
|
2019-08-24 20:47:32 +02:00
|
|
|
|
#include <QDialogButtonBox>
|
|
|
|
|
|
#include <QFormLayout>
|
2021-07-04 16:45:50 +02:00
|
|
|
|
#include <QFuture>
|
2019-08-24 20:47:32 +02:00
|
|
|
|
#include <QLabel>
|
|
|
|
|
|
#include <QLineEdit>
|
|
|
|
|
|
#include <QPushButton>
|
|
|
|
|
|
#include <QSpinBox>
|
2021-07-04 16:45:50 +02:00
|
|
|
|
#include <QtConcurrent>
|
2019-08-24 20:47:32 +02:00
|
|
|
|
|
|
|
|
|
|
#define SET_OBJECT_NAME(var) var->setObjectName(#var)
|
|
|
|
|
|
|
2021-07-04 16:45:50 +02:00
|
|
|
|
|
2022-01-16 17:59:28 +01:00
|
|
|
|
|
2021-07-04 19:33:06 +02:00
|
|
|
|
void ConnectionConfigurationWidget::editExistingInWindow(ConnectionController *connection_controller, const ConnectionConfig &cfg,
|
|
|
|
|
|
std::function<void(ConnectionConfigurationWidget&)> save_func)
|
2019-08-24 20:47:32 +02:00
|
|
|
|
{
|
2019-09-02 16:33:13 +02:00
|
|
|
|
try {
|
2021-07-04 19:33:06 +02:00
|
|
|
|
auto w = new ConnectionConfigurationWidget(connection_controller);
|
2019-09-02 16:33:13 +02:00
|
|
|
|
w->setData(cfg);
|
2019-08-24 20:47:32 +02:00
|
|
|
|
|
2019-09-02 16:33:13 +02:00
|
|
|
|
auto btn_hbox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal);
|
2019-08-24 20:47:32 +02:00
|
|
|
|
|
2019-09-02 16:33:13 +02:00
|
|
|
|
auto vbox = new QVBoxLayout;
|
|
|
|
|
|
vbox->addWidget(w);
|
|
|
|
|
|
vbox->addWidget(btn_hbox);
|
2019-08-24 20:47:32 +02:00
|
|
|
|
|
2019-09-02 16:33:13 +02:00
|
|
|
|
auto win = new QDialog;
|
|
|
|
|
|
win->setWindowTitle(tr("Edit connection configuration"));
|
|
|
|
|
|
win->setLayout(vbox);
|
|
|
|
|
|
win->setAttribute( Qt::WA_DeleteOnClose, true );
|
2019-08-24 20:47:32 +02:00
|
|
|
|
|
2021-07-04 19:33:06 +02:00
|
|
|
|
QObject::connect(btn_hbox, &QDialogButtonBox::accepted, [w, win, save_func] () {
|
|
|
|
|
|
save_func(*w);
|
|
|
|
|
|
win->accept();
|
|
|
|
|
|
});
|
2019-09-02 16:33:13 +02:00
|
|
|
|
QObject::connect(btn_hbox, &QDialogButtonBox::rejected, [win] ()
|
|
|
|
|
|
{
|
|
|
|
|
|
win->reject();
|
|
|
|
|
|
});
|
2019-08-24 20:47:32 +02:00
|
|
|
|
|
2019-09-02 16:33:13 +02:00
|
|
|
|
win->show();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (const std::exception &ex) {
|
|
|
|
|
|
qDebug() << "exception: " << QString::fromUtf8(ex.what());
|
|
|
|
|
|
}
|
2019-08-24 20:47:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-16 17:59:28 +01:00
|
|
|
|
ConnectionConfigurationWidget::ConnectionConfigurationWidget(
|
|
|
|
|
|
ConnectionController *connection_controller,
|
|
|
|
|
|
QWidget *parent)
|
2019-08-24 20:47:32 +02:00
|
|
|
|
: QWidget(parent)
|
2021-07-04 19:33:06 +02:00
|
|
|
|
, m_connectionController(connection_controller)
|
|
|
|
|
|
, m_connectionModel(connection_controller->getConnectionTreeModel())
|
2019-08-24 20:47:32 +02:00
|
|
|
|
{
|
2019-08-27 20:12:00 +02:00
|
|
|
|
lblGroup = new QLabel;
|
|
|
|
|
|
cmbbxGroup = new QComboBox;
|
2021-07-04 19:33:06 +02:00
|
|
|
|
cmbbxGroup->setModel(m_connectionModel);
|
2019-08-27 20:12:00 +02:00
|
|
|
|
cmbbxGroup->setModelColumn(0);
|
|
|
|
|
|
lblGroup->setBuddy(cmbbxGroup);
|
|
|
|
|
|
|
2019-08-24 20:47:32 +02:00
|
|
|
|
lblName = new QLabel;
|
|
|
|
|
|
SET_OBJECT_NAME(lblName);
|
|
|
|
|
|
edtName = new QLineEdit ;
|
|
|
|
|
|
SET_OBJECT_NAME(edtName);
|
|
|
|
|
|
lblName->setBuddy(edtName);
|
|
|
|
|
|
|
|
|
|
|
|
lblHost = new QLabel;
|
|
|
|
|
|
SET_OBJECT_NAME(lblHost);
|
|
|
|
|
|
edtHost = new QLineEdit;
|
|
|
|
|
|
SET_OBJECT_NAME(edtHost);
|
|
|
|
|
|
lblHost->setBuddy(edtHost);
|
|
|
|
|
|
|
|
|
|
|
|
lblPort = new QLabel;
|
|
|
|
|
|
SET_OBJECT_NAME(lblPort);
|
|
|
|
|
|
spinPort = new QSpinBox;
|
|
|
|
|
|
SET_OBJECT_NAME(spinPort);
|
|
|
|
|
|
spinPort->setRange(1, std::numeric_limits<uint16_t>::max());
|
|
|
|
|
|
lblPort->setBuddy(spinPort);
|
|
|
|
|
|
|
|
|
|
|
|
lblUser = new QLabel;
|
|
|
|
|
|
SET_OBJECT_NAME(lblUser);
|
|
|
|
|
|
edtUser = new QLineEdit;
|
|
|
|
|
|
SET_OBJECT_NAME(edtUser);
|
|
|
|
|
|
lblUser->setBuddy(edtUser);
|
|
|
|
|
|
|
2021-07-04 16:45:50 +02:00
|
|
|
|
lblPassword = new QLabel;
|
|
|
|
|
|
SET_OBJECT_NAME(lblPassword);
|
|
|
|
|
|
edtPassword = new QLineEdit;
|
|
|
|
|
|
SET_OBJECT_NAME(edtPassword);
|
|
|
|
|
|
edtPassword->setEchoMode(QLineEdit::PasswordEchoOnEdit);
|
2022-09-05 14:33:51 +02:00
|
|
|
|
connect(edtPassword, &QLineEdit::textEdited, this, &ConnectionConfigurationWidget::passwordEdited);
|
2021-07-04 16:45:50 +02:00
|
|
|
|
|
|
|
|
|
|
cbSavePassword = new QCheckBox;
|
|
|
|
|
|
SET_OBJECT_NAME(cbSavePassword);
|
|
|
|
|
|
|
2019-08-24 20:47:32 +02:00
|
|
|
|
lblDbName = new QLabel;
|
|
|
|
|
|
SET_OBJECT_NAME(lblDbName);
|
2021-07-04 16:45:50 +02:00
|
|
|
|
cmbDbname = new QComboBox;
|
|
|
|
|
|
cmbDbname->setEditable(true); // allows to enter the database name even if test failed.
|
|
|
|
|
|
cmbDbname->setInsertPolicy(QComboBox::InsertAlphabetically);
|
2019-08-24 20:47:32 +02:00
|
|
|
|
|
|
|
|
|
|
lblSsl = new QLabel;
|
|
|
|
|
|
SET_OBJECT_NAME(lblSsl);
|
|
|
|
|
|
cmbbxSsl = new QComboBox;
|
|
|
|
|
|
SET_OBJECT_NAME(cmbbxSsl);
|
|
|
|
|
|
cmbbxSsl->setModelColumn(0);
|
|
|
|
|
|
auto ssl_model = new SslModeModel(this);
|
|
|
|
|
|
cmbbxSsl->setModel(ssl_model);
|
2025-02-23 08:32:15 +01:00
|
|
|
|
cmbbxSsl->setEditable(true);
|
2019-08-24 20:47:32 +02:00
|
|
|
|
lblSsl->setBuddy(cmbbxSsl);
|
|
|
|
|
|
|
|
|
|
|
|
lblCert = new QLabel;
|
|
|
|
|
|
SET_OBJECT_NAME(lblCert);
|
|
|
|
|
|
edtCert = new QLineEdit;
|
|
|
|
|
|
SET_OBJECT_NAME(edtCert);
|
|
|
|
|
|
lblCert->setBuddy(edtCert);
|
|
|
|
|
|
|
|
|
|
|
|
lblKey = new QLabel;
|
|
|
|
|
|
SET_OBJECT_NAME(lblKey);
|
|
|
|
|
|
edtKey = new QLineEdit;
|
|
|
|
|
|
SET_OBJECT_NAME(edtKey);
|
|
|
|
|
|
lblKey->setBuddy(edtKey);
|
|
|
|
|
|
|
|
|
|
|
|
lblRootCert = new QLabel;
|
|
|
|
|
|
SET_OBJECT_NAME(lblRootCert);
|
|
|
|
|
|
edtRootCert = new QLineEdit;
|
|
|
|
|
|
SET_OBJECT_NAME(edtRootCert);
|
|
|
|
|
|
lblRootCert->setBuddy(edtRootCert);
|
|
|
|
|
|
|
|
|
|
|
|
lblCrl = new QLabel;
|
|
|
|
|
|
SET_OBJECT_NAME(lblCrl);
|
|
|
|
|
|
edtCrl = new QLineEdit;
|
|
|
|
|
|
SET_OBJECT_NAME(edtCrl);
|
|
|
|
|
|
lblCrl->setBuddy(edtCrl);
|
|
|
|
|
|
|
2021-07-04 16:45:50 +02:00
|
|
|
|
btnTest = new QPushButton;
|
|
|
|
|
|
connect(btnTest, &QPushButton::clicked, this, &ConnectionConfigurationWidget::testConnection);
|
|
|
|
|
|
|
|
|
|
|
|
lblResult = new QLabel;
|
|
|
|
|
|
|
2019-08-24 20:47:32 +02:00
|
|
|
|
formLayout = new QFormLayout;
|
|
|
|
|
|
|
2019-08-27 20:12:00 +02:00
|
|
|
|
formLayout->addRow(lblGroup, cmbbxGroup);
|
2019-08-24 20:47:32 +02:00
|
|
|
|
formLayout->addRow(lblName, edtName);
|
|
|
|
|
|
formLayout->addRow(lblHost, edtHost);
|
|
|
|
|
|
formLayout->addRow(lblPort, spinPort);
|
|
|
|
|
|
formLayout->addRow(lblUser, edtUser);
|
2021-07-04 16:45:50 +02:00
|
|
|
|
formLayout->addRow(lblPassword, edtPassword);
|
|
|
|
|
|
formLayout->addRow(nullptr, cbSavePassword);
|
|
|
|
|
|
formLayout->addRow(lblDbName, cmbDbname);
|
2019-08-24 20:47:32 +02:00
|
|
|
|
formLayout->addRow(lblSsl, cmbbxSsl);
|
|
|
|
|
|
formLayout->addRow(lblCert, edtCert);
|
|
|
|
|
|
formLayout->addRow(lblKey, edtKey);
|
|
|
|
|
|
formLayout->addRow(lblRootCert, edtRootCert);
|
|
|
|
|
|
formLayout->addRow(lblCrl, edtCrl);
|
2021-07-04 16:45:50 +02:00
|
|
|
|
formLayout->addRow(btnTest, lblResult);
|
|
|
|
|
|
setLayout(formLayout);
|
|
|
|
|
|
|
2019-08-24 20:47:32 +02:00
|
|
|
|
retranslateUi();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConnectionConfigurationWidget::retranslateUi()
|
|
|
|
|
|
{
|
2019-08-27 20:12:00 +02:00
|
|
|
|
lblGroup->setText(QApplication::translate("ConnectionConfigurationWidget", "&Group", nullptr));
|
2019-08-24 20:47:32 +02:00
|
|
|
|
lblName->setText(QApplication::translate("ConnectionConfigurationWidget", "&Name", nullptr));
|
|
|
|
|
|
lblHost->setText(QApplication::translate("ConnectionConfigurationWidget", "&Host", nullptr));
|
|
|
|
|
|
lblPort->setText(QApplication::translate("ConnectionConfigurationWidget", "&Port", nullptr));
|
|
|
|
|
|
lblUser->setText(QApplication::translate("ConnectionConfigurationWidget", "&Username", nullptr));
|
2021-07-04 16:45:50 +02:00
|
|
|
|
lblPassword->setText(QApplication::translate("ConnectionConfigurationWidget", "Password", nullptr));
|
|
|
|
|
|
cbSavePassword->setText(QApplication::translate("ConnectionConfigurationWidget", "&Save password", nullptr));
|
|
|
|
|
|
lblDbName->setText(QApplication::translate("ConnectionConfigurationWidget", "Database", nullptr));
|
2019-08-24 20:47:32 +02:00
|
|
|
|
lblSsl->setText(QApplication::translate("ConnectionConfigurationWidget", "&SSL mode", nullptr));
|
|
|
|
|
|
lblCert->setText(QApplication::translate("ConnectionConfigurationWidget", "&Certificate", nullptr));
|
|
|
|
|
|
lblKey->setText(QApplication::translate("ConnectionConfigurationWidget", "&Key", nullptr));
|
|
|
|
|
|
lblRootCert->setText(QApplication::translate("ConnectionConfigurationWidget", "&Root cert.", nullptr));
|
|
|
|
|
|
lblCrl->setText(QApplication::translate("ConnectionConfigurationWidget", "Revocation &list", nullptr));
|
2021-07-04 16:45:50 +02:00
|
|
|
|
btnTest->setText(QApplication::translate("ConnectionConfigurationWidget", "Test", nullptr));
|
2019-08-24 20:47:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ConnectionConfigurationWidget::setData(const ConnectionConfig &cfg)
|
|
|
|
|
|
{
|
2019-09-01 06:44:48 +02:00
|
|
|
|
auto group = cfg.parent();
|
2019-09-01 06:52:26 +02:00
|
|
|
|
if (group) {
|
|
|
|
|
|
auto group_idx = m_connectionModel->findGroup(group->conngroup_id);
|
|
|
|
|
|
cmbbxGroup->setCurrentIndex(group_idx);
|
|
|
|
|
|
}
|
2019-09-01 06:44:48 +02:00
|
|
|
|
|
2019-08-24 20:47:32 +02:00
|
|
|
|
m_uuid = cfg.uuid();
|
2019-09-16 19:24:39 +02:00
|
|
|
|
edtName->setText(cfg.name());
|
|
|
|
|
|
edtHost->setText(cfg.host());
|
2019-08-24 20:47:32 +02:00
|
|
|
|
spinPort->setValue(cfg.port());
|
2019-09-16 19:24:39 +02:00
|
|
|
|
edtUser->setText(cfg.user());
|
2021-07-04 19:33:06 +02:00
|
|
|
|
edtPassword->setText("");
|
|
|
|
|
|
|
2021-07-04 16:45:50 +02:00
|
|
|
|
cmbDbname->setCurrentText(cfg.dbname());
|
2025-02-23 08:32:15 +01:00
|
|
|
|
cmbbxSsl->setCurrentText(cfg.getParameter("sslmode"));
|
2019-09-16 19:24:39 +02:00
|
|
|
|
edtCert->setText(cfg.sslCert());
|
|
|
|
|
|
edtKey->setText(cfg.sslKey());
|
|
|
|
|
|
edtRootCert->setText(cfg.sslRootCert());
|
|
|
|
|
|
edtCrl->setText(cfg.sslCrl());
|
2021-07-04 19:33:06 +02:00
|
|
|
|
|
2025-03-10 19:08:02 +01:00
|
|
|
|
edtPassword->setText(cfg.password());
|
2021-07-04 19:33:06 +02:00
|
|
|
|
encodedPassword = cfg.encodedPassword();
|
2022-09-05 14:33:51 +02:00
|
|
|
|
cbSavePassword->setCheckState(
|
|
|
|
|
|
encodedPassword.isEmpty()
|
|
|
|
|
|
? Qt::CheckState::Unchecked
|
|
|
|
|
|
: Qt::CheckState::Checked
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
passwordChanged = false;
|
2019-08-24 20:47:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-04 16:45:50 +02:00
|
|
|
|
ConnectionConfig ConnectionConfigurationWidget::data() const
|
2019-08-24 20:47:32 +02:00
|
|
|
|
{
|
|
|
|
|
|
ConnectionConfig cfg;
|
|
|
|
|
|
cfg.setUuid(m_uuid);
|
2019-09-16 19:24:39 +02:00
|
|
|
|
cfg.setName(edtName->text());
|
|
|
|
|
|
cfg.setHost(edtHost->text());
|
2019-08-24 20:47:32 +02:00
|
|
|
|
cfg.setPort(static_cast<uint16_t>(spinPort->value()));
|
2019-09-16 19:24:39 +02:00
|
|
|
|
cfg.setUser(edtUser->text());
|
2021-07-04 16:45:50 +02:00
|
|
|
|
cfg.setPassword(edtPassword->text());
|
|
|
|
|
|
cfg.setDbname(cmbDbname->currentText());
|
2025-03-10 19:08:02 +01:00
|
|
|
|
cfg.setParameter("sslmode", cmbbxSsl->currentText());
|
2019-09-16 19:24:39 +02:00
|
|
|
|
cfg.setSslCert(edtCert->text());
|
|
|
|
|
|
cfg.setSslKey(edtKey->text());
|
|
|
|
|
|
cfg.setSslRootCert(edtRootCert->text());
|
|
|
|
|
|
cfg.setSslCrl(edtCrl->text());
|
2021-07-04 19:33:06 +02:00
|
|
|
|
cfg.setEncodedPassword(encodedPassword); // this could be old, it will be overriden later when there is a new password to save.
|
2021-07-04 16:45:50 +02:00
|
|
|
|
return cfg;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QString ConnectionConfigurationWidget::group() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return cmbbxGroup->currentText();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-10 19:08:02 +01:00
|
|
|
|
bool ConnectionConfigurationWidget::savePasswordEnabled() const
|
2021-07-04 19:33:06 +02:00
|
|
|
|
{
|
2025-03-10 19:08:02 +01:00
|
|
|
|
return cbSavePassword->isChecked();
|
2022-09-05 14:33:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-03-10 19:08:02 +01:00
|
|
|
|
bool ConnectionConfigurationWidget::passwordIsChanged() const
|
2022-09-05 14:33:51 +02:00
|
|
|
|
{
|
2025-03-10 19:08:02 +01:00
|
|
|
|
return passwordChanged;
|
2021-07-04 19:33:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-04 16:45:50 +02:00
|
|
|
|
void ConnectionConfigurationWidget::testConnection()
|
|
|
|
|
|
{
|
2021-07-04 19:33:06 +02:00
|
|
|
|
QString password = edtPassword->text();
|
|
|
|
|
|
if (password.isEmpty() && !encodedPassword.isEmpty()) {
|
|
|
|
|
|
m_connectionController->decodeConnectionPassword(m_uuid, encodedPassword, password);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-04 16:45:50 +02:00
|
|
|
|
auto cc = data();
|
2021-07-04 19:33:06 +02:00
|
|
|
|
cc.setPassword(password);
|
2022-01-16 17:59:28 +01:00
|
|
|
|
|
|
|
|
|
|
auto qthis = QPointer(this);
|
2022-08-14 08:04:21 +02:00
|
|
|
|
QFuture<void> result = QtConcurrent::run(
|
|
|
|
|
|
[cc]
|
|
|
|
|
|
{
|
|
|
|
|
|
return TestConnection(cc);
|
2022-01-16 17:59:28 +01:00
|
|
|
|
}
|
2022-08-14 08:04:21 +02:00
|
|
|
|
).then(qApp,
|
|
|
|
|
|
[qthis](TestConnectionResult result)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (qthis)
|
|
|
|
|
|
qthis.data()->handleTestResult(result);
|
|
|
|
|
|
}
|
|
|
|
|
|
);
|
2021-07-04 16:45:50 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-16 17:59:28 +01:00
|
|
|
|
void ConnectionConfigurationWidget::handleTestResult(TestConnectionResult result)
|
2021-07-04 16:45:50 +02:00
|
|
|
|
{
|
|
|
|
|
|
lblResult->setText(result.message());
|
|
|
|
|
|
QString current = cmbDbname->currentText();
|
|
|
|
|
|
cmbDbname->clear();
|
|
|
|
|
|
for (auto &dbn : result.databases())
|
|
|
|
|
|
cmbDbname->addItem(dbn);
|
|
|
|
|
|
cmbDbname->setCurrentText(current);
|
2019-08-24 20:47:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-09-05 14:33:51 +02:00
|
|
|
|
void ConnectionConfigurationWidget::passwordEdited(const QString &)
|
|
|
|
|
|
{
|
|
|
|
|
|
passwordChanged = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|