~ubuntu-branches/ubuntu/vivid/kate/vivid-proposed

« back to all changes in this revision

Viewing changes to addons/katesql/katesqlview.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2014-12-04 16:49:41 UTC
  • mfrom: (1.6.6)
  • Revision ID: package-import@ubuntu.com-20141204164941-l3qbvsly83hhlw2v
Tags: 4:14.11.97-0ubuntu1
* New upstream release
* Update build-deps and use pkg-kde v3 for Qt 5 build
* kate-data now kate5-data for co-installability

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
   Copyright (C) 2010  Marco Mentasti  <marcomentasti@gmail.com>
 
3
 
 
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.
 
7
 
 
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.
 
12
 
 
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.
 
17
*/
 
18
 
 
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"
 
31
 
 
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>
 
37
 
 
38
#include <QAction>
 
39
#include <kactioncollection.h>
 
40
#include <klocalizedstring.h>
 
41
#include <kconfig.h>
 
42
#include <kconfiggroup.h>
 
43
#include <kcombobox.h>
 
44
#include <kconfig.h>
 
45
#include <KSharedConfig>
 
46
#include <KXMLGUIFactory>
 
47
 
 
48
#include <qmenu.h>
 
49
#include <qstring.h>
 
50
#include <qsqlquery.h>
 
51
#include <QVBoxLayout>
 
52
#include <QApplication>
 
53
 
 
54
KateSQLView::KateSQLView(KTextEditor::Plugin *plugin, KTextEditor::MainWindow *mw)
 
55
: QObject (mw)
 
56
, KXMLGUIClient()
 
57
, m_manager (new SQLManager(this))
 
58
, m_mainWindow (mw)
 
59
{
 
60
  KXMLGUIClient::setComponentName (QLatin1String("katesql"), i18n ("Kate SQL Plugin"));
 
61
  setXMLFile( QLatin1String("ui.rc") );
 
62
 
 
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")
 
67
                                               );
 
68
 
 
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")
 
73
                                               );
 
74
 
 
75
  m_outputWidget = new KateSQLOutputWidget(m_outputToolView);
 
76
 
 
77
  m_schemaBrowserWidget = new SchemaBrowserWidget(m_schemaBrowserToolView, m_manager);
 
78
 
 
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) );
 
84
 
 
85
  setupActions();
 
86
 
 
87
  m_mainWindow->guiFactory()->addClient(this);
 
88
 
 
89
  QMenu *sqlMenu = (QMenu*)factory()->container(QLatin1String ("SQL"), this);
 
90
 
 
91
  m_connectionsGroup = new QActionGroup(sqlMenu);
 
92
  m_connectionsGroup->setExclusive(true);
 
93
 
 
94
  connect(sqlMenu, SIGNAL(aboutToShow()), this, SLOT(slotSQLMenuAboutToShow()));
 
95
  connect(m_connectionsGroup, SIGNAL(triggered(QAction*)), this, SLOT(slotConnectionSelectedFromMenu(QAction*)));
 
96
 
 
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)));
 
103
 
 
104
  stateChanged(QLatin1String ("has_connection_selected"), KXMLGUIClient::StateReverse);
 
105
}
 
106
 
 
107
 
 
108
KateSQLView::~KateSQLView()
 
109
{
 
110
  m_mainWindow->guiFactory()->removeClient( this );
 
111
 
 
112
  delete m_outputToolView;
 
113
  delete m_schemaBrowserToolView;
 
114
 
 
115
  delete m_manager;
 
116
}
 
117
 
 
118
 
 
119
void KateSQLView::setupActions()
 
120
{
 
121
  QAction* action;
 
122
  KActionCollection* collection = actionCollection();
 
123
 
 
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()) );
 
128
 
 
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()) );
 
133
 
 
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()) );
 
138
 
 
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()) );
 
143
 
 
144
  action = collection->addAction(QLatin1String ("connection_chooser"));
 
145
  action->setText( i18nc("@action:intoolbar", "Connection") );
 
146
  // FIXME KF5 action->setDefaultWidget(m_connectionsComboBox);
 
147
 
 
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()));
 
153
 
 
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()));
 
160
}
 
161
 
 
162
 
 
163
void KateSQLView::slotSQLMenuAboutToShow()
 
