From 7ea94ef42f4095262788f3060e7d6788b07ce7a4 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Mon, 23 Nov 2015 19:10:16 +0100 Subject: [PATCH] Refactored DB cleaner. --- src/core/feedsmodel.cpp | 38 +++++++++++++++++++++++++++++++++- src/core/feedsmodel.h | 7 ++++++- src/gui/feedmessageviewer.cpp | 39 +++-------------------------------- src/gui/feedmessageviewer.h | 6 ------ src/gui/messagesview.cpp | 5 +---- 5 files changed, 47 insertions(+), 48 deletions(-) diff --git a/src/core/feedsmodel.cpp b/src/core/feedsmodel.cpp index 09a5b10e6..95bdf6dd5 100755 --- a/src/core/feedsmodel.cpp +++ b/src/core/feedsmodel.cpp @@ -24,6 +24,7 @@ #include "services/standard/standardserviceroot.h" #include "miscellaneous/textfactory.h" #include "miscellaneous/databasefactory.h" +#include "miscellaneous/databasecleaner.h" #include "miscellaneous/iconfactory.h" #include "miscellaneous/mutex.h" #include "gui/messagebox.h" @@ -44,7 +45,9 @@ FeedsModel::FeedsModel(QObject *parent) - : QAbstractItemModel(parent), m_autoUpdateTimer(new QTimer(this)), m_feedDownloaderThread(NULL), m_feedDownloader(NULL) { + : QAbstractItemModel(parent), m_autoUpdateTimer(new QTimer(this)), + m_feedDownloaderThread(NULL), m_feedDownloader(NULL), + m_dbCleanerThread(NULL), m_dbCleaner(NULL) { setObjectName(QSL("FeedsModel")); // Create root item. @@ -98,12 +101,27 @@ void FeedsModel::quit() { } } + if (m_dbCleanerThread != NULL && m_dbCleanerThread->isRunning()) { + qDebug("Quitting database cleaner thread."); + m_dbCleanerThread->quit(); + + 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 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()) { markItemCleared(m_rootItem, true); } @@ -167,6 +185,24 @@ void FeedsModel::updateAllFeeds() { updateFeeds(m_rootItem->getSubTreeFeeds()); } + +DatabaseCleaner *FeedsModel::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 FeedsModel::executeNextAutoUpdate() { if (!qApp->feedUpdateLock()->tryLock()) { qDebug("Delaying scheduled feed auto-updates for one minute due to another running update."); diff --git a/src/core/feedsmodel.h b/src/core/feedsmodel.h index 412aa7b36..75ec450c7 100755 --- a/src/core/feedsmodel.h +++ b/src/core/feedsmodel.h @@ -24,7 +24,7 @@ #include "core/rootitem.h" #include "core/feeddownloader.h" - +class DatabaseCleaner; class Category; class Feed; class ServiceRoot; @@ -139,6 +139,8 @@ class FeedsModel : public QAbstractItemModel { // Schedules all feeds from all accounts for update. void updateAllFeeds(); + DatabaseCleaner *databaseCleaner(); + // Adds given service root account. bool addServiceAccount(ServiceRoot *root); @@ -209,6 +211,9 @@ class FeedsModel : public QAbstractItemModel { QThread *m_feedDownloaderThread; FeedDownloader *m_feedDownloader; + + QThread *m_dbCleanerThread; + DatabaseCleaner *m_dbCleaner; }; #endif // FEEDSMODEL_H diff --git a/src/gui/feedmessageviewer.cpp b/src/gui/feedmessageviewer.cpp index f956974ed..1ac26e82e 100755 --- a/src/gui/feedmessageviewer.cpp +++ b/src/gui/feedmessageviewer.cpp @@ -64,9 +64,7 @@ FeedMessageViewer::FeedMessageViewer(QWidget *parent) m_toolBarMessages(new MessagesToolBar(tr("Toolbar for messages"), this)), m_messagesView(new MessagesView(this)), m_feedsView(new FeedsView(this)), - m_messagesBrowser(new WebBrowser(this)), - m_dbCleanerThread(NULL), - m_dbCleaner(NULL) { + m_messagesBrowser(new WebBrowser(this)) { initialize(); initializeViews(); loadMessageViewerFonts(); @@ -77,23 +75,6 @@ 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(); @@ -148,21 +129,7 @@ void FeedMessageViewer::loadMessageViewerFonts() { void FeedMessageViewer::quit() { // Quit the feeds model (stops auto-update timer etc.). m_feedsView->sourceModel()->quit(); - - if (m_dbCleanerThread != NULL && m_dbCleanerThread->isRunning()) { - qDebug("Quitting database cleaner thread."); - m_dbCleanerThread->quit(); - - if (!m_dbCleanerThread->wait(CLOSE_LOCK_TIMEOUT)) { - qCritical("Database cleaner thread is running despite it was told to quit. Terminating it."); - m_dbCleanerThread->terminate(); - } - } - - if (m_dbCleaner != NULL) { - qDebug("Database cleaner exists. Deleting it from memory."); - m_dbCleaner->deleteLater(); - } + } bool FeedMessageViewer::areToolBarsEnabled() const { @@ -428,7 +395,7 @@ void FeedMessageViewer::initializeViews() { void FeedMessageViewer::showDbCleanupAssistant() { if (qApp->feedUpdateLock()->tryLock()) { QPointer form_pointer = new FormDatabaseCleanup(this); - form_pointer.data()->setCleaner(databaseCleaner()); + form_pointer.data()->setCleaner(m_feedsView->sourceModel()->databaseCleaner()); form_pointer.data()->exec(); delete form_pointer.data(); diff --git a/src/gui/feedmessageviewer.h b/src/gui/feedmessageviewer.h index 5d81e6ebb..83b07baa1 100755 --- a/src/gui/feedmessageviewer.h +++ b/src/gui/feedmessageviewer.h @@ -29,7 +29,6 @@ class MessagesView; class MessagesToolBar; class FeedsToolBar; class FeedsView; -class DatabaseCleaner; class StandardFeed; class QToolBar; class QSplitter; @@ -65,8 +64,6 @@ 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. @@ -134,9 +131,6 @@ class FeedMessageViewer : public TabContent { QWidget *m_feedsWidget; QWidget *m_messagesWidget; WebBrowser *m_messagesBrowser; - - QThread *m_dbCleanerThread; - DatabaseCleaner *m_dbCleaner; }; #endif // FEEDMESSAGEVIEWER_H diff --git a/src/gui/messagesview.cpp b/src/gui/messagesview.cpp index 39e1bcbdd..d60d38938 100755 --- a/src/gui/messagesview.cpp +++ b/src/gui/messagesview.cpp @@ -349,9 +349,6 @@ void MessagesView::setSelectedMessagesReadStatus(RootItem::ReadStatus read) { m_batchUnreadSwitch = false; } - -// TODO: restore messages je uplnÄ› stejnÄ› jako tahle fce -// akorat se vola jina metoda na source modelu. void MessagesView::deleteSelectedMessages() { QModelIndex current_index = selectionModel()->currentIndex(); @@ -409,7 +406,7 @@ void MessagesView::reselectIndexes(const QModelIndexList &indexes) { QItemSelection selection; foreach (const QModelIndex &index, indexes) { - // TODO: THIS IS very slow. Try to select 4000 messages and hit "mark as read" button. + // FIXME: THIS IS very slow. Try to select 4000 messages and hit "mark as read" button. selection.merge(QItemSelection(index, index), QItemSelectionModel::Select); }