Commit aa1842c0 authored by Artyom Kirnev's avatar Artyom Kirnev Committed by Christoph Cullmann
Browse files

Bug: 411920

Implement SessionConfigInterface in sql plugin (and expanded it's functionality to store SQLite connections)
parent 7e8ca516
Pipeline #105015 passed with stage
in 3 minutes and 16 seconds
......@@ -192,19 +192,9 @@ void KateSQLView::slotGlobalSettingsChanged()
m_outputWidget->dataOutputWidget()->model()->readConfig();
}
void KateSQLView::readSessionConfig(KConfigBase *config, const QString &groupPrefix)
void KateSQLView::readSessionConfig(KConfigGroup const &group)
{
KConfigGroup globalConfig(KSharedConfig::openConfig(), "KateSQLPlugin");
bool saveConnections = globalConfig.readEntry("SaveConnections", true);
if (!saveConnections) {
return;
}
KConfigGroup group(config, groupPrefix + QLatin1String(":connections"));
m_manager->loadConnections(&group);
m_manager->loadConnections(group);
QString lastConnection = group.readEntry("LastUsed");
......@@ -213,10 +203,8 @@ void KateSQLView::readSessionConfig(KConfigBase *config, const QString &groupPre
}
}
void KateSQLView::writeSessionConfig(KConfigBase *config, const QString &groupPrefix)
void KateSQLView::writeSessionConfig(KConfigGroup &group)
{
KConfigGroup group(config, groupPrefix + QLatin1String(":connections"));
group.deleteGroup();
KConfigGroup globalConfig(KSharedConfig::openConfig(), "KateSQLPlugin");
......@@ -227,8 +215,7 @@ void KateSQLView::writeSessionConfig(KConfigBase *config, const QString &groupPr
group.writeEntry("LastUsed", m_connectionsComboBox->currentText());
}
config->sync();
group.config()->sync();
}
void KateSQLView::slotConnectionCreate()
......
......@@ -32,17 +32,18 @@ class QActionGroup;
#include <KXMLGUIClient>
#include <ktexteditor/mainwindow.h>
#include <ktexteditor/sessionconfiginterface.h>
class KateSQLView : public QObject, public KXMLGUIClient
class KateSQLView : public QObject, public KXMLGUIClient, public KTextEditor::SessionConfigInterface
{
Q_OBJECT
Q_INTERFACES(KTextEditor::SessionConfigInterface)
public:
KateSQLView(KTextEditor::Plugin *plugin, KTextEditor::MainWindow *mw);
~KateSQLView() override;
void readSessionConfig(KConfigBase *config, const QString &groupPrefix);
void writeSessionConfig(KConfigBase *config, const QString &groupPrefix);
void readSessionConfig(const KConfigGroup &group) override;
void writeSessionConfig(KConfigGroup &group) override;
SchemaBrowserWidget *schemaBrowserWidget() const
{
......
......@@ -179,11 +179,6 @@ Wallet *SQLManager::openWallet()
// return 0 on success, -1 on error, -2 on user reject
int SQLManager::storeCredentials(const Connection &conn)
{
// Sqlite is without password, avoid to open wallet
if (conn.driver.contains(QLatin1String("QSQLITE"))) {
return 0;
}
Wallet *wallet = openWallet();
if (!wallet) { // user reject
......@@ -193,13 +188,20 @@ int SQLManager::storeCredentials(const Connection &conn)
QMap<QString, QString> map;
map[QStringLiteral("driver")] = conn.driver.toUpper();
map[QStringLiteral("hostname")] = conn.hostname.toUpper();
map[QStringLiteral("port")] = QString::number(conn.port);
map[QStringLiteral("database")] = conn.database.toUpper();
map[QStringLiteral("username")] = conn.username;
map[QStringLiteral("password")] = conn.password;
map[QStringLiteral("options")] = conn.options;
return (wallet->writeMap(conn.name, map) == 0) ? 0 : -1;
// Sqlite is without password
if (conn.driver.contains(QLatin1String("QSQLITE"))) {
map[QStringLiteral("database")] = conn.database;
} else {
map[QStringLiteral("database")] = conn.database.toUpper();
map[QStringLiteral("username")] = conn.username;
map[QStringLiteral("password")] = conn.password;
map[QStringLiteral("hostname")] = conn.hostname.toUpper();
map[QStringLiteral("port")] = QString::number(conn.port);
}
const int result = (wallet->writeMap(conn.name, map) == 0) ? 0 : -1;
return result;
}
// return 0 on success, -1 on error or not found, -2 on user reject
......@@ -240,23 +242,24 @@ void SQLManager::removeConnection(const QString &name)
Q_EMIT connectionRemoved(name);
}
/// TODO: read KUrl instead of QString for sqlite paths
void SQLManager::loadConnections(KConfigGroup *connectionsGroup)
void SQLManager::loadConnections(const KConfigGroup &connectionsGroup)
{
Connection c;
const auto groupList = connectionsGroup.groupList();
const auto groupList = connectionsGroup->groupList();
for (const QString &groupName : groupList) {
qDebug() << "reading group:" << groupName;
KConfigGroup group = connectionsGroup->group(groupName);
KConfigGroup group = connectionsGroup.group(groupName);
c.name = groupName;
c.driver = group.readEntry("driver");
c.database = group.readEntry("database");
c.options = group.readEntry("options");
if (!c.driver.contains(QLatin1String("QSQLITE"))) {
if (c.driver.contains(QLatin1String("QSQLITE"))) {
c.database = QUrl(group.readEntry("database")).path();
} else {
c.database = group.readEntry("database");
c.hostname = group.readEntry("hostname");
c.username = group.readEntry("username");
c.port = group.readEntry("port", 0);
......@@ -277,33 +280,34 @@ void SQLManager::loadConnections(KConfigGroup *connectionsGroup)
void SQLManager::saveConnections(KConfigGroup *connectionsGroup)
{
// qDebug() << "Saving " << m_model->rowCount() << " groups";
for (int i = 0; i < m_model->rowCount(); i++) {
saveConnection(connectionsGroup, m_model->data(m_model->index(i), Qt::UserRole).value<Connection>());
}
}
/// TODO: write KUrl instead of QString for sqlite paths
void SQLManager::saveConnection(KConfigGroup *connectionsGroup, const Connection &conn)
{
qDebug() << "saving connection" << conn.name;
// qDebug() << "saving connection " << conn.name;
KConfigGroup group = connectionsGroup->group(conn.name);
group.writeEntry("driver", conn.driver);
group.writeEntry("database", conn.database);
group.writeEntry("options", conn.options);
if (!conn.driver.contains(QLatin1String("QSQLITE"))) {
group.writeEntry("hostname", conn.hostname);
group.writeEntry("username", conn.username);
group.writeEntry("port", conn.port);
if (conn.driver.contains(QLatin1String("QSQLITE"))) {
group.writeEntry("database", QUrl::fromLocalFile(conn.database));
return;
}
group.writeEntry("database", conn.database);
group.writeEntry("hostname", conn.hostname);
group.writeEntry("username", conn.username);
group.writeEntry("port", conn.port);
}
void SQLManager::runQuery(const QString &text, const QString &connection)
{
qDebug() << "connection:" << connection;
qDebug() << "text:" << text;
// qDebug() << "connection:" << connection;
// qDebug() << "text:" << text;
if (text.isEmpty()) {
return;
......
......@@ -9,10 +9,12 @@
class ConnectionModel;
class KConfigGroup;
class QUrl;
#include "connection.h"
#include <KWallet>
#include <QSqlQuery>
#include <QUrl>
class SQLManager : public QObject
{
......@@ -34,7 +36,7 @@ public:
public Q_SLOTS:
void removeConnection(const QString &name);
void reopenConnection(const QString &name);
void loadConnections(KConfigGroup *connectionsGroup);
void loadConnections(const KConfigGroup &connectionsGroup);
void saveConnections(KConfigGroup *connectionsGroup);
void runQuery(const QString &text, const QString &connection);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment