2
Copyright (C) 2010 Marco Mentasti <marcomentasti@gmail.com>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License version 2 as published by the Free Software Foundation.
8
This library is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11
Library General Public License for more details.
13
You should have received a copy of the GNU Library General Public License
14
along with this library; see the file COPYING.LIB. If not, write to
15
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16
Boston, MA 02110-1301, USA.
19
#include "katesqlview.h"
20
#include "katesqlplugin.h"
21
#include "sqlmanager.h"
22
#include "connectionmodel.h"
23
#include "textoutputwidget.h"
24
#include "dataoutputwidget.h"
25
#include "dataoutputmodel.h"
26
#include "dataoutputview.h"
27
#include "schemawidget.h"
28
#include "schemabrowserwidget.h"
29
#include "connectionwizard.h"
30
#include "outputwidget.h"
32
#include <ktexteditor/plugin.h>
33
#include <ktexteditor/mainwindow.h>
34
#include <ktexteditor/application.h>
35
#include <ktexteditor/document.h>
36
#include <ktexteditor/view.h>
39
#include <kactioncollection.h>
40
#include <klocalizedstring.h>
42
#include <kconfiggroup.h>
43
#include <kcombobox.h>
45
#include <KSharedConfig>
46
#include <KXMLGUIFactory>
50
#include <qsqlquery.h>
51
#include <QVBoxLayout>
52
#include <QApplication>
54
KateSQLView::KateSQLView(KTextEditor::Plugin *plugin, KTextEditor::MainWindow *mw)
57
, m_manager (new SQLManager(this))
60
KXMLGUIClient::setComponentName (QLatin1String("katesql"), i18n ("Kate SQL Plugin"));
61
setXMLFile( QLatin1String("ui.rc") );
63
m_outputToolView = mw->createToolView(plugin, QLatin1String ("kate_private_plugin_katesql_output"),
64
KTextEditor::MainWindow::Bottom,
65
QIcon::fromTheme (QLatin1String ("view-form-table")),
66
i18nc("@title:window", "SQL Results")
69
m_schemaBrowserToolView = mw->createToolView(plugin, QLatin1String ("kate_private_plugin_katesql_schemabrowser"),
70
KTextEditor::MainWindow::Left,
71
QIcon::fromTheme (QLatin1String ("view-list-tree")),
72
i18nc("@title:window", "SQL Schema Browser")
75
m_outputWidget = new KateSQLOutputWidget(m_outputToolView);
77
m_schemaBrowserWidget = new SchemaBrowserWidget(m_schemaBrowserToolView, m_manager);
79
m_connectionsComboBox = new KComboBox(this);
80
m_connectionsComboBox->setEditable(false);
81
m_connectionsComboBox->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
82
m_connectionsComboBox->setModel(m_manager->connectionModel());
83
// m_connectionsComboBox->setItemDelegate( new ConnectionDelegate(this) );
87
m_mainWindow->guiFactory()->addClient(this);
89
QMenu *sqlMenu = (QMenu*)factory()->container(QLatin1String ("SQL"), this);
91
m_connectionsGroup = new QActionGroup(sqlMenu);
92
m_connectionsGroup->setExclusive(true);
94
connect(sqlMenu, SIGNAL(aboutToShow()), this, SLOT(slotSQLMenuAboutToShow()));
95
connect(m_connectionsGroup, SIGNAL(triggered(QAction*)), this, SLOT(slotConnectionSelectedFromMenu(QAction*)));
97
connect(m_manager, SIGNAL(error(QString)), this, SLOT(slotError(QString)));
98
connect(m_manager, SIGNAL(success(QString)), this, SLOT(slotSuccess(QString)));
99
connect(m_manager, SIGNAL(queryActivated(QSqlQuery&,QString)), this, SLOT(slotQueryActivated(QSqlQuery&,QString)));
100
connect(m_manager, SIGNAL(connectionCreated(QString)), this, SLOT(slotConnectionCreated(QString)));
101
connect(m_manager, SIGNAL(connectionAboutToBeClosed(QString)), this, SLOT(slotConnectionAboutToBeClosed(QString)));
102
connect(m_connectionsComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(slotConnectionChanged(QString)));
104
stateChanged(QLatin1String ("has_connection_selected"), KXMLGUIClient::StateReverse);
108
KateSQLView::~KateSQLView()
110
m_mainWindow->guiFactory()->removeClient( this );
112
delete m_outputToolView;
113
delete m_schemaBrowserToolView;
119
void KateSQLView::setupActions()
122
KActionCollection* collection = actionCollection();
124
action = collection->addAction(QLatin1String ("connection_create"));
125
action->setText( i18nc("@action:inmenu", "Add connection...") );
126
action->setIcon( QIcon::fromTheme (QLatin1String ("list-add")) );
127
connect( action , SIGNAL(triggered()) , this , SLOT(slotConnectionCreate()) );
129
action = collection->addAction(QLatin1String ("connection_remove"));
130
action->setText( i18nc("@action:inmenu", "Remove connection") );
131
action->setIcon( QIcon::fromTheme (QLatin1String ("list-remove")) );
132
connect( action , SIGNAL(triggered()) , this , SLOT(slotConnectionRemove()) );
134
action = collection->addAction(QLatin1String ("connection_edit"));
135
action->setText( i18nc("@action:inmenu", "Edit connection...") );
136
action->setIcon( QIcon::fromTheme (QLatin1String ("configure")) );
137
connect( action , SIGNAL(triggered()) , this , SLOT(slotConnectionEdit()) );
139
action = collection->addAction(QLatin1String ("connection_reconnect"));
140
action->setText( i18nc("@action:inmenu", "Reconnect") );
141
action->setIcon( QIcon::fromTheme (QLatin1String ("view-refresh")) );
142
connect( action , SIGNAL(triggered()) , this , SLOT(slotConnectionReconnect()) );
144
action = collection->addAction(QLatin1String ("connection_chooser"));
145
action->setText( i18nc("@action:intoolbar", "Connection") );
146
// FIXME KF5 action->setDefaultWidget(m_connectionsComboBox);
148
action = collection->addAction(QLatin1String ("query_run"));
149
action->setText( i18nc("@action:inmenu", "Run query") );
150
action->setIcon( QIcon::fromTheme (QLatin1String ("quickopen")) );
151
action->setShortcut( QKeySequence(Qt::CTRL + Qt::Key_E) );
152
connect( action , SIGNAL(triggered()) , this , SLOT(slotRunQuery()));
154
/// TODO: stop sql query
155
// action = collection->addAction("sql_stop");
156
// action->setText( i18n("Stop query") );
157
// action->setIcon( KIcon("process-stop") );
158
// action->setShortcut( QKeySequence(Qt::ALT + Qt::Key_F5) );
159
// connect( action , SIGNAL(triggered()) , this , SLOT(stopQuery()));
163
void KateSQLView::slotSQLMenuAboutToShow()
165
qDeleteAll( m_connectionsGroup->actions() );
167
QMenu *sqlMenu = (QMenu*)factory()->container(QLatin1String ("SQL"), this);
168
QAction *before = action("query_run");
169
QAbstractItemModel *model = m_manager->connectionModel();
171
int rows = model->rowCount(QModelIndex());
173
for (int row = 0; row < rows; row++)
175
QModelIndex index = model->index(row, 0, QModelIndex());
177
Q_ASSERT(index.isValid());
179
QString connectionName = index.data(Qt::DisplayRole).toString();
181
QAction *act = new QAction(connectionName, m_connectionsGroup);
182
act->setCheckable(true);
184
if (m_connectionsComboBox->currentText() == connectionName)
185
act->setChecked(true);
187
sqlMenu->insertAction(before, act);
190
sqlMenu->insertSeparator(before);
194
void KateSQLView::slotConnectionSelectedFromMenu(QAction *action)
196
m_connectionsComboBox->setCurrentItem(action->text());
200
void KateSQLView::slotConnectionChanged(const QString &connection)
202
stateChanged(QLatin1String ("has_connection_selected"), (connection.isEmpty()) ? KXMLGUIClient::StateReverse : KXMLGUIClient::StateNoReverse);
204
m_schemaBrowserWidget->schemaWidget()->buildTree(connection);
208
void KateSQLView::slotGlobalSettingsChanged()
210
m_outputWidget->dataOutputWidget()->model()->readConfig();
214
void KateSQLView::readSessionConfig (KConfigBase* config, const QString& groupPrefix)
216
KConfigGroup globalConfig(KSharedConfig::openConfig(), "KateSQLPlugin");
218
bool saveConnections = globalConfig.readEntry("SaveConnections", true);
220
if (!saveConnections)
223
KConfigGroup group(config, groupPrefix + QLatin1String (":connections"));
225
m_manager->loadConnections(&group);
227
QString lastConnection = group.readEntry("LastUsed");
229
if (m_connectionsComboBox->contains(lastConnection))
230
m_connectionsComboBox->setCurrentItem(lastConnection);
234
void KateSQLView::writeSessionConfig (KConfigBase* config, const QString& groupPrefix)
236
KConfigGroup group(config, groupPrefix + QLatin1String (":connections"));
240
KConfigGroup globalConfig(KSharedConfig::openConfig(), "KateSQLPlugin");
241
bool saveConnections = globalConfig.readEntry("SaveConnections", true);
245
m_manager->saveConnections(&group);
247
group.writeEntry("LastUsed", m_connectionsComboBox->currentText());
254
void KateSQLView::slotConnectionCreate()
258
ConnectionWizard wizard(m_manager, &c);
260
if (wizard.exec() != QDialog::Accepted)
263
for (int i = 1; QSqlDatabase::contains(c.name); i++)
264
c.name = QString::fromLatin1("%1 (%2)").arg(c.name).arg(i);
266
m_manager->createConnection(c);
268
if (m_manager->storeCredentials(c) != 0)
269
qDebug() << "Connection credentials not saved";
273
void KateSQLView::slotConnectionEdit()
275
int i = m_connectionsComboBox->currentIndex();
280
ConnectionModel *model = m_manager->connectionModel();
281
Connection c = model->data(model->index(i), Qt::UserRole).value<Connection>();
283
QString previousName = c.name;
285
ConnectionWizard wizard(m_manager, &c);
287
if (wizard.exec() != QDialog::Accepted)
290
m_manager->removeConnection(previousName);
291
m_manager->createConnection(c);
293
if (m_manager->storeCredentials(c) != 0)
294
qDebug() << "Connection credentials not saved";
298
void KateSQLView::slotConnectionRemove()
300
QString connection = m_connectionsComboBox->currentText();
302
if (!connection.isEmpty())
303
m_manager->removeConnection(connection);
307
void KateSQLView::slotConnectionReconnect()
309
QString connection = m_connectionsComboBox->currentText();
311
if (!connection.isEmpty())
312
m_manager->reopenConnection(connection);
316
void KateSQLView::slotConnectionAboutToBeClosed (const QString& name)
318
/// must delete the QSqlQuery object inside the model before closing connection
320
if (name == m_currentResultsetConnection)
321
m_outputWidget->dataOutputWidget()->clearResults();
325
void KateSQLView::slotRunQuery()
328
/// bind parameters dialog?
330
QString connection = m_connectionsComboBox->currentText();
332
if (connection.isEmpty())
334
slotConnectionCreate();
338
KTextEditor::View *view = m_mainWindow->activeView();
343
QString text = (view->selection()) ? view->selectionText() : view->document()->text();
344
text = text.trimmed();
349
m_manager->runQuery(text, connection);
353
void KateSQLView::slotError(const QString &message)
355
m_outputWidget->textOutputWidget()->showErrorMessage(message);
356
m_outputWidget->setCurrentWidget(m_outputWidget->textOutputWidget());
357
m_mainWindow->showToolView(m_outputToolView);
362
void KateSQLView::slotSuccess(const QString &message)
364
m_outputWidget->textOutputWidget()->showSuccessMessage(message);
365
m_outputWidget->setCurrentWidget(m_outputWidget->textOutputWidget());
366
m_mainWindow->showToolView(m_outputToolView);
371
void KateSQLView::slotQueryActivated(QSqlQuery &query, const QString &connection)
373
if (query.isSelect())
375
m_currentResultsetConnection = connection;
377
m_outputWidget->dataOutputWidget()->showQueryResultSets(query);
378
m_outputWidget->setCurrentWidget(m_outputWidget->dataOutputWidget());
379
m_mainWindow->showToolView(m_outputToolView);
385
void KateSQLView::slotConnectionCreated(const QString &name)
387
m_connectionsComboBox->setCurrentItem(name);
389
m_schemaBrowserWidget->schemaWidget()->buildTree(name);