summaryrefslogtreecommitdiffstats
path: root/common/modelresult.h
blob: 8ca6daa373caa7cafd586fc6a39014fcf7bd3503 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * 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 "resultprovider.h"

template<class T, class Ptr>
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 createIndexFromId(const qint64 &id) const
    {
        auto grandParentId = mParents.value(id, 0);
        auto row = mTree.value(grandParentId).indexOf(id);
        return createIndex(row, 0, id);
    }

    QModelIndex parent(const QModelIndex &index) const
    {
        auto id = getIdentifier(index);
        auto parentId = mParents.value(id);
        return createIndexFromId(parentId);
    }

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

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

    qint64 parentId(const Ptr &value)
    {
        return qHash(value->getProperty("parent").toByteArray());
    }

    void add(const Ptr &value)
    {
        auto childId = qHash(value->identifier());
        auto id = parentId(value);
        //Ignore updates we get before the initial fetch is done
        if (!mEntityChildrenFetched[id]) {
            return;
        }
        auto parent = createIndexFromId(id);
        qDebug() << "Added entity " << childId << value->identifier();
        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();
    }

    void modify(const Ptr &value)
    {
        auto childId = qHash(value->identifier());
        auto id = parentId(value);
        //Ignore updates we get before the initial fetch is done
        if (!mEntityChildrenFetched[id]) {
            return;
        }
        auto parent = createIndexFromId(id);
        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);
    }

    void remove(const Ptr &value)
    {
        auto childId = qHash(value->identifier());
        auto id = parentId(value);
        auto parent = createIndexFromId(id);
        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();
    }

    void fetchEntities(const QModelIndex &parent)
    {
        const auto id = getIdentifier(parent);
        mEntityChildrenFetched[id] = true;
        QByteArray parentIdentifier;
        if (!parent.isValid()) {
            qDebug() << "no parent";
        } else {
            qDebug() << "parent is valid";
            auto object = parent.data(DomainObjectRole).template value<Ptr>();
            Q_ASSERT(object);
            parentIdentifier = object->identifier();
        }
        Trace() << "Loading child entities of: " << parentIdentifier;
        loadEntities(parentIdentifier);
    }

    void setFetcher(const std::function<void(const QByteArray &parent)> &fetcher)
    {
        Trace() << "Setting fetcher";
        loadEntities = fetcher;
    }

private:
    //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 */, 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;
    std::function<void(const QByteArray &)> loadEntities;
};