syncer.h 8.29 KB
Newer Older
1 2 3 4
#ifndef SYNCER_H
#define SYNCER_H

#include <QObject>
5 6 7 8 9
#ifdef STATIC_MAUIKIT
#include "fmh.h"
#else
#include <MauiKit/fmh.h>
#endif
10 11 12 13 14 15 16 17 18
/**
 * @brief The Syncer class
 * This interfaces between local storage and cloud
 * Its work is to try and keep thing synced and do the background work on updating notes
 * from local to cloud and viceversa.
 * This interface should be used to handle the whol offline and online work,
 * instead of manually inserting to the db or the cloud providers
 */

19 20 21
struct STATE
{
    enum TYPE : uint
22 23 24 25 26 27
    {
        LOCAL,
        REMOTE
    };

    enum STATUS : uint
28 29 30 31 32 33
    {
        OK,
        ERROR
    };

    TYPE type;
34 35
    STATUS status;
    QString msg = QString();
36 37 38 39 40 41
};

class DB;
class AbstractNotesProvider;
class Tagging;
class Syncer: public QObject
42 43
{
    Q_OBJECT
44

45 46 47
public:
    explicit Syncer(QObject *parent = nullptr);

48 49 50
    /**
     * @brief setProviderAccount
     * sets the credentials to the current account
51
     * for the current provider being used
52 53 54 55 56 57 58 59 60
     * @param account
     * the account data represented by FMH::MODEL
     * where the valid keys are:
     * FMH::MODEL_KEY::USER user name
     * FMH::MODEL_KEY::PASSWORD users password
     * FMH::MODEL_KEY::PROVIDER the url to the provider server
     */
    void setAccount(const FMH::MODEL &account);

61 62 63 64 65 66 67 68 69
    /**
     * @brief setProvider
     * sets the provider interface
     * this allows to change the provider source
     * @param provider
     * the provider must inherit the asbtract class AbstractNotesProvider.
     * The value passed is then moved to this class private property Syncer::provider
     */
    void setProvider(AbstractNotesProvider *provider);
70 71 72 73 74


    //// NOTES INTERFACES
    /// interfaces with the the notes from both, local and remote

75 76 77
    /**
     * @brief insertNote
     * saves a new note online and offline
78
     * The signal Syncer::noteInserted(FMH::MODEL, STATE) is emitted,
79 80 81 82 83 84 85 86 87
     * indicating the created note and the transaction resulting state
     * @param note
     * the note to be stored represented by FMH::MODEL
     */
    void insertNote(FMH::MODEL &note);

    /**
     * @brief updateNote
     * Update online and offline an existing note.
88
     * The signal Syncer::noteUpdated(FMH::MODEL, STATE) is emitted,
89 90 91 92 93 94 95 96 97 98 99
     * indicating the updated note and the transaction resulting state
     * @param id
     * ID of the existing note
     * @param note
     * the new note contents represented by FMH::MODEL
     */
    void updateNote(const QString &id, const FMH::MODEL &note);

    /**
     * @brief removeNote
     * remove a note from online and offline storage
100
     * The signal Syncer::noteRemoved(FMH::MODEL, STATE) is emitted,
101 102 103 104 105 106 107 108 109
     * indicating the removed note and the transaction resulting state
     * @param id
     * ID of the exisiting  note
     */
    void removeNote(const QString &id);

    /**
     * @brief getNote
     * Retrieves an existing note, whether the note is located offline or online.
110
     * When the note is ready the signal Syncer::noteReady(FMH::MODEL) is emitted
111 112 113 114 115 116 117 118
     * @param id
     * ID of the exisiting  note
     */
    void getNote(const QString &id);

    /**
     * @brief getNotes
     * Retrieves all the notes, online and offline notes.
119
     * When the notes are ready the signal Syncer::notesReady(FMH::MODEL_LIST) is emitted.
120 121 122
     */
    void getNotes();

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    ////BOOKS & BOOKLET INTERFACES
    /// interfaces with the the books and booklets from both, local and remote

    /**
     * @brief getBooks
     * Retrieves all the books, online and offline.
     * When the books are ready the signal Syncer::booksReady(FMH::MODEL_LIST) is emitted
     */
    void getBooks();

    /**
     * @brief getBook
     * @param id
     */
    void getBook(const QString &id);

    /**
     * @brief insertBook
     * @param book
     */
143
    void insertBook(FMH::MODEL &book);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163

    /**
     * @brief updateBook
     * @param id
     * @param book
     */
    void updateBook(const QString &id, const FMH::MODEL &book);

    /**
     * @brief removeBook
     * @param id
     */
    void removeBook(const QString &id);

    //BOOKLETS INTERFACES

    /**
     * @brief getBooklet
     * @param id
     */
164
    void getBooklet(const QString &bookId);
165 166 167 168 169 170

    /**
     * @brief updateBooklet
     * @param id
     * @param booklet
     */
171
    void updateBooklet(const QString &id, const QString &bookId, FMH::MODEL &booklet);
172 173 174 175 176

    /**
     * @brief insertBooklet
     * @param booklet
     */
camilo higuita's avatar
camilo higuita committed
177
    void insertBooklet(const QString &bookId, FMH::MODEL &booklet);
178 179 180 181 182 183 184

    /**
     * @brief removeBooklet
     * @param id
     */
    void removeBooklet(const QString &id);

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
private:
    /**
     * @brief tag
     * Instance of the Maui project tag-ger. It adds tags to the abtract notes
     * For online tagging one could use the categories ?
     */
    Tagging *tag;

    /**
     * @brief db
     * Instance to the data base storing the notes information and location,
     * offline and online.
     */
    DB *db;

    /**
     * @brief server
     * Abstract instance to the online server to perfom CRUD actions
     */
    AbstractNotesProvider *provider;

    /**
     * @brief syncNote
     * Has the job to sync a note between the offline and online versions
     * @param id
     * ID of the note to be synced
     */
    void syncNote(const QString &id);

    /**
     * @brief stampNote
     * Adds an stamp id to identify the note offline and online
     * @param note
     * the note model is passed by ref and a STAMP key value is inserted
     */
220
    static void addId(FMH::MODEL &model);
221

222
    static const QString noteIdFromStamp(DB *_db, const QString &provider, const QString &stamp) ;
223 224 225 226 227
    static const QString noteStampFromId(DB *_db, const QString &id);

    static const QString bookletIdFromStamp(DB *_db, const QString &provider, const QString &stamp) ;
    static const QString bookletStampFromId(DB *_db, const QString &id) ;

228 229

    void setConections();
230 231 232 233 234 235 236 237 238 239

protected:
    /**
     * @brief insertLocal
     * performs the insertion of a new note in the local storage
     * @param note
     * note to be inserted
     * @return bool
     * true if the note was inserted sucessfully in the local storage
     */
240
    bool insertNoteLocal(FMH::MODEL &note);
241 242 243 244 245 246 247

    /**
     * @brief insertRemote
     * perfroms the insertion of a new note in the remote provider server
     * @param note
     * the note to be inserted
     */
248 249 250 251 252 253 254 255 256 257 258 259 260
    void insertNoteRemote(FMH::MODEL &note);
    bool updateNoteLocal(const QString &id, const FMH::MODEL &note);
    void updateNoteRemote(const QString &id, const FMH::MODEL &note);
    bool removeNoteLocal(const QString &id);
    void removeNoteRemote(const QString &id);

    bool insertBookLocal(FMH::MODEL &book);
    void insertBookRemote(FMH::MODEL &book);
    bool updateBookLocal(const QString &id, const FMH::MODEL &book);
    void updateBookRemote(const QString &id, const FMH::MODEL &book);
    bool removeBookLocal(const QString &id);
    void removeBookRemote(const QString &id);

camilo higuita's avatar
camilo higuita committed
261 262
    bool insertBookletLocal(const QString &bookId, FMH::MODEL &booklet);
    void insertBookletRemote(const QString &bookId, FMH::MODEL &booklet);
263
    bool updateBookletLocal(const QString &id, const QString &bookId, const FMH::MODEL &booklet);
264
    void updateBookletRemote(const QString &id, const QString &bookId, FMH::MODEL &booklet);
265 266
    bool removeBookletLocal(const QString &id);
    void removeBookletRemote(const QString &id);
camilo higuita's avatar
camilo higuita committed
267

268
    const FMH::MODEL_LIST collectAllNotes();
269
    const FMH::MODEL_LIST collectAllBooks();
270

camilo higuita's avatar
camilo higuita committed
271
    static inline const QUrl saveNoteFile(const QString &dir, const FMH::MODEL &data);
272 273
    static inline const QString noteFileContent(const QUrl &path);

274
signals:
275
    //FOR NOTES
276 277 278 279 280 281
    void noteInserted(FMH::MODEL note, STATE state);
    void noteUpdated(FMH::MODEL note, STATE state);
    void noteRemoved(FMH::MODEL note, STATE state);
    void noteReady(FMH::MODEL note);
    void notesReady(FMH::MODEL_LIST notes);

282 283 284 285 286 287 288
    //FOR BOOKS
    void bookInserted(FMH::MODEL book, STATE state);
    void bookUpdated(FMH::MODEL book, STATE state);
    void bookRemoved(FMH::MODEL book, STATE state);
    void bookReady(FMH::MODEL book);
    void booksReady(FMH::MODEL_LIST books);

camilo higuita's avatar
camilo higuita committed
289 290 291 292 293 294
    //FOR BOOKLETS
    void bookletInserted(FMH::MODEL booklet, STATE state);
    void bookletUpdated(FMH::MODEL booklet, STATE state);
    void bookletRemoved(FMH::MODEL booklet, STATE state);
    void bookletReady(FMH::MODEL_LIST booklets);

295

296 297 298
public slots:
};

299

300
#endif // SYNCER_H