MySQL backend now takes data from settings.

This commit is contained in:
Martin Rotter 2014-02-08 09:12:00 +01:00
parent 8da06ad5d3
commit eda6837197
5 changed files with 201 additions and 95 deletions

View File

@ -33,6 +33,13 @@ DatabaseFactory *DatabaseFactory::instance() {
return s_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;
}
void DatabaseFactory::sqliteAssemblyDatabaseFilePath() {
if (Settings::instance()->type() == Settings::Portable) {
m_sqliteDatabaseFilePath = qApp->applicationDirPath() +
@ -106,7 +113,7 @@ QSqlDatabase DatabaseFactory::sqliteInitializeInMemoryDatabase() {
}
// Loading messages from file-based database.
QSqlDatabase file_database = connection(objectName(), StrictlyFileBased);
QSqlDatabase file_database = sqliteConnection(objectName(), StrictlyFileBased);
QSqlQuery copy_contents(database);
// Attach database.
@ -245,8 +252,8 @@ void DatabaseFactory::removeConnection(const QString &connection_name) {
void DatabaseFactory::sqliteSaveMemoryDatabase() {
qDebug("Saving in-memory working database back to persistent file-based storage.");
QSqlDatabase database = connection(objectName(), StrictlyInMemory);
QSqlDatabase file_database = connection(objectName(), StrictlyFileBased);
QSqlDatabase database = sqliteConnection(objectName(), StrictlyInMemory);
QSqlDatabase file_database = sqliteConnection(objectName(), StrictlyFileBased);
QSqlQuery copy_contents(database);
// Attach database.
@ -317,11 +324,11 @@ QSqlDatabase DatabaseFactory::mysqlConnection(const QString &connection_name) {
// yet, add it and set it up.
database = QSqlDatabase::addDatabase(APP_DB_DRIVER_MYSQL, connection_name);
database.setHostName("localhost");
database.setPort(3306);
database.setUserName("root");
database.setDatabaseName("rssguard");
//database.setPassword("password");
database.setHostName(Settings::instance()->value(APP_CFG_DB, "mysql_hostname").toString());
database.setPort(Settings::instance()->value(APP_CFG_DB, "mysql_port", APP_DB_MYSQL_PORT).toInt());
database.setUserName(Settings::instance()->value(APP_CFG_DB, "mysql_username").toString());
database.setPassword(Settings::instance()->value(APP_CFG_DB, "mysql_password").toString());
database.setDatabaseName(APP_LOW_NAME);
}
if (!database.isOpen() && !database.open()) {
@ -343,10 +350,10 @@ QSqlDatabase DatabaseFactory::mysqlInitializeDatabase(const QString &connection_
QSqlDatabase database = QSqlDatabase::addDatabase(APP_DB_DRIVER_MYSQL,
connection_name);
database.setHostName("localhost");
database.setPort(3306);
database.setUserName("root");
//database.setPassword("password");
database.setHostName(Settings::instance()->value(APP_CFG_DB, "mysql_hostname").toString());
database.setPort(Settings::instance()->value(APP_CFG_DB, "mysql_port", APP_DB_MYSQL_PORT).toInt());
database.setUserName(Settings::instance()->value(APP_CFG_DB, "mysql_username").toString());
database.setPassword(Settings::instance()->value(APP_CFG_DB, "mysql_password").toString());
if (!database.open()) {
qFatal("MySQL database was NOT opened. Delivered error message: '%s'",
@ -392,8 +399,7 @@ QSqlDatabase DatabaseFactory::mysqlInitializeDatabase(const QString &connection_
query_db.next();
qDebug("MySQL database connection '%s' seems to be established.",
qPrintable(connection_name),
qPrintable(QDir::toNativeSeparators(database.databaseName())));
qPrintable(connection_name));
qDebug("MySQL database has version '%s'.", qPrintable(query_db.value(0).toString()));
}
@ -476,6 +482,13 @@ QSqlDatabase DatabaseFactory::sqliteConnection(const QString &connection_name,
}
}
bool DatabaseFactory::sqliteVacuumDatabase() {
QSqlDatabase database = sqliteConnection(objectName(), FromSettings);
QSqlQuery query_vacuum(database);
return query_vacuum.exec("VACUUM");
}
void DatabaseFactory::saveDatabase() {
switch (m_activeDatabaseDriver) {
case SQLITE_MEMORY:
@ -490,13 +503,8 @@ void DatabaseFactory::saveDatabase() {
bool DatabaseFactory::vacuumDatabase() {
switch (m_activeDatabaseDriver) {
case SQLITE_MEMORY:
case SQLITE: {
QSqlDatabase database = connection(objectName(), FromSettings);
QSqlQuery query_vacuum(database);
return query_vacuum.exec("VACUUM");
break;
}
case SQLITE:
return sqliteVacuumDatabase();
case MYSQL:
default:

View File

@ -61,6 +61,13 @@ class DatabaseFactory : public QObject {
// Singleton getter.
static DatabaseFactory *instance();
//
// MySQL stuff.
//
int mysqlTestConnection(const QString &hostname, int port,
const QString &usernam, const QString &password);
private:
//
// GENERAL stuff.
@ -100,6 +107,9 @@ class DatabaseFactory : public QObject {
QSqlDatabase sqliteConnection(const QString &connection_name,
DesiredType desired_type);
// Runs "VACUUM" on the database.
bool sqliteVacuumDatabase();
// Performs saving of items from in-memory database
// to file-based database.
void sqliteSaveMemoryDatabase();

View File

@ -2,6 +2,7 @@
#include "core/defs.h"
#include "core/settings.h"
#include "core/databasefactory.h"
#include "core/localization.h"
#include "core/systemfactory.h"
#include "core/feeddownloader.h"
@ -95,6 +96,14 @@ FormSettings::FormSettings(QWidget *parent) : QDialog(parent), m_ui(new Ui::Form
this, SLOT(changeDefaultBrowserArguments(int)));
connect(m_ui->m_btnExternalBrowserExecutable, SIGNAL(clicked()),
this, SLOT(selectBrowserExecutable()));
connect(m_ui->m_txtMysqlUsername->lineEdit(), SIGNAL(textChanged(QString)),
this, SLOT(onMysqlUsernameChanged(QString)));
connect(m_ui->m_txtMysqlHostname->lineEdit(), SIGNAL(textChanged(QString)),
this, SLOT(onMysqlHostnameChanged(QString)));
connect(m_ui->m_txtMysqlPassword->lineEdit(), SIGNAL(textChanged(QString)),
this, SLOT(onMysqlPasswordChanged(QString)));
connect(m_ui->m_btnMysqlTestSetup, SIGNAL(clicked()),
this, SLOT(mysqlTestConnection()));
// Load all settings.
loadGeneral();
@ -425,14 +434,21 @@ void FormSettings::saveShortcuts() {
void FormSettings::loadDataStorage() {
// Load SQLite.
m_ui->m_cmbDatabaseDriver->addItem("SQLite", APP_DB_DRIVER_SQLITE);
m_ui->m_cmbDatabaseDriver->addItem(
tr("SQLite (embedded database)"), APP_DB_DRIVER_SQLITE);
// Load in-memory database status.
m_ui->m_checkSqliteUseInMemoryDatabase->setChecked(Settings::instance()->value(APP_CFG_DB, "use_in_memory_db", false).toBool());
if (QSqlDatabase::isDriverAvailable(APP_DB_DRIVER_MYSQL)) {
// Load MySQL.
m_ui->m_cmbDatabaseDriver->addItem("MySQL", APP_DB_DRIVER_MYSQL);
m_ui->m_cmbDatabaseDriver->addItem(
tr("MySQL/MariaDB (dedicated database)"), APP_DB_DRIVER_MYSQL);
// Setup placeholders.
m_ui->m_txtMysqlHostname->lineEdit()->setPlaceholderText(tr("Hostname of your MySQL server"));
m_ui->m_txtMysqlUsername->lineEdit()->setPlaceholderText(tr("Username to login with"));
m_ui->m_txtMysqlPassword->lineEdit()->setPlaceholderText(tr("Password for your username"));
m_ui->m_txtMysqlHostname->lineEdit()->setText(Settings::instance()->value(APP_CFG_DB, "mysql_hostname").toString());
m_ui->m_txtMysqlUsername->lineEdit()->setText(Settings::instance()->value(APP_CFG_DB, "mysql_username").toString());
@ -440,7 +456,6 @@ void FormSettings::loadDataStorage() {
m_ui->m_spinMysqlPort->setValue(Settings::instance()->value(APP_CFG_DB, "mysql_port", APP_DB_MYSQL_PORT).toInt());
}
// TODO: nacist podle nastaveni
m_ui->m_cmbDatabaseDriver->setCurrentIndex(m_ui->m_cmbDatabaseDriver->findData(Settings::instance()->value(APP_CFG_DB,
"database_driver",
APP_DB_DRIVER_SQLITE).toString()));
@ -477,6 +492,48 @@ void FormSettings::saveDataStorage() {
}
}
void FormSettings::mysqlTestConnection() {
int result = 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
}
void FormSettings::onMysqlHostnameChanged(const QString &new_hostname) {
if (new_hostname.isEmpty()) {
m_ui->m_txtMysqlHostname->setStatus(LineEditWithStatus::Warning,
tr("Hostname is empty."));
}
else {
m_ui->m_txtMysqlHostname->setStatus(LineEditWithStatus::Ok,
tr("Hostname looks ok."));
}
}
void FormSettings::onMysqlUsernameChanged(const QString &new_username) {
if (new_username.isEmpty()) {
m_ui->m_txtMysqlUsername->setStatus(LineEditWithStatus::Warning,
tr("Username is empty."));
}
else {
m_ui->m_txtMysqlUsername->setStatus(LineEditWithStatus::Ok,
tr("Username looks ok."));
}
}
void FormSettings::onMysqlPasswordChanged(const QString &new_password) {
if (new_password.isEmpty()) {
m_ui->m_txtMysqlPassword->setStatus(LineEditWithStatus::Warning,
tr("Password is empty."));
}
else {
m_ui->m_txtMysqlPassword->setStatus(LineEditWithStatus::Ok,
tr("Password looks ok."));
}
}
void FormSettings::loadGeneral() {
// Load auto-start status.
SystemFactory::AutoStartStatus autostart_status = SystemFactory::instance()->getAutoStartStatus();

View File

@ -54,6 +54,10 @@ class FormSettings : public QDialog {
void loadDataStorage();
void saveDataStorage();
void mysqlTestConnection();
void onMysqlHostnameChanged(const QString &new_hostname);
void onMysqlUsernameChanged(const QString &new_username);
void onMysqlPasswordChanged(const QString &new_password);
void loadLanguage();
void saveLanguage();

View File

@ -14,76 +14,10 @@
<string>Settings</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QListWidget" name="m_listSettings">
<property name="minimumSize">
<size>
<width>220</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>220</width>
<height>16777215</height>
</size>
</property>
<property name="showDropIndicator" stdset="0">
<bool>false</bool>
</property>
<property name="alternatingRowColors">
<bool>false</bool>
</property>
<property name="movement">
<enum>QListView::Static</enum>
</property>
<property name="selectionRectVisible">
<bool>false</bool>
</property>
<property name="currentRow">
<number>0</number>
</property>
<item>
<property name="text">
<string>General</string>
</property>
</item>
<item>
<property name="text">
<string>Data storage</string>
</property>
</item>
<item>
<property name="text">
<string>Keyboard shortcuts</string>
</property>
</item>
<item>
<property name="text">
<string>User interface</string>
</property>
</item>
<item>
<property name="text">
<string>Language</string>
</property>
</item>
<item>
<property name="text">
<string>Web browser &amp; proxy</string>
</property>
</item>
<item>
<property name="text">
<string>Feeds &amp; messages</string>
</property>
</item>
</widget>
</item>
<item row="0" column="1">
<widget class="QStackedWidget" name="m_stackedSettings">
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<widget class="QWidget" name="m_pageGeneral">
<layout class="QFormLayout" name="formLayout_5">
@ -125,7 +59,7 @@
<item row="0" column="0" colspan="2">
<widget class="QLabel" name="m_lblDataStorageWarning">
<property name="text">
<string>WARNING: Note that switching to another data storage type will NOT preserve your data from currently active data storage.</string>
<string>WARNING: Note that switching to another data storage type will NOT copy existing your data from currently active data storage to newly selected one.</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
@ -148,7 +82,7 @@
<item row="2" column="0" colspan="2">
<widget class="QStackedWidget" name="m_stackedDatabaseDriver">
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<widget class="QWidget" name="m_pageSqlite">
<layout class="QFormLayout" name="formLayout_15">
@ -276,6 +210,33 @@ Authors of this application are NOT responsible for lost data.</string>
<item row="2" column="1">
<widget class="LineEditWithStatus" name="m_txtMysqlPassword" native="true"/>
</item>
<item row="3" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_11">
<item>
<widget class="QPushButton" name="m_btnMysqlTestSetup">
<property name="text">
<string>Test setup</string>
</property>
<property name="flat">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</widget>
@ -309,8 +270,8 @@ Authors of this application are NOT responsible for lost data.</string>
<rect>
<x>0</x>
<y>0</y>
<width>100</width>
<height>30</height>
<width>564</width>
<height>363</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout_4">
@ -1056,6 +1017,72 @@ Authors of this application are NOT responsible for lost data.</string>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QListWidget" name="m_listSettings">
<property name="minimumSize">
<size>
<width>220</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>220</width>
<height>16777215</height>
</size>
</property>
<property name="showDropIndicator" stdset="0">
<bool>false</bool>
</property>
<property name="alternatingRowColors">
<bool>false</bool>
</property>
<property name="movement">
<enum>QListView::Static</enum>
</property>
<property name="selectionRectVisible">
<bool>false</bool>
</property>
<property name="currentRow">
<number>0</number>
</property>
<item>
<property name="text">
<string>General</string>
</property>
</item>
<item>
<property name="text">
<string>Data storage</string>
</property>
</item>
<item>
<property name="text">
<string>Keyboard shortcuts</string>
</property>
</item>
<item>
<property name="text">
<string>User interface</string>
</property>
</item>
<item>
<property name="text">
<string>Language</string>
</property>
</item>
<item>
<property name="text">
<string>Web browser &amp; proxy</string>
</property>
</item>
<item>
<property name="text">
<string>Feeds &amp; messages</string>
</property>
</item>
</widget>
</item>
</layout>
</widget>
<customwidgets>