forked from jeena/FeedTheMonkey
made possible to use doOperation with callback, added setRead, added settings for window position and size
This commit is contained in:
parent
ddb54f398c
commit
41e762445f
8 changed files with 94 additions and 47 deletions
|
@ -2,6 +2,8 @@ TEMPLATE = app
|
|||
|
||||
QT += qml quick
|
||||
|
||||
CONFIG += c++11
|
||||
|
||||
SOURCES += main.cpp \
|
||||
tinytinyrss.cpp \
|
||||
tinytinyrsslogin.cpp \
|
||||
|
|
|
@ -50,8 +50,13 @@ ScrollView {
|
|||
|
||||
onCurrentItemChanged: {
|
||||
if(previousPost) {
|
||||
previousPost.read = true;
|
||||
if(!previousPost.dontChangeRead) {
|
||||
previousPost.read = true;
|
||||
} else {
|
||||
previousPost.dontChangeRead = false;
|
||||
}
|
||||
}
|
||||
|
||||
item.content.post = server.posts[currentIndex]
|
||||
content.flickableItem.contentY = 0
|
||||
|
||||
|
|
|
@ -31,7 +31,8 @@ MenuBar {
|
|||
MenuItem {
|
||||
text: qsTr("Reload")
|
||||
shortcut: "R"
|
||||
enabled: false
|
||||
enabled: true
|
||||
onTriggered: server.reload()
|
||||
}
|
||||
MenuItem {
|
||||
text: qsTr("Show &Starred")
|
||||
|
@ -47,7 +48,10 @@ MenuBar {
|
|||
text: qsTr("Set &Unread")
|
||||
shortcut: "U"
|
||||
enabled: true
|
||||
onTriggered: content.post.read = false
|
||||
onTriggered: {
|
||||
content.post.dontChangeRead = true
|
||||
content.post.read = false
|
||||
}
|
||||
}
|
||||
MenuItem {
|
||||
text: qsTr("Next")
|
||||
|
|
14
main.qml
14
main.qml
|
@ -1,6 +1,8 @@
|
|||
import QtQuick 2.3
|
||||
import QtQuick.Controls 1.3
|
||||
import QtQuick.Window 2.0
|
||||
import QtQuick.Layouts 1.1
|
||||
import Qt.labs.settings 1.0
|
||||
import TTRSS 1.0
|
||||
|
||||
ApplicationWindow {
|
||||
|
@ -8,15 +10,17 @@ ApplicationWindow {
|
|||
title: "FeedMonkey"
|
||||
visible: true
|
||||
|
||||
contentItem.minimumWidth: 640
|
||||
contentItem.minimumHeight: 480
|
||||
contentItem.implicitWidth: 1024
|
||||
contentItem.implicitHeight: 800
|
||||
|
||||
property Server server: server
|
||||
property Sidebar sidebar: sidebar
|
||||
property Content content: content
|
||||
|
||||
Settings {
|
||||
property alias x: app.x
|
||||
property alias y: app.y
|
||||
property alias width: app.width
|
||||
property alias height: app.height
|
||||
}
|
||||
|
||||
menuBar: TheMenuBar {
|
||||
id: menu
|
||||
server: server
|
||||
|
|
14
post.cpp
14
post.cpp
|
@ -11,7 +11,7 @@ Post::Post(QJsonObject post, QObject *parent) : QObject(parent)
|
|||
{
|
||||
mTitle = post.value("title").toString().trimmed();
|
||||
mFeedTitle = post.value("feed_title").toString().trimmed();
|
||||
mId = post.value("id").toString().trimmed();
|
||||
mId = post.value("id").toInt();
|
||||
mFeedId = post.value("feed_id").toString().trimmed();
|
||||
mAuthor = post.value("author").toString().trimmed();
|
||||
QUrl url(post.value("link").toString().trimmed());
|
||||
|
@ -23,6 +23,7 @@ Post::Post(QJsonObject post, QObject *parent) : QObject(parent)
|
|||
mExcerpt = post.value("excerpt").toString().remove(QRegExp("<[^>]*>")).replace("…", " ...").trimmed().replace("(\\s+)", " ").replace("\n", "");
|
||||
mStarred = post.value("marked").toBool();
|
||||
mRead = !post.value("unread").toBool();
|
||||
mDontChangeRead = false;
|
||||
|
||||
QJsonDocument doc(post);
|
||||
QString result(doc.toJson(QJsonDocument::Indented));
|
||||
|
@ -36,6 +37,17 @@ Post::~Post()
|
|||
|
||||
void Post::setRead(bool r)
|
||||
{
|
||||
if(mRead == r) return;
|
||||
|
||||
mRead = r;
|
||||
emit readChanged(mRead);
|
||||
}
|
||||
|
||||
void Post::setDontChangeRead(bool r)
|
||||
{
|
||||
qDebug() << "setDontChangeRead " << r << " " << mDontChangeRead;
|
||||
if(mDontChangeRead == r) return;
|
||||
|
||||
mDontChangeRead = r;
|
||||
emit dontChangeReadChanged(mDontChangeRead);
|
||||
}
|
||||
|
|
11
post.h
11
post.h
|
@ -11,7 +11,7 @@ class Post : public QObject
|
|||
Q_OBJECT
|
||||
Q_PROPERTY(QString title READ title CONSTANT)
|
||||
Q_PROPERTY(QString feedTitle READ feedTitle CONSTANT)
|
||||
Q_PROPERTY(QString id READ id CONSTANT)
|
||||
Q_PROPERTY(int id READ id CONSTANT)
|
||||
Q_PROPERTY(QString feedId READ feedId CONSTANT)
|
||||
Q_PROPERTY(QString author READ author CONSTANT)
|
||||
Q_PROPERTY(QUrl link READ link CONSTANT)
|
||||
|
@ -20,6 +20,7 @@ class Post : public QObject
|
|||
Q_PROPERTY(QString excerpt READ excerpt CONSTANT)
|
||||
Q_PROPERTY(bool starred READ starred NOTIFY starredChanged)
|
||||
Q_PROPERTY(bool read READ read WRITE setRead NOTIFY readChanged)
|
||||
Q_PROPERTY(bool dontChangeRead READ dontChangeRead WRITE setDontChangeRead NOTIFY dontChangeReadChanged)
|
||||
Q_PROPERTY(QString jsonString READ jsonString CONSTANT)
|
||||
|
||||
public:
|
||||
|
@ -28,7 +29,7 @@ public:
|
|||
~Post();
|
||||
QString title() const { return mTitle; }
|
||||
QString feedTitle() const { return mFeedTitle; }
|
||||
QString id() const { return mId; }
|
||||
int id() const { return mId; }
|
||||
QString feedId() const { return mFeedId; }
|
||||
QString author() const { return mAuthor; }
|
||||
QUrl link() const { return mLink; }
|
||||
|
@ -38,18 +39,21 @@ public:
|
|||
bool starred() const { return mStarred; }
|
||||
bool read() { return mRead; }
|
||||
void setRead(bool r);
|
||||
bool dontChangeRead() const { return mDontChangeRead; }
|
||||
void setDontChangeRead(bool r);
|
||||
QString jsonString() const { return mJsonString; }
|
||||
|
||||
signals:
|
||||
void starredChanged(bool);
|
||||
void readChanged(bool);
|
||||
void dontChangeReadChanged(bool);
|
||||
|
||||
public slots:
|
||||
|
||||
private:
|
||||
QString mTitle;
|
||||
QString mFeedTitle;
|
||||
QString mId;
|
||||
int mId;
|
||||
QString mFeedId;
|
||||
QString mAuthor;
|
||||
QUrl mLink;
|
||||
|
@ -58,6 +62,7 @@ private:
|
|||
QString mExcerpt;
|
||||
bool mStarred;
|
||||
bool mRead;
|
||||
bool mDontChangeRead;
|
||||
QString mJsonString;
|
||||
};
|
||||
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
#include "tinytinyrss.h"
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkRequest>
|
||||
#include <QJsonArray>
|
||||
|
@ -16,7 +15,7 @@ TinyTinyRSS::TinyTinyRSS(QObject *parent) :
|
|||
|
||||
TinyTinyRSS::~TinyTinyRSS()
|
||||
{
|
||||
mPosts.empty();
|
||||
mPosts.clear();
|
||||
delete mNetworkManager;
|
||||
}
|
||||
|
||||
|
@ -36,10 +35,24 @@ void TinyTinyRSS::reload()
|
|||
opts.insert("feed_id", -4);
|
||||
opts.insert("skip", 0);
|
||||
|
||||
doOperation("getHeadlines", opts);
|
||||
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, this);
|
||||
connect(post, SIGNAL(readChanged(bool)), this, SLOT(onPostReadChanged(bool)));
|
||||
mPosts.append(post);
|
||||
}
|
||||
|
||||
emit postsChanged(mPosts);
|
||||
});
|
||||
}
|
||||
|
||||
void TinyTinyRSS::doOperation(QString operation, QVariantMap opts)
|
||||
void TinyTinyRSS::doOperation(QString operation, QVariantMap opts, std::function<void (const QJsonObject &json)> callback)
|
||||
{
|
||||
QVariantMap options;
|
||||
options.insert("sid", mSessionId);
|
||||
|
@ -58,42 +71,41 @@ void TinyTinyRSS::doOperation(QString operation, QVariantMap opts)
|
|||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
||||
|
||||
QNetworkReply *reply = mNetworkManager->post(request, json.toJson());
|
||||
connect(reply, SIGNAL(finished()), this, SLOT(reply()));
|
||||
}
|
||||
|
||||
void TinyTinyRSS::reply()
|
||||
{
|
||||
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
|
||||
|
||||
if (reply) {
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
QString jsonString = QString(reply->readAll());
|
||||
QJsonDocument json = QJsonDocument::fromJson(jsonString.toUtf8());
|
||||
|
||||
QJsonArray posts = json.object().value("content").toArray();
|
||||
|
||||
for(int i = 0; i <= posts.count(); i++)
|
||||
{
|
||||
QJsonObject postJson = posts.at(i).toObject();
|
||||
Post *post = new Post(postJson, this);
|
||||
connect(post, SIGNAL(readChanged(bool)), this, SLOT(onPostReadChanged(bool)));
|
||||
mPosts.append(post);
|
||||
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;
|
||||
}
|
||||
|
||||
emit postsChanged(mPosts);
|
||||
|
||||
} else {
|
||||
int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
//do some error management
|
||||
qWarning() << "HTTP error: " << httpStatus;
|
||||
reply->deleteLater();
|
||||
}
|
||||
reply->deleteLater();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void TinyTinyRSS::onPostReadChanged(bool r)
|
||||
{
|
||||
qDebug() << r;
|
||||
Post *post = (Post *)sender();
|
||||
|
||||
updateArticle(post->id(), 2, !r, [post] (const QJsonObject &json) {
|
||||
qDebug() << json;
|
||||
// not doing anything with this yet.
|
||||
});
|
||||
}
|
||||
|
||||
void TinyTinyRSS::updateArticle(int articleId, int field, bool trueFalse, std::function<void (const QJsonObject &json)> callback)
|
||||
{
|
||||
QVariantMap opts;
|
||||
opts.insert("article_ids", articleId);
|
||||
opts.insert("field", field);
|
||||
opts.insert("mode", trueFalse ? 1 : 0);
|
||||
|
||||
doOperation("updateArticle", opts, callback);
|
||||
}
|
||||
|
||||
QQmlListProperty<Post> TinyTinyRSS::posts()
|
||||
|
|
|
@ -6,6 +6,9 @@
|
|||
#include <QNetworkReply>
|
||||
#include <QList>
|
||||
#include <QQmlListProperty>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "post.h"
|
||||
|
||||
|
@ -30,11 +33,11 @@ signals:
|
|||
void postsChanged(QList<Post *>);
|
||||
|
||||
private slots:
|
||||
void reply();
|
||||
void onPostReadChanged(bool);
|
||||
|
||||
private:
|
||||
void doOperation(QString operation, QVariantMap opts);
|
||||
void doOperation(QString operation, QVariantMap opts, std::function<void (const QJsonObject &json)> callback);
|
||||
void updateArticle(int articleId, int field, bool trueFalse, std::function<void (const QJsonObject &json)> callback);
|
||||
|
||||
QString mServerUrl;
|
||||
QString mSessionId;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue