added post
This commit is contained in:
parent
9cab3341b6
commit
5101a426c0
9 changed files with 139 additions and 33 deletions
|
@ -4,7 +4,8 @@ QT += qml quick
|
|||
|
||||
SOURCES += main.cpp \
|
||||
tinytinyrss.cpp \
|
||||
tinytinyrsslogin.cpp
|
||||
tinytinyrsslogin.cpp \
|
||||
post.cpp
|
||||
|
||||
RESOURCES += qml.qrc \
|
||||
html.qrc
|
||||
|
@ -25,4 +26,5 @@ OTHER_FILES += \
|
|||
|
||||
HEADERS += \
|
||||
tinytinyrss.h \
|
||||
tinytinyrsslogin.h
|
||||
tinytinyrsslogin.h \
|
||||
post.h
|
||||
|
|
5
main.cpp
5
main.cpp
|
@ -5,13 +5,16 @@
|
|||
#include <QtQml>
|
||||
|
||||
#include "tinytinyrsslogin.h"
|
||||
#include "tinytinyrss.h"
|
||||
#include "post.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QGuiApplication app(argc, argv);
|
||||
|
||||
qmlRegisterType<TinyTinyRSSLogin>("TTRSS", 1, 0, "ServerLogin");
|
||||
// qmlRegisterType<TinyTinyRSS>("TTRSS", 1, 0, "Server");
|
||||
qmlRegisterType<TinyTinyRSS>("TTRSS", 1, 0, "Server");
|
||||
qmlRegisterType<Post>("TTRSS", 1, 0, "Post");
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
|
||||
|
|
9
main.qml
9
main.qml
|
@ -28,6 +28,13 @@ ApplicationWindow {
|
|||
|
||||
ServerLogin {
|
||||
id: serverLogin
|
||||
onSessionIdChanged: login.visible = false
|
||||
onSessionIdChanged: {
|
||||
login.visible = false;
|
||||
server.initialize(serverUrl. sessionId);
|
||||
}
|
||||
}
|
||||
|
||||
Server {
|
||||
id: server
|
||||
}
|
||||
}
|
||||
|
|
20
post.cpp
Normal file
20
post.cpp
Normal file
|
@ -0,0 +1,20 @@
|
|||
#include "post.h"
|
||||
#include <QDebug>
|
||||
|
||||
Post::Post(QObject *parent) : QObject(parent),
|
||||
mStarred(false)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Post::Post(QJsonDocument post, QObject *parent) : QObject(parent),
|
||||
mStarred(false)
|
||||
{
|
||||
qDebug() << post;
|
||||
}
|
||||
|
||||
Post::~Post()
|
||||
{
|
||||
|
||||
}
|
||||
|
44
post.h
Normal file
44
post.h
Normal file
|
@ -0,0 +1,44 @@
|
|||
#ifndef POST_H
|
||||
#define POST_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QUrl>
|
||||
#include <QDate>
|
||||
#include <QJsonDocument>
|
||||
|
||||
class Post : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString title READ title)
|
||||
Q_PROPERTY(QString author READ author)
|
||||
Q_PROPERTY(QUrl link READ link)
|
||||
Q_PROPERTY(QDate date READ date)
|
||||
Q_PROPERTY(QString content READ content)
|
||||
Q_PROPERTY(bool starred READ starred NOTIFY starredChanged)
|
||||
|
||||
public:
|
||||
Post(QObject *parent = 0);
|
||||
Post(QJsonDocument post, QObject *parent = 0);
|
||||
~Post();
|
||||
QString title() const { return mTitle; }
|
||||
QString author() const { return mAuthor; }
|
||||
QUrl link() const { return mLink; }
|
||||
QDate date() const { return mDate; }
|
||||
QString content() const { return mContent; }
|
||||
bool starred() const { return mStarred; }
|
||||
|
||||
signals:
|
||||
void starredChanged(bool);
|
||||
|
||||
public slots:
|
||||
|
||||
private:
|
||||
QString mTitle;
|
||||
QString mAuthor;
|
||||
QUrl mLink;
|
||||
QDate mDate;
|
||||
QString mContent;
|
||||
bool mStarred;
|
||||
};
|
||||
|
||||
#endif // POST_H
|
|
@ -4,20 +4,27 @@
|
|||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkRequest>
|
||||
|
||||
TinyTinyRSS::TinyTinyRSS(QString serverUrl, QString sessionId)
|
||||
TinyTinyRSS::TinyTinyRSS(QObject *parent) :
|
||||
QObject(parent)
|
||||
{
|
||||
this->serverUrl = serverUrl;
|
||||
this->sessionId = sessionId;
|
||||
mNetworkManager = new QNetworkAccessManager(this);
|
||||
}
|
||||
|
||||
void TinyTinyRSS::initialize(const QString serverUrl, const QString sessionId)
|
||||
{
|
||||
mServerUrl = serverUrl;
|
||||
mSessionId = sessionId;
|
||||
}
|
||||
|
||||
TinyTinyRSS::~TinyTinyRSS()
|
||||
{
|
||||
|
||||
delete mNetworkManager;
|
||||
}
|
||||
|
||||
void TinyTinyRSS::doOperation(QString operation, QVariantMap opts) {
|
||||
void TinyTinyRSS::doOperation(QString operation, QVariantMap opts)
|
||||
{
|
||||
QVariantMap options;
|
||||
options.insert("sid", this->sessionId);
|
||||
options.insert("sid", mSessionId);
|
||||
options.insert("op", operation);
|
||||
|
||||
QMapIterator<QString, QVariant> i(opts);
|
||||
|
@ -29,16 +36,27 @@ void TinyTinyRSS::doOperation(QString operation, QVariantMap opts) {
|
|||
QJsonObject jsonobj = QJsonObject::fromVariantMap(options);
|
||||
QJsonDocument json = QJsonDocument(jsonobj);
|
||||
|
||||
QNetworkAccessManager *manager = new QNetworkAccessManager(this);
|
||||
|
||||
QUrl url(this->serverUrl + "/api/");
|
||||
QNetworkRequest request(url);
|
||||
QNetworkRequest request(mServerUrl);
|
||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
|
||||
|
||||
QObject::connect(manager, SIGNAL(finished(QNetworkReply)), this, SIGNAL(replyFinishedOperation(QNetworkReply)));
|
||||
manager->post(request, json.toBinaryData());
|
||||
QNetworkReply *reply = mNetworkManager->post(request, json.toBinaryData());
|
||||
connect(reply, SIGNAL(finished()), this, SLOT(reply()));
|
||||
}
|
||||
|
||||
void TinyTinyRSS::replyFinishedOperation(QNetworkReply *reply) {
|
||||
qWarning() << reply;
|
||||
void TinyTinyRSS::reply()
|
||||
{
|
||||
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
|
||||
|
||||
if (reply) {
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
//QJsonDocument json = QJsonDocument::fromBinaryData(reply->readAll());
|
||||
//mSessionId = json.toVariant().toMap().value("session_id");
|
||||
//emit sessionIdChanged(mSessionId);
|
||||
} else {
|
||||
int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
//do some error management
|
||||
qWarning() << "HTTP error: " << httpStatus;
|
||||
}
|
||||
reply->deleteLater();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,25 +2,38 @@
|
|||
#define TINYTINYRSS_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QQuickItem>
|
||||
#include <QMap>
|
||||
#include <QNetworkReply>
|
||||
#include <QList>
|
||||
|
||||
class TinyTinyRSS : QObject
|
||||
#include "post.h"
|
||||
|
||||
class TinyTinyRSS : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QList<Post> posts READ posts NOTIFY postsChanged)
|
||||
|
||||
public:
|
||||
TinyTinyRSS(QString serverUrl, QString sessionId);
|
||||
TinyTinyRSS(QObject *parent = 0);
|
||||
~TinyTinyRSS();
|
||||
void login();
|
||||
void replyFinishedOperation(QNetworkReply *reply);
|
||||
QList<Post> posts() const;
|
||||
|
||||
Q_INVOKABLE void initialize(const QString serverUrl, const QString sessionId);
|
||||
Q_INVOKABLE void reload();
|
||||
|
||||
signals:
|
||||
void postsChanged(QList<Post>);
|
||||
|
||||
private slots:
|
||||
void reply();
|
||||
|
||||
private:
|
||||
QString serverUrl;
|
||||
QString sessionId;
|
||||
|
||||
void doOperation(QString operation, QVariantMap opts);
|
||||
|
||||
QString mServerUrl;
|
||||
QString mSessionId;
|
||||
QList<Post> mPosts;
|
||||
QNetworkAccessManager *mNetworkManager;
|
||||
};
|
||||
|
||||
#endif // TINYTINYRSS_H
|
||||
|
|
|
@ -39,14 +39,14 @@ void TinyTinyRSSLogin::reply()
|
|||
|
||||
if (reply) {
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
//read data from reply
|
||||
QJsonDocument json = QJsonDocument::fromBinaryData(reply->readAll());
|
||||
mSessionId = json.toVariant().toMap().value("session_id").toString();
|
||||
emit sessionIdChanged(mSessionId);
|
||||
} else {
|
||||
//get http status code
|
||||
int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
//do some error management
|
||||
int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
//do some error management
|
||||
qWarning() << "HTTP error: " << httpStatus;
|
||||
}
|
||||
reply->deleteLater();
|
||||
}
|
||||
mSessionId = "1234";
|
||||
emit sessionIdChanged(mSessionId);
|
||||
}
|
||||
|
|
|
@ -30,7 +30,6 @@ private:
|
|||
QString mSessionId;
|
||||
QUrl mServerUrl;
|
||||
QNetworkAccessManager *mNetworkManager;
|
||||
|
||||
};
|
||||
|
||||
#endif // TINYTINYRSSLOGIN_H
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue