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();
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());
@ -54,6 +54,8 @@ void FeedDownloader::updateFeeds(const QList<Feed*> &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<Feed*> &feeds) {
FeedDownloadResults::FeedDownloadResults() : m_updatedFeeds(QList<QPair<QString,int> >()) {
}
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<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) {
return lhs.second > rhs.second;
}
QList<QPair<QString,int> > &FeedDownloadResults::updatedFeeds() {
QList<QPair<QString,int> > FeedDownloadResults::updatedFeeds() const {
return m_updatedFeeds;
}

View File

@ -30,8 +30,11 @@ class FeedDownloadResults {
public:
explicit FeedDownloadResults();
QList<QPair<QString,int> > &updatedFeeds();
QString overview(int how_many_feeds);
QList<QPair<QString,int> > updatedFeeds() const;
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);

View File

@ -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<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();
@ -559,7 +556,7 @@ QList<Feed*> FeedsModel::feedsForScheduledUpdate(bool auto_update_now) {
return feeds_for_update;
}
QList<Message> FeedsModel::messagesForItem(RootItem *item) {
QList<Message> 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<RootItem*> items) {
void FeedsModel::onItemDataChanged(const QList<RootItem *> &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<RootItem*> items) {
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() {
emit layoutAboutToBeChanged();
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();
}
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<Feed*> FeedsModel::allFeeds() {
QList<Feed*> FeedsModel::allFeeds() const {
return m_rootItem->getSubTreeFeeds();
}
QList<Category*> FeedsModel::allCategories() {
QList<Category*> FeedsModel::allCategories() const {
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
// 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<Feed*> feedsForScheduledUpdate(bool auto_update_now);
// Returns (undeleted) messages for given feeds.
// This is usually used for displaying whole feeds
// in "newspaper" mode.
QList<Message> messagesForItem(RootItem *item);
QList<Message> messagesForItem(RootItem *item) const;
// 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.
QList<Feed*> allFeeds();
QList<Feed*> allFeeds() const;
// Returns ALL RECURSIVE CHILD feeds contained within single index.
QList<Feed*> 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<Feed*> 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<RootItem*> items);
void onItemDataChanged(const QList<RootItem*> &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<Feed*> &feeds);
RootItem *m_rootItem;
QList<QString> m_headerData;
QList<QString> m_tooltipData;

View File

@ -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<RootItem* const>(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*> RootItem::getSubTree() {
QList<RootItem*> RootItem::getSubTree() const {
QList<RootItem*> children;
QList<RootItem*> traversable_items;
traversable_items.append(this);
traversable_items.append(const_cast<RootItem* const>(this));
// Iterate all nested items.
while (!traversable_items.isEmpty()) {
@ -260,11 +260,11 @@ QList<RootItem*> RootItem::getSubTree() {
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*> traversable_items;
traversable_items.append(this);
traversable_items.append(const_cast<RootItem* const>(this));
// Iterate all nested items.
while (!traversable_items.isEmpty()) {
@ -280,11 +280,11 @@ QList<RootItem*> RootItem::getSubTree(RootItemKind::Kind kind_of_item) {
return children;
}
QList<Category*> RootItem::getSubTreeCategories() {
QList<Category*> RootItem::getSubTreeCategories() const {
QList<Category*> children;
QList<RootItem*> traversable_items;
traversable_items.append(this);
traversable_items.append(const_cast<RootItem* const>(this));
// Iterate all nested items.
while (!traversable_items.isEmpty()) {
@ -300,11 +300,11 @@ QList<Category*> RootItem::getSubTreeCategories() {
return children;
}
QHash<int,Category*> RootItem::getHashedSubTreeCategories() {
QHash<int,Category*> RootItem::getHashedSubTreeCategories() const {
QHash<int,Category*> children;
QList<RootItem*> traversable_items;
traversable_items.append(this);
traversable_items.append(const_cast<RootItem* const>(this));
// Iterate all nested items.
while (!traversable_items.isEmpty()) {
@ -320,11 +320,11 @@ QHash<int,Category*> RootItem::getHashedSubTreeCategories() {
return children;
}
QList<Feed*> RootItem::getSubTreeFeeds() {
QList<Feed*> RootItem::getSubTreeFeeds() const {
QList<Feed*> children;
QList<RootItem*> traversable_items;
traversable_items.append(this);
traversable_items.append(const_cast<RootItem* const>(this));
// Iterate all nested items.
while (!traversable_items.isEmpty()) {

View File

@ -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<RootItem*> getSubTree();
QList<RootItem*> getSubTree(RootItemKind::Kind kind_of_item);
QList<Category*> getSubTreeCategories();
QHash<int,Category*> getHashedSubTreeCategories();
QList<Feed*> getSubTreeFeeds();
QList<RootItem*> getSubTree() const;
QList<RootItem*> getSubTree(RootItemKind::Kind kind_of_item) const;
QList<Category*> getSubTreeCategories() const;
QHash<int,Category*> getHashedSubTreeCategories() const;
QList<Feed*> getSubTreeFeeds() const;
// Returns the service root node which is direct or indirect parent of current item.
ServiceRoot *getParentServiceRoot();