abstractnotesprovider.h 4.62 KB
Newer Older
1 2
#ifndef ABSTRACTNOTESPROVIDER_H
#define ABSTRACTNOTESPROVIDER_H
3 4

#include <QObject>
5
#include <functional>
6 7 8 9 10 11 12 13 14 15 16
#ifdef STATIC_MAUIKIT
#include "fmh.h"
#else
#include <MauiKit/fmh.h>
#endif
/**
 * @brief The AbstractNoteSyncer class
 * is an abstraction for different services backend to sync notes.
 * Different services to be added to Buho are expected to derived from this.
 */

17
class AbstractNotesProvider : public QObject
18 19 20 21
{
    Q_OBJECT

public:
22 23
    AbstractNotesProvider(QObject *parent) : QObject(parent) {}
    virtual ~AbstractNotesProvider() {}
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
    /**
     * @brief setCredentials
     * sets the credential to authenticate to the provider server
     * @param account
     * the account data is represented by FMH::MODEL
     */
    virtual void setCredentials(const FMH::MODEL &account) final
    {
        this->m_user = account[FMH::MODEL_KEY::USER];
        this->m_password = account[FMH::MODEL_KEY::PASSWORD];
        this->m_provider = QUrl(account[FMH::MODEL_KEY::SERVER]).host();
    }

    virtual QString user() final { return this->m_user; }
    virtual QString provider() final { return this->m_provider; }

    /**
     * @brief isValid
     * check if the account acredentials are valid
     * by checking they are not empty or null
     * @return
     * true if the credentials are all set or false is somethign is missing
     */
    virtual bool isValid()
49
    {
50 51 52
        return !(this->m_user.isEmpty() || this->m_user.isNull()
                 || this->m_provider.isEmpty() || this->m_provider.isNull()
                 || this->m_password.isEmpty() || this->m_password.isNull());
53 54 55 56 57 58 59 60 61
    }

    /**
     * @brief getNote
     * gets a note identified by an ID
     * @param id
     * When the process is done it shoudl emit the noteReady(FMH::MODEL) signal
     */
//    virtual FMH::MODEL getNote(const QString &id) = 0;
62
    virtual void getNote(const QString &id) = 0;
63 64 65 66 67 68

    /**
     * @brief getNotes
     * returns all the notes or queried notes
     *  When the process is done it shoudl emit the notesReady(FMH::MODEL_LIST) signal
     */
69
    virtual void getNotes() = 0;
70
//    virtual void getNotes() const {}
71 72 73 74 75 76 77 78 79 80 81 82
//    virtual FMH::MODEL_LIST getNotes(const QString &query = QString()) = 0;
//    virtual FMH::MODEL_LIST getNotes(const QString &query = QString()) const = 0;


    /**
     * @brief insertNote
     * inserts a new note to the server
     * @param note
     * takes the new note to be inserted represented as FMH::MODEL
     * When the process is done it shoudl emit the noteInserted(FMH::MODEL) signal
     */
//    virtual bool insertNote(const FMH::MODEL &note) = 0;
83
    virtual void insertNote(const FMH::MODEL &note) = 0;
84 85 86 87 88 89 90 91 92 93 94

    /**
     * @brief updateNote
     * allows to update a note in the server, it takes an ID and the updated note
     * @param id
     * id of the note to be updated
     * @param note
     * the note prepresented as FMH::MODEL contening the up-to-date values
     * When the process is done it shoudl emit the noteUpdated(FMH::MODEL) signal
     */
//    virtual bool updateNote(const QString &id, const FMH::MODEL &note) = 0;
95
    virtual void updateNote(const QString &id, const FMH::MODEL &note) = 0;
96 97 98 99 100 101 102 103 104

    /**
     * @brief removeNote
     * removes a note from the server
     * @param id
     * ID of the note to be removed
     * When the process is done it shoudl emit the noteRemoved(FMH::MODEL) signal
     */
//    virtual bool removeNote(const QString &id) = 0;
105
    virtual void removeNote(const QString &id) = 0;
106 107 108 109 110 111 112

protected:
    QString m_user = "";
    QString m_password = "";
    QString m_provider = "";

    template<typename T>
113 114
     void request(const QString &url, const QMap<QString, QString> &header, T cb)
//    inline void request(const QString &url, const QMap<QString, QString> &header, std::function<void (QByteArray)>cb)
115 116 117 118
    {
        auto downloader = new FMH::Downloader;
        connect(downloader, &FMH::Downloader::dataReady, [&, downloader = std::move(downloader)](const QByteArray &array)
        {
119
//            if(cb != nullptr)
120 121 122 123 124 125 126 127
                cb(array);
            downloader->deleteLater();
        });

        downloader->getArray(url, header);
    }

signals:
128 129 130 131
    void noteReady(FMH::MODEL note);
    void notesReady(FMH::MODEL_LIST notes);
    void noteInserted(FMH::MODEL note);
    void noteUpdated(FMH::MODEL note);
camilo higuita's avatar
camilo higuita committed
132
    void noteRemoved();
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

    /**
     * @brief responseReady
     * gets emitted when the data is ready after requesting the array
     * with &Downloader::getArray()
     */
    void responseReady(QByteArray array);

    /**
     * @brief responseError
     * emitted if there's an error when trying to get the array
     */
    void responseError(QString);

};


150
#endif // ABSTRACTNOTESPROVIDER_H
151 152