-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
228 lines (177 loc) · 7.22 KB
/
MainWindow.cpp
File metadata and controls
228 lines (177 loc) · 7.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QDebug>
#include <QMessageBox>
#include <QtSql/QSqlDatabase>
#include <QSettings>
#include <QToolBar>
#include <QTime>
#include "OpenConnectionDialog.h"
#include "DatabaseConnectionWidget.h"
#include "Tools/SQLSplitterDialog.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(this, SIGNAL(statusEvent(QString)), ui->statusBar, SLOT(showMessage(QString)));
m_currentDatabase = NULL;
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_actionNew_Connection_triggered()
{
// Create the open connection dialog
OpenConnectionDialog connectionDialog(this);
// Show the dialog and wait for user input
if (connectionDialog.exec() == QDialog::Accepted) {
// Create a new database connection widget
DatabaseConnectionWidget *databaseConnectionWidget = new DatabaseConnectionWidget(this);
// Seed random number generator
qsrand(QTime::currentTime().msec());
// Get SSH Tunnel information from dialog
QString sshHostname = connectionDialog.getSshHostname();
int sshPort = connectionDialog.getSshPort();
int sshForwardedPort = qrand() % 1000 + 17000; // Get a random port to forward to between 17000 and 18000
// If we want this to be SSH tunneled
if (connectionDialog.getSshTunnelChecked()) {
// Try to create the tunnel
if (!databaseConnectionWidget->createSshTunnel(sshHostname, connectionDialog.getPort(), sshForwardedPort)) {
// Show message box if we couldn't connect
QMessageBox::critical(this, "Error", "Could not create SSH Tunnel");
// Update status
emit statusEvent("Could not create SSH tunnel");
// Delete this widget
delete databaseConnectionWidget;
// And return to prevent further execution
return;
}
}
// Get database information from dialog
QString name = connectionDialog.getName();
QString driver = connectionDialog.getDriver();
QString hostname = connectionDialog.getHostname();
QString database = connectionDialog.getDatabase();
QString username = connectionDialog.getUsername();
QString password = connectionDialog.getPassword();
int port = connectionDialog.getSshTunnelChecked() ? sshForwardedPort : connectionDialog.getPort();
// Connect to database
if (!databaseConnectionWidget->connectToDatabase(name, driver, hostname, database, username, password, port)) {
// Show message box if we couldn't connect
QMessageBox::critical(this, "Could not connect", databaseConnectionWidget->lastError().databaseText());
// Update status
emit statusEvent(databaseConnectionWidget->lastError().databaseText());
// Delete this widget
delete databaseConnectionWidget;
// And return to prevent further execution
return;
}
// Create icon and this to the list of tabs
ui->databaseConnectionsTabWidget->addTab(databaseConnectionWidget, QIcon::fromTheme("server-database"), databaseConnectionWidget->name());
ui->databaseConnectionsTabWidget->setCurrentWidget(databaseConnectionWidget);
// Set the current database
m_currentDatabase = databaseConnectionWidget;
// Emit status event
emit statusEvent("Connection to database successful");
}
}
void MainWindow::on_actionQuit_triggered()
{
QApplication::quit();
}
void MainWindow::on_actionAbout_Qt_triggered()
{
QMessageBox::aboutQt(this);
}
void MainWindow::on_actionAbout_QuteSql_triggered()
{
QMessageBox::about(this, "About QuteSQL", "QuteSQL developed by James Augustus Zuccon PhD");
}
void MainWindow::on_databaseConnectionsTabWidget_tabCloseRequested(int index)
{
// Get a pointer to the tab instance so we can delete it later (at the end of this function)
DatabaseConnectionWidget *widget = (DatabaseConnectionWidget*)ui->databaseConnectionsTabWidget->widget(index);
// Remove the tab
ui->databaseConnectionsTabWidget->removeTab(index);
// Actually delete the widget
delete widget;
}
void MainWindow::on_databaseConnectionsTabWidget_currentChanged(int index)
{
// Remove old database's toolbars if there is one
if (m_currentDatabase && m_currentDatabase->getExtension()) {
QList<QToolBar*> toolbars = m_currentDatabase->getExtension()->getToolBars();
for (QList<QToolBar*>::iterator i = toolbars.begin(); i != toolbars.end(); ++i) {
removeToolBar((*i));
}
}
// Set the new active database
m_currentDatabase = (DatabaseConnectionWidget*)ui->databaseConnectionsTabWidget->currentWidget();
// Add new database's toolbars
if (m_currentDatabase && m_currentDatabase->getExtension()) {
QList<QToolBar*> toolbars = m_currentDatabase->getExtension()->getToolBars();
for (QList<QToolBar*>::iterator i = toolbars.begin(); i != toolbars.end(); ++i) {
addToolBar((*i));
}
}
// Disable all tools by default
ui->actionImport_Database->setEnabled(false);
ui->actionExport_Database->setEnabled(false);
ui->actionClear_Database->setEnabled(false);
// Enable tools if there's a database selected
if (m_currentDatabase) {
ui->actionImport_Database->setEnabled(true);
// If extensions are supported...
if (m_currentDatabase->getExtension()) {
if (m_currentDatabase->getExtension()->hasCapability(EXPORT_DATABASE)) {
ui->actionExport_Database->setEnabled(true);
}
if (m_currentDatabase->getExtension()->hasCapability(CLEAR_DATABASE)) {
ui->actionClear_Database->setEnabled(true);
}
}
}
}
void MainWindow::on_actionClose_Connection_triggered()
{
// Get the current tab index
int currentTabIndex = ui->databaseConnectionsTabWidget->currentIndex();
// Get a pointer to the widget that the current index refers to so we can delete it later
DatabaseConnectionWidget *widget = (DatabaseConnectionWidget*)ui->databaseConnectionsTabWidget->widget(currentTabIndex);
// Remove the tab
ui->databaseConnectionsTabWidget->removeTab(currentTabIndex);
// Delete it
delete widget;
}
void MainWindow::on_actionRefresh_triggered()
{
// If there is a current database, then refresh it
if (m_currentDatabase) {
m_currentDatabase->refresh();
}
}
void MainWindow::on_actionSplit_SQL_File_triggered()
{
SQLSplitterDialog dialog;
dialog.exec();
}
void MainWindow::on_actionImport_Database_triggered()
{
if (m_currentDatabase) {
m_currentDatabase->importDatabase();
}
}
void MainWindow::on_actionExport_Database_triggered()
{
if (m_currentDatabase && m_currentDatabase->getExtension()->hasCapability(EXPORT_DATABASE)) {
m_currentDatabase->getExtension()->exportDatabase();
}
}
void MainWindow::on_actionClear_Database_triggered()
{
if (m_currentDatabase && m_currentDatabase->getExtension()->hasCapability(CLEAR_DATABASE)) {
m_currentDatabase->getExtension()->clearDatabase();
}
}