From 32b6775c831f56a432a16f60219e00d50cc46be3 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Sun, 10 Jan 2016 11:12:21 +0100 Subject: [PATCH] Const refactoring #2. --- src/core/feeddownloader.cpp | 18 +++++++--- src/core/feeddownloader.h | 7 ++-- src/core/feedsmodel.cpp | 58 +++++++----------------------- src/core/feedsmodel.h | 32 +++++++---------- src/services/abstract/rootitem.cpp | 28 +++++++-------- src/services/abstract/rootitem.h | 14 ++++---- 6 files changed, 63 insertions(+), 94 deletions(-) diff --git a/src/core/feeddownloader.cpp b/src/core/feeddownloader.cpp index ee8953571..e1ebb0e96 100755 --- a/src/core/feeddownloader.cpp +++ b/src/core/feeddownloader.cpp @@ -45,7 +45,7 @@ void FeedDownloader::updateFeeds(const QList &feeds) { int updated_messages = feeds.at(i)->update(); if (updated_messages > 0) { - results.updatedFeeds().append(QPair(feeds.at(i)->title(), updated_messages)); + results.appendUpdatedFeed(QPair(feeds.at(i)->title(), updated_messages)); } qDebug("Made progress in feed updates: %d/%d (id of feed is %d).", i + 1, total, feeds.at(i)->id()); @@ -54,6 +54,8 @@ void FeedDownloader::updateFeeds(const QList &feeds) { qDebug().nospace() << "Finished feed updates in thread: \'" << QThread::currentThreadId() << "\'."; + results.sort(); + // Update of feeds has finished. // NOTE: This means that now "update lock" can be unlocked // and feeds can be added/edited/deleted and application @@ -64,9 +66,7 @@ void FeedDownloader::updateFeeds(const QList &feeds) { FeedDownloadResults::FeedDownloadResults() : m_updatedFeeds(QList >()) { } -QString FeedDownloadResults::overview(int how_many_feeds) { - qSort(m_updatedFeeds.begin(), m_updatedFeeds.end(), FeedDownloadResults::lessThan); - +QString FeedDownloadResults::overview(int how_many_feeds) const { QStringList result; for (int i = 0, number_items_output = qMin(how_many_feeds, m_updatedFeeds.size()); i < number_items_output; i++) { @@ -82,10 +82,18 @@ QString FeedDownloadResults::overview(int how_many_feeds) { return res_str; } +void FeedDownloadResults::appendUpdatedFeed(const QPair &feed) { + m_updatedFeeds.append(feed); +} + +void FeedDownloadResults::sort() { + qSort(m_updatedFeeds.begin(), m_updatedFeeds.end(), FeedDownloadResults::lessThan); +} + bool FeedDownloadResults::lessThan(const QPair &lhs, const QPair &rhs) { return lhs.second > rhs.second; } -QList > &FeedDownloadResults::updatedFeeds() { +QList > FeedDownloadResults::updatedFeeds() const { return m_updatedFeeds; } diff --git a/src/core/feeddownloader.h b/src/core/feeddownloader.h index 105103a19..316c51075 100755 --- a/src/core/feeddownloader.h +++ b/src/core/feeddownloader.h @@ -30,8 +30,11 @@ class FeedDownloadResults { public: explicit FeedDownloadResults(); - QList > &updatedFeeds(); - QString overview(int how_many_feeds); + QList > updatedFeeds() const; + QString overview(int how_many_feeds) const; + + void appendUpdatedFeed(const QPair &feed); + void sort(); static bool lessThan(const QPair &lhs, const QPair &rhs); diff --git a/src/core/feedsmodel.cpp b/src/core/feedsmodel.cpp index 7b84b03a0..1bdaf330e 100755 --- a/src/core/feedsmodel.cpp +++ b/src/core/feedsmodel.cpp @@ -69,8 +69,6 @@ FeedsModel::FeedsModel(QObject *parent) /*: Feed list header "counts" column tooltip.*/ tr("Counts of unread/all meesages."); connect(m_autoUpdateTimer, SIGNAL(timeout()), this, SLOT(executeNextAutoUpdate())); - - //loadActivatedServiceAccounts(); updateAutoUpdateStatus(); } @@ -161,14 +159,14 @@ void FeedsModel::onFeedUpdatesStarted() { qApp->mainForm()->statusBar()->showProgressFeeds(0, tr("Feed update started")); } -void FeedsModel::onFeedUpdatesProgress(Feed *feed, int current, int total) { +void FeedsModel::onFeedUpdatesProgress(const Feed *feed, int current, int total) { // Some feed got updated. qApp->mainForm()->statusBar()->showProgressFeeds((current * 100.0) / total, //: Text display in status bar when particular feed is updated. tr("Updated feed '%1'").arg(feed->title())); } -void FeedsModel::onFeedUpdatesFinished(FeedDownloadResults results) { +void FeedsModel::onFeedUpdatesFinished(const FeedDownloadResults &results) { qApp->feedUpdateLock()->unlock(); qApp->mainForm()->statusBar()->clearProgressFeeds(); @@ -185,7 +183,6 @@ void FeedsModel::updateAllFeeds() { updateFeeds(m_rootItem->getSubTreeFeeds()); } - DatabaseCleaner *FeedsModel::databaseCleaner() { if (m_dbCleaner == NULL) { m_dbCleaner = new DatabaseCleaner(); @@ -293,7 +290,7 @@ Qt::DropActions FeedsModel::supportedDropActions() const { Qt::ItemFlags FeedsModel::flags(const QModelIndex &index) const { Qt::ItemFlags base_flags = QAbstractItemModel::flags(index); - RootItem *item_for_index = itemForIndex(index); + const RootItem *item_for_index = itemForIndex(index); Qt::ItemFlags additional_flags = item_for_index->additionalFlags(); return base_flags | additional_flags; @@ -318,7 +315,7 @@ void FeedsModel::executeNextAutoUpdate() { // Pass needed interval data and lets the model decide which feeds // should be updated in this pass. - QList feeds_for_update = feedsForScheduledUpdate(m_globalAutoUpdateEnabled && m_globalAutoUpdateRemainingInterval == 0); + const QList feeds_for_update = feedsForScheduledUpdate(m_globalAutoUpdateEnabled && m_globalAutoUpdateRemainingInterval == 0); qApp->feedUpdateLock()->unlock(); @@ -559,7 +556,7 @@ QList FeedsModel::feedsForScheduledUpdate(bool auto_update_now) { return feeds_for_update; } -QList FeedsModel::messagesForItem(RootItem *item) { +QList FeedsModel::messagesForItem(RootItem *item) const { return item->undeletedMessages(); } @@ -578,17 +575,6 @@ RootItem *FeedsModel::itemForIndex(const QModelIndex &index) const { } } -Category *FeedsModel::categoryForIndex(const QModelIndex &index) const { - RootItem *item = itemForIndex(index); - - if (item->kind() == RootItemKind::Category) { - return item->toCategory(); - } - else { - return NULL; - } -} - QModelIndex FeedsModel::indexForItem(RootItem *item) const { if (item == NULL || item->kind() == RootItemKind::Root) { // Root item lies on invalid index. @@ -614,7 +600,7 @@ QModelIndex FeedsModel::indexForItem(RootItem *item) const { return target_index; } -bool FeedsModel::hasAnyFeedNewMessages() { +bool FeedsModel::hasAnyFeedNewMessages() const { foreach (const Feed *feed, allFeeds()) { if (feed->status() == Feed::NewMessages) { return true; @@ -645,7 +631,7 @@ void FeedsModel::notifyWithCounts() { } } -void FeedsModel::onItemDataChanged(QList items) { +void FeedsModel::onItemDataChanged(const QList &items) { if (items.size() > RELOAD_MODEL_BORDER_NUM) { qDebug("There is request to reload feed model for more than %d items, reloading model fully.", RELOAD_MODEL_BORDER_NUM); reloadWholeLayout(); @@ -661,17 +647,6 @@ void FeedsModel::onItemDataChanged(QList items) { notifyWithCounts(); } -QStringList FeedsModel::textualFeedIds(const QList &feeds) { - QStringList stringy_ids; - stringy_ids.reserve(feeds.size()); - - foreach (Feed *feed, feeds) { - stringy_ids.append(QString::number(feed->id())); - } - - return stringy_ids; -} - void FeedsModel::reloadWholeLayout() { emit layoutAboutToBeChanged(); emit layoutChanged(); @@ -742,21 +717,10 @@ void FeedsModel::loadActivatedServiceAccounts() { } } -QList FeedsModel::feedsForIndex(const QModelIndex &index) { +QList FeedsModel::feedsForIndex(const QModelIndex &index) const { return itemForIndex(index)->getSubTreeFeeds(); } -Feed *FeedsModel::feedForIndex(const QModelIndex &index) { - RootItem *item = itemForIndex(index); - - if (item->kind() == RootItemKind::Feed) { - return item->toFeed(); - } - else { - return NULL; - } -} - bool FeedsModel::markItemRead(RootItem *item, RootItem::ReadStatus read) { return item->markAsReadUnread(read); } @@ -765,10 +729,12 @@ bool FeedsModel::markItemCleared(RootItem *item, bool clean_read_only) { return item->cleanMessages(clean_read_only); } -QList FeedsModel::allFeeds() { +QList FeedsModel::allFeeds() const { return m_rootItem->getSubTreeFeeds(); } -QList FeedsModel::allCategories() { +QList FeedsModel::allCategories() const { return m_rootItem->getSubTreeCategories(); } + +// TODO: pokračovat s const upravami v dalsi tride diff --git a/src/core/feedsmodel.h b/src/core/feedsmodel.h index a89bf35b3..2042f8d30 100755 --- a/src/core/feedsmodel.h +++ b/src/core/feedsmodel.h @@ -95,29 +95,23 @@ class FeedsModel : public QAbstractItemModel { // Variable "auto_update_now" is true, when global timeout // for scheduled auto-update was met and global auto-update strategy is enabled // so feeds with "default" auto-update strategy should be updated. + // + // This method might change some properties of some feeds. QList feedsForScheduledUpdate(bool auto_update_now); // Returns (undeleted) messages for given feeds. // This is usually used for displaying whole feeds // in "newspaper" mode. - QList messagesForItem(RootItem *item); + QList messagesForItem(RootItem *item) const; // Returns list of all categories contained in the model. - QList allCategories(); + QList allCategories() const; // Returns list of all feeds contained in the model. - QList allFeeds(); + QList allFeeds() const; // Returns ALL RECURSIVE CHILD feeds contained within single index. - QList feedsForIndex(const QModelIndex &index); - - // Returns pointer to feed if it lies on given index - // or NULL if no feed lies on given index. - Feed *feedForIndex(const QModelIndex &index); - - // Returns pointer to category if it lies on given index - // or NULL if no category lies on given index. - Category *categoryForIndex(const QModelIndex &index) const; + QList feedsForIndex(const QModelIndex &index) const; // Returns feed/category which lies at the specified index or // root item if index is invalid. @@ -130,7 +124,7 @@ class FeedsModel : public QAbstractItemModel { QModelIndex indexForItem(RootItem *item) const; // Determines if any feed has any new messages. - bool hasAnyFeedNewMessages(); + bool hasAnyFeedNewMessages() const; // Access to root item. inline RootItem *rootItem() const { @@ -161,8 +155,10 @@ class FeedsModel : public QAbstractItemModel { // If it is, then it reassigns original_node to new parent. void reassignNodeToNewParent(RootItem *original_node, RootItem *new_parent); + // Removes given item from the model/memory. void removeItem(RootItem *deleting_item); + // Recycle bins operations. bool restoreAllBins(); bool emptyAllBins(); @@ -187,15 +183,15 @@ class FeedsModel : public QAbstractItemModel { void notifyWithCounts(); private slots: - void onItemDataChanged(QList items); + void onItemDataChanged(const QList &items); // Is executed when next auto-update round could be done. void executeNextAutoUpdate(); // Reacts on feed updates. void onFeedUpdatesStarted(); - void onFeedUpdatesProgress(Feed *feed, int current, int total); - void onFeedUpdatesFinished(FeedDownloadResults results); + void onFeedUpdatesProgress(const Feed *feed, int current, int total); + void onFeedUpdatesFinished(const FeedDownloadResults &results); signals: // Update of feeds is finished. @@ -226,10 +222,6 @@ class FeedsModel : public QAbstractItemModel { void requireItemValidationAfterDragDrop(const QModelIndex &source_index); private: - // Returns converted ids of given feeds - // which are suitable as IN clause for SQL queries. - QStringList textualFeedIds(const QList &feeds); - RootItem *m_rootItem; QList m_headerData; QList m_tooltipData; diff --git a/src/services/abstract/rootitem.cpp b/src/services/abstract/rootitem.cpp index cc9c20097..d7c17b797 100755 --- a/src/services/abstract/rootitem.cpp +++ b/src/services/abstract/rootitem.cpp @@ -215,15 +215,15 @@ int RootItem::countOfAllMessages() const { return total_count; } -bool RootItem::isChildOf(RootItem *root) { +bool RootItem::isChildOf(const RootItem *root) const { if (root == NULL) { return false; } - RootItem *this_item = this; + const RootItem *this_item = this; while (this_item->kind() != RootItemKind::Root) { - if (root->childItems().contains(this_item)) { + if (root->childItems().contains(const_cast(this_item))) { return true; } else { @@ -234,7 +234,7 @@ bool RootItem::isChildOf(RootItem *root) { return false; } -bool RootItem::isParentOf(RootItem *child) { +bool RootItem::isParentOf(const RootItem *child) const { if (child == NULL) { return false; } @@ -243,11 +243,11 @@ bool RootItem::isParentOf(RootItem *child) { } } -QList RootItem::getSubTree() { +QList RootItem::getSubTree() const { QList children; QList traversable_items; - traversable_items.append(this); + traversable_items.append(const_cast(this)); // Iterate all nested items. while (!traversable_items.isEmpty()) { @@ -260,11 +260,11 @@ QList RootItem::getSubTree() { return children; } -QList RootItem::getSubTree(RootItemKind::Kind kind_of_item) { +QList RootItem::getSubTree(RootItemKind::Kind kind_of_item) const { QList children; QList traversable_items; - traversable_items.append(this); + traversable_items.append(const_cast(this)); // Iterate all nested items. while (!traversable_items.isEmpty()) { @@ -280,11 +280,11 @@ QList RootItem::getSubTree(RootItemKind::Kind kind_of_item) { return children; } -QList RootItem::getSubTreeCategories() { +QList RootItem::getSubTreeCategories() const { QList children; QList traversable_items; - traversable_items.append(this); + traversable_items.append(const_cast(this)); // Iterate all nested items. while (!traversable_items.isEmpty()) { @@ -300,11 +300,11 @@ QList RootItem::getSubTreeCategories() { return children; } -QHash RootItem::getHashedSubTreeCategories() { +QHash RootItem::getHashedSubTreeCategories() const { QHash children; QList traversable_items; - traversable_items.append(this); + traversable_items.append(const_cast(this)); // Iterate all nested items. while (!traversable_items.isEmpty()) { @@ -320,11 +320,11 @@ QHash RootItem::getHashedSubTreeCategories() { return children; } -QList RootItem::getSubTreeFeeds() { +QList RootItem::getSubTreeFeeds() const { QList children; QList traversable_items; - traversable_items.append(this); + traversable_items.append(const_cast(this)); // Iterate all nested items. while (!traversable_items.isEmpty()) { diff --git a/src/services/abstract/rootitem.h b/src/services/abstract/rootitem.h index af7e08ebd..602aee3c1 100755 --- a/src/services/abstract/rootitem.h +++ b/src/services/abstract/rootitem.h @@ -171,18 +171,18 @@ class RootItem : public QObject { // Checks whether "this" object is child (direct or indirect) // of the given root. - bool isChildOf(RootItem *root); + bool isChildOf(const RootItem *root) const; // Is "this" item parent (direct or indirect) if given child? - bool isParentOf(RootItem *child); + bool isParentOf(const RootItem *child) const; // Returns flat list of all items from subtree where this item is a root. // Returned list includes this item too. - QList getSubTree(); - QList getSubTree(RootItemKind::Kind kind_of_item); - QList getSubTreeCategories(); - QHash getHashedSubTreeCategories(); - QList getSubTreeFeeds(); + QList getSubTree() const; + QList getSubTree(RootItemKind::Kind kind_of_item) const; + QList getSubTreeCategories() const; + QHash getHashedSubTreeCategories() const; + QList getSubTreeFeeds() const; // Returns the service root node which is direct or indirect parent of current item. ServiceRoot *getParentServiceRoot();