diff --git a/src/core/databasefactory.cpp b/src/core/databasefactory.cpp
index 4d05b5c71..fdc8740de 100644
--- a/src/core/databasefactory.cpp
+++ b/src/core/databasefactory.cpp
@@ -34,10 +34,45 @@ DatabaseFactory *DatabaseFactory::instance() {
}
int DatabaseFactory::mysqlTestConnection(const QString &hostname, int port,
- const QString &usernam, const QString &password) {
- // TODO: Otestovat, připojení k databázi pod danými
- // údaji. Vrátit kód chyby. Použije se v dialogu nastavení.
- return 0;
+ const QString &username, const QString &password) {
+ QSqlDatabase database = QSqlDatabase::addDatabase(APP_DB_DRIVER_MYSQL,
+ APP_DB_TEST_MYSQL);
+
+ database.setHostName(hostname);
+ database.setPort(port);
+ database.setUserName(username);
+ database.setPassword(password);
+
+ if (database.open()) {
+ // Connection succeeded, clean up the mess and return 0.
+ database.close();
+ removeConnection(APP_DB_TEST_MYSQL);
+ return 0;
+ }
+ else {
+ // Connection failed, do cleanup and return specific
+ // error code.
+ int error_code = database.lastError().number();
+
+ removeConnection(APP_DB_TEST_MYSQL);
+ return error_code;
+ }
+}
+
+QString DatabaseFactory::mysqlInterpretErrorCode(int error_code) {
+ switch (error_code) {
+ case 0:
+ return QObject::tr("Operation successful.");
+
+ case 2005:
+ return QObject::tr("No MySQL server is running in the target destination.");
+
+ case 1045:
+ return QObject::tr("Access denied. Invalid username or password used.");
+
+ default:
+ return QObject::tr("Unknown error.");
+ }
}
void DatabaseFactory::sqliteAssemblyDatabaseFilePath() {
diff --git a/src/core/databasefactory.h b/src/core/databasefactory.h
index c88dc5138..db4d754c8 100644
--- a/src/core/databasefactory.h
+++ b/src/core/databasefactory.h
@@ -64,9 +64,14 @@ class DatabaseFactory : public QObject {
//
// MySQL stuff.
//
- int mysqlTestConnection(const QString &hostname, int port,
- const QString &usernam, const QString &password);
+ // Tests if database connection with given data
+ // can be established and returns 0 if it can.
+ // Otherwise returns MySQL-specific error code.
+ int mysqlTestConnection(const QString &hostname, int port,
+ const QString &username, const QString &password);
+
+ QString mysqlInterpretErrorCode(int error_code);
private:
//
diff --git a/src/core/defs.h.in b/src/core/defs.h.in
index e85637783..e81684631 100755
--- a/src/core/defs.h.in
+++ b/src/core/defs.h.in
@@ -42,6 +42,7 @@
#define AUTO_UPDATE_INTERVAL 60000
#define STARTUP_UPDATE_DELAY 1500
+#define APP_DB_TEST_MYSQL "MySQLTest"
#define APP_DB_MYSQL_PORT 3306
#define APP_DB_DRIVER_SQLITE "QSQLITE"
#define APP_DB_DRIVER_MYSQL "QMYSQL"
diff --git a/src/core/messagesmodel.cpp b/src/core/messagesmodel.cpp
index 3dfccf0c6..2e464163e 100644
--- a/src/core/messagesmodel.cpp
+++ b/src/core/messagesmodel.cpp
@@ -16,7 +16,6 @@ MessagesModel::MessagesModel(QObject *parent)
DatabaseFactory::instance()->connection("MessagesModel",
DatabaseFactory::FromSettings)) {
setObjectName("MessagesModel");
-
setupFonts();
setupIcons();
setupHeaderData();
diff --git a/src/gui/feedmessageviewer.cpp b/src/gui/feedmessageviewer.cpp
index 4b20402b5..64b63e3b2 100644
--- a/src/gui/feedmessageviewer.cpp
+++ b/src/gui/feedmessageviewer.cpp
@@ -66,12 +66,17 @@ void FeedMessageViewer::saveSize() {
// States of splitters are stored, let's store
// widths of columns.
- settings->setValue(APP_CFG_GUI,
- KEY_MESSAGES_VIEW + QString::number(MSG_DB_AUTHOR_INDEX),
- m_messagesView->columnWidth(MSG_DB_AUTHOR_INDEX));
- settings->setValue(APP_CFG_GUI,
- KEY_MESSAGES_VIEW + QString::number(MSG_DB_DCREATED_INDEX),
- m_messagesView->columnWidth(MSG_DB_DCREATED_INDEX));
+ int width_column_author = m_messagesView->columnWidth(MSG_DB_AUTHOR_INDEX);
+ int width_column_date = m_messagesView->columnWidth(MSG_DB_DCREATED_INDEX);
+
+ if (width_column_author != 0 && width_column_date != 0) {
+ settings->setValue(APP_CFG_GUI,
+ KEY_MESSAGES_VIEW + QString::number(MSG_DB_AUTHOR_INDEX),
+ width_column_author);
+ settings->setValue(APP_CFG_GUI,
+ KEY_MESSAGES_VIEW + QString::number(MSG_DB_DCREATED_INDEX),
+ width_column_date);
+ }
}
void FeedMessageViewer::loadSize() {
diff --git a/src/gui/formsettings.cpp b/src/gui/formsettings.cpp
index d342db0ba..6132e96e3 100755
--- a/src/gui/formsettings.cpp
+++ b/src/gui/formsettings.cpp
@@ -493,12 +493,13 @@ void FormSettings::saveDataStorage() {
}
void FormSettings::mysqlTestConnection() {
- int result = DatabaseFactory::instance()->mysqlTestConnection(m_ui->m_txtMysqlHostname->lineEdit()->text(),
+ int error_code = DatabaseFactory::instance()->mysqlTestConnection(m_ui->m_txtMysqlHostname->lineEdit()->text(),
m_ui->m_spinMysqlPort->value(),
m_ui->m_txtMysqlUsername->lineEdit()->text(),
m_ui->m_txtMysqlPassword->lineEdit()->text());
- // TODO: zobrazit výsledek
+ // Let's interpret the result.
+ m_ui->m_lblMysqlTestResult->setText(DatabaseFactory::instance()->mysqlInterpretErrorCode(error_code));
}
void FormSettings::onMysqlHostnameChanged(const QString &new_hostname) {
diff --git a/src/gui/formsettings.ui b/src/gui/formsettings.ui
index 902c79711..2feecf340 100644
--- a/src/gui/formsettings.ui
+++ b/src/gui/formsettings.ui
@@ -17,7 +17,7 @@
-
- 0
+ 1
@@ -82,7 +82,7 @@
-
- 0
+ 1
@@ -223,17 +223,20 @@ Authors of this application are NOT responsible for lost data.
-
-
-
- Qt::Horizontal
+
+
+
+ 0
+ 0
+
-
-
- 40
- 20
-
+
+ No test run so far.
-
+
+ true
+
+
@@ -248,6 +251,9 @@ Authors of this application are NOT responsible for lost data.
true
+
+ 10
+
@@ -283,8 +289,8 @@ Authors of this application are NOT responsible for lost data.
0
0
- 564
- 363
+ 100
+ 30
@@ -361,8 +367,8 @@ Authors of this application are NOT responsible for lost data.
0
0
- 558
- 337
+ 167
+ 219