Bit of refactoring, enhanced reporting of state during messages obtaining.

This commit is contained in:
Martin Rotter 2016-09-13 13:45:20 +02:00
parent 846cd1eac5
commit df10d6673e
10 changed files with 54 additions and 46 deletions

View File

@ -88,7 +88,7 @@ void FeedDownloader::stopRunningUpdate() {
m_feeds.clear();
}
void FeedDownloader::oneFeedUpdateFinished(const QList<Message> &messages) {
void FeedDownloader::oneFeedUpdateFinished(const QList<Message> &messages, bool error_during_obtaining) {
QMutexLocker locker(m_mutex);
m_feedsUpdated++;
@ -106,7 +106,7 @@ void FeedDownloader::oneFeedUpdateFinished(const QList<Message> &messages) {
<< feed->id() << " in thread: \'"
<< QThread::currentThreadId() << "\'.";
int updated_messages = feed->updateMessages(messages);
int updated_messages = feed->updateMessages(messages, error_during_obtaining);
/*
QMetaObject::invokeMethod(feed, "updateMessages", Qt::BlockingQueuedConnection,

View File

@ -71,7 +71,7 @@ class FeedDownloader : public QObject {
void stopRunningUpdate();
private slots:
void oneFeedUpdateFinished(const QList<Message> &messages);
void oneFeedUpdateFinished(const QList<Message> &messages, bool error_during_obtaining);
signals:
// Emitted if feed updates started.

View File

@ -50,7 +50,7 @@ QVariant Feed::data(int column, int role) const {
case NewMessages:
return QColor(Qt::blue);
case Error:
case NetworkError:
case ParsingError:
case OtherError:
return QColor(Qt::red);
@ -146,44 +146,44 @@ void Feed::run() {
<< customId() << " in thread: \'"
<< QThread::currentThreadId() << "\'.";
QList<Message> msgs = obtainNewMessages();
emit messagesObtained(msgs);
bool error_during_obtaining;
QList<Message> msgs = obtainNewMessages(&error_during_obtaining);
emit messagesObtained(msgs, error_during_obtaining);
}
int Feed::updateMessages(const QList<Message> &messages) {
int Feed::updateMessages(const QList<Message> &messages, bool error_during_obtaining) {
QList<RootItem*> items_to_update;
int updated_messages = 0;
bool is_main_thread = QThread::currentThread() == qApp->thread();
qDebug("Updating messages in DB. Main thread: '%s'.", qPrintable(is_main_thread ? "true." : "false."));
int custom_id = customId();
int account_id = getParentServiceRoot()->accountId();
bool anything_updated = false;
bool ok;
QSqlDatabase database = is_main_thread ?
qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings) :
qApp->database()->connection(QSL("feed_upd"), DatabaseFactory::FromSettings);
int updated_messages = DatabaseQueries::updateMessages(database, messages, custom_id, account_id, url(), &anything_updated, &ok);
if (ok) {
if (updated_messages > 0) {
setStatus(NewMessages);
if (!error_during_obtaining) {
bool anything_updated = false;
bool ok = true;
if (!messages.isEmpty()) {
int custom_id = customId();
int account_id = getParentServiceRoot()->accountId();
QSqlDatabase database = is_main_thread ?
qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings) :
qApp->database()->connection(QSL("feed_upd"), DatabaseFactory::FromSettings);
updated_messages = DatabaseQueries::updateMessages(database, messages, custom_id, account_id, url(), &anything_updated, &ok);
}
else {
setStatus(Normal);
if (ok) {
setStatus(updated_messages > 0 ? NewMessages : Normal);
updateCounts(true);
if (getParentServiceRoot()->recycleBin() != nullptr && anything_updated) {
getParentServiceRoot()->recycleBin()->updateCounts(true);
items_to_update.append(getParentServiceRoot()->recycleBin());
}
}
QList<RootItem*> items_to_update;
updateCounts(true);
items_to_update.append(this);
if (getParentServiceRoot()->recycleBin() != nullptr && anything_updated) {
getParentServiceRoot()->recycleBin()->updateCounts(true);
items_to_update.append(getParentServiceRoot()->recycleBin());
}
getParentServiceRoot()->itemChanged(items_to_update);
}
items_to_update.append(this);
getParentServiceRoot()->itemChanged(items_to_update);
return updated_messages;
}

View File

@ -44,7 +44,7 @@ class Feed : public RootItem, public QRunnable {
enum Status {
Normal = 0,
NewMessages = 1,
Error = 2,
NetworkError = 2,
ParsingError = 3,
OtherError = 4
};
@ -83,14 +83,14 @@ class Feed : public RootItem, public QRunnable {
public slots:
void updateCounts(bool including_total_count);
int updateMessages(const QList<Message> &messages);
int updateMessages(const QList<Message> &messages, bool error_during_obtaining);
private:
// Performs synchronous obtaining of new messages for this feed.
virtual QList<Message> obtainNewMessages() = 0;
virtual QList<Message> obtainNewMessages(bool *error_during_obtaining) = 0;
signals:
void messagesObtained(QList<Message> messages);
void messagesObtained(QList<Message> messages, bool error_during_obtaining);
private:
QString m_url;

View File

@ -108,15 +108,17 @@ OwnCloudServiceRoot *OwnCloudFeed::serviceRoot() const {
return qobject_cast<OwnCloudServiceRoot*>(getParentServiceRoot());
}
QList<Message> OwnCloudFeed::obtainNewMessages() {
QList<Message> OwnCloudFeed::obtainNewMessages(bool *error_during_obtaining) {
OwnCloudGetMessagesResponse messages = serviceRoot()->network()->getMessages(customId());
if (serviceRoot()->network()->lastError() != QNetworkReply::NoError) {
setStatus(Feed::Error);
setStatus(Feed::NetworkError);
*error_during_obtaining = true;
serviceRoot()->itemChanged(QList<RootItem*>() << this);
return QList<Message>();
}
else {
*error_during_obtaining = false;
return messages.messages();
}

View File

@ -45,7 +45,7 @@ class OwnCloudFeed : public Feed {
OwnCloudServiceRoot *serviceRoot() const;
private:
QList<Message> obtainNewMessages();
QList<Message> obtainNewMessages(bool *error_during_obtaining);
};
#endif // OWNCLOUDFEED_H

View File

@ -430,7 +430,7 @@ bool StandardFeed::editItself(StandardFeed *new_feed_data) {
return true;
}
QList<Message> StandardFeed::obtainNewMessages() {
QList<Message> StandardFeed::obtainNewMessages(bool *error_during_obtaining) {
QByteArray feed_contents;
int download_timeout = qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::UpdateTimeout)).toInt();
m_networkError = NetworkFactory::downloadFeedFile(url(), download_timeout, feed_contents,
@ -438,11 +438,13 @@ QList<Message> StandardFeed::obtainNewMessages() {
if (m_networkError != QNetworkReply::NoError) {
qWarning("Error during fetching of new messages for feed '%s' (id %d).", qPrintable(url()), id());
setStatus(Error);
setStatus(NetworkError);
*error_during_obtaining = true;
return QList<Message>();
}
else if (status() != NewMessages) {
setStatus(Normal);
*error_during_obtaining = false;
}
// Encode downloaded data for further parsing.
@ -507,4 +509,6 @@ StandardFeed::StandardFeed(const QSqlRecord &record) : Feed(nullptr) {
setAutoUpdateType(static_cast<Feed::AutoUpdateType>(record.value(FDS_DB_UPDATE_TYPE_INDEX).toInt()));
setAutoUpdateInitialInterval(record.value(FDS_DB_UPDATE_INTERVAL_INDEX).toInt());
m_networkError = QNetworkReply::NoError;
}

View File

@ -142,7 +142,7 @@ class StandardFeed : public Feed {
void fetchMetadataForItself();
private:
QList<Message> obtainNewMessages();
QList<Message> obtainNewMessages(bool *error_during_obtaining);
private:
bool m_passwordProtected;

View File

@ -156,7 +156,7 @@ bool TtRssFeed::editItself(TtRssFeed *new_feed_data) {
}
}
QList<Message> TtRssFeed::obtainNewMessages() {
QList<Message> TtRssFeed::obtainNewMessages(bool *error_during_obtaining) {
QList<Message> messages;
int newly_added_messages = 0;
int limit = MAX_MESSAGES;
@ -167,7 +167,8 @@ QList<Message> TtRssFeed::obtainNewMessages() {
true, true, false);
if (serviceRoot()->network()->lastError() != QNetworkReply::NoError) {
setStatus(Feed::Error);
setStatus(Feed::NetworkError);
*error_during_obtaining = true;
serviceRoot()->itemChanged(QList<RootItem*>() << this);
return QList<Message>();
}
@ -181,6 +182,7 @@ QList<Message> TtRssFeed::obtainNewMessages() {
}
while (newly_added_messages > 0);
*error_during_obtaining = false;
return messages;
}

View File

@ -47,7 +47,7 @@ class TtRssFeed : public Feed {
bool removeItself();
private:
QList<Message> obtainNewMessages();
QList<Message> obtainNewMessages(bool *error_during_obtaining);
};
#endif // TTRSSFEED_H