| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "gui/settings/settingsdatabase.h" |
| 4 | |
| 5 | #include "database/databasefactory.h" |
| 6 | #include "database/mariadbdriver.h" |
| 7 | #include "definitions/definitions.h" |
| 8 | #include "miscellaneous/application.h" |
| 9 | #include "miscellaneous/iconfactory.h" |
| 10 | #include "miscellaneous/settings.h" |
| 11 | |
| 12 | SettingsDatabase::SettingsDatabase(Settings* settings, QWidget* parent) |
| 13 | : SettingsPanel(settings, parent), m_ui(new Ui::SettingsDatabase) { |
| 14 | m_ui->setupUi(this); |
| 15 | |
| 16 | m_ui->m_lblMysqlInfo->setHelpText(tr("Note that speed of used MySQL server and latency of used connection " |
| 17 | "medium HEAVILY influences the final performance of this application. " |
| 18 | "Using slow database connections leads to bad performance when browsing " |
| 19 | "feeds or messages."), |
| 20 | false); |
| 21 | |
| 22 | m_ui->m_lblSqliteInMemoryWarnings |
| 23 | ->setHelpText(tr("Usage of in-memory working database has several advantages " |
| 24 | "and pitfalls. Make sure that you are familiar with these " |
| 25 | "before you turn this feature on.\n" |
| 26 | "\n" |
| 27 | "Advantages:\n" |
| 28 | " • higher speed for feed/message manipulations " |
| 29 | "(especially with thousands of messages displayed),\n" |
| 30 | " • whole database stored in RAM, thus your hard drive can " |
| 31 | "rest more.\n" |
| 32 | "\n" |
| 33 | "Disadvantages:\n" |
| 34 | " • if application crashes, your changes from last session are lost,\n" |
| 35 | " • application startup and shutdown can take little longer " |
| 36 | "(max. 2 seconds).\n" |
| 37 | "\n" |
| 38 | "Authors of this application are NOT responsible for lost data."), |
| 39 | true); |
| 40 | |
| 41 | m_ui->m_txtMysqlPassword->lineEdit()->setPasswordMode(true); |
| 42 | |
| 43 | connect(m_ui->m_cmbDatabaseDriver, |
| 44 | static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), |
| 45 | this, |
| 46 | &SettingsDatabase::dirtifySettings); |
| 47 | connect(m_ui->m_checkSqliteUseInMemoryDatabase, &QCheckBox::toggled, this, &SettingsDatabase::dirtifySettings); |
| 48 | connect(m_ui->m_txtMysqlDatabase->lineEdit(), &QLineEdit::textChanged, this, &SettingsDatabase::dirtifySettings); |
| 49 | connect(m_ui->m_txtMysqlHostname->lineEdit(), &QLineEdit::textChanged, this, &SettingsDatabase::dirtifySettings); |
| 50 | connect(m_ui->m_txtMysqlPassword->lineEdit(), &QLineEdit::textChanged, this, &SettingsDatabase::dirtifySettings); |
| 51 | connect(m_ui->m_txtMysqlUsername->lineEdit(), &QLineEdit::textChanged, this, &SettingsDatabase::dirtifySettings); |
| 52 | connect(m_ui->m_spinMysqlPort, |
| 53 | QOverload<int>::of(&QSpinBox::valueChanged), |
| 54 | this, |
| 55 | &SettingsDatabase::dirtifySettings); |
| 56 | connect(m_ui->m_cmbDatabaseDriver, |
| 57 | static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), |
| 58 | this, |
| 59 | &SettingsDatabase::selectSqlBackend); |
| 60 | connect(m_ui->m_txtMysqlUsername->lineEdit(), |
| 61 | &BaseLineEdit::textChanged, |
| 62 | this, |
| 63 | &SettingsDatabase::onMysqlUsernameChanged); |
| 64 | connect(m_ui->m_txtMysqlHostname->lineEdit(), |
| 65 | &BaseLineEdit::textChanged, |
| 66 | this, |
| 67 | &SettingsDatabase::onMysqlHostnameChanged); |
| 68 | connect(m_ui->m_txtMysqlPassword->lineEdit(), |
| 69 | &BaseLineEdit::textChanged, |
| 70 | this, |
| 71 | &SettingsDatabase::onMysqlPasswordChanged); |
| 72 | connect(m_ui->m_txtMysqlDatabase->lineEdit(), |
| 73 | &BaseLineEdit::textChanged, |
| 74 | this, |
| 75 | &SettingsDatabase::onMysqlDatabaseChanged); |
| 76 | connect(m_ui->m_btnMysqlTestSetup, &QPushButton::clicked, this, &SettingsDatabase::mysqlTestConnection); |
| 77 | connect(m_ui->m_cmbDatabaseDriver, |
| 78 | static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), |
| 79 | this, |
| 80 | &SettingsDatabase::requireRestart); |
| 81 | connect(m_ui->m_checkSqliteUseInMemoryDatabase, &QCheckBox::toggled, this, &SettingsDatabase::requireRestart); |
| 82 | connect(m_ui->m_spinMysqlPort, &QSpinBox::editingFinished, this, &SettingsDatabase::requireRestart); |
| 83 | connect(m_ui->m_txtMysqlHostname->lineEdit(), &BaseLineEdit::textEdited, this, &SettingsDatabase::requireRestart); |
| 84 | connect(m_ui->m_txtMysqlPassword->lineEdit(), &BaseLineEdit::textEdited, this, &SettingsDatabase::requireRestart); |
| 85 | connect(m_ui->m_txtMysqlUsername->lineEdit(), &BaseLineEdit::textEdited, this, &SettingsDatabase::requireRestart); |
| 86 | } |
| 87 | |
| 88 | SettingsDatabase::~SettingsDatabase() { |
| 89 | delete m_ui; |
| 90 | } |
| 91 | |
| 92 | QIcon SettingsDatabase::icon() const { |
| 93 | return qApp->icons()->fromTheme(QSL("folder-database"), QSL("libreoffice-database")); |
| 94 | } |
| 95 | |
| 96 | void SettingsDatabase::mysqlTestConnection() { |
| 97 | MariaDbDriver* driv = static_cast<MariaDbDriver*>(qApp->database()->driver()); |
| 98 | const MariaDbDriver::MariaDbError error_code = driv->testConnection(m_ui->m_txtMysqlHostname->lineEdit()->text(), |
| 99 | m_ui->m_spinMysqlPort->value(), |
| 100 | m_ui->m_txtMysqlDatabase->lineEdit()->text(), |
| 101 | m_ui->m_txtMysqlUsername->lineEdit()->text(), |
| 102 | m_ui->m_txtMysqlPassword->lineEdit()->text()); |
| 103 | const QString interpretation = driv->interpretErrorCode(error_code); |
| 104 | |
| 105 | switch (error_code) { |
| 106 | case MariaDbDriver::MariaDbError::Ok: |
| 107 | case MariaDbDriver::MariaDbError::UnknownDatabase: |
| 108 | m_ui->m_lblMysqlTestResult->setStatus(WidgetWithStatus::StatusType::Ok, interpretation, interpretation); |
| 109 | break; |
| 110 | |
| 111 | default: |
| 112 | m_ui->m_lblMysqlTestResult->setStatus(WidgetWithStatus::StatusType::Error, interpretation, interpretation); |
| 113 | break; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | void SettingsDatabase::onMysqlHostnameChanged(const QString& new_hostname) { |
| 118 | if (new_hostname.isEmpty()) { |
| 119 | m_ui->m_txtMysqlHostname->setStatus(LineEditWithStatus::StatusType::Warning, tr("Hostname is empty.")); |
| 120 | } |
| 121 | else { |
| 122 | m_ui->m_txtMysqlHostname->setStatus(LineEditWithStatus::StatusType::Ok, tr("Hostname looks ok.")); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | void SettingsDatabase::onMysqlUsernameChanged(const QString& new_username) { |
| 127 | if (new_username.isEmpty()) { |
| 128 | m_ui->m_txtMysqlUsername->setStatus(LineEditWithStatus::StatusType::Warning, tr("Username is empty.")); |
| 129 | } |
| 130 | else { |
| 131 | m_ui->m_txtMysqlUsername->setStatus(LineEditWithStatus::StatusType::Ok, tr("Username looks ok.")); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void SettingsDatabase::onMysqlPasswordChanged(const QString& new_password) { |
| 136 | if (new_password.isEmpty()) { |
| 137 | m_ui->m_txtMysqlPassword->setStatus(LineEditWithStatus::StatusType::Warning, tr("Password is empty.")); |
| 138 | } |
| 139 | else { |
| 140 | m_ui->m_txtMysqlPassword->setStatus(LineEditWithStatus::StatusType::Ok, tr("Password looks ok.")); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void SettingsDatabase::onMysqlDatabaseChanged(const QString& new_database) { |
| 145 | if (new_database.isEmpty()) { |
| 146 | m_ui->m_txtMysqlDatabase->setStatus(LineEditWithStatus::StatusType::Warning, tr("Working database is empty.")); |
| 147 | } |
| 148 | else { |
| 149 | m_ui->m_txtMysqlDatabase->setStatus(LineEditWithStatus::StatusType::Ok, tr("Working database is ok.")); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void SettingsDatabase::selectSqlBackend(int index) { |
| 154 | const QString selected_db_driver = m_ui->m_cmbDatabaseDriver->itemData(index).toString(); |
| 155 | |
| 156 | if (selected_db_driver == QSL(APP_DB_SQLITE_DRIVER)) { |
| 157 | m_ui->m_stackedDatabaseDriver->setCurrentIndex(0); |
| 158 | } |
| 159 | else if (selected_db_driver == QSL(APP_DB_MYSQL_DRIVER)) { |
| 160 | m_ui->m_stackedDatabaseDriver->setCurrentIndex(1); |
| 161 | } |
| 162 | else { |
| 163 | qWarningNN << LOGSEC_GUI << "GUI for given database driver '" << selected_db_driver << "' is not available."; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void SettingsDatabase::loadSettings() { |
| 168 | onBeginLoadSettings(); |
| 169 | m_ui->m_lblMysqlTestResult->setStatus(WidgetWithStatus::StatusType::Information, |
| 170 | tr("No connection test triggered so far."), |
| 171 | tr("You did not executed any connection test yet.")); |
| 172 | |
| 173 | // Load SQLite. |
| 174 | auto* lite_driver = qApp->database()->driverForType(DatabaseDriver::DriverType::SQLite); |
| 175 | |
| 176 | m_ui->m_cmbDatabaseDriver->addItem(lite_driver->humanDriverType(), lite_driver->qtDriverCode()); |
| 177 | |
| 178 | // Load in-memory database status. |
| 179 | m_ui->m_checkSqliteUseInMemoryDatabase |
| 180 | ->setChecked(settings()->value(GROUP(Database), SETTING(Database::UseInMemory)).toBool()); |
| 181 | |
| 182 | auto* mysq_driver = qApp->database()->driverForType(DatabaseDriver::DriverType::MySQL); |
| 183 | |
| 184 | if (mysq_driver != nullptr) { |
| 185 | onMysqlHostnameChanged(QString()); |
| 186 | onMysqlUsernameChanged(QString()); |
| 187 | onMysqlPasswordChanged(QString()); |
| 188 | onMysqlDatabaseChanged(QString()); |
| 189 | |
| 190 | // Load MySQL. |
| 191 | m_ui->m_cmbDatabaseDriver->addItem(mysq_driver->humanDriverType(), mysq_driver->qtDriverCode()); |
| 192 | |
| 193 | // Setup placeholders. |
| 194 | m_ui->m_txtMysqlHostname->lineEdit()->setPlaceholderText(tr("Hostname of your MySQL server")); |
| 195 | m_ui->m_txtMysqlUsername->lineEdit()->setPlaceholderText(tr("Username to login with")); |
| 196 | m_ui->m_txtMysqlPassword->lineEdit()->setPlaceholderText(tr("Password for your username")); |
| 197 | m_ui->m_txtMysqlDatabase->lineEdit()->setPlaceholderText(tr("Working database which you have full access to.")); |
| 198 | m_ui->m_txtMysqlHostname->lineEdit() |
| 199 | ->setText(settings()->value(GROUP(Database), SETTING(Database::MySQLHostname)).toString()); |
| 200 | m_ui->m_txtMysqlUsername->lineEdit() |
| 201 | ->setText(settings()->value(GROUP(Database), SETTING(Database::MySQLUsername)).toString()); |
| 202 | m_ui->m_txtMysqlPassword->lineEdit() |
| 203 | ->setText(settings()->password(GROUP(Database), SETTING(Database::MySQLPassword)).toString()); |
| 204 | m_ui->m_txtMysqlDatabase->lineEdit() |
| 205 | ->setText(settings()->value(GROUP(Database), SETTING(Database::MySQLDatabase)).toString()); |
| 206 | m_ui->m_spinMysqlPort->setValue(settings()->value(GROUP(Database), SETTING(Database::MySQLPort)).toInt()); |
| 207 | } |
| 208 | |
| 209 | int index_current_backend = |
| 210 | m_ui->m_cmbDatabaseDriver->findData(settings()->value(GROUP(Database), SETTING(Database::ActiveDriver)).toString()); |
| 211 | |
| 212 | if (index_current_backend >= 0) { |
| 213 | m_ui->m_cmbDatabaseDriver->setCurrentIndex(index_current_backend); |
| 214 | } |
| 215 | |
| 216 | onEndLoadSettings(); |
| 217 | } |
| 218 | |
| 219 | void SettingsDatabase::saveSettings() { |
| 220 | onBeginSaveSettings(); |
| 221 | |
| 222 | // Setup in-memory database status. |
| 223 | const bool original_inmemory = settings()->value(GROUP(Database), SETTING(Database::UseInMemory)).toBool(); |
| 224 | const bool new_inmemory = m_ui->m_checkSqliteUseInMemoryDatabase->isChecked(); |
| 225 | |
| 226 | // Save data storage settings. |
| 227 | QString original_db_driver = settings()->value(GROUP(Database), SETTING(Database::ActiveDriver)).toString(); |
| 228 | QString selected_db_driver = |
| 229 | m_ui->m_cmbDatabaseDriver->itemData(m_ui->m_cmbDatabaseDriver->currentIndex()).toString(); |
| 230 | |
| 231 | // Save SQLite. |
| 232 | settings()->setValue(GROUP(Database), Database::UseInMemory, new_inmemory); |
| 233 | |
| 234 | if (QSqlDatabase::isDriverAvailable(QSL(APP_DB_MYSQL_DRIVER))) { |
| 235 | // Save MySQL. |
| 236 | settings()->setValue(GROUP(Database), Database::MySQLHostname, m_ui->m_txtMysqlHostname->lineEdit()->text()); |
| 237 | settings()->setValue(GROUP(Database), Database::MySQLUsername, m_ui->m_txtMysqlUsername->lineEdit()->text()); |
| 238 | settings()->setPassword(GROUP(Database), Database::MySQLPassword, m_ui->m_txtMysqlPassword->lineEdit()->text()); |
| 239 | settings()->setValue(GROUP(Database), Database::MySQLDatabase, m_ui->m_txtMysqlDatabase->lineEdit()->text()); |
| 240 | settings()->setValue(GROUP(Database), Database::MySQLPort, m_ui->m_spinMysqlPort->value()); |
| 241 | } |
| 242 | |
| 243 | settings()->setValue(GROUP(Database), Database::ActiveDriver, selected_db_driver); |
| 244 | |
| 245 | if (original_db_driver != selected_db_driver || original_inmemory != new_inmemory) { |
| 246 | requireRestart(); |
| 247 | } |
| 248 | |
| 249 | onEndSaveSettings(); |
| 250 | } |