Commit 0dca2ce9 authored by camilo higuita's avatar camilo higuita

syncing booklets is now working... still needs some mroe work

parent 77dbd6a8
......@@ -19,9 +19,8 @@ FOREIGN KEY(id) REFERENCES NOTES(id)
);
CREATE TABLE IF NOT EXISTS BOOKS (
id TEXT PRIMARY KEY,
title TEXT PRIMARY KEY,
url TEXT,
title TEXT NOT NULL,
favorite INT,
adddate DATE,
modified DATE
......@@ -35,7 +34,16 @@ title TEXT NOT NULL,
adddate DATE,
modified DATE,
PRIMARY KEY(id, book),
FOREIGN KEY(book) REFERENCES BOOKS(id)
FOREIGN KEY(book) REFERENCES BOOKS(title)
);
CREATE TABLE IF NOT EXISTS BOOKLETS_SYNC (
id TEXT,
server TEXT,
user TEXT,
stamp TEXT,
PRIMARY KEY(server, stamp),
FOREIGN KEY(id) REFERENCES BOOKLETS(id)
);
CREATE TABLE IF NOT EXISTS LINKS (
......
#include "booklet.h"
#include "syncer.h"
#include "nextnote.h"
Booklet::Booklet(Syncer *_syncer, QObject *parent) : MauiList(parent),
syncer(_syncer)
{
{
connect(this->syncer, &Syncer::bookletReady, [&](FMH::MODEL_LIST booklets)
{
emit this->preListChanged();
......@@ -43,7 +45,7 @@ QString Booklet::getBook() const
return m_book;
}
void Booklet::setBook(const QString &book) //book id
void Booklet::setBook(const QString &book) //book id title
{
if (m_book == book)
return;
......
......@@ -3,13 +3,27 @@
#include "nextnote.h"
#include "booklet.h"
#ifdef STATIC_MAUIKIT
#include "mauiaccounts.h"
#include "mauiapp.h"
#else
#include <MauiKit/mauiapp.h>
#include <MauiKit/mauiaccounts.h>
#endif
Books::Books(QObject *parent) : MauiList(parent),
syncer(new Syncer(this)), m_booklet(new Booklet(syncer, this))
{
this->syncer->setProvider(new NextNote);
connect(this, &Books::currentBookChanged, this, &Books::openBook);
const auto m_account = MauiApp::instance()->getAccounts();
connect(m_account, &MauiAccounts::currentAccountChanged, [&](QVariantMap currentAccount)
{
Q_UNUSED(currentAccount)
this->syncer->getBooks();
});
connect(this, &Books::currentBookChanged, this, &Books::openBook);
connect(syncer, &Syncer::booksReady, [&](FMH::MODEL_LIST books)
{
emit this->preListChanged();
......@@ -103,7 +117,7 @@ void Books::openBook(const int &index)
if(index >= this->m_list.size() || index < 0)
return;
this->m_booklet->setBook(this->m_list.at(index)[FMH::MODEL_KEY::ID]);
this->m_booklet->setBook(this->m_list.at(index)[FMH::MODEL_KEY::TITLE]);
this->m_booklet->setBookTitle(this->m_list.at(index)[FMH::MODEL_KEY::TITLE]);
}
......
......@@ -5,9 +5,13 @@
#ifdef STATIC_MAUIKIT
#include "tagging.h"
#include "fm.h"
#include "mauiaccounts.h"
#include "mauiapp.h"
#else
#include <MauiKit/tagging.h>
#include <MauiKit/fm.h>
#include <MauiKit/mauiapp.h>
#include <MauiKit/mauiaccounts.h>
#endif
Notes::Notes(QObject *parent) : MauiList(parent),
......@@ -17,7 +21,13 @@ Notes::Notes(QObject *parent) : MauiList(parent),
this->syncer->setProvider(new NextNote);
connect(this, &Notes::accountChanged, syncer, &Syncer::getNotes);
const auto m_account = MauiApp::instance()->getAccounts();
connect(m_account, &MauiAccounts::currentAccountChanged, [&](QVariantMap currentAccount)
{
Q_UNUSED(currentAccount)
this->syncer->getNotes();
});
connect(this, &Notes::sortByChanged, this, &Notes::sortList);
connect(this, &Notes::orderChanged, this, &Notes::sortList);
......@@ -183,18 +193,6 @@ bool Notes::remove(const int &index)
return true;
}
void Notes::setAccount(const QVariantMap &account)
{
this->m_account = account;
syncer->setAccount(FM::toModel(this->m_account));
emit accountChanged();
}
QVariantMap Notes::getAccount() const
{
return this->m_account;
}
QVariantList Notes::getTags(const int &index)
{
// if(index < 0 || index >= this->notes.size())
......
......@@ -18,7 +18,6 @@ class Notes : public MauiList
Q_OBJECT
Q_PROPERTY(SORTBY sortBy READ getSortBy WRITE setSortBy NOTIFY sortByChanged)
Q_PROPERTY(ORDER order READ getOrder WRITE setOrder NOTIFY orderChanged)
Q_PROPERTY(QVariantMap account READ getAccount WRITE setAccount NOTIFY accountChanged)
public:
enum ORDER : uint8_t
......@@ -47,9 +46,6 @@ public:
void setOrder(const ORDER &order);
ORDER getOrder() const;
void setAccount(const QVariantMap &account);
QVariantMap getAccount() const;
private:
Syncer *syncer;
......@@ -64,7 +60,6 @@ private:
signals:
void orderChanged();
void sortByChanged();
void accountChanged();
public slots:
QVariantList getTags(const int &index);
......
......@@ -60,6 +60,7 @@ public:
*/
// virtual FMH::MODEL getNote(const QString &id) = 0;
virtual void getNote(const QString &id) = 0;
virtual void getBooklet(const QString &id) = 0;
/**
* @brief getNotes
......@@ -67,6 +68,7 @@ public:
* When the process is done it shoudl emit the notesReady(FMH::MODEL_LIST) signal
*/
virtual void getNotes() = 0;
virtual void getBooklets() = 0;
// virtual void getNotes() const {}
// virtual FMH::MODEL_LIST getNotes(const QString &query = QString()) = 0;
// virtual FMH::MODEL_LIST getNotes(const QString &query = QString()) const = 0;
......@@ -81,6 +83,7 @@ public:
*/
// virtual bool insertNote(const FMH::MODEL &note) = 0;
virtual void insertNote(const FMH::MODEL &note) = 0;
virtual void insertBooklet(const FMH::MODEL &booklet) = 0;
/**
* @brief updateNote
......@@ -93,6 +96,7 @@ public:
*/
// virtual bool updateNote(const QString &id, const FMH::MODEL &note) = 0;
virtual void updateNote(const QString &id, const FMH::MODEL &note) = 0;
virtual void updateBooklet(const QString &id, const FMH::MODEL &booklet) = 0;
/**
* @brief removeNote
......@@ -103,6 +107,7 @@ public:
*/
// virtual bool removeNote(const QString &id) = 0;
virtual void removeNote(const QString &id) = 0;
virtual void removeBooklet(const QString &id) = 0;
protected:
QString m_user = "";
......@@ -125,11 +130,20 @@ protected:
}
signals:
void noteReady(FMH::MODEL note);
void notesReady(FMH::MODEL_LIST notes);
void noteInserted(FMH::MODEL note);
void noteUpdated(FMH::MODEL note);
void noteRemoved();
void noteReady(FMH::MODEL note);
void bookletReady(FMH::MODEL booklet);
void notesReady(FMH::MODEL_LIST notes);
void bookletsReady(FMH::MODEL_LIST booklets);
void noteInserted(FMH::MODEL note);
void bookletInserted(FMH::MODEL booklet);
void noteUpdated(FMH::MODEL note);
void bookletUpdated(FMH::MODEL booklet);
void noteRemoved();
void bookletRemoved();
/**
* @brief responseReady
......
......@@ -66,6 +66,11 @@ void NextNote::getNote(const QString &id)
downloader->getArray(url, header);
}
void NextNote::getBooklet(const QString &id)
{
}
void NextNote::sendNotes(QByteArray array)
{
// emit this->notesReady(notes);
......@@ -84,7 +89,39 @@ void NextNote::getNotes()
const auto downloader = new FMH::Downloader;
connect(downloader, &FMH::Downloader::dataReady, [&, downloader = std::move(downloader)](QByteArray array)
{
emit this->notesReady(this->parseNotes(array));
//exclude notes that have its own category
FMH::MODEL_LIST notes;
for(const auto &data : this->parseNotes(array))
if(data[FMH::MODEL_KEY::CATEGORY].isEmpty() || data[FMH::MODEL_KEY::CATEGORY].isNull())
notes << data;
emit this->notesReady(notes);
downloader->deleteLater();
});
downloader->getArray(url, header);
}
void NextNote::getBooklets()
{
auto url = NextNote::formatUrl(this->m_user, this->m_password, this->m_provider)+"notes";
QString concatenated = this->m_user + ":" + this->m_password;
QByteArray data = concatenated.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
QMap<QString, QString> header {{"Authorization", headerData.toLocal8Bit()}};
const auto downloader = new FMH::Downloader;
connect(downloader, &FMH::Downloader::dataReady, [&, downloader = std::move(downloader)](QByteArray array)
{
//exclude notes that have its own category
FMH::MODEL_LIST booklets;
for(const auto &data : this->parseNotes(array))
if(!data[FMH::MODEL_KEY::CATEGORY].isEmpty() && !data[FMH::MODEL_KEY::CATEGORY].isNull())
booklets << data;
emit this->bookletsReady(booklets);
downloader->deleteLater();
});
......@@ -123,6 +160,38 @@ void NextNote::insertNote(const FMH::MODEL &note)
});
}
void NextNote::insertBooklet(const FMH::MODEL &booklet)
{
QByteArray payload = QJsonDocument::fromVariant(FM::toMap(booklet)).toJson();
qDebug() << "UPLOADING NEW BOOKLET" << QVariant(payload).toString();
const auto url = QString(NextNote::API+"%1").replace("PROVIDER", this->m_provider).arg("notes");
const auto request = formRequest(url, this->m_user, this->m_password);
auto restclient = new QNetworkAccessManager; //constructor
QNetworkReply *reply = restclient->post(request,payload);
connect(reply, &QNetworkReply::finished, [=, __booklet = booklet]()
{
qDebug() << "Note insertyion finished?";
const auto notes = this->parseNotes(reply->readAll());
emit this->bookletInserted([&]() -> FMH::MODEL {
FMH::MODEL note;
if(!notes.isEmpty())
{
note = notes.first();
note[FMH::MODEL_KEY::STAMP] = note[FMH::MODEL_KEY::ID]; //adds the id of the local note as a stamp
note[FMH::MODEL_KEY::ID] = __booklet[FMH::MODEL_KEY::ID]; //adds the id of the local note as a stamp
note[FMH::MODEL_KEY::SERVER] = this->m_provider; //adds the provider server address
note[FMH::MODEL_KEY::USER] = this->m_user; //adds the user name
}
return note;
}());
restclient->deleteLater();
});
}
void NextNote::updateNote(const QString &id, const FMH::MODEL &note)
{
if(id.isEmpty() || note.isEmpty())
......@@ -166,6 +235,50 @@ void NextNote::updateNote(const QString &id, const FMH::MODEL &note)
});
}
void NextNote::updateBooklet(const QString &id, const FMH::MODEL &booklet)
{
if(id.isEmpty() || booklet.isEmpty())
{
qWarning()<< "The id or note are empty. Can not proceed. NextNote::update";
return;
}
QByteArray payload = QJsonDocument::fromVariant(FM::toMap(FMH::filterModel(booklet, {FMH::MODEL_KEY::CONTENT,
FMH::MODEL_KEY::FAVORITE,
FMH::MODEL_KEY::MODIFIED,
FMH::MODEL_KEY::CATEGORY}))).toJson();
qDebug() << "UPDATING BOOKLET" << QVariant(payload).toString();
const auto url = QString(NextNote::API+"%1%2").replace("PROVIDER", this->m_provider).arg("notes/", id);
qDebug()<< "tryiong to update note" << url;
const auto request = formRequest(url, this->m_user, this->m_password);
auto restclient = new QNetworkAccessManager; //constructor
QNetworkReply *reply = restclient->put(request, payload);
connect(reply, &QNetworkReply::finished, [=, __booklet = booklet]()
{
qDebug() << "Note update finished?" << reply->errorString();
const auto booklets = this->parseNotes(reply->readAll());
emit this->bookletUpdated([&]() -> FMH::MODEL {
FMH::MODEL booklet;
if(booklets.isEmpty())
return booklet;
booklet = booklets.first();
booklet[FMH::MODEL_KEY::STAMP] = booklet[FMH::MODEL_KEY::ID]; //adds the stamp to the local note form the remote id
booklet[FMH::MODEL_KEY::ID] = __booklet[FMH::MODEL_KEY::TITLE]; //adds back the id of the local booklet
booklet[FMH::MODEL_KEY::SERVER] = this->m_provider; //adds the provider server address
booklet[FMH::MODEL_KEY::USER] = this->m_user; //adds the user name
return booklet;
}());
restclient->deleteLater();
});
}
void NextNote::removeNote(const QString &id)
{
if(id.isEmpty())
......@@ -187,6 +300,11 @@ void NextNote::removeNote(const QString &id)
});
}
void NextNote::removeBooklet(const QString &id)
{
}
const QString NextNote::formatUrl(const QString &user, const QString &password, const QString &provider)
{
auto url = NextNote::API;
......
......@@ -17,10 +17,19 @@ public:
explicit NextNote(QObject *parent = nullptr);
~NextNote() override final;
void getNote(const QString &id) override final;
void getBooklet(const QString &id) override final;
void getNotes() override final;
void getBooklets() override final;
void insertNote(const FMH::MODEL &note) override final;
void insertBooklet(const FMH::MODEL &booklet) override final;
void updateNote(const QString &id, const FMH::MODEL &note) override final;
void updateBooklet(const QString &id, const FMH::MODEL &booklet) override final;
void removeNote(const QString &id) override final;
void removeBooklet(const QString &id) override final;
private:
const static QString API;
......
This diff is collapsed.
......@@ -161,7 +161,7 @@ public:
* @brief getBooklet
* @param id
*/
void getBooklet(const QString &id);
void getBooklet(const QString &bookId);
/**
* @brief updateBooklet
......@@ -220,7 +220,11 @@ private:
static void addId(FMH::MODEL &model);
static const QString noteIdFromStamp(DB *_db, const QString &provider, const QString &stamp) ;
static const QString noteStampFromId(DB *_db, const QString &id) ;
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) ;
void setConections();
......@@ -257,7 +261,7 @@ protected:
bool insertBookletLocal(const QString &bookId, FMH::MODEL &booklet);
void insertBookletRemote(const QString &bookId, FMH::MODEL &booklet);
bool updateBookletLocal(const QString &id, const QString &bookId, const FMH::MODEL &booklet);
void updateBookletRemote(const QString &id, const QString &bookId, const FMH::MODEL &booklet);
void updateBookletRemote(const QString &id, const QString &bookId, FMH::MODEL &booklet);
bool removeBookletLocal(const QString &id);
void removeBookletRemote(const QString &id);
......
......@@ -22,6 +22,7 @@ namespace OWL
NOTES_SYNC,
BOOKS,
BOOKLETS,
BOOKLETS_SYNC,
LINKS,
NONE
};
......@@ -32,6 +33,7 @@ namespace OWL
{TABLE::NOTES_SYNC,"notes_sync"},
{TABLE::BOOKS,"books"},
{TABLE::BOOKLETS,"booklets"},
{TABLE::BOOKLETS_SYNC,"booklets_sync"},
{TABLE::LINKS,"links"},
};
......
......@@ -117,7 +117,6 @@ Maui.Page
Notes
{
id: notesList
account: root.currentAccount
}
Maui.BaseModel
......
......@@ -112,6 +112,8 @@ Row
Rectangle
{
Kirigami.Theme.inherit: false
Kirigami.Theme.colorSet: Kirigami.Theme.View
color: Kirigami.Theme.backgroundColor
anchors.verticalCenter: parent.verticalCenter
height: size
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment