1 | /* |
2 | Copyright (c) 2011 Christian Mollekopf <chrigi_1@fastmail.fm> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | #include "trashfilterproxymodel.h" |
20 | |
21 | #include <akonadi/entity.h> |
22 | #include <akonadi/entitydeletedattribute.h> |
23 | #include <akonadi/item.h> |
24 | #include <akonadi/entitytreemodel.h> |
25 | #include <akonadi/resourcesettings.h> |
26 | |
27 | using namespace Akonadi; |
28 | |
29 | class TrashFilterProxyModel::TrashFilterProxyModelPrivate |
30 | { |
31 | public: |
32 | TrashFilterProxyModelPrivate() |
33 | : mTrashIsShown(false) |
34 | { |
35 | }; |
36 | bool showEntity(const Entity &) const; |
37 | bool mTrashIsShown; |
38 | }; |
39 | |
40 | bool TrashFilterProxyModel::TrashFilterProxyModelPrivate::showEntity(const Akonadi::Entity &entity) const |
41 | { |
42 | if (entity.hasAttribute<EntityDeletedAttribute>()) { |
43 | return true; |
44 | } |
45 | if (entity.id() == Collection::root().id()) { |
46 | return false; |
47 | } |
48 | return showEntity(entity.parentCollection()); |
49 | } |
50 | |
51 | TrashFilterProxyModel::TrashFilterProxyModel(QObject *parent) |
52 | : KRecursiveFilterProxyModel(parent) |
53 | , d_ptr(new TrashFilterProxyModelPrivate()) |
54 | { |
55 | |
56 | } |
57 | |
58 | TrashFilterProxyModel::~TrashFilterProxyModel() |
59 | { |
60 | delete d_ptr; |
61 | } |
62 | |
63 | void TrashFilterProxyModel::showTrash(bool enable) |
64 | { |
65 | Q_D(TrashFilterProxyModel); |
66 | d->mTrashIsShown = enable; |
67 | invalidateFilter(); |
68 | } |
69 | |
70 | bool TrashFilterProxyModel::trashIsShown() const |
71 | { |
72 | Q_D(const TrashFilterProxyModel); |
73 | return d->mTrashIsShown; |
74 | } |
75 | |
76 | bool TrashFilterProxyModel::acceptRow(int sourceRow, const QModelIndex &sourceParent) const |
77 | { |
78 | Q_D(const TrashFilterProxyModel); |
79 | const QModelIndex &index = sourceModel()->index(sourceRow, 0, sourceParent); |
80 | const Item &item = index.data(EntityTreeModel::ItemRole).value<Item>(); |
81 | //kDebug() << item.id(); |
82 | if (item.isValid()) { |
83 | if (item.hasAttribute<EntityDeletedAttribute>()/* d->showEntity(item)*/) { |
84 | return d->mTrashIsShown; |
85 | } |
86 | } |
87 | const Collection &collection = index.data(EntityTreeModel::CollectionRole).value<Collection>(); |
88 | if (collection.isValid()) { |
89 | if (collection.hasAttribute<EntityDeletedAttribute>()/*d->showEntity(collection) */) { |
90 | return d->mTrashIsShown; |
91 | } |
92 | } |
93 | return !d->mTrashIsShown; |
94 | } |
95 | |