164
{
 
165
  qDeleteAll( m_connectionsGroup->actions() );
 
166
 
 
167
  QMenu *sqlMenu = (QMenu*)factory()->container(QLatin1String ("SQL"), this);
 
168
  QAction *before = action("query_run");
 
169
  QAbstractItemModel *model = m_manager->connectionModel();
 
170
 
 
171
  int rows = model->rowCount(QModelIndex());
 
172
 
 
173
  for (int row = 0; row < rows; row++)
 
174
  {
 
175
    QModelIndex index = model->index(row, 0, QModelIndex());
 
176
 
 
177
    Q_ASSERT(index.isValid());
 
178
 
 
179
    QString connectionName = index.data(Qt::DisplayRole).toString();
 
180
 
 
181
    QAction *act = new QAction(connectionName, m_connectionsGroup);
 
182
    act->setCheckable(true);
 
183
 
 
184
    if (m_connectionsComboBox->currentText() == connectionName)
 
185
      act->setChecked(true);
 
186
 
 
187
    sqlMenu->insertAction(before, act);
 
188
  }
 
189
 
 
190
  sqlMenu->insertSeparator(before);
 
191
}
 
192
 
 
193
 
 
194
void KateSQLView::slotConnectionSelectedFromMenu(QAction *action)
 
195
{
 
196
  m_connectionsComboBox->setCurrentItem(action->text());
 
197
}
 
198
 
 
199
 
 
200
void KateSQLView::slotConnectionChanged(const QString &connection)
 
201
{
 
202
  stateChanged(QLatin1String ("has_connection_selected"), (connection.isEmpty()) ? KXMLGUIClient::StateReverse : KXMLGUIClient::StateNoReverse);
 
203
 
 
204
  m_schemaBrowserWidget->schemaWidget()->buildTree(connection);
 
205
}
 
206
 
 
207
 
 
208
void KateSQLView::slotGlobalSettingsChanged()
 
209
{
 
210
  m_outputWidget->dataOutputWidget()->model()->readConfig();
 
211
}
 
212
 
 
213
 
 
214
void KateSQLView::readSessionConfig (KConfigBase* config, const QString& groupPrefix)
 
215
{
 
216
  KConfigGroup globalConfig(KSharedConfig::openConfig(), "KateSQLPlugin");
 
217
 
 
218
  bool saveConnections = globalConfig.readEntry("SaveConnections", true);
 
219
 
 
220
  if (!saveConnections)
 
221
    return;
 
222
 
 
223
  KConfigGroup group(config, groupPrefix + QLatin1String (":connections"));
 
224
 
 
225
  m_manager->loadConnections(&group);
 
226
 
 
227
  QString lastConnection = group.readEntry("LastUsed");
 
228
 
 
229
  if (m_connectionsComboBox->contains(lastConnection))
 
230
    m_connectionsComboBox->setCurrentItem(lastConnection);
 
231
}
 
232
 
 
233
 
 
234
void KateSQLView::writeSessionConfig (KConfigBase* config, const QString& groupPrefix)
 
235
{
 
236
  KConfigGroup group(config, groupPrefix + QLatin1String (":connections"));
 
237
 
 
238
  group.deleteGroup();
 
239
 
 
240
  KConfigGroup globalConfig(KSharedConfig::openConfig(), "KateSQLPlugin");
 
241
  bool saveConnections = globalConfig.readEntry("SaveConnections", true);
 
242
 
 
243
  if (saveConnections)
 
244
  {
 
245
    m_manager->saveConnections(&group);
 
246
 
 
247
    group.writeEntry("LastUsed", m_connectionsComboBox->currentText());
 
248
  }
 
249
 
 
250
  config->sync();
 
251
}
 
252
 
 
253
 
 
254
void KateSQLView::slotConnectionCreate()
 
255
{
 
256
  Connection c;
 
257
 
 
258
  ConnectionWizard wizard(m_manager, &c);
 
259
 
 
260
  if (wizard.exec() != QDialog::Accepted)
 
261
    return;
 
262
 
 
263
  for (int i = 1; QSqlDatabase::contains(c.name); i++)
 
264
    c.name = QString::fromLatin1("%1 (%2)").arg(c.name).arg(i);
 
265
 
 
266
  m_manager->createConnection(c);
 
267
 
 
268
  if (m_manager->storeCredentials(c) != 0)
 
269
    qDebug() << "Connection credentials not saved";
 
270
}
 
271
 
 
272
 
 
273
void KateSQLView::slotConnectionEdit()
 
