summaryrefslogtreecommitdiffstats
path: root/examples/dummyresource/resourcefactory.cpp
blob: 8dae74916997e54915e92f100dc6820b35199efb (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
 *   Copyright (C) 2014 Aaron Seigo <aseigo@kde.org>
 *   Copyright (C) 2015 Christian Mollekopf <chrigi_1@fastmail.fm>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program 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 General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
 */

#include "resourcefactory.h"
#include "facade.h"
#include "entitybuffer.h"
#include "pipeline.h"
#include "dummycalendar_generated.h"
#include "mail_generated.h"
#include "queuedcommand_generated.h"
#include "createentity_generated.h"
#include "domainadaptor.h"
#include "commands.h"
#include "index.h"
#include "log.h"
#include "domain/event.h"
#include "domain/mail.h"
#include "dummystore.h"
#include "definitions.h"
#include "facadefactory.h"
#include <QDate>
#include <QUuid>

//This is the resources entity type, and not the domain type
#define ENTITY_TYPE_EVENT "event"
#define ENTITY_TYPE_MAIL "mail"
#define ENTITY_TYPE_FOLDER "folder"

/**
 * Index types:
 * * uid - property
 * 
 * * Property can be:
 *     * fixed value like uid
 *     * fixed value where we want to do smaller/greater-than comparisons. (like start date)
 *     * range indexes like what date range an event affects.
 *     * group indexes like tree hierarchies as nested sets
 */
class IndexUpdater : public Akonadi2::Preprocessor {
public:
    IndexUpdater(const QByteArray &index, const QByteArray &type)
        :mIndexIdentifier(index),
        mBufferType(type)
    {

    }

    void newEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE
    {
        add(newEntity.getProperty("remoteId"), uid, transaction);
    }

    void modifiedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE
    {
        remove(oldEntity.getProperty("remoteId"), uid, transaction);
        add(newEntity.getProperty("remoteId"), uid, transaction);
    }

    void deletedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE
    {
        remove(oldEntity.getProperty("remoteId"), uid, transaction);
    }

private:
    void add(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction)
    {
        if (value.isValid()) {
            Index(mIndexIdentifier, transaction).add(value.toByteArray(), uid);
        }
    }

    void remove(const QVariant &value, const QByteArray &uid, Akonadi2::Storage::Transaction &transaction)
    {
        //TODO hide notfound error
        Index(mIndexIdentifier, transaction).remove(value.toByteArray(), uid);
    }

    QByteArray mIndexIdentifier;
    QByteArray mBufferType;
};

template<typename DomainType>
class DefaultIndexUpdater : public Akonadi2::Preprocessor {
public:
    void newEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE
    {
        Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::index(uid, newEntity, transaction);
    }

    void modifiedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, const Akonadi2::ApplicationDomain::BufferAdaptor &newEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE
    {
        Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::removeIndex(uid, oldEntity, transaction);
        Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::index(uid, newEntity, transaction);
    }

    void deletedEntity(const QByteArray &uid, qint64 revision, const Akonadi2::ApplicationDomain::BufferAdaptor &oldEntity, Akonadi2::Storage::Transaction &transaction) Q_DECL_OVERRIDE
    {
        Akonadi2::ApplicationDomain::TypeImplementation<DomainType>::removeIndex(uid, oldEntity, transaction);
    }
};

DummyResource::DummyResource(const QByteArray &instanceIdentifier, const QSharedPointer<Akonadi2::Pipeline> &pipeline)
    : Akonadi2::GenericResource(instanceIdentifier, pipeline)
{
    {
        QVector<Akonadi2::Preprocessor*> eventPreprocessors;
        eventPreprocessors << new DefaultIndexUpdater<Akonadi2::ApplicationDomain::Event>;
        eventPreprocessors << new IndexUpdater("event.index.rid", ENTITY_TYPE_EVENT);
        mPipeline->setPreprocessors(ENTITY_TYPE_EVENT, eventPreprocessors);
        mPipeline->setAdaptorFactory(ENTITY_TYPE_EVENT, QSharedPointer<DummyEventAdaptorFactory>::create());
    }
    {
        QVector<Akonadi2::Preprocessor*> mailPreprocessors;
        mailPreprocessors << new DefaultIndexUpdater<Akonadi2::ApplicationDomain::Mail>;
        mailPreprocessors << new IndexUpdater("mail.index.rid", ENTITY_TYPE_MAIL);
        mPipeline->setPreprocessors(ENTITY_TYPE_MAIL, mailPreprocessors);
        mPipeline->setAdaptorFactory(ENTITY_TYPE_MAIL, QSharedPointer<DummyMailAdaptorFactory>::create());
    }
    {
        QVector<Akonadi2::Preprocessor*> folderPreprocessors;
        folderPreprocessors << new DefaultIndexUpdater<Akonadi2::ApplicationDomain::Folder>;
        folderPreprocessors << new IndexUpdater("folder.index.rid", ENTITY_TYPE_FOLDER);
        mPipeline->setPreprocessors(ENTITY_TYPE_FOLDER, folderPreprocessors);
        mPipeline->setAdaptorFactory(ENTITY_TYPE_FOLDER, QSharedPointer<DummyFolderAdaptorFactory>::create());
    }
}

void DummyResource::createEvent(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb)
{
    //Map the source format to the buffer format (which happens to be an exact copy here)
    auto summary = m_fbb.CreateString(data.value("summary").toString().toStdString());
    auto rid = m_fbb.CreateString(std::string(ridBuffer.constData(), ridBuffer.size()));
    auto description = m_fbb.CreateString(std::string(ridBuffer.constData(), ridBuffer.size()));
    static uint8_t rawData[100];
    auto attachment = Akonadi2::EntityBuffer::appendAsVector(m_fbb, rawData, 100);

    auto builder = DummyCalendar::DummyEventBuilder(m_fbb);
    builder.add_summary(summary);
    builder.add_remoteId(rid);
    builder.add_description(description);
    builder.add_attachment(attachment);
    auto buffer = builder.Finish();
    DummyCalendar::FinishDummyEventBuffer(m_fbb, buffer);
    Akonadi2::EntityBuffer::assembleEntityBuffer(entityFbb, 0, 0, m_fbb.GetBufferPointer(), m_fbb.GetSize(), 0, 0);
}

void DummyResource::createMail(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb)
{
    //Map the source format to the buffer format (which happens to be an exact copy here)
    auto subject = m_fbb.CreateString(data.value("subject").toString().toStdString());
    auto sender = m_fbb.CreateString(data.value("sender").toString().toStdString());
    auto senderName = m_fbb.CreateString(data.value("senderName").toString().toStdString());
    auto date = m_fbb.CreateString(data.value("date").toDate().toString().toStdString());
    auto folder = m_fbb.CreateString(std::string("inbox"));

    auto builder = Akonadi2::ApplicationDomain::Buffer::MailBuilder(m_fbb);
    builder.add_subject(subject);
    builder.add_sender(sender);
    builder.add_senderName(senderName);
    builder.add_unread(data.value("unread").toBool());
    builder.add_important(data.value("important").toBool());
    builder.add_date(date);
    builder.add_folder(folder);
    auto buffer = builder.Finish();
    Akonadi2::ApplicationDomain::Buffer::FinishMailBuffer(m_fbb, buffer);
    Akonadi2::EntityBuffer::assembleEntityBuffer(entityFbb, 0, 0, 0, 0, m_fbb.GetBufferPointer(), m_fbb.GetSize());
}

QString DummyResource::resolveRemoteId(const QString &remoteId)
{
    //Lookup local id for remote id, or insert a new pair otherwise
    Akonadi2::Storage store(Akonadi2::storageLocation(), mResourceInstanceIdentifier + ".synchronization", Akonadi2::Storage::ReadWrite);
    auto transaction = store.createTransaction(Akonadi2::Storage::ReadWrite);
    QByteArray akonadiId = Index("rid.mapping", transaction).lookup(remoteId.toLatin1());
    if (akonadiId.isEmpty()) {
        akonadiId = QUuid::createUuid().toString().toUtf8();
        Index("rid.mapping", transaction).add(remoteId.toLatin1(), akonadiId);
    }
    return akonadiId;
}

void DummyResource::createFolder(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb)
{
    //Map the source format to the buffer format (which happens to be an exact copy here)
    auto name = m_fbb.CreateString(data.value("name").toString().toStdString());
    flatbuffers::Offset<flatbuffers::String> parent;
    bool hasParent = false;
    if (!data.value("parent").toString().isEmpty()) {
        hasParent = true;
        auto akonadiId = resolveRemoteId(data.value("parent").toString());
        parent = m_fbb.CreateString(akonadiId.toStdString());
    }

    auto builder = Akonadi2::ApplicationDomain::Buffer::FolderBuilder(m_fbb);
    builder.add_name(name);
    if (hasParent) {
        builder.add_parent(parent);
    }
    auto buffer = builder.Finish();
    Akonadi2::ApplicationDomain::Buffer::FinishFolderBuffer(m_fbb, buffer);
    Akonadi2::EntityBuffer::assembleEntityBuffer(entityFbb, 0, 0, 0, 0, m_fbb.GetBufferPointer(), m_fbb.GetSize());
}

void DummyResource::synchronize(const QString &bufferType, const QMap<QString, QMap<QString, QVariant> > &data, Akonadi2::Storage::Transaction &transaction, std::function<void(const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb)> createEntity)
{
    Index uidIndex("index.uid", transaction);
    for (auto it = data.constBegin(); it != data.constEnd(); it++) {
        bool isNew = true;
        uidIndex.lookup(it.key().toLatin1(), [&](const QByteArray &value) {
            isNew = false;
        },
        [](const Index::Error &error) {
            if (error.code != Index::IndexNotAvailable) {
                Warning() << "Error in uid index: " <<  error.message;
            }
        });
        if (isNew) {
            m_fbb.Clear();

            flatbuffers::FlatBufferBuilder entityFbb;
            createEntity(it.key().toUtf8(), it.value(), entityFbb);

            auto akonadiId = resolveRemoteId(it.key());

            flatbuffers::FlatBufferBuilder fbb;
            //This is the resource type and not the domain type
            auto entityId = fbb.CreateString(akonadiId.toStdString());
            auto type = fbb.CreateString(bufferType.toStdString());
            auto delta = Akonadi2::EntityBuffer::appendAsVector(fbb, entityFbb.GetBufferPointer(), entityFbb.GetSize());
            auto location = Akonadi2::Commands::CreateCreateEntity(fbb, entityId, type, delta);
            Akonadi2::Commands::FinishCreateEntityBuffer(fbb, location);

            enqueueCommand(mSynchronizerQueue, Akonadi2::Commands::CreateEntityCommand, QByteArray::fromRawData(reinterpret_cast<char const *>(fbb.GetBufferPointer()), fbb.GetSize()));
        } else { //modification
            //TODO diff and create modification if necessary
        }
    }
    //TODO find items to remove
}

KAsync::Job<void> DummyResource::synchronizeWithSource()
{
    Log() << " Synchronizing";
    return KAsync::start<void>([this](KAsync::Future<void> &f) {
        auto transaction = Akonadi2::Storage(Akonadi2::storageLocation(), mResourceInstanceIdentifier, Akonadi2::Storage::ReadOnly).createTransaction(Akonadi2::Storage::ReadOnly);

        synchronize(ENTITY_TYPE_EVENT, DummyStore::instance().events(), transaction, [this](const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb) {
            createEvent(ridBuffer, data, entityFbb);
        });
        synchronize(ENTITY_TYPE_MAIL, DummyStore::instance().mails(), transaction, [this](const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb) {
            createMail(ridBuffer, data, entityFbb);
        });
        synchronize(ENTITY_TYPE_FOLDER, DummyStore::instance().folders(), transaction, [this](const QByteArray &ridBuffer, const QMap<QString, QVariant> &data, flatbuffers::FlatBufferBuilder &entityFbb) {
            createFolder(ridBuffer, data, entityFbb);
        });

        f.setFinished();
    });
}

void DummyResource::removeFromDisk(const QByteArray &instanceIdentifier)
{
    GenericResource::removeFromDisk(instanceIdentifier);
    Akonadi2::Storage(Akonadi2::storageLocation(), instanceIdentifier + ".event.index.uid", Akonadi2::Storage::ReadWrite).removeFromDisk();
}

DummyResourceFactory::DummyResourceFactory(QObject *parent)
    : Akonadi2::ResourceFactory(parent)
{

}

Akonadi2::Resource *DummyResourceFactory::createResource(const QByteArray &instanceIdentifier)
{
    return new DummyResource(instanceIdentifier);
}

void DummyResourceFactory::registerFacades(Akonadi2::FacadeFactory &factory)
{
    factory.registerFacade<Akonadi2::ApplicationDomain::Event, DummyResourceFacade>(PLUGIN_NAME);
    factory.registerFacade<Akonadi2::ApplicationDomain::Mail, DummyResourceMailFacade>(PLUGIN_NAME);
    factory.registerFacade<Akonadi2::ApplicationDomain::Folder, DummyResourceFolderFacade>(PLUGIN_NAME);
}