nextnote.cpp 8.12 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 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
const QString NextNote::API = QStringLiteral("https://PROVIDER/index.php/apps/notes/api/v0.2/");

static const inline QNetworkRequest formRequest(const QUrl &url, const  QString &user, const QString &password)
{
    if(!url.isValid() && !user.isEmpty() && !password.isEmpty())
        return QNetworkRequest();

    const QString concatenated = user + ":" + password;
    const QByteArray data = concatenated.toLocal8Bit().toBase64();
    const 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());

    return request;
}
38

39
NextNote::NextNote(QObject *parent) : AbstractNotesProvider(parent)
40 41 42 43 44 45 46 47
{

}

NextNote::~NextNote()
{
}

48
void NextNote::getNote(const QString &id)
49
{
50
    auto url = QString(NextNote::API+"%1%2").replace("PROVIDER", this->m_provider).arg("notes/", id);
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

    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);
67 68
}

69 70
void NextNote::sendNotes(QByteArray array)
{
71
    //    emit this->notesReady(notes);
72 73
}

74 75 76 77 78 79 80 81 82 83
void NextNote::getNotes()
{
    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()}};

84
    const auto downloader = new FMH::Downloader;
85
    connect(downloader, &FMH::Downloader::dataReady, [&, downloader = std::move(downloader)](QByteArray array)
86
    {
87 88
        emit this->notesReady(this->parseNotes(array));
        downloader->deleteLater();
89
    });
90 91

    downloader->getArray(url, header);
92 93
}

94
void NextNote::insertNote(const FMH::MODEL &note)
95
{
96
    QByteArray payload = QJsonDocument::fromVariant(FM::toMap(note)).toJson();
97 98
    qDebug() << "UPLOADING NEW NOT" << QVariant(payload).toString();

99
    const auto url = QString(NextNote::API+"%1").replace("PROVIDER", this->m_provider).arg("notes");
100

101
    const auto request = formRequest(url, this->m_user, this->m_password);
102

103
    auto restclient = new QNetworkAccessManager; //constructor
104
    QNetworkReply *reply = restclient->post(request,payload);
105
    connect(reply, &QNetworkReply::finished, [=, __note = note]()
106 107 108
    {
        qDebug() << "Note insertyion finished?";
        const auto notes = this->parseNotes(reply->readAll());
109 110 111 112 113 114 115 116 117 118 119 120 121 122
        emit this->noteInserted([&]() -> 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] = __note[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();
123
    });
124 125
}

126
void NextNote::updateNote(const QString &id, const FMH::MODEL &note)
127
{
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    if(id.isEmpty() || note.isEmpty())
    {
        qWarning()<< "The id or note are empty. Can not proceed. NextNote::update";
        return;
    }

    QByteArray payload = QJsonDocument::fromVariant(FM::toMap(FMH::filterModel(note, {FMH::MODEL_KEY::CONTENT,
                                                                                      FMH::MODEL_KEY::FAVORITE,
                                                                                      FMH::MODEL_KEY::MODIFIED,
                                                                                      FMH::MODEL_KEY::CATEGORY}))).toJson();
    qDebug() << "UPDATING NOTE" << 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, [=, __note = note]()
    {
        qDebug() << "Note update finished?" << reply->errorString();
        const auto notes = this->parseNotes(reply->readAll());
        emit this->noteUpdated([&]() -> FMH::MODEL {
                                   FMH::MODEL note;
                                   if(notes.isEmpty())
                                   return note;

                                   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] = __note[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();
    });
167 168
}

169
void NextNote::removeNote(const QString &id)
170
{
camilo higuita's avatar
camilo higuita committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    if(id.isEmpty())
    {
        qWarning()<< "The id is empty. Can not proceed. NextNote::remove";
        return;
    }

    const auto url = QString(NextNote::API+"%1%2").replace("PROVIDER", this->m_provider).arg("notes/", id);
    const auto request = formRequest(url, this->m_user, this->m_password);

    auto restclient = new QNetworkAccessManager; //constructor
    QNetworkReply *reply = restclient->deleteResource(request);
    connect(reply, &QNetworkReply::finished, [=]()
    {
        qDebug() << "Note remove finished?" << reply->errorString();
        emit this->noteRemoved();
        restclient->deleteLater();
    });
188 189
}

190
const QString NextNote::formatUrl(const QString &user, const QString &password, const QString &provider)
191 192 193 194 195 196 197
{
    auto url = NextNote::API;
    url.replace("USER", user);
    url.replace("PASSWORD", password);
    url.replace("PROVIDER", provider);
    return url;
}
198

199
const FMH::MODEL_LIST NextNote::parseNotes(const QByteArray &array)
200 201 202 203 204 205 206 207 208 209 210 211
{
    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;
    }

212 213 214 215 216 217
    const auto data = jsonResponse.toVariant();

    if(data.isNull() || !data.isValid())
        return res;

    if(!data.toList().isEmpty())
218
    {
219 220
        for(const auto &map : data.toList())
            res << FM::toModel(map.toMap());
221
    }
222 223
    else
        res << FM::toModel(data.toMap());
224 225 226 227 228

    return res;
}