From 81d01f3cdac226bb2ed12c45f964ad0e62c8c4dc Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Fri, 5 Jun 2015 09:24:50 +0200 Subject: [PATCH] Work on cleaning, is able to vacuum in worker thread. Added GUI & logic. --- src/definitions/definitions.h.in | 2 +- src/gui/feedmessageviewer.cpp | 46 ++++++++- src/gui/feedmessageviewer.h | 4 + src/gui/formabout.ui | 50 +++++++--- src/gui/formdatabasecleanup.cpp | 101 +++++++++++++++++-- src/gui/formdatabasecleanup.h | 20 ++++ src/gui/formdatabasecleanup.ui | 134 ++++++++++++++++++++------ src/gui/formsettings.cpp | 4 +- src/miscellaneous/application.cpp | 2 +- src/miscellaneous/databasecleaner.cpp | 22 +++++ src/miscellaneous/databasecleaner.h | 11 +++ src/miscellaneous/databasefactory.cpp | 34 +++++++ src/miscellaneous/databasefactory.h | 5 + 13 files changed, 368 insertions(+), 67 deletions(-) diff --git a/src/definitions/definitions.h.in b/src/definitions/definitions.h.in index 518d77236..c0895794c 100755 --- a/src/definitions/definitions.h.in +++ b/src/definitions/definitions.h.in @@ -56,7 +56,7 @@ #define ID_RECYCLE_BIN -2 #define TRAY_ICON_BUBBLE_TIMEOUT 20000 #define KEY_MESSAGES_VIEW "messages_view_column_" -#define CLOSE_LOCK_TIMEOUT 3000 +#define CLOSE_LOCK_TIMEOUT 1500 #define DOWNLOAD_TIMEOUT 5000 #define MESSAGES_VIEW_DEFAULT_COL 170 #define FEEDS_VIEW_COLUMN_COUNT 2 diff --git a/src/gui/feedmessageviewer.cpp b/src/gui/feedmessageviewer.cpp index 403a3aaff..a5ba491ef 100755 --- a/src/gui/feedmessageviewer.cpp +++ b/src/gui/feedmessageviewer.cpp @@ -22,6 +22,7 @@ #include "miscellaneous/systemfactory.h" #include "miscellaneous/iconfactory.h" #include "miscellaneous/mutex.h" +#include "miscellaneous/databasecleaner.h" #include "core/messagesproxymodel.h" #include "core/feeddownloader.h" #include "core/feedsmodelfeed.h" @@ -65,7 +66,8 @@ FeedMessageViewer::FeedMessageViewer(QWidget *parent) m_messagesBrowser(new WebBrowser(this)), m_dbCleanerThread(NULL), m_feedDownloaderThread(NULL), - m_feedDownloader(NULL) { + m_feedDownloader(NULL), + m_dbCleaner(NULL) { initialize(); initializeViews(); createConnections(); @@ -78,6 +80,23 @@ FeedMessageViewer::~FeedMessageViewer() { qDebug("Destroying FeedMessageViewer instance."); } +DatabaseCleaner *FeedMessageViewer::databaseCleaner() { + if (m_dbCleaner == NULL) { + m_dbCleaner = new DatabaseCleaner(); + m_dbCleanerThread = new QThread(); + + // Downloader setup. + qRegisterMetaType("CleanerOrders"); + m_dbCleaner->moveToThread(m_dbCleanerThread); + connect(m_dbCleanerThread, SIGNAL(finished()), m_dbCleanerThread, SLOT(deleteLater())); + + // Connections are made, start the feed downloader thread. + m_dbCleanerThread->start(); + } + + return m_dbCleaner; +} + void FeedMessageViewer::saveSize() { Settings *settings = qApp->settings(); @@ -125,23 +144,38 @@ void FeedMessageViewer::quit() { // Quit the feeds view (stops auto-update timer etc.). m_feedsView->quit(); + // Close worker threads. if (m_feedDownloaderThread != NULL && m_feedDownloaderThread->isRunning()) { qDebug("Quitting feed downloader thread."); m_feedDownloaderThread->quit(); - m_feedDownloaderThread->wait(); + + if (!m_feedDownloaderThread->wait(CLOSE_LOCK_TIMEOUT)) { + qCritical("Feed downloader thread is running despite it was told to quit. Terminating it."); + m_feedDownloaderThread->terminate(); + } } if (m_dbCleanerThread != NULL && m_dbCleanerThread->isRunning()) { qDebug("Quitting database cleaner thread."); m_dbCleanerThread->quit(); - m_dbCleanerThread->wait(); + + if (!m_dbCleanerThread->wait(CLOSE_LOCK_TIMEOUT)) { + qCritical("Database cleaner thread is running despite it was told to quit. Terminating it."); + m_dbCleanerThread->terminate(); + } } + // Close workers. if (m_feedDownloader != NULL) { - qDebug("Feed downloader thread aborted. Deleting it from memory."); + qDebug("Feed downloader exists. Deleting it from memory."); m_feedDownloader->deleteLater(); } + if (m_dbCleaner != NULL) { + qDebug("Database cleaner exists. Deleting it from memory."); + m_dbCleaner->deleteLater(); + } + if (qApp->settings()->value(GROUP(Messages), SETTING(Messages::ClearReadOnExit)).toBool()) { m_feedsView->clearAllReadMessages(); } @@ -407,10 +441,12 @@ void FeedMessageViewer::initializeViews() { void FeedMessageViewer::showDbCleanupAssistant() { if (qApp->feedUpdateLock()->tryLock()) { QPointer form_pointer = new FormDatabaseCleanup(this); + form_pointer.data()->setCleaner(databaseCleaner()); form_pointer.data()->exec(); delete form_pointer.data(); - qApp->feedUpdateLock()->unlock(); + + m_messagesView->reloadSelections(true); } else { qApp->showGuiMessage(tr("Cannot cleanup database"), diff --git a/src/gui/feedmessageviewer.h b/src/gui/feedmessageviewer.h index 702810126..af8eb8af9 100644 --- a/src/gui/feedmessageviewer.h +++ b/src/gui/feedmessageviewer.h @@ -29,6 +29,7 @@ class MessagesToolBar; class FeedsToolBar; class FeedsView; class FeedDownloader; +class DatabaseCleaner; class FeedsModelFeed; class QToolBar; class QSplitter; @@ -64,6 +65,8 @@ class FeedMessageViewer : public TabContent { return m_toolBarFeeds; } + DatabaseCleaner *databaseCleaner(); + // Loads/saves sizes and states of ALL // underlying widgets, this contains primarily // splitters, toolbar and views. @@ -145,6 +148,7 @@ class FeedMessageViewer : public TabContent { QThread *m_feedDownloaderThread; QThread *m_dbCleanerThread; FeedDownloader *m_feedDownloader; + DatabaseCleaner *m_dbCleaner; }; #endif // FEEDMESSAGEVIEWER_H diff --git a/src/gui/formabout.ui b/src/gui/formabout.ui index 6766514a0..696b2d41e 100644 --- a/src/gui/formabout.ui +++ b/src/gui/formabout.ui @@ -104,7 +104,7 @@ - 0 + 3 @@ -132,8 +132,8 @@ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> -<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html> +</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;"> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif'; font-size:9pt;"><br /></p></body></html> false @@ -166,12 +166,12 @@ p, li { white-space: pre-wrap; } 0 0 - 693 - 196 + 685 + 184 - false + true GNU GPL License (applies to RSS Guard source code) @@ -180,7 +180,16 @@ p, li { white-space: pre-wrap; } GNU GPL License - + + 0 + + + 0 + + + 0 + + 0 @@ -215,8 +224,8 @@ p, li { white-space: pre-wrap; } <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'DejaVu Sans Mono'; font-size:9pt; font-weight:400; font-style:normal;"> -<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif';"><br /></p></body></html> +</style></head><body style=" font-family:'DejaVu Sans Mono'; font-size:8.25pt; font-weight:400; font-style:normal;"> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif'; font-size:9pt;"><br /></p></body></html> Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse @@ -234,14 +243,23 @@ p, li { white-space: pre-wrap; } 0 0 83 - 72 + 69 BSD License (applies to QtSingleApplication source code) - + + 0 + + + 0 + + + 0 + + 0 @@ -276,8 +294,8 @@ p, li { white-space: pre-wrap; } <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'DejaVu Sans Mono'; font-size:9pt; font-weight:400; font-style:normal;"> -<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif';"><br /></p></body></html> +</style></head><body style=" font-family:'DejaVu Sans Mono'; font-size:8.25pt; font-weight:400; font-style:normal;"> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif'; font-size:9pt;"><br /></p></body></html> Qt::LinksAccessibleByMouse|Qt::TextSelectableByMouse @@ -317,8 +335,8 @@ p, li { white-space: pre-wrap; } <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> <html><head><meta name="qrichtext" content="1" /><style type="text/css"> p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'Sans Serif'; font-size:9pt; font-weight:400; font-style:normal;"> -<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><br /></p></body></html> +</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;"> +<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:'Sans Serif'; font-size:9pt;"><br /></p></body></html> false @@ -342,7 +360,7 @@ p, li { white-space: pre-wrap; } - Paths + Resources diff --git a/src/gui/formdatabasecleanup.cpp b/src/gui/formdatabasecleanup.cpp index 3cf557a30..114ba37bb 100644 --- a/src/gui/formdatabasecleanup.cpp +++ b/src/gui/formdatabasecleanup.cpp @@ -2,9 +2,12 @@ #include "miscellaneous/application.h" #include "miscellaneous/iconfactory.h" +#include "miscellaneous/databasefactory.h" + +#include -FormDatabaseCleanup::FormDatabaseCleanup(QWidget *parent) : QDialog(parent), m_ui(new Ui::FormDatabaseCleanup) { +FormDatabaseCleanup::FormDatabaseCleanup(QWidget *parent) : QDialog(parent), m_ui(new Ui::FormDatabaseCleanup), m_cleaner(NULL) { m_ui->setupUi(this); // Set flags and attributes. @@ -12,22 +15,100 @@ FormDatabaseCleanup::FormDatabaseCleanup(QWidget *parent) : QDialog(parent), m_u setWindowIcon(qApp->icons()->fromTheme("cleanup-database")); connect(m_ui->m_spinDays, SIGNAL(valueChanged(int)), this, SLOT(updateDaysSuffix(int))); - m_ui->m_spinDays->setValue(DEFAULT_DAYS_TO_DELETE_MSG); - // TODO: Vytvořil jsem základ okna pro mazání. - // v tomto okně se nastaví parametry mazání - jak staré zprávy, zda přečtené, zda vakuovat db - // do třídy DatabaseCleaner se dodělají metody, které budou samotné mazání provádět - // ve FeedMessageViewer se udělá instance cleaneru stejně jako FeedDownloader (vlákno už tam mam - // deklarovano (m_dbCleanerThread). Po "OK" tady v tomdle dialogu se pošle signal - // do instance cleaneru ve feedmessagevieweru. - // před otevřením tohodle dialogu se zamkne hlavní lock, po oddělání dialogu se odemkne. - // databasecleaner by mohl reportovat i progress, poběží ve svém vlákně. + m_ui->m_lblResult->setStatus(WidgetWithStatus::Information, tr("I am ready."), tr("I am ready.")); + loadDatabaseInfo(); } FormDatabaseCleanup::~FormDatabaseCleanup() { delete m_ui; } +void FormDatabaseCleanup::setCleaner(DatabaseCleaner *cleaner) { + if (m_cleaner != NULL) { + disconnect(this, 0, m_cleaner, 0); + disconnect(m_cleaner, 0, this, 0); + } + + m_cleaner = cleaner; + + connect(m_ui->m_btnBox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(startPurging())); + connect(this, SIGNAL(purgeRequested(CleanerOrders)), m_cleaner, SLOT(purgeDatabaseData(CleanerOrders))); + connect(m_cleaner, SIGNAL(purgeStarted()), this, SLOT(onPurgeStarted())); + connect(m_cleaner, SIGNAL(purgeProgress(int)), this, SLOT(onPurgeProgress(int))); + connect(m_cleaner, SIGNAL(purgeFinished(bool)), this, SLOT(onPurgeFinished(bool))); +} + +void FormDatabaseCleanup::closeEvent(QCloseEvent *event) { + if (m_ui->m_progressBar->isEnabled()) { + event->ignore(); + } + else { + QDialog::closeEvent(event); + } +} + +void FormDatabaseCleanup::keyPressEvent(QKeyEvent *event) { + if (m_ui->m_progressBar->isEnabled()) { + event->ignore(); + } + else { + QDialog::keyPressEvent(event); + } +} + void FormDatabaseCleanup::updateDaysSuffix(int number) { m_ui->m_spinDays->setSuffix(tr(" day(s)", 0, number)); } + +void FormDatabaseCleanup::startPurging() { + CleanerOrders orders; + + orders.m_removeOldMessages = m_ui->m_checkRemoveOldMessages->isChecked(); + orders.m_barrierForRemovingOldMessagesInDays = m_ui->m_spinDays->value(); + orders.m_removeReadMessages = m_ui->m_checkRemoveReadMessages->isChecked(); + orders.m_shrinkDatabase = m_ui->m_checkShrink->isEnabled() && m_ui->m_checkShrink->isChecked(); + + emit purgeRequested(orders); +} + +void FormDatabaseCleanup::onPurgeStarted() { + m_ui->m_progressBar->setValue(0); + m_ui->m_progressBar->setEnabled(true); + m_ui->m_btnBox->setEnabled(false); + m_ui->m_lblResult->setStatus(WidgetWithStatus::Information, tr("Database cleanup is running."), tr("Database cleanup is running.")); +} + +void FormDatabaseCleanup::onPurgeProgress(int progress) { + m_ui->m_progressBar->setValue(progress); +} + +void FormDatabaseCleanup::onPurgeFinished(bool finished) { + m_ui->m_progressBar->setEnabled(false); + m_ui->m_progressBar->setValue(0); + m_ui->m_btnBox->setEnabled(true); + + if (finished) { + m_ui->m_lblResult->setStatus(WidgetWithStatus::Ok, tr("Database cleanup is completed."), tr("Database cleanup is completed.")); + } + else { + m_ui->m_lblResult->setStatus(WidgetWithStatus::Error, tr("Database cleanup failed."), tr("Database cleanup failed.")); + } + + loadDatabaseInfo(); +} + +void FormDatabaseCleanup::loadDatabaseInfo() { + qint64 db_size = qApp->database()->getDatabaseSize(); + + if (db_size > 0) { + m_ui->m_txtFileSize->setText(QString::number(db_size / 1000000.0) + " MB"); + } + else { + m_ui->m_txtFileSize->setText("-"); + } + + m_ui->m_txtDatabaseType->setText(qApp->database()->humanDriverName(qApp->database()->activeDatabaseDriver())); + m_ui->m_checkShrink->setEnabled(qApp->database()->activeDatabaseDriver() == DatabaseFactory::SQLITE); + m_ui->m_checkShrink->setChecked(m_ui->m_checkShrink->isEnabled()); +} diff --git a/src/gui/formdatabasecleanup.h b/src/gui/formdatabasecleanup.h index 6b4adbe47..2a3a05387 100644 --- a/src/gui/formdatabasecleanup.h +++ b/src/gui/formdatabasecleanup.h @@ -5,6 +5,8 @@ #include "ui_formdatabasecleanup.h" +#include "miscellaneous/databasecleaner.h" + namespace Ui { class FormDatabaseCleanup; @@ -17,11 +19,29 @@ class FormDatabaseCleanup : public QDialog { explicit FormDatabaseCleanup(QWidget *parent = 0); virtual ~FormDatabaseCleanup(); + void setCleaner(DatabaseCleaner *cleaner); + + protected: + void closeEvent(QCloseEvent *event); + void keyPressEvent(QKeyEvent *event); + private slots: void updateDaysSuffix(int number); + void startPurging(); + void onPurgeStarted(); + void onPurgeProgress(int progress); + void onPurgeFinished(bool finished); + + signals: + void purgeRequested(const CleanerOrders &which_data); + + private: + void loadDatabaseInfo(); + private: Ui::FormDatabaseCleanup *m_ui; + DatabaseCleaner *m_cleaner; }; #endif // FORMDATABASECLEANUP_H diff --git a/src/gui/formdatabasecleanup.ui b/src/gui/formdatabasecleanup.ui index 79095fd8e..3b700bd8b 100644 --- a/src/gui/formdatabasecleanup.ui +++ b/src/gui/formdatabasecleanup.ui @@ -7,7 +7,7 @@ 0 0 400 - 300 + 313 @@ -21,7 +21,7 @@ - + Shrink database file. @@ -41,17 +41,23 @@ - + Remove all messages older than + + true + - + Remove all read messages. + + true + @@ -67,72 +73,136 @@ - - - - Qt::Vertical + + + + + + + Database information + + + + + + Database file size - - - 20 - 40 - + + + + + + true - + + + + + + Database type + + + + + + + true + + - - - 24 + + + Progress + + + + + Qt::RightToLeft + + + + + + + false + + + 0 + + + + + + + + Qt::Vertical + + + + 20 + 40 + + + + Qt::Horizontal - QDialogButtonBox::Cancel|QDialogButtonBox::Ok + QDialogButtonBox::Close|QDialogButtonBox::Ok + + + LabelWithStatus + QWidget +
labelwithstatus.h
+ 1 +
+
m_btnBox - accepted() + rejected() FormDatabaseCleanup - accept() + close() - 248 - 254 + 199 + 271 - 157 - 274 + 199 + 145 - m_btnBox - rejected() - FormDatabaseCleanup - reject() + m_checkRemoveOldMessages + toggled(bool) + m_spinDays + setEnabled(bool) - 316 - 260 + 107 + 87 - 286 - 274 + 226 + 87 diff --git a/src/gui/formsettings.cpp b/src/gui/formsettings.cpp index faff27d4f..ba93e44a8 100755 --- a/src/gui/formsettings.cpp +++ b/src/gui/formsettings.cpp @@ -509,7 +509,7 @@ void FormSettings::loadDataStorage() { m_ui->m_lblMysqlTestResult->setStatus(WidgetWithStatus::Information, tr("No connection test triggered so far."), tr("You did not executed any connection test yet.")); // Load SQLite. - m_ui->m_cmbDatabaseDriver->addItem(tr("SQLite (embedded database)"), APP_DB_SQLITE_DRIVER); + m_ui->m_cmbDatabaseDriver->addItem(qApp->database()->humanDriverName(DatabaseFactory::SQLITE), APP_DB_SQLITE_DRIVER); // Load in-memory database status. Settings *settings = qApp->settings(); @@ -523,7 +523,7 @@ void FormSettings::loadDataStorage() { onMysqlDatabaseChanged(QString()); // Load MySQL. - m_ui->m_cmbDatabaseDriver->addItem(tr("MySQL/MariaDB (dedicated database)"), APP_DB_MYSQL_DRIVER); + m_ui->m_cmbDatabaseDriver->addItem(qApp->database()->humanDriverName(DatabaseFactory::MYSQL), APP_DB_MYSQL_DRIVER); // Setup placeholders. m_ui->m_txtMysqlHostname->lineEdit()->setPlaceholderText(tr("Hostname of your MySQL server")); diff --git a/src/miscellaneous/application.cpp b/src/miscellaneous/application.cpp index cbb872ba3..121c08689 100755 --- a/src/miscellaneous/application.cpp +++ b/src/miscellaneous/application.cpp @@ -195,7 +195,7 @@ void Application::onSaveState(QSessionManager &manager) { void Application::onAboutToQuit() { // Make sure that we obtain close lock BEFORE even trying to quit the application. - bool locked_safely = feedUpdateLock()->tryLock(CLOSE_LOCK_TIMEOUT); + bool locked_safely = feedUpdateLock()->tryLock(4 * CLOSE_LOCK_TIMEOUT); processEvents(); diff --git a/src/miscellaneous/databasecleaner.cpp b/src/miscellaneous/databasecleaner.cpp index 309c75f22..aaa598bcb 100644 --- a/src/miscellaneous/databasecleaner.cpp +++ b/src/miscellaneous/databasecleaner.cpp @@ -17,6 +17,12 @@ #include "miscellaneous/databasecleaner.h" +#include "miscellaneous/application.h" +#include "miscellaneous/databasefactory.h" + +#include +#include + DatabaseCleaner::DatabaseCleaner(QObject *parent) : QObject(parent) { } @@ -24,3 +30,19 @@ DatabaseCleaner::DatabaseCleaner(QObject *parent) : QObject(parent) { DatabaseCleaner::~DatabaseCleaner() { } +void DatabaseCleaner::purgeDatabaseData(const CleanerOrders &which_data) { + qDebug().nospace() << "Performing database cleanup in thread: \'" << QThread::currentThreadId() << "\'."; + + bool result = true; + int progress = 0; + + emit purgeStarted(); + + if (which_data.m_shrinkDatabase) { + result &= qApp->database()->vacuumDatabase(); + progress += 25; + emit purgeProgress(progress); + } + + emit purgeFinished(result); +} diff --git a/src/miscellaneous/databasecleaner.h b/src/miscellaneous/databasecleaner.h index c744d9435..5866a1748 100644 --- a/src/miscellaneous/databasecleaner.h +++ b/src/miscellaneous/databasecleaner.h @@ -21,6 +21,13 @@ #include +struct CleanerOrders { + bool m_removeReadMessages; + bool m_shrinkDatabase; + bool m_removeOldMessages; + int m_barrierForRemovingOldMessagesInDays; +}; + class DatabaseCleaner : public QObject { Q_OBJECT @@ -29,8 +36,12 @@ class DatabaseCleaner : public QObject { virtual ~DatabaseCleaner(); signals: + void purgeStarted(); + void purgeProgress(int progress); + void purgeFinished(bool result); public slots: + void purgeDatabaseData(const CleanerOrders &which_data); }; #endif // DATABASECLEANER_H diff --git a/src/miscellaneous/databasefactory.cpp b/src/miscellaneous/databasefactory.cpp index c0aca8233..561611e7f 100755 --- a/src/miscellaneous/databasefactory.cpp +++ b/src/miscellaneous/databasefactory.cpp @@ -38,6 +38,16 @@ DatabaseFactory::DatabaseFactory(QObject *parent) DatabaseFactory::~DatabaseFactory() { } +qint64 DatabaseFactory::getDatabaseSize() { + if (m_activeDatabaseDriver == SQLITE || m_activeDatabaseDriver == SQLITE_MEMORY) { + qint64 size = QFileInfo(sqliteDatabaseFilePath()).size(); + return size; + } + else { + return 0; + } +} + DatabaseFactory::MySQLError DatabaseFactory::mysqlTestConnection(const QString &hostname, int port, const QString &w_database, const QString &username, const QString &password) { QSqlDatabase database = QSqlDatabase::addDatabase(APP_DB_MYSQL_DRIVER, APP_DB_MYSQL_TEST); @@ -416,6 +426,30 @@ QSqlDatabase DatabaseFactory::connection(const QString &connection_name, Desired } } +QString DatabaseFactory::humanDriverName(DatabaseFactory::UsedDriver driver) { + switch (driver) { + case MYSQL: + return tr("MySQL/MariaDB (dedicated database)"); + + case SQLITE: + case SQLITE_MEMORY: + default: + return tr("SQLite (embedded database)"); + } +} + +QString DatabaseFactory::humanDriverName(const QString &driver_code) { + if (driver_code == APP_DB_SQLITE_DRIVER) { + return humanDriverName(SQLITE); + } + else if (driver_code == APP_DB_MYSQL_DRIVER) { + return humanDriverName(MYSQL); + } + else { + return humanDriverName(SQLITE); + } +} + void DatabaseFactory::removeConnection(const QString &connection_name) { qDebug("Removing database connection '%s'.", qPrintable(connection_name)); QSqlDatabase::removeDatabase(connection_name); diff --git a/src/miscellaneous/databasefactory.h b/src/miscellaneous/databasefactory.h index ebb675637..f062190cc 100755 --- a/src/miscellaneous/databasefactory.h +++ b/src/miscellaneous/databasefactory.h @@ -60,11 +60,16 @@ class DatabaseFactory : public QObject { // Destructor. virtual ~DatabaseFactory(); + qint64 getDatabaseSize(); + // If in-memory is true, then :memory: database is returned // In-memory database is DEFAULT database. // NOTE: This always returns OPENED database. QSqlDatabase connection(const QString &connection_name, DesiredType desired_type); + QString humanDriverName(UsedDriver driver); + QString humanDriverName(const QString &driver_code); + // Removes connection. void removeConnection(const QString &connection_name = QString());