274
{
 
275
  int i = m_connectionsComboBox->currentIndex();
 
276
 
 
277
  if (i == -1)
 
278
    return;
 
279
 
 
280
  ConnectionModel *model = m_manager->connectionModel();
 
281
  Connection c = model->data(model->index(i), Qt::UserRole).value<Connection>();
 
282
 
 
283
  QString previousName = c.name;
 
284
 
 
285
  ConnectionWizard wizard(m_manager, &c);
 
286
 
 
287
  if (wizard.exec() != QDialog::Accepted)
 
288
    return;
 
289
 
 
290
  m_manager->removeConnection(previousName);
 
291
  m_manager->createConnection(c);
 
292
 
 
293
  if (m_manager->storeCredentials(c) != 0)
 
294
    qDebug() << "Connection credentials not saved";
 
295
}
 
296
 
 
297
 
 
298
void KateSQLView::slotConnectionRemove()
 
299
{
 
300
  QString connection = m_connectionsComboBox->currentText();
 
301
 
 
302
  if (!connection.isEmpty())
 
303
    m_manager->removeConnection(connection);
 
304
}
 
305
 
 
306
 
 
307
void KateSQLView::slotConnectionReconnect()
 
308
{
 
309
  QString connection = m_connectionsComboBox->currentText();
 
310
 
 
311
  if (!connection.isEmpty())
 
312
    m_manager->reopenConnection(connection);
 
313
}
 
314
 
 
315
 
 
316
void KateSQLView::slotConnectionAboutToBeClosed (const QString& name)
 
317
{
 
318
  /// must delete the QSqlQuery object inside the model before closing connection
 
319
 
 
320
  if (name == m_currentResultsetConnection)
 
321
    m_outputWidget->dataOutputWidget()->clearResults();
 
322
}
 
323
 
 
324
 
 
325
void KateSQLView::slotRunQuery()
 
326
{
 
327
  /// TODO:
 
328
  /// bind parameters dialog?
 
329
 
 
330
  QString connection = m_connectionsComboBox->currentText();
 
331
 
 
332
  if (connection.isEmpty())
 
333
  {
 
334
    slotConnectionCreate();
 
335
    return;
 
336
  }
 
337
 
 
338
  KTextEditor::View *view = m_mainWindow->activeView();
 
339
 
 
340
  if (!view)
 
341
    return;
 
342
 
 
343
  QString text = (view->selection()) ? view->selectionText() : view->document()->text();
 
344
  text = text.trimmed();
 
345
 
 
346
  if (text.isEmpty())
 
347
    return;
 
348
 
 
349
  m_manager->runQuery(text, connection);
 
350
}
 
351
 
 
352
 
 
353
void KateSQLView::slotError(const QString &message)
 
354
{
 
355
  m_outputWidget->textOutputWidget()->showErrorMessage(message);
 
356
  m_outputWidget->setCurrentWidget(m_outputWidget->textOutputWidget());
 
357
  m_mainWindow->showToolView(m_outputToolView);
 
358
  
 
359
}
 
360
 
 
361
 
 
362
void KateSQLView::slotSuccess(const QString &message)
 
363
{
 
364
  m_outputWidget->textOutputWidget()->showSuccessMessage(message);
 
365
  m_outputWidget->setCurrentWidget(m_outputWidget->textOutputWidget());
 
366
  m_mainWindow->showToolView(m_outputToolView);
 
367
 
 
368
}
 
369
 
 
370
 
 
371
void KateSQLView::slotQueryActivated(QSqlQuery &query, const QString &connection)
 
372
{
 
373
  if (query.isSelect())
 
374
  {
 
375
    m_currentResultsetConnection = connection;
 
376
 
 
377
    m_outputWidget->dataOutputWidget()->showQueryResultSets(query);
 
378
    m_outputWidget->setCurrentWidget(m_outputWidget->dataOutputWidget());
 
379
    m_mainWindow->showToolView(m_outputToolView);
 
380
    
 
381
  }
 
382
}
 
383
 
 
384
 
 
385
void KateSQLView::slotConnectionCreated(const QString &name)
 
386
{
 
387
  m_connectionsComboBox->setCurrentItem(name);
 
388
 
 
389
  m_schemaBrowserWidget->schemaWidget()->buildTree(name);
 
390
}
 
391
 
 
392
//END KateSQLView
 
393