summaryrefslogtreecommitdiffstats
path: root/common/messagequeue.h
blob: b7e3daa772933a09d7c35e28fd4cfa59d97788c3 (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
#pragma once

#include <QObject>
#include <string>
#include <functional>
#include <QString>
#include "storage.h"

/**
 * A persistent FIFO message queue.
 */
class MessageQueue : public QObject
{
    Q_OBJECT
public:
    class Error
    {
    public:
        Error(const QByteArray &s, int c, const QByteArray &m)
            : store(s), message(m), code(c) {}
        QByteArray store;
        QByteArray message;
        int code;
    };

    MessageQueue(const QString &storageRoot, const QString &name);

    void enqueue(void const *msg, size_t size);
    //Dequeue a message. This will return a new message everytime called.
    //Call the result handler with a success response to remove the message from the store.
    //TODO track processing progress to avoid processing the same message with the same preprocessor twice?
    void dequeue(const std::function<void(void *ptr, int size, std::function<void(bool success)>)> & resultHandler,
              const std::function<void(const Error &error)> &errorHandler);
    bool isEmpty();
signals:
    void messageReady();
    void drained();

private:
    Q_DISABLE_COPY(MessageQueue);
    Akonadi2::Storage mStorage;
};