From 01a41de45b5878bdec746d6869ade74cb3aafb69 Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Fri, 17 Jan 2014 12:47:37 +0100 Subject: [PATCH] Some feed deleting. --- src/core/feedsmodel.cpp | 7 ++----- src/core/feedsmodelcategory.h | 10 --------- src/core/feedsmodelrootitem.h | 1 + src/core/feedsmodelstandardcategory.cpp | 27 +++++++++++++++++++++++++ src/core/feedsmodelstandardcategory.h | 4 ++++ src/core/feedsmodelstandardfeed.cpp | 21 ++++++++++++++++--- src/gui/feedsview.cpp | 5 +++-- 7 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/core/feedsmodel.cpp b/src/core/feedsmodel.cpp index 399f410b9..f01cad2b7 100644 --- a/src/core/feedsmodel.cpp +++ b/src/core/feedsmodel.cpp @@ -159,17 +159,14 @@ bool FeedsModel::removeItems(const QModelIndexList &indexes) { FeedsModelRootItem *parent_item = itemForIndex(parent_index); beginRemoveRows(parent_index, index.row(), index.row()); - if (parent_item->removeChild(index.row())) { + if (item->removeItself() || parent_item->removeChild(index.row())) { items_for_deletion << item; } endRemoveRows(); } } - // Free deleted items from the memory. - while (!items_for_deletion.isEmpty()) { - delete items_for_deletion.takeFirst(); - } + qDeleteAll(items_for_deletion); return true; } diff --git a/src/core/feedsmodelcategory.h b/src/core/feedsmodelcategory.h index df926fd98..74f145d81 100755 --- a/src/core/feedsmodelcategory.h +++ b/src/core/feedsmodelcategory.h @@ -24,16 +24,6 @@ class FeedsModelCategory : public FeedsModelRootItem { explicit FeedsModelCategory(const FeedsModelCategory &other); virtual ~FeedsModelCategory(); - inline bool removeItself() { - bool result = true; - - foreach (FeedsModelRootItem *child, m_childItems) { - result &= child->removeItself(); - } - - return result; - } - // All types of categories offer these getters/setters. inline Type type() const { return m_type; diff --git a/src/core/feedsmodelrootitem.h b/src/core/feedsmodelrootitem.h index 5cedbf338..315199a4f 100755 --- a/src/core/feedsmodelrootitem.h +++ b/src/core/feedsmodelrootitem.h @@ -62,6 +62,7 @@ class FeedsModelRootItem { // This typically removes item and its // "children" (for example messages or child feeds) // from the database. + // Returns true if "I" was removed. virtual bool removeItself() { return false; } diff --git a/src/core/feedsmodelstandardcategory.cpp b/src/core/feedsmodelstandardcategory.cpp index 6eb3a024d..8eb1ada82 100755 --- a/src/core/feedsmodelstandardcategory.cpp +++ b/src/core/feedsmodelstandardcategory.cpp @@ -1,11 +1,13 @@ #include "core/feedsmodelstandardcategory.h" #include "core/defs.h" +#include "core/databasefactory.h" #include "core/textfactory.h" #include "gui/iconthemefactory.h" #include "gui/iconfactory.h" #include +#include FeedsModelStandardCategory::FeedsModelStandardCategory(FeedsModelRootItem *parent_item) @@ -89,6 +91,31 @@ QVariant FeedsModelStandardCategory::data(int column, int role) const { } } +bool FeedsModelStandardCategory::removeItself() { + bool result = true; + + // Remove all child items (feeds, categories.) + foreach (FeedsModelRootItem *child, m_childItems) { + result &= child->removeItself(); + } + + if (!result) { + return result; + } + + // Children are removed, remove this standard category too. + QSqlDatabase database = DatabaseFactory::instance()->connection(); + QSqlQuery query_remove(database); + + query_remove.setForwardOnly(true); + + // Remove all messages from this standard feed. + query_remove.prepare("DELETE FROM Categories WHERE id = :category;"); + query_remove.bindValue(":category", id()); + + return query_remove.exec(); +} + FeedsModelStandardCategory *FeedsModelStandardCategory::loadFromRecord(const QSqlRecord &record) { FeedsModelStandardCategory *category = new FeedsModelStandardCategory(NULL); diff --git a/src/core/feedsmodelstandardcategory.h b/src/core/feedsmodelstandardcategory.h index 1ad437989..2c0e9b16d 100644 --- a/src/core/feedsmodelstandardcategory.h +++ b/src/core/feedsmodelstandardcategory.h @@ -21,6 +21,10 @@ class FeedsModelStandardCategory : public FeedsModelCategory { // Returns the actual data representation of standard category. QVariant data(int column, int role) const; + // Removes category and all its children from persistent + // database. + bool removeItself(); + // Loads particular "standard category" from given sql record. static FeedsModelStandardCategory *loadFromRecord(const QSqlRecord &record); }; diff --git a/src/core/feedsmodelstandardfeed.cpp b/src/core/feedsmodelstandardfeed.cpp index 6866bc9d2..beb8d154e 100755 --- a/src/core/feedsmodelstandardfeed.cpp +++ b/src/core/feedsmodelstandardfeed.cpp @@ -163,9 +163,24 @@ void FeedsModelStandardFeed::update() { } bool FeedsModelStandardFeed::removeItself() { - // TODO: pokracovat, vymazat tento standardni - // kanal z database a smazat jeho zpravy atp. - return false; + QSqlDatabase database = DatabaseFactory::instance()->connection(); + QSqlQuery query_remove(database); + + query_remove.setForwardOnly(true); + + // Remove all messages from this standard feed. + query_remove.prepare("DELETE FROM Messages WHERE feed = :feed;"); + query_remove.bindValue(":feed", id()); + + if (!query_remove.exec()) { + return false; + } + + // Remove feed itself. + query_remove.prepare("DELETE FROM Feeds WHERE id = :feed;"); + query_remove.bindValue(":feed", id()); + + return query_remove.exec(); } void FeedsModelStandardFeed::updateMessages(const QList &messages) { diff --git a/src/gui/feedsview.cpp b/src/gui/feedsview.cpp index d20482f1e..e85153329 100644 --- a/src/gui/feedsview.cpp +++ b/src/gui/feedsview.cpp @@ -107,10 +107,11 @@ void FeedsView::deleteSelectedItems() { QModelIndexList selection = selectionModel()->selectedRows(); QModelIndexList mapped_selection = m_proxyModel->mapListToSource(selection); - FeedsModelRootItem *parent = m_sourceModel->itemForIndex(mapped_selection.at(0).parent()); + //FeedsModelRootItem *parent = m_sourceModel->itemForIndex(mapped_selection.at(0).parent()); m_sourceModel->removeItems(mapped_selection); + /* QModelIndex id = m_sourceModel->indexForItem(parent); if (id.isValid()) { @@ -118,7 +119,7 @@ void FeedsView::deleteSelectedItems() { selectionModel()->select(m_proxyModel->mapFromSource(id), QItemSelectionModel::Rows | QItemSelectionModel::Select); - } + }*/ } void FeedsView::markSelectedFeedsReadStatus(int read) {