restructured files into directories
This commit is contained in:
parent
b05b6e34a3
commit
b6c0d89f1f
25 changed files with 33 additions and 304 deletions
27
src/main.cpp
Normal file
27
src/main.cpp
Normal file
|
@ -0,0 +1,27 @@
|
|||
#include <QGuiApplication>
|
||||
#include <QQmlApplicationEngine>
|
||||
#include <qdebug.h>
|
||||
#include <QMetaType>
|
||||
#include <QtQml>
|
||||
#include <QIcon>
|
||||
|
||||
#include "tinytinyrsslogin.h"
|
||||
#include "tinytinyrss.h"
|
||||
#include "post.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QGuiApplication app(argc, argv);
|
||||
app.setOrganizationName("Jeena");
|
||||
app.setOrganizationDomain("jeena.net");
|
||||
app.setApplicationName("FeedTheMonkey");
|
||||
|
||||
qmlRegisterType<TinyTinyRSSLogin>("TTRSS", 1, 0, "ServerLogin");
|
||||
qmlRegisterType<TinyTinyRSS>("TTRSS", 1, 0, "Server");
|
||||
qmlRegisterType<Post>("TTRSS", 1, 0, "Post");
|
||||
|
||||
QQmlApplicationEngine engine;
|
||||
engine.load(QUrl(QStringLiteral("qrc:/qml/main.qml")));
|
||||
|
||||
return app.exec();
|
||||
}
|
53
src/post.cpp
Normal file
53
src/post.cpp
Normal file
|
@ -0,0 +1,53 @@
|
|||
#include "post.h"
|
||||
#include <QDebug>
|
||||
#include <QJsonDocument>
|
||||
|
||||
Post::Post(QObject *parent) : QObject(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
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").toInt();
|
||||
mFeedId = post.value("feed_id").toString().trimmed();
|
||||
mAuthor = post.value("author").toString().trimmed();
|
||||
QUrl url(post.value("link").toString().trimmed());
|
||||
mLink = url;
|
||||
QDateTime timestamp;
|
||||
timestamp.setTime_t(post.value("updated").toInt());
|
||||
mDate = timestamp;
|
||||
mContent = post.value("content").toString().trimmed();
|
||||
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));
|
||||
mJsonString = result;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
69
src/post.h
Normal file
69
src/post.h
Normal file
|
@ -0,0 +1,69 @@
|
|||
#ifndef POST_H
|
||||
#define POST_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QUrl>
|
||||
#include <QDate>
|
||||
#include <QJsonObject>
|
||||
|
||||
class Post : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString title READ title CONSTANT)
|
||||
Q_PROPERTY(QString feedTitle READ feedTitle 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)
|
||||
Q_PROPERTY(QDateTime date READ date CONSTANT)
|
||||
Q_PROPERTY(QString content READ content CONSTANT)
|
||||
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:
|
||||
Post(QObject *parent = 0);
|
||||
Post(QJsonObject post, QObject *parent = 0);
|
||||
~Post();
|
||||
QString title() const { return mTitle; }
|
||||
QString feedTitle() const { return mFeedTitle; }
|
||||
int id() const { return mId; }
|
||||
QString feedId() const { return mFeedId; }
|
||||
QString author() const { return mAuthor; }
|
||||
QUrl link() const { return mLink; }
|
||||
QDateTime date() const { return mDate; }
|
||||
QString content() const { return mContent; }
|
||||
QString excerpt() const { return mExcerpt; }
|
||||
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;
|
||||
int mId;
|
||||
QString mFeedId;
|
||||
QString mAuthor;
|
||||
QUrl mLink;
|
||||
QDateTime mDate;
|
||||
QString mContent;
|
||||
QString mExcerpt;
|
||||
bool mStarred;
|
||||
bool mRead;
|
||||
bool mDontChangeRead;
|
||||
QString mJsonString;
|
||||
};
|
||||
|
||||
#endif // POST_H
|
131
src/tinytinyrss.cpp
Normal file
131
src/tinytinyrss.cpp
Normal file
|
@ -0,0 +1,131 @@
|
|||
#include "tinytinyrss.h"
|
||||
#include <QJsonDocument>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkRequest>
|
||||
#include <QJsonArray>
|
||||
|
||||
TinyTinyRSS::TinyTinyRSS(QObject *parent) :
|
||||
QObject(parent)
|
||||
{
|
||||
qRegisterMetaType<QList<Post *> >();
|
||||
|
||||
mNetworkManager = new QNetworkAccessManager(this);
|
||||
mPosts = QList<Post *>();
|
||||
}
|
||||
|
||||
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, 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<void (const QJsonObject &json)> callback)
|
||||
{
|
||||
QVariantMap options;
|
||||
options.insert("sid", mSessionId);
|
||||
options.insert("op", operation);
|
||||
|
||||
QMapIterator<QString, QVariant> 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 &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()
|
||||
{
|
||||
return QQmlListProperty<Post>(this, mPosts);
|
||||
}
|
||||
|
||||
int TinyTinyRSS::postsCount() const
|
||||
{
|
||||
return mPosts.count();
|
||||
}
|
||||
|
||||
Post *TinyTinyRSS::post(int index) const
|
||||
{
|
||||
return mPosts.at(index);
|
||||
}
|
49
src/tinytinyrss.h
Normal file
49
src/tinytinyrss.h
Normal file
|
@ -0,0 +1,49 @@
|
|||
#ifndef TINYTINYRSS_H
|
||||
#define TINYTINYRSS_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QMap>
|
||||
#include <QNetworkReply>
|
||||
#include <QList>
|
||||
#include <QQmlListProperty>
|
||||
#include <QJsonObject>
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include "post.h"
|
||||
|
||||
class TinyTinyRSS : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QQmlListProperty<Post> posts READ posts NOTIFY postsChanged)
|
||||
|
||||
public:
|
||||
TinyTinyRSS(QObject *parent = 0);
|
||||
~TinyTinyRSS();
|
||||
|
||||
Q_INVOKABLE void initialize(const QString serverUrl, const QString sessionId);
|
||||
Q_INVOKABLE void reload();
|
||||
Q_INVOKABLE void loggedOut();
|
||||
|
||||
|
||||
QQmlListProperty<Post> posts();
|
||||
int postsCount() const;
|
||||
Post *post(int) const;
|
||||
|
||||
signals:
|
||||
void postsChanged(QList<Post *>);
|
||||
|
||||
private slots:
|
||||
void onPostReadChanged(bool);
|
||||
|
||||
private:
|
||||
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;
|
||||
QList<Post*> mPosts;
|
||||
QNetworkAccessManager *mNetworkManager;
|
||||
};
|
||||
|
||||
#endif // TINYTINYRSS_H
|
92
src/tinytinyrsslogin.cpp
Normal file
92
src/tinytinyrsslogin.cpp
Normal file
|
@ -0,0 +1,92 @@
|
|||
#include "tinytinyrsslogin.h"
|
||||
#include <QJsonDocument>
|
||||
#include <QJsonObject>
|
||||
#include <QNetworkReply>
|
||||
#include <QSettings>
|
||||
|
||||
#define APP_URL "net.jeena"
|
||||
#define APP_NAME "FeedTheMonkey"
|
||||
|
||||
TinyTinyRSSLogin::TinyTinyRSSLogin(QObject *parent) :
|
||||
QObject(parent)
|
||||
{
|
||||
mNetworkManager = new QNetworkAccessManager(this);
|
||||
|
||||
QSettings settings;
|
||||
mSessionId = settings.value("sessionId").toString();
|
||||
mServerUrl = settings.value("serverUrl").toString();
|
||||
}
|
||||
|
||||
TinyTinyRSSLogin::~TinyTinyRSSLogin()
|
||||
{
|
||||
delete mNetworkManager;
|
||||
}
|
||||
|
||||
bool TinyTinyRSSLogin::loggedIn()
|
||||
{
|
||||
return !mSessionId.isEmpty();
|
||||
}
|
||||
|
||||
void TinyTinyRSSLogin::login(const QString serverUrl, const QString user, const QString password)
|
||||
{
|
||||
mServerUrl = QUrl(serverUrl + "/api/");
|
||||
|
||||
QVariantMap options;
|
||||
options.insert("op", "login");
|
||||
options.insert("user", user);
|
||||
options.insert("password", password);
|
||||
|
||||
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, SIGNAL(finished()), this, SLOT(reply()));
|
||||
}
|
||||
|
||||
void TinyTinyRSSLogin::logout()
|
||||
{
|
||||
if(mSessionId.length() > 0 && mServerUrl.toString().length() > 0) {
|
||||
QVariantMap options;
|
||||
options.insert("op", "logout");
|
||||
options.insert("sid", mSessionId);
|
||||
|
||||
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, SIGNAL(finished()), this, SLOT(reply()));
|
||||
}
|
||||
}
|
||||
|
||||
void TinyTinyRSSLogin::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());
|
||||
mSessionId = json.object().value("content").toObject().value("session_id").toString();
|
||||
|
||||
emit sessionIdChanged(mSessionId);
|
||||
|
||||
QSettings settings;
|
||||
settings.setValue("sessionId", mSessionId);
|
||||
settings.setValue("serverUrl", mServerUrl);
|
||||
settings.sync();
|
||||
|
||||
} else {
|
||||
int httpStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
//do some error management
|
||||
qWarning() << "HTTP error: " << httpStatus << " :: " << reply->error();
|
||||
}
|
||||
reply->deleteLater();
|
||||
}
|
||||
}
|
37
src/tinytinyrsslogin.h
Normal file
37
src/tinytinyrsslogin.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
#ifndef TINYTINYRSSLOGIN_H
|
||||
#define TINYTINYRSSLOGIN_H
|
||||
|
||||
#include <QObject>
|
||||
#include <QMetaType>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkRequest>
|
||||
|
||||
class TinyTinyRSSLogin : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString sessionId READ sessionId NOTIFY sessionIdChanged)
|
||||
Q_PROPERTY(QUrl serverUrl READ serverUrl)
|
||||
|
||||
public:
|
||||
TinyTinyRSSLogin(QObject *parent = 0);
|
||||
~TinyTinyRSSLogin();
|
||||
QString sessionId() const { return mSessionId; }
|
||||
QUrl serverUrl() const { return mServerUrl; }
|
||||
|
||||
Q_INVOKABLE bool loggedIn();
|
||||
Q_INVOKABLE void login(const QString serverUrl, const QString user, const QString password);
|
||||
Q_INVOKABLE void logout();
|
||||
|
||||
signals:
|
||||
void sessionIdChanged(QString);
|
||||
|
||||
private slots:
|
||||
void reply();
|
||||
|
||||
private:
|
||||
QString mSessionId;
|
||||
QUrl mServerUrl;
|
||||
QNetworkAccessManager *mNetworkManager;
|
||||
};
|
||||
|
||||
#endif // TINYTINYRSSLOGIN_H
|
Loading…
Add table
Add a link
Reference in a new issue