nextnote.cpp 4.23 KB
Newer Older
1
#include "nextnote.h"
2 3 4 5 6 7 8 9 10 11 12
#include <QUrl>
#include <QDomDocument>
#include <QJsonDocument>
#include <QJsonObject>
#include <QVariantMap>

#ifdef STATIC_MAUIKIT
#include "fm.h"
#else
#include <MauiKit/fm.h>
#endif
13

14
const QString NextNote::API = "https://PROVIDER/index.php/apps/notes/api/v0.2/";
15

16
NextNote::NextNote(QObject *parent) : AbstractNotesProvider(parent)
17 18 19 20 21 22 23 24
{

}

NextNote::~NextNote()
{
}

25
void NextNote::getNote(const QString &id)
26
{
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    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);
44 45
}

46 47
void NextNote::sendNotes(QByteArray array)
{
48
    //    emit this->notesReady(notes);
49 50
}

51 52 53 54 55 56 57 58 59 60 61
void NextNote::getNotes()
{
    //https://milo.h@aol.com:Corazon1corazon@free01.thegood.cloud/index.php/apps/notes/api/v0.2/notes
    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()}};

62 63
    auto downloader = new FMH::Downloader;
    connect(downloader, &FMH::Downloader::dataReady, [&, downloader = std::move(downloader)](QByteArray array)
64
    {
65 66
        emit this->notesReady(this->parseNotes(array));
        downloader->deleteLater();
67
    });
68 69

    downloader->getArray(url, header);
70 71
}

72
void NextNote::insertNote(const FMH::MODEL &note)
73
{
74 75 76
    QByteArray payload=QJsonDocument::fromVariant(FM::toMap(note)).toJson();
    qDebug() << "UPLOADING NEW NOT" << QVariant(payload).toString();

77
    const auto url = QString(NextNote::API+"%1").replace("PROVIDER", this->m_provider).arg("notes");
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

    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, [=]()
99 100 101 102 103 104
    {
        qDebug() << "Note insertyion finished?";
        const auto notes = this->parseNotes(reply->readAll());
        emit this->noteInserted(notes.isEmpty() ? FMH::MODEL() : notes.first());
        reply->deleteLater();
    });
105 106
}

107
void NextNote::updateNote(const QString &id, const FMH::MODEL &note)
108 109 110
{
}

111
void NextNote::removeNote(const QString &id)
112 113 114 115 116 117 118 119 120 121 122
{
}

QString NextNote::formatUrl(const QString &user, const QString &password, const QString &provider)
{
    auto url = NextNote::API;
    url.replace("USER", user);
    url.replace("PASSWORD", password);
    url.replace("PROVIDER", provider);
    return url;
}
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146

FMH::MODEL_LIST NextNote::parseNotes(const QByteArray &array)
{
    FMH::MODEL_LIST res;
    qDebug()<< "trying to parse notes" << array;
    QJsonParseError jsonParseError;
    QJsonDocument jsonResponse = QJsonDocument::fromJson(static_cast<QString>(array).toUtf8(), &jsonParseError);

    if (jsonParseError.error != QJsonParseError::NoError)
    {
        qDebug()<< "ERROR PARSING";
        return res;
    }

    auto notes = jsonResponse.toVariant();
    for(const auto &map : notes.toList())
    {
        res << FM::toModel(map.toMap());
    }

    return res;
}