File indexing completed on 2024-05-12 04:45:55

0001 #ifndef ABSTRACTNOTESPROVIDER_H
0002 #define ABSTRACTNOTESPROVIDER_H
0003 
0004 #include <QObject>
0005 #include <functional>
0006 
0007 #include <MauiKit3/FileBrowsing/downloader.h>
0008 #include <MauiKit3/Core/fmh.h>
0009 
0010 /**
0011  * @brief The AbstractNoteSyncer class
0012  * is an abstraction for different services backend to sync notes.
0013  * Different services to be added to Buho are expected to derived from this.
0014  */
0015 
0016 class AbstractNotesProvider : public QObject
0017 {
0018     Q_OBJECT
0019 
0020 public:
0021     AbstractNotesProvider(QObject *parent)
0022         : QObject(parent)
0023     {
0024     }
0025     virtual ~AbstractNotesProvider()
0026     {
0027     }
0028 
0029     /**
0030      * @brief setCredentials
0031      * sets the credential to authenticate to the provider server
0032      * @param account
0033      * the account data is represented by FMH::MODEL and must contain the fileds: USER, PASSWORD and SERVER
0034      */
0035     void setCredentials(const FMH::MODEL &account)
0036     {
0037         this->m_user = account[FMH::MODEL_KEY::USER];
0038         this->m_password = account[FMH::MODEL_KEY::PASSWORD];
0039         this->m_provider = account[FMH::MODEL_KEY::SERVER];
0040     }
0041 
0042     const QString user() const
0043     {
0044         return this->m_user;
0045     }
0046     const QString provider() const
0047     {
0048         return this->m_provider;
0049     }
0050 
0051     /**
0052      * @brief isValid
0053      * check if the account acredentials are valid
0054      * by checking they are not empty or null
0055      * @return
0056      * true if the credentials are all set or false is somethign is missing
0057      */
0058     bool isValid() const
0059     {
0060         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());
0061     }
0062 
0063     /**
0064      * @brief getNote
0065      * gets a note identified by an ID
0066      * @param id
0067      * When the process is done it shoudl emit the noteReady(FMH::MODEL) signal
0068      */
0069     //    virtual FMH::MODEL getNote(const QString &id) = 0;
0070     virtual void getNote(const QString &id) = 0;
0071     virtual void getBooklet(const QString &id) = 0;
0072 
0073     /**
0074      * @brief getNotes
0075      * returns all the notes or queried notes
0076      *  When the process is done it shoudl emit the notesReady(FMH::MODEL_LIST) signal
0077      */
0078     virtual void getNotes() = 0;
0079     virtual void getBooklets() = 0;
0080     //    virtual void getNotes() const {}
0081     //    virtual FMH::MODEL_LIST getNotes(const QString &query = QString()) = 0;
0082     //    virtual FMH::MODEL_LIST getNotes(const QString &query = QString()) const = 0;
0083 
0084     /**
0085      * @brief insertNote
0086      * inserts a new note to the server
0087      * @param note
0088      * takes the new note to be inserted represented as FMH::MODEL
0089      * When the process is done it shoudl emit the noteInserted(FMH::MODEL) signal
0090      */
0091     //    virtual bool insertNote(const FMH::MODEL &note) = 0;
0092     virtual void insertNote(const FMH::MODEL &note) = 0;
0093     virtual void insertBooklet(const FMH::MODEL &booklet) = 0;
0094 
0095     /**
0096      * @brief updateNote
0097      * allows to update a note in the server, it takes an ID and the updated note
0098      * @param id
0099      * id of the note to be updated
0100      * @param note
0101      * the note prepresented as FMH::MODEL contening the up-to-date values
0102      * When the process is done it shoudl emit the noteUpdated(FMH::MODEL) signal
0103      */
0104     //    virtual bool updateNote(const QString &id, const FMH::MODEL &note) = 0;
0105     virtual void updateNote(const QString &id, const FMH::MODEL &note) = 0;
0106     virtual void updateBooklet(const QString &id, const FMH::MODEL &booklet) = 0;
0107 
0108     /**
0109      * @brief removeNote
0110      * removes a note from the server
0111      * @param id
0112      * ID of the note to be removed
0113      * When the process is done it shoudl emit the noteRemoved(FMH::MODEL) signal
0114      */
0115     //    virtual bool removeNote(const QString &id) = 0;
0116     virtual void removeNote(const QString &id) = 0;
0117     virtual void removeBooklet(const QString &id) = 0;
0118 
0119 protected:
0120     QString m_user = "";
0121     QString m_password = "";
0122     QString m_provider = "";
0123 
0124     template<typename T> void request(const QString &url, const QMap<QString, QString> &header, T cb)
0125     //    inline void request(const QString &url, const QMap<QString, QString> &header, std::function<void (QByteArray)>cb)
0126     {
0127         auto downloader = new FMH::Downloader;
0128         connect(downloader, &FMH::Downloader::dataReady, [&, downloader = std::move(downloader)](const QByteArray &array) {
0129             //            if(cb != nullptr)
0130             cb(array);
0131             downloader->deleteLater();
0132         });
0133 
0134         downloader->getArray(url, header);
0135     }
0136 
0137 signals:
0138     void noteReady(FMH::MODEL note);
0139     void bookletReady(FMH::MODEL booklet);
0140 
0141     void notesReady(FMH::MODEL_LIST notes);
0142     void bookletsReady(FMH::MODEL_LIST booklets);
0143 
0144     void noteInserted(FMH::MODEL note);
0145     void bookletInserted(FMH::MODEL booklet);
0146 
0147     void noteUpdated(FMH::MODEL note);
0148     void bookletUpdated(FMH::MODEL booklet);
0149 
0150     void noteRemoved();
0151     void bookletRemoved();
0152 
0153     /**
0154      * @brief responseReady
0155      * gets emitted when the data is ready after requesting the array
0156      * with &Downloader::getArray()
0157      */
0158     void responseReady(QByteArray array);
0159 
0160     /**
0161      * @brief responseError
0162      * emitted if there's an error when trying to get the array
0163      */
0164     void responseError(QString);
0165 };
0166 
0167 #endif // ABSTRACTNOTESPROVIDER_H