Generalize some methods a bit

This commit is contained in:
Martin Rotter 2021-08-20 12:09:52 +02:00
parent f3287421fe
commit 620aa20868
9 changed files with 70 additions and 69 deletions

View File

@ -150,7 +150,8 @@ void FeedDownloader::updateFeeds(const QList<Feed*>& feeds) {
while (!m_feeds.isEmpty()) {
auto n_f = m_feeds.takeFirst();
updateOneFeed(n_f,
updateOneFeed(n_f->getParentServiceRoot(),
n_f,
stated_messages.value(n_f->getParentServiceRoot()).value(n_f->customId()),
tagged_messages.value(n_f->getParentServiceRoot()));
}
@ -165,7 +166,8 @@ void FeedDownloader::stopRunningUpdate() {
m_feedsOriginalCount = m_feedsUpdated = 0;
}
void FeedDownloader::updateOneFeed(Feed* feed,
void FeedDownloader::updateOneFeed(ServiceRoot* acc,
Feed* feed,
const QHash<ServiceRoot::BagOfMessages, QStringList>& stated_messages,
const QHash<QString, QStringList>& tagged_messages) {
qDebugNN << LOGSEC_FEEDDOWNLOADER
@ -351,7 +353,7 @@ void FeedDownloader::updateOneFeed(Feed* feed,
<< QThread::currentThreadId() << "'.";
tmr.restart();
auto updated_messages = feed->updateMessages(msgs, false);
auto updated_messages = acc->updateMessages(msgs, feed, false);
qDebugNN << LOGSEC_FEEDDOWNLOADER
<< "Updating messages in DB took " << tmr.nsecsElapsed() / 1000 << " microseconds.";

View File

@ -54,7 +54,8 @@ class FeedDownloader : public QObject {
void updateProgress(const Feed* feed, int current, int total);
private:
void updateOneFeed(Feed* feed,
void updateOneFeed(ServiceRoot* acc,
Feed* feed,
const QHash<ServiceRoot::BagOfMessages, QStringList>& stated_messages,
const QHash<QString, QStringList>& tagged_messages);
void finalizeUpdate();

View File

@ -215,8 +215,6 @@ void FormMain::prepareMenus() {
#endif
// Add needed items to the menu.
m_trayMenu->addAction(m_ui->m_actionSwitchMainWindow);
m_trayMenu->addSeparator();
m_trayMenu->addAction(m_ui->m_actionUpdateAllItems);
m_trayMenu->addAction(m_ui->m_actionMarkAllItemsRead);
m_trayMenu->addSeparator();
@ -846,7 +844,7 @@ void FormMain::restoreDatabaseSettings() {
void FormMain::changeEvent(QEvent* event) {
switch (event->type()) {
case QEvent::WindowStateChange: {
case QEvent::Type::WindowStateChange: {
if ((windowState() & Qt::WindowState::WindowMinimized) == Qt::WindowState::WindowMinimized &&
SystemTrayIcon::isSystemTrayDesired() &&
SystemTrayIcon::isSystemTrayAreaAvailable() &&

View File

@ -421,7 +421,7 @@
<bool>false</bool>
</property>
<property name="text">
<string>Switch main &amp;window visibility</string>
<string>Minimize (or hide) main window</string>
</property>
<property name="toolTip">
<string>Hides main window if it is visible and shows it if it is hidden.</string>

View File

@ -424,7 +424,7 @@ void FormMessageFiltersManager::processCheckedFeeds() {
}
// Update messages in DB and reload selection.
it->toFeed()->updateMessages(msgs, true);
it->getParentServiceRoot()->updateMessages(msgs, it->toFeed(), true);
displayMessagesOfFeed();
}
}

View File

@ -189,63 +189,6 @@ bool Feed::markAsReadUnread(RootItem::ReadStatus status) {
return service->markFeedsReadUnread(QList<Feed*>() << this, status);
}
QPair<int, int> Feed::updateMessages(QList<Message>& messages, bool force_update) {
QPair<int, int> updated_messages = { 0, 0 };
if (messages.isEmpty()) {
qDebugNN << "No messages to be updated/added in DB for feed"
<< QUOTE_W_SPACE_DOT(customId());
return updated_messages;
}
QList<RootItem*> items_to_update;
bool is_main_thread = QThread::currentThread() == qApp->thread();
qDebugNN << LOGSEC_CORE
<< "Updating messages in DB. Main thread:"
<< QUOTE_W_SPACE_DOT(is_main_thread);
bool ok = false;
QSqlDatabase database = is_main_thread ?
qApp->database()->driver()->connection(metaObject()->className()) :
qApp->database()->driver()->connection(QSL("feed_upd"));
updated_messages = DatabaseQueries::updateMessages(database, messages,
this, force_update,
&ok);
if (updated_messages.first > 0 || updated_messages.second > 0) {
// Something was added or updated in the DB, update numbers.
updateCounts(true);
if (getParentServiceRoot()->recycleBin() != nullptr) {
getParentServiceRoot()->recycleBin()->updateCounts(true);
items_to_update.append(getParentServiceRoot()->recycleBin());
}
if (getParentServiceRoot()->importantNode() != nullptr) {
getParentServiceRoot()->importantNode()->updateCounts(true);
items_to_update.append(getParentServiceRoot()->importantNode());
}
if (getParentServiceRoot()->unreadNode() != nullptr) {
getParentServiceRoot()->unreadNode()->updateCounts(true);
items_to_update.append(getParentServiceRoot()->unreadNode());
}
if (getParentServiceRoot()->labelsNode() != nullptr) {
getParentServiceRoot()->labelsNode()->updateCounts(true);
items_to_update.append(getParentServiceRoot()->labelsNode());
}
}
// Some messages were really added to DB, reload feed in model.
items_to_update.append(this);
getParentServiceRoot()->itemChanged(items_to_update);
return updated_messages;
}
QString Feed::getAutoUpdateStatusDescription() const {
QString auto_update_string;

View File

@ -78,9 +78,6 @@ class Feed : public RootItem {
void setMessageFilters(const QList<QPointer<MessageFilter>>& messageFilters);
void removeMessageFilter(MessageFilter* filter);
// Returns counts of updated messages <unread, all>.
QPair<int, int> updateMessages(QList<Message>& messages, bool force_update);
public slots:
virtual void updateCounts(bool including_total_count);

View File

@ -18,6 +18,8 @@
#include "services/abstract/recyclebin.h"
#include "services/abstract/unreadnode.h"
#include <QThread>
ServiceRoot::ServiceRoot(RootItem* parent)
: RootItem(parent), m_recycleBin(new RecycleBin(this)), m_importantNode(new ImportantNode(this)),
m_labelsNode(new LabelsNode(this)), m_unreadNode(new UnreadNode(this)),
@ -864,3 +866,58 @@ ServiceRoot::LabelOperation operator|(ServiceRoot::LabelOperation lhs, ServiceRo
ServiceRoot::LabelOperation operator&(ServiceRoot::LabelOperation lhs, ServiceRoot::LabelOperation rhs) {
return static_cast<ServiceRoot::LabelOperation>(static_cast<char>(lhs) & static_cast<char>(rhs));
}
QPair<int, int> ServiceRoot::updateMessages(QList<Message>& messages, Feed* feed, bool force_update) {
QPair<int, int> updated_messages = { 0, 0 };
if (messages.isEmpty()) {
qDebugNN << "No messages to be updated/added in DB for feed"
<< QUOTE_W_SPACE_DOT(feed->customId());
return updated_messages;
}
QList<RootItem*> items_to_update;
bool is_main_thread = QThread::currentThread() == qApp->thread();
qDebugNN << LOGSEC_CORE
<< "Updating messages in DB. Main thread:"
<< QUOTE_W_SPACE_DOT(is_main_thread);
bool ok = false;
QSqlDatabase database = is_main_thread ?
qApp->database()->driver()->connection(metaObject()->className()) :
qApp->database()->driver()->connection(QSL("feed_upd"));
updated_messages = DatabaseQueries::updateMessages(database, messages, feed, force_update, &ok);
if (updated_messages.first > 0 || updated_messages.second > 0) {
// Something was added or updated in the DB, update numbers.
feed->updateCounts(true);
if (recycleBin() != nullptr) {
recycleBin()->updateCounts(true);
items_to_update.append(recycleBin());
}
if (importantNode() != nullptr) {
importantNode()->updateCounts(true);
items_to_update.append(importantNode());
}
if (unreadNode() != nullptr) {
unreadNode()->updateCounts(true);
items_to_update.append(unreadNode());
}
if (labelsNode() != nullptr) {
labelsNode()->updateCounts(true);
items_to_update.append(labelsNode());
}
}
// Some messages were really added to DB, reload feed in model.
items_to_update.append(feed);
getParentServiceRoot()->itemChanged(items_to_update);
return updated_messages;
}

View File

@ -193,6 +193,9 @@ class ServiceRoot : public RootItem {
// and from model.
void completelyRemoveAllData();
// Returns counts of updated messages <unread, all>.
QPair<int, int> updateMessages(QList<Message>& messages, Feed* feed, bool force_update);
QIcon feedIconForMessage(const QString& feed_custom_id) const;
// Removes all/read only messages from given underlying feeds.