Const refactoring #2.

This commit is contained in:
Martin Rotter 2016-01-10 11:12:21 +01:00
parent ebbeb8056b
commit 32b6775c83
6 changed files with 63 additions and 94 deletions

View File

@ -45,7 +45,7 @@ void FeedDownloader::updateFeeds(const QList<Feed*> &feeds) {
int updated_messages = feeds.at(i)->update(); int updated_messages = feeds.at(i)->update();
if (updated_messages > 0) { if (updated_messages > 0) {
results.updatedFeeds().append(QPair<QString,int>(feeds.at(i)->title(), updated_messages)); results.appendUpdatedFeed(QPair<QString,int>(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()); 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<Feed*> &feeds) {
qDebug().nospace() << "Finished feed updates in thread: \'" << QThread::currentThreadId() << "\'."; qDebug().nospace() << "Finished feed updates in thread: \'" << QThread::currentThreadId() << "\'.";
results.sort();
// Update of feeds has finished. // Update of feeds has finished.
// NOTE: This means that now "update lock" can be unlocked // NOTE: This means that now "update lock" can be unlocked
// and feeds can be added/edited/deleted and application // and feeds can be added/edited/deleted and application
@ -64,9 +66,7 @@ void FeedDownloader::updateFeeds(const QList<Feed*> &feeds) {
FeedDownloadResults::FeedDownloadResults() : m_updatedFeeds(QList<QPair<QString,int> >()) { FeedDownloadResults::FeedDownloadResults() : m_updatedFeeds(QList<QPair<QString,int> >()) {
} }
QString FeedDownloadResults::overview(int how_many_feeds) { QString FeedDownloadResults::overview(int how_many_feeds) const {
qSort(m_updatedFeeds.begin(), m_updatedFeeds.end(), FeedDownloadResults::lessThan);
QStringList result; QStringList result;
for (int i = 0, number_items_output = qMin(how_many_feeds, m_updatedFeeds.size()); i < number_items_output; i++) { 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; return res_str;
} }
void FeedDownloadResults::appendUpdatedFeed(const QPair<QString,int> &feed) {
m_updatedFeeds.append(feed);
}
void FeedDownloadResults::sort() {
qSort(m_updatedFeeds.begin(), m_updatedFeeds.end(), FeedDownloadResults::lessThan);
}
bool FeedDownloadResults::lessThan(const QPair<QString, int> &lhs, const QPair<QString, int> &rhs) { bool FeedDownloadResults::lessThan(const QPair<QString, int> &lhs, const QPair<QString, int> &rhs) {
return lhs.second > rhs.second; return lhs.second > rhs.second;
} }
QList<QPair<QString,int> > &FeedDownloadResults::updatedFeeds() { QList<QPair<QString,int> > FeedDownloadResults::updatedFeeds() const {
return m_updatedFeeds; return m_updatedFeeds;
} }

View File

@ -30,8 +30,11 @@ class FeedDownloadResults {
public: public:
explicit FeedDownloadResults(); explicit FeedDownloadResults();
QList<QPair<QString,int> > &updatedFeeds(); QList<QPair<QString,int> > updatedFeeds() const;
QString overview(int how_many_feeds); QString overview(int how_many_feeds) const;
void appendUpdatedFeed(const QPair<QString,int> &feed);
void sort();
static bool lessThan(const QPair<QString,int> &lhs, const QPair<QString,int> &rhs); static bool lessThan(const QPair<QString,int> &lhs, const QPair<QString,int> &rhs);

View File

@ -69,8 +69,6 @@ FeedsModel::FeedsModel(QObject *parent)
/*: Feed list header "counts" column tooltip.*/ tr("Counts of unread/all meesages."); /*: Feed list header "counts" column tooltip.*/ tr("Counts of unread/all meesages.");
connect(m_autoUpdateTimer, SIGNAL(timeout()), this, SLOT(executeNextAutoUpdate())); connect(m_autoUpdateTimer, SIGNAL(timeout()), this, SLOT(executeNextAutoUpdate()));
//loadActivatedServiceAccounts();
updateAutoUpdateStatus(); updateAutoUpdateStatus();
} }
@ -161,14 +159,14 @@ void FeedsModel::onFeedUpdatesStarted() {
qApp->mainForm()->statusBar()->showProgressFeeds(0, tr("Feed update started")); 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. // Some feed got updated.
qApp->mainForm()->statusBar()->showProgressFeeds((current * 100.0) / total, qApp->mainForm()->statusBar()->showProgressFeeds((current * 100.0) / total,
//: Text display in status bar when particular feed is updated. //: Text display in status bar when particular feed is updated.
tr("Updated feed '%1'").arg(feed->title())); tr("Updated feed '%1'").arg(feed->title()));
} }
void FeedsModel::onFeedUpdatesFinished(FeedDownloadResults results) { void FeedsModel::onFeedUpdatesFinished(const FeedDownloadResults &results) {
qApp->feedUpdateLock()->unlock(); qApp->feedUpdateLock()->unlock();
qApp->mainForm()->statusBar()->clearProgressFeeds(); qApp->mainForm()->statusBar()->clearProgressFeeds();
@ -185,7 +183,6 @@ void FeedsModel::updateAllFeeds() {
updateFeeds(m_rootItem->getSubTreeFeeds()); updateFeeds(m_rootItem->getSubTreeFeeds());
} }
DatabaseCleaner *FeedsModel::databaseCleaner() { DatabaseCleaner *FeedsModel::databaseCleaner() {
if (m_dbCleaner == NULL) { if (m_dbCleaner == NULL) {
m_dbCleaner = new DatabaseCleaner(); m_dbCleaner = new DatabaseCleaner();
@ -293,7 +290,7 @@ Qt::DropActions FeedsModel::supportedDropActions() const {
Qt::ItemFlags FeedsModel::flags(const QModelIndex &index) const { Qt::ItemFlags FeedsModel::flags(const QModelIndex &index) const {
Qt::ItemFlags base_flags = QAbstractItemModel::flags(index); 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(); Qt::ItemFlags additional_flags = item_for_index->additionalFlags();
return base_flags | additional_flags; return base_flags | additional_flags;
@ -318,7 +315,7 @@ void FeedsModel::executeNextAutoUpdate() {
// Pass needed interval data and lets the model decide which feeds // Pass needed interval data and lets the model decide which feeds
// should be updated in this pass. // should be updated in this pass.
QList<Feed*> feeds_for_update = feedsForScheduledUpdate(m_globalAutoUpdateEnabled && m_globalAutoUpdateRemainingInterval == 0); const QList<Feed*> feeds_for_update = feedsForScheduledUpdate(m_globalAutoUpdateEnabled && m_globalAutoUpdateRemainingInterval == 0);
qApp->feedUpdateLock()->unlock(); qApp->feedUpdateLock()->unlock();
@ -559,7 +556,7 @@ QList<Feed*> FeedsModel::feedsForScheduledUpdate(bool auto_update_now) {
return feeds_for_update; return feeds_for_update;
} }
QList<Message> FeedsModel::messagesForItem(RootItem *item) { QList<Message> FeedsModel::messagesForItem(RootItem *item) const {
return item->undeletedMessages(); 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 { QModelIndex FeedsModel::indexForItem(RootItem *item) const {
if (item == NULL || item->kind() == RootItemKind::Root) { if (item == NULL || item->kind() == RootItemKind::Root) {
// Root item lies on invalid index. // Root item lies on invalid index.
@ -614,7 +600,7 @@ QModelIndex FeedsModel::indexForItem(RootItem *item) const {
return target_index; return target_index;
} }
bool FeedsModel::hasAnyFeedNewMessages() { bool FeedsModel::hasAnyFeedNewMessages() const {
foreach (const Feed *feed, allFeeds()) { foreach (const Feed *feed, allFeeds()) {
if (feed->status() == Feed::NewMessages) { if (feed->status() == Feed::NewMessages) {
return true; return true;
@ -645,7 +631,7 @@ void FeedsModel::notifyWithCounts() {
} }
} }
void FeedsModel::onItemDataChanged(QList<RootItem*> items) { void FeedsModel::onItemDataChanged(const QList<RootItem *> &items) {
if (items.size() > RELOAD_MODEL_BORDER_NUM) { 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); qDebug("There is request to reload feed model for more than %d items, reloading model fully.", RELOAD_MODEL_BORDER_NUM);
reloadWholeLayout(); reloadWholeLayout();
@ -661,17 +647,6 @@ void FeedsModel::onItemDataChanged(QList<RootItem*> items) {
notifyWithCounts(); notifyWithCounts();
} }
QStringList FeedsModel::textualFeedIds(const QList<Feed*> &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() { void FeedsModel::reloadWholeLayout() {
emit layoutAboutToBeChanged(); emit layoutAboutToBeChanged();
emit layoutChanged(); emit layoutChanged();
@ -742,21 +717,10 @@ void FeedsModel::loadActivatedServiceAccounts() {
} }
} }
QList<Feed*> FeedsModel::feedsForIndex(const QModelIndex &index) { QList<Feed*> FeedsModel::feedsForIndex(const QModelIndex &index) const {
return itemForIndex(index)->getSubTreeFeeds(); 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) { bool FeedsModel::markItemRead(RootItem *item, RootItem::ReadStatus read) {
return item->markAsReadUnread(read); return item->markAsReadUnread(read);
} }
@ -765,10 +729,12 @@ bool FeedsModel::markItemCleared(RootItem *item, bool clean_read_only) {
return item->cleanMessages(clean_read_only); return item->cleanMessages(clean_read_only);
} }
QList<Feed*> FeedsModel::allFeeds() { QList<Feed*> FeedsModel::allFeeds() const {
return m_rootItem->getSubTreeFeeds(); return m_rootItem->getSubTreeFeeds();
} }
QList<Category*> FeedsModel::allCategories() { QList<Category*> FeedsModel::allCategories() const {
return m_rootItem->getSubTreeCategories(); return m_rootItem->getSubTreeCategories();
} }
// TODO: pokračovat s const upravami v dalsi tride

View File

@ -95,29 +95,23 @@ class FeedsModel : public QAbstractItemModel {
// Variable "auto_update_now" is true, when global timeout // Variable "auto_update_now" is true, when global timeout
// for scheduled auto-update was met and global auto-update strategy is enabled // for scheduled auto-update was met and global auto-update strategy is enabled
// so feeds with "default" auto-update strategy should be updated. // so feeds with "default" auto-update strategy should be updated.
//
// This method might change some properties of some feeds.
QList<Feed*> feedsForScheduledUpdate(bool auto_update_now); QList<Feed*> feedsForScheduledUpdate(bool auto_update_now);
// Returns (undeleted) messages for given feeds. // Returns (undeleted) messages for given feeds.
// This is usually used for displaying whole feeds // This is usually used for displaying whole feeds
// in "newspaper" mode. // in "newspaper" mode.
QList<Message> messagesForItem(RootItem *item); QList<Message> messagesForItem(RootItem *item) const;
// Returns list of all categories contained in the model. // Returns list of all categories contained in the model.
QList<Category*> allCategories(); QList<Category*> allCategories() const;
// Returns list of all feeds contained in the model. // Returns list of all feeds contained in the model.
QList<Feed*> allFeeds(); QList<Feed*> allFeeds() const;
// Returns ALL RECURSIVE CHILD feeds contained within single index. // Returns ALL RECURSIVE CHILD feeds contained within single index.
QList<Feed*> feedsForIndex(const QModelIndex &index); QList<Feed*> feedsForIndex(const QModelIndex &index) const;
// 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;
// Returns feed/category which lies at the specified index or // Returns feed/category which lies at the specified index or
// root item if index is invalid. // root item if index is invalid.
@ -130,7 +124,7 @@ class FeedsModel : public QAbstractItemModel {
QModelIndex indexForItem(RootItem *item) const; QModelIndex indexForItem(RootItem *item) const;
// Determines if any feed has any new messages. // Determines if any feed has any new messages.
bool hasAnyFeedNewMessages(); bool hasAnyFeedNewMessages() const;
// Access to root item. // Access to root item.
inline RootItem *rootItem() const { inline RootItem *rootItem() const {
@ -161,8 +155,10 @@ class FeedsModel : public QAbstractItemModel {
// If it is, then it reassigns original_node to new parent. // If it is, then it reassigns original_node to new parent.
void reassignNodeToNewParent(RootItem *original_node, RootItem *new_parent); void reassignNodeToNewParent(RootItem *original_node, RootItem *new_parent);
// Removes given item from the model/memory.
void removeItem(RootItem *deleting_item); void removeItem(RootItem *deleting_item);
// Recycle bins operations.
bool restoreAllBins(); bool restoreAllBins();
bool emptyAllBins(); bool emptyAllBins();
@ -187,15 +183,15 @@ class FeedsModel : public QAbstractItemModel {
void notifyWithCounts(); void notifyWithCounts();
private slots: private slots:
void onItemDataChanged(QList<RootItem*> items); void onItemDataChanged(const QList<RootItem*> &items);
// Is executed when next auto-update round could be done. // Is executed when next auto-update round could be done.
void executeNextAutoUpdate(); void executeNextAutoUpdate();
// Reacts on feed updates. // Reacts on feed updates.
void onFeedUpdatesStarted(); void onFeedUpdatesStarted();
void onFeedUpdatesProgress(Feed *feed, int current, int total); void onFeedUpdatesProgress(const Feed *feed, int current, int total);
void onFeedUpdatesFinished(FeedDownloadResults results); void onFeedUpdatesFinished(const FeedDownloadResults &results);
signals: signals:
// Update of feeds is finished. // Update of feeds is finished.
@ -226,10 +222,6 @@ class FeedsModel : public QAbstractItemModel {
void requireItemValidationAfterDragDrop(const QModelIndex &source_index); void requireItemValidationAfterDragDrop(const QModelIndex &source_index);
private: private:
// Returns converted ids of given feeds
// which are suitable as IN clause for SQL queries.
QStringList textualFeedIds(const QList<Feed*> &feeds);
RootItem *m_rootItem; RootItem *m_rootItem;
QList<QString> m_headerData; QList<QString> m_headerData;
QList<QString> m_tooltipData; QList<QString> m_tooltipData;

View File

@ -215,15 +215,15 @@ int RootItem::countOfAllMessages() const {
return total_count; return total_count;
} }
bool RootItem::isChildOf(RootItem *root) { bool RootItem::isChildOf(const RootItem *root) const {
if (root == NULL) { if (root == NULL) {
return false; return false;
} }
RootItem *this_item = this; const RootItem *this_item = this;
while (this_item->kind() != RootItemKind::Root) { while (this_item->kind() != RootItemKind::Root) {
if (root->childItems().contains(this_item)) { if (root->childItems().contains(const_cast<RootItem* const>(this_item))) {
return true; return true;
} }
else { else {
@ -234,7 +234,7 @@ bool RootItem::isChildOf(RootItem *root) {
return false; return false;
} }
bool RootItem::isParentOf(RootItem *child) { bool RootItem::isParentOf(const RootItem *child) const {
if (child == NULL) { if (child == NULL) {
return false; return false;
} }
@ -243,11 +243,11 @@ bool RootItem::isParentOf(RootItem *child) {
} }
} }
QList<RootItem*> RootItem::getSubTree() { QList<RootItem*> RootItem::getSubTree() const {
QList<RootItem*> children; QList<RootItem*> children;
QList<RootItem*> traversable_items; QList<RootItem*> traversable_items;
traversable_items.append(this); traversable_items.append(const_cast<RootItem* const>(this));
// Iterate all nested items. // Iterate all nested items.
while (!traversable_items.isEmpty()) { while (!traversable_items.isEmpty()) {
@ -260,11 +260,11 @@ QList<RootItem*> RootItem::getSubTree() {
return children; return children;
} }
QList<RootItem*> RootItem::getSubTree(RootItemKind::Kind kind_of_item) { QList<RootItem*> RootItem::getSubTree(RootItemKind::Kind kind_of_item) const {
QList<RootItem*> children; QList<RootItem*> children;
QList<RootItem*> traversable_items; QList<RootItem*> traversable_items;
traversable_items.append(this); traversable_items.append(const_cast<RootItem* const>(this));
// Iterate all nested items. // Iterate all nested items.
while (!traversable_items.isEmpty()) { while (!traversable_items.isEmpty()) {
@ -280,11 +280,11 @@ QList<RootItem*> RootItem::getSubTree(RootItemKind::Kind kind_of_item) {
return children; return children;
} }
QList<Category*> RootItem::getSubTreeCategories() { QList<Category*> RootItem::getSubTreeCategories() const {
QList<Category*> children; QList<Category*> children;
QList<RootItem*> traversable_items; QList<RootItem*> traversable_items;
traversable_items.append(this); traversable_items.append(const_cast<RootItem* const>(this));
// Iterate all nested items. // Iterate all nested items.
while (!traversable_items.isEmpty()) { while (!traversable_items.isEmpty()) {
@ -300,11 +300,11 @@ QList<Category*> RootItem::getSubTreeCategories() {
return children; return children;
} }
QHash<int,Category*> RootItem::getHashedSubTreeCategories() { QHash<int,Category*> RootItem::getHashedSubTreeCategories() const {
QHash<int,Category*> children; QHash<int,Category*> children;
QList<RootItem*> traversable_items; QList<RootItem*> traversable_items;
traversable_items.append(this); traversable_items.append(const_cast<RootItem* const>(this));
// Iterate all nested items. // Iterate all nested items.
while (!traversable_items.isEmpty()) { while (!traversable_items.isEmpty()) {
@ -320,11 +320,11 @@ QHash<int,Category*> RootItem::getHashedSubTreeCategories() {
return children; return children;
} }
QList<Feed*> RootItem::getSubTreeFeeds() { QList<Feed*> RootItem::getSubTreeFeeds() const {
QList<Feed*> children; QList<Feed*> children;
QList<RootItem*> traversable_items; QList<RootItem*> traversable_items;
traversable_items.append(this); traversable_items.append(const_cast<RootItem* const>(this));
// Iterate all nested items. // Iterate all nested items.
while (!traversable_items.isEmpty()) { while (!traversable_items.isEmpty()) {

View File

@ -171,18 +171,18 @@ class RootItem : public QObject {
// Checks whether "this" object is child (direct or indirect) // Checks whether "this" object is child (direct or indirect)
// of the given root. // of the given root.
bool isChildOf(RootItem *root); bool isChildOf(const RootItem *root) const;
// Is "this" item parent (direct or indirect) if given child? // 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. // Returns flat list of all items from subtree where this item is a root.
// Returned list includes this item too. // Returned list includes this item too.
QList<RootItem*> getSubTree(); QList<RootItem*> getSubTree() const;
QList<RootItem*> getSubTree(RootItemKind::Kind kind_of_item); QList<RootItem*> getSubTree(RootItemKind::Kind kind_of_item) const;
QList<Category*> getSubTreeCategories(); QList<Category*> getSubTreeCategories() const;
QHash<int,Category*> getHashedSubTreeCategories(); QHash<int,Category*> getHashedSubTreeCategories() const;
QList<Feed*> getSubTreeFeeds(); QList<Feed*> getSubTreeFeeds() const;
// Returns the service root node which is direct or indirect parent of current item. // Returns the service root node which is direct or indirect parent of current item.
ServiceRoot *getParentServiceRoot(); ServiceRoot *getParentServiceRoot();