Skip to content

Commit

Permalink
Bit of refactoring, enhanced reporting of state during messages obtai…
Browse files Browse the repository at this point in the history
…ning.
  • Loading branch information
martinrotter committed Sep 13, 2016
1 parent 846cd1e commit df10d66
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/core/feeddownloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/core/feeddownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
60 changes: 30 additions & 30 deletions src/services/abstract/feed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

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());

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());
}
}

getParentServiceRoot()->itemChanged(items_to_update);
}

items_to_update.append(this);
getParentServiceRoot()->itemChanged(items_to_update);

return updated_messages;
}
8 changes: 4 additions & 4 deletions src/services/abstract/feed.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Feed : public RootItem, public QRunnable {
enum Status {
Normal = 0,
NewMessages = 1,
Error = 2,
NetworkError = 2,
ParsingError = 3,
OtherError = 4
};
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/services/owncloud/owncloudfeed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/owncloud/owncloudfeed.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 6 additions & 2 deletions src/services/standard/standardfeed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -430,19 +430,21 @@ 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,
passwordProtected(), username(), password()).first;

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.
Expand Down Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/services/standard/standardfeed.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/services/tt-rss/ttrssfeed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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>();
}
Expand All @@ -181,6 +182,7 @@ QList<Message> TtRssFeed::obtainNewMessages() {
}
while (newly_added_messages > 0);

*error_during_obtaining = false;
return messages;
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/tt-rss/ttrssfeed.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class TtRssFeed : public Feed {
bool removeItself();

private:
QList<Message> obtainNewMessages();
QList<Message> obtainNewMessages(bool *error_during_obtaining);
};

#endif // TTRSSFEED_H

0 comments on commit df10d66

Please sign in to comment.