/
dbconfwindow.cpp
399 lines (354 loc) · 12.8 KB
/
dbconfwindow.cpp
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#include "dbconfwindow.h"
#include <QDebug>
#include <QLabel>
#include <QLineEdit>
#include <QCheckBox>
#include <QComboBox>
#include <QPushButton>
#include <QSpinBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QFormLayout>
#include <QHostInfo>
#include <QHostAddress>
#include <QList>
#include <QWidget>
#include <QFileDialog>
#include <QMessageBox>
#include <QTextEdit>
#include <QScrollBar>
#include "dbconsts.h"
#include "dbsettings.h"
#include "dbcon.h"
// TODO: Add lines for warnings while entering text, after (partial) validation
DBConfWindow::DBConfWindow(DBSettings* _dbs, QFrame* _parent)
: QFrame(_parent),
dbSettings(_dbs),
saved(false) {
// First, check if the DBSettings object is valid. This constructor does not
// need a valid settings object to begin with (if this is the first run of
// an app, the database settings have probably not been configured yet).
if (!dbSettings) {
dbSettings = new DBSettings("/tmp/dbc/dbsettings.ini", this);
tempSettings = true;
}
else {
tempSettings = false;
}
QFormLayout* form = new QFormLayout();
// Database type widget
dbTypeWidget = new QComboBox();
// NOTE: Order of widgets matches order defined by DBSettings::DBType
dbTypeWidget->addItem(tr("MySQL"));
dbTypeWidget->addItem(tr("Microsoft SQL or Similar"));
dbTypeWidget->addItem(tr("Postgre SQL"));
dbTypeWidget->addItem(tr("SQLite v2"));
dbTypeWidget->addItem(tr("SQLite v3"));
// Have to connect after adding the widgets or segfaults happen (b/c)
// the other widgets are not created yet when trying to enable/disable them
// in dbTypeChanged().
connect(dbTypeWidget, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
this, &DBConfWindow::dbTypeChanged);
// Database host/IP widget
dbHostWidget = new QLineEdit();
dbHostWidget->setPlaceholderText(dbSettings->dbHost().toString());
// TODO: check out potential for QValidator, confirm correct signal
connect(dbHostWidget, &QLineEdit::textChanged,
this, &DBConfWindow::dbHostChanged);
// Database port widget
dbPortWidget = new QSpinBox();
dbPortWidget->setMinimum(0);
dbPortWidget->setMaximum(65535);
// Database name widget
dbNameWidget = new QLineEdit();
dbNameWidget->setPlaceholderText(dbSettings->dbName());
connect(dbNameWidget, &QLineEdit::textChanged,
this, &DBConfWindow::dbNameChanged);
// Database user widget
dbUserWidget = new QLineEdit();
dbUserWidget->setPlaceholderText(dbSettings->dbUser());
connect(dbUserWidget, &QLineEdit::textChanged,
this, &DBConfWindow::dbUserChanged);
// Database password widget
dbPassWidget = new QLineEdit();
QString placeholder = "Set password...";
if (dbSettings->dbPass() != "") {
placeholder = "Change password...";
}
dbPassWidget->setPlaceholderText(placeholder);
dbPassWidget->setEchoMode(QLineEdit::Password);
connect(dbPassWidget, &QLineEdit::textChanged,
this, &DBConfWindow::dbPassChanged);
showPassButton = new QPushButton("Show Password");
showPassButton->setCheckable(true);
connect(showPassButton, &QPushButton::clicked, this, &DBConfWindow::showPass);
showPassButton->setChecked(false);
dbPassWGroup = new QWidget();
// dbPassWGroup->setContentsMargins(0, 0, 0, 0);
QHBoxLayout* passLayout = new QHBoxLayout(dbPassWGroup);
passLayout->setContentsMargins(0, 0, 0, 0);
passLayout->addWidget(dbPassWidget);
passLayout->addWidget(showPassButton);
// Database file widget
dbFileWidget = new QLineEdit();
dbFileWidget->setPlaceholderText("File path");
connect(dbFileWidget, &QLineEdit::textChanged,
this, &DBConfWindow::dbFileChanged);
dbFileLocateButton = new QPushButton("Find File");
connect(dbFileLocateButton, &QPushButton::clicked,
this, &DBConfWindow::locateDBFile);
dbFileWGroup = new QWidget();
// dbFileWGroup->setContentsMargins(0, 0, 0, 0);
QHBoxLayout* fileLayout = new QHBoxLayout(dbFileWGroup);
fileLayout->setContentsMargins(0, 0, 0, 0);
fileLayout->addWidget(dbFileWidget);
fileLayout->addWidget(dbFileLocateButton);
// Now that everything is created and pointers are validated, we can set
// some defaults
if (tempSettings) {
dbTypeWidget->setCurrentIndex((int)DBSettings::MYSQL);
dbPortWidget->setValue(getDefaultPort());
}
else {
dbTypeWidget->setCurrentIndex((int)dbSettings->dbType());
dbPortWidget->setValue(dbSettings->dbPort());
// TODO: set dbFile also
}
dbTypeChanged((int)DBSettings::MYSQL);
// Add the input widgets to the form layout
form->addRow(tr("Database &Type:"), dbTypeWidget);
form->addRow(tr("IP/&Hostname:"), dbHostWidget);
form->addRow(tr("P&ort Number:"), dbPortWidget);
form->addRow(tr("Database &Name:"), dbNameWidget);
form->addRow(tr("Database &User:"), dbUserWidget);
form->addRow(tr("User's &Password:"), dbPassWGroup);
form->addRow(tr("SQLite 2/3 &File:"), dbFileWGroup);
// Create the "Do not show this window again" button
// TODO: Make this checkbox actually hide the window in the future
QCheckBox* hideWindowInFuture = new QCheckBox("Do not show this window at startup.");
hideWindowInFuture->setChecked(false);
// Create the test and save buttons
QHBoxLayout* buttons = new QHBoxLayout();
testButton = new QPushButton("Test Connection");
connect(testButton, &QPushButton::pressed,
this, &DBConfWindow::testConnection);
verifyTablesButton = new QPushButton("Verify Tables");
connect(verifyTablesButton, &QPushButton::pressed,
this, &DBConfWindow::verifyTables);
saveButton = new QPushButton("Save Settings");
connect(saveButton, &QPushButton::pressed,
this, &DBConfWindow::saveDBSettings);
buttons->addWidget(testButton);
buttons->addWidget(verifyTablesButton);
buttons->addWidget(saveButton);
// Create the status message area
statusBox = new QTextEdit();
statusBox->setReadOnly(true);
// statusBox->setStyleSheet("QTextArea {background-color: black;}");
// statusBox->setTextBackgroundColor(Qt::black);
// statusBox->setTextColor(Qt::green);
statusBox->setVisible(false);
statusScroll = statusBox->verticalScrollBar();
// Add everything to the main layout
QVBoxLayout* layout = new QVBoxLayout(this);
layout->addLayout(form);
QFrame* line = new QFrame();
line->setFrameShape(QFrame::HLine);
line->setFrameShadow(QFrame::Sunken);
layout->addWidget(line);
layout->addWidget(hideWindowInFuture);
layout->addLayout(buttons);
layout->addWidget(statusBox);
show();
}
DBConfWindow::~DBConfWindow() {
if (!saved) {
QMessageBox msgBox;
msgBox.setText("The database configuration has been modified.");
msgBox.setInformativeText("Do you want to save your changes?");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
switch (ret) {
case QMessageBox::Save:
// Save was clicked
saveDBSettings();
break;
case QMessageBox::Discard:
// Don't Save was clicked
break;
case QMessageBox::Cancel:
// Cancel was clicked
// TODO: this message box thing should not be in the destructor, but should trap the close signal
break;
default:
// should never be reached
break;
}
}
}
void DBConfWindow::scrollToEnd() {
statusScroll->setValue(statusScroll->maximum());
}
void DBConfWindow::testConnection() {
statusBox->setVisible(true);
QString dbTypeStr = dbSettings->dbTypeStr();
statusBox->insertPlainText(tr("\nVerifying %1 driver is installed...").arg(dbTypeStr));
bool isValidDBDriver = DBCon::isValidDatabaseDriver("Q" + dbTypeStr);
if (isValidDBDriver) {
statusBox->insertPlainText(tr("yes\n"));
}
else {
statusBox->setTextColor(Qt::red);
statusBox->insertPlainText(tr("no"));
statusBox->setTextColor(Qt::black);
statusBox->insertPlainText("\n");
}
statusBox->insertPlainText(tr("Attempting to connect to database...\n"));
DBCon dbc(dbSettings, "test-database");
if (dbc.isValid()) {
statusBox->insertPlainText(tr("Database connection driver is valid\n"));
}
if (dbc.isOpenError()) {
statusBox->setTextColor(Qt::red);
statusBox->insertPlainText(tr("Error opening database connection:\n"));
statusBox->insertPlainText(dbc.lastError());
statusBox->setTextColor(Qt::black);
statusBox->insertPlainText("\n");
}
else if (dbc.isOpen()) {
statusBox->insertPlainText(tr("Connection to database has been opened successfully!\n"));
statusBox->setTextColor(Qt::blue);
statusBox->insertPlainText(tr("You should save the database settings now."));
statusBox->setTextColor(Qt::black);
}
scrollToEnd();
}
void DBConfWindow::verifyTables() {
DBCon dbc(dbSettings, "check-tables");
if (dbc.isOpen()) {
QString tableCheck = "SHOW TABLES;";
statusBox->insertPlainText(tr("\nVerifying db tables exist...\n"));
QSqlQuery results = dbc.execQuery(tableCheck);
QStringList dbTableList;
while (results.next()) {
QString dbt = results.value(0).toString();
dbTableList.append(dbt);
}
DBTableList expected = dbSettings->getTables();
foreach (DBTable* table, expected) {
statusBox->insertPlainText("-> " + table->name() + ": ");
if (dbTableList.contains(table->name())) {
statusBox->setTextColor(Qt::green);
statusBox->insertPlainText(tr("exists"));
} else {
statusBox->setTextColor(Qt::red);
statusBox->insertPlainText(tr("missing"));
}
statusBox->setTextColor(Qt::black);
scrollToEnd();
statusBox->insertPlainText("\n");
}
}
}
void DBConfWindow::saveDBSettings() {
if (tempSettings) {
QString appDir = dbSettings->appRootDir();
// QString filename = QFileDialog::getOpenFileName(this,
// tr("Select Database file"), appDir);
QString newConfigDir = appDir + "/config";
QString filename = newConfigDir + "/db-settings.ini";
dbSettings->exportSettingsFile(filename, true);
dbSettings->setConfigDir(newConfigDir);
tempSettings = false;
}
else {
dbSettings->saveSettingsFile();
}
statusBox->insertPlainText(tr("Database settings saved successfully!\n"));
saved = true;
emit savedDBSettings();
}
void DBConfWindow::dbTypeChanged(int index) {
saved = false;
DBSettings::DBType type = (DBSettings::DBType) index;
int defaultPort = getDefaultPort();
if (defaultPort) {
// If this is a remote database (anything not SQLite)
bool remote = (type == DBSettings::MYSQL) ||
(type == DBSettings::ODBC) ||
(type == DBSettings::PSQL);
dbHostWidget->setEnabled(remote);
dbPortWidget->setEnabled(remote);
dbNameWidget->setEnabled(remote);
dbUserWidget->setEnabled(remote);
dbPassWGroup->setEnabled(remote);
dbFileWGroup->setEnabled(!remote);
dbPortWidget->setValue(defaultPort);
}
dbSettings->setDBType(type);
}
void DBConfWindow::dbHostChanged(QString hostText) {
saved = false;
QHostAddress hostAddr;
// Check if valid IP address
if (hostAddr.setAddress(hostText)) {
qDebug() << "IP Address is valid";
}
// Check if valid Hostname
else {
QHostInfo info = QHostInfo::fromName(hostText);
QList<QHostAddress> addrs = info.addresses();
// qDebug() << "Found host(s) for reverse lookup of" << hostText << ":" << addrs;
if (!addrs.size()) {
qWarning() << "Warning: Invalid database host";
return;
}
hostAddr = addrs.at(0);
}
dbSettings->setDBHost(hostAddr);
}
void DBConfWindow::dbPortChanged(quint16 port) {
saved = false;
dbSettings->setDBPort(port);
}
void DBConfWindow::dbNameChanged(QString name) {
saved = false;
dbSettings->setDBName(name);
}
void DBConfWindow::dbUserChanged(QString user) {
saved = false;
dbSettings->setDBUser(user);
}
void DBConfWindow::dbPassChanged(QString pass) {
saved = false;
dbSettings->setDBPass(pass);
}
void DBConfWindow::dbFileChanged(QString file) {
saved = false;
dbSettings->setDBFile(file);
}
void DBConfWindow::showPass(bool checked) {
dbPassWidget->setEchoMode(checked ? QLineEdit::Normal : QLineEdit::Password);
}
void DBConfWindow::locateDBFile() {
QString currentDir = dbFileWidget->text();
QString dbFile = QFileDialog::getOpenFileName(this,
tr("SQLite Database File Select"), currentDir);
dbFileChanged(dbFile);
}
/*
* Sets the default port number for the currently selected database type.
*/
quint16 DBConfWindow::getDefaultPort() {
DBSettings::DBType type = (DBSettings::DBType) dbTypeWidget->currentIndex();
switch (type) {
case DBSettings::MYSQL: return DEFAULT_PORT_MYSQL; break;
case DBSettings::ODBC: return DEFAULT_PORT_MSSQL; break;
case DBSettings::PSQL: return DEFAULT_PORT_PSQL; break;
case DBSettings::SQLITE2:
case DBSettings::SQLITE:
default:
return -1;
}
}