Commit 69feb1db authored by camilo higuita's avatar camilo higuita

now also allows to upload notes

parent ee76c7d7
...@@ -5,8 +5,10 @@ ...@@ -5,8 +5,10 @@
#ifdef STATIC_MAUIKIT #ifdef STATIC_MAUIKIT
#include "tagging.h" #include "tagging.h"
#include "fm.h"
#else #else
#include <MauiKit/tagging.h> #include <MauiKit/tagging.h>
#include <MauiKit/fm.h>
#endif #endif
Notes::Notes(QObject *parent) : MauiList(parent), Notes::Notes(QObject *parent) : MauiList(parent),
...@@ -119,6 +121,8 @@ bool Notes::insert(const QVariantMap &note) ...@@ -119,6 +121,8 @@ bool Notes::insert(const QVariantMap &note)
emit postItemAppended(); emit postItemAppended();
this->syncer->insertNote(FM::toModel(note_map));
return true; return true;
} else qDebug()<< "NOTE COULD NOT BE INSTED"; } else qDebug()<< "NOTE COULD NOT BE INSTED";
......
...@@ -36,14 +36,14 @@ public: ...@@ -36,14 +36,14 @@ public:
* When the process is done it shoudl emit the noteReady(FMH::MODEL) signal * When the process is done it shoudl emit the noteReady(FMH::MODEL) signal
*/ */
// virtual FMH::MODEL getNote(const QString &id) = 0; // virtual FMH::MODEL getNote(const QString &id) = 0;
virtual void getNote(const QString &id) const = 0; virtual void getNote(const QString &id) = 0;
/** /**
* @brief getNotes * @brief getNotes
* returns all the notes or queried notes * returns all the notes or queried notes
* When the process is done it shoudl emit the notesReady(FMH::MODEL_LIST) signal * When the process is done it shoudl emit the notesReady(FMH::MODEL_LIST) signal
*/ */
virtual void getNotes() {} virtual void getNotes() = 0;
// virtual void getNotes() const {} // virtual void getNotes() const {}
// virtual FMH::MODEL_LIST getNotes(const QString &query = QString()) = 0; // virtual FMH::MODEL_LIST getNotes(const QString &query = QString()) = 0;
// virtual FMH::MODEL_LIST getNotes(const QString &query = QString()) const = 0; // virtual FMH::MODEL_LIST getNotes(const QString &query = QString()) const = 0;
...@@ -57,7 +57,7 @@ public: ...@@ -57,7 +57,7 @@ public:
* When the process is done it shoudl emit the noteInserted(FMH::MODEL) signal * When the process is done it shoudl emit the noteInserted(FMH::MODEL) signal
*/ */
// virtual bool insertNote(const FMH::MODEL &note) = 0; // virtual bool insertNote(const FMH::MODEL &note) = 0;
virtual void insertNote(const FMH::MODEL &note) const = 0; virtual void insertNote(const FMH::MODEL &note) = 0;
/** /**
* @brief updateNote * @brief updateNote
...@@ -69,7 +69,7 @@ public: ...@@ -69,7 +69,7 @@ public:
* When the process is done it shoudl emit the noteUpdated(FMH::MODEL) signal * When the process is done it shoudl emit the noteUpdated(FMH::MODEL) signal
*/ */
// virtual bool updateNote(const QString &id, const FMH::MODEL &note) = 0; // virtual bool updateNote(const QString &id, const FMH::MODEL &note) = 0;
virtual void updateNote(const QString &id, const FMH::MODEL &note) const = 0; virtual void updateNote(const QString &id, const FMH::MODEL &note) = 0;
/** /**
* @brief removeNote * @brief removeNote
...@@ -79,7 +79,7 @@ public: ...@@ -79,7 +79,7 @@ public:
* When the process is done it shoudl emit the noteRemoved(FMH::MODEL) signal * When the process is done it shoudl emit the noteRemoved(FMH::MODEL) signal
*/ */
// virtual bool removeNote(const QString &id) = 0; // virtual bool removeNote(const QString &id) = 0;
virtual void removeNote(const QString &id) const = 0; virtual void removeNote(const QString &id) = 0;
protected: protected:
QString m_user = ""; QString m_user = "";
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include <MauiKit/fm.h> #include <MauiKit/fm.h>
#endif #endif
QString NextNote::API = "https://PROVIDER/index.php/apps/notes/api/v0.2/"; const QString NextNote::API = "https://PROVIDER/index.php/apps/notes/api/v0.2/";
NextNote::NextNote(QObject *parent) : AbstractNotesSyncer(parent) NextNote::NextNote(QObject *parent) : AbstractNotesSyncer(parent)
{ {
...@@ -22,13 +22,30 @@ NextNote::~NextNote() ...@@ -22,13 +22,30 @@ NextNote::~NextNote()
{ {
} }
void NextNote::getNote(const QString &id) const void NextNote::getNote(const QString &id)
{ {
auto url = QString(NextNote::API+"%1, %2").replace("PROVIDER", this->m_provider).arg("notes/", id);
QString concatenated = this->m_user + ":" + this->m_password;
QByteArray data = concatenated.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
QMap<QString, QString> header {{"Authorization", headerData.toLocal8Bit()}};
auto downloader = new FMH::Downloader;
connect(downloader, &FMH::Downloader::dataReady, [&, downloader = std::move(downloader)](QByteArray array)
{
const auto notes = this->parseNotes(array);
emit this->noteReady(notes.isEmpty() ? FMH::MODEL() : notes.first());
downloader->deleteLater();
});
downloader->getArray(url, header);
} }
void NextNote::sendNotes(QByteArray array) void NextNote::sendNotes(QByteArray array)
{ {
// emit this->notesReady(notes); // emit this->notesReady(notes);
} }
void NextNote::getNotes() void NextNote::getNotes()
...@@ -52,15 +69,45 @@ void NextNote::getNotes() ...@@ -52,15 +69,45 @@ void NextNote::getNotes()
downloader->getArray(url, header); downloader->getArray(url, header);
} }
void NextNote::insertNote(const FMH::MODEL &note) const void NextNote::insertNote(const FMH::MODEL &note)
{ {
QByteArray payload=QJsonDocument::fromVariant(FM::toMap(note)).toJson();
qDebug() << "UPLOADING NEW NOT" << QVariant(payload).toString();
auto url = QString(NextNote::API+"%1").replace("PROVIDER", this->m_provider).arg("notes");
QString concatenated = this->m_user + ":" + this->m_password;
QByteArray data = concatenated.toLocal8Bit().toBase64();
QString headerData = "Basic " + data;
QVariantMap headers
{
{"Authorization", headerData.toLocal8Bit()},
{QString::number(QNetworkRequest::ContentTypeHeader),"application/json"}
};
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setHeader(QNetworkRequest::ContentTypeHeader,"application/json");
request.setRawHeader(QString("Authorization").toLocal8Bit(), headerData.toLocal8Bit());
QNetworkAccessManager *restclient; //in class
restclient = new QNetworkAccessManager(this); //constructor
QNetworkReply *reply = restclient->post(request,payload);
connect(reply, &QNetworkReply::finished, [=]()
{
qDebug() << "Note insertyion finished?";
qDebug() << reply->readAll();
});
} }
void NextNote::updateNote(const QString &id, const FMH::MODEL &note) const void NextNote::updateNote(const QString &id, const FMH::MODEL &note)
{ {
} }
void NextNote::removeNote(const QString &id) const void NextNote::removeNote(const QString &id)
{ {
} }
......
...@@ -16,14 +16,14 @@ class NextNote : public AbstractNotesSyncer ...@@ -16,14 +16,14 @@ class NextNote : public AbstractNotesSyncer
public: public:
explicit NextNote(QObject *parent = nullptr); explicit NextNote(QObject *parent = nullptr);
~NextNote(); ~NextNote();
void getNote(const QString &id) const override final; void getNote(const QString &id) override final;
void getNotes() override final; void getNotes() override final;
void insertNote(const FMH::MODEL &note) const override final; void insertNote(const FMH::MODEL &note) override final;
void updateNote(const QString &id, const FMH::MODEL &note) const override final; void updateNote(const QString &id, const FMH::MODEL &note) override final;
void removeNote(const QString &id) const override final; void removeNote(const QString &id) override final;
private: private:
static QString API; const static QString API;
static QString formatUrl(const QString &user, const QString &password, const QString &provider); static QString formatUrl(const QString &user, const QString &password, const QString &provider);
static FMH::MODEL_LIST parseNotes(const QByteArray &array); static FMH::MODEL_LIST parseNotes(const QByteArray &array);
......
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