Switched ConnectionConfig to QString from std::string to fit better into Qt framework
This commit is contained in:
parent
bcfd82c27d
commit
082293e58a
20 changed files with 1077 additions and 211 deletions
|
|
@ -169,16 +169,16 @@ void ConnectionConfigurationWidget::setData(const ConnectionConfig &cfg)
|
|||
}
|
||||
|
||||
m_uuid = cfg.uuid();
|
||||
edtName->setText(stdStrToQ(cfg.name()));
|
||||
edtHost->setText(stdStrToQ(cfg.host()));
|
||||
edtName->setText(cfg.name());
|
||||
edtHost->setText(cfg.host());
|
||||
spinPort->setValue(cfg.port());
|
||||
edtUser->setText(stdStrToQ(cfg.user()));
|
||||
edtDbname->setText(stdStrToQ(cfg.dbname()));
|
||||
edtUser->setText(cfg.user());
|
||||
edtDbname->setText(cfg.dbname());
|
||||
cmbbxSsl->setCurrentIndex(static_cast<int>(cfg.sslMode()));
|
||||
edtCert->setText(stdStrToQ(cfg.sslCert()));
|
||||
edtKey->setText(stdStrToQ(cfg.sslKey()));
|
||||
edtRootCert->setText(stdStrToQ(cfg.sslRootCert()));
|
||||
edtCrl->setText(stdStrToQ(cfg.sslCrl()));
|
||||
edtCert->setText(cfg.sslCert());
|
||||
edtKey->setText(cfg.sslKey());
|
||||
edtRootCert->setText(cfg.sslRootCert());
|
||||
edtCrl->setText(cfg.sslCrl());
|
||||
}
|
||||
|
||||
std::tuple<QString, ConnectionConfig> ConnectionConfigurationWidget::data() const
|
||||
|
|
@ -188,16 +188,16 @@ std::tuple<QString, ConnectionConfig> ConnectionConfigurationWidget::data() cons
|
|||
|
||||
ConnectionConfig cfg;
|
||||
cfg.setUuid(m_uuid);
|
||||
cfg.setName(qStrToStd(edtName->text()));
|
||||
cfg.setHost(qStrToStd(edtHost->text()));
|
||||
cfg.setName(edtName->text());
|
||||
cfg.setHost(edtHost->text());
|
||||
cfg.setPort(static_cast<uint16_t>(spinPort->value()));
|
||||
cfg.setUser(qStrToStd(edtUser->text()));
|
||||
cfg.setDbname(qStrToStd(edtDbname->text()));
|
||||
cfg.setUser(edtUser->text());
|
||||
cfg.setDbname(edtDbname->text());
|
||||
cfg.setSslMode(static_cast<SslMode>(cmbbxSsl->currentIndex()));
|
||||
cfg.setSslCert(qStrToStd(edtCert->text()));
|
||||
cfg.setSslKey(qStrToStd(edtKey->text()));
|
||||
cfg.setSslRootCert(qStrToStd(edtRootCert->text()));
|
||||
cfg.setSslCrl(qStrToStd(edtCrl->text()));
|
||||
cfg.setSslCert(edtCert->text());
|
||||
cfg.setSslKey(edtKey->text());
|
||||
cfg.setSslRootCert(edtRootCert->text());
|
||||
cfg.setSslCrl(edtCrl->text());
|
||||
return { group, cfg };
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@
|
|||
#include <QMessageBox>
|
||||
#include <QTimer>
|
||||
|
||||
|
||||
ConnectionController::ConnectionController(MasterController *parent)
|
||||
: QObject(parent)
|
||||
, m_masterController(parent)
|
||||
|
|
@ -146,11 +145,12 @@ void ConnectionController::openServerWindowForConnection(QModelIndex index)
|
|||
bool ConnectionController::retrieveConnectionPassword(ConnectionConfig &cc)
|
||||
{
|
||||
auto enc_pwd = cc.encodedPassword();
|
||||
if (!enc_pwd.empty()) {
|
||||
if (!enc_pwd.isEmpty()) {
|
||||
std::string pw;
|
||||
bool result = decodePassword(getPskId(cc), cc.encodedPassword(), pw);// getPasswordFromPskdb(getPskId(cc), pw);
|
||||
bool result = decodePassword(getPskId(cc),
|
||||
std::string_view(enc_pwd.data(), enc_pwd.size()) , pw);// getPasswordFromPskdb(getPskId(cc), pw);
|
||||
if (result) {
|
||||
cc.setPassword(pw);
|
||||
cc.setPassword(QString::fromUtf8(pw.data(), pw.size()));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
@ -163,12 +163,14 @@ bool ConnectionController::retrieveConnectionPassword(ConnectionConfig &cc)
|
|||
int exec_result = dlg->exec();
|
||||
|
||||
if (exec_result == QDialog::Accepted) {
|
||||
std::string password = dlg->password().toUtf8().data();
|
||||
auto password = dlg->password();
|
||||
cc.setPassword(password);
|
||||
if (dlg->saveChecked()) {
|
||||
auto ba = password.toUtf8();
|
||||
std::string pw(ba.data(), static_cast<size_t>(ba.size()));
|
||||
std::string encoded_pw;
|
||||
if (encodePassword(getPskId(cc), password, encoded_pw)) {
|
||||
cc.setEncodedPassword(encoded_pw);
|
||||
if (encodePassword(getPskId(cc), pw, encoded_pw)) {
|
||||
cc.setEncodedPassword({ encoded_pw.data(), static_cast<int>(encoded_pw.size()) });
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
|
@ -176,7 +178,7 @@ bool ConnectionController::retrieveConnectionPassword(ConnectionConfig &cc)
|
|||
return false;
|
||||
}
|
||||
|
||||
bool ConnectionController::decodePassword(const std::string &password_id, const std::string &enc_password, std::string &password)
|
||||
bool ConnectionController::decodePassword(const std::string &password_id, const std::string_view &enc_password, std::string &password)
|
||||
{
|
||||
if (!UnlockPasswordManagerIfNeeded())
|
||||
return false;
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#define CONNECTIONCONTROLLER_H
|
||||
|
||||
#include <QObject>
|
||||
#include <string_view>
|
||||
|
||||
class MasterController;
|
||||
class ConnectionConfig;
|
||||
|
|
@ -56,7 +57,7 @@ private:
|
|||
*/
|
||||
bool retrieveConnectionPassword(ConnectionConfig &cc);
|
||||
|
||||
bool decodePassword(const std::string &password_id, const std::string &enc_password, std::string &password);
|
||||
bool decodePassword(const std::string &password_id, const std::string_view &enc_password, std::string &password);
|
||||
bool encodePassword(const std::string &password_id, const std::string &password, std::string &enc_password);
|
||||
|
||||
///
|
||||
|
|
|
|||
|
|
@ -4,11 +4,13 @@
|
|||
|
||||
#include <botan/cryptobox.h>
|
||||
#include <QDir>
|
||||
#include <QMimeData>
|
||||
#include <QSettings>
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlError>
|
||||
#include <QSqlQuery>
|
||||
#include <QStandardPaths>
|
||||
#include <QStringBuilder>
|
||||
|
||||
|
||||
namespace {
|
||||
|
|
@ -24,19 +26,19 @@ CREATE TABLE IF NOT EXISTS conngroup (
|
|||
R"__(
|
||||
CREATE TABLE IF NOT EXISTS connection (
|
||||
uuid TEXT PRIMARY KEY,
|
||||
cname TEXT NOT NULL,
|
||||
cname TEXT,
|
||||
conngroup_id INTEGER NOT NULL,
|
||||
host TEXT NOT NULL,
|
||||
hostaddr TEXT NOT NULL,
|
||||
host TEXT ,
|
||||
hostaddr TEXT ,
|
||||
port INTEGER NOT NULL,
|
||||
user TEXT NOT NULL,
|
||||
dbname TEXT NOT NULL,
|
||||
user TEXT ,
|
||||
dbname TEXT ,
|
||||
sslmode INTEGER NOT NULL,
|
||||
sslcert TEXT NOT NULL,
|
||||
sslkey TEXT NOT NULL,
|
||||
sslrootcert TEXT NOT NULL,
|
||||
sslcrl TEXT NOT NULL,
|
||||
password TEXT NOT NULL
|
||||
sslcert TEXT ,
|
||||
sslkey TEXT ,
|
||||
sslrootcert TEXT ,
|
||||
sslcrl TEXT ,
|
||||
password TEXT
|
||||
);)__";
|
||||
|
||||
|
||||
|
|
@ -68,22 +70,23 @@ R"__(INSERT OR REPLACE INTO connection
|
|||
QSqlQuery q(db);
|
||||
q.prepare(q_insert_or_replace_into_connection);
|
||||
q.bindValue(":uuid", cc.uuid().toString());
|
||||
q.bindValue(":name", stdStrToQ(cc.name()));
|
||||
q.bindValue(":name", cc.name());
|
||||
q.bindValue(":conngroup_id", conngroup_id);
|
||||
q.bindValue(":host", stdStrToQ(cc.host()));
|
||||
q.bindValue(":hostaddr", stdStrToQ(cc.hostAddr()));
|
||||
q.bindValue(":port", cc.port());
|
||||
q.bindValue(":user", stdStrToQ(cc.user()));
|
||||
q.bindValue(":dbname", stdStrToQ(cc.dbname()));
|
||||
q.bindValue(":host", cc.host());
|
||||
q.bindValue(":hostaddr", cc.hostAddr());
|
||||
q.bindValue(":port", (int)cc.port());
|
||||
q.bindValue(":user", cc.user());
|
||||
q.bindValue(":dbname", cc.dbname());
|
||||
q.bindValue(":sslmode", static_cast<int>(cc.sslMode()));
|
||||
q.bindValue(":sslcert", stdStrToQ(cc.sslCert()));
|
||||
q.bindValue(":sslkey", stdStrToQ(cc.sslKey()));
|
||||
q.bindValue(":sslrootcert", stdStrToQ(cc.sslRootCert()));
|
||||
q.bindValue(":sslcrl", stdStrToQ(cc.sslCrl()));
|
||||
q.bindValue(":password", stdStrToQ(cc.encodedPassword()));
|
||||
q.bindValue(":sslcert", cc.sslCert());
|
||||
q.bindValue(":sslkey", cc.sslKey());
|
||||
q.bindValue(":sslrootcert", cc.sslRootCert());
|
||||
q.bindValue(":sslcrl", cc.sslCrl());
|
||||
q.bindValue(":password", cc.encodedPassword());
|
||||
|
||||
if (!q.exec()) {
|
||||
return q.lastError();
|
||||
auto sql_error = q.lastError();
|
||||
return { sql_error };
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
|
@ -129,18 +132,18 @@ void ConnectionTreeModel::load()
|
|||
while (q.next()) {
|
||||
auto cc = std::make_shared<ConnectionConfig>();
|
||||
cc->setUuid(q.value(0).toUuid());
|
||||
cc->setName(qvarToStdStr(q.value(1)));
|
||||
cc->setHost(qvarToStdStr(q.value(3)));
|
||||
cc->setHostAddr(qvarToStdStr(q.value(4)));
|
||||
cc->setName(q.value(1).toString());
|
||||
cc->setHost(q.value(3).toString());
|
||||
cc->setHostAddr(q.value(4).toString());
|
||||
cc->setPort(static_cast<uint16_t>(q.value(5).toInt()));
|
||||
cc->setUser(qvarToStdStr(q.value(6)));
|
||||
cc->setDbname(qvarToStdStr(q.value(7)));
|
||||
cc->setUser(q.value(6).toString());
|
||||
cc->setDbname(q.value(7).toString());
|
||||
cc->setSslMode(static_cast<SslMode>(q.value(8).toInt()));
|
||||
cc->setSslCert(qvarToStdStr(q.value(9)));
|
||||
cc->setSslKey(qvarToStdStr(q.value(10)));
|
||||
cc->setSslRootCert(qvarToStdStr(q.value(11)));
|
||||
cc->setSslCrl(qvarToStdStr(q.value(12)));
|
||||
cc->setEncodedPassword(qvarToStdStr(q.value(13)));
|
||||
cc->setSslCert(q.value(9).toString());
|
||||
cc->setSslKey(q.value(10).toString());
|
||||
cc->setSslRootCert(q.value(11).toString());
|
||||
cc->setSslCrl(q.value(12).toString());
|
||||
cc->setEncodedPassword(q.value(13).toByteArray());
|
||||
|
||||
int group_id = q.value(2).toInt();
|
||||
auto find_res = std::find_if(m_groups.begin(), m_groups.end(),
|
||||
|
|
@ -173,11 +176,11 @@ QVariant ConnectionTreeModel::data(const QModelIndex &index, int role) const
|
|||
// This is a connection
|
||||
if (role == Qt::DisplayRole) {
|
||||
switch (index.column()) {
|
||||
case Name: v = stdStrToQ(conn->name()); break;
|
||||
case Host: v = stdStrToQ(conn->host()); break;
|
||||
case Name: v = conn->name(); break;
|
||||
case Host: v = conn->host(); break;
|
||||
case Port: v = conn->port(); break;
|
||||
case User: v = stdStrToQ(conn->user()); break;
|
||||
case DbName: v= stdStrToQ(conn->dbname()); break;
|
||||
case User: v = conn->user(); break;
|
||||
case DbName: v= conn->dbname(); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -338,7 +341,11 @@ void ConnectionTreeModel::save(const QString &group_name, const ConnectionConfig
|
|||
new_grp->add(node);
|
||||
auto save_res = saveToDb(*node);
|
||||
if (save_res) {
|
||||
throw std::runtime_error("SqlError2");
|
||||
QString msg = save_res->text()
|
||||
% "\n" % save_res->driverText()
|
||||
% "\n" % save_res->databaseText();
|
||||
|
||||
throw std::runtime_error(msg.toUtf8().data());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -446,3 +453,68 @@ std::optional<QSqlError> ConnectionTreeModel::saveToDb(const ConnectionConfig &c
|
|||
{
|
||||
return SaveConnectionConfig(m_db, cc, cc.parent()->conngroup_id);
|
||||
}
|
||||
|
||||
|
||||
Qt::DropActions ConnectionTreeModel::supportedDropActions() const
|
||||
{
|
||||
return Qt::MoveAction;
|
||||
}
|
||||
|
||||
Qt::DropActions ConnectionTreeModel::supportedDragActions() const
|
||||
{
|
||||
return Qt::MoveAction;
|
||||
}
|
||||
|
||||
Qt::ItemFlags ConnectionTreeModel::flags(const QModelIndex &index) const
|
||||
{
|
||||
Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);
|
||||
|
||||
ConnectionConfig* cfg = getConfigFromModelIndex(index);
|
||||
|
||||
if (cfg)
|
||||
return Qt::ItemIsDragEnabled | defaultFlags;
|
||||
else
|
||||
return Qt::ItemIsDropEnabled | defaultFlags;
|
||||
}
|
||||
|
||||
//bool ConnectionTreeModel::insertRows(int row, int count, const QModelIndex &parent)
|
||||
//{
|
||||
// return false;
|
||||
//}
|
||||
|
||||
//bool ConnectionTreeModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild)
|
||||
//{
|
||||
// return false;
|
||||
//}
|
||||
namespace {
|
||||
const auto mimeType = "application/vnd.pgLab.connection";
|
||||
}
|
||||
|
||||
QStringList ConnectionTreeModel::mimeTypes() const
|
||||
{
|
||||
return { mimeType };
|
||||
}
|
||||
|
||||
QMimeData *ConnectionTreeModel::mimeData(const QModelIndexList &indexes) const
|
||||
{
|
||||
QMimeData *mimeData = new QMimeData;
|
||||
QByteArray encodedData;
|
||||
|
||||
QDataStream stream(&encodedData, QIODevice::WriteOnly);
|
||||
|
||||
for (const QModelIndex &index : indexes) {
|
||||
if (index.isValid()) {
|
||||
QString text = data(index, Qt::DisplayRole).toString();
|
||||
stream << text;
|
||||
}
|
||||
}
|
||||
|
||||
mimeData->setData(mimeType, encodedData);
|
||||
return mimeData;
|
||||
}
|
||||
|
||||
bool ConnectionTreeModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent)
|
||||
{
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -80,6 +80,17 @@ private:
|
|||
int findGroup(const QString &name) const;
|
||||
|
||||
std::optional<QSqlError> saveToDb(const ConnectionConfig &cc);
|
||||
|
||||
// QAbstractItemModel interface
|
||||
public:
|
||||
virtual Qt::DropActions supportedDropActions() const override;
|
||||
virtual Qt::DropActions supportedDragActions() const override;
|
||||
virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
|
||||
// virtual bool insertRows(int row, int count, const QModelIndex &parent) override;
|
||||
// virtual bool moveRows(const QModelIndex &sourceParent, int sourceRow, int count, const QModelIndex &destinationParent, int destinationChild) override;
|
||||
virtual QStringList mimeTypes() const override;
|
||||
virtual QMimeData *mimeData(const QModelIndexList &indexes) const override;
|
||||
virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override;
|
||||
};
|
||||
|
||||
#endif // CONNECTIONLISTMODEL_H
|
||||
|
|
|
|||
|
|
@ -18,6 +18,8 @@ ConnectionManagerWindow::ConnectionManagerWindow(MasterController *master, QWidg
|
|||
{
|
||||
ui->setupUi(this);
|
||||
ui->treeView->setModel(m_connectionController->getConnectionTreeModel());
|
||||
ui->treeView->setDragDropMode(QAbstractItemView::InternalMove);
|
||||
|
||||
connect(ui->treeView, &QTreeView::activated, this,
|
||||
&ConnectionManagerWindow::connectionActivated);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -434,7 +434,7 @@ std::tuple<bool, CrudModel::ModifiedRow> CrudModel::updateRow(const PendingRow &
|
|||
int row_number = pending_row.row();
|
||||
Pgsql::Connection db_update_conn;
|
||||
auto dbconfig = m_database->config();
|
||||
db_update_conn.connect(dbconfig.getKeywords(), dbconfig.getValues(), false);
|
||||
db_update_conn.connect(dbconfig.connectionString().toStdString().c_str());
|
||||
auto result = db_update_conn.queryParam(buffer, params);
|
||||
if (result && result.rows() == 1) {
|
||||
|
||||
|
|
@ -523,7 +523,7 @@ std::tuple<bool, QString> CrudModel::removeRows(const std::set<IntegerRange<int>
|
|||
try {
|
||||
Pgsql::Connection db_update_conn;
|
||||
auto dbconfig = m_database->config();
|
||||
db_update_conn.connect(dbconfig.getKeywords(), dbconfig.getValues(), false);
|
||||
db_update_conn.connect(dbconfig.connectionString().toStdString().c_str());
|
||||
|
||||
// First delete rows in table
|
||||
QString delete_statement = createDeleteStatement();
|
||||
|
|
|
|||
|
|
@ -94,7 +94,7 @@ void DatabaseWindow::setConfig(const ConnectionConfig &config)
|
|||
m_config = config;
|
||||
try {
|
||||
QString title = "pglab - ";
|
||||
title += m_config.name().c_str();
|
||||
title += m_config.name();
|
||||
setWindowTitle(title);
|
||||
|
||||
auto f = TaskExecutor::run(new LoadCatalog(m_config));
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
#include "Pgsql_Connection.h"
|
||||
#include "Pgsql_PgException.h"
|
||||
#include "model/TypeSelectionItemModel.h"
|
||||
#include <QDebug>
|
||||
|
||||
OpenDatabase::OpenDatabaseSPtr OpenDatabase::createOpenDatabase(const ConnectionConfig &cfg)
|
||||
{
|
||||
|
|
@ -27,10 +28,13 @@ OpenDatabase::~OpenDatabase() = default;
|
|||
void OpenDatabase::Init()
|
||||
{
|
||||
Pgsql::Connection conn;
|
||||
auto kw = m_config.getKeywords();
|
||||
auto vals = m_config.getValues();
|
||||
conn.connect(kw, vals, 0);
|
||||
m_catalog->loadAll(conn, nullptr);
|
||||
std::string connstr = m_config.connectionString().toStdString();
|
||||
if (conn.connect(connstr.c_str())) {
|
||||
m_catalog->loadAll(conn, nullptr);
|
||||
}
|
||||
else {
|
||||
qDebug() << "Connect failed connstr: " << connstr.c_str();
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<PgDatabaseCatalog> OpenDatabase::catalog()
|
||||
|
|
|
|||
|
|
@ -40,6 +40,6 @@ void ServerWindow::setConfig(const ConnectionConfig &config)
|
|||
qWarning() << ex.text();
|
||||
}
|
||||
QString title = "pglab - ";
|
||||
title += m_config.name().c_str();
|
||||
title += m_config.name();
|
||||
setWindowTitle(title);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue