/*
* This file is part of FeedTheMonkey.
*
* Copyright 2015 Jeena
*
* FeedTheMonkey is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FeedTheMonkey is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FeedTheMonkey. If not, see .
*/
#include "tinytinyrss.h"
#include
#include
#include
#include
TinyTinyRSS::TinyTinyRSS(QObject *parent) :
QObject(parent)
{
qRegisterMetaType >();
mNetworkManager = new QNetworkAccessManager(this);
mPosts = QList();
}
TinyTinyRSS::~TinyTinyRSS()
{
mPosts.clear();
delete mNetworkManager;
}
void TinyTinyRSS::initialize(const QString serverUrl, const QString sessionId)
{
mServerUrl = serverUrl;
mSessionId = sessionId;
reload();
}
void TinyTinyRSS::reload()
{
QVariantMap opts;
opts.insert("show_excerpt", false);
opts.insert("view_mode", "unread");
opts.insert("show_content", true);
opts.insert("feed_id", -4);
opts.insert("skip", 0);
doOperation("getHeadlines", opts, [this] (const QJsonObject &json) {
mPosts.clear();
QJsonArray posts = json.value("content").toArray();
for(int i = 0; i < posts.count(); i++)
{
QJsonObject postJson = posts.at(i).toObject();
Post *post = new Post(postJson, mNetworkManager, this);
connect(post, SIGNAL(readChanged(bool)), this, SLOT(onPostReadChanged(bool)));
mPosts.append(post);
}
emit postsChanged(mPosts);
});
}
void TinyTinyRSS::loggedOut()
{
mServerUrl = nullptr;
mSessionId = nullptr;
mPosts.clear();
emit postsChanged(mPosts);
}
void TinyTinyRSS::doOperation(QString operation, QVariantMap opts, std::function callback)
{
QVariantMap options;
options.insert("sid", mSessionId);
options.insert("op", operation);
QMapIterator i(opts);
while (i.hasNext()) {
i.next();
options.insert(i.key(), i.value());
}
QJsonObject jsonobj = QJsonObject::fromVariantMap(options);
QJsonDocument json = QJsonDocument(jsonobj);
QNetworkRequest request(mServerUrl);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QNetworkReply *reply = mNetworkManager->post(request, json.toJson());
connect(reply, &QNetworkReply::finished, [callback, reply] () {
if (reply) {
if (reply->error() == QNetworkReply::NoError) {
QString jsonString = QString(reply->readAll());
QJsonDocument json = QJsonDocument::fromJson(jsonString.toUtf8());
callback(json.object());
} else {
int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
//do some error management
qWarning() << "HTTP error: " << httpStatus;
}
reply->deleteLater();
}
});
}
void TinyTinyRSS::onPostReadChanged(bool r)
{
Post *post = (Post *)sender();
updateArticle(post->id(), 2, !r, [post] (const QJsonObject &) {
// not doing anything with this yet.
});
}
void TinyTinyRSS::updateArticle(int articleId, int field, bool trueFalse, std::function callback)
{
QVariantMap opts;
opts.insert("article_ids", articleId);
opts.insert("field", field);
opts.insert("mode", trueFalse ? 1 : 0);
doOperation("updateArticle", opts, callback);
}
QQmlListProperty TinyTinyRSS::posts()
{
return QQmlListProperty(this, mPosts);
}
int TinyTinyRSS::postsCount() const
{
return mPosts.count();
}
Post *TinyTinyRSS::post(int index) const
{
return mPosts.at(index);
}