diff --git a/resources/graphics/icons/mini-kfaenza/defragment-database.png b/resources/graphics/icons/mini-kfaenza/defragment-database.png new file mode 100644 index 000000000..5d2b27421 Binary files /dev/null and b/resources/graphics/icons/mini-kfaenza/defragment-database.png differ diff --git a/resources/graphics/icons/mini-kfaenza/view-switch.png b/resources/graphics/icons/mini-kfaenza/view-switch.png index 636917de7..176596c31 100644 Binary files a/resources/graphics/icons/mini-kfaenza/view-switch.png and b/resources/graphics/icons/mini-kfaenza/view-switch.png differ diff --git a/src/core/databasefactory.cpp b/src/core/databasefactory.cpp index c3c70891c..03b270f97 100644 --- a/src/core/databasefactory.cpp +++ b/src/core/databasefactory.cpp @@ -221,6 +221,7 @@ QSqlDatabase DatabaseFactory::initializeFileBasedDatabase(const QString &connect return database; } + QSqlDatabase DatabaseFactory::connection(const QString &connection_name, DesiredType desired_type) { if (desired_type == DatabaseFactory::StrictlyInMemory || @@ -331,3 +332,10 @@ void DatabaseFactory::determineInMemoryDatabase() { qDebug("Working database source was determined as %s.", m_inMemoryEnabled ? "in-memory database" : "file-based database"); } + +bool DatabaseFactory::vacuumDatabase() { + QSqlDatabase database = connection(objectName(), FromSettings); + QSqlQuery query_vacuum(database); + + return query_vacuum.exec("VACUUM"); +} diff --git a/src/core/databasefactory.h b/src/core/databasefactory.h index 5608b4c72..564392ed6 100644 --- a/src/core/databasefactory.h +++ b/src/core/databasefactory.h @@ -41,6 +41,16 @@ class DatabaseFactory : public QObject { // Sets m_inMemoryEnabled according to user settings. void determineInMemoryDatabase(); + // Performs "VACUUM" on the database and + // returns true of operation succeeded. + bool vacuumDatabase(); + + // Returns whether in-memory database feature is currently + // used. + inline bool usingInMemoryDatabase() const { + return m_inMemoryEnabled; + } + // Singleton getter. static DatabaseFactory *instance(); diff --git a/src/gui/feedmessageviewer.cpp b/src/gui/feedmessageviewer.cpp index 4153d568b..8dc642c59 100644 --- a/src/gui/feedmessageviewer.cpp +++ b/src/gui/feedmessageviewer.cpp @@ -1,6 +1,7 @@ #include "gui/feedmessageviewer.h" #include "core/settings.h" +#include "core/databasefactory.h" #include "core/messagesproxymodel.h" #include "core/feeddownloader.h" #include "core/feedsmodelstandardfeed.h" @@ -216,6 +217,8 @@ void FeedMessageViewer::createConnections() { SIGNAL(triggered()), m_feedsView, SLOT(openSelectedFeedsInNewspaperMode())); connect(form_main->m_ui->m_actionDeleteSelectedFeedCategory, SIGNAL(triggered()), m_feedsView, SLOT(deleteSelectedItem())); + connect(form_main->m_ui->m_actionDefragmentDatabase, + SIGNAL(triggered()), this, SLOT(vacuumDatabase())); } void FeedMessageViewer::initialize() { @@ -279,3 +282,57 @@ void FeedMessageViewer::initializeViews() { // Set layout as active. setLayout(central_layout); } + + +void FeedMessageViewer::vacuumDatabase() { + bool is_tray_activated = SystemTrayIcon::isSystemTrayActivated(); + + if (!SystemFactory::instance()->applicationCloseLock()->tryLock()) { + // Lock was not obtained because + // it is used probably by feed updater or application + // is quitting. + if (is_tray_activated) { + SystemTrayIcon::instance()->showMessage(tr("Cannot defragment database"), + tr("Database cannot be defragmented because feed update is ongoing."), + QSystemTrayIcon::Warning); + } + else { + MessageBox::show(this, + QMessageBox::Warning, + tr("Cannot defragment database"), + tr("Database cannot be defragmented because feed update is ongoing.")); + } + + // Thus, cannot delete and quit the method. + return; + } + + if (DatabaseFactory::instance()->vacuumDatabase()) { + if (is_tray_activated) { + SystemTrayIcon::instance()->showMessage(tr("Database defragmented"), + tr("Database was successfully defragmented."), + QSystemTrayIcon::Information); + } + else { + MessageBox::show(this, + QMessageBox::Information, + tr("Database defragmented"), + tr("Database was successfully defragmented.")); + } + } + else { + if (is_tray_activated) { + SystemTrayIcon::instance()->showMessage(tr("Database was not defragmented"), + tr("Database was not defragmented, try again later."), + QSystemTrayIcon::Warning); + } + else { + MessageBox::show(this, + QMessageBox::Warning, + tr("Database was not defragmented"), + tr("Database was not defragmented, try again later.")); + } + } + + SystemFactory::instance()->applicationCloseLock()->unlock(); +} diff --git a/src/gui/feedmessageviewer.h b/src/gui/feedmessageviewer.h index 2436a01e7..d8cae5469 100644 --- a/src/gui/feedmessageviewer.h +++ b/src/gui/feedmessageviewer.h @@ -43,6 +43,9 @@ class FeedMessageViewer : public TabContent { // stops any child widgets/workers. void quit(); + public slots: + void vacuumDatabase(); + protected slots: // Updates counts of messages for example in tray icon. void updateTrayIconStatus(int unread_messages, int total_messages); diff --git a/src/gui/formmain.cpp b/src/gui/formmain.cpp index 1d80151a5..99ee40f7c 100755 --- a/src/gui/formmain.cpp +++ b/src/gui/formmain.cpp @@ -224,6 +224,7 @@ void FormMain::setupIcons() { m_ui->m_actionExport->setIcon(icon_theme_factory->fromTheme("document-export")); m_ui->m_actionFullscreen->setIcon(icon_theme_factory->fromTheme("view-fullscreen")); m_ui->m_actionSwitchMainWindow->setIcon(icon_theme_factory->fromTheme("view-switch")); + m_ui->m_actionDefragmentDatabase->setIcon(icon_theme_factory->fromTheme("defragment-database")); // Web browser. m_ui->m_actionAddBrowser->setIcon(icon_theme_factory->fromTheme("list-add")); diff --git a/src/gui/formmain.ui b/src/gui/formmain.ui index f0eac861d..f82351698 100644 --- a/src/gui/formmain.ui +++ b/src/gui/formmain.ui @@ -74,6 +74,8 @@ &Tools + + @@ -447,6 +449,14 @@ + + + Defragment database + + + Defragment database file so that its size decreases. + +