summaryrefslogtreecommitdiffstats
path: root/common/clientapi.cpp
blob: a98340ceb3373e385e8d1f8fc7d53cf37c30d9ec (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
#include "clientapi.h"
#include "resourceaccess.h"
#include "commands.h"
#include "resourcefacade.h"
#include "log.h"
#include <QtConcurrent/QtConcurrentRun>
#define ASYNCINTHREAD
#ifndef ASYNCINTHREAD
#include <QTimer>
#endif

namespace async
{
    void run(const std::function<void()> &runner) {
#ifndef ASYNCINTHREAD
        auto timer = new QTimer();
        timer->setSingleShot(true);
        QObject::connect(timer, &QTimer::timeout, [runner, timer]() {
            delete timer;
            runner();
        });
        timer->start(0);
#else
        //TODO use a job that runs in a thread?
        QtConcurrent::run(runner);
#endif
    };
} // namespace async


namespace Akonadi2
{

void Store::shutdown(const QByteArray &identifier)
{
    Trace() << "shutdown";
    ResourceAccess::connectToServer(identifier).then<void, QSharedPointer<QLocalSocket>>([identifier](const QSharedPointer<QLocalSocket> &socket, KAsync::Future<void> &future) {
        //We can't currently reuse the socket
        socket->close();
        auto resourceAccess = QSharedPointer<Akonadi2::ResourceAccess>::create(identifier);
        resourceAccess->open();
        resourceAccess->sendCommand(Akonadi2::Commands::ShutdownCommand).then<void>([&future, resourceAccess]() {
            future.setFinished();
        }).exec();
    },
    [](int, const QString &) {
        //Resource isn't started, nothing to shutdown
    }).exec().waitForFinished();
}

void Store::synchronize(const QByteArray &identifier)
{
    Trace() << "synchronize";
    auto resourceAccess = QSharedPointer<Akonadi2::ResourceAccess>::create(identifier);
    resourceAccess->open();
    resourceAccess->synchronizeResource(true, false).exec().waitForFinished();
}

} // namespace Akonadi2