-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOpenConnectionDialog.cpp
More file actions
333 lines (277 loc) · 10.2 KB
/
OpenConnectionDialog.cpp
File metadata and controls
333 lines (277 loc) · 10.2 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "OpenConnectionDialog.h"
#include "ui_OpenConnectionDialog.h"
#include <QSqlDatabase>
#include <QFileDialog>
#include <QSettings>
#include <QStandardPaths>
#include <QDebug>
OpenConnectionDialog::OpenConnectionDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::OpenConnectionDialog)
{
ui->setupUi(this);
// Load saved connections
reloadConnections();
// Setup list of drivers for selection
foreach (QString driver, QSqlDatabase::drivers()) {
ui->driverCombo->addItem(driver);
}
}
OpenConnectionDialog::~OpenConnectionDialog()
{
// Clear out SavedConnections
qDeleteAll(savedConnections);
savedConnections.clear();
// Delete UI
delete ui;
}
Connection * OpenConnectionDialog::getConnection()
{
Connection *connection = new Connection;
connection->driver = ui->driverCombo->currentText();
}
QString OpenConnectionDialog::getName()
{
if (getDriver() == "QMYSQL" || getDriver() == "QMYSQL3") {
QString name = getDatabase() + " - " + getUsername() + "@" + getHostname() + ":" + QString::number(getPort());
if (getSshTunnelChecked()) {
name += " (" + getSshHostname() + ":" + QString::number(getSshPort()) + ")";
}
return name;
}
QUrl url(ui->databaseEdit->text());
return url.fileName();
}
QString OpenConnectionDialog::getDriver()
{
return ui->driverCombo->currentText();
}
QString OpenConnectionDialog::getHostname()
{
return ui->hostnameEdit->text();
}
QString OpenConnectionDialog::getDatabase()
{
return ui->databaseEdit->text();
}
QString OpenConnectionDialog::getUsername()
{
return ui->usernameEdit->text();
}
QString OpenConnectionDialog::getPassword()
{
return ui->passwordEdit->text();
}
int OpenConnectionDialog::getPort()
{
return ui->portSpinBox->text().toInt();
}
bool OpenConnectionDialog::getSshTunnelChecked()
{
return ui->sshTunnelCheckBox->isChecked();
}
QString OpenConnectionDialog::getSshHostname()
{
return ui->sshHostnameEdit->text();
}
int OpenConnectionDialog::getSshPort()
{
return ui->sshPortSpinBox->text().toInt();
}
void OpenConnectionDialog::on_driverCombo_currentIndexChanged(const QString &arg1)
{
// Disable fields if using SQLite
if (arg1 == "QSQLITE") {
ui->fileButton->setEnabled(true);
ui->usernameEdit->setEnabled(false);
ui->passwordEdit->setEnabled(false);
ui->hostnameEdit->setEnabled(false);
ui->portSpinBox->setEnabled(false);
ui->sshTunnelCheckBox->setEnabled(false);
}
// Enable all fields if using MySQL
else {
ui->fileButton->setEnabled(false);
ui->usernameEdit->setEnabled(true);
ui->passwordEdit->setEnabled(true);
ui->hostnameEdit->setEnabled(true);
ui->portSpinBox->setEnabled(true);
ui->portSpinBox->setValue(3306);
ui->sshTunnelCheckBox->setEnabled(true);
// Check to see if ssh is on this computer
if (QStandardPaths::findExecutable("ssh").isEmpty()) {
ui->sshTunnelCheckBox->setEnabled(false);
ui->sshTunnelCheckBox->setToolTip("SSH could not be found on this machine.");
}
}
}
void OpenConnectionDialog::on_fileButton_clicked()
{
// Show dialog and get filename
QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::homePath(), tr("SQL Files (*.sqlite)"));
ui->databaseEdit->setText(fileName);
}
void OpenConnectionDialog::on_addButton_clicked()
{
// Only add connection if there's actually text there
if (getName().length()) {
// Add connection from settings to list
Connection *newConnection = new Connection;
newConnection->name = getName();
newConnection->database = getDatabase();
newConnection->driver = getDriver();
newConnection->hostname = getHostname();
newConnection->username = getUsername();
newConnection->password = getPassword();
newConnection->port = getPort();
newConnection->sshTunnel = getSshTunnelChecked();
newConnection->sshHostname = getSshHostname();
newConnection->sshPort = getSshPort();
// Append to saved connections
savedConnections.append(newConnection);
// Save connections
QSettings settings;
settings.beginWriteArray("connections");
int i = 0;
foreach(Connection* connection, savedConnections)
{
settings.setArrayIndex(i);
// Add connection from settings to list
settings.setValue("name", connection->name);
settings.setValue("database", connection->database);
settings.setValue("driver", connection->driver);
settings.setValue("hostname", connection->hostname);
settings.setValue("username", connection->username);
settings.setValue("password", connection->password);
settings.setValue("port", connection->port);
settings.setValue("sshTunnel", connection->sshTunnel);
settings.setValue("sshHostname", connection->sshHostname);
settings.setValue("sshPort", connection->sshPort);
i++;
}
settings.endArray();
// Reload connections widget
reloadConnections();
}
}
void OpenConnectionDialog::on_connectionsListWidget_itemActivated(QListWidgetItem *item)
{
Connection *connection;
QList<Connection*>::iterator i;
for (i = savedConnections.begin(); i != savedConnections.end(); ++i) {
if ((*i)->name == item->text()) {
connection = *i;
break;
}
}
int driver = ui->driverCombo->findText(connection->driver);
ui->driverCombo->setCurrentIndex(driver);
ui->databaseEdit->setText(connection->database);
ui->usernameEdit->setText(connection->username);
ui->passwordEdit->setText(connection->password);
ui->hostnameEdit->setText(connection->hostname);
ui->portSpinBox->setValue(connection->port);
ui->sshTunnelCheckBox->setChecked(connection->sshTunnel);
ui->sshHostnameEdit->setText(connection->sshHostname);
ui->sshPortSpinBox->setValue(connection->sshPort);
}
void OpenConnectionDialog::reloadConnections()
{
// Clear out SavedConnections
qDeleteAll(savedConnections);
savedConnections.clear();
// Clear out the list widget
ui->connectionsListWidget->clear();
// Load saved connections
QSettings settings;
int size = settings.beginReadArray("connections");
for (int i = 0; i < size; i++) {
settings.setArrayIndex(i);
// If filter is empty or the value in the filter matches the name, add it
if (ui->filterEdit->text().isEmpty() || settings.value("name").toString().contains(ui->filterEdit->text())) {
// Add connection from settings to list
Connection *connection = new Connection;
connection->name = settings.value("name").toString();
connection->database = settings.value("database").toString();
connection->driver = settings.value("driver").toString();
connection->hostname = settings.value("hostname").toString();
connection->username = settings.value("username").toString();
connection->password = settings.value("password").toString();
connection->port = settings.value("port").toInt();
connection->sshTunnel = settings.value("sshTunnel").toBool();
connection->sshHostname = settings.value("sshHostname").toString();
connection->sshPort = settings.value("sshPort").toInt();
// Append to saved connections
savedConnections.append(connection);
// Add item to combo box
ui->connectionsListWidget->addItem(connection->name);
}
}
settings.endArray();
ui->connectionsListWidget->sortItems();
}
void OpenConnectionDialog::on_removeButton_clicked()
{
// Get current item
QListWidgetItem *item = ui->connectionsListWidget->currentItem();
if (item) {
QList<Connection*>::iterator i;
for (i = savedConnections.begin(); i != savedConnections.end(); ++i) {
if (item->text() == "-- New Connection --") {
break;
}
if ((*i)->name == item->text()) {
savedConnections.removeOne(*i);
break;
}
}
// Save connections
QSettings settings;
settings.beginWriteArray("connections");
int j = 0;
foreach(Connection* connection, savedConnections)
{
settings.setArrayIndex(j);
// Add connection from settings to list
settings.setValue("name", connection->name);
settings.setValue("database", connection->database);
settings.setValue("driver", connection->driver);
settings.setValue("hostname", connection->hostname);
settings.setValue("username", connection->username);
settings.setValue("password", connection->password);
settings.setValue("port", connection->port);
settings.setValue("sshTunnel", connection->sshTunnel);
settings.setValue("sshHostname", connection->sshHostname);
settings.setValue("sshPort", connection->sshPort);
j++;
}
settings.endArray();
// Reload connections widget
reloadConnections();
}
}
void OpenConnectionDialog::on_newConnectionButton_clicked()
{
Connection *connection = new Connection;
int driver = ui->driverCombo->findText(connection->driver);
ui->driverCombo->setCurrentIndex(driver);
ui->databaseEdit->setText(connection->database);
ui->usernameEdit->setText(connection->username);
ui->passwordEdit->setText(connection->password);
ui->hostnameEdit->setText(connection->hostname);
ui->portSpinBox->setValue(connection->port);
ui->sshTunnelCheckBox->setChecked(false);
}
void OpenConnectionDialog::on_sshTunnelCheckBox_toggled(bool checked)
{
ui->sshGroupBox->setEnabled(checked);
ui->sshGroupBox->setVisible(checked);
ui->hostnameEdit->setReadOnly(checked);
if (checked) {
ui->hostnameEdit->setText("127.0.0.1");
}
}
void OpenConnectionDialog::on_filterEdit_textChanged(const QString &arg1)
{
reloadConnections();
}