summaryrefslogtreecommitdiffstats
path: root/common/modelresult.h
blob: c23c41e4a5a034151fb54a77f2efd2d7cc54df9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * Copyright (C) 2014 Christian Mollekopf <chrigi_1@fastmail.fm>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3, or any
 * later version accepted by the membership of KDE e.V. (or its
 * successor approved by the membership of KDE e.V.), which shall
 * act as a proxy defined in Section 6 of version 3 of the license.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include <QAbstractItemModel>
#include <QModelIndex>
#include <QDebug>
#include "query.h"
#include "clientapi.h"

#include "resultprovider.h"

template<class T>
class ModelResult : public QAbstractItemModel
{
public:

    enum Roles {
        DomainObjectRole = Qt::UserRole + 1
    };

    ModelResult(const Akonadi2::Query &query, const QList<QByteArray> &propertyColumns)
        :QAbstractItemModel(),
        mPropertyColumns(propertyColumns)
    {
    }

    static qint64 getIdentifier(const QModelIndex &idx)
    {
        if (!idx.isValid()) {
            return 0;
        }
        return idx.internalId();
    }

    int rowCount(const QModelIndex &parent = QModelIndex()) const
    {
        return mTree[getIdentifier(parent)].size();
    }

    int columnCount(const QModelIndex &parent = QModelIndex()) const
    {
        return mPropertyColumns.size();
    }

    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
    {
        if (role == DomainObjectRole) {
            qWarning() << "trying to get entity " << index.internalId();
            Q_ASSERT(mEntities.contains(index.internalId()));
            return QVariant::fromValue(mEntities.value(index.internalId()));
        }
        qDebug() << "Invalid role";
        return QVariant();
    }

    QModelIndex index(int row, int column, const QModelIndex & parent = QModelIndex()) const
    {
        auto id = getIdentifier(parent);
        auto childId = mTree.value(id).at(row);
        return createIndex(row, column, childId);
    }

    QModelIndex parent(const QModelIndex &index) const
    {
        auto id = getIdentifier(index);
        auto parentId = mParents.value(id);
        auto grandParentId = mParents.value(parentId, 0);
        auto row = mTree.value(grandParentId).indexOf(parentId);
        return createIndex(row, 0, parentId);
    }

    bool canFetchMore(const QModelIndex &parent) const
    {
        return mEntityChildrenFetched.value(parent.internalId());
    }

    void fetchMore(const QModelIndex &parent)
    {
        fetchEntities(parent);
    }

    void fetchEntities(const QModelIndex &parent)
    {
        qDebug() << "Fetching entities";
        const auto id = getIdentifier(parent);
        // beginResetModel();
        // mEntities.remove(id);
        mEntityChildrenFetched[id] = true;
        auto query = mQuery;
        if (!parent.isValid()) {
            qDebug() << "no parent";
            query.propertyFilter.insert("parent", QByteArray());
        } else {
            qDebug() << "parent is valid";
            auto object = parent.data(DomainObjectRole).template value<typename T::Ptr>();
            Q_ASSERT(object);
            query.propertyFilter.insert("parent", object->identifier());
        }
        auto emitter = Akonadi2::Store::load<T>(query);
        emitter->onAdded([this, id, parent](const typename T::Ptr &value) {
            auto childId = qHash(value->identifier());
            qDebug() << "Added entity " << childId;
            const auto keys = mTree[id];
            int index = 0;
            for (; index < keys.size(); index++) {
                if (childId < keys.at(index)) {
                    break;
                }
            }
            beginInsertRows(parent, index, index);
            mEntities.insert(childId, value);
            mTree[id].insert(index, childId);
            mParents.insert(childId, id);
            endInsertRows();
        });
        emitter->onModified([this, id, parent](const typename T::Ptr &value) {
            auto childId = qHash(value->identifier());
            qDebug() << "Modified entity" << childId;
            auto i = mTree[id].indexOf(childId);
            mEntities.remove(childId);
            mEntities.insert(childId, value);
            //TODO check for change of parents
            auto idx = index(i, 0, parent);
            emit dataChanged(idx, idx);
        });
        emitter->onRemoved([this, id, parent](const typename T::Ptr &value) {
            auto childId = qHash(value->identifier());
            qDebug() << "Removed entity" << childId;
            auto index = mTree[id].indexOf(qHash(value->identifier()));
            beginRemoveRows(parent, index, index);
            mEntities.remove(childId);
            mTree[id].removeAll(childId);
            mParents.remove(childId);
            //TODO remove children
            endRemoveRows();
        });
        emitter->onInitialResultSetComplete([this]() {
        });
        emitter->onComplete([this, id]() {
            mEmitter[id].clear();
        });
        emitter->onClear([this]() {
            // beginResetModel();
            // mEntities.clear();
            // endResetModel();
        });
        mEmitter.insert(id, emitter);
        // endResetModel();
    }

private:
    QMap<qint64 /* parent entity id */, QSharedPointer<Akonadi2::ResultEmitter<typename T::Ptr> >> mEmitter;
    //TODO we should be able to directly use T as index, with an appropriate hash function, and thus have a QMap<T, T> and QList<T>
    QMap<qint64 /* entity id */, typename T::Ptr> mEntities;
    QMap<qint64 /* parent entity id */, QList<qint64> /* child entity id*/> mTree;
    QMap<qint64 /* child entity id */, qint64 /* parent entity id*/> mParents;
    QMap<qint64 /* entity id */, bool> mEntityChildrenFetched;
    QList<QByteArray> mPropertyColumns;
    Akonadi2::Query mQuery;
};